| OLD | NEW |
| 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/singleton.h" | 16 #include "base/memory/singleton.h" |
| 16 #include "base/values.h" | 17 #include "base/values.h" |
| 18 #include "chrome/common/extensions/feature.h" |
| 19 #include "chrome/common/extensions/url_pattern_set.h" |
| 17 | 20 |
| 18 namespace base { | 21 namespace base { |
| 19 class DictionaryValue; | 22 class DictionaryValue; |
| 20 class ListValue; | 23 class ListValue; |
| 21 class Value; | 24 class Value; |
| 22 } | 25 } |
| 23 | 26 |
| 27 class GURL; |
| 24 class Extension; | 28 class Extension; |
| 25 class ExtensionPermissionSet; | 29 class ExtensionPermissionSet; |
| 26 | 30 |
| 27 namespace extensions { | 31 namespace extensions { |
| 28 | 32 |
| 29 // C++ Wrapper for the JSON API definitions in chrome/common/extensions/api/. | 33 // C++ Wrapper for the JSON API definitions in chrome/common/extensions/api/. |
| 30 class ExtensionAPI { | 34 class ExtensionAPI { |
| 31 public: | 35 public: |
| 32 // Filtering option for the GetSchemas functions. | |
| 33 enum GetSchemasFilter { | |
| 34 // Returns all schemas that an extension has permission for. | |
| 35 ALL, | |
| 36 | |
| 37 // Returns schemas for only APIs with unprivileged components (i.e. those | |
| 38 // where !IsWholeAPIPrivileged). | |
| 39 ONLY_UNPRIVILEGED | |
| 40 }; | |
| 41 | |
| 42 typedef std::map<std::string, linked_ptr<const DictionaryValue> > SchemaMap; | |
| 43 | |
| 44 // Returns the single instance of this class. | 36 // Returns the single instance of this class. |
| 45 static ExtensionAPI* GetInstance(); | 37 static ExtensionAPI* GetInstance(); |
| 46 | 38 |
| 47 // Returns true if |name| is a privileged API path. Privileged paths can only | 39 // Returns true if |name| is a privileged API path. Privileged paths can only |
| 48 // be called from extension code which is running in its own designated | 40 // be called from extension code which is running in its own designated |
| 49 // extension process. They cannot be called from extension code running in | 41 // extension process. They cannot be called from extension code running in |
| 50 // content scripts, or other low-privileged contexts. | 42 // content scripts, or other low-privileged contexts. |
| 51 bool IsPrivileged(const std::string& name) const; | 43 bool IsPrivileged(const std::string& name) const; |
| 52 | 44 |
| 53 // Returns whether *every* path in the API is privileged. This will be false | |
| 54 // for APIs such as "storage" which is entirely unprivileged, and "test" | |
| 55 // which has unprivileged components. | |
| 56 bool IsWholeAPIPrivileged(const std::string& api_name) const; | |
| 57 | |
| 58 // Gets a map of API name (aka namespace) to API schema. | |
| 59 const SchemaMap& schemas() { return schemas_; } | |
| 60 | |
| 61 // Gets the schema for the extension API with namespace |api_name|. | 45 // Gets the schema for the extension API with namespace |api_name|. |
| 62 // Ownership remains with this object. | 46 // Ownership remains with this object. |
| 63 const base::DictionaryValue* GetSchema(const std::string& api_name) const; | 47 const base::DictionaryValue* GetSchema(const std::string& api_name) const; |
| 64 | 48 |
| 65 // Gets the API schemas that are available to an Extension. | 49 // Gets the APIs available to |context| given an |extension| and |url|. The |
| 66 void GetSchemasForExtension(const Extension& extension, | 50 // extension or URL may not be relevant to all contexts, and may be left |
| 67 GetSchemasFilter filter, | 51 // NULL/empty. |
| 68 SchemaMap* out) const; | 52 scoped_ptr<std::set<std::string> > GetAPIsForContext( |
| 69 | 53 Feature::Context context, |
| 70 // Gets the schemas for the default set of APIs that are available to every | 54 const Extension* extension, |
| 71 // extension. | 55 const GURL& url) const; |
| 72 void GetDefaultSchemas(GetSchemasFilter filter, SchemaMap* out) const; | |
| 73 | 56 |
| 74 private: | 57 private: |
| 75 friend struct DefaultSingletonTraits<ExtensionAPI>; | 58 friend struct DefaultSingletonTraits<ExtensionAPI>; |
| 76 | 59 |
| 77 ExtensionAPI(); | 60 ExtensionAPI(); |
| 78 ~ExtensionAPI(); | 61 ~ExtensionAPI(); |
| 79 | 62 |
| 80 // Loads a schema from a resource. | 63 // Loads a schema from a resource. |
| 81 void LoadSchemaFromResource(int resource_id); | 64 void LoadSchemaFromResource(int resource_id); |
| 82 | 65 |
| 83 // Find an item in |list| with the specified property name and value, or NULL | 66 // Find an item in |list| with the specified property name and value, or NULL |
| 84 // if no such item exists. | 67 // if no such item exists. |
| 85 base::DictionaryValue* FindListItem(const base::ListValue* list, | 68 base::DictionaryValue* FindListItem(const base::ListValue* list, |
| 86 const std::string& property_name, | 69 const std::string& property_name, |
| 87 const std::string& property_value) const; | 70 const std::string& property_value) const; |
| 88 | 71 |
| 89 // Returns true if the function or event under |namespace_node| with | 72 // Returns true if the function or event under |namespace_node| with |
| 90 // the specified |child_name| is privileged, or false otherwise. If the name | 73 // the specified |child_name| is privileged, or false otherwise. If the name |
| 91 // is not found, defaults to privileged. | 74 // is not found, defaults to privileged. |
| 92 bool IsChildNamePrivileged(const base::DictionaryValue* namespace_node, | 75 bool IsChildNamePrivileged(const base::DictionaryValue* namespace_node, |
| 93 const std::string& child_kind, | 76 const std::string& child_kind, |
| 94 const std::string& child_name) const; | 77 const std::string& child_name) const; |
| 95 | 78 |
| 96 // Gets the schemas for the APIs that are allowed by a permission set. | 79 // Adds all APIs to |out| that |extension| has any permission (required or |
| 97 void GetSchemasForPermissions(const ExtensionPermissionSet& permissions, | 80 // optional) to use. |
| 98 GetSchemasFilter filter, | 81 void GetAllowedAPIs( |
| 99 SchemaMap* out) const; | 82 const Extension* extension, std::set<std::string>* out) const; |
| 100 | 83 |
| 101 // Adds dependent schemas to |out| as determined by the "dependencies" | 84 // Adds dependent schemas to |out| as determined by the "dependencies" |
| 102 // property. | 85 // property. |
| 103 void ResolveDependencies(SchemaMap* out) const; | 86 void ResolveDependencies(std::set<std::string>* out) const; |
| 87 |
| 88 // Adds any APIs listed in "dependencies" found in the schema for |api_name| |
| 89 // but not in |excluding| to |out|. |
| 90 void GetMissingDependencies( |
| 91 const std::string& api_name, |
| 92 const std::set<std::string>& excluding, |
| 93 std::set<std::string>* out) const; |
| 94 |
| 95 // Removes all APIs from |apis| which are *entirely* privileged. This won't |
| 96 // include APIs such as "storage" which is entirely unprivileged, nor |
| 97 // "extension" which has unprivileged components. |
| 98 void RemovePrivilegedAPIs(std::set<std::string>* apis) const; |
| 99 |
| 100 // Adds an APIs that match |url| to |out|. |
| 101 void GetAPIsMatchingURL(const GURL& url, std::set<std::string>* out) const; |
| 104 | 102 |
| 105 static ExtensionAPI* instance_; | 103 static ExtensionAPI* instance_; |
| 106 | 104 |
| 107 // Schemas for each namespace. | 105 // Schemas for each namespace. |
| 106 typedef std::map<std::string, linked_ptr<const DictionaryValue> > SchemaMap; |
| 108 SchemaMap schemas_; | 107 SchemaMap schemas_; |
| 109 | 108 |
| 110 // APIs that are entirely unprivileged. | 109 // APIs that are entirely unprivileged. |
| 111 std::set<std::string> completely_unprivileged_apis_; | 110 std::set<std::string> completely_unprivileged_apis_; |
| 112 | 111 |
| 113 // APIs that are not entirely unprivileged, but have unprivileged components. | 112 // APIs that are not entirely unprivileged, but have unprivileged components. |
| 114 std::set<std::string> partially_unprivileged_apis_; | 113 std::set<std::string> partially_unprivileged_apis_; |
| 115 | 114 |
| 115 // APIs that have URL matching permissions. |
| 116 std::map<std::string, URLPatternSet> url_matching_apis_; |
| 117 |
| 116 DISALLOW_COPY_AND_ASSIGN(ExtensionAPI); | 118 DISALLOW_COPY_AND_ASSIGN(ExtensionAPI); |
| 117 }; | 119 }; |
| 118 | 120 |
| 119 } // extensions | 121 } // extensions |
| 120 | 122 |
| 121 #endif // CHROME_COMMON_EXTENSIONS_API_EXTENSION_API_H_ | 123 #endif // CHROME_COMMON_EXTENSIONS_API_EXTENSION_API_H_ |
| OLD | NEW |