OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // 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 | 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 "chrome/browser/renderer_host/chrome_render_view_host_observer.h" | 5 #include "chrome/browser/renderer_host/chrome_render_view_host_observer.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "chrome/browser/dom_operation_notification_details.h" | 8 #include "chrome/browser/dom_operation_notification_details.h" |
| 9 #include "chrome/browser/extensions/extension_service.h" |
9 #include "chrome/browser/net/predictor_api.h" | 10 #include "chrome/browser/net/predictor_api.h" |
| 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/common/chrome_notification_types.h" |
10 #include "chrome/common/chrome_switches.h" | 13 #include "chrome/common/chrome_switches.h" |
| 14 #include "chrome/common/extensions/extension_messages.h" |
11 #include "chrome/common/render_messages.h" | 15 #include "chrome/common/render_messages.h" |
| 16 #include "content/browser/browsing_instance.h" |
| 17 #include "content/browser/child_process_security_policy.h" |
| 18 #include "content/browser/renderer_host/render_view_host_delegate.h" |
12 #include "content/browser/renderer_host/render_view_host.h" | 19 #include "content/browser/renderer_host/render_view_host.h" |
13 #include "content/browser/renderer_host/render_view_host_delegate.h" | 20 #include "content/browser/site_instance.h" |
14 #include "chrome/common/chrome_notification_types.h" | 21 #include "content/common/bindings_policy.h" |
15 #include "content/common/notification_service.h" | 22 #include "content/common/notification_service.h" |
16 #include "content/common/url_constants.h" | 23 #include "content/common/url_constants.h" |
17 #include "content/common/view_messages.h" | 24 #include "content/common/view_messages.h" |
18 | 25 |
19 ChromeRenderViewHostObserver::ChromeRenderViewHostObserver( | 26 ChromeRenderViewHostObserver::ChromeRenderViewHostObserver( |
20 RenderViewHost* render_view_host) | 27 RenderViewHost* render_view_host) |
21 : RenderViewHostObserver(render_view_host) { | 28 : RenderViewHostObserver(render_view_host) { |
| 29 InitRenderViewHostForExtensions(); |
22 } | 30 } |
23 | 31 |
24 ChromeRenderViewHostObserver::~ChromeRenderViewHostObserver() { | 32 ChromeRenderViewHostObserver::~ChromeRenderViewHostObserver() { |
25 } | 33 } |
26 | 34 |
| 35 void ChromeRenderViewHostObserver::RenderViewHostInitialized() { |
| 36 InitRenderViewForExtensions(); |
| 37 } |
| 38 |
27 void ChromeRenderViewHostObserver::Navigate( | 39 void ChromeRenderViewHostObserver::Navigate( |
28 const ViewMsg_Navigate_Params& params) { | 40 const ViewMsg_Navigate_Params& params) { |
29 const GURL& url = params.url; | 41 const GURL& url = params.url; |
30 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kChromeFrame) && | 42 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kChromeFrame) && |
31 (url.SchemeIs(chrome::kHttpScheme) || url.SchemeIs(chrome::kHttpsScheme))) | 43 (url.SchemeIs(chrome::kHttpScheme) || url.SchemeIs(chrome::kHttpsScheme))) |
32 chrome_browser_net::PreconnectUrlAndSubresources(url); | 44 chrome_browser_net::PreconnectUrlAndSubresources(url); |
33 } | 45 } |
34 | 46 |
35 bool ChromeRenderViewHostObserver::OnMessageReceived( | 47 bool ChromeRenderViewHostObserver::OnMessageReceived( |
36 const IPC::Message& message) { | 48 const IPC::Message& message) { |
37 bool handled = true; | 49 bool handled = true; |
38 IPC_BEGIN_MESSAGE_MAP(ChromeRenderViewHostObserver, message) | 50 IPC_BEGIN_MESSAGE_MAP(ChromeRenderViewHostObserver, message) |
39 IPC_MESSAGE_HANDLER(ViewHostMsg_DomOperationResponse, | 51 IPC_MESSAGE_HANDLER(ViewHostMsg_DomOperationResponse, |
40 OnDomOperationResponse) | 52 OnDomOperationResponse) |
41 IPC_MESSAGE_UNHANDLED(handled = false) | 53 IPC_MESSAGE_UNHANDLED(handled = false) |
42 IPC_END_MESSAGE_MAP() | 54 IPC_END_MESSAGE_MAP() |
43 return handled; | 55 return handled; |
44 } | 56 } |
45 | 57 |
| 58 void ChromeRenderViewHostObserver::InitRenderViewHostForExtensions() { |
| 59 const Extension* extension = GetExtension(); |
| 60 if (!extension) |
| 61 return; |
| 62 |
| 63 SiteInstance* site_instance = render_view_host()->site_instance(); |
| 64 Profile* profile = Profile::FromBrowserContext( |
| 65 site_instance->browsing_instance()->browser_context()); |
| 66 ExtensionProcessManager* process_manager = |
| 67 profile->GetExtensionProcessManager(); |
| 68 CHECK(process_manager); |
| 69 |
| 70 site_instance->GetProcess()->mark_is_extension_process(); |
| 71 |
| 72 // Register the association between extension and SiteInstance with |
| 73 // ExtensionProcessManager. |
| 74 // TODO(creis): Use this to replace SetInstalledAppForRenderer. |
| 75 process_manager->RegisterExtensionSiteInstance(site_instance->id(), |
| 76 extension->id()); |
| 77 |
| 78 if (extension->is_app()) { |
| 79 // Record which, if any, installed app is associated with this process. |
| 80 // TODO(aa): Totally lame to store this state in a global map in extension |
| 81 // service. Can we get it from EPM instead? |
| 82 profile->GetExtensionService()->SetInstalledAppForRenderer( |
| 83 render_view_host()->process()->id(), extension); |
| 84 } |
| 85 |
| 86 // Enable extension bindings for the renderer. Currently only extensions, |
| 87 // packaged apps, and hosted component apps use extension bindings. |
| 88 Extension::Type type = extension->GetType(); |
| 89 if (type == Extension::TYPE_EXTENSION || |
| 90 type == Extension::TYPE_USER_SCRIPT || |
| 91 type == Extension::TYPE_PACKAGED_APP || |
| 92 (type == Extension::TYPE_HOSTED_APP && |
| 93 extension->location() == Extension::COMPONENT)) { |
| 94 render_view_host()->AllowBindings(BindingsPolicy::EXTENSION); |
| 95 } |
| 96 } |
| 97 |
| 98 void ChromeRenderViewHostObserver::InitRenderViewForExtensions() { |
| 99 const Extension* extension = GetExtension(); |
| 100 if (!extension) |
| 101 return; |
| 102 |
| 103 SiteInstance* site_instance = render_view_host()->site_instance(); |
| 104 Profile* profile = Profile::FromBrowserContext( |
| 105 site_instance->browsing_instance()->browser_context()); |
| 106 RenderProcessHost* process = render_view_host()->process(); |
| 107 |
| 108 if (extension->is_app()) { |
| 109 Send(new ExtensionMsg_ActivateApplication(extension->id())); |
| 110 // Though we already record the associated process ID for the renderer in |
| 111 // InitRenderViewHostForExtensions, the process might have crashed and been |
| 112 // restarted (hence the re-initialization), so we need to update that |
| 113 // mapping. |
| 114 profile->GetExtensionService()->SetInstalledAppForRenderer( |
| 115 process->id(), extension); |
| 116 } |
| 117 |
| 118 // Some extensions use chrome:// URLs. |
| 119 Extension::Type type = extension->GetType(); |
| 120 if (type == Extension::TYPE_EXTENSION || |
| 121 type == Extension::TYPE_PACKAGED_APP) { |
| 122 ChildProcessSecurityPolicy::GetInstance()->GrantScheme( |
| 123 process->id(), chrome::kChromeUIScheme); |
| 124 } |
| 125 |
| 126 if (type == Extension::TYPE_EXTENSION || |
| 127 type == Extension::TYPE_USER_SCRIPT || |
| 128 type == Extension::TYPE_PACKAGED_APP || |
| 129 (type == Extension::TYPE_HOSTED_APP && |
| 130 extension->location() == Extension::COMPONENT)) { |
| 131 Send(new ExtensionMsg_ActivateExtension(extension->id())); |
| 132 } |
| 133 } |
| 134 |
| 135 const Extension* ChromeRenderViewHostObserver::GetExtension() { |
| 136 // Note that due to ChromeContentBrowserClient::GetEffectiveURL(), even hosted |
| 137 // apps will have a chrome-extension:// URL for their site, so we can ignore |
| 138 // that wrinkle here. |
| 139 SiteInstance* site_instance = render_view_host()->site_instance(); |
| 140 const GURL& site = site_instance->site(); |
| 141 |
| 142 if (!site.SchemeIs(chrome::kExtensionScheme)) |
| 143 return NULL; |
| 144 |
| 145 Profile* profile = Profile::FromBrowserContext( |
| 146 site_instance->browsing_instance()->browser_context()); |
| 147 ExtensionService* service = profile->GetExtensionService(); |
| 148 if (!service) |
| 149 return NULL; |
| 150 |
| 151 // May be null if somebody typos a chrome-extension:// URL. |
| 152 return service->GetExtensionByURL(site); |
| 153 } |
| 154 |
46 void ChromeRenderViewHostObserver::OnDomOperationResponse( | 155 void ChromeRenderViewHostObserver::OnDomOperationResponse( |
47 const std::string& json_string, int automation_id) { | 156 const std::string& json_string, int automation_id) { |
48 DomOperationNotificationDetails details(json_string, automation_id); | 157 DomOperationNotificationDetails details(json_string, automation_id); |
49 NotificationService::current()->Notify( | 158 NotificationService::current()->Notify( |
50 chrome::NOTIFICATION_DOM_OPERATION_RESPONSE, | 159 chrome::NOTIFICATION_DOM_OPERATION_RESPONSE, |
51 Source<RenderViewHost>(render_view_host()), | 160 Source<RenderViewHost>(render_view_host()), |
52 Details<DomOperationNotificationDetails>(&details)); | 161 Details<DomOperationNotificationDetails>(&details)); |
53 } | 162 } |
OLD | NEW |