OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef EXTENSIONS_COMMON_MANIFEST_HANDLER_H_ | 5 #ifndef EXTENSIONS_COMMON_MANIFEST_HANDLER_H_ |
6 #define EXTENSIONS_COMMON_MANIFEST_HANDLER_H_ | 6 #define EXTENSIONS_COMMON_MANIFEST_HANDLER_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
13 #include "base/memory/linked_ptr.h" | 13 #include "base/memory/linked_ptr.h" |
14 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
15 #include "extensions/common/manifest.h" | 15 #include "extensions/common/manifest.h" |
16 | 16 |
17 namespace extensions { | 17 namespace extensions { |
18 class Extension; | 18 class Extension; |
| 19 class ManifestPermission; |
| 20 class ManifestPermissionSet; |
19 | 21 |
20 // An interface for clients that recognize and parse keys in extension | 22 // An interface for clients that recognize and parse keys in extension |
21 // manifests. | 23 // manifests. |
22 class ManifestHandler { | 24 class ManifestHandler { |
23 public: | 25 public: |
24 ManifestHandler(); | 26 ManifestHandler(); |
25 virtual ~ManifestHandler(); | 27 virtual ~ManifestHandler(); |
26 | 28 |
27 // Attempts to parse the extension's manifest. | 29 // Attempts to parse the extension's manifest. |
28 // Returns true on success or false on failure; if false, |error| will | 30 // Returns true on success or false on failure; if false, |error| will |
(...skipping 28 matching lines...) Expand all Loading... |
57 virtual const std::vector<std::string> PrerequisiteKeys() const; | 59 virtual const std::vector<std::string> PrerequisiteKeys() const; |
58 | 60 |
59 // Associate us with our keys() in the manifest. A handler can register | 61 // Associate us with our keys() in the manifest. A handler can register |
60 // for multiple keys. The global registry takes ownership of this; | 62 // for multiple keys. The global registry takes ownership of this; |
61 // if it has an existing handler for |key|, it replaces it with this. | 63 // if it has an existing handler for |key|, it replaces it with this. |
62 // Manifest handlers must be registered at process startup in | 64 // Manifest handlers must be registered at process startup in |
63 // chrome_manifest_handlers.cc: | 65 // chrome_manifest_handlers.cc: |
64 // (new MyManifestHandler)->Register(); | 66 // (new MyManifestHandler)->Register(); |
65 void Register(); | 67 void Register(); |
66 | 68 |
| 69 // Creates a |ManifestPermission| instance for the given manifest key |name|. |
| 70 // The returned permission does not contain any permission data, so this |
| 71 // method is usually used before calling |FromValue| or |Read|. Returns |
| 72 // |NULL| if the manifest handler does not support custom permissions. |
| 73 virtual ManifestPermission* CreatePermission(); |
| 74 |
| 75 // Creates a |ManifestPermission| instance containing the initial set of |
| 76 // required manifest permissions for the given |extension|. Returns |NULL| if |
| 77 // the manifest handler does not support custom permissions or if there was |
| 78 // no manifest key in the extension manifest for this handler. |
| 79 virtual ManifestPermission* CreateInitialRequiredPermission( |
| 80 const Extension* extension); |
| 81 |
67 // Calling FinalizeRegistration indicates that there are no more | 82 // Calling FinalizeRegistration indicates that there are no more |
68 // manifest handlers to be registered. | 83 // manifest handlers to be registered. |
69 static void FinalizeRegistration(); | 84 static void FinalizeRegistration(); |
70 | 85 |
71 static bool IsRegistrationFinalized(); | 86 static bool IsRegistrationFinalized(); |
72 | 87 |
73 // Call Parse on all registered manifest handlers that should parse | 88 // Call Parse on all registered manifest handlers that should parse |
74 // this extension. | 89 // this extension. |
75 static bool ParseExtension(Extension* extension, string16* error); | 90 static bool ParseExtension(Extension* extension, string16* error); |
76 | 91 |
77 // Call Validate on all registered manifest handlers for this extension. | 92 // Call Validate on all registered manifest handlers for this extension. |
78 static bool ValidateExtension(const Extension* extension, | 93 static bool ValidateExtension(const Extension* extension, |
79 std::string* error, | 94 std::string* error, |
80 std::vector<InstallWarning>* warnings); | 95 std::vector<InstallWarning>* warnings); |
81 | 96 |
| 97 // Calls |CreatePermission| on the manifest handler for |key|. Returns |NULL| |
| 98 // if there is no manifest handler for |key| or if the manifest handler for |
| 99 // |key| does not support custom permissions. |
| 100 static ManifestPermission* CreatePermission(const std::string& key); |
| 101 |
| 102 // Calls |CreateInitialRequiredPermission| on all registered manifest handlers |
| 103 // and adds the returned permissions to |permission_set|. Note this should be |
| 104 // called after all manifest data elements have been read, parsed and stored |
| 105 // in the manifest data property of |extension|, as manifest handlers need |
| 106 // access to their manifest data to initialize their required manifest |
| 107 // permission. |
| 108 static void AddExtensionInitialRequiredPermissions( |
| 109 const Extension* extension, ManifestPermissionSet* permission_set); |
| 110 |
82 protected: | 111 protected: |
83 // A convenience method for handlers that only register for 1 key, | 112 // A convenience method for handlers that only register for 1 key, |
84 // so that they can define keys() { return SingleKey(kKey); } | 113 // so that they can define keys() { return SingleKey(kKey); } |
85 static const std::vector<std::string> SingleKey(const std::string& key); | 114 static const std::vector<std::string> SingleKey(const std::string& key); |
86 | 115 |
87 private: | 116 private: |
88 // The keys to register us for (in Register). | 117 // The keys to register us for (in Register). |
89 virtual const std::vector<std::string> Keys() const = 0; | 118 virtual const std::vector<std::string> Keys() const = 0; |
90 }; | 119 }; |
91 | 120 |
92 // The global registry for manifest handlers. | 121 // The global registry for manifest handlers. |
93 class ManifestHandlerRegistry { | 122 class ManifestHandlerRegistry { |
94 private: | 123 private: |
95 friend class ManifestHandler; | 124 friend class ManifestHandler; |
96 friend class ScopedTestingManifestHandlerRegistry; | 125 friend class ScopedTestingManifestHandlerRegistry; |
97 friend struct base::DefaultLazyInstanceTraits<ManifestHandlerRegistry>; | 126 friend struct base::DefaultLazyInstanceTraits<ManifestHandlerRegistry>; |
98 | 127 |
99 ManifestHandlerRegistry(); | 128 ManifestHandlerRegistry(); |
100 ~ManifestHandlerRegistry(); | 129 ~ManifestHandlerRegistry(); |
101 | 130 |
102 void Finalize(); | 131 void Finalize(); |
103 | 132 |
104 void RegisterManifestHandler(const std::string& key, | 133 void RegisterManifestHandler(const std::string& key, |
105 linked_ptr<ManifestHandler> handler); | 134 linked_ptr<ManifestHandler> handler); |
106 bool ParseExtension(Extension* extension, string16* error); | 135 bool ParseExtension(Extension* extension, string16* error); |
107 bool ValidateExtension(const Extension* extension, | 136 bool ValidateExtension(const Extension* extension, |
108 std::string* error, | 137 std::string* error, |
109 std::vector<InstallWarning>* warnings); | 138 std::vector<InstallWarning>* warnings); |
110 | 139 |
| 140 ManifestPermission* CreatePermission(const std::string& key); |
| 141 |
| 142 void AddExtensionInitialRequiredPermissions( |
| 143 const Extension* extension, |
| 144 ManifestPermissionSet* permission_set); |
| 145 |
111 // Overrides the current global ManifestHandlerRegistry with | 146 // Overrides the current global ManifestHandlerRegistry with |
112 // |registry|, returning the current one. | 147 // |registry|, returning the current one. |
113 static ManifestHandlerRegistry* SetForTesting( | 148 static ManifestHandlerRegistry* SetForTesting( |
114 ManifestHandlerRegistry* new_registry); | 149 ManifestHandlerRegistry* new_registry); |
115 | 150 |
116 typedef std::map<std::string, linked_ptr<ManifestHandler> > | 151 typedef std::map<std::string, linked_ptr<ManifestHandler> > |
117 ManifestHandlerMap; | 152 ManifestHandlerMap; |
118 typedef std::map<ManifestHandler*, int> ManifestHandlerPriorityMap; | 153 typedef std::map<ManifestHandler*, int> ManifestHandlerPriorityMap; |
119 | 154 |
120 // Puts the manifest handlers in order such that each handler comes after | 155 // Puts the manifest handlers in order such that each handler comes after |
121 // any handlers for their PrerequisiteKeys. If there is no handler for | 156 // any handlers for their PrerequisiteKeys. If there is no handler for |
122 // a prerequisite key, that dependency is simply ignored. | 157 // a prerequisite key, that dependency is simply ignored. |
123 // CHECKs that there are no manifest handlers with circular dependencies. | 158 // CHECKs that there are no manifest handlers with circular dependencies. |
124 void SortManifestHandlers(); | 159 void SortManifestHandlers(); |
125 | 160 |
126 // All registered manifest handlers. | 161 // All registered manifest handlers. |
127 ManifestHandlerMap handlers_; | 162 ManifestHandlerMap handlers_; |
128 | 163 |
129 // The priority for each manifest handler. Handlers with lower priority | 164 // The priority for each manifest handler. Handlers with lower priority |
130 // values are evaluated first. | 165 // values are evaluated first. |
131 ManifestHandlerPriorityMap priority_map_; | 166 ManifestHandlerPriorityMap priority_map_; |
132 | 167 |
133 bool is_finalized_; | 168 bool is_finalized_; |
134 }; | 169 }; |
135 | 170 |
136 } // namespace extensions | 171 } // namespace extensions |
137 | 172 |
138 #endif // EXTENSIONS_COMMON_MANIFEST_HANDLER_H_ | 173 #endif // EXTENSIONS_COMMON_MANIFEST_HANDLER_H_ |
OLD | NEW |