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 5 #ifndef CHROME_COMMON_EXTENSIONS_MANIFEST_HANDLER_H_ 6 #define CHROME_COMMON_EXTENSIONS_MANIFEST_HANDLER_H_ 7 8 #include <set> 9 #include <string> 10 #include <vector> 11 12 #include "base/lazy_instance.h" 13 #include "base/memory/linked_ptr.h" 14 #include "base/strings/string16.h" 15 #include "chrome/common/extensions/manifest.h" 16 17 namespace extensions { 18 class Extension; 19 20 // An interface for clients that recognize and parse keys in extension 21 // manifests. 22 class ManifestHandler { 23 public: 24 ManifestHandler(); 25 virtual ~ManifestHandler(); 26 27 // Attempts to parse the extension's manifest. 28 // Returns true on success or false on failure; if false, |error| will 29 // be set to a failure message. 30 virtual bool Parse(Extension* extension, string16* error) = 0; 31 32 // Validate that files associated with this manifest key exist. 33 // Validation takes place after parsing. May also append a series of 34 // warning messages to |warnings|. 35 // 36 // Otherwise, returns false, and a description of the error is 37 // returned in |error|. 38 // TODO(yoz): Change error to string16. See crbug.com/71980. 39 virtual bool Validate(const Extension* extension, 40 std::string* error, 41 std::vector<InstallWarning>* warnings) const; 42 43 // If false (the default), only parse the manifest if a registered 44 // key is present in the manifest. If true, always attempt to parse 45 // the manifest for this extension type, even if no registered keys 46 // are present. This allows specifying a default parsed value for 47 // extensions that don't declare our key in the manifest. 48 // TODO(yoz): Use Feature availability instead. 49 virtual bool AlwaysParseForType(Manifest::Type type) const; 50 51 // Same as AlwaysParseForType, but for Validate instead of Parse. 52 virtual bool AlwaysValidateForType(Manifest::Type type) const; 53 54 // The list of keys that, if present, should be parsed before calling our 55 // Parse (typically, because our Parse needs to read those keys). 56 // Defaults to empty. 57 virtual const std::vector<std::string> PrerequisiteKeys() const; 58 59 // Associate us with our keys() in the manifest. A handler can register 60 // for multiple keys. The global registry takes ownership of this; 61 // if it has an existing handler for |key|, it replaces it with this. 62 // Manifest handlers must be registered at process startup in 63 // chrome_manifest_handlers.cc: 64 // (new MyManifestHandler)->Register(); 65 void Register(); 66 67 // Calling FinalizeRegistration indicates that there are no more 68 // manifest handlers to be registered. 69 static void FinalizeRegistration(); 70 71 static bool IsRegistrationFinalized(); 72 73 // Call Parse on all registered manifest handlers that should parse 74 // this extension. 75 static bool ParseExtension(Extension* extension, string16* error); 76 77 // Call Validate on all registered manifest handlers for this extension. 78 static bool ValidateExtension(const Extension* extension, 79 std::string* error, 80 std::vector<InstallWarning>* warnings); 81 82 protected: 83 // A convenience method for handlers that only register for 1 key, 84 // so that they can define keys() { return SingleKey(kKey); } 85 static const std::vector<std::string> SingleKey(const std::string& key); 86 87 private: 88 // The keys to register us for (in Register). 89 virtual const std::vector<std::string> Keys() const = 0; 90 }; 91 92 // The global registry for manifest handlers. 93 class ManifestHandlerRegistry { 94 private: 95 friend class ManifestHandler; 96 friend class ScopedTestingManifestHandlerRegistry; 97 friend struct base::DefaultLazyInstanceTraits<ManifestHandlerRegistry>; 98 99 ManifestHandlerRegistry(); 100 ~ManifestHandlerRegistry(); 101 102 void Finalize(); 103 104 void RegisterManifestHandler(const std::string& key, 105 linked_ptr<ManifestHandler> handler); 106 bool ParseExtension(Extension* extension, string16* error); 107 bool ValidateExtension(const Extension* extension, 108 std::string* error, 109 std::vector<InstallWarning>* warnings); 110 111 // Overrides the current global ManifestHandlerRegistry with 112 // |registry|, returning the current one. 113 static ManifestHandlerRegistry* SetForTesting( 114 ManifestHandlerRegistry* new_registry); 115 116 typedef std::map<std::string, linked_ptr<ManifestHandler> > 117 ManifestHandlerMap; 118 typedef std::map<ManifestHandler*, int> ManifestHandlerPriorityMap; 119 120 // Puts the manifest handlers in order such that each handler comes after 121 // any handlers for their PrerequisiteKeys. If there is no handler for 122 // a prerequisite key, that dependency is simply ignored. 123 // CHECKs that there are no manifest handlers with circular dependencies. 124 void SortManifestHandlers(); 125 126 // All registered manifest handlers. 127 ManifestHandlerMap handlers_; 128 129 // The priority for each manifest handler. Handlers with lower priority 130 // values are evaluated first. 131 ManifestHandlerPriorityMap priority_map_; 132 133 bool is_finalized_; 134 }; 135 136 } // namespace extensions 137 138 #endif // CHROME_COMMON_EXTENSIONS_MANIFEST_HANDLER_H_ 139