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