Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(278)

Side by Side Diff: chrome/common/extensions/api/extension_api.h

Issue 9950046: Implement FeatureProvider for ExtensionAPI. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: hate hate hate Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
20 #include "chrome/common/extensions/url_pattern_set.h" 21 #include "chrome/common/extensions/url_pattern_set.h"
21 22
22 namespace base { 23 namespace base {
23 class DictionaryValue; 24 class DictionaryValue;
24 class ListValue; 25 class ListValue;
25 class Value; 26 class Value;
26 } 27 }
27 28
28 class GURL; 29 class GURL;
29 class Extension; 30 class Extension;
30 class ExtensionPermissionSet; 31 class ExtensionPermissionSet;
31 32
32 namespace extensions { 33 namespace extensions {
33 34
35 class Feature;
36
34 // C++ Wrapper for the JSON API definitions in chrome/common/extensions/api/. 37 // C++ Wrapper for the JSON API definitions in chrome/common/extensions/api/.
35 class ExtensionAPI { 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 {
36 public: 43 public:
37 // Returns the single instance of this class. 44 // Returns a single shared instance of this class. This is the typical use
38 static ExtensionAPI* GetInstance(); 45 // case in Chrome.
46 //
47 // TODO(aa): Make this const to enforce thread-safe usage.
48 static ExtensionAPI* GetSharedInstance();
39 49
40 // Public for construction from unit tests. Use GetInstance() normally. 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.
41 ExtensionAPI(); 63 ExtensionAPI();
42 ~ExtensionAPI(); 64 virtual ~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);
43 79
44 // Returns true if |name| is a privileged API path. Privileged paths can only 80 // Returns true if |name| is a privileged API path. Privileged paths can only
45 // be called from extension code which is running in its own designated 81 // be called from extension code which is running in its own designated
46 // extension process. They cannot be called from extension code running in 82 // extension process. They cannot be called from extension code running in
47 // content scripts, or other low-privileged contexts. 83 // content scripts, or other low-privileged contexts.
48 bool IsPrivileged(const std::string& name); 84 bool IsPrivileged(const std::string& name);
49 85
50 // Gets the schema for the extension API with namespace |api_name|. 86 // Gets the schema for the extension API with namespace |full_name|.
51 // Ownership remains with this object. 87 // Ownership remains with this object.
52 const base::DictionaryValue* GetSchema(const std::string& api_name); 88 const base::DictionaryValue* GetSchema(const std::string& full_name);
53 89
54 // Gets the APIs available to |context| given an |extension| and |url|. The 90 // Gets the APIs available to |context| given an |extension| and |url|. The
55 // extension or URL may not be relevant to all contexts, and may be left 91 // extension or URL may not be relevant to all contexts, and may be left
56 // NULL/empty. 92 // NULL/empty.
57 scoped_ptr<std::set<std::string> > GetAPIsForContext( 93 scoped_ptr<std::set<std::string> > GetAPIsForContext(
58 Feature::Context context, const Extension* extension, const GURL& url); 94 Feature::Context context, const Extension* extension, const GURL& url);
59 95
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
60 private: 118 private:
61 friend struct DefaultSingletonTraits<ExtensionAPI>; 119 friend struct DefaultSingletonTraits<ExtensionAPI>;
62 120
63 // Loads a schema. 121 // Loads a schema.
64 void LoadSchema(const base::StringPiece& schema); 122 void LoadSchema(const base::StringPiece& schema);
65 123
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
72 // Returns true if the function or event under |namespace_node| with 124 // Returns true if the function or event under |namespace_node| with
73 // the specified |child_name| is privileged, or false otherwise. If the name 125 // the specified |child_name| is privileged, or false otherwise. If the name
74 // is not found, defaults to privileged. 126 // is not found, defaults to privileged.
75 bool IsChildNamePrivileged(const base::DictionaryValue* namespace_node, 127 bool IsChildNamePrivileged(const base::DictionaryValue* namespace_node,
76 const std::string& child_kind,
77 const std::string& child_name); 128 const std::string& child_name);
78 129
79 // Adds all APIs to |out| that |extension| has any permission (required or 130 // Adds all APIs to |out| that |extension| has any permission (required or
80 // optional) to use. 131 // 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.
81 void GetAllowedAPIs(const Extension* extension, std::set<std::string>* out); 134 void GetAllowedAPIs(const Extension* extension, std::set<std::string>* out);
82 135
136 // Gets a feature from any dependency provider.
137 scoped_ptr<Feature> GetFeatureDependency(const std::string& dependency_name);
138
83 // Adds dependent schemas to |out| as determined by the "dependencies" 139 // Adds dependent schemas to |out| as determined by the "dependencies"
84 // property. 140 // property.
141 // TODO(aa): Consider making public and adding tests.
85 void ResolveDependencies(std::set<std::string>* out); 142 void ResolveDependencies(std::set<std::string>* out);
86 143
87 // Adds any APIs listed in "dependencies" found in the schema for |api_name| 144 // Adds any APIs listed in "dependencies" found in the schema for |api_name|
88 // but not in |excluding| to |out|. 145 // but not in |excluding| to |out|.
89 void GetMissingDependencies( 146 void GetMissingDependencies(
90 const std::string& api_name, 147 const std::string& api_name,
91 const std::set<std::string>& excluding, 148 const std::set<std::string>& excluding,
92 std::set<std::string>* out); 149 std::set<std::string>* out);
93 150
94 // Removes all APIs from |apis| which are *entirely* privileged. This won't 151 // Removes all APIs from |apis| which are *entirely* privileged. This won't
95 // include APIs such as "storage" which is entirely unprivileged, nor 152 // include APIs such as "storage" which is entirely unprivileged, nor
96 // "extension" which has unprivileged components. 153 // "extension" which has unprivileged components.
97 void RemovePrivilegedAPIs(std::set<std::string>* apis); 154 void RemovePrivilegedAPIs(std::set<std::string>* apis);
98 155
99 // Adds an APIs that match |url| to |out|. 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.
100 void GetAPIsMatchingURL(const GURL& url, std::set<std::string>* out); 159 void GetAPIsMatchingURL(const GURL& url, std::set<std::string>* out);
101 160
102 // Loads all remaining resources from |unloaded_schemas_|.
103 void LoadAllSchemas();
104
105 static ExtensionAPI* instance_;
106
107 // Map from each API that hasn't been loaded yet to the schema which defines 161 // Map from each API that hasn't been loaded yet to the schema which defines
108 // it. Note that there may be multiple APIs per schema. 162 // it. Note that there may be multiple APIs per schema.
109 std::map<std::string, base::StringPiece> unloaded_schemas_; 163 std::map<std::string, base::StringPiece> unloaded_schemas_;
110 164
111 // Schemas for each namespace. 165 // Schemas for each namespace.
112 typedef std::map<std::string, linked_ptr<const DictionaryValue> > SchemaMap; 166 typedef std::map<std::string, linked_ptr<const DictionaryValue> > SchemaMap;
113 SchemaMap schemas_; 167 SchemaMap schemas_;
114 168
115 // APIs that are entirely unprivileged. 169 // APIs that are entirely unprivileged.
116 std::set<std::string> completely_unprivileged_apis_; 170 std::set<std::string> completely_unprivileged_apis_;
117 171
118 // APIs that are not entirely unprivileged, but have unprivileged components. 172 // APIs that are not entirely unprivileged, but have unprivileged components.
119 std::set<std::string> partially_unprivileged_apis_; 173 std::set<std::string> partially_unprivileged_apis_;
120 174
121 // APIs that have URL matching permissions. 175 // APIs that have URL matching permissions.
122 std::map<std::string, URLPatternSet> url_matching_apis_; 176 std::map<std::string, URLPatternSet> url_matching_apis_;
123 177
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
124 DISALLOW_COPY_AND_ASSIGN(ExtensionAPI); 186 DISALLOW_COPY_AND_ASSIGN(ExtensionAPI);
125 }; 187 };
126 188
127 } // extensions 189 } // extensions
128 190
129 #endif // CHROME_COMMON_EXTENSIONS_API_EXTENSION_API_H_ 191 #endif // CHROME_COMMON_EXTENSIONS_API_EXTENSION_API_H_
OLDNEW
« no previous file with comments | « chrome/common/extensions/api/bookmarks.json ('k') | chrome/common/extensions/api/extension_api.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698