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

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

Issue 15091002: Lazily load API schemas from resource files and convert all APIs to features (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix chromeos tests Created 7 years, 6 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 7
8 #include <map> 8 #include <map>
9 #include <set>
10 #include <string> 9 #include <string>
11 10
12 #include "base/basictypes.h" 11 #include "base/basictypes.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/linked_ptr.h" 13 #include "base/memory/linked_ptr.h"
14 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/singleton.h" 15 #include "base/memory/singleton.h"
16 #include "base/strings/string_piece.h" 16 #include "base/strings/string_piece.h"
17 #include "base/values.h" 17 #include "base/values.h"
18 #include "chrome/common/extensions/features/feature.h" 18 #include "chrome/common/extensions/features/feature.h"
19 #include "chrome/common/extensions/features/feature_provider.h" 19 #include "chrome/common/extensions/features/feature_provider.h"
20 #include "extensions/common/url_pattern_set.h" 20 #include "extensions/common/url_pattern_set.h"
21 21
22 namespace base { 22 namespace base {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 // "permission", or "api". The second part is the full name of the feature. 54 // "permission", or "api". The second part is the full name of the feature.
55 static void SplitDependencyName(const std::string& full_name, 55 static void SplitDependencyName(const std::string& full_name,
56 std::string* feature_type, 56 std::string* feature_type,
57 std::string* feature_name); 57 std::string* feature_name);
58 58
59 // Creates a completely clean instance. Configure using RegisterSchema() and 59 // Creates a completely clean instance. Configure using RegisterSchema() and
60 // RegisterDependencyProvider before use. 60 // RegisterDependencyProvider before use.
61 ExtensionAPI(); 61 ExtensionAPI();
62 virtual ~ExtensionAPI(); 62 virtual ~ExtensionAPI();
63 63
64 void RegisterSchema(const std::string& api_name, 64 void RegisterSchemaResource(const std::string& api_name, int resource_id);
65 const base::StringPiece& source);
66 65
67 void RegisterDependencyProvider(const std::string& name, 66 void RegisterDependencyProvider(const std::string& name,
68 FeatureProvider* provider); 67 FeatureProvider* provider);
69 68
70 // Returns true if the specified API is available. |api_full_name| can be 69 // 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 70 // either a namespace name (like "bookmarks") or a member name (like
72 // "bookmarks.create"). Returns true if the feature and all of its 71 // "bookmarks.create"). Returns true if the feature and all of its
73 // dependencies are available to the specified context. 72 // dependencies are available to the specified context.
74 Feature::Availability IsAvailable(const std::string& api_full_name, 73 Feature::Availability IsAvailable(const std::string& api_full_name,
75 const Extension* extension, 74 const Extension* extension,
76 Feature::Context context, 75 Feature::Context context,
77 const GURL& url); 76 const GURL& url);
78 77
79 // Determines whether an API, or any parts of that API, are available in 78 // Determines whether an API, or any parts of that API, are available in
80 // |context|. 79 // |context|.
81 bool IsAnyFeatureAvailableToContext(const std::string& api_name, 80 bool IsAnyFeatureAvailableToContext(const std::string& api_name,
82 Feature::Context context, 81 Feature::Context context,
83 const GURL& url); 82 const GURL& url);
84 83
85 // Returns true if |name| is a privileged API path. Privileged paths can only 84 // Returns true if |name| is a privileged API path. Privileged paths can only
86 // be called from extension code which is running in its own designated 85 // be called from extension code which is running in its own designated
87 // extension process. They cannot be called from extension code running in 86 // extension process. They cannot be called from extension code running in
88 // content scripts, or other low-privileged contexts. 87 // content scripts, or other low-privileged contexts.
89 bool IsPrivileged(const std::string& name); 88 bool IsPrivileged(const std::string& name);
90 89
91 // Gets the schema for the extension API with namespace |full_name|. 90 // Gets the schema for the extension API with namespace |full_name|.
92 // Ownership remains with this object. 91 // Ownership remains with this object.
93 const base::DictionaryValue* GetSchema(const std::string& full_name); 92 const base::DictionaryValue* GetSchema(const std::string& full_name);
94 93
95 std::set<std::string> GetAllAPINames();
96
97 // Splits a full name from the extension API into its API and child name 94 // Splits a full name from the extension API into its API and child name
98 // parts. Some examples: 95 // parts. Some examples:
99 // 96 //
100 // "bookmarks.create" -> ("bookmarks", "create") 97 // "bookmarks.create" -> ("bookmarks", "create")
101 // "experimental.input.ui.cursorUp" -> ("experimental.input.ui", "cursorUp") 98 // "experimental.input.ui.cursorUp" -> ("experimental.input.ui", "cursorUp")
102 // "storage.sync.set" -> ("storage", "sync.get") 99 // "storage.sync.set" -> ("storage", "sync.get")
103 // "<unknown-api>.monkey" -> ("", "") 100 // "<unknown-api>.monkey" -> ("", "")
104 // 101 //
105 // The |child_name| parameter can be be NULL if you don't need that part. 102 // The |child_name| parameter can be be NULL if you don't need that part.
106 std::string GetAPINameFromFullName(const std::string& full_name, 103 std::string GetAPINameFromFullName(const std::string& full_name,
107 std::string* child_name); 104 std::string* child_name);
108 105
106 private:
107 FRIEND_TEST_ALL_PREFIXES(ExtensionAPITest, DefaultConfigurationFeatures);
108 FRIEND_TEST_ALL_PREFIXES(ExtensionAPITest, TypesHaveNamespace);
109 friend struct DefaultSingletonTraits<ExtensionAPI>;
110
109 void InitDefaultConfiguration(); 111 void InitDefaultConfiguration();
110 112
113 bool default_configuration_initialized_;
114
111 // Gets a feature from any dependency provider registered with ExtensionAPI. 115 // Gets a feature from any dependency provider registered with ExtensionAPI.
112 // Returns NULL if the feature could not be found. 116 // Returns NULL if the feature could not be found.
113 Feature* GetFeatureDependency(const std::string& dependency_name); 117 Feature* GetFeatureDependency(const std::string& dependency_name);
114 118
115 private:
116 friend struct DefaultSingletonTraits<ExtensionAPI>;
117
118 // Loads a schema. 119 // Loads a schema.
119 void LoadSchema(const std::string& name, const base::StringPiece& schema); 120 void LoadSchema(const std::string& name, const base::StringPiece& schema);
120 121
121 // Returns true if the function or event under |namespace_node| with
122 // the specified |child_name| is privileged, or false otherwise. If the name
123 // is not found, defaults to privileged.
124 bool IsChildNamePrivileged(const base::DictionaryValue* namespace_node,
125 const std::string& child_name);
126
127 // NOTE: This IsAPIAllowed() and IsNonFeatureAPIAvailable only work for
128 // non-feature-controlled APIs.
129 // TODO(aa): Remove these when all APIs are converted to the feature system.
130
131 // Checks if API |name| is allowed.
132 bool IsAPIAllowed(const std::string& name, const Extension* extension);
133
134 // Check if an API is available to |context| given an |extension| and |url|.
135 // The extension or URL may not be relevant to all contexts, and may be left
136 // NULL/empty.
137 bool IsNonFeatureAPIAvailable(const std::string& name,
138 Feature::Context context,
139 const Extension* extension,
140 const GURL& url);
141
142 // Checks if an API is *entirely* privileged. This won't include APIs such as
143 // "storage" which is entirely unprivileged, nor "extension" which has
144 // unprivileged components.
145 bool IsPrivilegedAPI(const std::string& name);
146
147 // Map from each API that hasn't been loaded yet to the schema which defines 122 // Map from each API that hasn't been loaded yet to the schema which defines
148 // it. Note that there may be multiple APIs per schema. 123 // it. Note that there may be multiple APIs per schema.
149 typedef std::map<std::string, base::StringPiece> UnloadedSchemaMap; 124 typedef std::map<std::string, int> UnloadedSchemaMap;
150 UnloadedSchemaMap unloaded_schemas_; 125 UnloadedSchemaMap unloaded_schemas_;
151 126
152 // Schemas for each namespace. 127 // Schemas for each namespace.
153 typedef std::map<std::string, linked_ptr<const DictionaryValue> > SchemaMap; 128 typedef std::map<std::string, linked_ptr<const DictionaryValue> > SchemaMap;
154 SchemaMap schemas_; 129 SchemaMap schemas_;
155 130
156 // APIs that are entirely unprivileged.
157 std::set<std::string> completely_unprivileged_apis_;
158
159 // APIs that are not entirely unprivileged, but have unprivileged components.
160 std::set<std::string> partially_unprivileged_apis_;
161
162 // FeatureProviders used for resolving dependencies. 131 // FeatureProviders used for resolving dependencies.
163 typedef std::map<std::string, FeatureProvider*> FeatureProviderMap; 132 typedef std::map<std::string, FeatureProvider*> FeatureProviderMap;
164 FeatureProviderMap dependency_providers_; 133 FeatureProviderMap dependency_providers_;
165 134
166 DISALLOW_COPY_AND_ASSIGN(ExtensionAPI); 135 DISALLOW_COPY_AND_ASSIGN(ExtensionAPI);
167 }; 136 };
168 137
169 } // extensions 138 } // namespace extensions
170 139
171 #endif // CHROME_COMMON_EXTENSIONS_API_EXTENSION_API_H_ 140 #endif // CHROME_COMMON_EXTENSIONS_API_EXTENSION_API_H_
OLDNEW
« no previous file with comments | « chrome/common/extensions/api/extension.json ('k') | chrome/common/extensions/api/extension_api.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698