| 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 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <set> | 10 #include <set> |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 14 #include "base/memory/linked_ptr.h" | 14 #include "base/memory/linked_ptr.h" |
| 15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/memory/singleton.h" | 16 #include "base/memory/singleton.h" |
| 17 #include "base/string_piece.h" | 17 #include "base/string_piece.h" |
| 18 #include "base/values.h" | 18 #include "base/values.h" |
| 19 #include "chrome/common/extensions/feature.h" | 19 #include "chrome/common/extensions/feature.h" |
| 20 #include "chrome/common/extensions/feature_provider.h" | |
| 21 #include "chrome/common/extensions/url_pattern_set.h" | 20 #include "chrome/common/extensions/url_pattern_set.h" |
| 22 | 21 |
| 23 namespace base { | 22 namespace base { |
| 24 class DictionaryValue; | 23 class DictionaryValue; |
| 25 class ListValue; | 24 class ListValue; |
| 26 class Value; | 25 class Value; |
| 27 } | 26 } |
| 28 | 27 |
| 29 class GURL; | 28 class GURL; |
| 30 class Extension; | 29 class Extension; |
| 31 class ExtensionPermissionSet; | 30 class ExtensionPermissionSet; |
| 32 | 31 |
| 33 namespace extensions { | 32 namespace extensions { |
| 34 | 33 |
| 35 class Feature; | 34 // C++ Wrapper for the JSON API definitions in chrome/common/extensions/api/. |
| 35 class ExtensionAPI { |
| 36 public: |
| 37 // Returns the single instance of this class. |
| 38 static ExtensionAPI* GetInstance(); |
| 36 | 39 |
| 37 // C++ Wrapper for the JSON API definitions in chrome/common/extensions/api/. | 40 // Public for construction from unit tests. Use GetInstance() normally. |
| 38 // | |
| 39 // WARNING: This class is accessed on multiple threads in the browser process | |
| 40 // (see ExtensionFunctionDispatcher). No state should be modified after | |
| 41 // construction. | |
| 42 class ExtensionAPI : public FeatureProvider { | |
| 43 public: | |
| 44 // Returns a single shared instance of this class. This is the typical use | |
| 45 // case in Chrome. | |
| 46 // | |
| 47 // TODO(aa): Make this const to enforce thread-safe usage. | |
| 48 static ExtensionAPI* GetSharedInstance(); | |
| 49 | |
| 50 // Creates a new instance configured the way ExtensionAPI typically is in | |
| 51 // Chrome. Use the default constructor to get a clean instance. | |
| 52 static ExtensionAPI* CreateWithDefaultConfiguration(); | |
| 53 | |
| 54 // Splits a name like "permission:bookmark" into ("permission", "bookmark"). | |
| 55 // The first part refers to a type of feature, for example "manifest", | |
| 56 // "permission", or "api". The second part is the full name of the feature. | |
| 57 static void SplitDependencyName(const std::string& full_name, | |
| 58 std::string* feature_type, | |
| 59 std::string* feature_name); | |
| 60 | |
| 61 // Creates a completely clean instance. Configure using RegisterSchema() and | |
| 62 // RegisterDependencyProvider before use. | |
| 63 ExtensionAPI(); | 41 ExtensionAPI(); |
| 64 virtual ~ExtensionAPI(); | 42 ~ExtensionAPI(); |
| 65 | |
| 66 void RegisterSchema(const std::string& api_name, | |
| 67 const base::StringPiece& source); | |
| 68 | |
| 69 void RegisterDependencyProvider(const std::string& name, | |
| 70 FeatureProvider* provider); | |
| 71 | |
| 72 // Returns true if the specified API is available. |api_full_name| can be | |
| 73 // either a namespace name (like "bookmarks") or a member name (like | |
| 74 // "bookmarks.create"). Returns true if the feature and all of its | |
| 75 // dependencies are available to the specified context. | |
| 76 bool IsAvailable(const std::string& api_full_name, | |
| 77 const Extension* extension, | |
| 78 Feature::Context context); | |
| 79 | 43 |
| 80 // Returns true if |name| is a privileged API path. Privileged paths can only | 44 // Returns true if |name| is a privileged API path. Privileged paths can only |
| 81 // be called from extension code which is running in its own designated | 45 // be called from extension code which is running in its own designated |
| 82 // extension process. They cannot be called from extension code running in | 46 // extension process. They cannot be called from extension code running in |
| 83 // content scripts, or other low-privileged contexts. | 47 // content scripts, or other low-privileged contexts. |
| 84 bool IsPrivileged(const std::string& name); | 48 bool IsPrivileged(const std::string& name); |
| 85 | 49 |
| 86 // Gets the schema for the extension API with namespace |full_name|. | 50 // Gets the schema for the extension API with namespace |api_name|. |
| 87 // Ownership remains with this object. | 51 // Ownership remains with this object. |
| 88 const base::DictionaryValue* GetSchema(const std::string& full_name); | 52 const base::DictionaryValue* GetSchema(const std::string& api_name); |
| 89 | 53 |
| 90 // Gets the APIs available to |context| given an |extension| and |url|. The | 54 // Gets the APIs available to |context| given an |extension| and |url|. The |
| 91 // extension or URL may not be relevant to all contexts, and may be left | 55 // extension or URL may not be relevant to all contexts, and may be left |
| 92 // NULL/empty. | 56 // NULL/empty. |
| 93 scoped_ptr<std::set<std::string> > GetAPIsForContext( | 57 scoped_ptr<std::set<std::string> > GetAPIsForContext( |
| 94 Feature::Context context, const Extension* extension, const GURL& url); | 58 Feature::Context context, const Extension* extension, const GURL& url); |
| 95 | 59 |
| 96 // Gets a Feature object describing the API with the specified |full_name|. | |
| 97 // This can be either an API namespace (like history, or | |
| 98 // experimental.bookmarks), or it can be an individual function or event. | |
| 99 virtual scoped_ptr<Feature> GetFeature(const std::string& full_name) OVERRIDE; | |
| 100 | |
| 101 // Splits a full name from the extension API into its API and child name | |
| 102 // parts. Some examples: | |
| 103 // | |
| 104 // "bookmarks.create" -> ("bookmarks", "create") | |
| 105 // "experimental.input.ui.cursorUp" -> ("experimental.input.ui", "cursorUp") | |
| 106 // "storage.sync.set" -> ("storage", "sync.get") | |
| 107 // "<unknown-api>.monkey" -> ("", "") | |
| 108 // | |
| 109 // The |child_name| parameter can be be NULL if you don't need that part. | |
| 110 std::string GetAPINameFromFullName(const std::string& full_name, | |
| 111 std::string* child_name); | |
| 112 | |
| 113 void InitDefaultConfiguration(); | |
| 114 | |
| 115 // Loads the schemas registered with RegisterSchema(). | |
| 116 void LoadAllSchemas(); | |
| 117 | |
| 118 private: | 60 private: |
| 119 friend struct DefaultSingletonTraits<ExtensionAPI>; | 61 friend struct DefaultSingletonTraits<ExtensionAPI>; |
| 120 | 62 |
| 121 // Loads a schema. | 63 // Loads a schema. |
| 122 void LoadSchema(const base::StringPiece& schema); | 64 void LoadSchema(const base::StringPiece& schema); |
| 123 | 65 |
| 66 // Find an item in |list| with the specified property name and value, or NULL |
| 67 // if no such item exists. |
| 68 base::DictionaryValue* FindListItem(const base::ListValue* list, |
| 69 const std::string& property_name, |
| 70 const std::string& property_value); |
| 71 |
| 124 // Returns true if the function or event under |namespace_node| with | 72 // Returns true if the function or event under |namespace_node| with |
| 125 // the specified |child_name| is privileged, or false otherwise. If the name | 73 // the specified |child_name| is privileged, or false otherwise. If the name |
| 126 // is not found, defaults to privileged. | 74 // is not found, defaults to privileged. |
| 127 bool IsChildNamePrivileged(const base::DictionaryValue* namespace_node, | 75 bool IsChildNamePrivileged(const base::DictionaryValue* namespace_node, |
| 76 const std::string& child_kind, |
| 128 const std::string& child_name); | 77 const std::string& child_name); |
| 129 | 78 |
| 130 // Adds all APIs to |out| that |extension| has any permission (required or | 79 // Adds all APIs to |out| that |extension| has any permission (required or |
| 131 // optional) to use. | 80 // optional) to use. |
| 132 // NOTE: This only works for non-feature-controlled APIs. | |
| 133 // TODO(aa): Remove this when all APIs are converted to the feature system. | |
| 134 void GetAllowedAPIs(const Extension* extension, std::set<std::string>* out); | 81 void GetAllowedAPIs(const Extension* extension, std::set<std::string>* out); |
| 135 | 82 |
| 136 // Gets a feature from any dependency provider. | |
| 137 scoped_ptr<Feature> GetFeatureDependency(const std::string& dependency_name); | |
| 138 | |
| 139 // Adds dependent schemas to |out| as determined by the "dependencies" | 83 // Adds dependent schemas to |out| as determined by the "dependencies" |
| 140 // property. | 84 // property. |
| 141 // TODO(aa): Consider making public and adding tests. | |
| 142 void ResolveDependencies(std::set<std::string>* out); | 85 void ResolveDependencies(std::set<std::string>* out); |
| 143 | 86 |
| 144 // Adds any APIs listed in "dependencies" found in the schema for |api_name| | 87 // Adds any APIs listed in "dependencies" found in the schema for |api_name| |
| 145 // but not in |excluding| to |out|. | 88 // but not in |excluding| to |out|. |
| 146 void GetMissingDependencies( | 89 void GetMissingDependencies( |
| 147 const std::string& api_name, | 90 const std::string& api_name, |
| 148 const std::set<std::string>& excluding, | 91 const std::set<std::string>& excluding, |
| 149 std::set<std::string>* out); | 92 std::set<std::string>* out); |
| 150 | 93 |
| 151 // Removes all APIs from |apis| which are *entirely* privileged. This won't | 94 // Removes all APIs from |apis| which are *entirely* privileged. This won't |
| 152 // include APIs such as "storage" which is entirely unprivileged, nor | 95 // include APIs such as "storage" which is entirely unprivileged, nor |
| 153 // "extension" which has unprivileged components. | 96 // "extension" which has unprivileged components. |
| 154 void RemovePrivilegedAPIs(std::set<std::string>* apis); | 97 void RemovePrivilegedAPIs(std::set<std::string>* apis); |
| 155 | 98 |
| 156 // Adds an APIs that match |url| to |out|. | 99 // 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); | 100 void GetAPIsMatchingURL(const GURL& url, std::set<std::string>* out); |
| 160 | 101 |
| 102 // Loads all remaining resources from |unloaded_schemas_|. |
| 103 void LoadAllSchemas(); |
| 104 |
| 105 static ExtensionAPI* instance_; |
| 106 |
| 161 // Map from each API that hasn't been loaded yet to the schema which defines | 107 // 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. | 108 // it. Note that there may be multiple APIs per schema. |
| 163 std::map<std::string, base::StringPiece> unloaded_schemas_; | 109 std::map<std::string, base::StringPiece> unloaded_schemas_; |
| 164 | 110 |
| 165 // Schemas for each namespace. | 111 // Schemas for each namespace. |
| 166 typedef std::map<std::string, linked_ptr<const DictionaryValue> > SchemaMap; | 112 typedef std::map<std::string, linked_ptr<const DictionaryValue> > SchemaMap; |
| 167 SchemaMap schemas_; | 113 SchemaMap schemas_; |
| 168 | 114 |
| 169 // APIs that are entirely unprivileged. | 115 // APIs that are entirely unprivileged. |
| 170 std::set<std::string> completely_unprivileged_apis_; | 116 std::set<std::string> completely_unprivileged_apis_; |
| 171 | 117 |
| 172 // APIs that are not entirely unprivileged, but have unprivileged components. | 118 // APIs that are not entirely unprivileged, but have unprivileged components. |
| 173 std::set<std::string> partially_unprivileged_apis_; | 119 std::set<std::string> partially_unprivileged_apis_; |
| 174 | 120 |
| 175 // APIs that have URL matching permissions. | 121 // APIs that have URL matching permissions. |
| 176 std::map<std::string, URLPatternSet> url_matching_apis_; | 122 std::map<std::string, URLPatternSet> url_matching_apis_; |
| 177 | 123 |
| 178 typedef std::map<std::string, linked_ptr<Feature> > FeatureMap; | |
| 179 typedef std::map<std::string, linked_ptr<FeatureMap> > APIFeatureMap; | |
| 180 APIFeatureMap features_; | |
| 181 | |
| 182 // FeatureProviders used for resolving dependencies. | |
| 183 typedef std::map<std::string, FeatureProvider*> FeatureProviderMap; | |
| 184 FeatureProviderMap dependency_providers_; | |
| 185 | |
| 186 DISALLOW_COPY_AND_ASSIGN(ExtensionAPI); | 124 DISALLOW_COPY_AND_ASSIGN(ExtensionAPI); |
| 187 }; | 125 }; |
| 188 | 126 |
| 189 } // extensions | 127 } // extensions |
| 190 | 128 |
| 191 #endif // CHROME_COMMON_EXTENSIONS_API_EXTENSION_API_H_ | 129 #endif // CHROME_COMMON_EXTENSIONS_API_EXTENSION_API_H_ |
| OLD | NEW |