manifest_handler.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Copyright (c) 2013 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. #ifndef EXTENSIONS_COMMON_MANIFEST_HANDLER_H_
  5. #define EXTENSIONS_COMMON_MANIFEST_HANDLER_H_
  6. #include <memory>
  7. #include <set>
  8. #include <string>
  9. #include <unordered_map>
  10. #include <vector>
  11. #include "base/containers/small_map.h"
  12. #include "base/containers/span.h"
  13. #include "base/gtest_prod_util.h"
  14. #include "base/lazy_instance.h"
  15. #include "extensions/common/manifest.h"
  16. namespace extensions {
  17. class Extension;
  18. class ManifestPermission;
  19. class ManifestPermissionSet;
  20. // An interface for clients that recognize and parse keys in extension
  21. // manifests.
  22. class ManifestHandler {
  23. public:
  24. ManifestHandler();
  25. ManifestHandler(const ManifestHandler&) = delete;
  26. ManifestHandler& operator=(const ManifestHandler&) = delete;
  27. virtual ~ManifestHandler();
  28. // Attempts to parse the extension's manifest.
  29. // Returns true on success or false on failure; if false, |error| will
  30. // be set to a failure message.
  31. // This does not perform any IO operations.
  32. virtual bool Parse(Extension* extension, std::u16string* error) = 0;
  33. // Validate that files associated with this manifest key exist.
  34. // Validation takes place after parsing. May also append a series of
  35. // warning messages to |warnings|.
  36. // This may perform IO operations.
  37. //
  38. // Otherwise, returns false, and a description of the error is
  39. // returned in |error|.
  40. // TODO(yoz): Change error to std::u16string. See crbug.com/71980.
  41. virtual bool Validate(const Extension* extension,
  42. std::string* error,
  43. std::vector<InstallWarning>* warnings) const;
  44. // If false (the default), only parse the manifest if a registered
  45. // key is present in the manifest. If true, always attempt to parse
  46. // the manifest for this extension type, even if no registered keys
  47. // are present. This allows specifying a default parsed value for
  48. // extensions that don't declare our key in the manifest.
  49. // TODO(yoz): Use Feature availability instead.
  50. virtual bool AlwaysParseForType(Manifest::Type type) const;
  51. // Same as AlwaysParseForType, but for Validate instead of Parse.
  52. virtual bool AlwaysValidateForType(Manifest::Type type) const;
  53. // The list of keys that, if present, should be parsed before calling our
  54. // Parse (typically, because our Parse needs to read those keys).
  55. // Defaults to empty.
  56. virtual const std::vector<std::string> PrerequisiteKeys() const;
  57. // Creates a |ManifestPermission| instance for the given manifest key |name|.
  58. // The returned permission does not contain any permission data, so this
  59. // method is usually used before calling |FromValue| or |Read|. Returns
  60. // |NULL| if the manifest handler does not support custom permissions.
  61. virtual ManifestPermission* CreatePermission();
  62. // Creates a |ManifestPermission| instance containing the initial set of
  63. // required manifest permissions for the given |extension|. Returns |NULL| if
  64. // the manifest handler does not support custom permissions or if there was
  65. // no manifest key in the extension manifest for this handler.
  66. virtual ManifestPermission* CreateInitialRequiredPermission(
  67. const Extension* extension);
  68. // The keys this handler is responsible for.
  69. virtual base::span<const char* const> Keys() const = 0;
  70. // Calling FinalizeRegistration indicates that there are no more
  71. // manifest handlers to be registered.
  72. static void FinalizeRegistration();
  73. static bool IsRegistrationFinalized();
  74. // Call Parse on all registered manifest handlers that should parse
  75. // this extension.
  76. static bool ParseExtension(Extension* extension, std::u16string* error);
  77. // Call Validate on all registered manifest handlers for this extension. This
  78. // may perform IO operations.
  79. static bool ValidateExtension(const Extension* extension,
  80. std::string* error,
  81. std::vector<InstallWarning>* warnings);
  82. // Calls |CreatePermission| on the manifest handler for |key|. Returns |NULL|
  83. // if there is no manifest handler for |key| or if the manifest handler for
  84. // |key| does not support custom permissions.
  85. static ManifestPermission* CreatePermission(const std::string& key);
  86. // Calls |CreateInitialRequiredPermission| on all registered manifest handlers
  87. // and adds the returned permissions to |permission_set|. Note this should be
  88. // called after all manifest data elements have been read, parsed and stored
  89. // in the manifest data property of |extension|, as manifest handlers need
  90. // access to their manifest data to initialize their required manifest
  91. // permission.
  92. static void AddExtensionInitialRequiredPermissions(
  93. const Extension* extension, ManifestPermissionSet* permission_set);
  94. protected:
  95. // A convenience method for handlers that only register for 1 key,
  96. // so that they can define keys() { return SingleKey(kKey); }
  97. static const std::vector<std::string> SingleKey(const std::string& key);
  98. };
  99. // The global registry for manifest handlers.
  100. class ManifestHandlerRegistry {
  101. public:
  102. ManifestHandlerRegistry(const ManifestHandlerRegistry&) = delete;
  103. ManifestHandlerRegistry& operator=(const ManifestHandlerRegistry&) = delete;
  104. // Get the one true instance.
  105. static ManifestHandlerRegistry* Get();
  106. // Registers a ManifestHandler, associating it with its keys. If there is
  107. // already a handler registered for any key |handler| manages, this method
  108. // will DCHECK.
  109. void RegisterHandler(std::unique_ptr<ManifestHandler> handler);
  110. private:
  111. friend class ManifestHandler;
  112. friend class ScopedTestingManifestHandlerRegistry;
  113. friend struct base::LazyInstanceTraitsBase<ManifestHandlerRegistry>;
  114. FRIEND_TEST_ALL_PREFIXES(ManifestHandlerPerfTest, MANUAL_CommonInitialize);
  115. FRIEND_TEST_ALL_PREFIXES(ManifestHandlerPerfTest, MANUAL_LookupTest);
  116. FRIEND_TEST_ALL_PREFIXES(ManifestHandlerPerfTest,
  117. MANUAL_CommonMeasureFinalization);
  118. FRIEND_TEST_ALL_PREFIXES(ChromeExtensionsClientTest,
  119. CheckManifestHandlerRegistryForOverflow);
  120. ManifestHandlerRegistry();
  121. ~ManifestHandlerRegistry();
  122. void Finalize();
  123. bool ParseExtension(Extension* extension, std::u16string* error);
  124. bool ValidateExtension(const Extension* extension,
  125. std::string* error,
  126. std::vector<InstallWarning>* warnings);
  127. ManifestPermission* CreatePermission(const std::string& key);
  128. void AddExtensionInitialRequiredPermissions(
  129. const Extension* extension,
  130. ManifestPermissionSet* permission_set);
  131. // Reset the one true instance.
  132. static void ResetForTesting();
  133. // Overrides the current global ManifestHandlerRegistry with
  134. // |registry|, returning the current one.
  135. static ManifestHandlerRegistry* SetForTesting(
  136. ManifestHandlerRegistry* new_registry);
  137. // The owned collection of manifest handlers. These are then referenced by
  138. // raw pointer in maps for keys and priority.
  139. std::vector<std::unique_ptr<ManifestHandler>> owned_manifest_handlers_;
  140. // This number is derived from determining the total number of manifest
  141. // handlers that are installed for all build configurations. It is
  142. // checked through a unit test:
  143. // ChromeExtensionsClientTest.CheckManifestHandlerRegistryForOverflow.
  144. //
  145. // Any new manifest handlers added may cause the small_map to overflow
  146. // to the backup std::unordered_map, which we don't want, as that would
  147. // defeat the optimization of using small_map.
  148. static constexpr size_t kHandlerMax = 87;
  149. using FallbackMap = std::unordered_map<std::string, ManifestHandler*>;
  150. using ManifestHandlerMap = base::small_map<FallbackMap, kHandlerMax>;
  151. using FallbackPriorityMap = std::unordered_map<ManifestHandler*, int>;
  152. using ManifestHandlerPriorityMap =
  153. base::small_map<FallbackPriorityMap, kHandlerMax>;
  154. // Puts the manifest handlers in order such that each handler comes after
  155. // any handlers for their PrerequisiteKeys. If there is no handler for
  156. // a prerequisite key, that dependency is simply ignored.
  157. // CHECKs that there are no manifest handlers with circular dependencies.
  158. void SortManifestHandlers();
  159. // All registered manifest handlers.
  160. ManifestHandlerMap handlers_;
  161. // The priority for each manifest handler. Handlers with lower priority
  162. // values are evaluated first.
  163. ManifestHandlerPriorityMap priority_map_;
  164. bool is_finalized_;
  165. };
  166. } // namespace extensions
  167. #endif // EXTENSIONS_COMMON_MANIFEST_HANDLER_H_