| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #ifndef CHROME_COMMON_EXTENSIONS_API_EXTENSION_API_H_ | |
| 6 #define CHROME_COMMON_EXTENSIONS_API_EXTENSION_API_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/gtest_prod_util.h" | |
| 13 #include "base/memory/linked_ptr.h" | |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #include "base/memory/singleton.h" | |
| 16 #include "base/strings/string_piece.h" | |
| 17 #include "base/values.h" | |
| 18 #include "extensions/common/features/feature.h" | |
| 19 #include "extensions/common/features/feature_provider.h" | |
| 20 #include "extensions/common/url_pattern_set.h" | |
| 21 | |
| 22 namespace base { | |
| 23 class DictionaryValue; | |
| 24 class Value; | |
| 25 } | |
| 26 | |
| 27 class GURL; | |
| 28 | |
| 29 namespace extensions { | |
| 30 | |
| 31 class Extension; | |
| 32 class Feature; | |
| 33 | |
| 34 // C++ Wrapper for the JSON API definitions in chrome/common/extensions/api/. | |
| 35 // | |
| 36 // WARNING: This class is accessed on multiple threads in the browser process | |
| 37 // (see ExtensionFunctionDispatcher). No state should be modified after | |
| 38 // construction. | |
| 39 class ExtensionAPI { | |
| 40 public: | |
| 41 // Returns a single shared instance of this class. This is the typical use | |
| 42 // case in Chrome. | |
| 43 // | |
| 44 // TODO(aa): Make this const to enforce thread-safe usage. | |
| 45 static ExtensionAPI* GetSharedInstance(); | |
| 46 | |
| 47 // Creates a new instance configured the way ExtensionAPI typically is in | |
| 48 // Chrome. Use the default constructor to get a clean instance. | |
| 49 static ExtensionAPI* CreateWithDefaultConfiguration(); | |
| 50 | |
| 51 // Splits a name like "permission:bookmark" into ("permission", "bookmark"). | |
| 52 // The first part refers to a type of feature, for example "manifest", | |
| 53 // "permission", or "api". The second part is the full name of the feature. | |
| 54 static void SplitDependencyName(const std::string& full_name, | |
| 55 std::string* feature_type, | |
| 56 std::string* feature_name); | |
| 57 | |
| 58 // Creates a completely clean instance. Configure using RegisterSchema() and | |
| 59 // RegisterDependencyProvider before use. | |
| 60 ExtensionAPI(); | |
| 61 virtual ~ExtensionAPI(); | |
| 62 | |
| 63 void RegisterSchemaResource(const std::string& api_name, int resource_id); | |
| 64 | |
| 65 void RegisterDependencyProvider(const std::string& name, | |
| 66 FeatureProvider* provider); | |
| 67 | |
| 68 // Returns true if the specified API is available. |api_full_name| can be | |
| 69 // either a namespace name (like "bookmarks") or a member name (like | |
| 70 // "bookmarks.create"). Returns true if the feature and all of its | |
| 71 // dependencies are available to the specified context. | |
| 72 Feature::Availability IsAvailable(const std::string& api_full_name, | |
| 73 const Extension* extension, | |
| 74 Feature::Context context, | |
| 75 const GURL& url); | |
| 76 | |
| 77 // Determines whether an API, or any parts of that API, are available in | |
| 78 // |context|. | |
| 79 bool IsAnyFeatureAvailableToContext(const std::string& api_name, | |
| 80 const Extension* extension, | |
| 81 Feature::Context context, | |
| 82 const GURL& url); | |
| 83 | |
| 84 // Returns true if |name| is a privileged API path. Privileged paths can only | |
| 85 // be called from extension code which is running in its own designated | |
| 86 // extension process. They cannot be called from extension code running in | |
| 87 // content scripts, or other low-privileged contexts. | |
| 88 bool IsPrivileged(const std::string& name); | |
| 89 | |
| 90 // Gets the schema for the extension API with namespace |full_name|. | |
| 91 // Ownership remains with this object. | |
| 92 const base::DictionaryValue* GetSchema(const std::string& full_name); | |
| 93 | |
| 94 // Splits a full name from the extension API into its API and child name | |
| 95 // parts. Some examples: | |
| 96 // | |
| 97 // "bookmarks.create" -> ("bookmarks", "create") | |
| 98 // "experimental.input.ui.cursorUp" -> ("experimental.input.ui", "cursorUp") | |
| 99 // "storage.sync.set" -> ("storage", "sync.get") | |
| 100 // "<unknown-api>.monkey" -> ("", "") | |
| 101 // | |
| 102 // The |child_name| parameter can be be NULL if you don't need that part. | |
| 103 std::string GetAPINameFromFullName(const std::string& full_name, | |
| 104 std::string* child_name); | |
| 105 | |
| 106 private: | |
| 107 FRIEND_TEST_ALL_PREFIXES(ExtensionAPITest, DefaultConfigurationFeatures); | |
| 108 FRIEND_TEST_ALL_PREFIXES(ExtensionAPITest, TypesHaveNamespace); | |
| 109 friend struct DefaultSingletonTraits<ExtensionAPI>; | |
| 110 | |
| 111 void InitDefaultConfiguration(); | |
| 112 | |
| 113 bool default_configuration_initialized_; | |
| 114 | |
| 115 // Gets a feature from any dependency provider registered with ExtensionAPI. | |
| 116 // Returns NULL if the feature could not be found. | |
| 117 Feature* GetFeatureDependency(const std::string& dependency_name); | |
| 118 | |
| 119 // Loads a schema. | |
| 120 void LoadSchema(const std::string& name, const base::StringPiece& schema); | |
| 121 | |
| 122 // Map from each API that hasn't been loaded yet to the schema which defines | |
| 123 // it. Note that there may be multiple APIs per schema. | |
| 124 typedef std::map<std::string, int> UnloadedSchemaMap; | |
| 125 UnloadedSchemaMap unloaded_schemas_; | |
| 126 | |
| 127 // Schemas for each namespace. | |
| 128 typedef std::map<std::string, linked_ptr<const base::DictionaryValue> > | |
| 129 SchemaMap; | |
| 130 SchemaMap schemas_; | |
| 131 | |
| 132 // FeatureProviders used for resolving dependencies. | |
| 133 typedef std::map<std::string, FeatureProvider*> FeatureProviderMap; | |
| 134 FeatureProviderMap dependency_providers_; | |
| 135 | |
| 136 DISALLOW_COPY_AND_ASSIGN(ExtensionAPI); | |
| 137 }; | |
| 138 | |
| 139 } // namespace extensions | |
| 140 | |
| 141 #endif // CHROME_COMMON_EXTENSIONS_API_EXTENSION_API_H_ | |
| OLD | NEW |