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 | */ |
| 16 | |
| 17 | #include "Privacy.h" |
| 18 | |
| 19 | // DESTINATION enum value |
| 20 | const uint8_t DEST_LOCAL = 0; |
| 21 | const uint8_t DEST_EXPLICIT = 1; |
| 22 | const uint8_t DEST_AUTOMATIC = 2; |
| 23 | |
| 24 | // type of the field, identitical to protobuf definition |
| 25 | const uint8_t TYPE_STRING = 9; |
| 26 | const uint8_t TYPE_MESSAGE = 11; |
| 27 | |
| 28 | Privacy::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 | |
| 37 | Privacy::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 | |
| 46 | Privacy::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 | |
| 55 | bool |
| 56 | Privacy::IsMessageType() const { return type == TYPE_MESSAGE; } |
| 57 | |
| 58 | bool |
| 59 | Privacy::IsStringType() const { return type == TYPE_STRING; } |
| 60 | |
| 61 | bool |
| 62 | Privacy::HasChildren() const { return children != NULL && children[0] != NULL; } |
| 63 | |
| 64 | const Privacy* |
| 65 | Privacy::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 | |
| 76 | static 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 | |
| 90 | bool |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 91 | PrivacySpec::operator<(const PrivacySpec& other) const |
| 92 | { |
| 93 | return dest < other.dest; |
| 94 | } |
| 95 | |
| 96 | bool |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 97 | PrivacySpec::CheckPremission(const Privacy* privacy) const |
| 98 | { |
| 99 | uint8_t policy = privacy == NULL ? DEST_DEFAULT_VALUE : privacy->dest; |
| 100 | return allowDest(dest, policy); |
| 101 | } |
| 102 | |
| 103 | bool |
| 104 | PrivacySpec::RequireAll() const { return dest == DEST_LOCAL; } |
| 105 | |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 106 | PrivacySpec new_spec_from_args(int dest) { |
| 107 | if (dest < 0) return PrivacySpec(); |
| 108 | return PrivacySpec(dest); |
| 109 | } |
| 110 | |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 111 | PrivacySpec get_default_dropbox_spec() { return PrivacySpec(DEST_AUTOMATIC); } |