blob: 50307688bc94ce16d37c6d05579e925b8b662733 [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 ||
103 type == Extension::TYPE_PACKAGED_APP ||
104 (type == Extension::TYPE_HOSTED_APP &&
105 extension->location() == Extension::COMPONENT)) {
106 render_view_host->Send(new ExtensionMsg_ActivateExtension(extension->id()));
107 render_view_host->AllowBindings(BindingsPolicy::EXTENSION);
108 }
109}
110
111}
112
[email protected]d977f9c2011-03-14 16:10:26113namespace chrome {
114
[email protected]f364d1392011-04-08 21:03:10115void ChromeContentBrowserClient::RenderViewHostCreated(
116 RenderViewHost* render_view_host) {
[email protected]53a0afa2011-04-28 02:09:33117 new ChromeRenderViewHostObserver(render_view_host);
[email protected]f364d1392011-04-08 21:03:10118 new DesktopNotificationHandler(render_view_host);
119 new DevToolsHandler(render_view_host);
120 new ExtensionMessageHandler(render_view_host);
[email protected]f364d1392011-04-08 21:03:10121
[email protected]c5dbef02011-05-13 05:06:09122 InitRenderViewHostForExtensions(render_view_host);
[email protected]d977f9c2011-03-14 16:10:26123}
124
[email protected]05fcf982011-04-19 00:44:14125void ChromeContentBrowserClient::BrowserRenderProcessHostCreated(
126 BrowserRenderProcessHost* host) {
[email protected]c47cfd62011-04-29 21:27:02127 int id = host->id();
128 Profile* profile = host->profile();
[email protected]05fcf982011-04-19 00:44:14129 host->channel()->AddFilter(new ChromeRenderMessageFilter(
[email protected]c47cfd62011-04-29 21:27:02130 id, profile, profile->GetRequestContextForRenderProcess(id)));
[email protected]05fcf982011-04-19 00:44:14131 host->channel()->AddFilter(new PrintingMessageFilter());
132 host->channel()->AddFilter(
[email protected]c47cfd62011-04-29 21:27:02133 new SearchProviderInstallStateMessageFilter(id, profile));
134 host->channel()->AddFilter(new SpellCheckMessageFilter(id));
[email protected]d4cff272011-05-02 15:46:01135#if defined(OS_MACOSX)
136 host->channel()->AddFilter(new TextInputClientMessageFilter(host->id()));
137#endif
[email protected]05fcf982011-04-19 00:44:14138}
139
[email protected]97e6c4c2011-05-18 16:08:51140void ChromeContentBrowserClient::PluginProcessHostCreated(
141 PluginProcessHost* host) {
142 host->AddFilter(new ChromePluginMessageFilter(host));
143}
144
[email protected]5327dfb2011-05-03 17:50:36145void ChromeContentBrowserClient::WorkerProcessHostCreated(
146 WorkerProcessHost* host) {
147 host->AddFilter(new ChromeWorkerMessageFilter(host));
148}
149
[email protected]1fd1a502011-03-30 16:55:56150content::WebUIFactory* ChromeContentBrowserClient::GetWebUIFactory() {
151 return ChromeWebUIFactory::GetInstance();
152}
153
[email protected]36fb2c7c2011-04-04 15:49:08154GURL ChromeContentBrowserClient::GetEffectiveURL(Profile* profile,
155 const GURL& url) {
156 // Get the effective URL for the given actual URL. If the URL is part of an
157 // installed app, the effective URL is an extension URL with the ID of that
158 // extension as the host. This has the effect of grouping apps together in
159 // a common SiteInstance.
160 if (!profile || !profile->GetExtensionService())
161 return url;
162
163 const Extension* extension =
164 profile->GetExtensionService()->GetExtensionByWebExtent(url);
165 if (!extension)
166 return url;
167
168 // If the URL is part of an extension's web extent, convert it to an
169 // extension URL.
170 return extension->GetResourceURL(url.path());
171}
172
[email protected]763ec4ca2011-04-29 15:48:12173GURL ChromeContentBrowserClient::GetAlternateErrorPageURL(
174 const TabContents* tab) {
175 GURL url;
176 // Disable alternate error pages when in OffTheRecord/Incognito mode.
177 if (tab->profile()->IsOffTheRecord())
178 return url;
179
180 PrefService* prefs = tab->profile()->GetPrefs();
181 DCHECK(prefs);
182 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
183 url = google_util::AppendGoogleLocaleParam(
184 GURL(google_util::kLinkDoctorBaseURL));
185 url = google_util::AppendGoogleTLDParam(url);
186 }
187 return url;
188}
189
190std::string ChromeContentBrowserClient::GetCanonicalEncodingNameByAliasName(
191 const std::string& alias_name) {
192 return CharacterEncoding::GetCanonicalEncodingNameByAliasName(alias_name);
193}
194
[email protected]b80f68432011-05-02 17:22:30195void ChromeContentBrowserClient::AppendExtraCommandLineSwitches(
196 CommandLine* command_line, int child_process_id) {
197#if defined(USE_LINUX_BREAKPAD)
198 if (IsCrashReporterEnabled()) {
199 command_line->AppendSwitchASCII(switches::kEnableCrashReporter,
200 child_process_logging::GetClientId() + "," + base::GetLinuxDistro());
201 }
202#elif defined(OS_MACOSX)
203 if (IsCrashReporterEnabled()) {
204 command_line->AppendSwitchASCII(switches::kEnableCrashReporter,
205 child_process_logging::GetClientId());
206 }
207#endif // OS_MACOSX
208
209 std::string process_type =
210 command_line->GetSwitchValueASCII(switches::kProcessType);
211 if (process_type == switches::kExtensionProcess ||
212 process_type == switches::kRendererProcess) {
213 const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess();
214 FilePath user_data_dir =
215 browser_command_line.GetSwitchValuePath(switches::kUserDataDir);
216 if (!user_data_dir.empty())
217 command_line->AppendSwitchPath(switches::kUserDataDir, user_data_dir);
218#if defined(OS_CHROMEOS)
219 const std::string& login_profile =
220 browser_command_line.GetSwitchValueASCII(switches::kLoginProfile);
221 if (!login_profile.empty())
222 command_line->AppendSwitchASCII(switches::kLoginProfile, login_profile);
223#endif
224
225 RenderProcessHost* process = RenderProcessHost::FromID(child_process_id);
226
227 PrefService* prefs = process->profile()->GetPrefs();
228 // Currently this pref is only registered if applied via a policy.
229 if (prefs->HasPrefPath(prefs::kDisable3DAPIs) &&
230 prefs->GetBoolean(prefs::kDisable3DAPIs)) {
231 // Turn this policy into a command line switch.
232 command_line->AppendSwitch(switches::kDisable3DAPIs);
233 }
234
235 // Disable client-side phishing detection in the renderer if it is disabled
236 // in the browser process.
237 if (!g_browser_process->safe_browsing_detection_service())
238 command_line->AppendSwitch(switches::kDisableClientSidePhishingDetection);
239 }
240}
241
242std::string ChromeContentBrowserClient::GetApplicationLocale() {
243 return g_browser_process->GetApplicationLocale();
244}
245
[email protected]a2176792011-05-08 19:30:49246bool ChromeContentBrowserClient::AllowAppCache(
[email protected]ed24fad2011-05-10 22:44:01247 const GURL& manifest_url, const content::ResourceContext& context) {
[email protected]8093a542011-05-13 07:29:32248 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
249 ProfileIOData* io_data =
250 reinterpret_cast<ProfileIOData*>(context.GetUserData(NULL));
[email protected]efa55212011-05-13 16:19:38251 ContentSetting setting = io_data->GetHostContentSettingsMap()->
[email protected]a2176792011-05-08 19:30:49252 GetContentSetting(manifest_url, CONTENT_SETTINGS_TYPE_COOKIES, "");
253 DCHECK(setting != CONTENT_SETTING_DEFAULT);
254 return setting != CONTENT_SETTING_BLOCK;
255}
256
[email protected]ed24fad2011-05-10 22:44:01257bool ChromeContentBrowserClient::AllowGetCookie(
258 const GURL& url,
259 const GURL& first_party,
260 const net::CookieList& cookie_list,
261 const content::ResourceContext& context,
262 int render_process_id,
263 int render_view_id) {
264 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
265 bool allow = true;
[email protected]8093a542011-05-13 07:29:32266 ProfileIOData* io_data =
267 reinterpret_cast<ProfileIOData*>(context.GetUserData(NULL));
[email protected]efa55212011-05-13 16:19:38268 if (io_data->GetHostContentSettingsMap()->BlockThirdPartyCookies()) {
[email protected]ed24fad2011-05-10 22:44:01269 bool strict = CommandLine::ForCurrentProcess()->HasSwitch(
270 switches::kBlockReadingThirdPartyCookies);
271 net::StaticCookiePolicy policy(strict ?
272 net::StaticCookiePolicy::BLOCK_ALL_THIRD_PARTY_COOKIES :
273 net::StaticCookiePolicy::BLOCK_SETTING_THIRD_PARTY_COOKIES);
274 int rv = policy.CanGetCookies(url, first_party);
275 DCHECK_NE(net::ERR_IO_PENDING, rv);
276 if (rv != net::OK)
277 allow = false;
278 }
279
280 if (allow) {
[email protected]efa55212011-05-13 16:19:38281 ContentSetting setting = io_data->GetHostContentSettingsMap()->
[email protected]ed24fad2011-05-10 22:44:01282 GetContentSetting(url, CONTENT_SETTINGS_TYPE_COOKIES, "");
283 allow = setting == CONTENT_SETTING_ALLOW ||
284 setting == CONTENT_SETTING_SESSION_ONLY;
285 }
286
[email protected]8093a542011-05-13 07:29:32287 BrowserThread::PostTask(
288 BrowserThread::UI, FROM_HERE,
289 NewRunnableFunction(
290 &TabSpecificContentSettings::CookiesRead,
291 render_process_id, render_view_id, url, cookie_list, !allow));
[email protected]ed24fad2011-05-10 22:44:01292 return allow;
293}
294
295bool ChromeContentBrowserClient::AllowSetCookie(
296 const GURL& url,
297 const GURL& first_party,
298 const std::string& cookie_line,
299 const content::ResourceContext& context,
300 int render_process_id,
301 int render_view_id,
302 net::CookieOptions* options) {
303 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
304 bool allow = true;
[email protected]8093a542011-05-13 07:29:32305 ProfileIOData* io_data =
306 reinterpret_cast<ProfileIOData*>(context.GetUserData(NULL));
[email protected]efa55212011-05-13 16:19:38307 if (io_data->GetHostContentSettingsMap()->BlockThirdPartyCookies()) {
[email protected]ed24fad2011-05-10 22:44:01308 bool strict = CommandLine::ForCurrentProcess()->HasSwitch(
309 switches::kBlockReadingThirdPartyCookies);
310 net::StaticCookiePolicy policy(strict ?
311 net::StaticCookiePolicy::BLOCK_ALL_THIRD_PARTY_COOKIES :
312 net::StaticCookiePolicy::BLOCK_SETTING_THIRD_PARTY_COOKIES);
313 int rv = policy.CanSetCookie(url, first_party, cookie_line);
314 if (rv != net::OK)
315 allow = false;
316 }
317
318 if (allow) {
[email protected]efa55212011-05-13 16:19:38319 ContentSetting setting = io_data->GetHostContentSettingsMap()->
[email protected]ed24fad2011-05-10 22:44:01320 GetContentSetting(url, CONTENT_SETTINGS_TYPE_COOKIES, "");
321
322 if (setting == CONTENT_SETTING_SESSION_ONLY)
323 options->set_force_session();
324
325 allow = setting == CONTENT_SETTING_ALLOW ||
326 setting == CONTENT_SETTING_SESSION_ONLY;
327 }
328
[email protected]8093a542011-05-13 07:29:32329 BrowserThread::PostTask(
330 BrowserThread::UI, FROM_HERE,
331 NewRunnableFunction(
332 &TabSpecificContentSettings::CookieChanged,
333 render_process_id, render_view_id, url, cookie_line, *options,
334 !allow));
[email protected]ed24fad2011-05-10 22:44:01335 return allow;
336}
337
[email protected]b80f68432011-05-02 17:22:30338#if defined(OS_LINUX)
339int ChromeContentBrowserClient::GetCrashSignalFD(
340 const std::string& process_type) {
341 if (process_type == switches::kRendererProcess)
342 return RendererCrashHandlerHostLinux::GetInstance()->GetDeathSignalSocket();
343
344 if (process_type == switches::kPluginProcess)
345 return PluginCrashHandlerHostLinux::GetInstance()->GetDeathSignalSocket();
346
347 if (process_type == switches::kPpapiPluginProcess)
348 return PpapiCrashHandlerHostLinux::GetInstance()->GetDeathSignalSocket();
349
350 if (process_type == switches::kGpuProcess)
351 return GpuCrashHandlerHostLinux::GetInstance()->GetDeathSignalSocket();
352
353 return -1;
354}
355#endif
356
[email protected]d977f9c2011-03-14 16:10:26357} // namespace chrome