Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Joe Onorato | 7669012 | 2016-12-20 08:18:32 -0800 | [diff] [blame] | 17 | #include <frameworks/base/core/proto/android/os/incident.pb.h> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 18 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 19 | #include <map> |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 20 | #include <set> |
Yi Jin | 0f2599f | 2017-11-16 18:19:45 -0800 | [diff] [blame] | 21 | #include <sstream> |
Kweku Adams | 71a9531 | 2018-04-16 16:54:24 -0700 | [diff] [blame^] | 22 | #include <string> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 23 | |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 24 | using namespace android; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 25 | using namespace android::os; |
| 26 | using namespace google::protobuf; |
| 27 | using namespace google::protobuf::io; |
| 28 | using namespace google::protobuf::internal; |
| 29 | using namespace std; |
| 30 | |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 31 | /** |
| 32 | * Implementation details: |
| 33 | * This binary auto generates .cpp files for incident and incidentd. |
| 34 | * |
| 35 | * When argument "incident" is specified, it generates incident_section.cpp file. |
| 36 | * |
| 37 | * When argument "incidentd" is specified, it generates section_list.cpp file. |
| 38 | * |
| 39 | * In section_list.cpp file, it generates a SECTION_LIST array and a PRIVACY_POLICY_LIST array. |
| 40 | * For SECTION_LIST, it generates Section.h classes only for proto fields with section option enabled. |
| 41 | * For PRIVACY_POLICY_LIST, it generates Privacy.h classes only for proto fields with privacy option enabled. |
| 42 | * |
| 43 | * For Privacy struct, it is possible to have self recursion definitions since protobuf is defining "classes" |
| 44 | * So the logic to handle it becomes very complicated when Privacy tag of a message contains a list of Privacies |
| 45 | * of its sub-messages. The code also handles multiple depth of self recursion fields. |
| 46 | * |
| 47 | * For example here is a one level self recursion message WindowManager: |
| 48 | * message WindowState { |
| 49 | * string state = 1 [(privacy).dest = LOCAL]; |
| 50 | * int32 display_id = 2; |
| 51 | * repeated WindowState child_windows = 3; |
| 52 | * } |
| 53 | * |
| 54 | * message WindowManager { |
| 55 | * WindowState my_window = 1; |
| 56 | * } |
| 57 | * |
| 58 | * When generating Privacy options for WindowManager, this tool will generate cpp syntax source code: |
| 59 | * |
| 60 | * #include "section_list.h" |
| 61 | * ... |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 62 | * Privacy WindowState__state { 1, 9, NULL, LOCAL, NULL }; // first two integers are values for field id and proto type. |
| 63 | * Privacy WindowState__child_windows { 3, 11, NULL, UNSET, NULL }; // reserved for WindowState_LIST |
| 64 | * Privacy* WindowState__MSG__UNSET[] = { |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 65 | * &WindowState_state, |
| 66 | * // display id is default, nothing is generated. |
| 67 | * &WindowState_child_windows, |
| 68 | * NULL // terminator of the array |
| 69 | * }; |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 70 | * Privacy WindowState__my_window { 1, 11, WindowState__MSG__UNSET, UNSET, NULL }; |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 71 | * |
| 72 | * createList() { |
| 73 | * ... |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 74 | * WindowState_child_windows.children = WindowState__MSG_UNSET; // point to its own definition after the list is defined. |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 75 | * ... |
| 76 | * } |
| 77 | * |
| 78 | * const Privacy** PRIVACY_POLICY_LIST = createList(); |
| 79 | * const int PRIVACY_POLICY_COUNT = 1; |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 80 | * |
| 81 | * Privacy Value Inheritance rules: |
| 82 | * 1. Both field and message can be tagged with DESTINATION: LOCAL(L), EXPLICIT(E), AUTOMATIC(A). |
| 83 | * 2. Primitives inherits containing message's tag unless defined explicitly. |
| 84 | * 3. Containing message's tag doesn't apply to message fields, even when unset (in this case, uses its default message tag). |
| 85 | * 4. Message field tag overrides its default message tag. |
| 86 | * 5. UNSET tag defaults to EXPLICIT. |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 87 | */ |
| 88 | |
| 89 | // The assignments will be called when constructs PRIVACY_POLICY_LIST, has to be global variable |
| 90 | vector<string> gSelfRecursionAssignments; |
| 91 | |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 92 | static inline void emptyline() { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 93 | printf("\n"); |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 94 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 95 | |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 96 | static void generateHead(const char* header) { |
| 97 | printf("// Auto generated file. Do not modify\n"); |
| 98 | emptyline(); |
| 99 | printf("#include \"%s.h\"\n", header); |
| 100 | emptyline(); |
| 101 | } |
| 102 | |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 103 | // ======================== incident_sections ============================= |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 104 | static bool generateIncidentSectionsCpp(Descriptor const* descriptor) |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 105 | { |
| 106 | generateHead("incident_sections"); |
| 107 | |
| 108 | map<string,FieldDescriptor const*> sections; |
| 109 | int N; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 110 | N = descriptor->field_count(); |
| 111 | for (int i=0; i<N; i++) { |
| 112 | const FieldDescriptor* field = descriptor->field(i); |
| 113 | if (field->type() == FieldDescriptor::TYPE_MESSAGE) { |
| 114 | sections[field->name()] = field; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | printf("IncidentSection const INCIDENT_SECTIONS[] = {\n"); |
| 119 | N = sections.size(); |
| 120 | int i = 0; |
| 121 | for (map<string,FieldDescriptor const*>::const_iterator it = sections.begin(); |
| 122 | it != sections.end(); it++, i++) { |
| 123 | const FieldDescriptor* field = it->second; |
| 124 | printf(" { %d, \"%s\" }", field->number(), field->name().c_str()); |
| 125 | if (i != N-1) { |
| 126 | printf(",\n"); |
| 127 | } else { |
| 128 | printf("\n"); |
| 129 | } |
| 130 | } |
| 131 | printf("};\n"); |
| 132 | |
| 133 | printf("const int INCIDENT_SECTION_COUNT = %d;\n", N); |
| 134 | |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 135 | return true; |
| 136 | } |
| 137 | |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 138 | // ========================= section_list =================================== |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 139 | static void splitAndPrint(const string& args) { |
| 140 | size_t base = 0; |
| 141 | size_t found; |
| 142 | while (true) { |
| 143 | found = args.find_first_of(" ", base); |
| 144 | if (found != base) { |
| 145 | string arg = args.substr(base, found - base); |
| 146 | printf(" \"%s\",", arg.c_str()); |
| 147 | } |
| 148 | if (found == args.npos) break; |
| 149 | base = found + 1; |
| 150 | } |
| 151 | } |
| 152 | |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 153 | static string replaceAll(const string& fieldName, const char oldC, const string& newS) { |
| 154 | if (fieldName.find_first_of(oldC) == fieldName.npos) return fieldName.c_str(); |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 155 | size_t pos = 0, idx = 0; |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 156 | char* res = new char[fieldName.size() * newS.size() + 1]; // assign a larger buffer |
| 157 | while (pos != fieldName.size()) { |
| 158 | char cur = fieldName[pos++]; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 159 | if (cur != oldC) { |
| 160 | res[idx++] = cur; |
| 161 | continue; |
| 162 | } |
| 163 | |
| 164 | for (size_t i=0; i<newS.size(); i++) { |
| 165 | res[idx++] = newS[i]; |
| 166 | } |
| 167 | } |
| 168 | res[idx] = '\0'; |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 169 | string result(res); |
Yunlian Jiang | 3809bbf | 2017-09-05 15:50:58 -0700 | [diff] [blame] | 170 | delete [] res; |
| 171 | return result; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 174 | static inline void printPrivacy(const string& name, const FieldDescriptor* field, const string& children, |
| 175 | const Destination dest, const string& patterns, const string& comments = "") { |
| 176 | printf("Privacy %s = { %d, %d, %s, %d, %s };%s\n", name.c_str(), field->number(), field->type(), |
| 177 | children.c_str(), dest, patterns.c_str(), comments.c_str()); |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 180 | // Get Custom Options ================================================================================ |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 181 | static inline SectionFlags getSectionFlags(const FieldDescriptor* field) { |
| 182 | return field->options().GetExtension(section); |
| 183 | } |
| 184 | |
| 185 | static inline PrivacyFlags getPrivacyFlags(const FieldDescriptor* field) { |
| 186 | return field->options().GetExtension(privacy); |
| 187 | } |
| 188 | |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 189 | static inline PrivacyFlags getPrivacyFlags(const Descriptor* descriptor) { |
| 190 | return descriptor->options().GetExtension(msg_privacy); |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 193 | // Get Destinations =================================================================================== |
| 194 | static inline Destination getMessageDest(const Descriptor* descriptor, const Destination overridden) { |
| 195 | return overridden != DEST_UNSET ? overridden : getPrivacyFlags(descriptor).dest(); |
| 196 | } |
| 197 | |
| 198 | // Returns field's own dest, when it is a message field, uses its message default tag if unset. |
| 199 | static inline Destination getFieldDest(const FieldDescriptor* field) { |
| 200 | Destination fieldDest = getPrivacyFlags(field).dest(); |
| 201 | return field->type() != FieldDescriptor::TYPE_MESSAGE ? fieldDest : |
| 202 | getMessageDest(field->message_type(), fieldDest); |
| 203 | } |
| 204 | |
Kweku Adams | ecf4bdb | 2018-02-22 15:05:48 -0800 | [diff] [blame] | 205 | // Converts Destination to a string. |
| 206 | static inline string getDestString(const Destination dest) { |
| 207 | switch (dest) { |
| 208 | case DEST_AUTOMATIC: return "AUTOMATIC"; |
| 209 | case DEST_LOCAL: return "LOCAL"; |
| 210 | case DEST_EXPLICIT: return "EXPLICIT"; |
| 211 | // UNSET is considered EXPLICIT by default. |
| 212 | case DEST_UNSET: return "EXPLICIT"; |
| 213 | default: return "UNKNOWN"; |
| 214 | } |
| 215 | } |
| 216 | |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 217 | // Get Names =========================================================================================== |
| 218 | static inline string getFieldName(const FieldDescriptor* field) { |
| 219 | // replace . with double underscores to avoid name conflicts since fields use snake naming convention |
| 220 | return replaceAll(field->full_name(), '.', "__"); |
| 221 | } |
| 222 | |
| 223 | |
| 224 | static inline string getMessageName(const Descriptor* descriptor, const Destination overridden) { |
| 225 | // replace . with one underscore since messages use camel naming convention |
| 226 | return replaceAll(descriptor->full_name(), '.', "_") + "__MSG__" + |
| 227 | to_string(getMessageDest(descriptor, overridden)); |
| 228 | } |
| 229 | |
| 230 | // IsDefault ============================================================================================ |
| 231 | // Returns true if a field is default. Default is defined as this field has same dest as its containing message. |
Kweku Adams | ecf4bdb | 2018-02-22 15:05:48 -0800 | [diff] [blame] | 232 | // For message fields, it only looks at its field tag and own default message tag, doesn't recursively go deeper. |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 233 | static inline bool isDefaultField(const FieldDescriptor* field, const Destination containerDest) { |
| 234 | Destination fieldDest = getFieldDest(field); |
| 235 | if (field->type() != FieldDescriptor::TYPE_MESSAGE) { |
| 236 | return fieldDest == containerDest || (fieldDest == DEST_UNSET); |
| 237 | } else { |
| 238 | return fieldDest == containerDest || |
| 239 | (containerDest == DEST_UNSET && fieldDest == DEST_EXPLICIT) || |
| 240 | (containerDest == DEST_EXPLICIT && fieldDest == DEST_UNSET); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | static bool isDefaultMessageImpl(const Descriptor* descriptor, const Destination dest, set<string>* parents) { |
| 245 | const int N = descriptor->field_count(); |
| 246 | const Destination messageDest = getMessageDest(descriptor, dest); |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 247 | parents->insert(descriptor->full_name()); |
| 248 | for (int i=0; i<N; ++i) { |
| 249 | const FieldDescriptor* field = descriptor->field(i); |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 250 | const Destination fieldDest = getFieldDest(field); |
| 251 | // If current field is not default, return false immediately |
| 252 | if (!isDefaultField(field, messageDest)) return false; |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 253 | switch (field->type()) { |
| 254 | case FieldDescriptor::TYPE_MESSAGE: |
| 255 | // if self recursion, don't go deep. |
| 256 | if (parents->find(field->message_type()->full_name()) != parents->end()) break; |
| 257 | // if is a default message, just continue |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 258 | if (isDefaultMessageImpl(field->message_type(), fieldDest, parents)) break; |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 259 | // sub message is not default, so this message is always not default |
| 260 | return false; |
| 261 | case FieldDescriptor::TYPE_STRING: |
| 262 | if (getPrivacyFlags(field).patterns_size() != 0) return false; |
| 263 | default: |
| 264 | continue; |
| 265 | } |
| 266 | } |
| 267 | parents->erase(descriptor->full_name()); |
| 268 | return true; |
| 269 | } |
| 270 | |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 271 | // Recursively look at if this message is default, meaning all its fields and sub-messages |
| 272 | // can be described by the same dest. |
| 273 | static bool isDefaultMessage(const Descriptor* descriptor, const Destination dest) { |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 274 | set<string> parents; |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 275 | return isDefaultMessageImpl(descriptor, dest, &parents); |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 278 | // =============================================================================================================== |
| 279 | static bool numberInOrder(const FieldDescriptor* f1, const FieldDescriptor* f2) { |
| 280 | return f1->number() < f2->number(); |
| 281 | } |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 282 | |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 283 | // field numbers are possibly out of order, sort them here. |
| 284 | static vector<const FieldDescriptor*> sortFields(const Descriptor* descriptor) { |
| 285 | vector<const FieldDescriptor*> fields; |
| 286 | fields.reserve(descriptor->field_count()); |
| 287 | for (int i=0; i<descriptor->field_count(); i++) { |
| 288 | fields.push_back(descriptor->field(i)); |
| 289 | } |
| 290 | std::sort(fields.begin(), fields.end(), numberInOrder); |
| 291 | return fields; |
| 292 | } |
| 293 | |
| 294 | // This function looks for privacy tags of a message type and recursively its sub-messages. |
| 295 | // It generates Privacy objects for each non-default fields including non-default sub-messages. |
| 296 | // And if the message has Privacy objects generated, it returns a list of them. |
| 297 | // Returns false if the descriptor doesn't have any non default privacy flags set, including its submessages |
| 298 | static bool generatePrivacyFlags(const Descriptor* descriptor, const Destination overridden, |
| 299 | map<string, bool> &variableNames, set<string>* parents) { |
| 300 | const string messageName = getMessageName(descriptor, overridden); |
| 301 | const Destination messageDest = getMessageDest(descriptor, overridden); |
| 302 | |
| 303 | if (variableNames.find(messageName) != variableNames.end()) { |
| 304 | bool hasDefault = variableNames[messageName]; |
| 305 | return !hasDefault; // if has default, then don't generate privacy flags. |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 306 | } |
| 307 | // insert the message type name so sub-message will figure out if self-recursion occurs |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 308 | parents->insert(messageName); |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 309 | |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 310 | // sort fields based on number, iterate though them and generate sub flags first |
| 311 | vector<const FieldDescriptor*> fieldsInOrder = sortFields(descriptor); |
| 312 | bool hasDefaultFlags[fieldsInOrder.size()]; |
| 313 | for (size_t i=0; i<fieldsInOrder.size(); i++) { |
| 314 | const FieldDescriptor* field = fieldsInOrder[i]; |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 315 | const string fieldName = getFieldName(field); |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 316 | const Destination fieldDest = getFieldDest(field); |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 317 | |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 318 | if (variableNames.find(fieldName) != variableNames.end()) { |
| 319 | hasDefaultFlags[i] = variableNames[fieldName]; |
| 320 | continue; |
| 321 | } |
| 322 | hasDefaultFlags[i] = isDefaultField(field, messageDest); |
| 323 | |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 324 | string fieldMessageName; |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 325 | PrivacyFlags p = getPrivacyFlags(field); |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 326 | switch (field->type()) { |
| 327 | case FieldDescriptor::TYPE_MESSAGE: |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 328 | fieldMessageName = getMessageName(field->message_type(), fieldDest); |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 329 | if (parents->find(fieldMessageName) != parents->end()) { // Self-Recursion proto definition |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 330 | if (hasDefaultFlags[i]) { |
| 331 | hasDefaultFlags[i] = isDefaultMessage(field->message_type(), fieldDest); |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 332 | } |
| 333 | if (!hasDefaultFlags[i]) { |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 334 | printPrivacy(fieldName, field, "NULL", fieldDest, "NULL", |
| 335 | " // self recursion field of " + fieldMessageName); |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 336 | // generate the assignment and used to construct createList function later on. |
| 337 | gSelfRecursionAssignments.push_back(fieldName + ".children = " + fieldMessageName); |
| 338 | } |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 339 | } else if (generatePrivacyFlags(field->message_type(), p.dest(), variableNames, parents)) { |
| 340 | if (variableNames.find(fieldName) == variableNames.end()) { |
| 341 | printPrivacy(fieldName, field, fieldMessageName, fieldDest, "NULL"); |
| 342 | } |
| 343 | hasDefaultFlags[i] = false; |
| 344 | } else if (!hasDefaultFlags[i]) { |
| 345 | printPrivacy(fieldName, field, "NULL", fieldDest, "NULL"); |
Yi Jin | 22769e0 | 2017-10-16 14:42:50 -0700 | [diff] [blame] | 346 | } |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 347 | break; |
| 348 | case FieldDescriptor::TYPE_STRING: |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 349 | if (p.patterns_size() != 0) { // if patterns are specified |
| 350 | if (hasDefaultFlags[i]) break; |
| 351 | printf("const char* %s_patterns[] = {\n", fieldName.c_str()); |
| 352 | for (int j=0; j<p.patterns_size(); j++) { |
| 353 | // generated string needs to escape backslash too, duplicate it to allow escape again. |
| 354 | printf(" \"%s\",\n", replaceAll(p.patterns(j), '\\', "\\\\").c_str()); |
| 355 | } |
| 356 | printf(" NULL };\n"); |
| 357 | printPrivacy(fieldName, field, "NULL", fieldDest, fieldName + "_patterns"); |
| 358 | break; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 359 | } |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 360 | // else treat string field as primitive field and goes to default |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 361 | default: |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 362 | if (!hasDefaultFlags[i]) printPrivacy(fieldName, field, "NULL", fieldDest, "NULL"); |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 363 | } |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 364 | // Don't generate a variable twice |
| 365 | if (!hasDefaultFlags[i]) variableNames[fieldName] = false; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | bool allDefaults = true; |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 369 | for (size_t i=0; i<fieldsInOrder.size(); i++) { |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 370 | allDefaults &= hasDefaultFlags[i]; |
| 371 | } |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 372 | |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 373 | parents->erase(messageName); // erase the message type name when exit the message. |
| 374 | variableNames[messageName] = allDefaults; // store the privacy tags of the message here to avoid overhead. |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 375 | |
Yi Jin | 22769e0 | 2017-10-16 14:42:50 -0700 | [diff] [blame] | 376 | if (allDefaults) return false; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 377 | |
| 378 | emptyline(); |
Yi Jin | 7e0b4e5 | 2017-09-12 20:00:25 -0700 | [diff] [blame] | 379 | int policyCount = 0; |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 380 | printf("Privacy* %s[] = {\n", messageName.c_str()); |
| 381 | for (size_t i=0; i<fieldsInOrder.size(); i++) { |
| 382 | const FieldDescriptor* field = fieldsInOrder[i]; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 383 | if (hasDefaultFlags[i]) continue; |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 384 | printf(" &%s,\n", getFieldName(field).c_str()); |
Yi Jin | 7e0b4e5 | 2017-09-12 20:00:25 -0700 | [diff] [blame] | 385 | policyCount++; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 386 | } |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 387 | printf(" NULL };\n"); |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 388 | emptyline(); |
Yi Jin | 22769e0 | 2017-10-16 14:42:50 -0700 | [diff] [blame] | 389 | return true; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | static bool generateSectionListCpp(Descriptor const* descriptor) { |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 393 | generateHead("section_list"); |
| 394 | |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 395 | // generate namespaces |
| 396 | printf("namespace android {\n"); |
| 397 | printf("namespace os {\n"); |
| 398 | printf("namespace incidentd {\n"); |
| 399 | |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 400 | // generates SECTION_LIST |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 401 | printf("// Generate SECTION_LIST.\n\n"); |
| 402 | |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 403 | printf("const Section* SECTION_LIST[] = {\n"); |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 404 | for (int i=0; i<descriptor->field_count(); i++) { |
| 405 | const FieldDescriptor* field = descriptor->field(i); |
| 406 | |
| 407 | if (field->type() != FieldDescriptor::TYPE_MESSAGE) { |
| 408 | continue; |
| 409 | } |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 410 | const SectionFlags s = getSectionFlags(field); |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 411 | switch (s.type()) { |
| 412 | case SECTION_NONE: |
| 413 | continue; |
| 414 | case SECTION_FILE: |
Yi Jin | 7fe3dee | 2018-04-16 16:13:04 -0700 | [diff] [blame] | 415 | printf(" new FileSection(%d, \"%s\", %s),\n", field->number(), s.args().c_str(), |
| 416 | s.device_specific() ? "true" : "false"); |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 417 | break; |
| 418 | case SECTION_COMMAND: |
| 419 | printf(" new CommandSection(%d,", field->number()); |
| 420 | splitAndPrint(s.args()); |
| 421 | printf(" NULL),\n"); |
| 422 | break; |
| 423 | case SECTION_DUMPSYS: |
Kweku Adams | 71a9531 | 2018-04-16 16:54:24 -0700 | [diff] [blame^] | 424 | printf(" new DumpsysSection(%d, \"%s\",", field->number(), |
| 425 | s.userdebug_and_eng_only() ? "true" : "false"); |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 426 | splitAndPrint(s.args()); |
| 427 | printf(" NULL),\n"); |
| 428 | break; |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 429 | case SECTION_LOG: |
| 430 | printf(" new LogSection(%d, %s),\n", field->number(), s.args().c_str()); |
| 431 | break; |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 432 | case SECTION_GZIP: |
| 433 | printf(" new GZipSection(%d,", field->number()); |
| 434 | splitAndPrint(s.args()); |
| 435 | printf(" NULL),\n"); |
Yi Jin | 934cc61 | 2018-03-15 14:14:26 -0700 | [diff] [blame] | 436 | break; |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 437 | case SECTION_TOMBSTONE: |
| 438 | printf(" new TombstoneSection(%d, \"%s\"),\n", field->number(), s.args().c_str()); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 439 | break; |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 440 | } |
| 441 | } |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 442 | printf(" NULL };\n"); |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 443 | |
| 444 | emptyline(); |
| 445 | printf("// =============================================================================\n"); |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 446 | emptyline(); |
| 447 | |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 448 | // generates PRIVACY_POLICY_LIST |
| 449 | printf("// Generate PRIVACY_POLICY_LIST.\n\n"); |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 450 | map<string, bool> variableNames; |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 451 | set<string> parents; |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 452 | vector<const FieldDescriptor*> fieldsInOrder = sortFields(descriptor); |
| 453 | bool skip[fieldsInOrder.size()]; |
| 454 | const Destination incidentDest = getPrivacyFlags(descriptor).dest(); |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 455 | |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 456 | for (size_t i=0; i<fieldsInOrder.size(); i++) { |
| 457 | const FieldDescriptor* field = fieldsInOrder[i]; |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 458 | const string fieldName = getFieldName(field); |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 459 | const Destination fieldDest = getFieldDest(field); |
| 460 | const string fieldMessageName = getMessageName(field->message_type(), fieldDest); |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 461 | |
| 462 | skip[i] = true; |
| 463 | |
| 464 | if (field->type() != FieldDescriptor::TYPE_MESSAGE) { |
| 465 | continue; |
| 466 | } |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 467 | // generate privacy flags for each section. |
| 468 | if (generatePrivacyFlags(field->message_type(), fieldDest, variableNames, &parents)) { |
| 469 | printPrivacy(fieldName, field, fieldMessageName, fieldDest, "NULL"); |
| 470 | } else if (isDefaultField(field, incidentDest)) { |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 471 | continue; // don't create a new privacy if the value is default. |
| 472 | } else { |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 473 | printPrivacy(fieldName, field, "NULL", fieldDest, "NULL"); |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 474 | } |
| 475 | skip[i] = false; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 478 | // generate final PRIVACY_POLICY_LIST |
| 479 | emptyline(); |
| 480 | int policyCount = 0; |
| 481 | if (gSelfRecursionAssignments.empty()) { |
| 482 | printf("Privacy* privacyArray[] = {\n"); |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 483 | for (size_t i=0; i<fieldsInOrder.size(); i++) { |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 484 | if (skip[i]) continue; |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 485 | printf(" &%s,\n", getFieldName(fieldsInOrder[i]).c_str()); |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 486 | policyCount++; |
| 487 | } |
| 488 | printf("};\n\n"); |
| 489 | printf("const Privacy** PRIVACY_POLICY_LIST = const_cast<const Privacy**>(privacyArray);\n\n"); |
| 490 | printf("const int PRIVACY_POLICY_COUNT = %d;\n", policyCount); |
| 491 | } else { |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 492 | for (size_t i=0; i<fieldsInOrder.size(); i++) { |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 493 | if (!skip[i]) policyCount++; |
| 494 | } |
| 495 | |
| 496 | printf("static const Privacy** createList() {\n"); |
| 497 | for (size_t i=0; i<gSelfRecursionAssignments.size(); ++i) { |
| 498 | printf(" %s;\n", gSelfRecursionAssignments[i].c_str()); |
| 499 | } |
| 500 | printf(" Privacy** privacyArray = (Privacy**)malloc(%d * sizeof(Privacy**));\n", policyCount); |
| 501 | policyCount = 0; // reset |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 502 | for (size_t i=0; i<fieldsInOrder.size(); i++) { |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 503 | if (skip[i]) continue; |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 504 | printf(" privacyArray[%d] = &%s;\n", policyCount++, getFieldName(fieldsInOrder[i]).c_str()); |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 505 | } |
| 506 | printf(" return const_cast<const Privacy**>(privacyArray);\n"); |
| 507 | printf("}\n\n"); |
| 508 | printf("const Privacy** PRIVACY_POLICY_LIST = createList();\n\n"); |
| 509 | printf("const int PRIVACY_POLICY_COUNT = %d;\n", policyCount); |
| 510 | } |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 511 | |
| 512 | printf("} // incidentd\n"); |
| 513 | printf("} // os\n"); |
| 514 | printf("} // android\n"); |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 515 | return true; |
| 516 | } |
| 517 | |
| 518 | // ================================================================================ |
Yi Jin | 0f2599f | 2017-11-16 18:19:45 -0800 | [diff] [blame] | 519 | static string replace_string(const string& str, const char replace, const char with) |
| 520 | { |
| 521 | string result(str); |
| 522 | const int N = result.size(); |
| 523 | for (int i=0; i<N; i++) { |
| 524 | if (result[i] == replace) { |
| 525 | result[i] = with; |
| 526 | } |
| 527 | } |
| 528 | return result; |
| 529 | } |
| 530 | |
Kweku Adams | ecf4bdb | 2018-02-22 15:05:48 -0800 | [diff] [blame] | 531 | static void generateCsv(Descriptor const* descriptor, const string& indent, set<string>* parents, const Destination containerDest = DEST_UNSET) { |
Yi Jin | 0f2599f | 2017-11-16 18:19:45 -0800 | [diff] [blame] | 532 | DebugStringOptions options; |
| 533 | options.include_comments = true; |
| 534 | for (int i=0; i<descriptor->field_count(); i++) { |
| 535 | const FieldDescriptor* field = descriptor->field(i); |
Kweku Adams | ecf4bdb | 2018-02-22 15:05:48 -0800 | [diff] [blame] | 536 | const Destination fieldDest = getFieldDest(field); |
Yi Jin | 0f2599f | 2017-11-16 18:19:45 -0800 | [diff] [blame] | 537 | stringstream text; |
| 538 | if (field->type() == FieldDescriptor::TYPE_MESSAGE) { |
| 539 | text << field->message_type()->name(); |
| 540 | } else { |
| 541 | text << field->type_name(); |
| 542 | } |
| 543 | text << " " << field->name(); |
Kweku Adams | ecf4bdb | 2018-02-22 15:05:48 -0800 | [diff] [blame] | 544 | text << " (PRIVACY="; |
| 545 | if (isDefaultField(field, containerDest)) { |
| 546 | text << getDestString(containerDest); |
| 547 | } else { |
| 548 | text << getDestString(fieldDest); |
| 549 | } |
| 550 | text << ")"; |
Yi Jin | 0f2599f | 2017-11-16 18:19:45 -0800 | [diff] [blame] | 551 | printf("%s%s,\n", indent.c_str(), replace_string(text.str(), '\n', ' ').c_str()); |
| 552 | if (field->type() == FieldDescriptor::TYPE_MESSAGE && |
| 553 | parents->find(field->message_type()->full_name()) == parents->end()) { |
| 554 | parents->insert(field->message_type()->full_name()); |
Kweku Adams | ecf4bdb | 2018-02-22 15:05:48 -0800 | [diff] [blame] | 555 | generateCsv(field->message_type(), indent + ",", parents, fieldDest); |
Yi Jin | 0f2599f | 2017-11-16 18:19:45 -0800 | [diff] [blame] | 556 | parents->erase(field->message_type()->full_name()); |
| 557 | } |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | // ================================================================================ |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 562 | int main(int argc, char const *argv[]) |
| 563 | { |
Yi Jin | 0f2599f | 2017-11-16 18:19:45 -0800 | [diff] [blame] | 564 | if (argc < 2) return 1; |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 565 | const char* module = argv[1]; |
| 566 | |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 567 | Descriptor const* descriptor = IncidentProto::descriptor(); |
| 568 | |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 569 | if (strcmp(module, "incident") == 0) { |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 570 | return !generateIncidentSectionsCpp(descriptor); |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 571 | } |
| 572 | if (strcmp(module, "incidentd") == 0 ) { |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 573 | return !generateSectionListCpp(descriptor); |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 574 | } |
Yi Jin | 0f2599f | 2017-11-16 18:19:45 -0800 | [diff] [blame] | 575 | // Generates Csv Format of proto definition for each section. |
| 576 | if (strcmp(module, "csv") == 0 && argc > 2) { |
| 577 | int sectionId = atoi(argv[2]); |
| 578 | for (int i=0; i<descriptor->field_count(); i++) { |
| 579 | const FieldDescriptor* field = descriptor->field(i); |
| 580 | if (strcmp(field->name().c_str(), argv[2]) == 0 |
| 581 | || field->number() == sectionId) { |
| 582 | set<string> parents; |
| 583 | printf("%s\n", field->name().c_str()); |
Kweku Adams | ecf4bdb | 2018-02-22 15:05:48 -0800 | [diff] [blame] | 584 | generateCsv(field->message_type(), "", &parents, getFieldDest(field)); |
Yi Jin | 0f2599f | 2017-11-16 18:19:45 -0800 | [diff] [blame] | 585 | break; |
| 586 | } |
| 587 | } |
| 588 | // Returns failure if csv is enabled to prevent Android building with it. |
| 589 | // It doesn't matter if this command runs manually. |
| 590 | return 1; |
| 591 | } |
| 592 | // Returns failure if not called by the whitelisted modules |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 593 | return 1; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 594 | } |