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
3 // found in the LICENSE file.
5 #ifndef CHROME_COMMON_EXTENSIONS_API_EXTENSION_API_H_
6 #define CHROME_COMMON_EXTENSIONS_API_EXTENSION_API_H_
12 #include "base/basictypes.h"
13 #include "base/memory/linked_ptr.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/singleton.h"
16 #include "base/string_piece.h"
17 #include "base/values.h"
18 #include "chrome/common/extensions/features/feature.h"
19 #include "chrome/common/extensions/features/feature_provider.h"
20 #include "chrome/common/extensions/url_pattern_set.h"
23 class DictionaryValue
;
30 namespace extensions
{
35 // C++ Wrapper for the JSON API definitions in chrome/common/extensions/api/.
37 // WARNING: This class is accessed on multiple threads in the browser process
38 // (see ExtensionFunctionDispatcher). No state should be modified after
40 class ExtensionAPI
: public FeatureProvider
{
42 // Returns a single shared instance of this class. This is the typical use
45 // TODO(aa): Make this const to enforce thread-safe usage.
46 static ExtensionAPI
* GetSharedInstance();
48 // Creates a new instance configured the way ExtensionAPI typically is in
49 // Chrome. Use the default constructor to get a clean instance.
50 static ExtensionAPI
* CreateWithDefaultConfiguration();
52 // Splits a name like "permission:bookmark" into ("permission", "bookmark").
53 // The first part refers to a type of feature, for example "manifest",
54 // "permission", or "api". The second part is the full name of the feature.
55 static void SplitDependencyName(const std::string
& full_name
,
56 std::string
* feature_type
,
57 std::string
* feature_name
);
59 // Creates a completely clean instance. Configure using RegisterSchema() and
60 // RegisterDependencyProvider before use.
62 virtual ~ExtensionAPI();
64 void RegisterSchema(const std::string
& api_name
,
65 const base::StringPiece
& source
);
67 void RegisterDependencyProvider(const std::string
& name
,
68 FeatureProvider
* provider
);
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
72 // "bookmarks.create"). Returns true if the feature and all of its
73 // dependencies are available to the specified context.
74 bool IsAvailable(const std::string
& api_full_name
,
75 const Extension
* extension
,
76 Feature::Context context
);
78 // 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 // extension process. They cannot be called from extension code running in
81 // content scripts, or other low-privileged contexts.
82 bool IsPrivileged(const std::string
& name
);
84 // Gets the schema for the extension API with namespace |full_name|.
85 // Ownership remains with this object.
86 const base::DictionaryValue
* GetSchema(const std::string
& full_name
);
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
91 scoped_ptr
<std::set
<std::string
> > GetAPIsForContext(
92 Feature::Context context
, const Extension
* extension
, const GURL
& url
);
94 // Gets a Feature object describing the API with the specified |full_name|.
95 // This can be either an API namespace (like history, or
96 // experimental.bookmarks), or it can be an individual function or event.
97 virtual Feature
* GetFeature(const std::string
& full_name
) OVERRIDE
;
99 // Splits a full name from the extension API into its API and child name
100 // parts. Some examples:
102 // "bookmarks.create" -> ("bookmarks", "create")
103 // "experimental.input.ui.cursorUp" -> ("experimental.input.ui", "cursorUp")
104 // "storage.sync.set" -> ("storage", "sync.get")
105 // "<unknown-api>.monkey" -> ("", "")
107 // The |child_name| parameter can be be NULL if you don't need that part.
108 std::string
GetAPINameFromFullName(const std::string
& full_name
,
109 std::string
* child_name
);
111 void InitDefaultConfiguration();
113 // Loads the schemas registered with RegisterSchema().
114 void LoadAllSchemas();
117 friend struct DefaultSingletonTraits
<ExtensionAPI
>;
120 void LoadSchema(const std::string
& name
, const base::StringPiece
& schema
);
122 // Returns true if the function or event under |namespace_node| with
123 // the specified |child_name| is privileged, or false otherwise. If the name
124 // is not found, defaults to privileged.
125 bool IsChildNamePrivileged(const base::DictionaryValue
* namespace_node
,
126 const std::string
& child_name
);
128 // Adds all APIs to |out| that |extension| has any permission (required or
130 // NOTE: This only works for non-feature-controlled APIs.
131 // TODO(aa): Remove this when all APIs are converted to the feature system.
132 void GetAllowedAPIs(const Extension
* extension
, std::set
<std::string
>* out
);
134 // Gets a feature from any dependency provider.
135 Feature
* GetFeatureDependency(const std::string
& dependency_name
);
137 // Adds dependent schemas to |out| as determined by the "dependencies"
139 // TODO(aa): Consider making public and adding tests.
140 void ResolveDependencies(std::set
<std::string
>* out
);
142 // Adds any APIs listed in "dependencies" found in the schema for |api_name|
143 // but not in |excluding| to |out|.
144 void GetMissingDependencies(
145 const std::string
& api_name
,
146 const std::set
<std::string
>& excluding
,
147 std::set
<std::string
>* out
);
149 // Removes all APIs from |apis| which are *entirely* privileged. This won't
150 // include APIs such as "storage" which is entirely unprivileged, nor
151 // "extension" which has unprivileged components.
152 void RemovePrivilegedAPIs(std::set
<std::string
>* apis
);
154 // Adds an APIs that match |url| to |out|.
155 // NOTE: This only works for non-feature-controlled APIs.
156 // TODO(aa): Remove this when all APIs are converted to the feature system.
157 void GetAPIsMatchingURL(const GURL
& url
, std::set
<std::string
>* out
);
159 // Map from each API that hasn't been loaded yet to the schema which defines
160 // it. Note that there may be multiple APIs per schema.
161 std::map
<std::string
, base::StringPiece
> unloaded_schemas_
;
163 // Schemas for each namespace.
164 typedef std::map
<std::string
, linked_ptr
<const DictionaryValue
> > SchemaMap
;
167 // APIs that are entirely unprivileged.
168 std::set
<std::string
> completely_unprivileged_apis_
;
170 // APIs that are not entirely unprivileged, but have unprivileged components.
171 std::set
<std::string
> partially_unprivileged_apis_
;
173 // APIs that have URL matching permissions.
174 std::map
<std::string
, URLPatternSet
> url_matching_apis_
;
176 typedef std::map
<std::string
, linked_ptr
<Feature
> > FeatureMap
;
177 typedef std::map
<std::string
, linked_ptr
<FeatureMap
> > APIFeatureMap
;
178 APIFeatureMap features_
;
180 // FeatureProviders used for resolving dependencies.
181 typedef std::map
<std::string
, FeatureProvider
*> FeatureProviderMap
;
182 FeatureProviderMap dependency_providers_
;
184 DISALLOW_COPY_AND_ASSIGN(ExtensionAPI
);
189 #endif // CHROME_COMMON_EXTENSIONS_API_EXTENSION_API_H_