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/api/sockets/sockets_manifest_handler.h"
11 #include "extensions/common/common_manifest_handlers.h"
12 #include "extensions/common/features/api_feature.h"
13 #include "extensions/common/features/base_feature_provider.h"
14 #include "extensions/common/features/json_feature_provider_source.h"
15 #include "extensions/common/features/manifest_feature.h"
16 #include "extensions/common/features/permission_feature.h"
17 #include "extensions/common/features/simple_feature.h"
18 #include "extensions/common/manifest_handler.h"
19 #include "extensions/common/permissions/permission_message_provider.h"
20 #include "extensions/common/permissions/permissions_info.h"
21 #include "extensions/common/permissions/permissions_provider.h"
22 #include "extensions/common/url_pattern_set.h"
23 #include "extensions/shell/common/api/generated_schemas.h"
24 #include "grit/app_shell_resources.h"
25 #include "grit/extensions_resources.h"
27 namespace extensions
{
31 template <class FeatureClass
>
32 SimpleFeature
* CreateFeature() {
33 return new FeatureClass
;
36 // TODO(jamescook): Refactor ChromePermissionsMessageProvider so we can share
37 // code. For now, this implementation does nothing.
38 class ShellPermissionMessageProvider
: public PermissionMessageProvider
{
40 ShellPermissionMessageProvider() {}
41 virtual ~ShellPermissionMessageProvider() {}
43 // PermissionMessageProvider implementation.
44 virtual PermissionMessages
GetPermissionMessages(
45 const PermissionSet
* permissions
,
46 Manifest::Type extension_type
) const OVERRIDE
{
47 return PermissionMessages();
50 virtual std::vector
<base::string16
> GetWarningMessages(
51 const PermissionSet
* permissions
,
52 Manifest::Type extension_type
) const OVERRIDE
{
53 return std::vector
<base::string16
>();
56 virtual std::vector
<base::string16
> GetWarningMessagesDetails(
57 const PermissionSet
* permissions
,
58 Manifest::Type extension_type
) const OVERRIDE
{
59 return std::vector
<base::string16
>();
62 virtual bool IsPrivilegeIncrease(
63 const PermissionSet
* old_permissions
,
64 const PermissionSet
* new_permissions
,
65 Manifest::Type extension_type
) const OVERRIDE
{
66 // Ensure we implement this before shipping.
72 DISALLOW_COPY_AND_ASSIGN(ShellPermissionMessageProvider
);
75 base::LazyInstance
<ShellPermissionMessageProvider
>
76 g_permission_message_provider
= LAZY_INSTANCE_INITIALIZER
;
80 ShellExtensionsClient::ShellExtensionsClient()
81 : extensions_api_permissions_(ExtensionsAPIPermissions()) {
84 ShellExtensionsClient::~ShellExtensionsClient() {
87 void ShellExtensionsClient::Initialize() {
88 RegisterCommonManifestHandlers();
90 // TODO(rockot): API manifest handlers which move out to src/extensions
91 // should either end up in RegisterCommonManifestHandlers or some new
92 // initialization step specifically for API manifest handlers.
93 (new SocketsManifestHandler
)->Register();
95 ManifestHandler::FinalizeRegistration();
96 // TODO(jamescook): Do we need to whitelist any extensions?
98 PermissionsInfo::GetInstance()->AddProvider(extensions_api_permissions_
);
101 const PermissionMessageProvider
&
102 ShellExtensionsClient::GetPermissionMessageProvider() const {
104 return g_permission_message_provider
.Get();
107 const std::string
ShellExtensionsClient::GetProductName() {
111 scoped_ptr
<FeatureProvider
> ShellExtensionsClient::CreateFeatureProvider(
112 const std::string
& name
) const {
113 scoped_ptr
<FeatureProvider
> provider
;
114 scoped_ptr
<JSONFeatureProviderSource
> source(
115 CreateFeatureProviderSource(name
));
117 provider
.reset(new BaseFeatureProvider(source
->dictionary(),
118 CreateFeature
<APIFeature
>));
119 } else if (name
== "manifest") {
120 provider
.reset(new BaseFeatureProvider(source
->dictionary(),
121 CreateFeature
<ManifestFeature
>));
122 } else if (name
== "permission") {
123 provider
.reset(new BaseFeatureProvider(source
->dictionary(),
124 CreateFeature
<PermissionFeature
>));
128 return provider
.Pass();
131 scoped_ptr
<JSONFeatureProviderSource
>
132 ShellExtensionsClient::CreateFeatureProviderSource(
133 const std::string
& name
) const {
134 scoped_ptr
<JSONFeatureProviderSource
> source(
135 new JSONFeatureProviderSource(name
));
137 source
->LoadJSON(IDR_EXTENSION_API_FEATURES
);
138 source
->LoadJSON(IDR_SHELL_EXTENSION_API_FEATURES
);
139 } else if (name
== "manifest") {
140 source
->LoadJSON(IDR_EXTENSION_MANIFEST_FEATURES
);
141 } else if (name
== "permission") {
142 source
->LoadJSON(IDR_EXTENSION_PERMISSION_FEATURES
);
147 return source
.Pass();
150 void ShellExtensionsClient::FilterHostPermissions(
151 const URLPatternSet
& hosts
,
152 URLPatternSet
* new_hosts
,
153 std::set
<PermissionMessage
>* messages
) const {
157 void ShellExtensionsClient::SetScriptingWhitelist(
158 const ScriptingWhitelist
& whitelist
) {
159 scripting_whitelist_
= whitelist
;
162 const ExtensionsClient::ScriptingWhitelist
&
163 ShellExtensionsClient::GetScriptingWhitelist() const {
164 // TODO(jamescook): Real whitelist.
165 return scripting_whitelist_
;
168 URLPatternSet
ShellExtensionsClient::GetPermittedChromeSchemeHosts(
169 const Extension
* extension
,
170 const APIPermissionSet
& api_permissions
) const {
172 return URLPatternSet();
175 bool ShellExtensionsClient::IsScriptableURL(const GURL
& url
,
176 std::string
* error
) const {
181 bool ShellExtensionsClient::IsAPISchemaGenerated(
182 const std::string
& name
) const {
183 // TODO(rockot): Remove dependency on src/chrome once we have some core APIs
184 // moved out. See http://crbug.com/349042.
185 // Special-case our simplified app.runtime implementation because we don't
186 // have the Chrome app APIs available.
187 return core_api::GeneratedSchemas::IsGenerated(name
) ||
188 shell_api::GeneratedSchemas::IsGenerated(name
);
191 base::StringPiece
ShellExtensionsClient::GetAPISchema(
192 const std::string
& name
) const {
193 // Schema for chrome.shell APIs.
194 if (shell_api::GeneratedSchemas::IsGenerated(name
))
195 return shell_api::GeneratedSchemas::Get(name
);
197 return core_api::GeneratedSchemas::Get(name
);
200 void ShellExtensionsClient::RegisterAPISchemaResources(
201 ExtensionAPI
* api
) const {
204 bool ShellExtensionsClient::ShouldSuppressFatalErrors() const {
208 } // namespace extensions