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(); | |
Charlie Reis
2011/08/11 21:02:23
Please add a comment saying this is the one-time i
| |
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 in | |
75 // InitRenderViewForExtensions. | |
76 process_manager->RegisterExtensionSiteInstance(site_instance->id(), | |
77 extension->id()); | |
78 | |
79 // Enable extension bindings for the renderer. Currently only extensions, | |
80 // packaged apps, and hosted component apps use extension bindings. | |
81 Extension::Type type = extension->GetType(); | |
82 if (type == Extension::TYPE_EXTENSION || | |
83 type == Extension::TYPE_USER_SCRIPT || | |
84 type == Extension::TYPE_PACKAGED_APP || | |
85 (type == Extension::TYPE_HOSTED_APP && | |
86 extension->location() == Extension::COMPONENT)) { | |
87 render_view_host()->AllowBindings(BindingsPolicy::EXTENSION); | |
88 } | |
89 } | |
90 | |
91 void ChromeRenderViewHostObserver::InitRenderViewForExtensions() { | |
92 const Extension* extension = GetExtension(); | |
93 if (!extension) | |
94 return; | |
95 | |
96 SiteInstance* site_instance = render_view_host()->site_instance(); | |
97 Profile* profile = Profile::FromBrowserContext( | |
98 site_instance->browsing_instance()->browser_context()); | |
99 RenderProcessHost* process = render_view_host()->process(); | |
100 | |
101 if (extension->is_app()) { | |
102 Send(new ExtensionMsg_ActivateApplication(extension->id())); | |
103 // Record which, if any, installed app is associated with this process. | |
104 // TODO(aa): Totally lame to store this state in a global map in extension | |
105 // service. Can we get it from EPM instead? | |
106 profile->GetExtensionService()->SetInstalledAppForRenderer( | |
107 process->id(), extension); | |
108 } | |
109 | |
110 // Some extensions use chrome:// URLs. | |
111 Extension::Type type = extension->GetType(); | |
112 if (type == Extension::TYPE_EXTENSION || | |
113 type == Extension::TYPE_PACKAGED_APP) { | |
114 ChildProcessSecurityPolicy::GetInstance()->GrantScheme( | |
115 process->id(), chrome::kChromeUIScheme); | |
116 } | |
117 | |
118 if (type == Extension::TYPE_EXTENSION || | |
119 type == Extension::TYPE_USER_SCRIPT || | |
120 type == Extension::TYPE_PACKAGED_APP || | |
121 (type == Extension::TYPE_HOSTED_APP && | |
122 extension->location() == Extension::COMPONENT)) { | |
123 Send(new ExtensionMsg_ActivateExtension(extension->id())); | |
124 } | |
125 } | |
126 | |
127 const Extension* ChromeRenderViewHostObserver::GetExtension() { | |
128 // Note that due to ChromeContentBrowserClient::GetEffectiveURL(), even hosted | |
129 // apps will have a chrome-extension:// URL for their site, so we can ignore | |
130 // that wrinkle here. | |
131 SiteInstance* site_instance = render_view_host()->site_instance(); | |
132 const GURL& site = site_instance->site(); | |
133 | |
134 if (!site.SchemeIs(chrome::kExtensionScheme)) | |
135 return NULL; | |
136 | |
137 Profile* profile = Profile::FromBrowserContext( | |
138 site_instance->browsing_instance()->browser_context()); | |
139 ExtensionService* service = profile->GetExtensionService(); | |
140 if (!service) | |
141 return NULL; | |
142 | |
143 // May be null if somebody typos a chrome-extension:// URL. | |
144 return service->GetExtensionByURL(site); | |
145 } | |
146 | |
46 void ChromeRenderViewHostObserver::OnDomOperationResponse( | 147 void ChromeRenderViewHostObserver::OnDomOperationResponse( |
47 const std::string& json_string, int automation_id) { | 148 const std::string& json_string, int automation_id) { |
48 DomOperationNotificationDetails details(json_string, automation_id); | 149 DomOperationNotificationDetails details(json_string, automation_id); |
49 NotificationService::current()->Notify( | 150 NotificationService::current()->Notify( |
50 chrome::NOTIFICATION_DOM_OPERATION_RESPONSE, | 151 chrome::NOTIFICATION_DOM_OPERATION_RESPONSE, |
51 Source<RenderViewHost>(render_view_host()), | 152 Source<RenderViewHost>(render_view_host()), |
52 Details<DomOperationNotificationDetails>(&details)); | 153 Details<DomOperationNotificationDetails>(&details)); |
53 } | 154 } |
OLD | NEW |