| 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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 void RegisterSchema(const std::string& api_name, | 64 void RegisterSchema(const std::string& api_name, |
| 65 const base::StringPiece& source); | 65 const base::StringPiece& source); |
| 66 | 66 |
| 67 void RegisterDependencyProvider(const std::string& name, | 67 void RegisterDependencyProvider(const std::string& name, |
| 68 FeatureProvider* provider); | 68 FeatureProvider* provider); |
| 69 | 69 |
| 70 // Returns true if the specified API is available. |api_full_name| can be | 70 // Returns true if the specified API is available. |api_full_name| can be |
| 71 // either a namespace name (like "bookmarks") or a member name (like | 71 // either a namespace name (like "bookmarks") or a member name (like |
| 72 // "bookmarks.create"). Returns true if the feature and all of its | 72 // "bookmarks.create"). Returns true if the feature and all of its |
| 73 // dependencies are available to the specified context. | 73 // dependencies are available to the specified context. |
| 74 bool IsAvailable(const std::string& api_full_name, | 74 Feature::Availability IsAvailable(const std::string& api_full_name, |
| 75 const Extension* extension, | 75 const Extension* extension, |
| 76 Feature::Context context); | 76 Feature::Context context, |
| 77 const GURL& url); |
| 77 | 78 |
| 78 // Returns true if |name| is a privileged API path. Privileged paths can only | 79 // Returns true if |name| is a privileged API path. Privileged paths can only |
| 79 // be called from extension code which is running in its own designated | 80 // be called from extension code which is running in its own designated |
| 80 // extension process. They cannot be called from extension code running in | 81 // extension process. They cannot be called from extension code running in |
| 81 // content scripts, or other low-privileged contexts. | 82 // content scripts, or other low-privileged contexts. |
| 82 bool IsPrivileged(const std::string& name); | 83 bool IsPrivileged(const std::string& name); |
| 83 | 84 |
| 84 // Gets the schema for the extension API with namespace |full_name|. | 85 // Gets the schema for the extension API with namespace |full_name|. |
| 85 // Ownership remains with this object. | 86 // Ownership remains with this object. |
| 86 const base::DictionaryValue* GetSchema(const std::string& full_name); | 87 const base::DictionaryValue* GetSchema(const std::string& full_name); |
| 87 | 88 |
| 88 // Gets the APIs available to |context| given an |extension| and |url|. The | |
| 89 // extension or URL may not be relevant to all contexts, and may be left | |
| 90 // NULL/empty. | |
| 91 std::set<std::string> GetAPIsForContext( | |
| 92 Feature::Context context, const Extension* extension, const GURL& url); | |
| 93 | |
| 94 std::set<std::string> GetAllAPINames(); | 89 std::set<std::string> GetAllAPINames(); |
| 95 | 90 |
| 96 // Gets a Feature object describing the API with the specified |full_name|. | 91 // Gets a Feature object describing the API with the specified |full_name|. |
| 97 // This can be either an API namespace (like history, or | 92 // This can be either an API namespace (like history, or |
| 98 // experimental.bookmarks), or it can be an individual function or event. | 93 // experimental.bookmarks), or it can be an individual function or event. |
| 99 virtual Feature* GetFeature(const std::string& full_name) OVERRIDE; | 94 virtual Feature* GetFeature(const std::string& full_name) OVERRIDE; |
| 100 | 95 |
| 101 // 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 |
| 102 // parts. Some examples: | 97 // parts. Some examples: |
| 103 // | 98 // |
| 104 // "bookmarks.create" -> ("bookmarks", "create") | 99 // "bookmarks.create" -> ("bookmarks", "create") |
| 105 // "experimental.input.ui.cursorUp" -> ("experimental.input.ui", "cursorUp") | 100 // "experimental.input.ui.cursorUp" -> ("experimental.input.ui", "cursorUp") |
| 106 // "storage.sync.set" -> ("storage", "sync.get") | 101 // "storage.sync.set" -> ("storage", "sync.get") |
| 107 // "<unknown-api>.monkey" -> ("", "") | 102 // "<unknown-api>.monkey" -> ("", "") |
| 108 // | 103 // |
| 109 // 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. |
| 110 std::string GetAPINameFromFullName(const std::string& full_name, | 105 std::string GetAPINameFromFullName(const std::string& full_name, |
| 111 std::string* child_name); | 106 std::string* child_name); |
| 112 | 107 |
| 113 void InitDefaultConfiguration(); | 108 void InitDefaultConfiguration(); |
| 114 | 109 |
| 115 // Loads the schemas registered with RegisterSchema(). | |
| 116 void LoadAllSchemas(); | |
| 117 | |
| 118 private: | 110 private: |
| 119 friend struct DefaultSingletonTraits<ExtensionAPI>; | 111 friend struct DefaultSingletonTraits<ExtensionAPI>; |
| 120 | 112 |
| 121 // Loads a schema. | 113 // Loads a schema. |
| 122 void LoadSchema(const std::string& name, const base::StringPiece& schema); | 114 void LoadSchema(const std::string& name, const base::StringPiece& schema); |
| 123 | 115 |
| 124 // Returns true if the function or event under |namespace_node| with | 116 // Returns true if the function or event under |namespace_node| with |
| 125 // 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 |
| 126 // is not found, defaults to privileged. | 118 // is not found, defaults to privileged. |
| 127 bool IsChildNamePrivileged(const base::DictionaryValue* namespace_node, | 119 bool IsChildNamePrivileged(const base::DictionaryValue* namespace_node, |
| 128 const std::string& child_name); | 120 const std::string& child_name); |
| 129 | 121 |
| 130 // Adds all APIs to |out| that |extension| has any permission (required or | 122 // NOTE: This IsAPIAllowed() and IsNonFeatureAPIAvailable only work for |
| 131 // optional) to use. | 123 // non-feature-controlled APIs. |
| 132 // NOTE: This only works for non-feature-controlled APIs. | 124 // TODO(aa): Remove these when all APIs are converted to the feature system. |
| 133 // TODO(aa): Remove this when all APIs are converted to the feature system. | 125 |
| 134 void GetAllowedAPIs(const Extension* extension, std::set<std::string>* out); | 126 // Checks if API |name| is allowed. |
| 127 bool IsAPIAllowed(const std::string& name, const Extension* extension); |
| 128 |
| 129 // Check if an API is available to |context| given an |extension| and |url|. |
| 130 // The extension or URL may not be relevant to all contexts, and may be left |
| 131 // NULL/empty. |
| 132 bool IsNonFeatureAPIAvailable(const std::string& name, |
| 133 Feature::Context context, |
| 134 const Extension* extension, |
| 135 const GURL& url); |
| 136 |
| 137 // Returns true if the API uses the feature system. |
| 138 bool UsesFeatureSystem(const std::string& full_name); |
| 135 | 139 |
| 136 // Gets a feature from any dependency provider. | 140 // Gets a feature from any dependency provider. |
| 137 Feature* GetFeatureDependency(const std::string& dependency_name); | 141 Feature* GetFeatureDependency(const std::string& dependency_name); |
| 138 | 142 |
| 139 // Adds dependent schemas to |out| as determined by the "dependencies" | 143 // Adds dependent schemas to |out| as determined by the "dependencies" |
| 140 // property. | 144 // property. |
| 141 // TODO(aa): Consider making public and adding tests. | 145 // TODO(aa): Consider making public and adding tests. |
| 142 void ResolveDependencies(std::set<std::string>* out); | 146 void ResolveDependencies(std::set<std::string>* out); |
| 143 | 147 |
| 144 // Adds any APIs listed in "dependencies" found in the schema for |api_name| | 148 // Adds any APIs listed in "dependencies" found in the schema for |api_name| |
| 145 // but not in |excluding| to |out|. | 149 // but not in |excluding| to |out|. |
| 146 void GetMissingDependencies( | 150 void GetMissingDependencies( |
| 147 const std::string& api_name, | 151 const std::string& api_name, |
| 148 const std::set<std::string>& excluding, | 152 const std::set<std::string>& excluding, |
| 149 std::set<std::string>* out); | 153 std::set<std::string>* out); |
| 150 | 154 |
| 151 // Removes all APIs from |apis| which are *entirely* privileged. This won't | 155 // Checks if an API is *entirely* privileged. This won't include APIs such as |
| 152 // include APIs such as "storage" which is entirely unprivileged, nor | 156 // "storage" which is entirely unprivileged, nor "extension" which has |
| 153 // "extension" which has unprivileged components. | 157 // unprivileged components. |
| 154 void RemovePrivilegedAPIs(std::set<std::string>* apis); | 158 bool IsPrivilegedAPI(const std::string& name); |
| 155 | |
| 156 // Adds an APIs that match |url| to |out|. | |
| 157 // NOTE: This only works for non-feature-controlled APIs. | |
| 158 // TODO(aa): Remove this when all APIs are converted to the feature system. | |
| 159 void GetAPIsMatchingURL(const GURL& url, std::set<std::string>* out); | |
| 160 | 159 |
| 161 // 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 |
| 162 // it. Note that there may be multiple APIs per schema. | 161 // it. Note that there may be multiple APIs per schema. |
| 163 typedef std::map<std::string, base::StringPiece> UnloadedSchemaMap; | 162 typedef std::map<std::string, base::StringPiece> UnloadedSchemaMap; |
| 164 UnloadedSchemaMap unloaded_schemas_; | 163 UnloadedSchemaMap unloaded_schemas_; |
| 165 | 164 |
| 166 // Schemas for each namespace. | 165 // Schemas for each namespace. |
| 167 typedef std::map<std::string, linked_ptr<const DictionaryValue> > SchemaMap; | 166 typedef std::map<std::string, linked_ptr<const DictionaryValue> > SchemaMap; |
| 168 SchemaMap schemas_; | 167 SchemaMap schemas_; |
| 169 | 168 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 183 // FeatureProviders used for resolving dependencies. | 182 // FeatureProviders used for resolving dependencies. |
| 184 typedef std::map<std::string, FeatureProvider*> FeatureProviderMap; | 183 typedef std::map<std::string, FeatureProvider*> FeatureProviderMap; |
| 185 FeatureProviderMap dependency_providers_; | 184 FeatureProviderMap dependency_providers_; |
| 186 | 185 |
| 187 DISALLOW_COPY_AND_ASSIGN(ExtensionAPI); | 186 DISALLOW_COPY_AND_ASSIGN(ExtensionAPI); |
| 188 }; | 187 }; |
| 189 | 188 |
| 190 } // extensions | 189 } // extensions |
| 191 | 190 |
| 192 #endif // CHROME_COMMON_EXTENSIONS_API_EXTENSION_API_H_ | 191 #endif // CHROME_COMMON_EXTENSIONS_API_EXTENSION_API_H_ |
| OLD | NEW |