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/browser/shell_extensions_browser_client.h"
7 #include "content/public/browser/browser_context.h"
8 #include "content/public/browser/browser_thread.h"
9 #include "content/public/browser/render_frame_host.h"
10 #include "extensions/browser/api/extensions_api_client.h"
11 #include "extensions/browser/api/generated_api_registration.h"
12 #include "extensions/browser/app_sorting.h"
13 #include "extensions/browser/event_router.h"
14 #include "extensions/browser/extension_function_registry.h"
15 #include "extensions/browser/mojo/service_registration.h"
16 #include "extensions/browser/null_app_sorting.h"
17 #include "extensions/browser/updater/null_extension_cache.h"
18 #include "extensions/browser/url_request_util.h"
19 #include "extensions/shell/browser/api/generated_api_registration.h"
20 #include "extensions/shell/browser/shell_extension_host_delegate.h"
21 #include "extensions/shell/browser/shell_extension_system_factory.h"
22 #include "extensions/shell/browser/shell_extensions_api_client.h"
23 #include "extensions/shell/browser/shell_runtime_api_delegate.h"
25 #if defined(OS_CHROMEOS)
26 #include "chromeos/login/login_state.h"
29 using content::BrowserContext
;
30 using content::BrowserThread
;
32 namespace extensions
{
34 ShellExtensionsBrowserClient::ShellExtensionsBrowserClient(
35 BrowserContext
* context
,
36 PrefService
* pref_service
)
37 : browser_context_(context
),
38 pref_service_(pref_service
),
39 api_client_(new ShellExtensionsAPIClient
),
40 extension_cache_(new NullExtensionCache()) {
43 ShellExtensionsBrowserClient::~ShellExtensionsBrowserClient() {
46 bool ShellExtensionsBrowserClient::IsShuttingDown() {
50 bool ShellExtensionsBrowserClient::AreExtensionsDisabled(
51 const base::CommandLine
& command_line
,
52 BrowserContext
* context
) {
56 bool ShellExtensionsBrowserClient::IsValidContext(BrowserContext
* context
) {
57 return context
== browser_context_
;
60 bool ShellExtensionsBrowserClient::IsSameContext(BrowserContext
* first
,
61 BrowserContext
* second
) {
62 return first
== second
;
65 bool ShellExtensionsBrowserClient::HasOffTheRecordContext(
66 BrowserContext
* context
) {
70 BrowserContext
* ShellExtensionsBrowserClient::GetOffTheRecordContext(
71 BrowserContext
* context
) {
72 // app_shell only supports a single context.
76 BrowserContext
* ShellExtensionsBrowserClient::GetOriginalContext(
77 BrowserContext
* context
) {
81 #if defined(OS_CHROMEOS)
82 std::string
ShellExtensionsBrowserClient::GetUserIdHashFromContext(
83 content::BrowserContext
* context
) {
84 return chromeos::LoginState::Get()->primary_user_hash();;
88 bool ShellExtensionsBrowserClient::IsGuestSession(
89 BrowserContext
* context
) const {
93 bool ShellExtensionsBrowserClient::IsExtensionIncognitoEnabled(
94 const std::string
& extension_id
,
95 content::BrowserContext
* context
) const {
99 bool ShellExtensionsBrowserClient::CanExtensionCrossIncognito(
100 const Extension
* extension
,
101 content::BrowserContext
* context
) const {
106 ShellExtensionsBrowserClient::MaybeCreateResourceBundleRequestJob(
107 net::URLRequest
* request
,
108 net::NetworkDelegate
* network_delegate
,
109 const base::FilePath
& directory_path
,
110 const std::string
& content_security_policy
,
111 bool send_cors_header
) {
115 bool ShellExtensionsBrowserClient::AllowCrossRendererResourceLoad(
116 net::URLRequest
* request
,
118 const Extension
* extension
,
119 InfoMap
* extension_info_map
) {
120 bool allowed
= false;
121 if (url_request_util::AllowCrossRendererResourceLoad(
122 request
, is_incognito
, extension
, extension_info_map
, &allowed
)) {
126 // Couldn't determine if resource is allowed. Block the load.
130 PrefService
* ShellExtensionsBrowserClient::GetPrefServiceForContext(
131 BrowserContext
* context
) {
132 return pref_service_
;
135 void ShellExtensionsBrowserClient::GetEarlyExtensionPrefsObservers(
136 content::BrowserContext
* context
,
137 std::vector
<ExtensionPrefsObserver
*>* observers
) const {
140 ProcessManagerDelegate
*
141 ShellExtensionsBrowserClient::GetProcessManagerDelegate() const {
145 scoped_ptr
<ExtensionHostDelegate
>
146 ShellExtensionsBrowserClient::CreateExtensionHostDelegate() {
147 return scoped_ptr
<ExtensionHostDelegate
>(new ShellExtensionHostDelegate
);
150 bool ShellExtensionsBrowserClient::DidVersionUpdate(BrowserContext
* context
) {
151 // TODO(jamescook): We might want to tell extensions when app_shell updates.
155 void ShellExtensionsBrowserClient::PermitExternalProtocolHandler() {
158 scoped_ptr
<AppSorting
> ShellExtensionsBrowserClient::CreateAppSorting() {
159 return scoped_ptr
<AppSorting
>(new NullAppSorting
);
162 bool ShellExtensionsBrowserClient::IsRunningInForcedAppMode() {
166 ApiActivityMonitor
* ShellExtensionsBrowserClient::GetApiActivityMonitor(
167 BrowserContext
* context
) {
168 // app_shell doesn't monitor API function calls or events.
172 ExtensionSystemProvider
*
173 ShellExtensionsBrowserClient::GetExtensionSystemFactory() {
174 return ShellExtensionSystemFactory::GetInstance();
177 void ShellExtensionsBrowserClient::RegisterExtensionFunctions(
178 ExtensionFunctionRegistry
* registry
) const {
179 // Register core extension-system APIs.
180 core_api::GeneratedFunctionRegistry::RegisterAll(registry
);
182 // app_shell-only APIs.
183 shell::api::GeneratedFunctionRegistry::RegisterAll(registry
);
186 void ShellExtensionsBrowserClient::RegisterMojoServices(
187 content::RenderFrameHost
* render_frame_host
,
188 const Extension
* extension
) const {
189 RegisterServicesForFrame(render_frame_host
, extension
);
192 scoped_ptr
<RuntimeAPIDelegate
>
193 ShellExtensionsBrowserClient::CreateRuntimeAPIDelegate(
194 content::BrowserContext
* context
) const {
195 return scoped_ptr
<RuntimeAPIDelegate
>(new ShellRuntimeAPIDelegate());
198 const ComponentExtensionResourceManager
*
199 ShellExtensionsBrowserClient::GetComponentExtensionResourceManager() {
203 void ShellExtensionsBrowserClient::BroadcastEventToRenderers(
204 const std::string
& event_name
,
205 scoped_ptr
<base::ListValue
> args
) {
206 if (!BrowserThread::CurrentlyOn(BrowserThread::UI
)) {
207 BrowserThread::PostTask(
210 base::Bind(&ShellExtensionsBrowserClient::BroadcastEventToRenderers
,
211 base::Unretained(this),
213 base::Passed(&args
)));
217 scoped_ptr
<Event
> event(new Event(event_name
, args
.Pass()));
218 EventRouter::Get(browser_context_
)->BroadcastEvent(event
.Pass());
221 net::NetLog
* ShellExtensionsBrowserClient::GetNetLog() {
225 ExtensionCache
* ShellExtensionsBrowserClient::GetExtensionCache() {
226 return extension_cache_
.get();
229 bool ShellExtensionsBrowserClient::IsBackgroundUpdateAllowed() {
233 bool ShellExtensionsBrowserClient::IsMinBrowserVersionSupported(
234 const std::string
& min_version
) {
238 void ShellExtensionsBrowserClient::SetAPIClientForTest(
239 ExtensionsAPIClient
* api_client
) {
240 api_client_
.reset(api_client
);
243 } // namespace extensions