blob: 3204252915c7814e9f606e817abf917d639d251d [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"
[email protected]b80f68432011-05-02 17:22:308#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"
[email protected]b80f68432011-05-02 17:22:3031#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]3e69bc82011-05-26 23:22:3834#include "chrome/common/render_messages.h"
[email protected]c5dbef02011-05-13 05:06:0935#include "chrome/common/url_constants.h"
36#include "content/browser/browsing_instance.h"
37#include "content/browser/child_process_security_policy.h"
[email protected]97e6c4c2011-05-18 16:08:5138#include "content/browser/plugin_process_host.h"
[email protected]05fcf982011-04-19 00:44:1439#include "content/browser/renderer_host/browser_render_process_host.h"
[email protected]d977f9c2011-03-14 16:10:2640#include "content/browser/renderer_host/render_view_host.h"
[email protected]a2176792011-05-08 19:30:4941#include "content/browser/resource_context.h"
[email protected]c5dbef02011-05-13 05:06:0942#include "content/browser/site_instance.h"
[email protected]763ec4ca2011-04-29 15:48:1243#include "content/browser/tab_contents/tab_contents.h"
[email protected]5327dfb2011-05-03 17:50:3644#include "content/browser/worker_host/worker_process_host.h"
[email protected]c5dbef02011-05-13 05:06:0945#include "content/common/bindings_policy.h"
[email protected]ed24fad2011-05-10 22:44:0146#include "net/base/cookie_monster.h"
47#include "net/base/cookie_options.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"
[email protected]1fd5302c2011-05-28 04:06:4352#endif
[email protected]b80f68432011-05-02 17:22:3053
[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
[email protected]c5dbef02011-05-13 05:06:09112}
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]3e69bc82011-05-26 23:22:38139
140 host->Send(new ViewMsg_SetIsIncognitoProcess(profile->IsOffTheRecord()));
[email protected]05fcf982011-04-19 00:44:14141}
142
[email protected]97e6c4c2011-05-18 16:08:51143void ChromeContentBrowserClient::PluginProcessHostCreated(
144 PluginProcessHost* host) {
145 host->AddFilter(new ChromePluginMessageFilter(host));
146}
147
[email protected]5327dfb2011-05-03 17:50:36148void ChromeContentBrowserClient::WorkerProcessHostCreated(
149 WorkerProcessHost* host) {
150 host->AddFilter(new ChromeWorkerMessageFilter(host));
151}
152
[email protected]1fd1a502011-03-30 16:55:56153content::WebUIFactory* ChromeContentBrowserClient::GetWebUIFactory() {
154 return ChromeWebUIFactory::GetInstance();
155}
156
[email protected]36fb2c7c2011-04-04 15:49:08157GURL ChromeContentBrowserClient::GetEffectiveURL(Profile* profile,
158 const GURL& url) {
159 // Get the effective URL for the given actual URL. If the URL is part of an
160 // installed app, the effective URL is an extension URL with the ID of that
161 // extension as the host. This has the effect of grouping apps together in
162 // a common SiteInstance.
163 if (!profile || !profile->GetExtensionService())
164 return url;
165
166 const Extension* extension =
167 profile->GetExtensionService()->GetExtensionByWebExtent(url);
168 if (!extension)
169 return url;
170
171 // If the URL is part of an extension's web extent, convert it to an
172 // extension URL.
173 return extension->GetResourceURL(url.path());
174}
175
[email protected]0f012df82011-05-19 14:15:29176bool ChromeContentBrowserClient::IsURLSameAsAnySiteInstance(const GURL& url) {
177 return url.spec() == chrome::kAboutKillURL ||
178 url.spec() == chrome::kAboutHangURL ||
179 url.spec() == chrome::kAboutShorthangURL;
180}
181
[email protected]763ec4ca2011-04-29 15:48:12182std::string ChromeContentBrowserClient::GetCanonicalEncodingNameByAliasName(
183 const std::string& alias_name) {
184 return CharacterEncoding::GetCanonicalEncodingNameByAliasName(alias_name);
185}
186
[email protected]b80f68432011-05-02 17:22:30187void ChromeContentBrowserClient::AppendExtraCommandLineSwitches(
188 CommandLine* command_line, int child_process_id) {
189#if defined(USE_LINUX_BREAKPAD)
190 if (IsCrashReporterEnabled()) {
191 command_line->AppendSwitchASCII(switches::kEnableCrashReporter,
192 child_process_logging::GetClientId() + "," + base::GetLinuxDistro());
193 }
194#elif defined(OS_MACOSX)
195 if (IsCrashReporterEnabled()) {
196 command_line->AppendSwitchASCII(switches::kEnableCrashReporter,
197 child_process_logging::GetClientId());
198 }
199#endif // OS_MACOSX
200
201 std::string process_type =
202 command_line->GetSwitchValueASCII(switches::kProcessType);
203 if (process_type == switches::kExtensionProcess ||
204 process_type == switches::kRendererProcess) {
205 const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess();
206 FilePath user_data_dir =
207 browser_command_line.GetSwitchValuePath(switches::kUserDataDir);
208 if (!user_data_dir.empty())
209 command_line->AppendSwitchPath(switches::kUserDataDir, user_data_dir);
210#if defined(OS_CHROMEOS)
211 const std::string& login_profile =
212 browser_command_line.GetSwitchValueASCII(switches::kLoginProfile);
213 if (!login_profile.empty())
214 command_line->AppendSwitchASCII(switches::kLoginProfile, login_profile);
215#endif
216
217 RenderProcessHost* process = RenderProcessHost::FromID(child_process_id);
218
219 PrefService* prefs = process->profile()->GetPrefs();
220 // Currently this pref is only registered if applied via a policy.
221 if (prefs->HasPrefPath(prefs::kDisable3DAPIs) &&
222 prefs->GetBoolean(prefs::kDisable3DAPIs)) {
223 // Turn this policy into a command line switch.
224 command_line->AppendSwitch(switches::kDisable3DAPIs);
225 }
226
227 // Disable client-side phishing detection in the renderer if it is disabled
228 // in the browser process.
229 if (!g_browser_process->safe_browsing_detection_service())
230 command_line->AppendSwitch(switches::kDisableClientSidePhishingDetection);
231 }
232}
233
234std::string ChromeContentBrowserClient::GetApplicationLocale() {
235 return g_browser_process->GetApplicationLocale();
236}
237
[email protected]b5cca982011-05-26 04:42:08238std::string ChromeContentBrowserClient::GetAcceptLangs(const TabContents* tab) {
239 return tab->profile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
240}
241
[email protected]a2176792011-05-08 19:30:49242bool ChromeContentBrowserClient::AllowAppCache(
[email protected]5b52ad42011-05-26 14:26:09243 const GURL& manifest_url,
244 const content::ResourceContext& context) {
[email protected]8093a542011-05-13 07:29:32245 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
246 ProfileIOData* io_data =
247 reinterpret_cast<ProfileIOData*>(context.GetUserData(NULL));
[email protected]5b52ad42011-05-26 14:26:09248 // FIXME(jochen): get the correct top-level origin.
[email protected]efa55212011-05-13 16:19:38249 ContentSetting setting = io_data->GetHostContentSettingsMap()->
[email protected]5b52ad42011-05-26 14:26:09250 GetCookieContentSetting(manifest_url, manifest_url, true);
[email protected]a2176792011-05-08 19:30:49251 DCHECK(setting != CONTENT_SETTING_DEFAULT);
252 return setting != CONTENT_SETTING_BLOCK;
253}
254
[email protected]ed24fad2011-05-10 22:44:01255bool ChromeContentBrowserClient::AllowGetCookie(
256 const GURL& url,
257 const GURL& first_party,
258 const net::CookieList& cookie_list,
259 const content::ResourceContext& context,
260 int render_process_id,
261 int render_view_id) {
262 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]8093a542011-05-13 07:29:32263 ProfileIOData* io_data =
264 reinterpret_cast<ProfileIOData*>(context.GetUserData(NULL));
[email protected]5b52ad42011-05-26 14:26:09265 ContentSetting setting = io_data->GetHostContentSettingsMap()->
266 GetCookieContentSetting(url, first_party, false);
267 bool allow = setting == CONTENT_SETTING_ALLOW ||
268 setting == CONTENT_SETTING_SESSION_ONLY;
[email protected]ed24fad2011-05-10 22:44:01269
[email protected]8093a542011-05-13 07:29:32270 BrowserThread::PostTask(
271 BrowserThread::UI, FROM_HERE,
272 NewRunnableFunction(
273 &TabSpecificContentSettings::CookiesRead,
274 render_process_id, render_view_id, url, cookie_list, !allow));
[email protected]ed24fad2011-05-10 22:44:01275 return allow;
276}
277
278bool ChromeContentBrowserClient::AllowSetCookie(
279 const GURL& url,
280 const GURL& first_party,
281 const std::string& cookie_line,
282 const content::ResourceContext& context,
283 int render_process_id,
284 int render_view_id,
285 net::CookieOptions* options) {
286 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
[email protected]8093a542011-05-13 07:29:32287 ProfileIOData* io_data =
288 reinterpret_cast<ProfileIOData*>(context.GetUserData(NULL));
[email protected]5b52ad42011-05-26 14:26:09289 ContentSetting setting = io_data->GetHostContentSettingsMap()->
290 GetCookieContentSetting(url, first_party, true);
[email protected]ed24fad2011-05-10 22:44:01291
[email protected]5b52ad42011-05-26 14:26:09292 if (setting == CONTENT_SETTING_SESSION_ONLY)
293 options->set_force_session();
[email protected]ed24fad2011-05-10 22:44:01294
[email protected]5b52ad42011-05-26 14:26:09295 bool allow = setting == CONTENT_SETTING_ALLOW ||
296 setting == CONTENT_SETTING_SESSION_ONLY;
[email protected]ed24fad2011-05-10 22:44:01297
[email protected]8093a542011-05-13 07:29:32298 BrowserThread::PostTask(
299 BrowserThread::UI, FROM_HERE,
300 NewRunnableFunction(
301 &TabSpecificContentSettings::CookieChanged,
302 render_process_id, render_view_id, url, cookie_line, *options,
303 !allow));
[email protected]ed24fad2011-05-10 22:44:01304 return allow;
305}
306
[email protected]b80f68432011-05-02 17:22:30307#if defined(OS_LINUX)
308int ChromeContentBrowserClient::GetCrashSignalFD(
309 const std::string& process_type) {
310 if (process_type == switches::kRendererProcess)
311 return RendererCrashHandlerHostLinux::GetInstance()->GetDeathSignalSocket();
312
313 if (process_type == switches::kPluginProcess)
314 return PluginCrashHandlerHostLinux::GetInstance()->GetDeathSignalSocket();
315
316 if (process_type == switches::kPpapiPluginProcess)
317 return PpapiCrashHandlerHostLinux::GetInstance()->GetDeathSignalSocket();
318
319 if (process_type == switches::kGpuProcess)
320 return GpuCrashHandlerHostLinux::GetInstance()->GetDeathSignalSocket();
321
322 return -1;
323}
324#endif
325
[email protected]d977f9c2011-03-14 16:10:26326} // namespace chrome