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_embedder.h" | |
6 | |
7 #include <set> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/stl_util.h" | |
11 #include "base/time.h" | |
12 #include "content/browser/browser_plugin/browser_plugin_embedder_helper.h" | |
13 #include "content/browser/browser_plugin/browser_plugin_guest.h" | |
14 #include "content/browser/browser_plugin/browser_plugin_guest_helper.h" | |
15 #include "content/browser/browser_plugin/browser_plugin_host_factory.h" | |
16 #include "content/browser/renderer_host/render_view_host_impl.h" | |
17 #include "content/browser/renderer_host/render_widget_host_impl.h" | |
18 #include "content/browser/web_contents/web_contents_impl.h" | |
19 #include "content/common/browser_plugin_messages.h" | |
20 #include "content/common/view_messages.h" | |
21 #include "content/public/browser/notification_details.h" | |
22 #include "content/public/browser/notification_service.h" | |
23 #include "content/public/browser/notification_source.h" | |
24 #include "content/public/browser/notification_types.h" | |
25 #include "content/public/browser/render_process_host.h" | |
26 #include "content/public/browser/render_view_host.h" | |
27 #include "content/public/browser/render_widget_host_view.h" | |
28 #include "content/public/browser/web_contents_observer.h" | |
29 #include "content/public/browser/web_contents_view.h" | |
30 #include "content/public/common/url_constants.h" | |
31 #include "ui/gfx/size.h" | |
32 | |
33 namespace content { | |
34 | |
35 // static | |
36 BrowserPluginHostFactory* BrowserPluginEmbedder::factory_ = NULL; | |
37 | |
38 BrowserPluginEmbedder::BrowserPluginEmbedder( | |
39 WebContentsImpl* web_contents, | |
40 RenderViewHost* render_view_host) | |
41 : WebContentsObserver(web_contents), | |
42 render_view_host_(render_view_host) { | |
43 // Listen to visibility changes so that an embedder hides its guests | |
44 // as well. | |
45 registrar_.Add(this, | |
46 NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED, | |
47 Source<WebContents>(web_contents)); | |
48 | |
49 // |render_view_host| manages the ownership of this BrowserPluginGuestHelper. | |
50 new BrowserPluginEmbedderHelper(this, render_view_host); | |
51 } | |
52 | |
53 BrowserPluginEmbedder::~BrowserPluginEmbedder() { | |
54 // Destroy guests that are managed by the current embedder, for more details | |
55 // see BrowserPluginEmbedder::DidCommitProvisionalLoadForFrame(). | |
56 DestroyGuests(); | |
57 } | |
58 | |
59 // static | |
60 BrowserPluginEmbedder* BrowserPluginEmbedder::Create( | |
61 WebContentsImpl* web_contents, | |
62 content::RenderViewHost* render_view_host) { | |
63 if (factory_) { | |
64 return factory_->CreateBrowserPluginEmbedder(web_contents, | |
65 render_view_host); | |
66 } | |
67 return new BrowserPluginEmbedder(web_contents, render_view_host); | |
68 } | |
69 | |
70 BrowserPluginGuest* BrowserPluginEmbedder::GetGuestByInstanceID( | |
71 int instance_id) const { | |
72 ContainerInstanceMap::const_iterator it = | |
73 guest_web_contents_by_instance_id_.find(instance_id); | |
74 if (it != guest_web_contents_by_instance_id_.end()) | |
75 return static_cast<WebContentsImpl*>(it->second)->GetBrowserPluginGuest(); | |
76 return NULL; | |
77 } | |
78 | |
79 void BrowserPluginEmbedder::AddGuest(int instance_id, | |
awong
2012/09/10 23:57:32
AddGuest can take the guest_web_contents directly
lazyboy
2012/09/11 19:58:39
Done.
| |
80 BrowserPluginGuest* guest, | |
81 int64 frame_id) { | |
82 DCHECK(guest_web_contents_by_instance_id_.find(instance_id) == | |
83 guest_web_contents_by_instance_id_.end()); | |
84 guest_web_contents_by_instance_id_[instance_id] = guest->web_contents(); | |
85 guest_web_contents_container_[guest->web_contents()] = frame_id; | |
86 } | |
87 | |
88 void BrowserPluginEmbedder::NavigateGuest(RenderViewHost* render_view_host, | |
89 int instance_id, | |
90 int64 frame_id, | |
91 const std::string& src, | |
92 const gfx::Size& size) { | |
93 BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); | |
94 WebContentsImpl* guest_web_contents = NULL; | |
95 GURL url(src); | |
96 if (!guest) { | |
97 const std::string& host = | |
98 render_view_host->GetSiteInstance()->GetSite().host(); | |
99 guest_web_contents = static_cast<WebContentsImpl*>( | |
100 WebContents::CreateGuest(web_contents()->GetBrowserContext(), | |
101 host, | |
102 instance_id)); | |
103 | |
104 guest = guest_web_contents->GetBrowserPluginGuest(); | |
105 guest->set_embedder_render_process_host( | |
106 render_view_host->GetProcess()); | |
107 | |
108 guest_web_contents->GetMutableRendererPrefs()-> | |
109 throttle_input_events = false; | |
110 AddGuest(instance_id, guest, frame_id); | |
111 guest_web_contents->SetDelegate(guest); | |
112 } else { | |
113 guest_web_contents = static_cast<WebContentsImpl*>(guest->web_contents()); | |
114 } | |
115 guest_web_contents->GetController().LoadURL(url, | |
116 Referrer(), | |
117 PAGE_TRANSITION_AUTO_SUBFRAME, | |
118 std::string()); | |
119 if (!size.IsEmpty()) | |
120 guest_web_contents->GetView()->SizeContents(size); | |
121 } | |
122 | |
123 void BrowserPluginEmbedder::UpdateRectACK(int instance_id, | |
124 int message_id, | |
125 const gfx::Size& size) { | |
126 BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); | |
127 if (guest) | |
128 guest->UpdateRectACK(message_id, size); | |
129 } | |
130 | |
131 void BrowserPluginEmbedder::ResizeGuest(int instance_id, | |
132 TransportDIB* damage_buffer, | |
133 int width, | |
134 int height, | |
135 bool resize_pending, | |
136 float scale_factor) { | |
137 BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); | |
138 if (!guest) | |
139 return; | |
140 WebContentsImpl* guest_web_contents = | |
141 static_cast<WebContentsImpl*>(guest->web_contents()); | |
142 guest->SetDamageBuffer(damage_buffer, gfx::Size(width, height), scale_factor); | |
143 if (!resize_pending) | |
144 guest_web_contents->GetView()->SizeContents(gfx::Size(width, height)); | |
145 } | |
146 | |
147 void BrowserPluginEmbedder::SetFocus(int instance_id, | |
148 bool focused) { | |
149 BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); | |
150 if (guest) | |
151 guest->SetFocus(focused); | |
152 } | |
153 | |
154 void BrowserPluginEmbedder::DestroyGuests() { | |
155 STLDeleteContainerPairFirstPointers(guest_web_contents_container_.begin(), | |
156 guest_web_contents_container_.end()); | |
157 guest_web_contents_container_.clear(); | |
158 guest_web_contents_by_instance_id_.clear(); | |
159 } | |
160 | |
161 void BrowserPluginEmbedder::HandleInputEvent(int instance_id, | |
162 RenderViewHost* render_view_host, | |
163 const gfx::Rect& guest_rect, | |
164 const WebKit::WebInputEvent& event, | |
165 IPC::Message* reply_message) { | |
166 BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); | |
167 if (guest) | |
168 guest->HandleInputEvent(render_view_host, guest_rect, event, reply_message); | |
169 } | |
170 | |
171 void BrowserPluginEmbedder::DestroyGuestByInstanceID(int instance_id) { | |
172 BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); | |
173 if (guest) { | |
174 WebContents* guest_web_contents = guest->web_contents(); | |
175 | |
176 GuestWebContentsMap::iterator guest_it = guest_web_contents_container_.find( | |
177 guest_web_contents); | |
178 DCHECK(guest_it != guest_web_contents_container_.end()); | |
179 | |
180 guest_web_contents_container_.erase(guest_it); | |
181 guest_web_contents_by_instance_id_.erase(instance_id); | |
182 | |
183 // Destroy the guest's web_contents. | |
184 delete guest_it->first; | |
185 } | |
186 } | |
187 | |
188 void BrowserPluginEmbedder::DidCommitProvisionalLoadForFrame( | |
189 int64 frame_id, | |
190 bool is_main_frame, | |
191 const GURL& url, | |
192 PageTransition transition_type, | |
193 RenderViewHost* render_view_host) { | |
194 // Clean up guests that lie in the frame that we're navigating. | |
195 // | |
196 // Subframe's guest(s) clean up is not handled here. They are handled by | |
197 // either the lifetime of WebContents associated directly with the guest(s) or | |
198 // by the lifetime of their embedders. Consider the following cases: | |
199 // | |
200 // Case 1: (Subframe's guest clean up managed by embedder) A WebContents WC1 | |
201 // embeds a guest (therefore it is an embedder E1), call this guest G1. G1 | |
202 // points to a page that has an iframe. Call the iframe's WebContents WC2. Now | |
203 // the iframe has a guest of its own in it, lets call it G2, which makes WC2 | |
204 // an embedder as well, call it E2. Now when | |
205 // DidCommitProvisionalLoadForFrame() is called, G2 won't be cleaned up by the | |
206 // code below since it doesn't directly belong to the top level frame. However | |
207 // when the WebContents WC2 gets destroyed, it destroys the embedder E2 | |
208 // associated with it, eventually destroying all of its guests (G2). | |
209 // | |
210 // Case 2: (Subframe's guest cleanup managed by WebContents) A page has an | |
211 // iframe which points to a page that has a guest G1. Therefore this makes top | |
212 // level page's WebContents WC1 an embedder E1. Lets say the WebContents | |
213 // associated with G1 is WC2. When DidCommitProvisionalLoadForFrame() occurs, | |
214 // G1 is not a guest within the top level frame that called commit, so it | |
215 // won't be cleaned up with the code below; However since WC2 goes away, this | |
216 // eventually destroys G1 since G1 is an observer for WC2. | |
217 // | |
218 // The following code is repsonsible for deleting the top-level frame which | |
219 // will eventually invoke one of the two cases above covering all guests in | |
220 // the whole frame tree. | |
221 typedef std::set<WebContents*> GuestSet; | |
222 GuestSet guests_to_delete; | |
223 for (GuestWebContentsMap::const_iterator it = | |
224 guest_web_contents_container_.begin(); | |
225 it != guest_web_contents_container_.end(); ++it) { | |
226 if (it->second == frame_id) | |
227 guests_to_delete.insert(it->first); | |
228 } | |
229 for (GuestSet::const_iterator it = guests_to_delete.begin(); | |
230 it != guests_to_delete.end(); ++it) { | |
231 int instance_id = static_cast<WebContentsImpl*>(*it)-> | |
232 GetBrowserPluginGuest()->instance_id(); | |
233 DestroyGuestByInstanceID(instance_id); | |
234 } | |
235 } | |
236 | |
237 void BrowserPluginEmbedder::RenderViewDeleted( | |
238 RenderViewHost* render_view_host) { | |
239 DestroyGuests(); | |
240 } | |
241 | |
242 void BrowserPluginEmbedder::RenderViewGone(base::TerminationStatus status) { | |
243 DestroyGuests(); | |
244 } | |
245 | |
246 void BrowserPluginEmbedder::WebContentsVisibilityChanged(bool visible) { | |
247 // If the embedder is hidden we need to hide the guests as well. | |
248 for (GuestWebContentsMap::const_iterator it = | |
249 guest_web_contents_container_.begin(); | |
250 it != guest_web_contents_container_.end(); ++it) { | |
251 WebContents* web_contents = it->first; | |
252 if (visible) | |
253 web_contents->WasShown(); | |
254 else | |
255 web_contents->WasHidden(); | |
256 } | |
257 } | |
258 | |
259 void BrowserPluginEmbedder::PluginDestroyed(int instance_id) { | |
260 DestroyGuestByInstanceID(instance_id); | |
261 } | |
262 | |
263 void BrowserPluginEmbedder::Observe(int type, | |
264 const NotificationSource& source, | |
265 const NotificationDetails& details) { | |
266 switch (type) { | |
267 case NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED: { | |
268 bool visible = *Details<bool>(details).ptr(); | |
269 WebContentsVisibilityChanged(visible); | |
270 break; | |
271 } | |
272 default: | |
273 NOTREACHED() << "Unexpected notification type: " << type; | |
274 } | |
275 } | |
276 | |
277 } // namespace content | |
OLD | NEW |