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_host.h" |
| 6 |
| 7 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 8 #include "content/browser/web_contents/web_contents_impl.h" |
| 9 #include "content/common/browser_plugin_messages.h" |
| 10 #include "content/public/browser/notification_details.h" |
| 11 #include "content/public/browser/notification_source.h" |
| 12 #include "content/public/browser/notification_types.h" |
| 13 #include "content/public/browser/render_process_host.h" |
| 14 #include "content/public/browser/render_widget_host_view.h" |
| 15 #include "content/public/browser/site_instance.h" |
| 16 #include "content/public/browser/web_contents_view.h" |
| 17 #include "ppapi/proxy/ppapi_messages.h" |
| 18 |
| 19 namespace content { |
| 20 |
| 21 BrowserPluginHost::BrowserPluginHost( |
| 22 WebContentsImpl* web_contents) |
| 23 : WebContentsObserver(web_contents), |
| 24 embedder_render_process_host_(NULL), |
| 25 instance_id_(0), |
| 26 pending_render_view_host_(NULL) { |
| 27 // Listen to visibility changes so that an embedder hides its guests |
| 28 // as well. |
| 29 registrar_.Add(this, |
| 30 NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED, |
| 31 Source<WebContents>(web_contents)); |
| 32 } |
| 33 |
| 34 BrowserPluginHost::~BrowserPluginHost() { |
| 35 } |
| 36 |
| 37 BrowserPluginHost* BrowserPluginHost::GetGuestByContainerID(int container_id) { |
| 38 ContainerInstanceMap::const_iterator it = |
| 39 guests_by_container_id_.find(container_id); |
| 40 if (it != guests_by_container_id_.end()) |
| 41 return it->second; |
| 42 return NULL; |
| 43 } |
| 44 |
| 45 void BrowserPluginHost::RegisterContainerInstance( |
| 46 int container_id, |
| 47 BrowserPluginHost* observer) { |
| 48 DCHECK(guests_by_container_id_.find(container_id) == |
| 49 guests_by_container_id_.end()); |
| 50 guests_by_container_id_[container_id] = observer; |
| 51 } |
| 52 |
| 53 void BrowserPluginHost::OnPendingNavigation(RenderViewHost* dest_rvh) { |
| 54 if (web_contents()->GetRenderViewHost() != dest_rvh) { |
| 55 pending_render_view_host_ = dest_rvh; |
| 56 } |
| 57 } |
| 58 |
| 59 bool BrowserPluginHost::OnMessageReceived(const IPC::Message& message) { |
| 60 bool handled = true; |
| 61 IPC_BEGIN_MESSAGE_MAP(BrowserPluginHost, message) |
| 62 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_NavigateFromGuest, |
| 63 OnNavigateFromGuest) |
| 64 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_MapInstance, |
| 65 OnMapInstance) |
| 66 IPC_MESSAGE_UNHANDLED(handled = false) |
| 67 IPC_END_MESSAGE_MAP() |
| 68 return handled; |
| 69 } |
| 70 |
| 71 void BrowserPluginHost::NavigateGuestFromEmbedder( |
| 72 RenderViewHost* render_view_host, |
| 73 int container_instance_id, |
| 74 long long frame_id, |
| 75 const std::string& src, |
| 76 const gfx::Size& size) { |
| 77 BrowserPluginHost* guest_observer = |
| 78 GetGuestByContainerID(container_instance_id); |
| 79 WebContentsImpl* guest_web_contents = |
| 80 guest_observer ? |
| 81 static_cast<WebContentsImpl*>(guest_observer->web_contents()): NULL; |
| 82 if (!guest_observer) { |
| 83 guest_web_contents = |
| 84 static_cast<WebContentsImpl*>( |
| 85 WebContents::Create( |
| 86 web_contents()->GetBrowserContext(), |
| 87 render_view_host->GetSiteInstance(), |
| 88 MSG_ROUTING_NONE, |
| 89 NULL, // base WebContents |
| 90 NULL // session storage namespace |
| 91 )); |
| 92 guest_observer = |
| 93 guest_web_contents->browser_plugin_host(); |
| 94 guest_observer->set_embedder_render_process_host( |
| 95 render_view_host->GetProcess()); |
| 96 guest_observer->set_instance_id(container_instance_id); |
| 97 guest_observer->set_initial_size(size); |
| 98 RegisterContainerInstance(container_instance_id, guest_observer); |
| 99 AddGuest(guest_web_contents, frame_id); |
| 100 } |
| 101 |
| 102 GURL url(src); |
| 103 guest_observer->web_contents()->SetDelegate(guest_observer); |
| 104 guest_observer->web_contents()->GetController().LoadURL( |
| 105 url, |
| 106 Referrer(), |
| 107 PAGE_TRANSITION_AUTO_SUBFRAME, |
| 108 std::string()); |
| 109 } |
| 110 |
| 111 void BrowserPluginHost::OnNavigateFromGuest( |
| 112 PP_Instance instance, |
| 113 const std::string& src) { |
| 114 DCHECK(embedder_render_process_host()); |
| 115 GURL url(src); |
| 116 web_contents()->GetController().LoadURL( |
| 117 url, |
| 118 Referrer(), |
| 119 PAGE_TRANSITION_AUTO_SUBFRAME, |
| 120 std::string()); |
| 121 } |
| 122 |
| 123 // TODO(fsamuel): This handler is all kinds of bad and could be racy. |
| 124 // Between the time we set the pending_render_view_host and use it here, |
| 125 // the pending_render_view_host may no longer be valid and this message |
| 126 // may not go to the right place. |
| 127 // See https://code.google.com/p/chromium/issues/detail?id=128976. |
| 128 // The correct solution is probably to send |
| 129 // "BrowserPluginMsg_CompleteNavigation" over the pepper channel to the guest |
| 130 // and then have the guest send the browser "BrowserPluginHostMsg_ResizeGuest" |
| 131 // to resize appropriately. |
| 132 void BrowserPluginHost::OnMapInstance(int container_instance_id, |
| 133 PP_Instance instance) { |
| 134 BrowserPluginHost* guest_observer = |
| 135 GetGuestByContainerID(container_instance_id); |
| 136 WebContentsImpl* guest_web_contents = |
| 137 static_cast<WebContentsImpl*>(guest_observer->web_contents()); |
| 138 RenderViewHost* rvh = guest_observer->pending_render_view_host() ? |
| 139 guest_observer->pending_render_view_host() : |
| 140 guest_web_contents->GetRenderViewHost(); |
| 141 |
| 142 guest_web_contents->GetView()->SizeContents(guest_observer->initial_size()); |
| 143 |
| 144 rvh->Send(new BrowserPluginMsg_CompleteNavigation( |
| 145 rvh->GetRoutingID(), |
| 146 instance)); |
| 147 } |
| 148 |
| 149 void BrowserPluginHost::ConnectEmbedderToChannel( |
| 150 RenderViewHost* render_view_host, |
| 151 const IPC::ChannelHandle& channel_handle) { |
| 152 DCHECK(embedder_render_process_host()); |
| 153 // Tell the BrowserPlugin in the embedder that we're done and that it can |
| 154 // begin using the guest renderer. |
| 155 embedder_render_process_host()->Send( |
| 156 new BrowserPluginMsg_LoadGuest( |
| 157 instance_id(), |
| 158 render_view_host->GetProcess()-> |
| 159 GetID(), |
| 160 channel_handle)); |
| 161 } |
| 162 |
| 163 void BrowserPluginHost::AddGuest(WebContentsImpl* guest, int64 frame_id) { |
| 164 guests_[guest] = frame_id; |
| 165 } |
| 166 |
| 167 void BrowserPluginHost::RemoveGuest(WebContentsImpl* guest) { |
| 168 guests_.erase(guest); |
| 169 } |
| 170 |
| 171 void BrowserPluginHost::DestroyGuests() { |
| 172 for (GuestMap::const_iterator it = guests_.begin(); |
| 173 it != guests_.end(); ++it) { |
| 174 WebContentsImpl* web_contents = it->first; |
| 175 delete web_contents; |
| 176 } |
| 177 guests_.clear(); |
| 178 guests_by_container_id_.clear(); |
| 179 } |
| 180 |
| 181 void BrowserPluginHost::DidCommitProvisionalLoadForFrame( |
| 182 int64 frame_id, |
| 183 bool is_main_frame, |
| 184 const GURL& url, |
| 185 PageTransition transition_type, |
| 186 RenderViewHost* render_view_host) { |
| 187 // Clean-up guests that lie in the frame that we're navigating. |
| 188 typedef std::set<WebContentsImpl*> GuestSet; |
| 189 GuestSet guests_to_delete; |
| 190 for (GuestMap::const_iterator it = guests_.begin(); |
| 191 it != guests_.end(); ++it) { |
| 192 WebContentsImpl* web_contents = it->first; |
| 193 if (it->second == frame_id) { |
| 194 guests_to_delete.insert(web_contents); |
| 195 } |
| 196 } |
| 197 for (GuestSet::const_iterator it = guests_to_delete.begin(); |
| 198 it != guests_to_delete.end(); ++it) { |
| 199 delete *it; |
| 200 guests_.erase(*it); |
| 201 } |
| 202 } |
| 203 |
| 204 void BrowserPluginHost::RenderViewDeleted(RenderViewHost* render_view_host) { |
| 205 // TODO(fsamuel): For some reason ToT hangs when killing a guest, this wasn't |
| 206 // the case earlier. Presumably, when a guest is killed/crashes, one of |
| 207 // RenderViewDeleted, RenderViewGone or WebContentsDestroyed will get called. |
| 208 // At that point, we should remove reference to this BrowserPluginHost |
| 209 // from its embedder, in addition to destroying its guests, to ensure |
| 210 // that we do not attempt a double delete. |
| 211 DestroyGuests(); |
| 212 } |
| 213 |
| 214 void BrowserPluginHost::RenderViewGone(base::TerminationStatus status) { |
| 215 DestroyGuests(); |
| 216 } |
| 217 |
| 218 void BrowserPluginHost::WebContentsDestroyed(WebContents* web_contents) { |
| 219 DestroyGuests(); |
| 220 } |
| 221 |
| 222 void BrowserPluginHost::Observe( |
| 223 int type, |
| 224 const NotificationSource& source, |
| 225 const NotificationDetails& details) { |
| 226 DCHECK(type == NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED); |
| 227 bool visible = *Details<bool>(details).ptr(); |
| 228 |
| 229 // If the embedder is hidden we need to hide the guests as well. |
| 230 for (GuestMap::const_iterator it = guests_.begin(); |
| 231 it != guests_.end(); ++it) { |
| 232 WebContentsImpl* web_contents = it->first; |
| 233 if (visible) |
| 234 web_contents->ShowContents(); |
| 235 else |
| 236 web_contents->HideContents(); |
| 237 } |
| 238 } |
| 239 |
| 240 } // namespace content |
OLD | NEW |