OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "apps/shell/browser/shell_content_browser_client.h" | 5 #include "extensions/shell/browser/shell_content_browser_client.h" |
6 | 6 |
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" | 7 #include "base/command_line.h" |
11 #include "content/public/browser/browser_thread.h" | 8 #include "content/public/browser/browser_thread.h" |
12 #include "content/public/browser/render_process_host.h" | 9 #include "content/public/browser/render_process_host.h" |
13 #include "content/public/browser/site_instance.h" | 10 #include "content/public/browser/site_instance.h" |
14 #include "content/public/common/content_switches.h" | 11 #include "content/public/common/content_switches.h" |
15 #include "content/public/common/url_constants.h" | 12 #include "content/public/common/url_constants.h" |
16 #include "content/shell/browser/shell_browser_context.h" | 13 #include "content/shell/browser/shell_browser_context.h" |
17 #include "extensions/browser/extension_message_filter.h" | 14 #include "extensions/browser/extension_message_filter.h" |
18 #include "extensions/browser/extension_protocols.h" | 15 #include "extensions/browser/extension_protocols.h" |
19 #include "extensions/browser/extension_registry.h" | 16 #include "extensions/browser/extension_registry.h" |
20 #include "extensions/browser/info_map.h" | 17 #include "extensions/browser/info_map.h" |
21 #include "extensions/browser/process_map.h" | 18 #include "extensions/browser/process_map.h" |
22 #include "extensions/common/constants.h" | 19 #include "extensions/common/constants.h" |
23 #include "extensions/common/extension.h" | 20 #include "extensions/common/extension.h" |
24 #include "extensions/common/switches.h" | 21 #include "extensions/common/switches.h" |
| 22 #include "extensions/shell/browser/shell_browser_context.h" |
| 23 #include "extensions/shell/browser/shell_browser_main_parts.h" |
| 24 #include "extensions/shell/browser/shell_extension_system.h" |
25 #include "url/gurl.h" | 25 #include "url/gurl.h" |
26 | 26 |
27 using content::BrowserThread; | 27 using content::BrowserThread; |
28 using extensions::ExtensionRegistry; | |
29 | 28 |
30 namespace apps { | 29 namespace extensions { |
31 namespace { | 30 namespace { |
32 | 31 |
33 ShellContentBrowserClient* g_instance = NULL; | 32 ShellContentBrowserClient* g_instance = NULL; |
34 | 33 |
35 } // namespace | 34 } // namespace |
36 | 35 |
37 ShellContentBrowserClient::ShellContentBrowserClient( | 36 ShellContentBrowserClient::ShellContentBrowserClient( |
38 ShellBrowserMainDelegate* browser_main_delegate) | 37 ShellBrowserMainDelegate* browser_main_delegate) |
39 : browser_main_parts_(NULL), browser_main_delegate_(browser_main_delegate) { | 38 : browser_main_parts_(NULL), browser_main_delegate_(browser_main_delegate) { |
40 DCHECK(!g_instance); | 39 DCHECK(!g_instance); |
41 g_instance = this; | 40 g_instance = this; |
42 } | 41 } |
43 | 42 |
44 ShellContentBrowserClient::~ShellContentBrowserClient() { g_instance = NULL; } | 43 ShellContentBrowserClient::~ShellContentBrowserClient() { |
| 44 g_instance = NULL; |
| 45 } |
45 | 46 |
46 // static | 47 // static |
47 ShellContentBrowserClient* ShellContentBrowserClient::Get() { | 48 ShellContentBrowserClient* ShellContentBrowserClient::Get() { |
48 return g_instance; | 49 return g_instance; |
49 } | 50 } |
50 | 51 |
51 content::BrowserContext* ShellContentBrowserClient::GetBrowserContext() { | 52 content::BrowserContext* ShellContentBrowserClient::GetBrowserContext() { |
52 return browser_main_parts_->browser_context(); | 53 return browser_main_parts_->browser_context(); |
53 } | 54 } |
54 | 55 |
55 content::BrowserMainParts* ShellContentBrowserClient::CreateBrowserMainParts( | 56 content::BrowserMainParts* ShellContentBrowserClient::CreateBrowserMainParts( |
56 const content::MainFunctionParams& parameters) { | 57 const content::MainFunctionParams& parameters) { |
57 browser_main_parts_ = | 58 browser_main_parts_ = |
58 new ShellBrowserMainParts(parameters, browser_main_delegate_); | 59 new ShellBrowserMainParts(parameters, browser_main_delegate_); |
59 return browser_main_parts_; | 60 return browser_main_parts_; |
60 } | 61 } |
61 | 62 |
62 void ShellContentBrowserClient::RenderProcessWillLaunch( | 63 void ShellContentBrowserClient::RenderProcessWillLaunch( |
63 content::RenderProcessHost* host) { | 64 content::RenderProcessHost* host) { |
64 int render_process_id = host->GetID(); | 65 int render_process_id = host->GetID(); |
65 host->AddFilter(new extensions::ExtensionMessageFilter( | 66 host->AddFilter(new ExtensionMessageFilter( |
66 render_process_id, browser_main_parts_->browser_context())); | 67 render_process_id, browser_main_parts_->browser_context())); |
67 } | 68 } |
68 | 69 |
69 bool ShellContentBrowserClient::ShouldUseProcessPerSite( | 70 bool ShellContentBrowserClient::ShouldUseProcessPerSite( |
70 content::BrowserContext* browser_context, | 71 content::BrowserContext* browser_context, |
71 const GURL& effective_url) { | 72 const GURL& effective_url) { |
72 // This ensures that all render views created for a single app will use the | 73 // This ensures that all render views created for a single app will use the |
73 // same render process (see content::SiteInstance::GetProcess). Otherwise the | 74 // same render process (see content::SiteInstance::GetProcess). Otherwise the |
74 // default behavior of ContentBrowserClient will lead to separate render | 75 // default behavior of ContentBrowserClient will lead to separate render |
75 // processes for the background page and each app window view. | 76 // processes for the background page and each app window view. |
76 return true; | 77 return true; |
77 } | 78 } |
78 | 79 |
79 net::URLRequestContextGetter* ShellContentBrowserClient::CreateRequestContext( | 80 net::URLRequestContextGetter* ShellContentBrowserClient::CreateRequestContext( |
80 content::BrowserContext* content_browser_context, | 81 content::BrowserContext* content_browser_context, |
81 content::ProtocolHandlerMap* protocol_handlers, | 82 content::ProtocolHandlerMap* protocol_handlers, |
82 content::URLRequestInterceptorScopedVector request_interceptors) { | 83 content::URLRequestInterceptorScopedVector request_interceptors) { |
83 // Handle only chrome-extension:// requests. app_shell does not support | 84 // Handle only chrome-extension:// requests. app_shell does not support |
84 // chrome-extension-resource:// requests (it does not store shared extension | 85 // chrome-extension-resource:// requests (it does not store shared extension |
85 // data in its installation directory). | 86 // data in its installation directory). |
86 extensions::InfoMap* extension_info_map = | 87 InfoMap* extension_info_map = |
87 browser_main_parts_->extension_system()->info_map(); | 88 browser_main_parts_->extension_system()->info_map(); |
88 (*protocol_handlers)[extensions::kExtensionScheme] = | 89 (*protocol_handlers)[kExtensionScheme] = |
89 linked_ptr<net::URLRequestJobFactory::ProtocolHandler>( | 90 linked_ptr<net::URLRequestJobFactory::ProtocolHandler>( |
90 extensions::CreateExtensionProtocolHandler(false /* is_incognito */, | 91 CreateExtensionProtocolHandler(false /* is_incognito */, |
91 extension_info_map)); | 92 extension_info_map)); |
92 // Let content::ShellBrowserContext handle the rest of the setup. | 93 // Let content::ShellBrowserContext handle the rest of the setup. |
93 return browser_main_parts_->browser_context()->CreateRequestContext( | 94 return browser_main_parts_->browser_context()->CreateRequestContext( |
94 protocol_handlers, request_interceptors.Pass()); | 95 protocol_handlers, request_interceptors.Pass()); |
95 } | 96 } |
96 | 97 |
97 bool ShellContentBrowserClient::IsHandledURL(const GURL& url) { | 98 bool ShellContentBrowserClient::IsHandledURL(const GURL& url) { |
98 if (!url.is_valid()) | 99 if (!url.is_valid()) |
99 return false; | 100 return false; |
100 // Keep in sync with ProtocolHandlers added in CreateRequestContext() and in | 101 // Keep in sync with ProtocolHandlers added in CreateRequestContext() and in |
101 // content::ShellURLRequestContextGetter::GetURLRequestContext(). | 102 // content::ShellURLRequestContextGetter::GetURLRequestContext(). |
102 static const char* const kProtocolList[] = { | 103 static const char* const kProtocolList[] = { |
103 url::kBlobScheme, | 104 url::kBlobScheme, |
104 content::kChromeDevToolsScheme, | 105 content::kChromeDevToolsScheme, |
105 content::kChromeUIScheme, | 106 content::kChromeUIScheme, |
106 url::kDataScheme, | 107 url::kDataScheme, |
107 url::kFileScheme, | 108 url::kFileScheme, |
108 url::kFileSystemScheme, | 109 url::kFileSystemScheme, |
109 extensions::kExtensionScheme, | 110 kExtensionScheme, |
110 extensions::kExtensionResourceScheme, | 111 kExtensionResourceScheme, |
111 }; | 112 }; |
112 for (size_t i = 0; i < arraysize(kProtocolList); ++i) { | 113 for (size_t i = 0; i < arraysize(kProtocolList); ++i) { |
113 if (url.scheme() == kProtocolList[i]) | 114 if (url.scheme() == kProtocolList[i]) |
114 return true; | 115 return true; |
115 } | 116 } |
116 return false; | 117 return false; |
117 } | 118 } |
118 | 119 |
119 void ShellContentBrowserClient::SiteInstanceGotProcess( | 120 void ShellContentBrowserClient::SiteInstanceGotProcess( |
120 content::SiteInstance* site_instance) { | 121 content::SiteInstance* site_instance) { |
121 // If this isn't an extension renderer there's nothing to do. | 122 // If this isn't an extension renderer there's nothing to do. |
122 const extensions::Extension* extension = GetExtension(site_instance); | 123 const Extension* extension = GetExtension(site_instance); |
123 if (!extension) | 124 if (!extension) |
124 return; | 125 return; |
125 | 126 |
126 extensions::ProcessMap::Get(browser_main_parts_->browser_context()) | 127 ProcessMap::Get(browser_main_parts_->browser_context()) |
127 ->Insert(extension->id(), | 128 ->Insert(extension->id(), |
128 site_instance->GetProcess()->GetID(), | 129 site_instance->GetProcess()->GetID(), |
129 site_instance->GetId()); | 130 site_instance->GetId()); |
130 | 131 |
131 BrowserThread::PostTask( | 132 BrowserThread::PostTask( |
132 BrowserThread::IO, | 133 BrowserThread::IO, |
133 FROM_HERE, | 134 FROM_HERE, |
134 base::Bind(&extensions::InfoMap::RegisterExtensionProcess, | 135 base::Bind(&InfoMap::RegisterExtensionProcess, |
135 browser_main_parts_->extension_system()->info_map(), | 136 browser_main_parts_->extension_system()->info_map(), |
136 extension->id(), | 137 extension->id(), |
137 site_instance->GetProcess()->GetID(), | 138 site_instance->GetProcess()->GetID(), |
138 site_instance->GetId())); | 139 site_instance->GetId())); |
139 } | 140 } |
140 | 141 |
141 void ShellContentBrowserClient::SiteInstanceDeleting( | 142 void ShellContentBrowserClient::SiteInstanceDeleting( |
142 content::SiteInstance* site_instance) { | 143 content::SiteInstance* site_instance) { |
143 // If this isn't an extension renderer there's nothing to do. | 144 // If this isn't an extension renderer there's nothing to do. |
144 const extensions::Extension* extension = GetExtension(site_instance); | 145 const Extension* extension = GetExtension(site_instance); |
145 if (!extension) | 146 if (!extension) |
146 return; | 147 return; |
147 | 148 |
148 extensions::ProcessMap::Get(browser_main_parts_->browser_context()) | 149 ProcessMap::Get(browser_main_parts_->browser_context()) |
149 ->Remove(extension->id(), | 150 ->Remove(extension->id(), |
150 site_instance->GetProcess()->GetID(), | 151 site_instance->GetProcess()->GetID(), |
151 site_instance->GetId()); | 152 site_instance->GetId()); |
152 | 153 |
153 BrowserThread::PostTask( | 154 BrowserThread::PostTask( |
154 BrowserThread::IO, | 155 BrowserThread::IO, |
155 FROM_HERE, | 156 FROM_HERE, |
156 base::Bind(&extensions::InfoMap::UnregisterExtensionProcess, | 157 base::Bind(&InfoMap::UnregisterExtensionProcess, |
157 browser_main_parts_->extension_system()->info_map(), | 158 browser_main_parts_->extension_system()->info_map(), |
158 extension->id(), | 159 extension->id(), |
159 site_instance->GetProcess()->GetID(), | 160 site_instance->GetProcess()->GetID(), |
160 site_instance->GetId())); | 161 site_instance->GetId())); |
161 } | 162 } |
162 | 163 |
163 void ShellContentBrowserClient::AppendExtraCommandLineSwitches( | 164 void ShellContentBrowserClient::AppendExtraCommandLineSwitches( |
164 CommandLine* command_line, int child_process_id) { | 165 CommandLine* command_line, |
| 166 int child_process_id) { |
165 std::string process_type = | 167 std::string process_type = |
166 command_line->GetSwitchValueASCII(switches::kProcessType); | 168 command_line->GetSwitchValueASCII(::switches::kProcessType); |
167 if (process_type == switches::kRendererProcess) { | 169 if (process_type == ::switches::kRendererProcess) { |
168 // TODO(jamescook): Should we check here if the process is in the extension | 170 // TODO(jamescook): Should we check here if the process is in the extension |
169 // service process map, or can we assume all renderers are extension | 171 // service process map, or can we assume all renderers are extension |
170 // renderers? | 172 // renderers? |
171 command_line->AppendSwitch(extensions::switches::kExtensionProcess); | 173 command_line->AppendSwitch(switches::kExtensionProcess); |
172 } | 174 } |
173 } | 175 } |
174 | 176 |
175 void ShellContentBrowserClient::GetAdditionalAllowedSchemesForFileSystem( | 177 void ShellContentBrowserClient::GetAdditionalAllowedSchemesForFileSystem( |
176 std::vector<std::string>* additional_allowed_schemes) { | 178 std::vector<std::string>* additional_allowed_schemes) { |
177 ContentBrowserClient::GetAdditionalAllowedSchemesForFileSystem( | 179 ContentBrowserClient::GetAdditionalAllowedSchemesForFileSystem( |
178 additional_allowed_schemes); | 180 additional_allowed_schemes); |
179 additional_allowed_schemes->push_back(extensions::kExtensionScheme); | 181 additional_allowed_schemes->push_back(kExtensionScheme); |
180 } | 182 } |
181 | 183 |
182 const extensions::Extension* ShellContentBrowserClient::GetExtension( | 184 const Extension* ShellContentBrowserClient::GetExtension( |
183 content::SiteInstance* site_instance) { | 185 content::SiteInstance* site_instance) { |
184 ExtensionRegistry* registry = | 186 ExtensionRegistry* registry = |
185 ExtensionRegistry::Get(site_instance->GetBrowserContext()); | 187 ExtensionRegistry::Get(site_instance->GetBrowserContext()); |
186 return registry->enabled_extensions().GetExtensionOrAppByURL( | 188 return registry->enabled_extensions().GetExtensionOrAppByURL( |
187 site_instance->GetSiteURL()); | 189 site_instance->GetSiteURL()); |
188 } | 190 } |
189 | 191 |
190 } // namespace apps | 192 } // namespace extensions |
OLD | NEW |