OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 "content/browser/browser_plugin/browser_plugin_host.h" | |
6 | |
7 #include "content/browser/browser_plugin/browser_plugin_host_helper.h" | |
8 #include "content/browser/renderer_host/render_view_host_impl.h" | |
9 #include "content/browser/web_contents/web_contents_impl.h" | |
10 #include "content/common/browser_plugin_messages.h" | |
11 #include "content/public/browser/notification_details.h" | |
12 #include "content/public/browser/notification_source.h" | |
13 #include "content/public/browser/notification_types.h" | |
14 #include "content/public/browser/render_process_host.h" | |
15 #include "content/public/browser/render_widget_host_view.h" | |
16 #include "content/public/browser/site_instance.h" | |
17 #include "content/public/browser/web_contents_view.h" | |
18 #include "ppapi/proxy/ppapi_messages.h" | |
19 | |
20 namespace content { | |
21 | |
22 BrowserPluginHost::BrowserPluginHost( | |
23 WebContentsImpl* web_contents) | |
24 : WebContentsObserver(web_contents), | |
25 embedder_render_process_host_(NULL), | |
26 instance_id_(0) { | |
27 // Listen to visibility changes so that an embedder hides its guests | |
28 // as well. | |
29 registrar_.Add(this, | |
30 NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED, | |
31 Source<WebContents>(web_contents)); | |
32 // Construct plumbing helpers when a new RenderViewHost is created for | |
33 // this BrowserPluginHost's WebContentsImpl. | |
34 registrar_.Add(this, | |
35 NOTIFICATION_RENDER_VIEW_HOST_CREATED_FOR_TAB, | |
36 Source<WebContents>(web_contents)); | |
37 } | |
38 | |
39 BrowserPluginHost::~BrowserPluginHost() { | |
40 } | |
41 | |
42 BrowserPluginHost* BrowserPluginHost::GetGuestByContainerID(int container_id) { | |
43 ContainerInstanceMap::const_iterator it = | |
44 guests_by_container_id_.find(container_id); | |
45 if (it != guests_by_container_id_.end()) | |
46 return it->second; | |
47 return NULL; | |
48 } | |
49 | |
50 void BrowserPluginHost::RegisterContainerInstance( | |
51 int container_id, | |
52 BrowserPluginHost* observer) { | |
53 DCHECK(guests_by_container_id_.find(container_id) == | |
54 guests_by_container_id_.end()); | |
55 guests_by_container_id_[container_id] = observer; | |
56 } | |
57 | |
58 bool BrowserPluginHost::TakeFocus(bool reverse) { | |
59 embedder_render_process_host()->Send( | |
60 new BrowserPluginMsg_AdvanceFocus(instance_id(), reverse)); | |
61 return true; | |
62 } | |
63 | |
64 bool BrowserPluginHost::OnMessageReceived(const IPC::Message& message) { | |
65 bool handled = true; | |
66 IPC_BEGIN_MESSAGE_MAP(BrowserPluginHost, message) | |
67 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_NavigateFromGuest, | |
68 OnNavigateFromGuest) | |
69 IPC_MESSAGE_UNHANDLED(handled = false) | |
70 IPC_END_MESSAGE_MAP() | |
71 return handled; | |
72 } | |
73 | |
74 void BrowserPluginHost::NavigateGuestFromEmbedder( | |
75 RenderViewHost* render_view_host, | |
76 int container_instance_id, | |
77 long long frame_id, | |
78 const std::string& src) { | |
79 BrowserPluginHost* guest_observer = | |
80 GetGuestByContainerID(container_instance_id); | |
81 WebContentsImpl* guest_web_contents = | |
82 guest_observer ? | |
83 static_cast<WebContentsImpl*>(guest_observer->web_contents()): NULL; | |
84 if (!guest_observer) { | |
85 guest_web_contents = | |
86 static_cast<WebContentsImpl*>( | |
87 WebContents::Create( | |
88 web_contents()->GetBrowserContext(), | |
89 render_view_host->GetSiteInstance(), | |
90 MSG_ROUTING_NONE, | |
91 NULL, // base WebContents | |
92 NULL // session storage namespace | |
93 )); | |
94 guest_observer = | |
95 guest_web_contents->browser_plugin_host(); | |
96 guest_observer->set_embedder_render_process_host( | |
97 render_view_host->GetProcess()); | |
98 guest_observer->set_instance_id(container_instance_id); | |
99 RegisterContainerInstance(container_instance_id, guest_observer); | |
100 AddGuest(guest_web_contents, frame_id); | |
101 } | |
102 GURL url(src); | |
103 guest_observer->web_contents()->SetDelegate(guest_observer); | |
104 guest_observer->web_contents()->GetController().LoadURL( | |
105 url, | |
106 Referrer(), | |
107 PAGE_TRANSITION_AUTO_SUBFRAME, | |
108 std::string()); | |
109 } | |
110 | |
111 void BrowserPluginHost::OnNavigateFromGuest( | |
112 PP_Instance instance, | |
113 const std::string& src) { | |
114 DCHECK(embedder_render_process_host()); | |
115 GURL url(src); | |
116 web_contents()->GetController().LoadURL( | |
117 url, | |
118 Referrer(), | |
119 PAGE_TRANSITION_AUTO_SUBFRAME, | |
120 std::string()); | |
121 } | |
122 | |
123 void BrowserPluginHost::ConnectEmbedderToChannel( | |
124 RenderViewHost* render_view_host, | |
125 const IPC::ChannelHandle& channel_handle) { | |
126 DCHECK(embedder_render_process_host()); | |
127 // Tell the BrowserPlugin in the embedder that we're done and that it can | |
128 // begin using the guest renderer. | |
129 embedder_render_process_host()->Send( | |
130 new BrowserPluginMsg_LoadGuest( | |
131 instance_id(), | |
132 render_view_host->GetProcess()-> | |
133 GetID(), | |
134 channel_handle)); | |
135 } | |
136 | |
137 void BrowserPluginHost::AddGuest(WebContentsImpl* guest, int64 frame_id) { | |
138 guests_[guest] = frame_id; | |
139 } | |
140 | |
141 void BrowserPluginHost::RemoveGuest(WebContentsImpl* guest) { | |
142 guests_.erase(guest); | |
143 } | |
144 | |
145 void BrowserPluginHost::DestroyGuests() { | |
146 for (GuestMap::const_iterator it = guests_.begin(); | |
147 it != guests_.end(); ++it) { | |
148 WebContentsImpl* web_contents = it->first; | |
149 delete web_contents; | |
150 } | |
151 guests_.clear(); | |
152 guests_by_container_id_.clear(); | |
153 } | |
154 | |
155 void BrowserPluginHost::DidCommitProvisionalLoadForFrame( | |
156 int64 frame_id, | |
157 bool is_main_frame, | |
158 const GURL& url, | |
159 PageTransition transition_type, | |
160 RenderViewHost* render_view_host) { | |
161 // Clean-up guests that lie in the frame that we're navigating. | |
162 typedef std::set<WebContentsImpl*> GuestSet; | |
163 GuestSet guests_to_delete; | |
164 for (GuestMap::const_iterator it = guests_.begin(); | |
165 it != guests_.end(); ++it) { | |
166 WebContentsImpl* web_contents = it->first; | |
167 if (it->second == frame_id) { | |
168 guests_to_delete.insert(web_contents); | |
169 } | |
170 } | |
171 for (GuestSet::const_iterator it = guests_to_delete.begin(); | |
172 it != guests_to_delete.end(); ++it) { | |
173 delete *it; | |
174 guests_.erase(*it); | |
175 } | |
176 } | |
177 | |
178 void BrowserPluginHost::RenderViewDeleted(RenderViewHost* render_view_host) { | |
179 // TODO(fsamuel): For some reason ToT hangs when killing a guest, this wasn't | |
180 // the case earlier. Presumably, when a guest is killed/crashes, one of | |
181 // RenderViewDeleted, RenderViewGone or WebContentsDestroyed will get called. | |
182 // At that point, we should remove reference to this BrowserPluginHost | |
183 // from its embedder, in addition to destroying its guests, to ensure | |
184 // that we do not attempt a double delete. | |
185 DestroyGuests(); | |
186 } | |
187 | |
188 void BrowserPluginHost::RenderViewGone(base::TerminationStatus status) { | |
189 DestroyGuests(); | |
190 } | |
191 | |
192 void BrowserPluginHost::WebContentsDestroyed(WebContents* web_contents) { | |
193 DestroyGuests(); | |
194 } | |
195 | |
196 void BrowserPluginHost::Observe( | |
197 int type, | |
198 const NotificationSource& source, | |
199 const NotificationDetails& details) { | |
200 switch (type) { | |
201 case NOTIFICATION_RENDER_VIEW_HOST_CREATED_FOR_TAB: { | |
202 RenderViewHost* render_view_host = | |
203 Details<RenderViewHost>(details).ptr(); | |
204 // BrowserPluginHostHelper is destroyed when its associated RenderViewHost | |
205 // is destroyed. | |
206 new BrowserPluginHostHelper(this, render_view_host); | |
207 break; | |
208 } | |
209 case NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED: { | |
210 bool visible = *Details<bool>(details).ptr(); | |
211 // If the embedder is hidden we need to hide the guests as well. | |
212 for (GuestMap::const_iterator it = guests_.begin(); | |
213 it != guests_.end(); ++it) { | |
214 WebContentsImpl* web_contents = it->first; | |
215 if (visible) | |
216 web_contents->ShowContents(); | |
217 else | |
218 web_contents->HideContents(); | |
219 } | |
220 break; | |
221 } | |
222 default: | |
223 NOTREACHED() << "Unexpected notification type: " << type; | |
224 } | |
225 } | |
226 | |
227 } // namespace content | |
OLD | NEW |