feature_compiler.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. # Copyright 2016 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. from __future__ import print_function
  5. import argparse
  6. import copy
  7. from datetime import datetime
  8. from functools import partial
  9. import json
  10. import os
  11. import posixpath
  12. import re
  13. import sys
  14. from code import Code
  15. import json_parse
  16. # The template for the header file of the generated FeatureProvider.
  17. HEADER_FILE_TEMPLATE = """
  18. // Copyright %(year)s The Chromium Authors. All rights reserved.
  19. // Use of this source code is governed by a BSD-style license that can be
  20. // found in the LICENSE file.
  21. // GENERATED FROM THE FEATURES FILE:
  22. // %(source_files)s
  23. // by tools/json_schema_compiler.
  24. // DO NOT EDIT.
  25. #ifndef %(header_guard)s
  26. #define %(header_guard)s
  27. namespace extensions {
  28. class FeatureProvider;
  29. void %(method_name)s(FeatureProvider* provider);
  30. } // namespace extensions
  31. #endif // %(header_guard)s
  32. """
  33. # The beginning of the .cc file for the generated FeatureProvider.
  34. CC_FILE_BEGIN = """
  35. // Copyright %(year)s The Chromium Authors. All rights reserved.
  36. // Use of this source code is governed by a BSD-style license that can be
  37. // found in the LICENSE file.
  38. // GENERATED FROM THE FEATURES FILE:
  39. // %(source_files)s
  40. // by tools/json_schema_compiler.
  41. // DO NOT EDIT.
  42. #include "%(header_file_path)s"
  43. #include "extensions/common/features/complex_feature.h"
  44. #include "extensions/common/features/feature_provider.h"
  45. #include "extensions/common/features/manifest_feature.h"
  46. #include "extensions/common/features/permission_feature.h"
  47. #include "extensions/common/mojom/feature_session_type.mojom.h"
  48. namespace extensions {
  49. void %(method_name)s(FeatureProvider* provider) {
  50. """
  51. # The end of the .cc file for the generated FeatureProvider.
  52. CC_FILE_END = """
  53. }
  54. } // namespace extensions
  55. """
  56. def ToPosixPath(path):
  57. """Returns |path| with separator converted to POSIX style.
  58. This is needed to generate C++ #include paths.
  59. """
  60. return path.replace(os.path.sep, posixpath.sep)
  61. # Returns true if the list 'l' only contains strings that are a hex-encoded SHA1
  62. # hashes.
  63. def ListContainsOnlySha1Hashes(l):
  64. return len(list(filter(lambda s: not re.match("^[A-F0-9]{40}$", s), l))) == 0
  65. # A "grammar" for what is and isn't allowed in the features.json files. This
  66. # grammar has to list all possible keys and the requirements for each. The
  67. # format of each entry is:
  68. # 'key': {
  69. # allowed_type_1: optional_properties,
  70. # allowed_type_2: optional_properties,
  71. # }
  72. # |allowed_types| are the types of values that can be used for a given key. The
  73. # possible values are list, str, bool, and int.
  74. # |optional_properties| provide more restrictions on the given type. The options
  75. # are:
  76. # 'subtype': Only applicable for lists. If provided, this enforces that each
  77. # entry in the list is of the specified type.
  78. # 'enum_map': A map of strings to C++ enums. When the compiler sees the given
  79. # enum string, it will replace it with the C++ version in the
  80. # compiled code. For instance, if a feature specifies
  81. # 'channel': 'stable', the generated C++ will assign
  82. # version_info::Channel::STABLE to channel. The keys in this map
  83. # also serve as a list all of possible values.
  84. # 'allow_all': Only applicable for lists. If present, this will check for
  85. # a value of "all" for a list value, and will replace it with
  86. # the collection of all possible values. For instance, if a
  87. # feature specifies 'contexts': 'all', the generated C++ will
  88. # assign the list of Feature::BLESSED_EXTENSION_CONTEXT,
  89. # Feature::BLESSED_WEB_PAGE_CONTEXT et al for contexts. If not
  90. # specified, defaults to false.
  91. # 'allow_empty': Only applicable for lists. Whether an empty list is a valid
  92. # value. If omitted, empty lists are prohibited.
  93. # 'validators': A list of (function, str) pairs with a function to run on the
  94. # value for a feature. Validators allow for more flexible or
  95. # one-off style validation than just what's in the grammar (such
  96. # as validating the content of a string). The validator function
  97. # should return True if the value is valid, and False otherwise.
  98. # If the value is invalid, the specified error will be added for
  99. # that key.
  100. # 'values': A list of all possible allowed values for a given key.
  101. # 'shared': Boolean that, if set, ensures that only one of the associated
  102. # features has the feature property set. Used primarily for complex
  103. # features - for simple features, there is always at most one feature
  104. # setting an option.
  105. # If a type definition does not have any restrictions (beyond the type itself),
  106. # an empty definition ({}) is used.
  107. FEATURE_GRAMMAR = ({
  108. 'alias': {
  109. str: {},
  110. 'shared': True
  111. },
  112. 'allowlist': {
  113. list: {
  114. 'subtype':
  115. str,
  116. 'validators':
  117. [(ListContainsOnlySha1Hashes,
  118. 'list should only have hex-encoded SHA1 hashes of extension ids')]
  119. }
  120. },
  121. 'blocklist': {
  122. list: {
  123. 'subtype':
  124. str,
  125. 'validators':
  126. [(ListContainsOnlySha1Hashes,
  127. 'list should only have hex-encoded SHA1 hashes of extension ids')]
  128. }
  129. },
  130. 'channel': {
  131. str: {
  132. 'enum_map': {
  133. 'trunk': 'version_info::Channel::UNKNOWN',
  134. 'canary': 'version_info::Channel::CANARY',
  135. 'dev': 'version_info::Channel::DEV',
  136. 'beta': 'version_info::Channel::BETA',
  137. 'stable': 'version_info::Channel::STABLE',
  138. }
  139. }
  140. },
  141. 'command_line_switch': {
  142. str: {}
  143. },
  144. 'component_extensions_auto_granted': {
  145. bool: {}
  146. },
  147. 'contexts': {
  148. list: {
  149. 'enum_map': {
  150. 'blessed_extension': 'Feature::BLESSED_EXTENSION_CONTEXT',
  151. 'blessed_web_page': 'Feature::BLESSED_WEB_PAGE_CONTEXT',
  152. 'content_script': 'Feature::CONTENT_SCRIPT_CONTEXT',
  153. 'lock_screen_extension':
  154. 'Feature::LOCK_SCREEN_EXTENSION_CONTEXT',
  155. 'offscreen_extension': 'Feature::OFFSCREEN_EXTENSION_CONTEXT',
  156. 'web_page': 'Feature::WEB_PAGE_CONTEXT',
  157. 'webui': 'Feature::WEBUI_CONTEXT',
  158. 'webui_untrusted': 'Feature::WEBUI_UNTRUSTED_CONTEXT',
  159. 'unblessed_extension': 'Feature::UNBLESSED_EXTENSION_CONTEXT',
  160. },
  161. 'allow_all': True,
  162. 'allow_empty': True
  163. },
  164. },
  165. 'default_parent': {
  166. bool: {
  167. 'values': [True]
  168. }
  169. },
  170. 'dependencies': {
  171. list: {
  172. # We allow an empty list of dependencies for child features that
  173. # want to override their parents' dependency set.
  174. 'allow_empty': True,
  175. 'subtype': str
  176. }
  177. },
  178. 'developer_mode_only': {
  179. bool: {}
  180. },
  181. 'disallow_for_service_workers': {
  182. bool: {}
  183. },
  184. 'extension_types': {
  185. list: {
  186. 'enum_map': {
  187. 'extension': 'Manifest::TYPE_EXTENSION',
  188. 'hosted_app': 'Manifest::TYPE_HOSTED_APP',
  189. 'legacy_packaged_app': 'Manifest::TYPE_LEGACY_PACKAGED_APP',
  190. 'platform_app': 'Manifest::TYPE_PLATFORM_APP',
  191. 'shared_module': 'Manifest::TYPE_SHARED_MODULE',
  192. 'theme': 'Manifest::TYPE_THEME',
  193. 'login_screen_extension':
  194. 'Manifest::TYPE_LOGIN_SCREEN_EXTENSION',
  195. 'chromeos_system_extension':
  196. 'Manifest::TYPE_CHROMEOS_SYSTEM_EXTENSION',
  197. },
  198. 'allow_all': True
  199. },
  200. },
  201. 'feature_flag': {
  202. str: {}
  203. },
  204. 'location': {
  205. str: {
  206. 'enum_map': {
  207. 'component': 'SimpleFeature::COMPONENT_LOCATION',
  208. 'external_component':
  209. 'SimpleFeature::EXTERNAL_COMPONENT_LOCATION',
  210. 'policy': 'SimpleFeature::POLICY_LOCATION',
  211. 'unpacked': 'SimpleFeature::UNPACKED_LOCATION',
  212. }
  213. }
  214. },
  215. 'internal': {
  216. bool: {
  217. 'values': [True]
  218. }
  219. },
  220. 'matches': {
  221. list: {
  222. 'subtype': str
  223. }
  224. },
  225. 'max_manifest_version': {
  226. int: {
  227. 'values': [1, 2]
  228. }
  229. },
  230. 'min_manifest_version': {
  231. int: {
  232. 'values': [2, 3]
  233. }
  234. },
  235. 'noparent': {
  236. bool: {
  237. 'values': [True]
  238. }
  239. },
  240. 'platforms': {
  241. list: {
  242. 'enum_map': {
  243. 'chromeos': 'Feature::CHROMEOS_PLATFORM',
  244. 'lacros': 'Feature::LACROS_PLATFORM',
  245. 'linux': 'Feature::LINUX_PLATFORM',
  246. 'mac': 'Feature::MACOSX_PLATFORM',
  247. 'win': 'Feature::WIN_PLATFORM',
  248. 'fuchsia': 'Feature::FUCHSIA_PLATFORM',
  249. }
  250. }
  251. },
  252. 'session_types': {
  253. list: {
  254. 'enum_map': {
  255. 'regular': 'mojom::FeatureSessionType::kRegular',
  256. 'kiosk': 'mojom::FeatureSessionType::kKiosk',
  257. 'kiosk.autolaunched':
  258. 'mojom::FeatureSessionType::kAutolaunchedKiosk',
  259. }
  260. }
  261. },
  262. 'source': {
  263. str: {},
  264. 'shared': True
  265. },
  266. })
  267. FEATURE_TYPES = ['APIFeature', 'BehaviorFeature',
  268. 'ManifestFeature', 'PermissionFeature']
  269. def HasProperty(property_name, value):
  270. return property_name in value
  271. def HasAtLeastOneProperty(property_names, value):
  272. return any([HasProperty(name, value) for name in property_names])
  273. def DoesNotHaveAllProperties(property_names, value):
  274. return not all([HasProperty(name, value) for name in property_names])
  275. def DoesNotHaveProperty(property_name, value):
  276. return property_name not in value
  277. def IsEmptyContextsAllowed(feature, all_features):
  278. # An alias feature wouldn't have the 'contexts' feature value.
  279. if feature.GetValue('source'):
  280. return True
  281. if type(feature) is ComplexFeature:
  282. for child_feature in feature.feature_list:
  283. if not IsEmptyContextsAllowed(child_feature, all_features):
  284. return False
  285. return True
  286. contexts = feature.GetValue('contexts')
  287. assert contexts, 'contexts must have been specified for the APIFeature'
  288. allowlisted_empty_context_namespaces = [
  289. 'manifestTypes',
  290. 'extensionsManifestTypes',
  291. 'empty_contexts' # Only added for testing.
  292. ]
  293. return (contexts != '{}' or
  294. feature.name in allowlisted_empty_context_namespaces)
  295. def IsFeatureCrossReference(property_name, reverse_property_name, feature,
  296. all_features):
  297. """ Verifies that |property_name| on |feature| references a feature that
  298. references |feature| back using |reverse_property_name| property.
  299. |property_name| and |reverse_property_name| are expected to have string
  300. values.
  301. """
  302. value = feature.GetValue(property_name)
  303. if not value:
  304. return True
  305. # String property values will be wrapped in "", strip those.
  306. value_regex = re.compile('^"(.+)"$')
  307. parsed_value = value_regex.match(value)
  308. assert parsed_value, (
  309. 'IsFeatureCrossReference should only be used on unicode properties')
  310. referenced_feature = all_features.get(parsed_value.group(1))
  311. if not referenced_feature:
  312. return False
  313. reverse_reference_value = referenced_feature.GetValue(reverse_property_name)
  314. if not reverse_reference_value:
  315. return False
  316. # Don't validate reverse reference value for child features - chances are that
  317. # the value was inherited from a feature parent, in which case it won't match
  318. # current feature name.
  319. if feature.has_parent:
  320. return True
  321. return reverse_reference_value == ('"%s"' % feature.name)
  322. # Verifies that a feature with an allowlist is not available to hosted apps,
  323. # returning true on success.
  324. def DoesNotHaveAllowlistForHostedApps(value):
  325. if not 'allowlist' in value:
  326. return True
  327. # Hack Alert: |value| here has the code for the generated C++ feature. Since
  328. # we're looking at the individual values, we do a bit of yucky back-parsing
  329. # to get a better look at the feature. This would be cleaner if we were
  330. # operating on the JSON feature itself, but we currently never generate a
  331. # JSON-based feature object that has all the values inherited from its
  332. # parents. Since this is the only scenario we need this type of validation,
  333. # doing it in a slightly ugly way isn't too bad. If we need more of these,
  334. # we should find a smoother way to do it (e.g. first generate JSON-based
  335. # features with inherited properties, do any necessary validation, then
  336. # generate the C++ code strings).
  337. # The feature did not specify extension types; this is fine for e.g.
  338. # API features (which would typically rely on a permission feature, which
  339. # is required to specify types).
  340. if not 'extension_types' in value:
  341. return True
  342. types = value['extension_types']
  343. # |types| looks like "{Manifest::TYPE_1, Manifest::TYPE_2}", so just looking
  344. # for the "TYPE_HOSTED_APP substring is sufficient.
  345. if 'TYPE_HOSTED_APP' not in types:
  346. return True
  347. # Helper to convert our C++ string array like "{\"aaa\", \"bbb\"}" (which is
  348. # what the allowlist looks like) to a python list of strings.
  349. def cpp_list_to_list(cpp_list):
  350. assert type(cpp_list) is str
  351. assert cpp_list[0] == '{'
  352. assert cpp_list[-1] == '}'
  353. new_list = json.loads('[%s]' % cpp_list[1:-1])
  354. assert type(new_list) is list
  355. return new_list
  356. # Exceptions (see the feature files).
  357. # DO NOT ADD MORE.
  358. HOSTED_APP_EXCEPTIONS = [
  359. 'B44D08FD98F1523ED5837D78D0A606EA9D6206E5',
  360. ]
  361. allowlist = cpp_list_to_list(value['allowlist'])
  362. for entry in allowlist:
  363. if entry not in HOSTED_APP_EXCEPTIONS:
  364. return False
  365. return True
  366. SIMPLE_FEATURE_CPP_CLASSES = ({
  367. 'APIFeature': 'SimpleFeature',
  368. 'ManifestFeature': 'ManifestFeature',
  369. 'PermissionFeature': 'PermissionFeature',
  370. 'BehaviorFeature': 'SimpleFeature',
  371. })
  372. VALIDATION = ({
  373. 'all': [
  374. (partial(HasAtLeastOneProperty, ['channel', 'dependencies']),
  375. 'Features must specify either a channel or dependencies'),
  376. (DoesNotHaveAllowlistForHostedApps,
  377. 'Hosted apps are not allowed to use restricted features')
  378. ],
  379. 'APIFeature': [
  380. (partial(HasProperty, 'contexts'),
  381. 'APIFeatures must specify the contexts property'),
  382. (partial(DoesNotHaveAllProperties, ['alias', 'source']),
  383. 'Features cannot specify both alias and source.')
  384. ],
  385. 'ManifestFeature': [
  386. (partial(HasProperty, 'extension_types'),
  387. 'ManifestFeatures must specify at least one extension type'),
  388. (partial(DoesNotHaveProperty, 'contexts'),
  389. 'ManifestFeatures do not support contexts.'),
  390. (partial(DoesNotHaveProperty, 'alias'),
  391. 'ManifestFeatures do not support alias.'),
  392. (partial(DoesNotHaveProperty, 'source'),
  393. 'ManifestFeatures do not support source.'),
  394. ],
  395. 'BehaviorFeature': [
  396. (partial(DoesNotHaveProperty, 'alias'),
  397. 'BehaviorFeatures do not support alias.'),
  398. (partial(DoesNotHaveProperty, 'source'),
  399. 'BehaviorFeatures do not support source.'),
  400. ],
  401. 'PermissionFeature': [
  402. (partial(HasProperty, 'extension_types'),
  403. 'PermissionFeatures must specify at least one extension type'),
  404. (partial(DoesNotHaveProperty, 'contexts'),
  405. 'PermissionFeatures do not support contexts.'),
  406. (partial(DoesNotHaveProperty, 'alias'),
  407. 'PermissionFeatures do not support alias.'),
  408. (partial(DoesNotHaveProperty, 'source'),
  409. 'PermissionFeatures do not support source.'),
  410. ],
  411. })
  412. FINAL_VALIDATION = ({
  413. 'all': [],
  414. 'APIFeature': [
  415. (partial(IsFeatureCrossReference, 'alias', 'source'),
  416. 'A feature alias property should reference a feature whose source '
  417. 'property references it back.'),
  418. (partial(IsFeatureCrossReference, 'source', 'alias'),
  419. 'A feature source property should reference a feature whose alias '
  420. 'property references it back.'),
  421. (IsEmptyContextsAllowed,
  422. 'An empty contexts list is not allowed for this feature.')
  423. ],
  424. 'ManifestFeature': [],
  425. 'BehaviorFeature': [],
  426. 'PermissionFeature': []
  427. })
  428. # These keys are used to find the parents of different features, but are not
  429. # compiled into the features themselves.
  430. IGNORED_KEYS = ['default_parent']
  431. # By default, if an error is encountered, assert to stop the compilation. This
  432. # can be disabled for testing.
  433. ENABLE_ASSERTIONS = True
  434. def GetCodeForFeatureValues(feature_values):
  435. """ Gets the Code object for setting feature values for this object. """
  436. c = Code()
  437. for key in sorted(feature_values.keys()):
  438. if key in IGNORED_KEYS:
  439. continue;
  440. c.Append('feature->set_%s(%s);' % (key, feature_values[key]))
  441. return c
  442. class Feature(object):
  443. """A representation of a single simple feature that can handle all parsing,
  444. validation, and code generation.
  445. """
  446. def __init__(self, name):
  447. self.name = name
  448. self.has_parent = False
  449. self.errors = []
  450. self.feature_values = {}
  451. self.shared_values = {}
  452. def _GetType(self, value):
  453. """Returns the type of the given value.
  454. """
  455. # For Py3 compatibility we use str in the grammar and treat unicode as str
  456. # in Py2.
  457. if sys.version_info.major == 2 and type(value) is unicode:
  458. return str
  459. return type(value)
  460. def AddError(self, error):
  461. """Adds an error to the feature. If ENABLE_ASSERTIONS is active, this will
  462. also assert to stop the compilation process (since errors should never be
  463. found in production).
  464. """
  465. self.errors.append(error)
  466. if ENABLE_ASSERTIONS:
  467. assert False, error
  468. def _AddKeyError(self, key, error):
  469. """Adds an error relating to a particular key in the feature.
  470. """
  471. self.AddError('Error parsing feature "%s" at key "%s": %s' %
  472. (self.name, key, error))
  473. def _GetCheckedValue(self, key, expected_type, expected_values,
  474. enum_map, value):
  475. """Returns a string to be used in the generated C++ code for a given key's
  476. python value, or None if the value is invalid. For example, if the python
  477. value is True, this returns 'true', for a string foo, this returns "foo",
  478. and for an enum, this looks up the C++ definition in the enum map.
  479. key: The key being parsed.
  480. expected_type: The expected type for this value, or None if any type is
  481. allowed.
  482. expected_values: The list of allowed values for this value, or None if any
  483. value is allowed.
  484. enum_map: The map from python value -> cpp value for all allowed values,
  485. or None if no special mapping should be made.
  486. value: The value to check.
  487. """
  488. valid = True
  489. if expected_values and value not in expected_values:
  490. self._AddKeyError(key, 'Illegal value: "%s"' % value)
  491. valid = False
  492. t = self._GetType(value)
  493. if expected_type and t is not expected_type:
  494. self._AddKeyError(key, 'Illegal value: "%s"' % value)
  495. valid = False
  496. if not valid:
  497. return None
  498. if enum_map:
  499. return enum_map[value]
  500. if t is str:
  501. return '"%s"' % str(value)
  502. if t is int:
  503. return str(value)
  504. if t is bool:
  505. return 'true' if value else 'false'
  506. assert False, 'Unsupported type: %s' % value
  507. def _ParseKey(self, key, value, shared_values, grammar):
  508. """Parses the specific key according to the grammar rule for that key if it
  509. is present in the json value.
  510. key: The key to parse.
  511. value: The full value for this feature.
  512. shared_values: Set of shared vfalues associated with this feature.
  513. grammar: The rule for the specific key.
  514. """
  515. if key not in value:
  516. return
  517. v = value[key]
  518. is_all = False
  519. if v == 'all' and list in grammar and 'allow_all' in grammar[list]:
  520. assert grammar[list]['allow_all'], '`allow_all` only supports `True`.'
  521. v = []
  522. is_all = True
  523. if 'shared' in grammar and key in shared_values:
  524. self._AddKeyError(key, 'Key can be set at most once per feature.')
  525. return
  526. value_type = self._GetType(v)
  527. if value_type not in grammar:
  528. self._AddKeyError(key, 'Illegal value: "%s"' % v)
  529. return
  530. if value_type is list and not is_all and len(v) == 0:
  531. if 'allow_empty' in grammar[list]:
  532. assert grammar[list]['allow_empty'], \
  533. '`allow_empty` only supports `True`.'
  534. else:
  535. self._AddKeyError(key, 'List must specify at least one element.')
  536. return
  537. expected = grammar[value_type]
  538. expected_values = None
  539. enum_map = None
  540. if 'values' in expected:
  541. expected_values = expected['values']
  542. elif 'enum_map' in expected:
  543. enum_map = expected['enum_map']
  544. expected_values = list(enum_map)
  545. if is_all:
  546. v = copy.deepcopy(expected_values)
  547. expected_type = None
  548. if value_type is list and 'subtype' in expected:
  549. expected_type = expected['subtype']
  550. cpp_value = None
  551. # If this value is a list, iterate over each entry and validate. Otherwise,
  552. # validate the single value.
  553. if value_type is list:
  554. cpp_value = []
  555. for sub_value in v:
  556. cpp_sub_value = self._GetCheckedValue(key, expected_type,
  557. expected_values, enum_map,
  558. sub_value)
  559. if cpp_sub_value:
  560. cpp_value.append(cpp_sub_value)
  561. cpp_value = '{' + ','.join(cpp_value) + '}'
  562. else:
  563. cpp_value = self._GetCheckedValue(key, expected_type, expected_values,
  564. enum_map, v)
  565. if 'validators' in expected:
  566. validators = expected['validators']
  567. for validator, error in validators:
  568. if not validator(v):
  569. self._AddKeyError(key, error)
  570. if cpp_value:
  571. if 'shared' in grammar:
  572. shared_values[key] = cpp_value
  573. else:
  574. self.feature_values[key] = cpp_value
  575. elif key in self.feature_values:
  576. # If the key is empty and this feature inherited a value from its parent,
  577. # remove the inherited value.
  578. del self.feature_values[key]
  579. def SetParent(self, parent):
  580. """Sets the parent of this feature, and inherits all properties from that
  581. parent.
  582. """
  583. assert not self.feature_values, 'Parents must be set before parsing'
  584. self.feature_values = copy.deepcopy(parent.feature_values)
  585. self.has_parent = True
  586. def SetSharedValues(self, values):
  587. self.shared_values = values
  588. def Parse(self, parsed_json, shared_values):
  589. """Parses the feature from the given json value."""
  590. for key in parsed_json.keys():
  591. if key not in FEATURE_GRAMMAR:
  592. self._AddKeyError(key, 'Unrecognized key')
  593. for key, key_grammar in FEATURE_GRAMMAR.items():
  594. self._ParseKey(key, parsed_json, shared_values, key_grammar)
  595. def Validate(self, feature_type, shared_values):
  596. feature_values = self.feature_values.copy()
  597. feature_values.update(shared_values)
  598. for validator, error in (VALIDATION[feature_type] + VALIDATION['all']):
  599. if not validator(feature_values):
  600. self.AddError(error)
  601. def GetCode(self, feature_type):
  602. """Returns the Code object for generating this feature."""
  603. c = Code()
  604. cpp_feature_class = SIMPLE_FEATURE_CPP_CLASSES[feature_type]
  605. c.Append('%s* feature = new %s();' % (cpp_feature_class, cpp_feature_class))
  606. c.Append('feature->set_name("%s");' % self.name)
  607. c.Concat(GetCodeForFeatureValues(self.GetAllFeatureValues()))
  608. return c
  609. def AsParent(self):
  610. """ Returns the feature values that should be inherited by children features
  611. when this feature is set as parent.
  612. """
  613. return self
  614. def GetValue(self, key):
  615. """ Gets feature value for the specified key """
  616. value = self.feature_values.get(key)
  617. return value if value else self.shared_values.get(key)
  618. def GetAllFeatureValues(self):
  619. """ Gets all values set for this feature. """
  620. values = self.feature_values.copy()
  621. values.update(self.shared_values)
  622. return values
  623. def GetErrors(self):
  624. return self.errors;
  625. class ComplexFeature(Feature):
  626. """ Complex feature - feature that is comprised of list of features.
  627. Overall complex feature is available if any of contained
  628. feature is available.
  629. """
  630. def __init__(self, name):
  631. Feature.__init__(self, name)
  632. self.feature_list = []
  633. def GetCode(self, feature_type):
  634. c = Code()
  635. c.Append('std::vector<Feature*> features;')
  636. for f in self.feature_list:
  637. # Sanity check that components of complex features have no shared values
  638. # set.
  639. assert not f.shared_values
  640. c.Sblock('{')
  641. c.Concat(f.GetCode(feature_type))
  642. c.Append('features.push_back(feature);')
  643. c.Eblock('}')
  644. c.Append('ComplexFeature* feature(new ComplexFeature(&features));')
  645. c.Append('feature->set_name("%s");' % self.name)
  646. c.Concat(GetCodeForFeatureValues(self.shared_values))
  647. return c
  648. def AsParent(self):
  649. parent = None
  650. for p in self.feature_list:
  651. if 'default_parent' in p.feature_values:
  652. parent = p
  653. break
  654. assert parent, 'No default parent found for %s' % self.name
  655. return parent
  656. def GetErrors(self):
  657. errors = copy.copy(self.errors)
  658. for feature in self.feature_list:
  659. errors.extend(feature.GetErrors())
  660. return errors
  661. class FeatureCompiler(object):
  662. """A compiler to load, parse, and generate C++ code for a number of
  663. features.json files."""
  664. def __init__(self, chrome_root, source_files, feature_type,
  665. method_name, out_root, gen_dir_relpath, out_base_filename):
  666. # See __main__'s ArgumentParser for documentation on these properties.
  667. self._chrome_root = chrome_root
  668. self._source_files = source_files
  669. self._feature_type = feature_type
  670. self._method_name = method_name
  671. self._out_root = out_root
  672. self._out_base_filename = out_base_filename
  673. self._gen_dir_relpath = gen_dir_relpath
  674. # The json value for the feature files.
  675. self._json = {}
  676. # The parsed features.
  677. self._features = {}
  678. def Load(self):
  679. """Loads and parses the source from each input file and puts the result in
  680. self._json."""
  681. for f in self._source_files:
  682. abs_source_file = os.path.join(self._chrome_root, f)
  683. try:
  684. with open(abs_source_file, 'r') as f:
  685. f_json = json_parse.Parse(f.read())
  686. except:
  687. print('FAILED: Exception encountered while loading "%s"' %
  688. abs_source_file)
  689. raise
  690. dupes = set(f_json) & set(self._json)
  691. assert not dupes, 'Duplicate keys found: %s' % list(dupes)
  692. self._json.update(f_json)
  693. def _FindParent(self, feature_name, feature_value):
  694. """Checks to see if a feature has a parent. If it does, returns the
  695. parent."""
  696. no_parent = False
  697. if type(feature_value) is list:
  698. no_parent_values = ['noparent' in v for v in feature_value]
  699. no_parent = all(no_parent_values)
  700. assert no_parent or not any(no_parent_values), (
  701. '"%s:" All child features must contain the same noparent value' %
  702. feature_name)
  703. else:
  704. no_parent = 'noparent' in feature_value
  705. sep = feature_name.rfind('.')
  706. if sep == -1 or no_parent:
  707. return None
  708. parent_name = feature_name[:sep]
  709. while sep != -1 and parent_name not in self._features:
  710. # This recursion allows for a feature to have a parent that isn't a direct
  711. # ancestor. For instance, we could have feature 'alpha', and feature
  712. # 'alpha.child.child', where 'alpha.child.child' inherits from 'alpha'.
  713. # TODO(devlin): Is this useful? Or logical?
  714. sep = feature_name.rfind('.', 0, sep)
  715. parent_name = feature_name[:sep]
  716. if sep == -1:
  717. # TODO(devlin): It'd be kind of nice to be able to assert that the
  718. # deduced parent name is in our features, but some dotted features don't
  719. # have parents and also don't have noparent, e.g. system.cpu. We should
  720. # probably just noparent them so that we can assert this.
  721. # raise KeyError('Could not find parent "%s" for feature "%s".' %
  722. # (parent_name, feature_name))
  723. return None
  724. return self._features[parent_name].AsParent()
  725. def _CompileFeature(self, feature_name, feature_value):
  726. """Parses a single feature."""
  727. if 'nocompile' in feature_value:
  728. assert feature_value['nocompile'], (
  729. 'nocompile should only be true; otherwise omit this key.')
  730. return
  731. def parse_and_validate(name, value, parent, shared_values):
  732. try:
  733. feature = Feature(name)
  734. if parent:
  735. feature.SetParent(parent)
  736. feature.Parse(value, shared_values)
  737. feature.Validate(self._feature_type, shared_values)
  738. return feature
  739. except:
  740. print('Failure to parse feature "%s"' % feature_name)
  741. raise
  742. parent = self._FindParent(feature_name, feature_value)
  743. shared_values = {}
  744. # Handle complex features, which are lists of simple features.
  745. if type(feature_value) is list:
  746. feature = ComplexFeature(feature_name)
  747. # This doesn't handle nested complex features. I think that's probably for
  748. # the best.
  749. for v in feature_value:
  750. feature.feature_list.append(
  751. parse_and_validate(feature_name, v, parent, shared_values))
  752. self._features[feature_name] = feature
  753. else:
  754. self._features[feature_name] = parse_and_validate(
  755. feature_name, feature_value, parent, shared_values)
  756. # Apply parent shared values at the end to enable child features to
  757. # override parent shared value - if parent shared values are added to
  758. # shared value set before a child feature is parsed, the child feature
  759. # overriding shared values set by its parent would cause an error due to
  760. # shared values being set twice.
  761. final_shared_values = copy.deepcopy(parent.shared_values) if parent else {}
  762. final_shared_values.update(shared_values)
  763. self._features[feature_name].SetSharedValues(final_shared_values)
  764. def _FinalValidation(self):
  765. validators = FINAL_VALIDATION['all'] + FINAL_VALIDATION[self._feature_type]
  766. for name, feature in self._features.items():
  767. for validator, error in validators:
  768. if not validator(feature, self._features):
  769. feature.AddError(error)
  770. def Compile(self):
  771. """Parses all features after loading the input files."""
  772. # Iterate over in sorted order so that parents come first.
  773. for k in sorted(self._json.keys()):
  774. self._CompileFeature(k, self._json[k])
  775. self._FinalValidation()
  776. def Render(self):
  777. """Returns the Code object for the body of the .cc file, which handles the
  778. initialization of all features."""
  779. c = Code()
  780. c.Sblock()
  781. for k in sorted(self._features.keys()):
  782. c.Sblock('{')
  783. feature = self._features[k]
  784. c.Concat(feature.GetCode(self._feature_type))
  785. c.Append('provider->AddFeature("%s", feature);' % k)
  786. c.Eblock('}')
  787. c.Eblock()
  788. return c
  789. def Write(self):
  790. """Writes the output."""
  791. header_file = self._out_base_filename + '.h'
  792. cc_file = self._out_base_filename + '.cc'
  793. include_file_root = self._out_root[len(self._gen_dir_relpath)+1:]
  794. header_file_path = '%s/%s' % (include_file_root, header_file)
  795. cc_file_path = '%s/%s' % (include_file_root, cc_file)
  796. substitutions = ({
  797. 'header_file_path': header_file_path,
  798. 'header_guard': (header_file_path.replace('/', '_').
  799. replace('.', '_').upper()),
  800. 'method_name': self._method_name,
  801. 'source_files': str([ToPosixPath(f) for f in self._source_files]),
  802. 'year': str(datetime.now().year)
  803. })
  804. if not os.path.exists(self._out_root):
  805. os.makedirs(self._out_root)
  806. # Write the .h file.
  807. with open(os.path.join(self._out_root, header_file), 'w') as f:
  808. header_file = Code()
  809. header_file.Append(HEADER_FILE_TEMPLATE)
  810. header_file.Substitute(substitutions)
  811. f.write(header_file.Render().strip())
  812. # Write the .cc file.
  813. with open(os.path.join(self._out_root, cc_file), 'w') as f:
  814. cc_file = Code()
  815. cc_file.Append(CC_FILE_BEGIN)
  816. cc_file.Substitute(substitutions)
  817. cc_file.Concat(self.Render())
  818. cc_end = Code()
  819. cc_end.Append(CC_FILE_END)
  820. cc_end.Substitute(substitutions)
  821. cc_file.Concat(cc_end)
  822. f.write(cc_file.Render().strip())
  823. if __name__ == '__main__':
  824. parser = argparse.ArgumentParser(description='Compile json feature files')
  825. parser.add_argument('chrome_root', type=str,
  826. help='The root directory of the chrome checkout')
  827. parser.add_argument(
  828. 'feature_type', type=str,
  829. help='The name of the class to use in feature generation ' +
  830. '(e.g. APIFeature, PermissionFeature)')
  831. parser.add_argument('method_name', type=str,
  832. help='The name of the method to populate the provider')
  833. parser.add_argument('out_root', type=str,
  834. help='The root directory to generate the C++ files into')
  835. parser.add_argument('gen_dir_relpath', default='gen', help='Path of the '
  836. 'gen directory relative to the out/. If running in the default '
  837. 'toolchain, the path is gen, otherwise $toolchain_name/gen')
  838. parser.add_argument(
  839. 'out_base_filename', type=str,
  840. help='The base filename for the C++ files (.h and .cc will be appended)')
  841. parser.add_argument('source_files', type=str, nargs='+',
  842. help='The source features.json files')
  843. args = parser.parse_args()
  844. if args.feature_type not in FEATURE_TYPES:
  845. raise NameError('Unknown feature type: %s' % args.feature_type)
  846. c = FeatureCompiler(args.chrome_root, args.source_files, args.feature_type,
  847. args.method_name, args.out_root, args.gen_dir_relpath,
  848. args.out_base_filename)
  849. c.Load()
  850. c.Compile()
  851. c.Write()