blob: 636c22df8146f5dbd9ba69481a5ef38e5936d0af [file] [log] [blame]
Joe Onorato1754d742016-11-21 17:51:35 -08001/*
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
17
Joe Onorato76690122016-12-20 08:18:32 -080018#include <frameworks/base/core/proto/android/os/incident.pb.h>
Joe Onorato1754d742016-11-21 17:51:35 -080019
Joe Onorato1754d742016-11-21 17:51:35 -080020#include <map>
Yi Jinbe6de302017-10-24 12:30:24 -070021#include <set>
Yi Jinf8601842017-08-15 22:01:41 -070022#include <string>
Yi Jin0f2599f2017-11-16 18:19:45 -080023#include <sstream>
Joe Onorato1754d742016-11-21 17:51:35 -080024
Yi Jinf8601842017-08-15 22:01:41 -070025using namespace android;
Joe Onorato1754d742016-11-21 17:51:35 -080026using namespace android::os;
27using namespace google::protobuf;
28using namespace google::protobuf::io;
29using namespace google::protobuf::internal;
30using namespace std;
31
Yi Jinbe6de302017-10-24 12:30:24 -070032/**
33 * Implementation details:
34 * This binary auto generates .cpp files for incident and incidentd.
35 *
36 * When argument "incident" is specified, it generates incident_section.cpp file.
37 *
38 * When argument "incidentd" is specified, it generates section_list.cpp file.
39 *
40 * In section_list.cpp file, it generates a SECTION_LIST array and a PRIVACY_POLICY_LIST array.
41 * For SECTION_LIST, it generates Section.h classes only for proto fields with section option enabled.
42 * For PRIVACY_POLICY_LIST, it generates Privacy.h classes only for proto fields with privacy option enabled.
43 *
44 * For Privacy struct, it is possible to have self recursion definitions since protobuf is defining "classes"
45 * So the logic to handle it becomes very complicated when Privacy tag of a message contains a list of Privacies
46 * of its sub-messages. The code also handles multiple depth of self recursion fields.
47 *
48 * For example here is a one level self recursion message WindowManager:
49 * message WindowState {
50 * string state = 1 [(privacy).dest = LOCAL];
51 * int32 display_id = 2;
52 * repeated WindowState child_windows = 3;
53 * }
54 *
55 * message WindowManager {
56 * WindowState my_window = 1;
57 * }
58 *
59 * When generating Privacy options for WindowManager, this tool will generate cpp syntax source code:
60 *
61 * #include "section_list.h"
62 * ...
Yi Jinbdf58942017-11-14 17:58:19 -080063 * Privacy WindowState__state { 1, 9, NULL, LOCAL, NULL }; // first two integers are values for field id and proto type.
64 * Privacy WindowState__child_windows { 3, 11, NULL, UNSET, NULL }; // reserved for WindowState_LIST
65 * Privacy* WindowState__MSG__UNSET[] = {
Yi Jinbe6de302017-10-24 12:30:24 -070066 * &WindowState_state,
67 * // display id is default, nothing is generated.
68 * &WindowState_child_windows,
69 * NULL // terminator of the array
70 * };
Yi Jinbdf58942017-11-14 17:58:19 -080071 * Privacy WindowState__my_window { 1, 11, WindowState__MSG__UNSET, UNSET, NULL };
Yi Jinbe6de302017-10-24 12:30:24 -070072 *
73 * createList() {
74 * ...
Yi Jinbdf58942017-11-14 17:58:19 -080075 * WindowState_child_windows.children = WindowState__MSG_UNSET; // point to its own definition after the list is defined.
Yi Jinbe6de302017-10-24 12:30:24 -070076 * ...
77 * }
78 *
79 * const Privacy** PRIVACY_POLICY_LIST = createList();
80 * const int PRIVACY_POLICY_COUNT = 1;
Yi Jinbdf58942017-11-14 17:58:19 -080081 *
82 * Privacy Value Inheritance rules:
83 * 1. Both field and message can be tagged with DESTINATION: LOCAL(L), EXPLICIT(E), AUTOMATIC(A).
84 * 2. Primitives inherits containing message's tag unless defined explicitly.
85 * 3. Containing message's tag doesn't apply to message fields, even when unset (in this case, uses its default message tag).
86 * 4. Message field tag overrides its default message tag.
87 * 5. UNSET tag defaults to EXPLICIT.
Yi Jinbe6de302017-10-24 12:30:24 -070088 */
89
90// The assignments will be called when constructs PRIVACY_POLICY_LIST, has to be global variable
91vector<string> gSelfRecursionAssignments;
92
Yi Jin0ed9b682017-08-18 14:51:20 -070093static inline void emptyline() {
Joe Onorato1754d742016-11-21 17:51:35 -080094 printf("\n");
Yi Jinf8601842017-08-15 22:01:41 -070095}
Joe Onorato1754d742016-11-21 17:51:35 -080096
Yi Jin0ed9b682017-08-18 14:51:20 -070097static void generateHead(const char* header) {
98 printf("// Auto generated file. Do not modify\n");
99 emptyline();
100 printf("#include \"%s.h\"\n", header);
101 emptyline();
102}
103
Yi Jinbe6de302017-10-24 12:30:24 -0700104// ======================== incident_sections =============================
Yi Jin0ed9b682017-08-18 14:51:20 -0700105static bool generateIncidentSectionsCpp(Descriptor const* descriptor)
Yi Jinf8601842017-08-15 22:01:41 -0700106{
107 generateHead("incident_sections");
108
109 map<string,FieldDescriptor const*> sections;
110 int N;
Joe Onorato1754d742016-11-21 17:51:35 -0800111 N = descriptor->field_count();
112 for (int i=0; i<N; i++) {
113 const FieldDescriptor* field = descriptor->field(i);
114 if (field->type() == FieldDescriptor::TYPE_MESSAGE) {
115 sections[field->name()] = field;
116 }
117 }
118
119 printf("IncidentSection const INCIDENT_SECTIONS[] = {\n");
120 N = sections.size();
121 int i = 0;
122 for (map<string,FieldDescriptor const*>::const_iterator it = sections.begin();
123 it != sections.end(); it++, i++) {
124 const FieldDescriptor* field = it->second;
125 printf(" { %d, \"%s\" }", field->number(), field->name().c_str());
126 if (i != N-1) {
127 printf(",\n");
128 } else {
129 printf("\n");
130 }
131 }
132 printf("};\n");
133
134 printf("const int INCIDENT_SECTION_COUNT = %d;\n", N);
135
Yi Jinf8601842017-08-15 22:01:41 -0700136 return true;
137}
138
Yi Jinbe6de302017-10-24 12:30:24 -0700139// ========================= section_list ===================================
Yi Jinf8601842017-08-15 22:01:41 -0700140static void splitAndPrint(const string& args) {
141 size_t base = 0;
142 size_t found;
143 while (true) {
144 found = args.find_first_of(" ", base);
145 if (found != base) {
146 string arg = args.substr(base, found - base);
147 printf(" \"%s\",", arg.c_str());
148 }
149 if (found == args.npos) break;
150 base = found + 1;
151 }
152}
153
Yi Jinbe6de302017-10-24 12:30:24 -0700154static string replaceAll(const string& fieldName, const char oldC, const string& newS) {
155 if (fieldName.find_first_of(oldC) == fieldName.npos) return fieldName.c_str();
Yi Jin0ed9b682017-08-18 14:51:20 -0700156 size_t pos = 0, idx = 0;
Yi Jinbe6de302017-10-24 12:30:24 -0700157 char* res = new char[fieldName.size() * newS.size() + 1]; // assign a larger buffer
158 while (pos != fieldName.size()) {
159 char cur = fieldName[pos++];
Yi Jin0ed9b682017-08-18 14:51:20 -0700160 if (cur != oldC) {
161 res[idx++] = cur;
162 continue;
163 }
164
165 for (size_t i=0; i<newS.size(); i++) {
166 res[idx++] = newS[i];
167 }
168 }
169 res[idx] = '\0';
Yi Jinbe6de302017-10-24 12:30:24 -0700170 string result(res);
Yunlian Jiang3809bbf2017-09-05 15:50:58 -0700171 delete [] res;
172 return result;
Yi Jin0ed9b682017-08-18 14:51:20 -0700173}
174
Yi Jinbdf58942017-11-14 17:58:19 -0800175static inline void printPrivacy(const string& name, const FieldDescriptor* field, const string& children,
176 const Destination dest, const string& patterns, const string& comments = "") {
177 printf("Privacy %s = { %d, %d, %s, %d, %s };%s\n", name.c_str(), field->number(), field->type(),
178 children.c_str(), dest, patterns.c_str(), comments.c_str());
Yi Jin0ed9b682017-08-18 14:51:20 -0700179}
180
Yi Jinbdf58942017-11-14 17:58:19 -0800181// Get Custom Options ================================================================================
Yi Jinbe6de302017-10-24 12:30:24 -0700182static inline SectionFlags getSectionFlags(const FieldDescriptor* field) {
183 return field->options().GetExtension(section);
184}
185
186static inline PrivacyFlags getPrivacyFlags(const FieldDescriptor* field) {
187 return field->options().GetExtension(privacy);
188}
189
Yi Jinbdf58942017-11-14 17:58:19 -0800190static inline PrivacyFlags getPrivacyFlags(const Descriptor* descriptor) {
191 return descriptor->options().GetExtension(msg_privacy);
Yi Jinbe6de302017-10-24 12:30:24 -0700192}
193
Yi Jinbdf58942017-11-14 17:58:19 -0800194// Get Destinations ===================================================================================
195static inline Destination getMessageDest(const Descriptor* descriptor, const Destination overridden) {
196 return overridden != DEST_UNSET ? overridden : getPrivacyFlags(descriptor).dest();
197}
198
199// Returns field's own dest, when it is a message field, uses its message default tag if unset.
200static inline Destination getFieldDest(const FieldDescriptor* field) {
201 Destination fieldDest = getPrivacyFlags(field).dest();
202 return field->type() != FieldDescriptor::TYPE_MESSAGE ? fieldDest :
203 getMessageDest(field->message_type(), fieldDest);
204}
205
Kweku Adamsecf4bdb2018-02-22 15:05:48 -0800206// Converts Destination to a string.
207static inline string getDestString(const Destination dest) {
208 switch (dest) {
209 case DEST_AUTOMATIC: return "AUTOMATIC";
210 case DEST_LOCAL: return "LOCAL";
211 case DEST_EXPLICIT: return "EXPLICIT";
212 // UNSET is considered EXPLICIT by default.
213 case DEST_UNSET: return "EXPLICIT";
214 default: return "UNKNOWN";
215 }
216}
217
Yi Jinbdf58942017-11-14 17:58:19 -0800218// Get Names ===========================================================================================
219static inline string getFieldName(const FieldDescriptor* field) {
220 // replace . with double underscores to avoid name conflicts since fields use snake naming convention
221 return replaceAll(field->full_name(), '.', "__");
222}
223
224
225static inline string getMessageName(const Descriptor* descriptor, const Destination overridden) {
226 // replace . with one underscore since messages use camel naming convention
227 return replaceAll(descriptor->full_name(), '.', "_") + "__MSG__" +
228 to_string(getMessageDest(descriptor, overridden));
229}
230
231// IsDefault ============================================================================================
232// Returns true if a field is default. Default is defined as this field has same dest as its containing message.
Kweku Adamsecf4bdb2018-02-22 15:05:48 -0800233// For message fields, it only looks at its field tag and own default message tag, doesn't recursively go deeper.
Yi Jinbdf58942017-11-14 17:58:19 -0800234static inline bool isDefaultField(const FieldDescriptor* field, const Destination containerDest) {
235 Destination fieldDest = getFieldDest(field);
236 if (field->type() != FieldDescriptor::TYPE_MESSAGE) {
237 return fieldDest == containerDest || (fieldDest == DEST_UNSET);
238 } else {
239 return fieldDest == containerDest ||
240 (containerDest == DEST_UNSET && fieldDest == DEST_EXPLICIT) ||
241 (containerDest == DEST_EXPLICIT && fieldDest == DEST_UNSET);
242 }
243}
244
245static bool isDefaultMessageImpl(const Descriptor* descriptor, const Destination dest, set<string>* parents) {
246 const int N = descriptor->field_count();
247 const Destination messageDest = getMessageDest(descriptor, dest);
Yi Jinbe6de302017-10-24 12:30:24 -0700248 parents->insert(descriptor->full_name());
249 for (int i=0; i<N; ++i) {
250 const FieldDescriptor* field = descriptor->field(i);
Yi Jinbdf58942017-11-14 17:58:19 -0800251 const Destination fieldDest = getFieldDest(field);
252 // If current field is not default, return false immediately
253 if (!isDefaultField(field, messageDest)) return false;
Yi Jinbe6de302017-10-24 12:30:24 -0700254 switch (field->type()) {
255 case FieldDescriptor::TYPE_MESSAGE:
256 // if self recursion, don't go deep.
257 if (parents->find(field->message_type()->full_name()) != parents->end()) break;
258 // if is a default message, just continue
Yi Jinbdf58942017-11-14 17:58:19 -0800259 if (isDefaultMessageImpl(field->message_type(), fieldDest, parents)) break;
Yi Jinbe6de302017-10-24 12:30:24 -0700260 // sub message is not default, so this message is always not default
261 return false;
262 case FieldDescriptor::TYPE_STRING:
263 if (getPrivacyFlags(field).patterns_size() != 0) return false;
264 default:
265 continue;
266 }
267 }
268 parents->erase(descriptor->full_name());
269 return true;
270}
271
Yi Jinbdf58942017-11-14 17:58:19 -0800272// Recursively look at if this message is default, meaning all its fields and sub-messages
273// can be described by the same dest.
274static bool isDefaultMessage(const Descriptor* descriptor, const Destination dest) {
Yi Jinbe6de302017-10-24 12:30:24 -0700275 set<string> parents;
Yi Jinbdf58942017-11-14 17:58:19 -0800276 return isDefaultMessageImpl(descriptor, dest, &parents);
Yi Jinbe6de302017-10-24 12:30:24 -0700277}
278
Yi Jinbdf58942017-11-14 17:58:19 -0800279// ===============================================================================================================
280static bool numberInOrder(const FieldDescriptor* f1, const FieldDescriptor* f2) {
281 return f1->number() < f2->number();
282}
Yi Jinbe6de302017-10-24 12:30:24 -0700283
Yi Jinbdf58942017-11-14 17:58:19 -0800284// field numbers are possibly out of order, sort them here.
285static vector<const FieldDescriptor*> sortFields(const Descriptor* descriptor) {
286 vector<const FieldDescriptor*> fields;
287 fields.reserve(descriptor->field_count());
288 for (int i=0; i<descriptor->field_count(); i++) {
289 fields.push_back(descriptor->field(i));
290 }
291 std::sort(fields.begin(), fields.end(), numberInOrder);
292 return fields;
293}
294
295// This function looks for privacy tags of a message type and recursively its sub-messages.
296// It generates Privacy objects for each non-default fields including non-default sub-messages.
297// And if the message has Privacy objects generated, it returns a list of them.
298// Returns false if the descriptor doesn't have any non default privacy flags set, including its submessages
299static bool generatePrivacyFlags(const Descriptor* descriptor, const Destination overridden,
300 map<string, bool> &variableNames, set<string>* parents) {
301 const string messageName = getMessageName(descriptor, overridden);
302 const Destination messageDest = getMessageDest(descriptor, overridden);
303
304 if (variableNames.find(messageName) != variableNames.end()) {
305 bool hasDefault = variableNames[messageName];
306 return !hasDefault; // if has default, then don't generate privacy flags.
Yi Jinbe6de302017-10-24 12:30:24 -0700307 }
308 // insert the message type name so sub-message will figure out if self-recursion occurs
Yi Jinbdf58942017-11-14 17:58:19 -0800309 parents->insert(messageName);
Yi Jinbe6de302017-10-24 12:30:24 -0700310
Yi Jinbdf58942017-11-14 17:58:19 -0800311 // sort fields based on number, iterate though them and generate sub flags first
312 vector<const FieldDescriptor*> fieldsInOrder = sortFields(descriptor);
313 bool hasDefaultFlags[fieldsInOrder.size()];
314 for (size_t i=0; i<fieldsInOrder.size(); i++) {
315 const FieldDescriptor* field = fieldsInOrder[i];
Yi Jinbe6de302017-10-24 12:30:24 -0700316 const string fieldName = getFieldName(field);
Yi Jinbdf58942017-11-14 17:58:19 -0800317 const Destination fieldDest = getFieldDest(field);
Yi Jin0ed9b682017-08-18 14:51:20 -0700318
Yi Jinbdf58942017-11-14 17:58:19 -0800319 if (variableNames.find(fieldName) != variableNames.end()) {
320 hasDefaultFlags[i] = variableNames[fieldName];
321 continue;
322 }
323 hasDefaultFlags[i] = isDefaultField(field, messageDest);
324
Yi Jinbe6de302017-10-24 12:30:24 -0700325 string fieldMessageName;
Yi Jinbdf58942017-11-14 17:58:19 -0800326 PrivacyFlags p = getPrivacyFlags(field);
Yi Jin0ed9b682017-08-18 14:51:20 -0700327 switch (field->type()) {
328 case FieldDescriptor::TYPE_MESSAGE:
Yi Jinbdf58942017-11-14 17:58:19 -0800329 fieldMessageName = getMessageName(field->message_type(), fieldDest);
Yi Jinbe6de302017-10-24 12:30:24 -0700330 if (parents->find(fieldMessageName) != parents->end()) { // Self-Recursion proto definition
Yi Jinbdf58942017-11-14 17:58:19 -0800331 if (hasDefaultFlags[i]) {
332 hasDefaultFlags[i] = isDefaultMessage(field->message_type(), fieldDest);
Yi Jinbe6de302017-10-24 12:30:24 -0700333 }
334 if (!hasDefaultFlags[i]) {
Yi Jinbdf58942017-11-14 17:58:19 -0800335 printPrivacy(fieldName, field, "NULL", fieldDest, "NULL",
336 " // self recursion field of " + fieldMessageName);
Yi Jinbe6de302017-10-24 12:30:24 -0700337 // generate the assignment and used to construct createList function later on.
338 gSelfRecursionAssignments.push_back(fieldName + ".children = " + fieldMessageName);
339 }
Yi Jinbdf58942017-11-14 17:58:19 -0800340 } else if (generatePrivacyFlags(field->message_type(), p.dest(), variableNames, parents)) {
341 if (variableNames.find(fieldName) == variableNames.end()) {
342 printPrivacy(fieldName, field, fieldMessageName, fieldDest, "NULL");
343 }
344 hasDefaultFlags[i] = false;
345 } else if (!hasDefaultFlags[i]) {
346 printPrivacy(fieldName, field, "NULL", fieldDest, "NULL");
Yi Jin22769e02017-10-16 14:42:50 -0700347 }
Yi Jin0ed9b682017-08-18 14:51:20 -0700348 break;
349 case FieldDescriptor::TYPE_STRING:
Yi Jinbdf58942017-11-14 17:58:19 -0800350 if (p.patterns_size() != 0) { // if patterns are specified
351 if (hasDefaultFlags[i]) break;
352 printf("const char* %s_patterns[] = {\n", fieldName.c_str());
353 for (int j=0; j<p.patterns_size(); j++) {
354 // generated string needs to escape backslash too, duplicate it to allow escape again.
355 printf(" \"%s\",\n", replaceAll(p.patterns(j), '\\', "\\\\").c_str());
356 }
357 printf(" NULL };\n");
358 printPrivacy(fieldName, field, "NULL", fieldDest, fieldName + "_patterns");
359 break;
Yi Jin0ed9b682017-08-18 14:51:20 -0700360 }
Yi Jinbdf58942017-11-14 17:58:19 -0800361 // else treat string field as primitive field and goes to default
Yi Jin0ed9b682017-08-18 14:51:20 -0700362 default:
Yi Jinbdf58942017-11-14 17:58:19 -0800363 if (!hasDefaultFlags[i]) printPrivacy(fieldName, field, "NULL", fieldDest, "NULL");
Yi Jin0ed9b682017-08-18 14:51:20 -0700364 }
Yi Jinbdf58942017-11-14 17:58:19 -0800365 // Don't generate a variable twice
366 if (!hasDefaultFlags[i]) variableNames[fieldName] = false;
Yi Jin0ed9b682017-08-18 14:51:20 -0700367 }
368
369 bool allDefaults = true;
Yi Jinbdf58942017-11-14 17:58:19 -0800370 for (size_t i=0; i<fieldsInOrder.size(); i++) {
Yi Jin0ed9b682017-08-18 14:51:20 -0700371 allDefaults &= hasDefaultFlags[i];
372 }
Yi Jinbe6de302017-10-24 12:30:24 -0700373
Yi Jinbdf58942017-11-14 17:58:19 -0800374 parents->erase(messageName); // erase the message type name when exit the message.
375 variableNames[messageName] = allDefaults; // store the privacy tags of the message here to avoid overhead.
Yi Jinbe6de302017-10-24 12:30:24 -0700376
Yi Jin22769e02017-10-16 14:42:50 -0700377 if (allDefaults) return false;
Yi Jin0ed9b682017-08-18 14:51:20 -0700378
379 emptyline();
Yi Jin7e0b4e52017-09-12 20:00:25 -0700380 int policyCount = 0;
Yi Jinbdf58942017-11-14 17:58:19 -0800381 printf("Privacy* %s[] = {\n", messageName.c_str());
382 for (size_t i=0; i<fieldsInOrder.size(); i++) {
383 const FieldDescriptor* field = fieldsInOrder[i];
Yi Jin0ed9b682017-08-18 14:51:20 -0700384 if (hasDefaultFlags[i]) continue;
Yi Jinbe6de302017-10-24 12:30:24 -0700385 printf(" &%s,\n", getFieldName(field).c_str());
Yi Jin7e0b4e52017-09-12 20:00:25 -0700386 policyCount++;
Yi Jin0ed9b682017-08-18 14:51:20 -0700387 }
Yi Jinbe6de302017-10-24 12:30:24 -0700388 printf(" NULL };\n");
Yi Jin0ed9b682017-08-18 14:51:20 -0700389 emptyline();
Yi Jin22769e02017-10-16 14:42:50 -0700390 return true;
Yi Jin0ed9b682017-08-18 14:51:20 -0700391}
392
393static bool generateSectionListCpp(Descriptor const* descriptor) {
Yi Jinf8601842017-08-15 22:01:41 -0700394 generateHead("section_list");
395
Yi Jin0ed9b682017-08-18 14:51:20 -0700396 // generates SECTION_LIST
Yi Jinbe6de302017-10-24 12:30:24 -0700397 printf("// Generate SECTION_LIST.\n\n");
398
Yi Jinf8601842017-08-15 22:01:41 -0700399 printf("const Section* SECTION_LIST[] = {\n");
Yi Jinf8601842017-08-15 22:01:41 -0700400 for (int i=0; i<descriptor->field_count(); i++) {
401 const FieldDescriptor* field = descriptor->field(i);
402
403 if (field->type() != FieldDescriptor::TYPE_MESSAGE) {
404 continue;
405 }
Yi Jinbe6de302017-10-24 12:30:24 -0700406 const SectionFlags s = getSectionFlags(field);
Yi Jinf8601842017-08-15 22:01:41 -0700407 switch (s.type()) {
408 case SECTION_NONE:
409 continue;
410 case SECTION_FILE:
411 printf(" new FileSection(%d, \"%s\"),\n", field->number(), s.args().c_str());
412 break;
413 case SECTION_COMMAND:
414 printf(" new CommandSection(%d,", field->number());
415 splitAndPrint(s.args());
416 printf(" NULL),\n");
417 break;
418 case SECTION_DUMPSYS:
419 printf(" new DumpsysSection(%d,", field->number());
420 splitAndPrint(s.args());
421 printf(" NULL),\n");
422 break;
Yi Jin3c034c92017-12-22 17:36:47 -0800423 case SECTION_LOG:
424 printf(" new LogSection(%d, %s),\n", field->number(), s.args().c_str());
425 break;
Yi Jinf8601842017-08-15 22:01:41 -0700426 }
427 }
Yi Jin0ed9b682017-08-18 14:51:20 -0700428 printf(" NULL };\n");
Yi Jinbe6de302017-10-24 12:30:24 -0700429
430 emptyline();
431 printf("// =============================================================================\n");
Yi Jin0ed9b682017-08-18 14:51:20 -0700432 emptyline();
433
Yi Jinbe6de302017-10-24 12:30:24 -0700434 // generates PRIVACY_POLICY_LIST
435 printf("// Generate PRIVACY_POLICY_LIST.\n\n");
Yi Jinbdf58942017-11-14 17:58:19 -0800436 map<string, bool> variableNames;
Yi Jinbe6de302017-10-24 12:30:24 -0700437 set<string> parents;
Yi Jinbdf58942017-11-14 17:58:19 -0800438 vector<const FieldDescriptor*> fieldsInOrder = sortFields(descriptor);
439 bool skip[fieldsInOrder.size()];
440 const Destination incidentDest = getPrivacyFlags(descriptor).dest();
Yi Jinbe6de302017-10-24 12:30:24 -0700441
Yi Jinbdf58942017-11-14 17:58:19 -0800442 for (size_t i=0; i<fieldsInOrder.size(); i++) {
443 const FieldDescriptor* field = fieldsInOrder[i];
Yi Jinbe6de302017-10-24 12:30:24 -0700444 const string fieldName = getFieldName(field);
Yi Jinbdf58942017-11-14 17:58:19 -0800445 const Destination fieldDest = getFieldDest(field);
446 const string fieldMessageName = getMessageName(field->message_type(), fieldDest);
Yi Jinbe6de302017-10-24 12:30:24 -0700447
448 skip[i] = true;
449
450 if (field->type() != FieldDescriptor::TYPE_MESSAGE) {
451 continue;
452 }
Yi Jinbdf58942017-11-14 17:58:19 -0800453 // generate privacy flags for each section.
454 if (generatePrivacyFlags(field->message_type(), fieldDest, variableNames, &parents)) {
455 printPrivacy(fieldName, field, fieldMessageName, fieldDest, "NULL");
456 } else if (isDefaultField(field, incidentDest)) {
Yi Jinbe6de302017-10-24 12:30:24 -0700457 continue; // don't create a new privacy if the value is default.
458 } else {
Yi Jinbdf58942017-11-14 17:58:19 -0800459 printPrivacy(fieldName, field, "NULL", fieldDest, "NULL");
Yi Jinbe6de302017-10-24 12:30:24 -0700460 }
461 skip[i] = false;
Yi Jin0ed9b682017-08-18 14:51:20 -0700462 }
463
Yi Jinbe6de302017-10-24 12:30:24 -0700464 // generate final PRIVACY_POLICY_LIST
465 emptyline();
466 int policyCount = 0;
467 if (gSelfRecursionAssignments.empty()) {
468 printf("Privacy* privacyArray[] = {\n");
Yi Jinbdf58942017-11-14 17:58:19 -0800469 for (size_t i=0; i<fieldsInOrder.size(); i++) {
Yi Jinbe6de302017-10-24 12:30:24 -0700470 if (skip[i]) continue;
Yi Jinbdf58942017-11-14 17:58:19 -0800471 printf(" &%s,\n", getFieldName(fieldsInOrder[i]).c_str());
Yi Jinbe6de302017-10-24 12:30:24 -0700472 policyCount++;
473 }
474 printf("};\n\n");
475 printf("const Privacy** PRIVACY_POLICY_LIST = const_cast<const Privacy**>(privacyArray);\n\n");
476 printf("const int PRIVACY_POLICY_COUNT = %d;\n", policyCount);
477 } else {
Yi Jinbdf58942017-11-14 17:58:19 -0800478 for (size_t i=0; i<fieldsInOrder.size(); i++) {
Yi Jinbe6de302017-10-24 12:30:24 -0700479 if (!skip[i]) policyCount++;
480 }
481
482 printf("static const Privacy** createList() {\n");
483 for (size_t i=0; i<gSelfRecursionAssignments.size(); ++i) {
484 printf(" %s;\n", gSelfRecursionAssignments[i].c_str());
485 }
486 printf(" Privacy** privacyArray = (Privacy**)malloc(%d * sizeof(Privacy**));\n", policyCount);
487 policyCount = 0; // reset
Yi Jinbdf58942017-11-14 17:58:19 -0800488 for (size_t i=0; i<fieldsInOrder.size(); i++) {
Yi Jinbe6de302017-10-24 12:30:24 -0700489 if (skip[i]) continue;
Yi Jinbdf58942017-11-14 17:58:19 -0800490 printf(" privacyArray[%d] = &%s;\n", policyCount++, getFieldName(fieldsInOrder[i]).c_str());
Yi Jinbe6de302017-10-24 12:30:24 -0700491 }
492 printf(" return const_cast<const Privacy**>(privacyArray);\n");
493 printf("}\n\n");
494 printf("const Privacy** PRIVACY_POLICY_LIST = createList();\n\n");
495 printf("const int PRIVACY_POLICY_COUNT = %d;\n", policyCount);
496 }
Yi Jinf8601842017-08-15 22:01:41 -0700497 return true;
498}
499
500// ================================================================================
Yi Jin0f2599f2017-11-16 18:19:45 -0800501static string replace_string(const string& str, const char replace, const char with)
502{
503 string result(str);
504 const int N = result.size();
505 for (int i=0; i<N; i++) {
506 if (result[i] == replace) {
507 result[i] = with;
508 }
509 }
510 return result;
511}
512
Kweku Adamsecf4bdb2018-02-22 15:05:48 -0800513static void generateCsv(Descriptor const* descriptor, const string& indent, set<string>* parents, const Destination containerDest = DEST_UNSET) {
Yi Jin0f2599f2017-11-16 18:19:45 -0800514 DebugStringOptions options;
515 options.include_comments = true;
516 for (int i=0; i<descriptor->field_count(); i++) {
517 const FieldDescriptor* field = descriptor->field(i);
Kweku Adamsecf4bdb2018-02-22 15:05:48 -0800518 const Destination fieldDest = getFieldDest(field);
Yi Jin0f2599f2017-11-16 18:19:45 -0800519 stringstream text;
520 if (field->type() == FieldDescriptor::TYPE_MESSAGE) {
521 text << field->message_type()->name();
522 } else {
523 text << field->type_name();
524 }
525 text << " " << field->name();
Kweku Adamsecf4bdb2018-02-22 15:05:48 -0800526 text << " (PRIVACY=";
527 if (isDefaultField(field, containerDest)) {
528 text << getDestString(containerDest);
529 } else {
530 text << getDestString(fieldDest);
531 }
532 text << ")";
Yi Jin0f2599f2017-11-16 18:19:45 -0800533 printf("%s%s,\n", indent.c_str(), replace_string(text.str(), '\n', ' ').c_str());
534 if (field->type() == FieldDescriptor::TYPE_MESSAGE &&
535 parents->find(field->message_type()->full_name()) == parents->end()) {
536 parents->insert(field->message_type()->full_name());
Kweku Adamsecf4bdb2018-02-22 15:05:48 -0800537 generateCsv(field->message_type(), indent + ",", parents, fieldDest);
Yi Jin0f2599f2017-11-16 18:19:45 -0800538 parents->erase(field->message_type()->full_name());
539 }
540 }
541}
542
543// ================================================================================
Yi Jinf8601842017-08-15 22:01:41 -0700544int main(int argc, char const *argv[])
545{
Yi Jin0f2599f2017-11-16 18:19:45 -0800546 if (argc < 2) return 1;
Yi Jinf8601842017-08-15 22:01:41 -0700547 const char* module = argv[1];
548
Yi Jin0ed9b682017-08-18 14:51:20 -0700549 Descriptor const* descriptor = IncidentProto::descriptor();
550
Yi Jinf8601842017-08-15 22:01:41 -0700551 if (strcmp(module, "incident") == 0) {
Yi Jin0ed9b682017-08-18 14:51:20 -0700552 return !generateIncidentSectionsCpp(descriptor);
Yi Jinf8601842017-08-15 22:01:41 -0700553 }
554 if (strcmp(module, "incidentd") == 0 ) {
Yi Jin0ed9b682017-08-18 14:51:20 -0700555 return !generateSectionListCpp(descriptor);
Yi Jinf8601842017-08-15 22:01:41 -0700556 }
Yi Jin0f2599f2017-11-16 18:19:45 -0800557 // Generates Csv Format of proto definition for each section.
558 if (strcmp(module, "csv") == 0 && argc > 2) {
559 int sectionId = atoi(argv[2]);
560 for (int i=0; i<descriptor->field_count(); i++) {
561 const FieldDescriptor* field = descriptor->field(i);
562 if (strcmp(field->name().c_str(), argv[2]) == 0
563 || field->number() == sectionId) {
564 set<string> parents;
565 printf("%s\n", field->name().c_str());
Kweku Adamsecf4bdb2018-02-22 15:05:48 -0800566 generateCsv(field->message_type(), "", &parents, getFieldDest(field));
Yi Jin0f2599f2017-11-16 18:19:45 -0800567 break;
568 }
569 }
570 // Returns failure if csv is enabled to prevent Android building with it.
571 // It doesn't matter if this command runs manually.
572 return 1;
573 }
574 // Returns failure if not called by the whitelisted modules
Yi Jinf8601842017-08-15 22:01:41 -0700575 return 1;
Joe Onorato1754d742016-11-21 17:51:35 -0800576}