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

Unified 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, 9 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 side-by-side diff with in-line comments
Download patch
« 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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/common/extensions/api/extension_api.h
diff --git a/chrome/common/extensions/api/extension_api.h b/chrome/common/extensions/api/extension_api.h
index 1ae78245b33d4118727d6eed8c057f95b54750e6..b2da19060ef0fce3f41d26c0574efb9c054667d9 100644
--- a/chrome/common/extensions/api/extension_api.h
+++ b/chrome/common/extensions/api/extension_api.h
@@ -17,6 +17,7 @@
#include "base/string_piece.h"
#include "base/values.h"
#include "chrome/common/extensions/feature.h"
+#include "chrome/common/extensions/feature_provider.h"
#include "chrome/common/extensions/url_pattern_set.h"
namespace base {
@@ -31,15 +32,50 @@ class ExtensionPermissionSet;
namespace extensions {
+class Feature;
+
// C++ Wrapper for the JSON API definitions in chrome/common/extensions/api/.
-class ExtensionAPI {
+//
+// WARNING: This class is accessed on multiple threads in the browser process
+// (see ExtensionFunctionDispatcher). No state should be modified after
+// construction.
+class ExtensionAPI : public FeatureProvider {
public:
- // Returns the single instance of this class.
- static ExtensionAPI* GetInstance();
-
- // Public for construction from unit tests. Use GetInstance() normally.
+ // Returns a single shared instance of this class. This is the typical use
+ // case in Chrome.
+ //
+ // TODO(aa): Make this const to enforce thread-safe usage.
+ static ExtensionAPI* GetSharedInstance();
+
+ // Creates a new instance configured the way ExtensionAPI typically is in
+ // Chrome. Use the default constructor to get a clean instance.
+ static ExtensionAPI* CreateWithDefaultConfiguration();
+
+ // Splits a name like "permission:bookmark" into ("permission", "bookmark").
+ // The first part refers to a type of feature, for example "manifest",
+ // "permission", or "api". The second part is the full name of the feature.
+ static void SplitDependencyName(const std::string& full_name,
+ std::string* feature_type,
+ std::string* feature_name);
+
+ // Creates a completely clean instance. Configure using RegisterSchema() and
+ // RegisterDependencyProvider before use.
ExtensionAPI();
- ~ExtensionAPI();
+ virtual ~ExtensionAPI();
+
+ void RegisterSchema(const std::string& api_name,
+ const base::StringPiece& source);
+
+ void RegisterDependencyProvider(const std::string& name,
+ FeatureProvider* provider);
+
+ // Returns true if the specified API is available. |api_full_name| can be
+ // either a namespace name (like "bookmarks") or a member name (like
+ // "bookmarks.create"). Returns true if the feature and all of its
+ // dependencies are available to the specified context.
+ bool IsAvailable(const std::string& api_full_name,
+ const Extension* extension,
+ Feature::Context context);
// Returns true if |name| is a privileged API path. Privileged paths can only
// be called from extension code which is running in its own designated
@@ -47,9 +83,9 @@ class ExtensionAPI {
// content scripts, or other low-privileged contexts.
bool IsPrivileged(const std::string& name);
- // Gets the schema for the extension API with namespace |api_name|.
+ // Gets the schema for the extension API with namespace |full_name|.
// Ownership remains with this object.
- const base::DictionaryValue* GetSchema(const std::string& api_name);
+ const base::DictionaryValue* GetSchema(const std::string& full_name);
// Gets the APIs available to |context| given an |extension| and |url|. The
// extension or URL may not be relevant to all contexts, and may be left
@@ -57,31 +93,52 @@ class ExtensionAPI {
scoped_ptr<std::set<std::string> > GetAPIsForContext(
Feature::Context context, const Extension* extension, const GURL& url);
+ // Gets a Feature object describing the API with the specified |full_name|.
+ // This can be either an API namespace (like history, or
+ // experimental.bookmarks), or it can be an individual function or event.
+ virtual scoped_ptr<Feature> GetFeature(const std::string& full_name) OVERRIDE;
+
+ // Splits a full name from the extension API into its API and child name
+ // parts. Some examples:
+ //
+ // "bookmarks.create" -> ("bookmarks", "create")
+ // "experimental.input.ui.cursorUp" -> ("experimental.input.ui", "cursorUp")
+ // "storage.sync.set" -> ("storage", "sync.get")
+ // "<unknown-api>.monkey" -> ("", "")
+ //
+ // The |child_name| parameter can be be NULL if you don't need that part.
+ std::string GetAPINameFromFullName(const std::string& full_name,
+ std::string* child_name);
+
+ void InitDefaultConfiguration();
+
+ // Loads the schemas registered with RegisterSchema().
+ void LoadAllSchemas();
+
private:
friend struct DefaultSingletonTraits<ExtensionAPI>;
// Loads a schema.
void LoadSchema(const base::StringPiece& schema);
- // Find an item in |list| with the specified property name and value, or NULL
- // if no such item exists.
- base::DictionaryValue* FindListItem(const base::ListValue* list,
- const std::string& property_name,
- const std::string& property_value);
-
// Returns true if the function or event under |namespace_node| with
// the specified |child_name| is privileged, or false otherwise. If the name
// is not found, defaults to privileged.
bool IsChildNamePrivileged(const base::DictionaryValue* namespace_node,
- const std::string& child_kind,
const std::string& child_name);
// Adds all APIs to |out| that |extension| has any permission (required or
// optional) to use.
+ // NOTE: This only works for non-feature-controlled APIs.
+ // TODO(aa): Remove this when all APIs are converted to the feature system.
void GetAllowedAPIs(const Extension* extension, std::set<std::string>* out);
+ // Gets a feature from any dependency provider.
+ scoped_ptr<Feature> GetFeatureDependency(const std::string& dependency_name);
+
// Adds dependent schemas to |out| as determined by the "dependencies"
// property.
+ // TODO(aa): Consider making public and adding tests.
void ResolveDependencies(std::set<std::string>* out);
// Adds any APIs listed in "dependencies" found in the schema for |api_name|
@@ -97,13 +154,10 @@ class ExtensionAPI {
void RemovePrivilegedAPIs(std::set<std::string>* apis);
// Adds an APIs that match |url| to |out|.
+ // NOTE: This only works for non-feature-controlled APIs.
+ // TODO(aa): Remove this when all APIs are converted to the feature system.
void GetAPIsMatchingURL(const GURL& url, std::set<std::string>* out);
- // Loads all remaining resources from |unloaded_schemas_|.
- void LoadAllSchemas();
-
- static ExtensionAPI* instance_;
-
// Map from each API that hasn't been loaded yet to the schema which defines
// it. Note that there may be multiple APIs per schema.
std::map<std::string, base::StringPiece> unloaded_schemas_;
@@ -121,6 +175,14 @@ class ExtensionAPI {
// APIs that have URL matching permissions.
std::map<std::string, URLPatternSet> url_matching_apis_;
+ typedef std::map<std::string, linked_ptr<Feature> > FeatureMap;
+ typedef std::map<std::string, linked_ptr<FeatureMap> > APIFeatureMap;
+ APIFeatureMap features_;
+
+ // FeatureProviders used for resolving dependencies.
+ typedef std::map<std::string, FeatureProvider*> FeatureProviderMap;
+ FeatureProviderMap dependency_providers_;
+
DISALLOW_COPY_AND_ASSIGN(ExtensionAPI);
};
« 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