1 // Copyright 2013 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 "chrome/common/extensions/chrome_extensions_client.h"
7 #include "apps/common/api/generated_schemas.h"
8 #include "base/command_line.h"
9 #include "chrome/common/chrome_version_info.h"
10 #include "chrome/common/extensions/api/generated_schemas.h"
11 #include "chrome/common/extensions/chrome_manifest_handlers.h"
12 #include "chrome/common/extensions/extension_constants.h"
13 #include "chrome/common/extensions/features/chrome_channel_feature_filter.h"
14 #include "chrome/common/extensions/features/feature_channel.h"
15 #include "chrome/common/url_constants.h"
16 #include "content/public/common/url_constants.h"
17 #include "extensions/common/api/generated_schemas.h"
18 #include "extensions/common/common_manifest_handlers.h"
19 #include "extensions/common/extension.h"
20 #include "extensions/common/extension_api.h"
21 #include "extensions/common/features/api_feature.h"
22 #include "extensions/common/features/base_feature_provider.h"
23 #include "extensions/common/features/feature_provider.h"
24 #include "extensions/common/features/json_feature_provider_source.h"
25 #include "extensions/common/features/manifest_feature.h"
26 #include "extensions/common/features/permission_feature.h"
27 #include "extensions/common/features/simple_feature.h"
28 #include "extensions/common/manifest_constants.h"
29 #include "extensions/common/manifest_handler.h"
30 #include "extensions/common/permissions/api_permission_set.h"
31 #include "extensions/common/permissions/permission_message.h"
32 #include "extensions/common/permissions/permissions_info.h"
33 #include "extensions/common/switches.h"
34 #include "extensions/common/url_pattern.h"
35 #include "extensions/common/url_pattern_set.h"
36 #include "grit/common_resources.h"
37 #if defined(ENABLE_EXTENSIONS)
38 #include "grit/extensions_api_resources.h"
40 #include "grit/extensions_resources.h"
41 #include "grit/generated_resources.h"
42 #include "ui/base/l10n/l10n_util.h"
45 namespace extensions
{
49 const char kThumbsWhiteListedExtension
[] = "khopmbdjffemhegeeobelklnbglcdgfh";
51 template <class FeatureClass
>
52 SimpleFeature
* CreateFeature() {
53 SimpleFeature
* feature
= new FeatureClass
;
55 scoped_ptr
<SimpleFeatureFilter
>(new ChromeChannelFeatureFilter(feature
)));
61 static base::LazyInstance
<ChromeExtensionsClient
> g_client
=
62 LAZY_INSTANCE_INITIALIZER
;
64 ChromeExtensionsClient::ChromeExtensionsClient()
65 : chrome_api_permissions_(ChromeAPIPermissions()),
66 extensions_api_permissions_(ExtensionsAPIPermissions()) {
69 ChromeExtensionsClient::~ChromeExtensionsClient() {
72 void ChromeExtensionsClient::Initialize() {
73 // Registration could already be finalized in unit tests, where the utility
74 // thread runs in-process.
75 if (!ManifestHandler::IsRegistrationFinalized()) {
76 RegisterCommonManifestHandlers();
77 RegisterChromeManifestHandlers();
78 ManifestHandler::FinalizeRegistration();
81 // Set up permissions.
82 PermissionsInfo::GetInstance()->AddProvider(chrome_api_permissions_
);
83 PermissionsInfo::GetInstance()->AddProvider(extensions_api_permissions_
);
85 // Set up the scripting whitelist.
86 // Whitelist ChromeVox, an accessibility extension from Google that needs
87 // the ability to script webui pages. This is temporary and is not
88 // meant to be a general solution.
89 // TODO(dmazzoni): remove this once we have an extension API that
90 // allows any extension to request read-only access to webui pages.
91 scripting_whitelist_
.push_back(extension_misc::kChromeVoxExtensionId
);
93 // Whitelist "Discover DevTools Companion" extension from Google that
94 // needs the ability to script DevTools pages. Companion will assist
95 // online courses and will be needed while the online educational programs
97 scripting_whitelist_
.push_back("angkfkebojeancgemegoedelbnjgcgme");
100 const PermissionMessageProvider
&
101 ChromeExtensionsClient::GetPermissionMessageProvider() const {
102 return permission_message_provider_
;
105 scoped_ptr
<FeatureProvider
> ChromeExtensionsClient::CreateFeatureProvider(
106 const std::string
& name
) const {
107 scoped_ptr
<FeatureProvider
> provider
;
108 scoped_ptr
<JSONFeatureProviderSource
> source(
109 CreateFeatureProviderSource(name
));
111 provider
.reset(new BaseFeatureProvider(source
->dictionary(),
112 CreateFeature
<APIFeature
>));
113 } else if (name
== "manifest") {
114 provider
.reset(new BaseFeatureProvider(source
->dictionary(),
115 CreateFeature
<ManifestFeature
>));
116 } else if (name
== "permission") {
117 provider
.reset(new BaseFeatureProvider(source
->dictionary(),
118 CreateFeature
<PermissionFeature
>));
122 return provider
.Pass();
125 scoped_ptr
<JSONFeatureProviderSource
>
126 ChromeExtensionsClient::CreateFeatureProviderSource(
127 const std::string
& name
) const {
128 scoped_ptr
<JSONFeatureProviderSource
> source(
129 new JSONFeatureProviderSource(name
));
131 source
->LoadJSON(IDR_EXTENSION_API_FEATURES
);
132 source
->LoadJSON(IDR_CHROME_EXTENSION_API_FEATURES
);
133 } else if (name
== "manifest") {
134 source
->LoadJSON(IDR_EXTENSION_MANIFEST_FEATURES
);
135 source
->LoadJSON(IDR_CHROME_EXTENSION_MANIFEST_FEATURES
);
136 } else if (name
== "permission") {
137 source
->LoadJSON(IDR_EXTENSION_PERMISSION_FEATURES
);
138 source
->LoadJSON(IDR_CHROME_EXTENSION_PERMISSION_FEATURES
);
143 return source
.Pass();
146 void ChromeExtensionsClient::FilterHostPermissions(
147 const URLPatternSet
& hosts
,
148 URLPatternSet
* new_hosts
,
149 std::set
<PermissionMessage
>* messages
) const {
150 for (URLPatternSet::const_iterator i
= hosts
.begin();
151 i
!= hosts
.end(); ++i
) {
152 // Filters out every URL pattern that matches chrome:// scheme.
153 if (i
->scheme() == content::kChromeUIScheme
) {
154 // chrome://favicon is the only URL for chrome:// scheme that we
155 // want to support. We want to deprecate the "chrome" scheme.
156 // We should not add any additional "host" here.
157 if (GURL(chrome::kChromeUIFaviconURL
).host() != i
->host())
159 messages
->insert(PermissionMessage(
160 PermissionMessage::kFavicon
,
161 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_FAVICON
)));
163 new_hosts
->AddPattern(*i
);
168 void ChromeExtensionsClient::SetScriptingWhitelist(
169 const ExtensionsClient::ScriptingWhitelist
& whitelist
) {
170 scripting_whitelist_
= whitelist
;
173 const ExtensionsClient::ScriptingWhitelist
&
174 ChromeExtensionsClient::GetScriptingWhitelist() const {
175 return scripting_whitelist_
;
178 URLPatternSet
ChromeExtensionsClient::GetPermittedChromeSchemeHosts(
179 const Extension
* extension
,
180 const APIPermissionSet
& api_permissions
) const {
182 // Regular extensions are only allowed access to chrome://favicon.
183 hosts
.AddPattern(URLPattern(URLPattern::SCHEME_CHROMEUI
,
184 chrome::kChromeUIFaviconURL
));
186 // Experimental extensions are also allowed chrome://thumb.
188 // TODO: A public API should be created for retrieving thumbnails.
189 // See http://crbug.com/222856. A temporary hack is implemented here to
190 // make chrome://thumbs available to NTP Russia extension as
192 if ((api_permissions
.find(APIPermission::kExperimental
) !=
193 api_permissions
.end()) ||
194 (extension
->id() == kThumbsWhiteListedExtension
&&
195 extension
->from_webstore())) {
196 hosts
.AddPattern(URLPattern(URLPattern::SCHEME_CHROMEUI
,
197 chrome::kChromeUIThumbnailURL
));
202 bool ChromeExtensionsClient::IsScriptableURL(
203 const GURL
& url
, std::string
* error
) const {
204 // The gallery is special-cased as a restricted URL for scripting to prevent
205 // access to special JS bindings we expose to the gallery (and avoid things
206 // like extensions removing the "report abuse" link).
207 // TODO(erikkay): This seems like the wrong test. Shouldn't we we testing
208 // against the store app extent?
209 GURL
store_url(extension_urls::GetWebstoreLaunchURL());
210 if (url
.host() == store_url
.host()) {
212 *error
= manifest_errors::kCannotScriptGallery
;
218 bool ChromeExtensionsClient::IsAPISchemaGenerated(
219 const std::string
& name
) const {
220 // Test from most common to least common.
221 return api::GeneratedSchemas::IsGenerated(name
) ||
222 core_api::GeneratedSchemas::IsGenerated(name
) ||
223 apps::api::GeneratedSchemas::IsGenerated(name
);
226 base::StringPiece
ChromeExtensionsClient::GetAPISchema(
227 const std::string
& name
) const {
228 // Test from most common to least common.
229 if (api::GeneratedSchemas::IsGenerated(name
))
230 return api::GeneratedSchemas::Get(name
);
232 if (core_api::GeneratedSchemas::IsGenerated(name
))
233 return core_api::GeneratedSchemas::Get(name
);
235 return apps::api::GeneratedSchemas::Get(name
);
238 void ChromeExtensionsClient::RegisterAPISchemaResources(
239 ExtensionAPI
* api
) const {
240 #if defined(ENABLE_EXTENSIONS)
241 api
->RegisterSchemaResource("accessibilityPrivate",
242 IDR_EXTENSION_API_JSON_ACCESSIBILITYPRIVATE
);
243 api
->RegisterSchemaResource("app", IDR_EXTENSION_API_JSON_APP
);
244 api
->RegisterSchemaResource("browserAction",
245 IDR_EXTENSION_API_JSON_BROWSERACTION
);
246 api
->RegisterSchemaResource("commands", IDR_EXTENSION_API_JSON_COMMANDS
);
247 api
->RegisterSchemaResource("declarativeContent",
248 IDR_EXTENSION_API_JSON_DECLARATIVE_CONTENT
);
249 api
->RegisterSchemaResource("declarativeWebRequest",
250 IDR_EXTENSION_API_JSON_DECLARATIVE_WEBREQUEST
);
251 api
->RegisterSchemaResource("fileBrowserHandler",
252 IDR_EXTENSION_API_JSON_FILEBROWSERHANDLER
);
253 api
->RegisterSchemaResource("inputMethodPrivate",
254 IDR_EXTENSION_API_JSON_INPUTMETHODPRIVATE
);
255 api
->RegisterSchemaResource("pageAction", IDR_EXTENSION_API_JSON_PAGEACTION
);
256 api
->RegisterSchemaResource("pageActions",
257 IDR_EXTENSION_API_JSON_PAGEACTIONS
);
258 api
->RegisterSchemaResource("privacy", IDR_EXTENSION_API_JSON_PRIVACY
);
259 api
->RegisterSchemaResource("processes", IDR_EXTENSION_API_JSON_PROCESSES
);
260 api
->RegisterSchemaResource("proxy", IDR_EXTENSION_API_JSON_PROXY
);
261 api
->RegisterSchemaResource("scriptBadge",
262 IDR_EXTENSION_API_JSON_SCRIPTBADGE
);
263 api
->RegisterSchemaResource("ttsEngine", IDR_EXTENSION_API_JSON_TTSENGINE
);
264 api
->RegisterSchemaResource("tts", IDR_EXTENSION_API_JSON_TTS
);
265 api
->RegisterSchemaResource("types", IDR_EXTENSION_API_JSON_TYPES
);
266 api
->RegisterSchemaResource("types.private",
267 IDR_EXTENSION_API_JSON_TYPES_PRIVATE
);
268 api
->RegisterSchemaResource("webstore", IDR_EXTENSION_API_JSON_WEBSTORE
);
269 api
->RegisterSchemaResource("webViewRequest",
270 IDR_EXTENSION_API_JSON_WEBVIEW_REQUEST
);
271 #endif // defined(ENABLE_EXTENSIONS)
274 bool ChromeExtensionsClient::ShouldSuppressFatalErrors() const {
275 // Suppress fatal errors only on beta and stable channels.
276 return GetCurrentChannel() > chrome::VersionInfo::CHANNEL_DEV
;
280 ChromeExtensionsClient
* ChromeExtensionsClient::GetInstance() {
281 return g_client
.Pointer();
284 } // namespace extensions