blob: 4b5677a75b70dba7f834079bf14fa0dbe07f0081 [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
Joe Onorato76690122016-12-20 08:18:32 -080017#include <frameworks/base/core/proto/android/os/incident.pb.h>
Joe Onorato1754d742016-11-21 17:51:35 -080018
Joe Onorato1754d742016-11-21 17:51:35 -080019#include <map>
Yi Jinbe6de302017-10-24 12:30:24 -070020#include <set>
Yi Jin0f2599f2017-11-16 18:19:45 -080021#include <sstream>
Kweku Adams71a95312018-04-16 16:54:24 -070022#include <string>
Joe Onorato1754d742016-11-21 17:51:35 -080023
Yi Jinf8601842017-08-15 22:01:41 -070024using namespace android;
Joe Onorato1754d742016-11-21 17:51:35 -080025using namespace android::os;
26using namespace google::protobuf;
27using namespace google::protobuf::io;
28using namespace google::protobuf::internal;
29using namespace std;
30
Yi Jinbe6de302017-10-24 12:30:24 -070031/**
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 Jinbdf58942017-11-14 17:58:19 -080062 * 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 Jinbe6de302017-10-24 12:30:24 -070065 * &WindowState_state,
66 * // display id is default, nothing is generated.
67 * &WindowState_child_windows,
68 * NULL // terminator of the array
69 * };
Yi Jinbdf58942017-11-14 17:58:19 -080070 * Privacy WindowState__my_window { 1, 11, WindowState__MSG__UNSET, UNSET, NULL };
Yi Jinbe6de302017-10-24 12:30:24 -070071 *
72 * createList() {
73 * ...
Yi Jinbdf58942017-11-14 17:58:19 -080074 * WindowState_child_windows.children = WindowState__MSG_UNSET; // point to its own definition after the list is defined.
Yi Jinbe6de302017-10-24 12:30:24 -070075 * ...
76 * }
77 *
78 * const Privacy** PRIVACY_POLICY_LIST = createList();
79 * const int PRIVACY_POLICY_COUNT = 1;
Yi Jinbdf58942017-11-14 17:58:19 -080080 *
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 Jinbe6de302017-10-24 12:30:24 -070087 */
88
89// The assignments will be called when constructs PRIVACY_POLICY_LIST, has to be global variable
90vector<string> gSelfRecursionAssignments;
91
Yi Jin0ed9b682017-08-18 14:51:20 -070092static inline void emptyline() {
Joe Onorato1754d742016-11-21 17:51:35 -080093 printf("\n");
Yi Jinf8601842017-08-15 22:01:41 -070094}
Joe Onorato1754d742016-11-21 17:51:35 -080095
Yi Jin0ed9b682017-08-18 14:51:20 -070096static 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 Jinbe6de302017-10-24 12:30:24 -0700103// ======================== incident_sections =============================
Yi Jin0ed9b682017-08-18 14:51:20 -0700104static bool generateIncidentSectionsCpp(Descriptor const* descriptor)
Yi Jinf8601842017-08-15 22:01:41 -0700105{
106 generateHead("incident_sections");
107
108 map<string,FieldDescriptor const*> sections;
109 int N;
Joe Onorato1754d742016-11-21 17:51:35 -0800110 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 Jinf8601842017-08-15 22:01:41 -0700135 return true;
136}
137
Yi Jinbe6de302017-10-24 12:30:24 -0700138// ========================= section_list ===================================
Yi Jinf8601842017-08-15 22:01:41 -0700139static void splitAndPrint(const string& args) {
140 size_t base = 0;
141 size_t found;
142 while (true) {
Chih-Hung Hsieh6b3fac22018-09-17 15:12:02 -0700143 found = args.find_first_of(' ', base);
Yi Jinf8601842017-08-15 22:01:41 -0700144 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 Jinbe6de302017-10-24 12:30:24 -0700153static 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 Jin0ed9b682017-08-18 14:51:20 -0700155 size_t pos = 0, idx = 0;
Yi Jinbe6de302017-10-24 12:30:24 -0700156 char* res = new char[fieldName.size() * newS.size() + 1]; // assign a larger buffer
157 while (pos != fieldName.size()) {
158 char cur = fieldName[pos++];
Yi Jin0ed9b682017-08-18 14:51:20 -0700159 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 Jinbe6de302017-10-24 12:30:24 -0700169 string result(res);
Yunlian Jiang3809bbf2017-09-05 15:50:58 -0700170 delete [] res;
171 return result;
Yi Jin0ed9b682017-08-18 14:51:20 -0700172}
173
Yi Jinbdf58942017-11-14 17:58:19 -0800174static 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 Jin0ed9b682017-08-18 14:51:20 -0700178}
179
Yi Jinbdf58942017-11-14 17:58:19 -0800180// Get Custom Options ================================================================================
Yi Jinbe6de302017-10-24 12:30:24 -0700181static inline SectionFlags getSectionFlags(const FieldDescriptor* field) {
182 return field->options().GetExtension(section);
183}
184
185static inline PrivacyFlags getPrivacyFlags(const FieldDescriptor* field) {
186 return field->options().GetExtension(privacy);
187}
188
Yi Jinbdf58942017-11-14 17:58:19 -0800189static inline PrivacyFlags getPrivacyFlags(const Descriptor* descriptor) {
190 return descriptor->options().GetExtension(msg_privacy);
Yi Jinbe6de302017-10-24 12:30:24 -0700191}
192
Yi Jinbdf58942017-11-14 17:58:19 -0800193// Get Destinations ===================================================================================
194static 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.
199static 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 Adamsecf4bdb2018-02-22 15:05:48 -0800205// Converts Destination to a string.
206static 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 Jinbdf58942017-11-14 17:58:19 -0800217// Get Names ===========================================================================================
218static 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
224static 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 Adamsecf4bdb2018-02-22 15:05:48 -0800232// 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 -0800233static 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
244static 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 Jinbe6de302017-10-24 12:30:24 -0700247 parents->insert(descriptor->full_name());
248 for (int i=0; i<N; ++i) {
249 const FieldDescriptor* field = descriptor->field(i);
Yi Jinbdf58942017-11-14 17:58:19 -0800250 const Destination fieldDest = getFieldDest(field);
251 // If current field is not default, return false immediately
252 if (!isDefaultField(field, messageDest)) return false;
Yi Jinbe6de302017-10-24 12:30:24 -0700253 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 Jinbdf58942017-11-14 17:58:19 -0800258 if (isDefaultMessageImpl(field->message_type(), fieldDest, parents)) break;
Yi Jinbe6de302017-10-24 12:30:24 -0700259 // 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 Jinbdf58942017-11-14 17:58:19 -0800271// Recursively look at if this message is default, meaning all its fields and sub-messages
272// can be described by the same dest.
273static bool isDefaultMessage(const Descriptor* descriptor, const Destination dest) {
Yi Jinbe6de302017-10-24 12:30:24 -0700274 set<string> parents;
Yi Jinbdf58942017-11-14 17:58:19 -0800275 return isDefaultMessageImpl(descriptor, dest, &parents);
Yi Jinbe6de302017-10-24 12:30:24 -0700276}
277
Yi Jinbdf58942017-11-14 17:58:19 -0800278// ===============================================================================================================
279static bool numberInOrder(const FieldDescriptor* f1, const FieldDescriptor* f2) {
280 return f1->number() < f2->number();
281}
Yi Jinbe6de302017-10-24 12:30:24 -0700282
Yi Jinbdf58942017-11-14 17:58:19 -0800283// field numbers are possibly out of order, sort them here.
284static 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
298static 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 Jinbe6de302017-10-24 12:30:24 -0700306 }
307 // insert the message type name so sub-message will figure out if self-recursion occurs
Yi Jinbdf58942017-11-14 17:58:19 -0800308 parents->insert(messageName);
Yi Jinbe6de302017-10-24 12:30:24 -0700309
Yi Jinbdf58942017-11-14 17:58:19 -0800310 // 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 Jinbe6de302017-10-24 12:30:24 -0700315 const string fieldName = getFieldName(field);
Yi Jinbdf58942017-11-14 17:58:19 -0800316 const Destination fieldDest = getFieldDest(field);
Yi Jin0ed9b682017-08-18 14:51:20 -0700317
Yi Jinbdf58942017-11-14 17:58:19 -0800318 if (variableNames.find(fieldName) != variableNames.end()) {
319 hasDefaultFlags[i] = variableNames[fieldName];
320 continue;
321 }
322 hasDefaultFlags[i] = isDefaultField(field, messageDest);
323
Yi Jinbe6de302017-10-24 12:30:24 -0700324 string fieldMessageName;
Yi Jinbdf58942017-11-14 17:58:19 -0800325 PrivacyFlags p = getPrivacyFlags(field);
Yi Jin0ed9b682017-08-18 14:51:20 -0700326 switch (field->type()) {
327 case FieldDescriptor::TYPE_MESSAGE:
Yi Jinbdf58942017-11-14 17:58:19 -0800328 fieldMessageName = getMessageName(field->message_type(), fieldDest);
Yi Jinbe6de302017-10-24 12:30:24 -0700329 if (parents->find(fieldMessageName) != parents->end()) { // Self-Recursion proto definition
Yi Jinbdf58942017-11-14 17:58:19 -0800330 if (hasDefaultFlags[i]) {
331 hasDefaultFlags[i] = isDefaultMessage(field->message_type(), fieldDest);
Yi Jinbe6de302017-10-24 12:30:24 -0700332 }
333 if (!hasDefaultFlags[i]) {
Yi Jinbdf58942017-11-14 17:58:19 -0800334 printPrivacy(fieldName, field, "NULL", fieldDest, "NULL",
335 " // self recursion field of " + fieldMessageName);
Yi Jinbe6de302017-10-24 12:30:24 -0700336 // generate the assignment and used to construct createList function later on.
337 gSelfRecursionAssignments.push_back(fieldName + ".children = " + fieldMessageName);
338 }
Yi Jinbdf58942017-11-14 17:58:19 -0800339 } 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 Jin22769e02017-10-16 14:42:50 -0700346 }
Yi Jin0ed9b682017-08-18 14:51:20 -0700347 break;
348 case FieldDescriptor::TYPE_STRING:
Yi Jinbdf58942017-11-14 17:58:19 -0800349 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 Jin0ed9b682017-08-18 14:51:20 -0700359 }
Yi Jinbdf58942017-11-14 17:58:19 -0800360 // else treat string field as primitive field and goes to default
Yi Jin0ed9b682017-08-18 14:51:20 -0700361 default:
Yi Jinbdf58942017-11-14 17:58:19 -0800362 if (!hasDefaultFlags[i]) printPrivacy(fieldName, field, "NULL", fieldDest, "NULL");
Yi Jin0ed9b682017-08-18 14:51:20 -0700363 }
Yi Jinbdf58942017-11-14 17:58:19 -0800364 // Don't generate a variable twice
365 if (!hasDefaultFlags[i]) variableNames[fieldName] = false;
Yi Jin0ed9b682017-08-18 14:51:20 -0700366 }
367
368 bool allDefaults = true;
Yi Jinbdf58942017-11-14 17:58:19 -0800369 for (size_t i=0; i<fieldsInOrder.size(); i++) {
Yi Jin0ed9b682017-08-18 14:51:20 -0700370 allDefaults &= hasDefaultFlags[i];
371 }
Yi Jinbe6de302017-10-24 12:30:24 -0700372
Yi Jinbdf58942017-11-14 17:58:19 -0800373 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 Jinbe6de302017-10-24 12:30:24 -0700375
Yi Jin22769e02017-10-16 14:42:50 -0700376 if (allDefaults) return false;
Yi Jin0ed9b682017-08-18 14:51:20 -0700377
378 emptyline();
Yi Jin7e0b4e52017-09-12 20:00:25 -0700379 int policyCount = 0;
Yi Jinbdf58942017-11-14 17:58:19 -0800380 printf("Privacy* %s[] = {\n", messageName.c_str());
381 for (size_t i=0; i<fieldsInOrder.size(); i++) {
382 const FieldDescriptor* field = fieldsInOrder[i];
Yi Jin0ed9b682017-08-18 14:51:20 -0700383 if (hasDefaultFlags[i]) continue;
Yi Jinbe6de302017-10-24 12:30:24 -0700384 printf(" &%s,\n", getFieldName(field).c_str());
Yi Jin7e0b4e52017-09-12 20:00:25 -0700385 policyCount++;
Yi Jin0ed9b682017-08-18 14:51:20 -0700386 }
Yi Jinbe6de302017-10-24 12:30:24 -0700387 printf(" NULL };\n");
Yi Jin0ed9b682017-08-18 14:51:20 -0700388 emptyline();
Yi Jin22769e02017-10-16 14:42:50 -0700389 return true;
Yi Jin0ed9b682017-08-18 14:51:20 -0700390}
391
392static bool generateSectionListCpp(Descriptor const* descriptor) {
Yi Jinf8601842017-08-15 22:01:41 -0700393 generateHead("section_list");
394
Yi Jin6cacbcb2018-03-30 14:04:52 -0700395 // generate namespaces
396 printf("namespace android {\n");
397 printf("namespace os {\n");
398 printf("namespace incidentd {\n");
399
Yi Jin0ed9b682017-08-18 14:51:20 -0700400 // generates SECTION_LIST
Yi Jinbe6de302017-10-24 12:30:24 -0700401 printf("// Generate SECTION_LIST.\n\n");
402
Yi Jinf8601842017-08-15 22:01:41 -0700403 printf("const Section* SECTION_LIST[] = {\n");
Yi Jinf8601842017-08-15 22:01:41 -0700404 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 Jinbe6de302017-10-24 12:30:24 -0700410 const SectionFlags s = getSectionFlags(field);
Yi Jinf8601842017-08-15 22:01:41 -0700411 switch (s.type()) {
412 case SECTION_NONE:
413 continue;
414 case SECTION_FILE:
Yi Jin7fe3dee2018-04-16 16:13:04 -0700415 printf(" new FileSection(%d, \"%s\", %s),\n", field->number(), s.args().c_str(),
416 s.device_specific() ? "true" : "false");
Yi Jinf8601842017-08-15 22:01:41 -0700417 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:
Yi Jin480a9562018-05-14 18:04:29 -0700424 printf(" new DumpsysSection(%d, %s,", field->number(),
Kweku Adams71a95312018-04-16 16:54:24 -0700425 s.userdebug_and_eng_only() ? "true" : "false");
Yi Jinf8601842017-08-15 22:01:41 -0700426 splitAndPrint(s.args());
427 printf(" NULL),\n");
428 break;
Yi Jin3c034c92017-12-22 17:36:47 -0800429 case SECTION_LOG:
430 printf(" new LogSection(%d, %s),\n", field->number(), s.args().c_str());
431 break;
Yi Jin1a11fa12018-02-22 16:44:10 -0800432 case SECTION_GZIP:
433 printf(" new GZipSection(%d,", field->number());
434 splitAndPrint(s.args());
435 printf(" NULL),\n");
Yi Jin934cc612018-03-15 14:14:26 -0700436 break;
Kweku Adamseadd1232018-02-05 16:45:13 -0800437 case SECTION_TOMBSTONE:
438 printf(" new TombstoneSection(%d, \"%s\"),\n", field->number(), s.args().c_str());
Yi Jin1a11fa12018-02-22 16:44:10 -0800439 break;
Yi Jinf8601842017-08-15 22:01:41 -0700440 }
441 }
Yi Jin0ed9b682017-08-18 14:51:20 -0700442 printf(" NULL };\n");
Yi Jinbe6de302017-10-24 12:30:24 -0700443
444 emptyline();
445 printf("// =============================================================================\n");
Yi Jin0ed9b682017-08-18 14:51:20 -0700446 emptyline();
447
Yi Jinbe6de302017-10-24 12:30:24 -0700448 // generates PRIVACY_POLICY_LIST
449 printf("// Generate PRIVACY_POLICY_LIST.\n\n");
Yi Jinbdf58942017-11-14 17:58:19 -0800450 map<string, bool> variableNames;
Yi Jinbe6de302017-10-24 12:30:24 -0700451 set<string> parents;
Yi Jinbdf58942017-11-14 17:58:19 -0800452 vector<const FieldDescriptor*> fieldsInOrder = sortFields(descriptor);
453 bool skip[fieldsInOrder.size()];
454 const Destination incidentDest = getPrivacyFlags(descriptor).dest();
Yi Jinbe6de302017-10-24 12:30:24 -0700455
Yi Jinbdf58942017-11-14 17:58:19 -0800456 for (size_t i=0; i<fieldsInOrder.size(); i++) {
457 const FieldDescriptor* field = fieldsInOrder[i];
Yi Jinbe6de302017-10-24 12:30:24 -0700458 const string fieldName = getFieldName(field);
Yi Jinbdf58942017-11-14 17:58:19 -0800459 const Destination fieldDest = getFieldDest(field);
460 const string fieldMessageName = getMessageName(field->message_type(), fieldDest);
Yi Jinbe6de302017-10-24 12:30:24 -0700461
462 skip[i] = true;
463
464 if (field->type() != FieldDescriptor::TYPE_MESSAGE) {
465 continue;
466 }
Yi Jinbdf58942017-11-14 17:58:19 -0800467 // 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 Jinbe6de302017-10-24 12:30:24 -0700471 continue; // don't create a new privacy if the value is default.
472 } else {
Yi Jinbdf58942017-11-14 17:58:19 -0800473 printPrivacy(fieldName, field, "NULL", fieldDest, "NULL");
Yi Jinbe6de302017-10-24 12:30:24 -0700474 }
475 skip[i] = false;
Yi Jin0ed9b682017-08-18 14:51:20 -0700476 }
477
Yi Jinbe6de302017-10-24 12:30:24 -0700478 // generate final PRIVACY_POLICY_LIST
479 emptyline();
480 int policyCount = 0;
481 if (gSelfRecursionAssignments.empty()) {
482 printf("Privacy* privacyArray[] = {\n");
Yi Jinbdf58942017-11-14 17:58:19 -0800483 for (size_t i=0; i<fieldsInOrder.size(); i++) {
Yi Jinbe6de302017-10-24 12:30:24 -0700484 if (skip[i]) continue;
Yi Jinbdf58942017-11-14 17:58:19 -0800485 printf(" &%s,\n", getFieldName(fieldsInOrder[i]).c_str());
Yi Jinbe6de302017-10-24 12:30:24 -0700486 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 Jinbdf58942017-11-14 17:58:19 -0800492 for (size_t i=0; i<fieldsInOrder.size(); i++) {
Yi Jinbe6de302017-10-24 12:30:24 -0700493 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 Jinbdf58942017-11-14 17:58:19 -0800502 for (size_t i=0; i<fieldsInOrder.size(); i++) {
Yi Jinbe6de302017-10-24 12:30:24 -0700503 if (skip[i]) continue;
Yi Jinbdf58942017-11-14 17:58:19 -0800504 printf(" privacyArray[%d] = &%s;\n", policyCount++, getFieldName(fieldsInOrder[i]).c_str());
Yi Jinbe6de302017-10-24 12:30:24 -0700505 }
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 Jin6cacbcb2018-03-30 14:04:52 -0700511
512 printf("} // incidentd\n");
513 printf("} // os\n");
514 printf("} // android\n");
Yi Jinf8601842017-08-15 22:01:41 -0700515 return true;
516}
517
518// ================================================================================
Yi Jin0f2599f2017-11-16 18:19:45 -0800519static 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 Adamsecf4bdb2018-02-22 15:05:48 -0800531static void generateCsv(Descriptor const* descriptor, const string& indent, set<string>* parents, const Destination containerDest = DEST_UNSET) {
Yi Jin0f2599f2017-11-16 18:19:45 -0800532 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 Adamsecf4bdb2018-02-22 15:05:48 -0800536 const Destination fieldDest = getFieldDest(field);
Yi Jin0f2599f2017-11-16 18:19:45 -0800537 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 Adamsecf4bdb2018-02-22 15:05:48 -0800544 text << " (PRIVACY=";
545 if (isDefaultField(field, containerDest)) {
546 text << getDestString(containerDest);
547 } else {
548 text << getDestString(fieldDest);
549 }
550 text << ")";
Yi Jin0f2599f2017-11-16 18:19:45 -0800551 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 Adamsecf4bdb2018-02-22 15:05:48 -0800555 generateCsv(field->message_type(), indent + ",", parents, fieldDest);
Yi Jin0f2599f2017-11-16 18:19:45 -0800556 parents->erase(field->message_type()->full_name());
557 }
558 }
559}
560
561// ================================================================================
Yi Jinf8601842017-08-15 22:01:41 -0700562int main(int argc, char const *argv[])
563{
Yi Jin0f2599f2017-11-16 18:19:45 -0800564 if (argc < 2) return 1;
Yi Jinf8601842017-08-15 22:01:41 -0700565 const char* module = argv[1];
566
Yi Jin0ed9b682017-08-18 14:51:20 -0700567 Descriptor const* descriptor = IncidentProto::descriptor();
568
Yi Jinf8601842017-08-15 22:01:41 -0700569 if (strcmp(module, "incident") == 0) {
Yi Jin0ed9b682017-08-18 14:51:20 -0700570 return !generateIncidentSectionsCpp(descriptor);
Yi Jinf8601842017-08-15 22:01:41 -0700571 }
572 if (strcmp(module, "incidentd") == 0 ) {
Yi Jin0ed9b682017-08-18 14:51:20 -0700573 return !generateSectionListCpp(descriptor);
Yi Jinf8601842017-08-15 22:01:41 -0700574 }
Yi Jin0f2599f2017-11-16 18:19:45 -0800575 // 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 Adamsecf4bdb2018-02-22 15:05:48 -0800584 generateCsv(field->message_type(), "", &parents, getFieldDest(field));
Yi Jin0f2599f2017-11-16 18:19:45 -0800585 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 Jinf8601842017-08-15 22:01:41 -0700593 return 1;
Joe Onorato1754d742016-11-21 17:51:35 -0800594}