1 // Copyright 2014 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 #include "extensions/test/test_extensions_client.h"
7 #include "base/stl_util.h"
8 #include "extensions/common/api/generated_schemas.h"
9 #include "extensions/common/common_manifest_handlers.h"
10 #include "extensions/common/extension_urls.h"
11 #include "extensions/common/features/api_feature.h"
12 #include "extensions/common/features/base_feature_provider.h"
13 #include "extensions/common/features/behavior_feature.h"
14 #include "extensions/common/features/feature_provider.h"
15 #include "extensions/common/features/json_feature_provider_source.h"
16 #include "extensions/common/features/manifest_feature.h"
17 #include "extensions/common/features/permission_feature.h"
18 #include "extensions/common/manifest_handler.h"
19 #include "extensions/common/permissions/extensions_api_permissions.h"
20 #include "extensions/common/permissions/permissions_info.h"
21 #include "extensions/common/url_pattern_set.h"
22 #include "extensions/test/test_permission_message_provider.h"
23 #include "grit/extensions_resources.h"
25 namespace extensions
{
29 template <class FeatureClass
>
30 SimpleFeature
* CreateFeature() {
31 return new FeatureClass
;
36 TestExtensionsClient::TestExtensionsClient() {
39 TestExtensionsClient::~TestExtensionsClient() {
42 void TestExtensionsClient::AddBrowserImagePathsFilter(
43 BrowserImagePathsFilter
* filter
) {
44 browser_image_filters_
.insert(filter
);
47 void TestExtensionsClient::RemoveBrowserImagePathsFilter(
48 BrowserImagePathsFilter
* filter
) {
49 browser_image_filters_
.erase(filter
);
52 void TestExtensionsClient::Initialize() {
53 // Registration could already be finalized in unit tests, where the utility
54 // thread runs in-process.
55 if (!ManifestHandler::IsRegistrationFinalized()) {
56 RegisterCommonManifestHandlers();
57 ManifestHandler::FinalizeRegistration();
60 // Allow the core API permissions.
61 static ExtensionsAPIPermissions extensions_api_permissions
;
62 PermissionsInfo::GetInstance()->AddProvider(extensions_api_permissions
);
65 const PermissionMessageProvider
&
66 TestExtensionsClient::GetPermissionMessageProvider() const {
67 static TestPermissionMessageProvider provider
;
71 const std::string
TestExtensionsClient::GetProductName() {
72 return "extensions_test";
75 scoped_ptr
<FeatureProvider
> TestExtensionsClient::CreateFeatureProvider(
76 const std::string
& name
) const {
77 scoped_ptr
<FeatureProvider
> provider
;
78 scoped_ptr
<JSONFeatureProviderSource
> source(
79 CreateFeatureProviderSource(name
));
81 provider
.reset(new BaseFeatureProvider(source
->dictionary(),
82 CreateFeature
<APIFeature
>));
83 } else if (name
== "manifest") {
84 provider
.reset(new BaseFeatureProvider(source
->dictionary(),
85 CreateFeature
<ManifestFeature
>));
86 } else if (name
== "permission") {
87 provider
.reset(new BaseFeatureProvider(source
->dictionary(),
88 CreateFeature
<PermissionFeature
>));
89 } else if (name
== "behavior") {
90 provider
.reset(new BaseFeatureProvider(source
->dictionary(),
91 CreateFeature
<BehaviorFeature
>));
95 return provider
.Pass();
98 scoped_ptr
<JSONFeatureProviderSource
>
99 TestExtensionsClient::CreateFeatureProviderSource(
100 const std::string
& name
) const {
101 scoped_ptr
<JSONFeatureProviderSource
> source(
102 new JSONFeatureProviderSource(name
));
104 source
->LoadJSON(IDR_EXTENSION_API_FEATURES
);
105 } else if (name
== "manifest") {
106 source
->LoadJSON(IDR_EXTENSION_MANIFEST_FEATURES
);
107 } else if (name
== "permission") {
108 source
->LoadJSON(IDR_EXTENSION_PERMISSION_FEATURES
);
109 } else if (name
== "behavior") {
110 source
->LoadJSON(IDR_EXTENSION_BEHAVIOR_FEATURES
);
115 return source
.Pass();
118 void TestExtensionsClient::FilterHostPermissions(
119 const URLPatternSet
& hosts
,
120 URLPatternSet
* new_hosts
,
121 std::set
<PermissionMessage
>* messages
) const {
124 void TestExtensionsClient::FilterHostPermissions(
125 const URLPatternSet
& hosts
,
126 URLPatternSet
* new_hosts
,
127 PermissionIDSet
* permissions
) const {
130 void TestExtensionsClient::SetScriptingWhitelist(
131 const ExtensionsClient::ScriptingWhitelist
& whitelist
) {
132 scripting_whitelist_
= whitelist
;
135 const ExtensionsClient::ScriptingWhitelist
&
136 TestExtensionsClient::GetScriptingWhitelist() const {
137 return scripting_whitelist_
;
140 URLPatternSet
TestExtensionsClient::GetPermittedChromeSchemeHosts(
141 const Extension
* extension
,
142 const APIPermissionSet
& api_permissions
) const {
147 bool TestExtensionsClient::IsScriptableURL(const GURL
& url
,
148 std::string
* error
) const {
152 bool TestExtensionsClient::IsAPISchemaGenerated(
153 const std::string
& name
) const {
154 return core_api::GeneratedSchemas::IsGenerated(name
);
157 base::StringPiece
TestExtensionsClient::GetAPISchema(
158 const std::string
& name
) const {
159 return core_api::GeneratedSchemas::Get(name
);
162 void TestExtensionsClient::RegisterAPISchemaResources(ExtensionAPI
* api
) const {
165 bool TestExtensionsClient::ShouldSuppressFatalErrors() const {
169 void TestExtensionsClient::RecordDidSuppressFatalError() {
172 std::string
TestExtensionsClient::GetWebstoreBaseURL() const {
173 return extension_urls::kChromeWebstoreBaseURL
;
176 std::string
TestExtensionsClient::GetWebstoreUpdateURL() const {
177 return extension_urls::kChromeWebstoreUpdateURL
;
180 bool TestExtensionsClient::IsBlacklistUpdateURL(const GURL
& url
) const {
184 std::set
<base::FilePath
> TestExtensionsClient::GetBrowserImagePaths(
185 const Extension
* extension
) {
186 std::set
<base::FilePath
> result
=
187 ExtensionsClient::GetBrowserImagePaths(extension
);
188 for (auto filter
: browser_image_filters_
)
189 filter
->Filter(extension
, &result
);
193 } // namespace extensions