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 | |
Yi Jin | 7e0b4e5 | 2017-09-12 20:00:25 -0700 | [diff] [blame^] | 19 | #include <stdlib.h> |
| 20 | |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 21 | // DESTINATION enum value |
| 22 | const uint8_t DEST_LOCAL = 0; |
| 23 | const uint8_t DEST_EXPLICIT = 1; |
| 24 | const uint8_t DEST_AUTOMATIC = 2; |
| 25 | |
| 26 | // type of the field, identitical to protobuf definition |
| 27 | const uint8_t TYPE_STRING = 9; |
| 28 | const uint8_t TYPE_MESSAGE = 11; |
| 29 | |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 30 | bool |
| 31 | Privacy::IsMessageType() const { return type == TYPE_MESSAGE; } |
| 32 | |
| 33 | bool |
| 34 | Privacy::IsStringType() const { return type == TYPE_STRING; } |
| 35 | |
| 36 | bool |
| 37 | Privacy::HasChildren() const { return children != NULL && children[0] != NULL; } |
| 38 | |
| 39 | const Privacy* |
| 40 | Privacy::lookup(uint32_t fieldId) const |
| 41 | { |
| 42 | if (children == NULL) return NULL; |
| 43 | for (int i=0; children[i] != NULL; i++) { |
| 44 | if (children[i]->field_id == fieldId) return children[i]; |
| 45 | // This assumes the list's field id is in ascending order and must be true. |
| 46 | if (children[i]->field_id > fieldId) return NULL; |
| 47 | } |
| 48 | return NULL; |
| 49 | } |
| 50 | |
| 51 | static bool allowDest(const uint8_t dest, const uint8_t policy) |
| 52 | { |
| 53 | switch (policy) { |
| 54 | case DEST_LOCAL: |
| 55 | return dest == DEST_LOCAL; |
| 56 | case DEST_EXPLICIT: |
| 57 | return dest == DEST_LOCAL || dest == DEST_EXPLICIT; |
| 58 | case DEST_AUTOMATIC: |
| 59 | return true; |
| 60 | default: |
| 61 | return false; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | bool |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 66 | PrivacySpec::operator<(const PrivacySpec& other) const |
| 67 | { |
| 68 | return dest < other.dest; |
| 69 | } |
| 70 | |
| 71 | bool |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 72 | PrivacySpec::CheckPremission(const Privacy* privacy) const |
| 73 | { |
| 74 | uint8_t policy = privacy == NULL ? DEST_DEFAULT_VALUE : privacy->dest; |
| 75 | return allowDest(dest, policy); |
| 76 | } |
| 77 | |
| 78 | bool |
| 79 | PrivacySpec::RequireAll() const { return dest == DEST_LOCAL; } |
| 80 | |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 81 | PrivacySpec new_spec_from_args(int dest) { |
| 82 | if (dest < 0) return PrivacySpec(); |
| 83 | return PrivacySpec(dest); |
| 84 | } |
| 85 | |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 86 | PrivacySpec get_default_dropbox_spec() { return PrivacySpec(DEST_AUTOMATIC); } |