blob: eac3862b2cab0302648b92f2196bbb4621183810 [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 */
Yi Jinb592e3b2018-02-01 15:17:04 -080016#pragma once
Yi Jin99c248f2017-08-25 18:11:58 -070017
Yi Jinc23fad22017-09-15 17:24:59 -070018#ifndef PRIVACY_BUFFER_H
19#define PRIVACY_BUFFER_H
Yi Jin99c248f2017-08-25 18:11:58 -070020
Yi Jin99c248f2017-08-25 18:11:58 -070021#include "Privacy.h"
22
Yi Jinc23fad22017-09-15 17:24:59 -070023#include <android/util/EncodedBuffer.h>
Yi Jin42711a02017-10-11 18:20:24 -070024#include <android/util/ProtoOutputStream.h>
Yi Jin99c248f2017-08-25 18:11:58 -070025#include <stdint.h>
Yi Jinc23fad22017-09-15 17:24:59 -070026#include <utils/Errors.h>
27
Yi Jin6cacbcb2018-03-30 14:04:52 -070028namespace android {
29namespace os {
30namespace incidentd {
31
Yi Jinc23fad22017-09-15 17:24:59 -070032using namespace android::util;
Yi Jin99c248f2017-08-25 18:11:58 -070033
34/**
Yi Jinc23fad22017-09-15 17:24:59 -070035 * PrivacyBuffer holds the original protobuf data and strips PII-sensitive fields
36 * based on the request and holds stripped data in its own buffer for output.
Yi Jin99c248f2017-08-25 18:11:58 -070037 */
Yi Jinb592e3b2018-02-01 15:17:04 -080038class PrivacyBuffer {
Yi Jin99c248f2017-08-25 18:11:58 -070039public:
Yi Jin86dce412018-03-07 11:36:57 -080040 PrivacyBuffer(const Privacy* policy, EncodedBuffer::iterator data);
Yi Jinc23fad22017-09-15 17:24:59 -070041 ~PrivacyBuffer();
Yi Jin99c248f2017-08-25 18:11:58 -070042
43 /**
Yi Jinb592e3b2018-02-01 15:17:04 -080044 * Strip based on the request and hold data in its own buffer. Return NO_ERROR if strip
45 * succeeds.
Yi Jin99c248f2017-08-25 18:11:58 -070046 */
47 status_t strip(const PrivacySpec& spec);
48
49 /**
50 * Clear encoded buffer so it can be reused by another request.
51 */
52 void clear();
53
54 /**
55 * Return the size of the stripped data.
56 */
57 size_t size() const;
58
59 /**
60 * Flush buffer to the given fd. NO_ERROR is returned if the flush succeeds.
61 */
62 status_t flush(int fd);
63
64private:
Yi Jin99c248f2017-08-25 18:11:58 -070065 const Privacy* mPolicy;
Yi Jin86dce412018-03-07 11:36:57 -080066 EncodedBuffer::iterator mData;
Yi Jinc23fad22017-09-15 17:24:59 -070067
Yi Jin42711a02017-10-11 18:20:24 -070068 ProtoOutputStream mProto;
Yi Jin99c248f2017-08-25 18:11:58 -070069 size_t mSize;
Yi Jin42711a02017-10-11 18:20:24 -070070
Yi Jinb592e3b2018-02-01 15:17:04 -080071 status_t stripField(const Privacy* parentPolicy, const PrivacySpec& spec, int depth);
Yi Jin42711a02017-10-11 18:20:24 -070072 void writeFieldOrSkip(uint32_t fieldTag, bool skip);
Yi Jin99c248f2017-08-25 18:11:58 -070073};
74
Yi Jin6cacbcb2018-03-30 14:04:52 -070075} // namespace incidentd
76} // namespace os
77} // namespace android
78
Yi Jinb592e3b2018-02-01 15:17:04 -080079#endif // PRIVACY_BUFFER_H