blob: 47ba71188fccf83bddc266eea58007eb8db95730 [file] [log] [blame]
[email protected]d977f9c2011-03-14 16:10:261// Copyright (c) 2011 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.
4
5#include "chrome/browser/chrome_content_browser_client.h"
6
[email protected]b80f68432011-05-02 17:22:307#include "base/command_line.h"
8#include "chrome/app/breakpad_mac.h"
9#include "chrome/browser/browser_process.h"
[email protected]763ec4ca2011-04-29 15:48:1210#include "chrome/browser/character_encoding.h"
[email protected]97e6c4c2011-05-18 16:08:5111#include "chrome/browser/chrome_plugin_message_filter.h"
[email protected]5327dfb2011-05-03 17:50:3612#include "chrome/browser/chrome_worker_message_filter.h"
[email protected]a2176792011-05-08 19:30:4913#include "chrome/browser/content_settings/host_content_settings_map.h"
[email protected]8093a542011-05-13 07:29:3214#include "chrome/browser/content_settings/tab_specific_content_settings.h"
[email protected]f364d1392011-04-08 21:03:1015#include "chrome/browser/debugger/devtools_handler.h"
16#include "chrome/browser/desktop_notification_handler.h"
[email protected]f364d1392011-04-08 21:03:1017#include "chrome/browser/extensions/extension_message_handler.h"
[email protected]d977f9c2011-03-14 16:10:2618#include "chrome/browser/extensions/extension_service.h"
[email protected]763ec4ca2011-04-29 15:48:1219#include "chrome/browser/google/google_util.h"
20#include "chrome/browser/prefs/pref_service.h"
[email protected]05fcf982011-04-19 00:44:1421#include "chrome/browser/printing/printing_message_filter.h"
22#include "chrome/browser/profiles/profile.h"
[email protected]8093a542011-05-13 07:29:3223#include "chrome/browser/profiles/profile_io_data.h"
[email protected]05fcf982011-04-19 00:44:1424#include "chrome/browser/renderer_host/chrome_render_message_filter.h"
[email protected]53a0afa2011-04-28 02:09:3325#include "chrome/browser/renderer_host/chrome_render_view_host_observer.h"
[email protected]d4cff272011-05-02 15:46:0126#include "chrome/browser/renderer_host/text_input_client_message_filter.h"
[email protected]05fcf982011-04-19 00:44:1427#include "chrome/browser/search_engines/search_provider_install_state_message_filter.h"
28#include "chrome/browser/spellcheck_message_filter.h"
[email protected]1fd1a502011-03-30 16:55:5629#include "chrome/browser/ui/webui/chrome_web_ui_factory.h"
[email protected]b80f68432011-05-02 17:22:3030#include "chrome/common/child_process_logging.h"
31#include "chrome/common/chrome_switches.h"
[email protected]c5dbef02011-05-13 05:06:0932#include "chrome/common/extensions/extension_messages.h"
[email protected]763ec4ca2011-04-29 15:48:1233#include "chrome/common/pref_names.h"
[email protected]c5dbef02011-05-13 05:06:0934#include "chrome/common/url_constants.h"
35#include "content/browser/browsing_instance.h"
36#include "content/browser/child_process_security_policy.h"
[email protected]97e6c4c2011-05-18 16:08:5137#include "content/browser/plugin_process_host.h"
[email protected]05fcf982011-04-19 00:44:1438#include "content/browser/renderer_host/browser_render_process_host.h"
[email protected]d977f9c2011-03-14 16:10:2639#include "content/browser/renderer_host/render_view_host.h"
[email protected]a2176792011-05-08 19:30:4940#include "content/browser/resource_context.h"
[email protected]c5dbef02011-05-13 05:06:0941#include "content/browser/site_instance.h"
[email protected]763ec4ca2011-04-29 15:48:1242#include "content/browser/tab_contents/tab_contents.h"
[email protected]5327dfb2011-05-03 17:50:3643#include "content/browser/worker_host/worker_process_host.h"
[email protected]c5dbef02011-05-13 05:06:0944#include "content/common/bindings_policy.h"
[email protected]ed24fad2011-05-10 22:44:0145#include "net/base/cookie_monster.h"
46#include "net/base/cookie_options.h"
47#include "net/base/static_cookie_policy.h"
[email protected]d977f9c2011-03-14 16:10:2648
[email protected]b80f68432011-05-02 17:22:3049#if defined(OS_LINUX)
50#include "base/linux_util.h"
51#include "chrome/browser/crash_handler_host_linux.h"
52#endif // OS_LINUX
53
[email protected]c5dbef02011-05-13 05:06:0954namespace {
55
56void InitRenderViewHostForExtensions(RenderViewHost* render_view_host) {
57 // Note that due to GetEffectiveURL(), even hosted apps will have a
58 // chrome-extension:// URL for their site, so we can ignore that wrinkle here.
59 SiteInstance* site_instance = render_view_host->site_instance();
60 const GURL& site = site_instance->site();
61 RenderProcessHost* process = render_view_host->process();
62
63 if (!site.SchemeIs(chrome::kExtensionScheme))
64 return;
65
66 Profile* profile = site_instance->browsing_instance()->profile();
67 ExtensionService* service = profile->GetExtensionService();
68 if (!service)
69 return;
70
71 ExtensionProcessManager* process_manager =
72 profile->GetExtensionProcessManager();
73 CHECK(process_manager);
74
75 // This can happen if somebody typos a chrome-extension:// URL.
76 const Extension* extension = service->GetExtensionByURL(site);
77 if (!extension)
78 return;
79
80 site_instance->GetProcess()->mark_is_extension_process();
81
82 // Register the association between extension and process with
83 // ExtensionProcessManager.
84 process_manager->RegisterExtensionProcess(extension->id(), process->id());
85
86 // Record which, if any, installed app is associated with this process.
87 // TODO(aa): Totally lame to store this state in a global map in extension
88 // service. Can we get it from EPM instead?
89 if (extension->is_app())
90 service->SetInstalledAppForRenderer(process->id(), extension);
91
92 // Some extensions use chrome:// URLs.
93 Extension::Type type = extension->GetType();
94 if (type == Extension::TYPE_EXTENSION ||
95 type == Extension::TYPE_PACKAGED_APP) {
96 ChildProcessSecurityPolicy::GetInstance()->GrantScheme(
97 process->id(), chrome::kChromeUIScheme);
98 }
99
100 // Enable extension bindings for the renderer. Currently only extensions,
101 // packaged apps, and hosted component apps use extension bindings.
102 if (type == Extension::TYPE_EXTENSION ||
[email protected]2333bf22011-05-19 02:16:51103 type == Extension::TYPE_USER_SCRIPT ||
[email protected]c5dbef02011-05-13 05:06:09104 type == Extension::TYPE_PACKAGED_APP ||
105 (type == Extension::TYPE_HOSTED_APP &&
106 extension->location() == Extension::COMPONENT)) {
107 render_view_host->Send(new ExtensionMsg_ActivateExtension(extension->id()));
108 render_view_host->AllowBindings(BindingsPolicy::EXTENSION);
109 }
110}
111
112}
113
[email protected]d977f9c2011-03-14 16:10:26114namespace chrome {
115
[email protected]f364d1392011-04-08 21:03:10116void ChromeContentBrowserClient::RenderViewHostCreated(
117 RenderViewHost* render_view_host) {
[email protected]53a0afa2011-04-28 02:09:33118 new ChromeRenderViewHostObserver(render_view_host);
[email protected]f364d1392011-04-08 21:03:10119 new DesktopNotificationHandler(render_view_host);
120 new DevToolsHandler(render_view_host);
121 new ExtensionMessageHandler(render_view_host);
[email protected]f364d1392011-04-08 21:03:10122
[email protected]c5dbef02011-05-13 05:06:09123 InitRenderViewHostForExtensions(render_view_host);
[email protected]d977f9c2011-03-14 16:10:26124}
125
[email protected]05fcf982011-04-19 00:44:14126void ChromeContentBrowserClient::BrowserRenderProcessHostCreated(
127 BrowserRenderProcessHost* host) {
[email protected]c47cfd62011-04-29 21:27:02128 int id = host->id();
129 Profile* profile = host->profile();
[email protected]05fcf982011-04-19 00:44:14130 host->channel()->AddFilter(new ChromeRenderMessageFilter(
[email protected]c47cfd62011-04-29 21:27:02131 id, profile, profile->GetRequestContextForRenderProcess(id)));
[email protected]05fcf982011-04-19 00:44:14132 host->channel()->AddFilter(new PrintingMessageFilter());
133 host->channel()->AddFilter(
[email protected]c47cfd62011-04-29 21:27:02134 new SearchProviderInstallStateMessageFilter(id, profile));
135 host->channel()->AddFilter(new SpellCheckMessageFilter(id));
[email protected]d4cff272011-05-02 15:46:01136#if defined(OS_MACOSX)
137 host->channel()->AddFilter(new TextInputClientMessageFilter(host->id()));
138#endif
[email protected]05fcf982011-04-19 00:44:14139}
140
[email protected]97e6c4c2011-05-18 16:08:51141void ChromeContentBrowserClient::PluginProcessHostCreated(
142 PluginProcessHost* host) {
143 host->AddFilter(new ChromePluginMessageFilter(host));
144}
145
[email protected]5327dfb2011-05-03 17:50:36146void ChromeContentBrowserClient::WorkerProcessHostCreated(
147 WorkerProcessHost* host) {
148 host->AddFilter(new ChromeWorkerMessageFilter(host));
149}
150
[email protected]1fd1a502011-03-30 16:55:56151content::WebUIFactory* ChromeContentBrowserClient::GetWebUIFactory() {
152 return ChromeWebUIFactory::GetInstance();
153}
154
[email protected]36fb2c7c2011-04-04 15:49:08155GURL ChromeContentBrowserClient::GetEffectiveURL(Profile* profile,
156 const GURL& url) {
157 // Get the effective URL for the given actual URL. If the URL is part of an
158 // installed app, the effective URL is an extension URL with the ID of that
159 // extension as the host. This has the effect of grouping apps together in
160 // a common SiteInstance.
161 if (!profile || !profile->GetExtensionService())
162 return url;
163
164 const Extension* extension =
165 profile->GetExtensionService()->GetExtensionByWebExtent(url);
166 if (!extension)
167 return url;
168
169 // If the URL is part of an extension's web extent, convert it to an
170 // extension URL.
171 return extension->GetResourceURL(url.path());
172}
173
[email protected]0f012df82011-05-19 14:15:29174bool ChromeContentBrowserClient::IsURLSameAsAnySiteInstance(const GURL& url) {
175 return url.spec() == chrome::kAboutKillURL ||
176 url.spec() == chrome::kAboutHangURL ||
177 url.spec() == chrome::kAboutShorthangURL;
178}
179
[email protected]763ec4ca2011-04-29 15:48:12180std::string ChromeContentBrowserClient::GetCanonicalEncodingNameByAliasName(
181 const std::string& alias_name) {
182 return CharacterEncoding::GetCanonicalEncodingNameByAliasName(alias_name);
183}
184
[email protected]b80f68432011-05-02 17:22:30185void ChromeContentBrowserClient::AppendExtraCommandLineSwitches(
186 CommandLine* command_line, int child_process_id) {
187#if defined(USE_LINUX_BREAKPAD)
188 if (IsCrashReporterEnabled()) {
189 command_line->AppendSwitchASCII(switches::kEnableCrashReporter,
190 child_process_logging::GetClientId() + "," + base::GetLinuxDistro());
191 }
192#elif defined(OS_MACOSX)
193 if (IsCrashReporterEnabled()) {
194 command_line->AppendSwitchASCII(switches::kEnableCrashReporter,
195 child_process_logging::GetClientId());
196 }
197#endif // OS_MACOSX
198
199 std::string process_type =
200 command_line->GetSwitchValueASCII(switches::kProcessType);
201 if (process_type == switches::kExtensionProcess ||
202 process_type == switches::kRendererProcess) {
203 const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess();
204 FilePath user_data_dir =
205 browser_command_line.GetSwitchValuePath(switches::kUserDataDir);
206 if (!user_data_dir.empty())
207 command_line->AppendSwitchPath(switches::kUserDataDir, user_data_dir);
208#if defined(OS_CHROMEOS)
209 const std::string& login_profile =
210 browser_command_line.GetSwitchValueASCII(switches::kLoginProfile);
211 if (!login_profile.empty())
212 command_line->AppendSwitchASCII(switches::kLoginProfile, login_profile);
213#endif
214
215 RenderProcessHost* process = RenderProcessHost::FromID(child_process_id);
216
217 PrefService* prefs = process->profile()->GetPrefs();
218 // Currently this pref is only registered if applied via a policy.
219 if (prefs->HasPrefPath(prefs::kDisable3DAPIs) &&
220 prefs->GetBoolean(prefs::kDisable3DAPIs)) {
221 // Turn this policy into a command line switch.
222 command_line->AppendSwitch(switches::kDisable3DAPIs);
223 }
224
225 // Disable client-side phishing detection in the renderer if it is disabled
226 // in the browser process.
227 if (!g_browser_process->safe_browsing_detection_service())
228 command_line->AppendSwitch(switches::kDisableClientSidePhishingDetection);
229 }
230}
231
232std::string ChromeContentBrowserClient::GetApplicationLocale() {
233 return g_browser_process->GetApplicationLocale();
234}
235
[email protected]b5cca982011-05-26 04:42:08236std::string ChromeContentBrowserClient::GetAcceptLangs(const TabContents* tab) {
237 return tab->profile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
238}
239
[email protected]a2176792011-05-08 19:30:49240bool ChromeContentBrowserClient::AllowAppCache(
[email protected]ed24fad2011-05-10 22:44:01241 const GURL& manifest_url, const content::ResourceContext& context) {
[email protected]8093a542011-05-13 07:29:32242 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
243 ProfileIOData* io_data =
244 reinterpret_cast<ProfileIOData*>(context.GetUserData(NULL));
[email protected]efa55212011-05-13 16:19:38245 ContentSetting setting = io_data->GetHostContentSettingsMap()->
[email protected]a2176792011-05-08 19:30:49246 GetContentSetting(manifest_url, CONTENT_SETTINGS_TYPE_COOKIES, "");
247 DCHECK(setting != CONTENT_SETTING_DEFAULT);
248 return setting != CONTENT_SETTING_BLOCK;
249}
250
[email protected]ed24fad2011-05-10 22:44:01251bool ChromeContentBrowserClient::AllowGetCookie(
252 const GURL& url,
253 const GURL& first_party,
254 const net::CookieList& cookie_list,
255 const content::ResourceContext& context,
256 int render_process_id,
257 int render_view_id) {
258 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
259 bool allow = true;
[email protected]8093a542011-05-13 07:29:32260 ProfileIOData* io_data =
261 reinterpret_cast<ProfileIOData*>(context.GetUserData(NULL));
[email protected]efa55212011-05-13 16:19:38262 if (io_data->GetHostContentSettingsMap()->BlockThirdPartyCookies()) {
[email protected]ed24fad2011-05-10 22:44:01263 bool strict = CommandLine::ForCurrentProcess()->HasSwitch(
264 switches::kBlockReadingThirdPartyCookies);
265 net::StaticCookiePolicy policy(strict ?
266 net::StaticCookiePolicy::BLOCK_ALL_THIRD_PARTY_COOKIES :
267 net::StaticCookiePolicy::BLOCK_SETTING_THIRD_PARTY_COOKIES);
268 int rv = policy.CanGetCookies(url, first_party);
269 DCHECK_NE(net::ERR_IO_PENDING, rv);
270 if (rv != net::OK)
271 allow = false;
272 }
273
274 if (allow) {
[email protected]efa55212011-05-13 16:19:38275 ContentSetting setting = io_data->GetHostContentSettingsMap()->
[email protected]ed24fad2011-05-10 22:44:01276 GetContentSetting(url, CONTENT_SETTINGS_TYPE_COOKIES, "");
277 allow = setting == CONTENT_SETTING_ALLOW ||
278 setting == CONTENT_SETTING_SESSION_ONLY;
279 }
280
[email protected]8093a542011-05-13 07:29:32281 BrowserThread::PostTask(
282 BrowserThread::UI, FROM_HERE,
283 NewRunnableFunction(
284 &TabSpecificContentSettings::CookiesRead,
285 render_process_id, render_view_id, url, cookie_list, !allow));
[email protected]ed24fad2011-05-10 22:44:01286 return allow;
287}
288
289bool ChromeContentBrowserClient::AllowSetCookie(
290 const GURL& url,
291 const GURL& first_party,
292 const std::string& cookie_line,
293 const content::ResourceContext& context,
294 int render_process_id,
295 int render_view_id,
296 net::CookieOptions* options) {
297 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
298 bool allow = true;
[email protected]8093a542011-05-13 07:29:32299 ProfileIOData* io_data =
300 reinterpret_cast<ProfileIOData*>(context.GetUserData(NULL));
[email protected]efa55212011-05-13 16:19:38301 if (io_data->GetHostContentSettingsMap()->BlockThirdPartyCookies()) {
[email protected]ed24fad2011-05-10 22:44:01302 bool strict = CommandLine::ForCurrentProcess()->HasSwitch(
303 switches::kBlockReadingThirdPartyCookies);
304 net::StaticCookiePolicy policy(strict ?
305 net::StaticCookiePolicy::BLOCK_ALL_THIRD_PARTY_COOKIES :
306 net::StaticCookiePolicy::BLOCK_SETTING_THIRD_PARTY_COOKIES);
307 int rv = policy.CanSetCookie(url, first_party, cookie_line);
308 if (rv != net::OK)
309 allow = false;
310 }
311
312 if (allow) {
[email protected]efa55212011-05-13 16:19:38313 ContentSetting setting = io_data->GetHostContentSettingsMap()->
[email protected]ed24fad2011-05-10 22:44:01314 GetContentSetting(url, CONTENT_SETTINGS_TYPE_COOKIES, "");
315
316 if (setting == CONTENT_SETTING_SESSION_ONLY)
317 options->set_force_session();
318
319 allow = setting == CONTENT_SETTING_ALLOW ||
320 setting == CONTENT_SETTING_SESSION_ONLY;
321 }
322
[email protected]8093a542011-05-13 07:29:32323 BrowserThread::PostTask(
324 BrowserThread::UI, FROM_HERE,
325 NewRunnableFunction(
326 &TabSpecificContentSettings::CookieChanged,
327 render_process_id, render_view_id, url, cookie_line, *options,
328 !allow));
[email protected]ed24fad2011-05-10 22:44:01329 return allow;
330}
331
[email protected]b80f68432011-05-02 17:22:30332#if defined(OS_LINUX)
333int ChromeContentBrowserClient::GetCrashSignalFD(
334 const std::string& process_type) {
335 if (process_type == switches::kRendererProcess)
336 return RendererCrashHandlerHostLinux::GetInstance()->GetDeathSignalSocket();
337
338 if (process_type == switches::kPluginProcess)
339 return PluginCrashHandlerHostLinux::GetInstance()->GetDeathSignalSocket();
340
341 if (process_type == switches::kPpapiPluginProcess)
342 return PpapiCrashHandlerHostLinux::GetInstance()->GetDeathSignalSocket();
343
344 if (process_type == switches::kGpuProcess)
345 return GpuCrashHandlerHostLinux::GetInstance()->GetDeathSignalSocket();
346
347 return -1;
348}
349#endif
350
[email protected]d977f9c2011-03-14 16:10:26351} // namespace chrome