Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 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 | */ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 16 | #pragma once |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 17 | |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 18 | #ifndef PRIVACY_BUFFER_H |
| 19 | #define PRIVACY_BUFFER_H |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 20 | |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 21 | #include "Privacy.h" |
| 22 | |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 23 | #include <android/util/EncodedBuffer.h> |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 24 | #include <android/util/ProtoOutputStream.h> |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 25 | #include <stdint.h> |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 26 | #include <utils/Errors.h> |
| 27 | |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 28 | namespace android { |
| 29 | namespace os { |
| 30 | namespace incidentd { |
| 31 | |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 32 | using namespace android::util; |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 35 | * 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 Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 37 | */ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 38 | class PrivacyBuffer { |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 39 | public: |
Yi Jin | 86dce41 | 2018-03-07 11:36:57 -0800 | [diff] [blame] | 40 | PrivacyBuffer(const Privacy* policy, EncodedBuffer::iterator data); |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 41 | ~PrivacyBuffer(); |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 42 | |
| 43 | /** |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 44 | * Strip based on the request and hold data in its own buffer. Return NO_ERROR if strip |
| 45 | * succeeds. |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 46 | */ |
| 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 | |
| 64 | private: |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 65 | const Privacy* mPolicy; |
Yi Jin | 86dce41 | 2018-03-07 11:36:57 -0800 | [diff] [blame] | 66 | EncodedBuffer::iterator mData; |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 67 | |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 68 | ProtoOutputStream mProto; |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 69 | size_t mSize; |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 70 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 71 | status_t stripField(const Privacy* parentPolicy, const PrivacySpec& spec, int depth); |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 72 | void writeFieldOrSkip(uint32_t fieldTag, bool skip); |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 75 | } // namespace incidentd |
| 76 | } // namespace os |
| 77 | } // namespace android |
| 78 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 79 | #endif // PRIVACY_BUFFER_H |