blob: 639f98062b719e16086f28be5cd924c8af66e126 [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);
Yi Jin908c02f2018-06-22 16:51:40 -0700113 sections[field->name()] = field;
Joe Onorato1754d742016-11-21 17:51:35 -0800114 }
115
116 printf("IncidentSection const INCIDENT_SECTIONS[] = {\n");
117 N = sections.size();
118 int i = 0;
119 for (map<string,FieldDescriptor const*>::const_iterator it = sections.begin();
120 it != sections.end(); it++, i++) {
121 const FieldDescriptor* field = it->second;
122 printf(" { %d, \"%s\" }", field->number(), field->name().c_str());
123 if (i != N-1) {
124 printf(",\n");
125 } else {
126 printf("\n");
127 }
128 }
129 printf("};\n");
130
131 printf("const int INCIDENT_SECTION_COUNT = %d;\n", N);
132
Yi Jinf8601842017-08-15 22:01:41 -0700133 return true;
134}
135
Yi Jinbe6de302017-10-24 12:30:24 -0700136// ========================= section_list ===================================
Yi Jinf8601842017-08-15 22:01:41 -0700137static void splitAndPrint(const string& args) {
138 size_t base = 0;
139 size_t found;
140 while (true) {
141 found = args.find_first_of(" ", base);
142 if (found != base) {
143 string arg = args.substr(base, found - base);
144 printf(" \"%s\",", arg.c_str());
145 }
146 if (found == args.npos) break;
147 base = found + 1;
148 }
149}
150
Yi Jinbe6de302017-10-24 12:30:24 -0700151static string replaceAll(const string& fieldName, const char oldC, const string& newS) {
152 if (fieldName.find_first_of(oldC) == fieldName.npos) return fieldName.c_str();
Yi Jin0ed9b682017-08-18 14:51:20 -0700153 size_t pos = 0, idx = 0;
Yi Jinbe6de302017-10-24 12:30:24 -0700154 char* res = new char[fieldName.size() * newS.size() + 1]; // assign a larger buffer
155 while (pos != fieldName.size()) {
156 char cur = fieldName[pos++];
Yi Jin0ed9b682017-08-18 14:51:20 -0700157 if (cur != oldC) {
158 res[idx++] = cur;
159 continue;
160 }
161
162 for (size_t i=0; i<newS.size(); i++) {
163 res[idx++] = newS[i];
164 }
165 }
166 res[idx] = '\0';
Yi Jinbe6de302017-10-24 12:30:24 -0700167 string result(res);
Yunlian Jiang3809bbf2017-09-05 15:50:58 -0700168 delete [] res;
169 return result;
Yi Jin0ed9b682017-08-18 14:51:20 -0700170}
171
Yi Jinbdf58942017-11-14 17:58:19 -0800172static inline void printPrivacy(const string& name, const FieldDescriptor* field, const string& children,
173 const Destination dest, const string& patterns, const string& comments = "") {
174 printf("Privacy %s = { %d, %d, %s, %d, %s };%s\n", name.c_str(), field->number(), field->type(),
175 children.c_str(), dest, patterns.c_str(), comments.c_str());
Yi Jin0ed9b682017-08-18 14:51:20 -0700176}
177
Yi Jinbdf58942017-11-14 17:58:19 -0800178// Get Custom Options ================================================================================
Yi Jinbe6de302017-10-24 12:30:24 -0700179static inline SectionFlags getSectionFlags(const FieldDescriptor* field) {
180 return field->options().GetExtension(section);
181}
182
183static inline PrivacyFlags getPrivacyFlags(const FieldDescriptor* field) {
184 return field->options().GetExtension(privacy);
185}
186
Yi Jinbdf58942017-11-14 17:58:19 -0800187static inline PrivacyFlags getPrivacyFlags(const Descriptor* descriptor) {
188 return descriptor->options().GetExtension(msg_privacy);
Yi Jinbe6de302017-10-24 12:30:24 -0700189}
190
Yi Jinbdf58942017-11-14 17:58:19 -0800191// Get Destinations ===================================================================================
192static inline Destination getMessageDest(const Descriptor* descriptor, const Destination overridden) {
193 return overridden != DEST_UNSET ? overridden : getPrivacyFlags(descriptor).dest();
194}
195
196// Returns field's own dest, when it is a message field, uses its message default tag if unset.
197static inline Destination getFieldDest(const FieldDescriptor* field) {
198 Destination fieldDest = getPrivacyFlags(field).dest();
199 return field->type() != FieldDescriptor::TYPE_MESSAGE ? fieldDest :
200 getMessageDest(field->message_type(), fieldDest);
201}
202
Kweku Adamsecf4bdb2018-02-22 15:05:48 -0800203// Converts Destination to a string.
204static inline string getDestString(const Destination dest) {
205 switch (dest) {
206 case DEST_AUTOMATIC: return "AUTOMATIC";
207 case DEST_LOCAL: return "LOCAL";
208 case DEST_EXPLICIT: return "EXPLICIT";
209 // UNSET is considered EXPLICIT by default.
210 case DEST_UNSET: return "EXPLICIT";
211 default: return "UNKNOWN";
212 }
213}
214
Yi Jinbdf58942017-11-14 17:58:19 -0800215// Get Names ===========================================================================================
216static inline string getFieldName(const FieldDescriptor* field) {
217 // replace . with double underscores to avoid name conflicts since fields use snake naming convention
218 return replaceAll(field->full_name(), '.', "__");
219}
220
221
222static inline string getMessageName(const Descriptor* descriptor, const Destination overridden) {
223 // replace . with one underscore since messages use camel naming convention
224 return replaceAll(descriptor->full_name(), '.', "_") + "__MSG__" +
225 to_string(getMessageDest(descriptor, overridden));
226}
227
228// IsDefault ============================================================================================
229// 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 -0800230// 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 -0800231static inline bool isDefaultField(const FieldDescriptor* field, const Destination containerDest) {
232 Destination fieldDest = getFieldDest(field);
233 if (field->type() != FieldDescriptor::TYPE_MESSAGE) {
234 return fieldDest == containerDest || (fieldDest == DEST_UNSET);
235 } else {
236 return fieldDest == containerDest ||
237 (containerDest == DEST_UNSET && fieldDest == DEST_EXPLICIT) ||
238 (containerDest == DEST_EXPLICIT && fieldDest == DEST_UNSET);
239 }
240}
241
242static bool isDefaultMessageImpl(const Descriptor* descriptor, const Destination dest, set<string>* parents) {
243 const int N = descriptor->field_count();
244 const Destination messageDest = getMessageDest(descriptor, dest);
Yi Jinbe6de302017-10-24 12:30:24 -0700245 parents->insert(descriptor->full_name());
246 for (int i=0; i<N; ++i) {
247 const FieldDescriptor* field = descriptor->field(i);
Yi Jinbdf58942017-11-14 17:58:19 -0800248 const Destination fieldDest = getFieldDest(field);
249 // If current field is not default, return false immediately
250 if (!isDefaultField(field, messageDest)) return false;
Yi Jinbe6de302017-10-24 12:30:24 -0700251 switch (field->type()) {
252 case FieldDescriptor::TYPE_MESSAGE:
253 // if self recursion, don't go deep.
254 if (parents->find(field->message_type()->full_name()) != parents->end()) break;
255 // if is a default message, just continue
Yi Jinbdf58942017-11-14 17:58:19 -0800256 if (isDefaultMessageImpl(field->message_type(), fieldDest, parents)) break;
Yi Jinbe6de302017-10-24 12:30:24 -0700257 // sub message is not default, so this message is always not default
258 return false;
259 case FieldDescriptor::TYPE_STRING:
260 if (getPrivacyFlags(field).patterns_size() != 0) return false;
261 default:
262 continue;
263 }
264 }
265 parents->erase(descriptor->full_name());
266 return true;
267}
268
Yi Jinbdf58942017-11-14 17:58:19 -0800269// Recursively look at if this message is default, meaning all its fields and sub-messages
270// can be described by the same dest.
271static bool isDefaultMessage(const Descriptor* descriptor, const Destination dest) {
Yi Jinbe6de302017-10-24 12:30:24 -0700272 set<string> parents;
Yi Jinbdf58942017-11-14 17:58:19 -0800273 return isDefaultMessageImpl(descriptor, dest, &parents);
Yi Jinbe6de302017-10-24 12:30:24 -0700274}
275
Yi Jinbdf58942017-11-14 17:58:19 -0800276// ===============================================================================================================
277static bool numberInOrder(const FieldDescriptor* f1, const FieldDescriptor* f2) {
278 return f1->number() < f2->number();
279}
Yi Jinbe6de302017-10-24 12:30:24 -0700280
Yi Jinbdf58942017-11-14 17:58:19 -0800281// field numbers are possibly out of order, sort them here.
282static vector<const FieldDescriptor*> sortFields(const Descriptor* descriptor) {
283 vector<const FieldDescriptor*> fields;
284 fields.reserve(descriptor->field_count());
285 for (int i=0; i<descriptor->field_count(); i++) {
286 fields.push_back(descriptor->field(i));
287 }
288 std::sort(fields.begin(), fields.end(), numberInOrder);
289 return fields;
290}
291
292// This function looks for privacy tags of a message type and recursively its sub-messages.
293// It generates Privacy objects for each non-default fields including non-default sub-messages.
294// And if the message has Privacy objects generated, it returns a list of them.
295// Returns false if the descriptor doesn't have any non default privacy flags set, including its submessages
296static bool generatePrivacyFlags(const Descriptor* descriptor, const Destination overridden,
297 map<string, bool> &variableNames, set<string>* parents) {
298 const string messageName = getMessageName(descriptor, overridden);
299 const Destination messageDest = getMessageDest(descriptor, overridden);
300
301 if (variableNames.find(messageName) != variableNames.end()) {
302 bool hasDefault = variableNames[messageName];
303 return !hasDefault; // if has default, then don't generate privacy flags.
Yi Jinbe6de302017-10-24 12:30:24 -0700304 }
305 // insert the message type name so sub-message will figure out if self-recursion occurs
Yi Jinbdf58942017-11-14 17:58:19 -0800306 parents->insert(messageName);
Yi Jinbe6de302017-10-24 12:30:24 -0700307
Yi Jinbdf58942017-11-14 17:58:19 -0800308 // sort fields based on number, iterate though them and generate sub flags first
309 vector<const FieldDescriptor*> fieldsInOrder = sortFields(descriptor);
310 bool hasDefaultFlags[fieldsInOrder.size()];
311 for (size_t i=0; i<fieldsInOrder.size(); i++) {
312 const FieldDescriptor* field = fieldsInOrder[i];
Yi Jinbe6de302017-10-24 12:30:24 -0700313 const string fieldName = getFieldName(field);
Yi Jinbdf58942017-11-14 17:58:19 -0800314 const Destination fieldDest = getFieldDest(field);
Yi Jin0ed9b682017-08-18 14:51:20 -0700315
Yi Jinbdf58942017-11-14 17:58:19 -0800316 if (variableNames.find(fieldName) != variableNames.end()) {
317 hasDefaultFlags[i] = variableNames[fieldName];
318 continue;
319 }
320 hasDefaultFlags[i] = isDefaultField(field, messageDest);
321
Yi Jinbe6de302017-10-24 12:30:24 -0700322 string fieldMessageName;
Yi Jinbdf58942017-11-14 17:58:19 -0800323 PrivacyFlags p = getPrivacyFlags(field);
Yi Jin0ed9b682017-08-18 14:51:20 -0700324 switch (field->type()) {
325 case FieldDescriptor::TYPE_MESSAGE:
Yi Jinbdf58942017-11-14 17:58:19 -0800326 fieldMessageName = getMessageName(field->message_type(), fieldDest);
Yi Jinbe6de302017-10-24 12:30:24 -0700327 if (parents->find(fieldMessageName) != parents->end()) { // Self-Recursion proto definition
Yi Jinbdf58942017-11-14 17:58:19 -0800328 if (hasDefaultFlags[i]) {
329 hasDefaultFlags[i] = isDefaultMessage(field->message_type(), fieldDest);
Yi Jinbe6de302017-10-24 12:30:24 -0700330 }
331 if (!hasDefaultFlags[i]) {
Yi Jinbdf58942017-11-14 17:58:19 -0800332 printPrivacy(fieldName, field, "NULL", fieldDest, "NULL",
333 " // self recursion field of " + fieldMessageName);
Yi Jinbe6de302017-10-24 12:30:24 -0700334 // generate the assignment and used to construct createList function later on.
335 gSelfRecursionAssignments.push_back(fieldName + ".children = " + fieldMessageName);
336 }
Yi Jinbdf58942017-11-14 17:58:19 -0800337 } else if (generatePrivacyFlags(field->message_type(), p.dest(), variableNames, parents)) {
338 if (variableNames.find(fieldName) == variableNames.end()) {
339 printPrivacy(fieldName, field, fieldMessageName, fieldDest, "NULL");
340 }
341 hasDefaultFlags[i] = false;
342 } else if (!hasDefaultFlags[i]) {
343 printPrivacy(fieldName, field, "NULL", fieldDest, "NULL");
Yi Jin22769e02017-10-16 14:42:50 -0700344 }
Yi Jin0ed9b682017-08-18 14:51:20 -0700345 break;
346 case FieldDescriptor::TYPE_STRING:
Yi Jinbdf58942017-11-14 17:58:19 -0800347 if (p.patterns_size() != 0) { // if patterns are specified
348 if (hasDefaultFlags[i]) break;
349 printf("const char* %s_patterns[] = {\n", fieldName.c_str());
350 for (int j=0; j<p.patterns_size(); j++) {
351 // generated string needs to escape backslash too, duplicate it to allow escape again.
352 printf(" \"%s\",\n", replaceAll(p.patterns(j), '\\', "\\\\").c_str());
353 }
354 printf(" NULL };\n");
355 printPrivacy(fieldName, field, "NULL", fieldDest, fieldName + "_patterns");
356 break;
Yi Jin0ed9b682017-08-18 14:51:20 -0700357 }
Yi Jinbdf58942017-11-14 17:58:19 -0800358 // else treat string field as primitive field and goes to default
Yi Jin0ed9b682017-08-18 14:51:20 -0700359 default:
Yi Jinbdf58942017-11-14 17:58:19 -0800360 if (!hasDefaultFlags[i]) printPrivacy(fieldName, field, "NULL", fieldDest, "NULL");
Yi Jin0ed9b682017-08-18 14:51:20 -0700361 }
Yi Jinbdf58942017-11-14 17:58:19 -0800362 // Don't generate a variable twice
363 if (!hasDefaultFlags[i]) variableNames[fieldName] = false;
Yi Jin0ed9b682017-08-18 14:51:20 -0700364 }
365
366 bool allDefaults = true;
Yi Jinbdf58942017-11-14 17:58:19 -0800367 for (size_t i=0; i<fieldsInOrder.size(); i++) {
Yi Jin0ed9b682017-08-18 14:51:20 -0700368 allDefaults &= hasDefaultFlags[i];
369 }
Yi Jinbe6de302017-10-24 12:30:24 -0700370
Yi Jinbdf58942017-11-14 17:58:19 -0800371 parents->erase(messageName); // erase the message type name when exit the message.
372 variableNames[messageName] = allDefaults; // store the privacy tags of the message here to avoid overhead.
Yi Jinbe6de302017-10-24 12:30:24 -0700373
Yi Jin22769e02017-10-16 14:42:50 -0700374 if (allDefaults) return false;
Yi Jin0ed9b682017-08-18 14:51:20 -0700375
376 emptyline();
Yi Jin7e0b4e52017-09-12 20:00:25 -0700377 int policyCount = 0;
Yi Jinbdf58942017-11-14 17:58:19 -0800378 printf("Privacy* %s[] = {\n", messageName.c_str());
379 for (size_t i=0; i<fieldsInOrder.size(); i++) {
380 const FieldDescriptor* field = fieldsInOrder[i];
Yi Jin0ed9b682017-08-18 14:51:20 -0700381 if (hasDefaultFlags[i]) continue;
Yi Jinbe6de302017-10-24 12:30:24 -0700382 printf(" &%s,\n", getFieldName(field).c_str());
Yi Jin7e0b4e52017-09-12 20:00:25 -0700383 policyCount++;
Yi Jin0ed9b682017-08-18 14:51:20 -0700384 }
Yi Jinbe6de302017-10-24 12:30:24 -0700385 printf(" NULL };\n");
Yi Jin0ed9b682017-08-18 14:51:20 -0700386 emptyline();
Yi Jin22769e02017-10-16 14:42:50 -0700387 return true;
Yi Jin0ed9b682017-08-18 14:51:20 -0700388}
389
390static bool generateSectionListCpp(Descriptor const* descriptor) {
Yi Jinf8601842017-08-15 22:01:41 -0700391 generateHead("section_list");
392
Yi Jin6cacbcb2018-03-30 14:04:52 -0700393 // generate namespaces
394 printf("namespace android {\n");
395 printf("namespace os {\n");
396 printf("namespace incidentd {\n");
397
Yi Jin0ed9b682017-08-18 14:51:20 -0700398 // generates SECTION_LIST
Yi Jinbe6de302017-10-24 12:30:24 -0700399 printf("// Generate SECTION_LIST.\n\n");
400
Yi Jinf8601842017-08-15 22:01:41 -0700401 printf("const Section* SECTION_LIST[] = {\n");
Yi Jinf8601842017-08-15 22:01:41 -0700402 for (int i=0; i<descriptor->field_count(); i++) {
403 const FieldDescriptor* field = descriptor->field(i);
404
Yi Jin908c02f2018-06-22 16:51:40 -0700405 if (field->type() != FieldDescriptor::TYPE_MESSAGE && field->type() != FieldDescriptor::TYPE_STRING) {
Yi Jinf8601842017-08-15 22:01:41 -0700406 continue;
407 }
Yi Jinbe6de302017-10-24 12:30:24 -0700408 const SectionFlags s = getSectionFlags(field);
Yi Jinf8601842017-08-15 22:01:41 -0700409 switch (s.type()) {
410 case SECTION_NONE:
411 continue;
412 case SECTION_FILE:
Kweku Adamse04ef772018-06-13 12:24:38 -0700413 printf(" new FileSection(%d, \"%s\"),\n", field->number(), s.args().c_str());
Yi Jinf8601842017-08-15 22:01:41 -0700414 break;
415 case SECTION_COMMAND:
416 printf(" new CommandSection(%d,", field->number());
417 splitAndPrint(s.args());
418 printf(" NULL),\n");
419 break;
420 case SECTION_DUMPSYS:
Yi Jin480a9562018-05-14 18:04:29 -0700421 printf(" new DumpsysSection(%d, %s,", field->number(),
Kweku Adams71a95312018-04-16 16:54:24 -0700422 s.userdebug_and_eng_only() ? "true" : "false");
Yi Jinf8601842017-08-15 22:01:41 -0700423 splitAndPrint(s.args());
424 printf(" NULL),\n");
425 break;
Yi Jin3c034c92017-12-22 17:36:47 -0800426 case SECTION_LOG:
427 printf(" new LogSection(%d, %s),\n", field->number(), s.args().c_str());
428 break;
Yi Jin1a11fa12018-02-22 16:44:10 -0800429 case SECTION_GZIP:
430 printf(" new GZipSection(%d,", field->number());
431 splitAndPrint(s.args());
432 printf(" NULL),\n");
Yi Jin934cc612018-03-15 14:14:26 -0700433 break;
Kweku Adamseadd1232018-02-05 16:45:13 -0800434 case SECTION_TOMBSTONE:
435 printf(" new TombstoneSection(%d, \"%s\"),\n", field->number(), s.args().c_str());
Yi Jin1a11fa12018-02-22 16:44:10 -0800436 break;
Yi Jinf8601842017-08-15 22:01:41 -0700437 }
438 }
Yi Jin0ed9b682017-08-18 14:51:20 -0700439 printf(" NULL };\n");
Yi Jinbe6de302017-10-24 12:30:24 -0700440
441 emptyline();
442 printf("// =============================================================================\n");
Yi Jin0ed9b682017-08-18 14:51:20 -0700443 emptyline();
444
Yi Jinbe6de302017-10-24 12:30:24 -0700445 // generates PRIVACY_POLICY_LIST
446 printf("// Generate PRIVACY_POLICY_LIST.\n\n");
Yi Jinbdf58942017-11-14 17:58:19 -0800447 map<string, bool> variableNames;
Yi Jinbe6de302017-10-24 12:30:24 -0700448 set<string> parents;
Yi Jinbdf58942017-11-14 17:58:19 -0800449 vector<const FieldDescriptor*> fieldsInOrder = sortFields(descriptor);
450 bool skip[fieldsInOrder.size()];
451 const Destination incidentDest = getPrivacyFlags(descriptor).dest();
Yi Jinbe6de302017-10-24 12:30:24 -0700452
Yi Jinbdf58942017-11-14 17:58:19 -0800453 for (size_t i=0; i<fieldsInOrder.size(); i++) {
454 const FieldDescriptor* field = fieldsInOrder[i];
Yi Jinbe6de302017-10-24 12:30:24 -0700455 const string fieldName = getFieldName(field);
Yi Jinbdf58942017-11-14 17:58:19 -0800456 const Destination fieldDest = getFieldDest(field);
Yi Jinbe6de302017-10-24 12:30:24 -0700457 if (field->type() != FieldDescriptor::TYPE_MESSAGE) {
Yi Jin908c02f2018-06-22 16:51:40 -0700458 printPrivacy(fieldName, field, "NULL", fieldDest, "NULL");
Yi Jinbe6de302017-10-24 12:30:24 -0700459 continue;
460 }
Yi Jin908c02f2018-06-22 16:51:40 -0700461
462 skip[i] = true;
463 const string fieldMessageName = getMessageName(field->message_type(), fieldDest);
Yi Jinbdf58942017-11-14 17:58:19 -0800464 // generate privacy flags for each section.
465 if (generatePrivacyFlags(field->message_type(), fieldDest, variableNames, &parents)) {
466 printPrivacy(fieldName, field, fieldMessageName, fieldDest, "NULL");
467 } else if (isDefaultField(field, incidentDest)) {
Yi Jinbe6de302017-10-24 12:30:24 -0700468 continue; // don't create a new privacy if the value is default.
469 } else {
Yi Jinbdf58942017-11-14 17:58:19 -0800470 printPrivacy(fieldName, field, "NULL", fieldDest, "NULL");
Yi Jinbe6de302017-10-24 12:30:24 -0700471 }
472 skip[i] = false;
Yi Jin0ed9b682017-08-18 14:51:20 -0700473 }
474
Yi Jinbe6de302017-10-24 12:30:24 -0700475 // generate final PRIVACY_POLICY_LIST
476 emptyline();
477 int policyCount = 0;
478 if (gSelfRecursionAssignments.empty()) {
479 printf("Privacy* privacyArray[] = {\n");
Yi Jinbdf58942017-11-14 17:58:19 -0800480 for (size_t i=0; i<fieldsInOrder.size(); i++) {
Yi Jinbe6de302017-10-24 12:30:24 -0700481 if (skip[i]) continue;
Yi Jinbdf58942017-11-14 17:58:19 -0800482 printf(" &%s,\n", getFieldName(fieldsInOrder[i]).c_str());
Yi Jinbe6de302017-10-24 12:30:24 -0700483 policyCount++;
484 }
485 printf("};\n\n");
486 printf("const Privacy** PRIVACY_POLICY_LIST = const_cast<const Privacy**>(privacyArray);\n\n");
487 printf("const int PRIVACY_POLICY_COUNT = %d;\n", policyCount);
488 } else {
Yi Jinbdf58942017-11-14 17:58:19 -0800489 for (size_t i=0; i<fieldsInOrder.size(); i++) {
Yi Jinbe6de302017-10-24 12:30:24 -0700490 if (!skip[i]) policyCount++;
491 }
492
493 printf("static const Privacy** createList() {\n");
494 for (size_t i=0; i<gSelfRecursionAssignments.size(); ++i) {
495 printf(" %s;\n", gSelfRecursionAssignments[i].c_str());
496 }
497 printf(" Privacy** privacyArray = (Privacy**)malloc(%d * sizeof(Privacy**));\n", policyCount);
498 policyCount = 0; // reset
Yi Jinbdf58942017-11-14 17:58:19 -0800499 for (size_t i=0; i<fieldsInOrder.size(); i++) {
Yi Jinbe6de302017-10-24 12:30:24 -0700500 if (skip[i]) continue;
Yi Jinbdf58942017-11-14 17:58:19 -0800501 printf(" privacyArray[%d] = &%s;\n", policyCount++, getFieldName(fieldsInOrder[i]).c_str());
Yi Jinbe6de302017-10-24 12:30:24 -0700502 }
503 printf(" return const_cast<const Privacy**>(privacyArray);\n");
504 printf("}\n\n");
505 printf("const Privacy** PRIVACY_POLICY_LIST = createList();\n\n");
506 printf("const int PRIVACY_POLICY_COUNT = %d;\n", policyCount);
507 }
Yi Jin6cacbcb2018-03-30 14:04:52 -0700508
509 printf("} // incidentd\n");
510 printf("} // os\n");
511 printf("} // android\n");
Yi Jinf8601842017-08-15 22:01:41 -0700512 return true;
513}
514
515// ================================================================================
Yi Jin0f2599f2017-11-16 18:19:45 -0800516static string replace_string(const string& str, const char replace, const char with)
517{
518 string result(str);
519 const int N = result.size();
520 for (int i=0; i<N; i++) {
521 if (result[i] == replace) {
522 result[i] = with;
523 }
524 }
525 return result;
526}
527
Kweku Adamsecf4bdb2018-02-22 15:05:48 -0800528static void generateCsv(Descriptor const* descriptor, const string& indent, set<string>* parents, const Destination containerDest = DEST_UNSET) {
Yi Jin0f2599f2017-11-16 18:19:45 -0800529 DebugStringOptions options;
530 options.include_comments = true;
531 for (int i=0; i<descriptor->field_count(); i++) {
532 const FieldDescriptor* field = descriptor->field(i);
Kweku Adamsecf4bdb2018-02-22 15:05:48 -0800533 const Destination fieldDest = getFieldDest(field);
Yi Jin0f2599f2017-11-16 18:19:45 -0800534 stringstream text;
535 if (field->type() == FieldDescriptor::TYPE_MESSAGE) {
536 text << field->message_type()->name();
537 } else {
538 text << field->type_name();
539 }
540 text << " " << field->name();
Kweku Adamsecf4bdb2018-02-22 15:05:48 -0800541 text << " (PRIVACY=";
542 if (isDefaultField(field, containerDest)) {
543 text << getDestString(containerDest);
544 } else {
545 text << getDestString(fieldDest);
546 }
547 text << ")";
Yi Jin0f2599f2017-11-16 18:19:45 -0800548 printf("%s%s,\n", indent.c_str(), replace_string(text.str(), '\n', ' ').c_str());
549 if (field->type() == FieldDescriptor::TYPE_MESSAGE &&
550 parents->find(field->message_type()->full_name()) == parents->end()) {
551 parents->insert(field->message_type()->full_name());
Kweku Adamsecf4bdb2018-02-22 15:05:48 -0800552 generateCsv(field->message_type(), indent + ",", parents, fieldDest);
Yi Jin0f2599f2017-11-16 18:19:45 -0800553 parents->erase(field->message_type()->full_name());
554 }
555 }
556}
557
558// ================================================================================
Yi Jinf8601842017-08-15 22:01:41 -0700559int main(int argc, char const *argv[])
560{
Yi Jin0f2599f2017-11-16 18:19:45 -0800561 if (argc < 2) return 1;
Yi Jinf8601842017-08-15 22:01:41 -0700562 const char* module = argv[1];
563
Yi Jin0ed9b682017-08-18 14:51:20 -0700564 Descriptor const* descriptor = IncidentProto::descriptor();
565
Yi Jinf8601842017-08-15 22:01:41 -0700566 if (strcmp(module, "incident") == 0) {
Yi Jin0ed9b682017-08-18 14:51:20 -0700567 return !generateIncidentSectionsCpp(descriptor);
Yi Jinf8601842017-08-15 22:01:41 -0700568 }
569 if (strcmp(module, "incidentd") == 0 ) {
Yi Jin0ed9b682017-08-18 14:51:20 -0700570 return !generateSectionListCpp(descriptor);
Yi Jinf8601842017-08-15 22:01:41 -0700571 }
Yi Jin0f2599f2017-11-16 18:19:45 -0800572 // Generates Csv Format of proto definition for each section.
573 if (strcmp(module, "csv") == 0 && argc > 2) {
574 int sectionId = atoi(argv[2]);
575 for (int i=0; i<descriptor->field_count(); i++) {
576 const FieldDescriptor* field = descriptor->field(i);
577 if (strcmp(field->name().c_str(), argv[2]) == 0
578 || field->number() == sectionId) {
579 set<string> parents;
580 printf("%s\n", field->name().c_str());
Kweku Adamsecf4bdb2018-02-22 15:05:48 -0800581 generateCsv(field->message_type(), "", &parents, getFieldDest(field));
Yi Jin0f2599f2017-11-16 18:19:45 -0800582 break;
583 }
584 }
585 // Returns failure if csv is enabled to prevent Android building with it.
586 // It doesn't matter if this command runs manually.
587 return 1;
588 }
589 // Returns failure if not called by the whitelisted modules
Yi Jinf8601842017-08-15 22:01:41 -0700590 return 1;
Joe Onorato1754d742016-11-21 17:51:35 -0800591}