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 "apps/shell/browser/shell_content_browser_client.h"
7 #include "apps/shell/browser/shell_browser_context.h"
8 #include "apps/shell/browser/shell_browser_main_parts.h"
9 #include "apps/shell/browser/shell_extension_system.h"
10 #include "base/command_line.h"
11 #include "chrome/browser/extensions/extension_protocols.h"
12 #include "chrome/browser/extensions/extension_resource_protocols.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/render_process_host.h"
15 #include "content/public/browser/site_instance.h"
16 #include "content/public/common/content_switches.h"
17 #include "content/public/common/url_constants.h"
18 #include "content/shell/browser/shell_browser_context.h"
19 #include "extensions/browser/extension_message_filter.h"
20 #include "extensions/browser/extension_registry.h"
21 #include "extensions/browser/info_map.h"
22 #include "extensions/browser/process_map.h"
23 #include "extensions/common/constants.h"
24 #include "extensions/common/extension.h"
25 #include "extensions/common/switches.h"
28 using content::BrowserThread
;
29 using extensions::ExtensionRegistry
;
33 ShellContentBrowserClient::ShellContentBrowserClient()
34 : browser_main_parts_(NULL
) {
37 ShellContentBrowserClient::~ShellContentBrowserClient() {
40 content::BrowserMainParts
* ShellContentBrowserClient::CreateBrowserMainParts(
41 const content::MainFunctionParams
& parameters
) {
42 browser_main_parts_
= new ShellBrowserMainParts(parameters
);
43 return browser_main_parts_
;
46 void ShellContentBrowserClient::RenderProcessWillLaunch(
47 content::RenderProcessHost
* host
) {
48 int render_process_id
= host
->GetID();
49 host
->AddFilter(new extensions::ExtensionMessageFilter(
50 render_process_id
, browser_main_parts_
->browser_context()));
53 net::URLRequestContextGetter
*
54 ShellContentBrowserClient::CreateRequestContext(
55 content::BrowserContext
* content_browser_context
,
56 content::ProtocolHandlerMap
* protocol_handlers
) {
57 // Handle chrome-extension: and chrome-extension-resource: requests.
58 extensions::InfoMap
* extension_info_map
=
59 browser_main_parts_
->extension_system()->info_map();
60 (*protocol_handlers
)[extensions::kExtensionScheme
] =
61 linked_ptr
<net::URLRequestJobFactory::ProtocolHandler
>(
62 CreateExtensionProtocolHandler(false /*is_incognito*/,
64 (*protocol_handlers
)[extensions::kExtensionResourceScheme
] =
65 linked_ptr
<net::URLRequestJobFactory::ProtocolHandler
>(
66 CreateExtensionResourceProtocolHandler());
67 // Let content::ShellBrowserContext handle the rest of the setup.
68 return browser_main_parts_
->browser_context()->CreateRequestContext(
72 bool ShellContentBrowserClient::IsHandledURL(const GURL
& url
) {
75 // Keep in sync with ProtocolHandlers added in CreateRequestContext() and in
76 // content::ShellURLRequestContextGetter::GetURLRequestContext().
77 static const char* const kProtocolList
[] = {
79 chrome::kChromeUIScheme
,
80 chrome::kChromeDevToolsScheme
,
83 content::kFileSystemScheme
,
84 extensions::kExtensionScheme
,
85 extensions::kExtensionResourceScheme
,
87 for (size_t i
= 0; i
< arraysize(kProtocolList
); ++i
) {
88 if (url
.scheme() == kProtocolList
[i
])
94 void ShellContentBrowserClient::SiteInstanceGotProcess(
95 content::SiteInstance
* site_instance
) {
96 // If this isn't an extension renderer there's nothing to do.
97 const extensions::Extension
* extension
= GetExtension(site_instance
);
101 extensions::ProcessMap::Get(browser_main_parts_
->browser_context())
102 ->Insert(extension
->id(),
103 site_instance
->GetProcess()->GetID(),
104 site_instance
->GetId());
106 BrowserThread::PostTask(
109 base::Bind(&extensions::InfoMap::RegisterExtensionProcess
,
110 browser_main_parts_
->extension_system()->info_map(),
112 site_instance
->GetProcess()->GetID(),
113 site_instance
->GetId()));
116 void ShellContentBrowserClient::SiteInstanceDeleting(
117 content::SiteInstance
* site_instance
) {
118 // If this isn't an extension renderer there's nothing to do.
119 const extensions::Extension
* extension
= GetExtension(site_instance
);
123 extensions::ProcessMap::Get(browser_main_parts_
->browser_context())
124 ->Remove(extension
->id(),
125 site_instance
->GetProcess()->GetID(),
126 site_instance
->GetId());
128 BrowserThread::PostTask(
131 base::Bind(&extensions::InfoMap::UnregisterExtensionProcess
,
132 browser_main_parts_
->extension_system()->info_map(),
134 site_instance
->GetProcess()->GetID(),
135 site_instance
->GetId()));
138 void ShellContentBrowserClient::AppendExtraCommandLineSwitches(
139 CommandLine
* command_line
, int child_process_id
) {
140 std::string process_type
=
141 command_line
->GetSwitchValueASCII(switches::kProcessType
);
142 if (process_type
== switches::kRendererProcess
) {
143 // TODO(jamescook): Should we check here if the process is in the extension
144 // service process map, or can we assume all renderers are extension
146 command_line
->AppendSwitch(extensions::switches::kExtensionProcess
);
150 const extensions::Extension
* ShellContentBrowserClient::GetExtension(
151 content::SiteInstance
* site_instance
) {
152 ExtensionRegistry
* registry
=
153 ExtensionRegistry::Get(site_instance
->GetBrowserContext());
154 return registry
->enabled_extensions().GetExtensionOrAppByURL(
155 site_instance
->GetSiteURL());