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 | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame^] | 19 | #include <android/os/IncidentReportArgs.h> |
Yi Jin | 7e0b4e5 | 2017-09-12 20:00:25 -0700 | [diff] [blame] | 20 | #include <stdlib.h> |
| 21 | |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame^] | 22 | uint64_t encode_field_id(const Privacy* p) { return (uint64_t)p->type << 32 | p->field_id; } |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 23 | |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame^] | 24 | const Privacy* lookup(const Privacy* p, uint32_t fieldId) |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 25 | { |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame^] | 26 | if (p->children == NULL) return NULL; |
| 27 | for (int i=0; p->children[i] != NULL; i++) { // NULL-terminated. |
| 28 | if (p->children[i]->field_id == fieldId) return p->children[i]; |
| 29 | // Incident section gen tool guarantees field ids in ascending order. |
| 30 | if (p->children[i]->field_id > fieldId) return NULL; |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 31 | } |
| 32 | return NULL; |
| 33 | } |
| 34 | |
| 35 | static bool allowDest(const uint8_t dest, const uint8_t policy) |
| 36 | { |
| 37 | switch (policy) { |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame^] | 38 | case android::os::DEST_LOCAL: |
| 39 | return dest == android::os::DEST_LOCAL; |
| 40 | case android::os::DEST_EXPLICIT: |
| 41 | case DEST_UNSET: |
| 42 | return dest == android::os::DEST_LOCAL || |
| 43 | dest == android::os::DEST_EXPLICIT || |
| 44 | dest == DEST_UNSET; |
| 45 | case android::os::DEST_AUTOMATIC: |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 46 | return true; |
| 47 | default: |
| 48 | return false; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | bool |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 53 | PrivacySpec::operator<(const PrivacySpec& other) const |
| 54 | { |
| 55 | return dest < other.dest; |
| 56 | } |
| 57 | |
| 58 | bool |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame^] | 59 | PrivacySpec::CheckPremission(const Privacy* privacy, const uint8_t defaultDest) const |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 60 | { |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame^] | 61 | uint8_t policy = privacy != NULL ? privacy->dest : defaultDest; |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 62 | return allowDest(dest, policy); |
| 63 | } |
| 64 | |
| 65 | bool |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame^] | 66 | PrivacySpec::RequireAll() const { return dest == android::os::DEST_LOCAL; } |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 67 | |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame^] | 68 | PrivacySpec new_spec_from_args(int dest) |
| 69 | { |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 70 | if (dest < 0) return PrivacySpec(); |
| 71 | return PrivacySpec(dest); |
| 72 | } |
| 73 | |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame^] | 74 | PrivacySpec get_default_dropbox_spec() { return PrivacySpec(android::os::DEST_AUTOMATIC); } |