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 | |
| 17 | |
Joe Onorato | 7669012 | 2016-12-20 08:18:32 -0800 | [diff] [blame] | 18 | #include <frameworks/base/core/proto/android/os/incident.pb.h> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 19 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 20 | #include <map> |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 21 | #include <string> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 22 | |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 23 | using namespace android; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 24 | using namespace android::os; |
| 25 | using namespace google::protobuf; |
| 26 | using namespace google::protobuf::io; |
| 27 | using namespace google::protobuf::internal; |
| 28 | using namespace std; |
| 29 | |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 30 | static inline void emptyline() { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 31 | printf("\n"); |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 32 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 33 | |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 34 | static void generateHead(const char* header) { |
| 35 | printf("// Auto generated file. Do not modify\n"); |
| 36 | emptyline(); |
| 37 | printf("#include \"%s.h\"\n", header); |
| 38 | emptyline(); |
| 39 | } |
| 40 | |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 41 | // ================================================================================ |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 42 | static bool generateIncidentSectionsCpp(Descriptor const* descriptor) |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 43 | { |
| 44 | generateHead("incident_sections"); |
| 45 | |
| 46 | map<string,FieldDescriptor const*> sections; |
| 47 | int N; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 48 | N = descriptor->field_count(); |
| 49 | for (int i=0; i<N; i++) { |
| 50 | const FieldDescriptor* field = descriptor->field(i); |
| 51 | if (field->type() == FieldDescriptor::TYPE_MESSAGE) { |
| 52 | sections[field->name()] = field; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | printf("IncidentSection const INCIDENT_SECTIONS[] = {\n"); |
| 57 | N = sections.size(); |
| 58 | int i = 0; |
| 59 | for (map<string,FieldDescriptor const*>::const_iterator it = sections.begin(); |
| 60 | it != sections.end(); it++, i++) { |
| 61 | const FieldDescriptor* field = it->second; |
| 62 | printf(" { %d, \"%s\" }", field->number(), field->name().c_str()); |
| 63 | if (i != N-1) { |
| 64 | printf(",\n"); |
| 65 | } else { |
| 66 | printf("\n"); |
| 67 | } |
| 68 | } |
| 69 | printf("};\n"); |
| 70 | |
| 71 | printf("const int INCIDENT_SECTION_COUNT = %d;\n", N); |
| 72 | |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 73 | return true; |
| 74 | } |
| 75 | |
| 76 | // ================================================================================ |
| 77 | static void splitAndPrint(const string& args) { |
| 78 | size_t base = 0; |
| 79 | size_t found; |
| 80 | while (true) { |
| 81 | found = args.find_first_of(" ", base); |
| 82 | if (found != base) { |
| 83 | string arg = args.substr(base, found - base); |
| 84 | printf(" \"%s\",", arg.c_str()); |
| 85 | } |
| 86 | if (found == args.npos) break; |
| 87 | base = found + 1; |
| 88 | } |
| 89 | } |
| 90 | |
Yunlian Jiang | 3809bbf | 2017-09-05 15:50:58 -0700 | [diff] [blame] | 91 | static const std::string replaceAll(const string& field_name, const char oldC, const string& newS) { |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 92 | if (field_name.find_first_of(oldC) == field_name.npos) return field_name.c_str(); |
| 93 | size_t pos = 0, idx = 0; |
| 94 | char* res = new char[field_name.size() * newS.size() + 1]; // assign a larger buffer |
| 95 | while (pos != field_name.size()) { |
| 96 | char cur = field_name[pos++]; |
| 97 | if (cur != oldC) { |
| 98 | res[idx++] = cur; |
| 99 | continue; |
| 100 | } |
| 101 | |
| 102 | for (size_t i=0; i<newS.size(); i++) { |
| 103 | res[idx++] = newS[i]; |
| 104 | } |
| 105 | } |
| 106 | res[idx] = '\0'; |
Yunlian Jiang | 3809bbf | 2017-09-05 15:50:58 -0700 | [diff] [blame] | 107 | std::string result(res); |
| 108 | delete [] res; |
| 109 | return result; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static inline bool isDefaultDest(const FieldDescriptor* field) { |
| 113 | return field->options().GetExtension(privacy).dest() == PrivacyFlags::default_instance().dest(); |
| 114 | } |
| 115 | |
Yi Jin | 22769e0 | 2017-10-16 14:42:50 -0700 | [diff] [blame^] | 116 | // Returns false if the descriptor doesn't have any non default privacy flags set, including its submessages |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 117 | static bool generatePrivacyFlags(const Descriptor* descriptor, const char* alias, map<string, bool> &msgNames) { |
| 118 | bool hasDefaultFlags[descriptor->field_count()]; |
| 119 | // iterate though its field and generate sub flags first |
| 120 | for (int i=0; i<descriptor->field_count(); i++) { |
| 121 | hasDefaultFlags[i] = true; // set default to true |
| 122 | const FieldDescriptor* field = descriptor->field(i); |
Yunlian Jiang | 3809bbf | 2017-09-05 15:50:58 -0700 | [diff] [blame] | 123 | const std::string field_name_str = replaceAll(field->full_name(), '.', "__"); |
| 124 | const char* field_name = field_name_str.c_str(); |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 125 | // check if the same name is already defined |
| 126 | if (msgNames.find(field_name) != msgNames.end()) { |
| 127 | hasDefaultFlags[i] = msgNames[field_name]; |
| 128 | continue; |
| 129 | }; |
| 130 | |
| 131 | PrivacyFlags p = field->options().GetExtension(privacy); |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 132 | switch (field->type()) { |
| 133 | case FieldDescriptor::TYPE_MESSAGE: |
Yi Jin | 22769e0 | 2017-10-16 14:42:50 -0700 | [diff] [blame^] | 134 | if (generatePrivacyFlags(field->message_type(), field_name, msgNames)) { |
| 135 | printf("Privacy %s { %d, %d, %s_LIST, %d, NULL };\n", field_name, field->number(), |
| 136 | field->type(), field_name, p.dest()); |
| 137 | } else if (isDefaultDest(field)) { |
| 138 | // don't create a new privacy if the value is default. |
| 139 | break; |
| 140 | } else{ |
| 141 | printf("Privacy %s { %d, %d, NULL, %d, NULL };\n", field_name, field->number(), |
| 142 | field->type(), p.dest()); |
| 143 | } |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 144 | hasDefaultFlags[i] = false; |
| 145 | break; |
| 146 | case FieldDescriptor::TYPE_STRING: |
| 147 | if (isDefaultDest(field) && p.patterns_size() == 0) break; |
| 148 | |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 149 | printf("const char* %s_patterns[] = {\n", field_name); |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 150 | for (int i=0; i<p.patterns_size(); i++) { |
| 151 | // the generated string need to escape backslash as well, need to dup it here |
Yunlian Jiang | 3809bbf | 2017-09-05 15:50:58 -0700 | [diff] [blame] | 152 | printf(" \"%s\",\n", replaceAll(p.patterns(i), '\\', "\\\\").c_str()); |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 153 | } |
| 154 | printf(" NULL };\n"); |
Yi Jin | 22769e0 | 2017-10-16 14:42:50 -0700 | [diff] [blame^] | 155 | printf("Privacy %s { %d, %d, NULL, %d, %s_patterns };\n", field_name, field->number(), |
| 156 | field->type(), p.dest(), field_name); |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 157 | hasDefaultFlags[i] = false; |
| 158 | break; |
| 159 | default: |
| 160 | if (isDefaultDest(field)) break; |
Yi Jin | 22769e0 | 2017-10-16 14:42:50 -0700 | [diff] [blame^] | 161 | printf("Privacy %s { %d, %d, NULL, %d, NULL };\n", field_name, field->number(), |
| 162 | field->type(), p.dest()); |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 163 | hasDefaultFlags[i] = false; |
| 164 | } |
| 165 | // add the field name to message map, true means it has default flags |
| 166 | msgNames[field_name] = hasDefaultFlags[i]; |
| 167 | } |
| 168 | |
| 169 | bool allDefaults = true; |
| 170 | for (int i=0; i<descriptor->field_count(); i++) { |
| 171 | allDefaults &= hasDefaultFlags[i]; |
| 172 | } |
Yi Jin | 22769e0 | 2017-10-16 14:42:50 -0700 | [diff] [blame^] | 173 | if (allDefaults) return false; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 174 | |
| 175 | emptyline(); |
Yi Jin | 7e0b4e5 | 2017-09-12 20:00:25 -0700 | [diff] [blame] | 176 | |
| 177 | bool needConst = strcmp(alias, "PRIVACY_POLICY") == 0; |
| 178 | int policyCount = 0; |
| 179 | |
Yi Jin | 22769e0 | 2017-10-16 14:42:50 -0700 | [diff] [blame^] | 180 | printf("%sPrivacy* %s_LIST[] = {\n", needConst ? "const " : "", alias); |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 181 | for (int i=0; i<descriptor->field_count(); i++) { |
| 182 | const FieldDescriptor* field = descriptor->field(i); |
| 183 | if (hasDefaultFlags[i]) continue; |
Yunlian Jiang | 3809bbf | 2017-09-05 15:50:58 -0700 | [diff] [blame] | 184 | printf(" &%s,\n", replaceAll(field->full_name(), '.', "__").c_str()); |
Yi Jin | 7e0b4e5 | 2017-09-12 20:00:25 -0700 | [diff] [blame] | 185 | policyCount++; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 186 | } |
Yi Jin | 7e0b4e5 | 2017-09-12 20:00:25 -0700 | [diff] [blame] | 187 | if (needConst) { |
| 188 | printf("};\n\n"); |
| 189 | printf("const int PRIVACY_POLICY_COUNT = %d;\n", policyCount); |
| 190 | } else { |
| 191 | printf(" NULL };\n"); |
| 192 | } |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 193 | emptyline(); |
Yi Jin | 22769e0 | 2017-10-16 14:42:50 -0700 | [diff] [blame^] | 194 | return true; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | static bool generateSectionListCpp(Descriptor const* descriptor) { |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 198 | generateHead("section_list"); |
| 199 | |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 200 | // generates SECTION_LIST |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 201 | printf("const Section* SECTION_LIST[] = {\n"); |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 202 | for (int i=0; i<descriptor->field_count(); i++) { |
| 203 | const FieldDescriptor* field = descriptor->field(i); |
| 204 | |
| 205 | if (field->type() != FieldDescriptor::TYPE_MESSAGE) { |
| 206 | continue; |
| 207 | } |
| 208 | const SectionFlags s = field->options().GetExtension(section); |
| 209 | switch (s.type()) { |
| 210 | case SECTION_NONE: |
| 211 | continue; |
| 212 | case SECTION_FILE: |
| 213 | printf(" new FileSection(%d, \"%s\"),\n", field->number(), s.args().c_str()); |
| 214 | break; |
| 215 | case SECTION_COMMAND: |
| 216 | printf(" new CommandSection(%d,", field->number()); |
| 217 | splitAndPrint(s.args()); |
| 218 | printf(" NULL),\n"); |
| 219 | break; |
| 220 | case SECTION_DUMPSYS: |
| 221 | printf(" new DumpsysSection(%d,", field->number()); |
| 222 | splitAndPrint(s.args()); |
| 223 | printf(" NULL),\n"); |
| 224 | break; |
| 225 | } |
| 226 | } |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 227 | printf(" NULL };\n"); |
| 228 | emptyline(); |
| 229 | |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 230 | // generates PRIVACY_POLICY |
| 231 | map<string, bool> messageNames; |
Yi Jin | 22769e0 | 2017-10-16 14:42:50 -0700 | [diff] [blame^] | 232 | if (!generatePrivacyFlags(descriptor, "PRIVACY_POLICY", messageNames)) { |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 233 | // if no privacy options set at all, define an empty list |
Yi Jin | 7e0b4e5 | 2017-09-12 20:00:25 -0700 | [diff] [blame] | 234 | printf("const Privacy* PRIVACY_POLICY_LIST[] = {};\n"); |
| 235 | printf("const int PRIVACY_POLICY_COUNT = 0;\n"); |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 238 | return true; |
| 239 | } |
| 240 | |
| 241 | // ================================================================================ |
| 242 | int main(int argc, char const *argv[]) |
| 243 | { |
| 244 | if (argc != 2) return 1; |
| 245 | const char* module = argv[1]; |
| 246 | |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 247 | Descriptor const* descriptor = IncidentProto::descriptor(); |
| 248 | |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 249 | if (strcmp(module, "incident") == 0) { |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 250 | return !generateIncidentSectionsCpp(descriptor); |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 251 | } |
| 252 | if (strcmp(module, "incidentd") == 0 ) { |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 253 | return !generateSectionListCpp(descriptor); |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | // return failure if not called by the whitelisted modules |
| 257 | return 1; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 258 | } |