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 guests_by_instance_id_.find(instance_id); |
| 74 if (it != guests_by_instance_id_.end()) |
| 75 return it->second; |
| 76 return NULL; |
| 77 } |
| 78 |
| 79 void BrowserPluginEmbedder::AddGuest(int instance_id, |
| 80 BrowserPluginGuest* guest, |
| 81 int64 frame_id) { |
| 82 DCHECK(guests_by_instance_id_.find(instance_id) == |
| 83 guests_by_instance_id_.end()); |
| 84 guests_by_instance_id_[instance_id] = guest; |
| 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 // The SiteInstance of a given guest is based on the fact that it's a guest |
| 98 // in addition to which platform application the guest belongs to, rather |
| 99 // than the URL that the guest is being navigated to. |
| 100 const std::string& host = |
| 101 render_view_host->GetSiteInstance()->GetSite().host(); |
| 102 guest_web_contents = static_cast<WebContentsImpl*>( |
| 103 WebContents::CreateGuest(web_contents()->GetBrowserContext(), |
| 104 host, |
| 105 instance_id)); |
| 106 |
| 107 guest = guest_web_contents->GetBrowserPluginGuest(); |
| 108 guest->set_embedder_render_process_host( |
| 109 render_view_host->GetProcess()); |
| 110 |
| 111 guest_web_contents->GetMutableRendererPrefs()-> |
| 112 throttle_input_events = false; |
| 113 AddGuest(instance_id, guest, frame_id); |
| 114 guest_web_contents->SetDelegate(guest); |
| 115 } else { |
| 116 guest_web_contents = static_cast<WebContentsImpl*>(guest->web_contents()); |
| 117 } |
| 118 guest_web_contents->GetController().LoadURL(url, |
| 119 Referrer(), |
| 120 PAGE_TRANSITION_AUTO_SUBFRAME, |
| 121 std::string()); |
| 122 if (!size.IsEmpty()) |
| 123 guest_web_contents->GetView()->SizeContents(size); |
| 124 } |
| 125 |
| 126 void BrowserPluginEmbedder::UpdateRectACK(int instance_id, |
| 127 int message_id, |
| 128 const gfx::Size& size) { |
| 129 BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); |
| 130 if (guest) |
| 131 guest->UpdateRectACK(message_id, size); |
| 132 } |
| 133 |
| 134 void BrowserPluginEmbedder::ResizeGuest(int instance_id, |
| 135 TransportDIB* damage_buffer, |
| 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, gfx::Size(width, height), scale_factor); |
| 146 if (!resize_pending) |
| 147 guest_web_contents->GetView()->SizeContents(gfx::Size(width, height)); |
| 148 } |
| 149 |
| 150 void BrowserPluginEmbedder::SetFocus(int instance_id, |
| 151 bool focused) { |
| 152 BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); |
| 153 if (guest) |
| 154 guest->SetFocus(focused); |
| 155 } |
| 156 |
| 157 void BrowserPluginEmbedder::DestroyGuests() { |
| 158 STLDeleteContainerPairFirstPointers(guest_web_contents_container_.begin(), |
| 159 guest_web_contents_container_.end()); |
| 160 guest_web_contents_container_.clear(); |
| 161 guests_by_instance_id_.clear(); |
| 162 } |
| 163 |
| 164 void BrowserPluginEmbedder::HandleInputEvent(int instance_id, |
| 165 RenderViewHost* render_view_host, |
| 166 const gfx::Rect& guest_rect, |
| 167 const WebKit::WebInputEvent& event, |
| 168 IPC::Message* reply_message) { |
| 169 BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); |
| 170 if (guest) |
| 171 guest->HandleInputEvent(render_view_host, guest_rect, event, reply_message); |
| 172 } |
| 173 |
| 174 void BrowserPluginEmbedder::DestroyGuestByInstanceID(int instance_id) { |
| 175 BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); |
| 176 if (guest) { |
| 177 WebContents* guest_web_contents = guest->web_contents(); |
| 178 |
| 179 GuestWebContentsMap::iterator guest_it = guest_web_contents_container_.find( |
| 180 guest_web_contents); |
| 181 DCHECK(guest_it != guest_web_contents_container_.end()); |
| 182 |
| 183 guest_web_contents_container_.erase(guest_it); |
| 184 guests_by_instance_id_.erase(instance_id); |
| 185 |
| 186 guest->Destroy(); |
| 187 } |
| 188 } |
| 189 |
| 190 void BrowserPluginEmbedder::DidCommitProvisionalLoadForFrame( |
| 191 int64 frame_id, |
| 192 bool is_main_frame, |
| 193 const GURL& url, |
| 194 PageTransition transition_type, |
| 195 RenderViewHost* render_view_host) { |
| 196 // Clean up guests that lie in the frame that we're navigating. |
| 197 // |
| 198 // Subframe's guest(s) clean up is not handled here. They are handled by |
| 199 // either the lifetime of WebContents associated directly with the guest(s) or |
| 200 // by the lifetime of their embedders. Consider the following cases: |
| 201 // |
| 202 // Case 1: (Subframe's guest clean up managed by embedder) A WebContents WC1 |
| 203 // embeds a guest (therefore it is an embedder E1), call this guest G1. G1 |
| 204 // points to a page that has an iframe. Call the iframe's WebContents WC2. Now |
| 205 // the iframe has a guest of its own in it, lets call it G2, which makes WC2 |
| 206 // an embedder as well, call it E2. Now when |
| 207 // DidCommitProvisionalLoadForFrame() is called, G2 won't be cleaned up by the |
| 208 // code below since it doesn't directly belong to the top level frame. However |
| 209 // when the WebContents WC2 gets destroyed, it destroys the embedder E2 |
| 210 // associated with it, eventually destroying all of its guests (G2). |
| 211 // |
| 212 // Case 2: (Subframe's guest cleanup managed by WebContents) A page has an |
| 213 // iframe which points to a page that has a guest G1. Therefore this makes top |
| 214 // level page's WebContents WC1 an embedder E1. Lets say the WebContents |
| 215 // associated with G1 is WC2. When DidCommitProvisionalLoadForFrame() occurs, |
| 216 // G1 is not a guest within the top level frame that called commit, so it |
| 217 // won't be cleaned up with the code below; However since WC2 goes away, this |
| 218 // eventually destroys G1 since G1 is an observer for WC2. |
| 219 // |
| 220 // The following code is repsonsible for deleting the top-level frame which |
| 221 // will eventually invoke one of the two cases above covering all guests in |
| 222 // the whole frame tree. |
| 223 typedef std::set<WebContents*> GuestSet; |
| 224 GuestSet guests_to_delete; |
| 225 for (GuestWebContentsMap::const_iterator it = |
| 226 guest_web_contents_container_.begin(); |
| 227 it != guest_web_contents_container_.end(); ++it) { |
| 228 if (it->second == frame_id) |
| 229 guests_to_delete.insert(it->first); |
| 230 } |
| 231 for (GuestSet::const_iterator it = guests_to_delete.begin(); |
| 232 it != guests_to_delete.end(); ++it) { |
| 233 int instance_id = static_cast<WebContentsImpl*>(*it)-> |
| 234 GetBrowserPluginGuest()->instance_id(); |
| 235 DestroyGuestByInstanceID(instance_id); |
| 236 } |
| 237 } |
| 238 |
| 239 void BrowserPluginEmbedder::RenderViewDeleted( |
| 240 RenderViewHost* render_view_host) { |
| 241 DestroyGuests(); |
| 242 } |
| 243 |
| 244 void BrowserPluginEmbedder::RenderViewGone(base::TerminationStatus status) { |
| 245 DestroyGuests(); |
| 246 } |
| 247 |
| 248 void BrowserPluginEmbedder::WebContentsVisibilityChanged(bool visible) { |
| 249 // If the embedder is hidden we need to hide the guests as well. |
| 250 for (GuestWebContentsMap::const_iterator it = |
| 251 guest_web_contents_container_.begin(); |
| 252 it != guest_web_contents_container_.end(); ++it) { |
| 253 WebContents* web_contents = it->first; |
| 254 if (visible) |
| 255 web_contents->WasShown(); |
| 256 else |
| 257 web_contents->WasHidden(); |
| 258 } |
| 259 } |
| 260 |
| 261 void BrowserPluginEmbedder::PluginDestroyed(int instance_id) { |
| 262 DestroyGuestByInstanceID(instance_id); |
| 263 } |
| 264 |
| 265 void BrowserPluginEmbedder::Observe(int type, |
| 266 const NotificationSource& source, |
| 267 const NotificationDetails& details) { |
| 268 switch (type) { |
| 269 case NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED: { |
| 270 bool visible = *Details<bool>(details).ptr(); |
| 271 WebContentsVisibilityChanged(visible); |
| 272 break; |
| 273 } |
| 274 default: |
| 275 NOTREACHED() << "Unexpected notification type: " << type; |
| 276 } |
| 277 } |
| 278 |
| 279 } // namespace content |
OLD | NEW |