blob: e7969e78e9e474de6a18eddc46910c18169990dd [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
33bool
34Privacy::IsStringType() const { return type == TYPE_STRING; }
35
36bool
37Privacy::HasChildren() const { return children != NULL && children[0] != NULL; }
38
39const Privacy*
40Privacy::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
51static 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
65bool
Yi Jin0f047162017-09-05 13:44:22 -070066PrivacySpec::operator<(const PrivacySpec& other) const
67{
68 return dest < other.dest;
69}
70
71bool
Yi Jin99c248f2017-08-25 18:11:58 -070072PrivacySpec::CheckPremission(const Privacy* privacy) const
73{
74 uint8_t policy = privacy == NULL ? DEST_DEFAULT_VALUE : privacy->dest;
75 return allowDest(dest, policy);
76}
77
78bool
79PrivacySpec::RequireAll() const { return dest == DEST_LOCAL; }
80
Yi Jin0f047162017-09-05 13:44:22 -070081PrivacySpec new_spec_from_args(int dest) {
82 if (dest < 0) return PrivacySpec();
83 return PrivacySpec(dest);
84}
85
Yi Jin99c248f2017-08-25 18:11:58 -070086PrivacySpec get_default_dropbox_spec() { return PrivacySpec(DEST_AUTOMATIC); }