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

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: 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 class ExtensionAPI : public FeatureProvider {
36 public: 39 public:
37 // Returns the single instance of this class. 40 // Returns a single shared instance of this class. This is the typical use
38 static ExtensionAPI* GetInstance(); 41 // case in Chrome.
42 static ExtensionAPI* GetSharedInstance();
39 43
40 // Public for construction from unit tests. Use GetInstance() normally. 44 // Creates a new instance configured the way ExtensionAPI typically is in
45 // Chrome. Use the default constructor to get a clean instance.
46 static ExtensionAPI* CreateWithDefaultConfiguration();
47
48 // Splits a name like "permission:bookmark" into ("permission", "bookmark").
49 // The first part refers to a type of feature, for example "manifest",
50 // "permission", or "api". The second part is the full name of the feature.
51 static void SplitDependencyName(const std::string& full_name,
52 std::string* feature_type,
53 std::string* feature_name);
54
55 // Creates a completely clean instance. Configure using RegisterSchema() and
56 // RegisterDependencyProvider before use.
41 ExtensionAPI(); 57 ExtensionAPI();
42 ~ExtensionAPI(); 58 ~ExtensionAPI();
43 59
60 void RegisterSchema(const std::string& api_name,
61 const base::StringPiece& source);
62
63 void RegisterDependencyProvider(const std::string& name,
64 FeatureProvider* provider);
65
66 // Returns true if the specified API is available. |api_full_name| can be
67 // either a namespace name (like "bookmarks") or a member name (like
68 // "bookmarks.create"). Returns true if the feature and all of its
69 // dependencies are available to the specified context.
70 bool IsAvailable(const std::string& api_full_name,
71 const Extension* extension,
72 Feature::Context context);
73
44 // Returns true if |name| is a privileged API path. Privileged paths can only 74 // 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 75 // be called from extension code which is running in its own designated
46 // extension process. They cannot be called from extension code running in 76 // extension process. They cannot be called from extension code running in
47 // content scripts, or other low-privileged contexts. 77 // content scripts, or other low-privileged contexts.
48 bool IsPrivileged(const std::string& name); 78 bool IsPrivileged(const std::string& name);
49 79
50 // Gets the schema for the extension API with namespace |api_name|. 80 // Gets the schema for the extension API with namespace |full_name|.
51 // Ownership remains with this object. 81 // Ownership remains with this object.
52 const base::DictionaryValue* GetSchema(const std::string& api_name); 82 const base::DictionaryValue* GetSchema(const std::string& full_name);
53 83
54 // Gets the APIs available to |context| given an |extension| and |url|. The 84 // 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 85 // extension or URL may not be relevant to all contexts, and may be left
56 // NULL/empty. 86 // NULL/empty.
57 scoped_ptr<std::set<std::string> > GetAPIsForContext( 87 scoped_ptr<std::set<std::string> > GetAPIsForContext(
58 Feature::Context context, const Extension* extension, const GURL& url); 88 Feature::Context context, const Extension* extension, const GURL& url);
59 89
90 // Gets a Feature object describing the API with the specified full_name. This
koz (OOO until 15th September) 2012/04/02 06:39:48 full_name -> |full_name|
Aaron Boodman 2012/04/02 08:21:54 Done.
91 // can be either an API namespace (like history, or experimental.bookmarks),
92 // or it can be an individual function or event.
93 virtual scoped_ptr<Feature> GetFeature(const std::string& full_name) OVERRIDE;
94
95 // Splits a full name from the extension API into its API and child name
96 // parts. Some examples:
97 //
98 // "bookmarks.create" -> ("bookmarks", "create")
99 // "experimental.input.ui.cursorUp" -> ("experimental.input.ui", "cursorUp")
100 // "storage.sync.set" -> ("storage", "sync.get")
101 // "<unknown-api>.monkey" -> ("", "")
102 //
103 // The |child_name| parameter can be be NULL if you don't need that part.
104 std::string GetAPINameForFullName(const std::string& full_name,
105 std::string* child_name);
106
107 void InitDefaultConfiguration();
108
60 private: 109 private:
61 friend struct DefaultSingletonTraits<ExtensionAPI>; 110 friend struct DefaultSingletonTraits<ExtensionAPI>;
62 111
63 // Loads a schema. 112 // Loads a schema.
64 void LoadSchema(const base::StringPiece& schema); 113 void LoadSchema(const base::StringPiece& schema);
65 114
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 115 // Returns true if the function or event under |namespace_node| with
73 // the specified |child_name| is privileged, or false otherwise. If the name 116 // the specified |child_name| is privileged, or false otherwise. If the name
74 // is not found, defaults to privileged. 117 // is not found, defaults to privileged.
75 bool IsChildNamePrivileged(const base::DictionaryValue* namespace_node, 118 bool IsChildNamePrivileged(const base::DictionaryValue* namespace_node,
76 const std::string& child_kind,
77 const std::string& child_name); 119 const std::string& child_name);
78 120
79 // Adds all APIs to |out| that |extension| has any permission (required or 121 // Adds all APIs to |out| that |extension| has any permission (required or
80 // optional) to use. 122 // optional) to use.
81 void GetAllowedAPIs(const Extension* extension, std::set<std::string>* out); 123 void GetAllowedAPIs(const Extension* extension, std::set<std::string>* out);
82 124
83 // Adds dependent schemas to |out| as determined by the "dependencies" 125 // Adds dependent schemas to |out| as determined by the "dependencies"
84 // property. 126 // property.
127 // TODO(aa): Consider making public and adding tests.
85 void ResolveDependencies(std::set<std::string>* out); 128 void ResolveDependencies(std::set<std::string>* out);
86 129
87 // Adds any APIs listed in "dependencies" found in the schema for |api_name| 130 // Adds any APIs listed in "dependencies" found in the schema for |api_name|
88 // but not in |excluding| to |out|. 131 // but not in |excluding| to |out|.
89 void GetMissingDependencies( 132 void GetMissingDependencies(
90 const std::string& api_name, 133 const std::string& api_name,
91 const std::set<std::string>& excluding, 134 const std::set<std::string>& excluding,
92 std::set<std::string>* out); 135 std::set<std::string>* out);
93 136
94 // Removes all APIs from |apis| which are *entirely* privileged. This won't 137 // Removes all APIs from |apis| which are *entirely* privileged. This won't
95 // include APIs such as "storage" which is entirely unprivileged, nor 138 // include APIs such as "storage" which is entirely unprivileged, nor
96 // "extension" which has unprivileged components. 139 // "extension" which has unprivileged components.
97 void RemovePrivilegedAPIs(std::set<std::string>* apis); 140 void RemovePrivilegedAPIs(std::set<std::string>* apis);
98 141
99 // Adds an APIs that match |url| to |out|. 142 // Adds an APIs that match |url| to |out|.
100 void GetAPIsMatchingURL(const GURL& url, std::set<std::string>* out); 143 void GetAPIsMatchingURL(const GURL& url, std::set<std::string>* out);
101 144
102 // Loads all remaining resources from |unloaded_schemas_|. 145 // Loads all remaining resources from |unloaded_schemas_|.
103 void LoadAllSchemas(); 146 void LoadAllSchemas();
104 147
105 static ExtensionAPI* instance_;
106
107 // Map from each API that hasn't been loaded yet to the schema which defines 148 // 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. 149 // it. Note that there may be multiple APIs per schema.
109 std::map<std::string, base::StringPiece> unloaded_schemas_; 150 std::map<std::string, base::StringPiece> unloaded_schemas_;
110 151
111 // Schemas for each namespace. 152 // Schemas for each namespace.
112 typedef std::map<std::string, linked_ptr<const DictionaryValue> > SchemaMap; 153 typedef std::map<std::string, linked_ptr<const DictionaryValue> > SchemaMap;
113 SchemaMap schemas_; 154 SchemaMap schemas_;
114 155
115 // APIs that are entirely unprivileged. 156 // APIs that are entirely unprivileged.
116 std::set<std::string> completely_unprivileged_apis_; 157 std::set<std::string> completely_unprivileged_apis_;
117 158
118 // APIs that are not entirely unprivileged, but have unprivileged components. 159 // APIs that are not entirely unprivileged, but have unprivileged components.
119 std::set<std::string> partially_unprivileged_apis_; 160 std::set<std::string> partially_unprivileged_apis_;
120 161
121 // APIs that have URL matching permissions. 162 // APIs that have URL matching permissions.
122 std::map<std::string, URLPatternSet> url_matching_apis_; 163 std::map<std::string, URLPatternSet> url_matching_apis_;
123 164
165 typedef std::map<std::string, linked_ptr<Feature> > FeatureMap;
166 typedef std::map<std::string, linked_ptr<FeatureMap> > APIFeatureMap;
167 APIFeatureMap features_;
168
169 // FeatureProviders used for resolving dependencies.
170 typedef std::map<std::string, FeatureProvider*> FeatureProviderMap;
171 FeatureProviderMap dependency_providers_;
172
124 DISALLOW_COPY_AND_ASSIGN(ExtensionAPI); 173 DISALLOW_COPY_AND_ASSIGN(ExtensionAPI);
125 }; 174 };
126 175
127 } // extensions 176 } // extensions
128 177
129 #endif // CHROME_COMMON_EXTENSIONS_API_EXTENSION_API_H_ 178 #endif // CHROME_COMMON_EXTENSIONS_API_EXTENSION_API_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698