PrivacyBuffer.h revision 86dce413f808ca9ef160e8762f74deaafd7c23ae
1/*
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#pragma once
17
18#ifndef PRIVACY_BUFFER_H
19#define PRIVACY_BUFFER_H
20
21#include "Privacy.h"
22
23#include <android/util/EncodedBuffer.h>
24#include <android/util/ProtoOutputStream.h>
25#include <stdint.h>
26#include <utils/Errors.h>
27
28using namespace android;
29using namespace android::util;
30
31/**
32 * PrivacyBuffer holds the original protobuf data and strips PII-sensitive fields
33 * based on the request and holds stripped data in its own buffer for output.
34 */
35class PrivacyBuffer {
36public:
37    PrivacyBuffer(const Privacy* policy, EncodedBuffer::iterator data);
38    ~PrivacyBuffer();
39
40    /**
41     * Strip based on the request and hold data in its own buffer. Return NO_ERROR if strip
42     * succeeds.
43     */
44    status_t strip(const PrivacySpec& spec);
45
46    /**
47     * Clear encoded buffer so it can be reused by another request.
48     */
49    void clear();
50
51    /**
52     * Return the size of the stripped data.
53     */
54    size_t size() const;
55
56    /**
57     * Flush buffer to the given fd. NO_ERROR is returned if the flush succeeds.
58     */
59    status_t flush(int fd);
60
61private:
62    const Privacy* mPolicy;
63    EncodedBuffer::iterator mData;
64
65    ProtoOutputStream mProto;
66    size_t mSize;
67
68    status_t stripField(const Privacy* parentPolicy, const PrivacySpec& spec, int depth);
69    void writeFieldOrSkip(uint32_t fieldTag, bool skip);
70};
71
72#endif  // PRIVACY_BUFFER_H