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 <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 | |
|
Charlie Reis
2012/09/13 00:51:44
nit: These are two separate sentences. (There hav
lazyboy
2012/09/13 15:56:23
Ok.
Done.
| |
| 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, | |
| 80 WebContents* guest_web_contents, | |
| 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 = WebContentsImpl::CreateGuest( | |
| 100 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_web_contents, 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 #if defined(OS_WIN) | |
| 134 int damage_buffer_size, | |
| 135 #endif | |
| 136 int width, | |
| 137 int height, | |
| 138 bool resize_pending, | |
| 139 float scale_factor) { | |
| 140 BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); | |
| 141 if (!guest) | |
| 142 return; | |
| 143 WebContentsImpl* guest_web_contents = | |
| 144 static_cast<WebContentsImpl*>(guest->web_contents()); | |
| 145 guest->SetDamageBuffer(damage_buffer, | |
| 146 #if defined(OS_WIN) | |
| 147 damage_buffer_size, | |
| 148 #endif | |
| 149 gfx::Size(width, height), | |
| 150 scale_factor); | |
| 151 if (!resize_pending) | |
| 152 guest_web_contents->GetView()->SizeContents(gfx::Size(width, height)); | |
| 153 } | |
| 154 | |
| 155 void BrowserPluginEmbedder::SetFocus(int instance_id, | |
| 156 bool focused) { | |
| 157 BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); | |
| 158 if (guest) | |
| 159 guest->SetFocus(focused); | |
| 160 } | |
| 161 | |
| 162 void BrowserPluginEmbedder::DestroyGuests() { | |
| 163 STLDeleteContainerPairFirstPointers(guest_web_contents_container_.begin(), | |
| 164 guest_web_contents_container_.end()); | |
| 165 guest_web_contents_container_.clear(); | |
| 166 guest_web_contents_by_instance_id_.clear(); | |
| 167 } | |
| 168 | |
| 169 void BrowserPluginEmbedder::HandleInputEvent(int instance_id, | |
| 170 RenderViewHost* render_view_host, | |
| 171 const gfx::Rect& guest_rect, | |
| 172 const WebKit::WebInputEvent& event, | |
| 173 IPC::Message* reply_message) { | |
| 174 BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); | |
| 175 if (guest) | |
| 176 guest->HandleInputEvent(render_view_host, guest_rect, event, reply_message); | |
| 177 } | |
| 178 | |
| 179 void BrowserPluginEmbedder::DestroyGuestByInstanceID(int instance_id) { | |
| 180 BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); | |
| 181 if (guest) { | |
| 182 WebContents* guest_web_contents = guest->web_contents(); | |
| 183 | |
| 184 GuestWebContentsMap::iterator guest_it = guest_web_contents_container_.find( | |
| 185 guest_web_contents); | |
| 186 DCHECK(guest_it != guest_web_contents_container_.end()); | |
| 187 | |
| 188 // Destroy the guest's web_contents. | |
| 189 delete guest_it->first; | |
| 190 | |
| 191 guest_web_contents_container_.erase(guest_it); | |
| 192 guest_web_contents_by_instance_id_.erase(instance_id); | |
| 193 } | |
| 194 } | |
| 195 | |
| 196 void BrowserPluginEmbedder::DidCommitProvisionalLoadForFrame( | |
| 197 int64 frame_id, | |
| 198 bool is_main_frame, | |
| 199 const GURL& url, | |
| 200 PageTransition transition_type, | |
| 201 RenderViewHost* render_view_host) { | |
| 202 // Clean up guests that lie in the frame that we're navigating. | |
| 203 // | |
| 204 // Subframe's guest(s) clean up is not handled here. They are handled by | |
| 205 // either the lifetime of WebContents associated directly with the guest(s) or | |
| 206 // by the lifetime of their embedders. Consider the following cases: | |
| 207 // | |
| 208 // Case 1: (Subframe's guest clean up managed by embedder) A WebContents WC1 | |
| 209 // embeds a guest (therefore it is an embedder E1), call this guest G1. G1 | |
| 210 // points to a page that has an iframe. Call the iframe's WebContents WC2. Now | |
| 211 // the iframe has a guest of its own in it, lets call it G2, which makes WC2 | |
| 212 // an embedder as well, call it E2. Now when | |
|
Charlie Reis
2012/09/13 00:51:44
Is this possible? I didn't think we could support
lazyboy
2012/09/13 15:56:23
We don't forcefully disallow it yet, but we will d
| |
| 213 // DidCommitProvisionalLoadForFrame() is called, G2 won't be cleaned up by the | |
| 214 // code below since it doesn't directly belong to the top level frame. However | |
| 215 // when the WebContents WC2 gets destroyed, it destroys the embedder E2 | |
| 216 // associated with it, eventually destroying all of its guests (G2). | |
| 217 // | |
| 218 // Case 2: (Subframe's guest cleanup managed by WebContents) A page has an | |
| 219 // iframe which points to a page that has a guest G1. Therefore this makes top | |
| 220 // level page's WebContents WC1 an embedder E1. Lets say the WebContents | |
| 221 // associated with G1 is WC2. When DidCommitProvisionalLoadForFrame() occurs, | |
| 222 // G1 is not a guest within the top level frame that called commit, so it | |
| 223 // won't be cleaned up with the code below; However since WC2 goes away, this | |
|
Charlie Reis
2012/09/13 00:51:44
How does WC2 go away? There's no code that I know
lazyboy
2012/09/13 15:56:23
I talked to Fady about this part of the code: it s
Charlie Reis
2012/09/13 16:55:30
Great! I'm not aware of another need for this cod
Fady Samuel
2012/09/13 16:59:57
I don't believe this code is necessary any more. P
lazyboy
2012/09/13 18:52:49
Removed.
| |
| 224 // eventually destroys G1 since G1 is an observer for WC2. | |
| 225 // | |
| 226 // The following code is repsonsible for deleting the top-level frame which | |
| 227 // will eventually invoke one of the two cases above covering all guests in | |
| 228 // the whole frame tree. | |
| 229 typedef std::set<WebContents*> GuestSet; | |
| 230 GuestSet guests_to_delete; | |
| 231 for (GuestWebContentsMap::const_iterator it = | |
| 232 guest_web_contents_container_.begin(); | |
| 233 it != guest_web_contents_container_.end(); ++it) { | |
| 234 if (it->second == frame_id) | |
| 235 guests_to_delete.insert(it->first); | |
| 236 } | |
| 237 for (GuestSet::const_iterator it = guests_to_delete.begin(); | |
| 238 it != guests_to_delete.end(); ++it) { | |
| 239 int instance_id = static_cast<WebContentsImpl*>(*it)-> | |
| 240 GetBrowserPluginGuest()->instance_id(); | |
| 241 DestroyGuestByInstanceID(instance_id); | |
| 242 } | |
| 243 } | |
| 244 | |
| 245 void BrowserPluginEmbedder::RenderViewDeleted( | |
| 246 RenderViewHost* render_view_host) { | |
| 247 DestroyGuests(); | |
| 248 } | |
| 249 | |
| 250 void BrowserPluginEmbedder::RenderViewGone(base::TerminationStatus status) { | |
| 251 DestroyGuests(); | |
| 252 } | |
| 253 | |
| 254 void BrowserPluginEmbedder::WebContentsVisibilityChanged(bool visible) { | |
| 255 // If the embedder is hidden we need to hide the guests as well. | |
| 256 for (GuestWebContentsMap::const_iterator it = | |
| 257 guest_web_contents_container_.begin(); | |
| 258 it != guest_web_contents_container_.end(); ++it) { | |
| 259 WebContents* web_contents = it->first; | |
| 260 if (visible) | |
| 261 web_contents->WasShown(); | |
| 262 else | |
| 263 web_contents->WasHidden(); | |
| 264 } | |
| 265 } | |
| 266 | |
| 267 void BrowserPluginEmbedder::PluginDestroyed(int instance_id) { | |
| 268 DestroyGuestByInstanceID(instance_id); | |
| 269 } | |
| 270 | |
| 271 void BrowserPluginEmbedder::Observe(int type, | |
| 272 const NotificationSource& source, | |
| 273 const NotificationDetails& details) { | |
| 274 switch (type) { | |
| 275 case NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED: { | |
| 276 bool visible = *Details<bool>(details).ptr(); | |
| 277 WebContentsVisibilityChanged(visible); | |
| 278 break; | |
| 279 } | |
| 280 default: | |
| 281 NOTREACHED() << "Unexpected notification type: " << type; | |
| 282 } | |
| 283 } | |
| 284 | |
| 285 } // namespace content | |
| OLD | NEW |