blob: c9ca9a752c9c42c34477cb46236886d01b86e132 [file] [log] [blame]
Yi Jin99c248f2017-08-25 18:11:58 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yi Jinc23fad22017-09-15 17:24:59 -070017#ifndef PRIVACY_BUFFER_H
18#define PRIVACY_BUFFER_H
Yi Jin99c248f2017-08-25 18:11:58 -070019
Yi Jin99c248f2017-08-25 18:11:58 -070020#include "Privacy.h"
21
Yi Jinc23fad22017-09-15 17:24:59 -070022#include <android/util/EncodedBuffer.h>
Yi Jin42711a02017-10-11 18:20:24 -070023#include <android/util/ProtoOutputStream.h>
Yi Jin99c248f2017-08-25 18:11:58 -070024#include <stdint.h>
Yi Jinc23fad22017-09-15 17:24:59 -070025#include <utils/Errors.h>
26
27using namespace android;
28using namespace android::util;
Yi Jin99c248f2017-08-25 18:11:58 -070029
30/**
Yi Jinc23fad22017-09-15 17:24:59 -070031 * PrivacyBuffer holds the original protobuf data and strips PII-sensitive fields
32 * based on the request and holds stripped data in its own buffer for output.
Yi Jin99c248f2017-08-25 18:11:58 -070033 */
Yi Jinc23fad22017-09-15 17:24:59 -070034class PrivacyBuffer
Yi Jin99c248f2017-08-25 18:11:58 -070035{
36public:
Yi Jinc23fad22017-09-15 17:24:59 -070037 PrivacyBuffer(const Privacy* policy, EncodedBuffer::iterator& data);
38 ~PrivacyBuffer();
Yi Jin99c248f2017-08-25 18:11:58 -070039
40 /**
41 * Strip based on the request and hold data in its own buffer. Return NO_ERROR if strip succeeds.
42 */
43 status_t strip(const PrivacySpec& spec);
44
45 /**
46 * Clear encoded buffer so it can be reused by another request.
47 */
48 void clear();
49
50 /**
51 * Return the size of the stripped data.
52 */
53 size_t size() const;
54
55 /**
56 * Flush buffer to the given fd. NO_ERROR is returned if the flush succeeds.
57 */
58 status_t flush(int fd);
59
60private:
Yi Jin99c248f2017-08-25 18:11:58 -070061 const Privacy* mPolicy;
Yi Jinc23fad22017-09-15 17:24:59 -070062 EncodedBuffer::iterator& mData;
63
Yi Jin42711a02017-10-11 18:20:24 -070064 ProtoOutputStream mProto;
Yi Jin99c248f2017-08-25 18:11:58 -070065 size_t mSize;
Yi Jin42711a02017-10-11 18:20:24 -070066
67 status_t stripField(const Privacy* parentPolicy, const PrivacySpec& spec);
68 void writeFieldOrSkip(uint32_t fieldTag, bool skip);
Yi Jin99c248f2017-08-25 18:11:58 -070069};
70
Yi Jinc23fad22017-09-15 17:24:59 -070071#endif // PRIVACY_BUFFER_H