Chromium Code Reviews| 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 | |
| 51 // see BrowserPluginEmbedder::DidCommitProvisionalLoadForFrame. | |
|
awong
2012/09/06 00:23:27
DidCommitProvisionalLoadForFrame -> DidCommitProvi
lazyboy
2012/09/06 04:33:53
Done.
| |
| 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 )); | |
| 109 | |
| 110 guest = guest_web_contents->SetBrowserPluginGuest(instance_id); | |
| 111 guest->set_embedder_render_process_host( | |
| 112 render_view_host->GetProcess()); | |
| 113 | |
| 114 guest_web_contents->GetMutableRendererPrefs()-> | |
| 115 throttle_input_events = false; | |
| 116 AddGuest(instance_id, guest, frame_id); | |
| 117 guest_web_contents->SetDelegate(guest); | |
| 118 } else { | |
| 119 guest_web_contents = static_cast<WebContentsImpl*>(guest->web_contents()); | |
| 120 } | |
| 121 guest_web_contents->GetController().LoadURL(url, | |
| 122 Referrer(), | |
| 123 PAGE_TRANSITION_AUTO_SUBFRAME, | |
| 124 std::string()); | |
| 125 if (!size.IsEmpty()) | |
| 126 guest_web_contents->GetView()->SizeContents(size); | |
| 127 } | |
| 128 | |
| 129 void BrowserPluginEmbedder::UpdateRectACK(int instance_id, | |
| 130 int message_id, | |
| 131 const gfx::Size& size) { | |
| 132 BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); | |
| 133 if (guest) | |
| 134 guest->UpdateRectACK(message_id, size); | |
| 135 } | |
| 136 | |
| 137 void BrowserPluginEmbedder::ResizeGuest(int instance_id, | |
| 138 TransportDIB* damage_buffer, | |
| 139 int width, | |
| 140 int height, | |
| 141 bool resize_pending, | |
| 142 float scale_factor) { | |
| 143 BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); | |
| 144 if (!guest) | |
| 145 return; | |
| 146 WebContentsImpl* guest_web_contents = | |
| 147 static_cast<WebContentsImpl*>(guest->web_contents()); | |
| 148 guest->SetDamageBuffer(damage_buffer, gfx::Size(width, height), scale_factor); | |
| 149 if (!resize_pending) | |
| 150 guest_web_contents->GetView()->SizeContents(gfx::Size(width, height)); | |
| 151 } | |
| 152 | |
| 153 void BrowserPluginEmbedder::SetFocus(int instance_id, | |
| 154 bool focused) { | |
| 155 BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); | |
| 156 if (guest) | |
| 157 guest->SetFocus(focused); | |
| 158 } | |
| 159 | |
| 160 void BrowserPluginEmbedder::DestroyGuests() { | |
| 161 for (GuestWebContentsMap::const_iterator it = | |
| 162 guest_web_contents_container_.begin(); | |
|
awong
2012/09/06 00:23:27
line continuations should be 4 spaces indented fro
lazyboy
2012/09/06 04:33:53
Done.
Is the style guide for "for (...)" indenta
awong
2012/09/06 19:55:26
The latter. What I meant was that the guest_web_c
lazyboy
2012/09/07 19:33:19
Ok.
| |
| 163 it != guest_web_contents_container_.end(); ++it) { | |
| 164 WebContents* web_contents = it->first; | |
| 165 delete web_contents; | |
| 166 } | |
| 167 guest_web_contents_container_.clear(); | |
| 168 guests_by_instance_id_.clear(); | |
| 169 } | |
| 170 | |
| 171 void BrowserPluginEmbedder::HandleInputEvent(int instance_id, | |
| 172 RenderViewHost* render_view_host, | |
| 173 const gfx::Rect& guest_rect, | |
| 174 const WebKit::WebInputEvent& event, | |
| 175 IPC::Message* reply_message) { | |
| 176 BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); | |
| 177 if (guest) | |
| 178 guest->HandleInputEvent(render_view_host, guest_rect, event, reply_message); | |
| 179 } | |
| 180 | |
| 181 void BrowserPluginEmbedder::DestroyGuestByInstanceID(int instance_id) { | |
| 182 BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); | |
| 183 if (guest) { | |
| 184 WebContents* guest_web_contents = guest->web_contents(); | |
| 185 | |
| 186 GuestWebContentsMap::iterator guest_it = guest_web_contents_container_.find( | |
| 187 guest_web_contents); | |
| 188 DCHECK(guest_it != guest_web_contents_container_.end()); | |
|
awong
2012/09/06 00:23:27
How impossible is this DCHECK()?
The erase() belo
lazyboy
2012/09/06 04:33:53
Very unlikely since that would mean guest_web_cont
| |
| 189 | |
| 190 guest_web_contents_container_.erase(guest_it); | |
| 191 guests_by_instance_id_.erase(instance_id); | |
| 192 | |
| 193 guest->Destroy(); | |
| 194 } | |
| 195 } | |
| 196 | |
| 197 void BrowserPluginEmbedder::DidCommitProvisionalLoadForFrame( | |
| 198 int64 frame_id, | |
| 199 bool is_main_frame, | |
| 200 const GURL& url, | |
| 201 PageTransition transition_type, | |
| 202 RenderViewHost* render_view_host) { | |
| 203 // Clean up guests that lie in the frame that we're navigating. | |
| 204 // | |
| 205 // Subframe's guest(s) cleaning up is not handled here, they are handled by | |
|
awong
2012/09/06 00:23:27
cleaning up -> clean up
here, they -> here. They
lazyboy
2012/09/06 04:33:53
Done.
| |
| 206 // either the lifetime of WebContents associated directly with the guest(s) or | |
| 207 // by the lifetime of their embedders. Consider the following cases: | |
| 208 // | |
| 209 // Case 1: (Subframe's guest clean up managed by embedder) An WebContents WC1 | |
|
awong
2012/09/06 00:23:27
An WebContents -> A WebContents
lazyboy
2012/09/06 04:33:53
Done.
| |
| 210 // embeds a guest (therefore its an embedder E1), call the guest G1. G1 points | |
|
awong
2012/09/06 00:23:27
its -> it is
, call the guest -> . Call this guest
lazyboy
2012/09/06 04:33:53
Done.
| |
| 211 // to a page that has an iframe, and its WebContents is WC2. Now the iframe | |
|
awong
2012/09/06 00:23:27
iframe, and its WebContents is WC2 -> iframe. Call
lazyboy
2012/09/06 04:33:53
Done.
| |
| 212 // has a guest of its own in it, lets call it G2, which makes WC2 an embedder | |
| 213 // as well, call it E2. Now when DidCommitProvisionalLoadForFrame is called, | |
|
awong
2012/09/06 00:23:27
When talking about Functions in comments, please i
lazyboy
2012/09/06 04:33:53
Done.
| |
| 214 // G2 won't be cleaned up by the code below since it doesn't directly belong | |
| 215 // to the top level frame. However when the WebContents WC2 gets destroyed, | |
| 216 // it destroys the embedder E2 associated with it, eventually destroying all | |
| 217 // of its guests (G2). | |
| 218 // | |
| 219 // Case 2: (Subframe's guest cleanup managed by WebContents) A page has an | |
| 220 // iframe which points to a page that has a guest G1. Therefore this makes top | |
| 221 // level page's WebContents WC1 an embedder E1. Lets say the WebContents | |
| 222 // associated with G1 is WC2. When DidCommitProvisionalLoadForFrame occurs, G1 | |
| 223 // is not a guest lying in the top level frame that called commit, so it won't | |
|
awong
2012/09/06 00:23:27
guest lying in -> guest within
lazyboy
2012/09/06 04:33:53
Done.
| |
| 224 // be cleaned up with the code below; However since WC2 goes away, this | |
| 225 // eventually destroys G1 since G1 is an observer for WC2. | |
| 226 typedef std::set<WebContents*> GuestSet; | |
| 227 GuestSet guests_to_delete; | |
| 228 for (GuestWebContentsMap::const_iterator it = | |
| 229 guest_web_contents_container_.begin(); | |
| 230 it != guest_web_contents_container_.end(); ++it) { | |
| 231 WebContents* web_contents = it->first; | |
| 232 if (it->second == frame_id) | |
| 233 guests_to_delete.insert(web_contents); | |
|
awong
2012/09/06 00:23:27
Simplify to guests_to_delete.insert(it->first);
lazyboy
2012/09/06 04:33:53
Done.
| |
| 234 } | |
| 235 for (GuestSet::const_iterator it = guests_to_delete.begin(); | |
| 236 it != guests_to_delete.end(); ++it) { | |
| 237 int instance_id = static_cast<WebContentsImpl*>(*it)-> | |
| 238 GetBrowserPluginGuest()->instance_id(); | |
| 239 DestroyGuestByInstanceID(instance_id); | |
| 240 } | |
| 241 } | |
| 242 | |
| 243 void BrowserPluginEmbedder::RenderViewDeleted( | |
| 244 RenderViewHost* render_view_host) { | |
| 245 DestroyGuests(); | |
| 246 } | |
| 247 | |
| 248 void BrowserPluginEmbedder::RenderViewGone(base::TerminationStatus status) { | |
| 249 DestroyGuests(); | |
| 250 } | |
| 251 | |
| 252 void BrowserPluginEmbedder::WebContentsVisibilityChanged(bool visible) { | |
| 253 // If the embedder is hidden we need to hide the guests as well. | |
| 254 for (GuestWebContentsMap::const_iterator it = | |
| 255 guest_web_contents_container_.begin(); | |
| 256 it != guest_web_contents_container_.end(); ++it) { | |
| 257 WebContents* web_contents = it->first; | |
| 258 if (visible) | |
| 259 web_contents->WasShown(); | |
| 260 else | |
| 261 web_contents->WasHidden(); | |
| 262 } | |
| 263 } | |
| 264 | |
| 265 void BrowserPluginEmbedder::PluginDestroyed(int instance_id) { | |
| 266 DestroyGuestByInstanceID(instance_id); | |
| 267 } | |
| 268 | |
| 269 void BrowserPluginEmbedder::Observe(int type, | |
| 270 const NotificationSource& source, | |
| 271 const NotificationDetails& details) { | |
| 272 switch (type) { | |
| 273 case NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED: { | |
| 274 bool visible = *Details<bool>(details).ptr(); | |
| 275 WebContentsVisibilityChanged(visible); | |
| 276 break; | |
| 277 } | |
| 278 default: | |
| 279 NOTREACHED() << "Unexpected notification type: " << type; | |
| 280 } | |
| 281 } | |
| 282 | |
| 283 bool BrowserPluginEmbedder::IsForRenderViewHost(RenderViewHost* rvh) { | |
| 284 return render_view_host_ == rvh; | |
| 285 } | |
| 286 | |
| 287 } // namespace content | |
| OLD | NEW |