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_extension_web_contents_observer.h"
23 #include "extensions/shell/browser/shell_extensions_api_client.h"
24 #include "extensions/shell/browser/shell_runtime_api_delegate.h"
26 #if defined(OS_CHROMEOS)
27 #include "chromeos/login/login_state.h"
30 using content::BrowserContext
;
31 using content::BrowserThread
;
33 namespace extensions
{
35 ShellExtensionsBrowserClient::ShellExtensionsBrowserClient(
36 BrowserContext
* context
,
37 PrefService
* pref_service
)
38 : browser_context_(context
),
39 pref_service_(pref_service
),
40 api_client_(new ShellExtensionsAPIClient
),
41 extension_cache_(new NullExtensionCache()) {
44 ShellExtensionsBrowserClient::~ShellExtensionsBrowserClient() {
47 bool ShellExtensionsBrowserClient::IsShuttingDown() {
51 bool ShellExtensionsBrowserClient::AreExtensionsDisabled(
52 const base::CommandLine
& command_line
,
53 BrowserContext
* context
) {
57 bool ShellExtensionsBrowserClient::IsValidContext(BrowserContext
* context
) {
58 return context
== browser_context_
;
61 bool ShellExtensionsBrowserClient::IsSameContext(BrowserContext
* first
,
62 BrowserContext
* second
) {
63 return first
== second
;
66 bool ShellExtensionsBrowserClient::HasOffTheRecordContext(
67 BrowserContext
* context
) {
71 BrowserContext
* ShellExtensionsBrowserClient::GetOffTheRecordContext(
72 BrowserContext
* context
) {
73 // app_shell only supports a single context.
77 BrowserContext
* ShellExtensionsBrowserClient::GetOriginalContext(
78 BrowserContext
* context
) {
82 #if defined(OS_CHROMEOS)
83 std::string
ShellExtensionsBrowserClient::GetUserIdHashFromContext(
84 content::BrowserContext
* context
) {
85 return chromeos::LoginState::Get()->primary_user_hash();;
89 bool ShellExtensionsBrowserClient::IsGuestSession(
90 BrowserContext
* context
) const {
94 bool ShellExtensionsBrowserClient::IsExtensionIncognitoEnabled(
95 const std::string
& extension_id
,
96 content::BrowserContext
* context
) const {
100 bool ShellExtensionsBrowserClient::CanExtensionCrossIncognito(
101 const Extension
* extension
,
102 content::BrowserContext
* context
) const {
107 ShellExtensionsBrowserClient::MaybeCreateResourceBundleRequestJob(
108 net::URLRequest
* request
,
109 net::NetworkDelegate
* network_delegate
,
110 const base::FilePath
& directory_path
,
111 const std::string
& content_security_policy
,
112 bool send_cors_header
) {
116 bool ShellExtensionsBrowserClient::AllowCrossRendererResourceLoad(
117 net::URLRequest
* request
,
119 const Extension
* extension
,
120 InfoMap
* extension_info_map
) {
121 bool allowed
= false;
122 if (url_request_util::AllowCrossRendererResourceLoad(
123 request
, is_incognito
, extension
, extension_info_map
, &allowed
)) {
127 // Couldn't determine if resource is allowed. Block the load.
131 PrefService
* ShellExtensionsBrowserClient::GetPrefServiceForContext(
132 BrowserContext
* context
) {
133 return pref_service_
;
136 void ShellExtensionsBrowserClient::GetEarlyExtensionPrefsObservers(
137 content::BrowserContext
* context
,
138 std::vector
<ExtensionPrefsObserver
*>* observers
) const {
141 ProcessManagerDelegate
*
142 ShellExtensionsBrowserClient::GetProcessManagerDelegate() const {
146 scoped_ptr
<ExtensionHostDelegate
>
147 ShellExtensionsBrowserClient::CreateExtensionHostDelegate() {
148 return scoped_ptr
<ExtensionHostDelegate
>(new ShellExtensionHostDelegate
);
151 bool ShellExtensionsBrowserClient::DidVersionUpdate(BrowserContext
* context
) {
152 // TODO(jamescook): We might want to tell extensions when app_shell updates.
156 void ShellExtensionsBrowserClient::PermitExternalProtocolHandler() {
159 scoped_ptr
<AppSorting
> ShellExtensionsBrowserClient::CreateAppSorting(
160 content::BrowserContext
* context
) {
161 return scoped_ptr
<AppSorting
>(new NullAppSorting
);
164 bool ShellExtensionsBrowserClient::IsRunningInForcedAppMode() {
168 ApiActivityMonitor
* ShellExtensionsBrowserClient::GetApiActivityMonitor(
169 BrowserContext
* context
) {
170 // app_shell doesn't monitor API function calls or events.
174 ExtensionSystemProvider
*
175 ShellExtensionsBrowserClient::GetExtensionSystemFactory() {
176 return ShellExtensionSystemFactory::GetInstance();
179 void ShellExtensionsBrowserClient::RegisterExtensionFunctions(
180 ExtensionFunctionRegistry
* registry
) const {
181 // Register core extension-system APIs.
182 api::GeneratedFunctionRegistry::RegisterAll(registry
);
184 // app_shell-only APIs.
185 shell::api::ShellGeneratedFunctionRegistry::RegisterAll(registry
);
188 void ShellExtensionsBrowserClient::RegisterMojoServices(
189 content::RenderFrameHost
* render_frame_host
,
190 const Extension
* extension
) const {
191 RegisterServicesForFrame(render_frame_host
, extension
);
194 scoped_ptr
<RuntimeAPIDelegate
>
195 ShellExtensionsBrowserClient::CreateRuntimeAPIDelegate(
196 content::BrowserContext
* context
) const {
197 return scoped_ptr
<RuntimeAPIDelegate
>(new ShellRuntimeAPIDelegate());
200 const ComponentExtensionResourceManager
*
201 ShellExtensionsBrowserClient::GetComponentExtensionResourceManager() {
205 void ShellExtensionsBrowserClient::BroadcastEventToRenderers(
206 events::HistogramValue histogram_value
,
207 const std::string
& event_name
,
208 scoped_ptr
<base::ListValue
> args
) {
209 if (!BrowserThread::CurrentlyOn(BrowserThread::UI
)) {
210 BrowserThread::PostTask(
211 BrowserThread::UI
, FROM_HERE
,
212 base::Bind(&ShellExtensionsBrowserClient::BroadcastEventToRenderers
,
213 base::Unretained(this), histogram_value
, event_name
,
214 base::Passed(&args
)));
218 scoped_ptr
<Event
> event(new Event(histogram_value
, event_name
, args
.Pass()));
219 EventRouter::Get(browser_context_
)->BroadcastEvent(event
.Pass());
222 net::NetLog
* ShellExtensionsBrowserClient::GetNetLog() {
226 ExtensionCache
* ShellExtensionsBrowserClient::GetExtensionCache() {
227 return extension_cache_
.get();
230 bool ShellExtensionsBrowserClient::IsBackgroundUpdateAllowed() {
234 bool ShellExtensionsBrowserClient::IsMinBrowserVersionSupported(
235 const std::string
& min_version
) {
239 void ShellExtensionsBrowserClient::SetAPIClientForTest(
240 ExtensionsAPIClient
* api_client
) {
241 api_client_
.reset(api_client
);
244 ExtensionWebContentsObserver
*
245 ShellExtensionsBrowserClient::GetExtensionWebContentsObserver(
246 content::WebContents
* web_contents
) {
247 return ShellExtensionWebContentsObserver::FromWebContents(web_contents
);
250 } // namespace extensions