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