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