blob: 6b6de9cd9823fdb610484ed51fcfa0d24e13d2ee [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
18#ifndef PRIVACY_H
19#define PRIVACY_H
20
21#include <stdint.h>
22
Yi Jinbdf58942017-11-14 17:58:19 -080023// This is the default value of DEST enum, sync with privacy.proto
Yi Jinb592e3b2018-02-01 15:17:04 -080024const uint8_t DEST_UNSET = 255; // DEST_UNSET is not exposed to libincident
Yi Jinbdf58942017-11-14 17:58:19 -080025const uint8_t DEST_DEFAULT_VALUE = DEST_UNSET;
Yi Jin99c248f2017-08-25 18:11:58 -070026
27/*
Yi Jinbdf58942017-11-14 17:58:19 -080028 * In order to NOT auto-generate large chuck of code by proto compiler in incidentd,
29 * privacy options's data structure are explicitly redefined here and
30 * the values are populated by incident_section_gen tool.
31 *
32 * Each proto field will have a Privacy when it is different from its parent, otherwise
33 * it uses its parent's tag. A message type will have an array of Privacy.
Yi Jin99c248f2017-08-25 18:11:58 -070034 */
35struct Privacy {
Yi Jinbdf58942017-11-14 17:58:19 -080036 // The field number
Yi Jin99c248f2017-08-25 18:11:58 -070037 uint32_t field_id;
Yi Jinbdf58942017-11-14 17:58:19 -080038
39 // The field type, see external/protobuf/src/google/protobuf/descriptor.h
Yi Jin99c248f2017-08-25 18:11:58 -070040 uint8_t type;
Yi Jinbdf58942017-11-14 17:58:19 -080041
42 // If children is null, it is a primitive field,
43 // otherwise it is a message field which could have overridden privacy tags here.
44 // This array is NULL-terminated.
Yi Jin7e0b4e52017-09-12 20:00:25 -070045 Privacy** children;
Yi Jin99c248f2017-08-25 18:11:58 -070046
Yi Jinbdf58942017-11-14 17:58:19 -080047 // DESTINATION Enum in frameworks/base/libs/incident/proto/android/privacy.proto.
Yi Jin99c248f2017-08-25 18:11:58 -070048 uint8_t dest;
Yi Jinbdf58942017-11-14 17:58:19 -080049 // A list of regexp rules for stripping string fields in proto.
50 const char** patterns;
Yi Jin99c248f2017-08-25 18:11:58 -070051};
52
Yi Jinbdf58942017-11-14 17:58:19 -080053// Encode field id used by ProtoOutputStream.
54uint64_t encode_field_id(const Privacy* p);
55
56// Look up the child with given fieldId, if not found, return NULL.
57const Privacy* lookup(const Privacy* p, uint32_t fieldId);
58
Yi Jin99c248f2017-08-25 18:11:58 -070059/**
60 * PrivacySpec defines the request has what level of privacy authorization.
61 * For example, a device without user consent should only be able to upload AUTOMATIC fields.
Yi Jinbdf58942017-11-14 17:58:19 -080062 * DEST_UNSET are treated as DEST_EXPLICIT.
Yi Jin99c248f2017-08-25 18:11:58 -070063 */
64class PrivacySpec {
65public:
66 const uint8_t dest;
67
68 PrivacySpec() : dest(DEST_DEFAULT_VALUE) {}
Yi Jin0f047162017-09-05 13:44:22 -070069 bool operator<(const PrivacySpec& other) const;
70
Yi Jinbdf58942017-11-14 17:58:19 -080071 // check permission of a policy, if returns true, don't strip the data.
Yi Jinb592e3b2018-02-01 15:17:04 -080072 bool CheckPremission(const Privacy* privacy,
73 const uint8_t defaultDest = DEST_DEFAULT_VALUE) const;
Yi Jinbdf58942017-11-14 17:58:19 -080074
75 // if returns true, no data need to be stripped.
Yi Jin99c248f2017-08-25 18:11:58 -070076 bool RequireAll() const;
Yi Jin99c248f2017-08-25 18:11:58 -070077
Yi Jin3ec5cc72018-01-26 13:42:43 -080078 // Constructs spec using static methods below.
79 static PrivacySpec new_spec(int dest);
Yi Jinb592e3b2018-02-01 15:17:04 -080080
Yi Jin3ec5cc72018-01-26 13:42:43 -080081private:
82 PrivacySpec(uint8_t dest) : dest(dest) {}
83};
Yi Jin99c248f2017-08-25 18:11:58 -070084
Yi Jinb592e3b2018-02-01 15:17:04 -080085#endif // PRIVACY_H