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/shell/common/shell_extensions_client.h"
7 #include "base/lazy_instance.h"
8 #include "base/logging.h"
9 #include "extensions/common/api/generated_schemas.h"
10 #include "extensions/common/common_manifest_handlers.h"
11 #include "extensions/common/extension_urls.h"
12 #include "extensions/common/features/api_feature.h"
13 #include "extensions/common/features/base_feature_provider.h"
14 #include "extensions/common/features/behavior_feature.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/features/simple_feature.h"
19 #include "extensions/common/manifest_handler.h"
20 #include "extensions/common/permissions/permission_message_provider.h"
21 #include "extensions/common/permissions/permissions_info.h"
22 #include "extensions/common/permissions/permissions_provider.h"
23 #include "extensions/common/url_pattern_set.h"
24 #include "extensions/shell/common/api/generated_schemas.h"
25 #include "grit/app_shell_resources.h"
26 #include "grit/extensions_resources.h"
28 namespace extensions
{
32 template <class FeatureClass
>
33 SimpleFeature
* CreateFeature() {
34 return new FeatureClass
;
37 // TODO(jamescook): Refactor ChromePermissionsMessageProvider so we can share
38 // code. For now, this implementation does nothing.
39 class ShellPermissionMessageProvider
: public PermissionMessageProvider
{
41 ShellPermissionMessageProvider() {}
42 ~ShellPermissionMessageProvider() override
{}
44 // PermissionMessageProvider implementation.
45 CoalescedPermissionMessages
GetCoalescedPermissionMessages(
46 const PermissionIDSet
& permissions
) const override
{
47 return CoalescedPermissionMessages();
50 bool IsPrivilegeIncrease(const PermissionSet
* old_permissions
,
51 const PermissionSet
* new_permissions
,
52 Manifest::Type extension_type
) const override
{
53 // Ensure we implement this before shipping.
58 PermissionIDSet
GetAllPermissionIDs(
59 const PermissionSet
* permissions
,
60 Manifest::Type extension_type
) const override
{
61 return PermissionIDSet();
65 DISALLOW_COPY_AND_ASSIGN(ShellPermissionMessageProvider
);
68 base::LazyInstance
<ShellPermissionMessageProvider
>
69 g_permission_message_provider
= LAZY_INSTANCE_INITIALIZER
;
73 ShellExtensionsClient::ShellExtensionsClient()
74 : extensions_api_permissions_(ExtensionsAPIPermissions()) {
77 ShellExtensionsClient::~ShellExtensionsClient() {
80 void ShellExtensionsClient::Initialize() {
81 RegisterCommonManifestHandlers();
82 ManifestHandler::FinalizeRegistration();
83 // TODO(jamescook): Do we need to whitelist any extensions?
85 PermissionsInfo::GetInstance()->AddProvider(extensions_api_permissions_
);
88 const PermissionMessageProvider
&
89 ShellExtensionsClient::GetPermissionMessageProvider() const {
91 return g_permission_message_provider
.Get();
94 const std::string
ShellExtensionsClient::GetProductName() {
98 scoped_ptr
<FeatureProvider
> ShellExtensionsClient::CreateFeatureProvider(
99 const std::string
& name
) const {
100 scoped_ptr
<FeatureProvider
> provider
;
101 scoped_ptr
<JSONFeatureProviderSource
> source(
102 CreateFeatureProviderSource(name
));
104 provider
.reset(new BaseFeatureProvider(source
->dictionary(),
105 CreateFeature
<APIFeature
>));
106 } else if (name
== "manifest") {
107 provider
.reset(new BaseFeatureProvider(source
->dictionary(),
108 CreateFeature
<ManifestFeature
>));
109 } else if (name
== "permission") {
110 provider
.reset(new BaseFeatureProvider(source
->dictionary(),
111 CreateFeature
<PermissionFeature
>));
112 } else if (name
== "behavior") {
113 provider
.reset(new BaseFeatureProvider(source
->dictionary(),
114 CreateFeature
<BehaviorFeature
>));
118 return provider
.Pass();
121 scoped_ptr
<JSONFeatureProviderSource
>
122 ShellExtensionsClient::CreateFeatureProviderSource(
123 const std::string
& name
) const {
124 scoped_ptr
<JSONFeatureProviderSource
> source(
125 new JSONFeatureProviderSource(name
));
127 source
->LoadJSON(IDR_EXTENSION_API_FEATURES
);
128 source
->LoadJSON(IDR_SHELL_EXTENSION_API_FEATURES
);
129 } else if (name
== "manifest") {
130 source
->LoadJSON(IDR_EXTENSION_MANIFEST_FEATURES
);
131 } else if (name
== "permission") {
132 source
->LoadJSON(IDR_EXTENSION_PERMISSION_FEATURES
);
133 } else if (name
== "behavior") {
134 source
->LoadJSON(IDR_EXTENSION_BEHAVIOR_FEATURES
);
139 return source
.Pass();
142 void ShellExtensionsClient::FilterHostPermissions(
143 const URLPatternSet
& hosts
,
144 URLPatternSet
* new_hosts
,
145 std::set
<PermissionMessage
>* messages
) const {
149 void ShellExtensionsClient::FilterHostPermissions(
150 const URLPatternSet
& hosts
,
151 URLPatternSet
* new_hosts
,
152 PermissionIDSet
* permissions
) const {
156 void ShellExtensionsClient::SetScriptingWhitelist(
157 const ScriptingWhitelist
& whitelist
) {
158 scripting_whitelist_
= whitelist
;
161 const ExtensionsClient::ScriptingWhitelist
&
162 ShellExtensionsClient::GetScriptingWhitelist() const {
163 // TODO(jamescook): Real whitelist.
164 return scripting_whitelist_
;
167 URLPatternSet
ShellExtensionsClient::GetPermittedChromeSchemeHosts(
168 const Extension
* extension
,
169 const APIPermissionSet
& api_permissions
) const {
171 return URLPatternSet();
174 bool ShellExtensionsClient::IsScriptableURL(const GURL
& url
,
175 std::string
* error
) const {
180 bool ShellExtensionsClient::IsAPISchemaGenerated(
181 const std::string
& name
) const {
182 return api::GeneratedSchemas::IsGenerated(name
) ||
183 shell::api::ShellGeneratedSchemas::IsGenerated(name
);
186 base::StringPiece
ShellExtensionsClient::GetAPISchema(
187 const std::string
& name
) const {
188 // Schema for app_shell-only APIs.
189 if (shell::api::ShellGeneratedSchemas::IsGenerated(name
))
190 return shell::api::ShellGeneratedSchemas::Get(name
);
192 // Core extensions APIs.
193 return api::GeneratedSchemas::Get(name
);
196 void ShellExtensionsClient::RegisterAPISchemaResources(
197 ExtensionAPI
* api
) const {
200 bool ShellExtensionsClient::ShouldSuppressFatalErrors() const {
204 void ShellExtensionsClient::RecordDidSuppressFatalError() {
207 std::string
ShellExtensionsClient::GetWebstoreBaseURL() const {
208 return extension_urls::kChromeWebstoreBaseURL
;
211 std::string
ShellExtensionsClient::GetWebstoreUpdateURL() const {
212 return extension_urls::kChromeWebstoreUpdateURL
;
215 bool ShellExtensionsClient::IsBlacklistUpdateURL(const GURL
& url
) const {
216 // TODO(rockot): Maybe we want to do something else here. For now we accept
217 // any URL as a blacklist URL because we don't really care.
221 } // namespace extensions