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