| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 CHROME_COMMON_EXTENSIONS_API_EXTENSION_API_H_ | 5 #ifndef CHROME_COMMON_EXTENSIONS_API_EXTENSION_API_H_ |
| 6 #define CHROME_COMMON_EXTENSIONS_API_EXTENSION_API_H_ | 6 #define CHROME_COMMON_EXTENSIONS_API_EXTENSION_API_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 namespace extensions { | 30 namespace extensions { |
| 31 | 31 |
| 32 class Extension; | 32 class Extension; |
| 33 class Feature; | 33 class Feature; |
| 34 | 34 |
| 35 // C++ Wrapper for the JSON API definitions in chrome/common/extensions/api/. | 35 // C++ Wrapper for the JSON API definitions in chrome/common/extensions/api/. |
| 36 // | 36 // |
| 37 // WARNING: This class is accessed on multiple threads in the browser process | 37 // WARNING: This class is accessed on multiple threads in the browser process |
| 38 // (see ExtensionFunctionDispatcher). No state should be modified after | 38 // (see ExtensionFunctionDispatcher). No state should be modified after |
| 39 // construction. | 39 // construction. |
| 40 class ExtensionAPI { | 40 class ExtensionAPI : public FeatureProvider { |
| 41 public: | 41 public: |
| 42 // Returns a single shared instance of this class. This is the typical use | 42 // Returns a single shared instance of this class. This is the typical use |
| 43 // case in Chrome. | 43 // case in Chrome. |
| 44 // | 44 // |
| 45 // TODO(aa): Make this const to enforce thread-safe usage. | 45 // TODO(aa): Make this const to enforce thread-safe usage. |
| 46 static ExtensionAPI* GetSharedInstance(); | 46 static ExtensionAPI* GetSharedInstance(); |
| 47 | 47 |
| 48 // Creates a new instance configured the way ExtensionAPI typically is in | 48 // Creates a new instance configured the way ExtensionAPI typically is in |
| 49 // Chrome. Use the default constructor to get a clean instance. | 49 // Chrome. Use the default constructor to get a clean instance. |
| 50 static ExtensionAPI* CreateWithDefaultConfiguration(); | 50 static ExtensionAPI* CreateWithDefaultConfiguration(); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 81 // extension process. They cannot be called from extension code running in | 81 // extension process. They cannot be called from extension code running in |
| 82 // content scripts, or other low-privileged contexts. | 82 // content scripts, or other low-privileged contexts. |
| 83 bool IsPrivileged(const std::string& name); | 83 bool IsPrivileged(const std::string& name); |
| 84 | 84 |
| 85 // Gets the schema for the extension API with namespace |full_name|. | 85 // Gets the schema for the extension API with namespace |full_name|. |
| 86 // Ownership remains with this object. | 86 // Ownership remains with this object. |
| 87 const base::DictionaryValue* GetSchema(const std::string& full_name); | 87 const base::DictionaryValue* GetSchema(const std::string& full_name); |
| 88 | 88 |
| 89 std::set<std::string> GetAllAPINames(); | 89 std::set<std::string> GetAllAPINames(); |
| 90 | 90 |
| 91 // Gets a Feature object describing the API with the specified |full_name|. |
| 92 // This can be either an API namespace (like history, or |
| 93 // experimental.bookmarks), or it can be an individual function or event. |
| 94 virtual Feature* GetFeature(const std::string& full_name) OVERRIDE; |
| 95 |
| 91 // Splits a full name from the extension API into its API and child name | 96 // Splits a full name from the extension API into its API and child name |
| 92 // parts. Some examples: | 97 // parts. Some examples: |
| 93 // | 98 // |
| 94 // "bookmarks.create" -> ("bookmarks", "create") | 99 // "bookmarks.create" -> ("bookmarks", "create") |
| 95 // "experimental.input.ui.cursorUp" -> ("experimental.input.ui", "cursorUp") | 100 // "experimental.input.ui.cursorUp" -> ("experimental.input.ui", "cursorUp") |
| 96 // "storage.sync.set" -> ("storage", "sync.get") | 101 // "storage.sync.set" -> ("storage", "sync.get") |
| 97 // "<unknown-api>.monkey" -> ("", "") | 102 // "<unknown-api>.monkey" -> ("", "") |
| 98 // | 103 // |
| 99 // The |child_name| parameter can be be NULL if you don't need that part. | 104 // The |child_name| parameter can be be NULL if you don't need that part. |
| 100 std::string GetAPINameFromFullName(const std::string& full_name, | 105 std::string GetAPINameFromFullName(const std::string& full_name, |
| 101 std::string* child_name); | 106 std::string* child_name); |
| 102 | 107 |
| 103 void InitDefaultConfiguration(); | 108 void InitDefaultConfiguration(); |
| 104 | 109 |
| 105 // Gets a feature from any dependency provider registered with ExtensionAPI. | |
| 106 // Returns NULL if the feature could not be found. | |
| 107 Feature* GetFeatureDependency(const std::string& dependency_name); | |
| 108 | |
| 109 private: | 110 private: |
| 110 friend struct DefaultSingletonTraits<ExtensionAPI>; | 111 friend struct DefaultSingletonTraits<ExtensionAPI>; |
| 111 | 112 |
| 112 // Loads a schema. | 113 // Loads a schema. |
| 113 void LoadSchema(const std::string& name, const base::StringPiece& schema); | 114 void LoadSchema(const std::string& name, const base::StringPiece& schema); |
| 114 | 115 |
| 115 // Returns true if the function or event under |namespace_node| with | 116 // Returns true if the function or event under |namespace_node| with |
| 116 // the specified |child_name| is privileged, or false otherwise. If the name | 117 // the specified |child_name| is privileged, or false otherwise. If the name |
| 117 // is not found, defaults to privileged. | 118 // is not found, defaults to privileged. |
| 118 bool IsChildNamePrivileged(const base::DictionaryValue* namespace_node, | 119 bool IsChildNamePrivileged(const base::DictionaryValue* namespace_node, |
| 119 const std::string& child_name); | 120 const std::string& child_name); |
| 120 | 121 |
| 121 // NOTE: This IsAPIAllowed() and IsNonFeatureAPIAvailable only work for | 122 // NOTE: This IsAPIAllowed() and IsNonFeatureAPIAvailable only work for |
| 122 // non-feature-controlled APIs. | 123 // non-feature-controlled APIs. |
| 123 // TODO(aa): Remove these when all APIs are converted to the feature system. | 124 // TODO(aa): Remove these when all APIs are converted to the feature system. |
| 124 | 125 |
| 125 // Checks if API |name| is allowed. | 126 // Checks if API |name| is allowed. |
| 126 bool IsAPIAllowed(const std::string& name, const Extension* extension); | 127 bool IsAPIAllowed(const std::string& name, const Extension* extension); |
| 127 | 128 |
| 128 // Check if an API is available to |context| given an |extension| and |url|. | 129 // Check if an API is available to |context| given an |extension| and |url|. |
| 129 // The extension or URL may not be relevant to all contexts, and may be left | 130 // The extension or URL may not be relevant to all contexts, and may be left |
| 130 // NULL/empty. | 131 // NULL/empty. |
| 131 bool IsNonFeatureAPIAvailable(const std::string& name, | 132 bool IsNonFeatureAPIAvailable(const std::string& name, |
| 132 Feature::Context context, | 133 Feature::Context context, |
| 133 const Extension* extension, | 134 const Extension* extension, |
| 134 const GURL& url); | 135 const GURL& url); |
| 135 | 136 |
| 137 // Returns true if the API uses the feature system. |
| 138 bool UsesFeatureSystem(const std::string& full_name); |
| 139 |
| 140 // Gets a feature from any dependency provider. |
| 141 Feature* GetFeatureDependency(const std::string& dependency_name); |
| 142 |
| 143 // Adds dependent schemas to |out| as determined by the "dependencies" |
| 144 // property. |
| 145 // TODO(aa): Consider making public and adding tests. |
| 146 void ResolveDependencies(std::set<std::string>* out); |
| 147 |
| 148 // Adds any APIs listed in "dependencies" found in the schema for |api_name| |
| 149 // but not in |excluding| to |out|. |
| 150 void GetMissingDependencies( |
| 151 const std::string& api_name, |
| 152 const std::set<std::string>& excluding, |
| 153 std::set<std::string>* out); |
| 154 |
| 136 // Checks if an API is *entirely* privileged. This won't include APIs such as | 155 // Checks if an API is *entirely* privileged. This won't include APIs such as |
| 137 // "storage" which is entirely unprivileged, nor "extension" which has | 156 // "storage" which is entirely unprivileged, nor "extension" which has |
| 138 // unprivileged components. | 157 // unprivileged components. |
| 139 bool IsPrivilegedAPI(const std::string& name); | 158 bool IsPrivilegedAPI(const std::string& name); |
| 140 | 159 |
| 141 // Map from each API that hasn't been loaded yet to the schema which defines | 160 // Map from each API that hasn't been loaded yet to the schema which defines |
| 142 // it. Note that there may be multiple APIs per schema. | 161 // it. Note that there may be multiple APIs per schema. |
| 143 typedef std::map<std::string, base::StringPiece> UnloadedSchemaMap; | 162 typedef std::map<std::string, base::StringPiece> UnloadedSchemaMap; |
| 144 UnloadedSchemaMap unloaded_schemas_; | 163 UnloadedSchemaMap unloaded_schemas_; |
| 145 | 164 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 163 // FeatureProviders used for resolving dependencies. | 182 // FeatureProviders used for resolving dependencies. |
| 164 typedef std::map<std::string, FeatureProvider*> FeatureProviderMap; | 183 typedef std::map<std::string, FeatureProvider*> FeatureProviderMap; |
| 165 FeatureProviderMap dependency_providers_; | 184 FeatureProviderMap dependency_providers_; |
| 166 | 185 |
| 167 DISALLOW_COPY_AND_ASSIGN(ExtensionAPI); | 186 DISALLOW_COPY_AND_ASSIGN(ExtensionAPI); |
| 168 }; | 187 }; |
| 169 | 188 |
| 170 } // extensions | 189 } // extensions |
| 171 | 190 |
| 172 #endif // CHROME_COMMON_EXTENSIONS_API_EXTENSION_API_H_ | 191 #endif // CHROME_COMMON_EXTENSIONS_API_EXTENSION_API_H_ |
| OLD | NEW |