blob: dbab5480e69822e16c3df1905047528e9676f0db [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
17#include "Privacy.h"
18
19// DESTINATION enum value
20const uint8_t DEST_LOCAL = 0;
21const uint8_t DEST_EXPLICIT = 1;
22const uint8_t DEST_AUTOMATIC = 2;
23
24// type of the field, identitical to protobuf definition
25const uint8_t TYPE_STRING = 9;
26const uint8_t TYPE_MESSAGE = 11;
27
28Privacy::Privacy(uint32_t field_id, uint8_t type, uint8_t dest)
29 : field_id(field_id),
30 type(type),
31 children(NULL),
32 dest(dest),
33 patterns(NULL)
34{
35}
36
37Privacy::Privacy(uint32_t field_id, const Privacy** children)
38 : field_id(field_id),
39 type(TYPE_MESSAGE),
40 children(children),
41 dest(DEST_DEFAULT_VALUE), // this will be ignored
42 patterns(NULL)
43{
44}
45
46Privacy::Privacy(uint32_t field_id, uint8_t dest, const char** patterns)
47 : field_id(field_id),
48 type(TYPE_STRING),
49 children(NULL),
50 dest(dest),
51 patterns(patterns)
52{
53}
54
55bool
56Privacy::IsMessageType() const { return type == TYPE_MESSAGE; }
57
58bool
59Privacy::IsStringType() const { return type == TYPE_STRING; }
60
61bool
62Privacy::HasChildren() const { return children != NULL && children[0] != NULL; }
63
64const Privacy*
65Privacy::lookup(uint32_t fieldId) const
66{
67 if (children == NULL) return NULL;
68 for (int i=0; children[i] != NULL; i++) {
69 if (children[i]->field_id == fieldId) return children[i];
70 // This assumes the list's field id is in ascending order and must be true.
71 if (children[i]->field_id > fieldId) return NULL;
72 }
73 return NULL;
74}
75
76static bool allowDest(const uint8_t dest, const uint8_t policy)
77{
78 switch (policy) {
79 case DEST_LOCAL:
80 return dest == DEST_LOCAL;
81 case DEST_EXPLICIT:
82 return dest == DEST_LOCAL || dest == DEST_EXPLICIT;
83 case DEST_AUTOMATIC:
84 return true;
85 default:
86 return false;
87 }
88}
89
90bool
Yi Jin0f047162017-09-05 13:44:22 -070091PrivacySpec::operator<(const PrivacySpec& other) const
92{
93 return dest < other.dest;
94}
95
96bool
Yi Jin99c248f2017-08-25 18:11:58 -070097PrivacySpec::CheckPremission(const Privacy* privacy) const
98{
99 uint8_t policy = privacy == NULL ? DEST_DEFAULT_VALUE : privacy->dest;
100 return allowDest(dest, policy);
101}
102
103bool
104PrivacySpec::RequireAll() const { return dest == DEST_LOCAL; }
105
Yi Jin0f047162017-09-05 13:44:22 -0700106PrivacySpec new_spec_from_args(int dest) {
107 if (dest < 0) return PrivacySpec();
108 return PrivacySpec(dest);
109}
110
Yi Jin99c248f2017-08-25 18:11:58 -0700111PrivacySpec get_default_dropbox_spec() { return PrivacySpec(DEST_AUTOMATIC); }