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. |
| 55 DestroyGuests(); |
| 56 } |
| 57 |
| 58 // static |
| 59 BrowserPluginEmbedder* BrowserPluginEmbedder::Create( |
| 60 WebContentsImpl* web_contents, |
| 61 content::RenderViewHost* render_view_host) { |
| 62 if (factory_) { |
| 63 return factory_->CreateBrowserPluginEmbedder(web_contents, |
| 64 render_view_host); |
| 65 } |
| 66 return new BrowserPluginEmbedder(web_contents, render_view_host); |
| 67 } |
| 68 |
| 69 BrowserPluginGuest* BrowserPluginEmbedder::GetGuestByInstanceID( |
| 70 int instance_id) const { |
| 71 ContainerInstanceMap::const_iterator it = |
| 72 guest_web_contents_by_instance_id_.find(instance_id); |
| 73 if (it != guest_web_contents_by_instance_id_.end()) |
| 74 return static_cast<WebContentsImpl*>(it->second)->GetBrowserPluginGuest(); |
| 75 return NULL; |
| 76 } |
| 77 |
| 78 void BrowserPluginEmbedder::AddGuest(int instance_id, |
| 79 WebContents* guest_web_contents, |
| 80 int64 frame_id) { |
| 81 DCHECK(guest_web_contents_by_instance_id_.find(instance_id) == |
| 82 guest_web_contents_by_instance_id_.end()); |
| 83 guest_web_contents_by_instance_id_[instance_id] = guest_web_contents; |
| 84 } |
| 85 |
| 86 void BrowserPluginEmbedder::NavigateGuest(RenderViewHost* render_view_host, |
| 87 int instance_id, |
| 88 int64 frame_id, |
| 89 const std::string& src, |
| 90 const gfx::Size& size) { |
| 91 BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); |
| 92 WebContentsImpl* guest_web_contents = NULL; |
| 93 GURL url(src); |
| 94 if (!guest) { |
| 95 const std::string& host = |
| 96 render_view_host->GetSiteInstance()->GetSite().host(); |
| 97 guest_web_contents = WebContentsImpl::CreateGuest( |
| 98 web_contents()->GetBrowserContext(), |
| 99 host, |
| 100 instance_id); |
| 101 |
| 102 guest = guest_web_contents->GetBrowserPluginGuest(); |
| 103 guest->set_embedder_render_process_host( |
| 104 render_view_host->GetProcess()); |
| 105 |
| 106 guest_web_contents->GetMutableRendererPrefs()-> |
| 107 throttle_input_events = false; |
| 108 AddGuest(instance_id, guest_web_contents, frame_id); |
| 109 guest_web_contents->SetDelegate(guest); |
| 110 } else { |
| 111 guest_web_contents = static_cast<WebContentsImpl*>(guest->web_contents()); |
| 112 } |
| 113 |
| 114 // We ignore loading empty urls in web_contents. |
| 115 // If a guest sets empty src attribute after it has navigated to some |
| 116 // non-empty page, the action is considered no-op. |
| 117 // TODO(lazyboy): The js shim for browser-plugin might need to reflect empty |
| 118 // src ignoring in the shadow DOM element: http://crbug.com/149001. |
| 119 if (!src.empty()) { |
| 120 guest_web_contents->GetController().LoadURL(url, |
| 121 Referrer(), |
| 122 PAGE_TRANSITION_AUTO_SUBFRAME, |
| 123 std::string()); |
| 124 } |
| 125 |
| 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 #if defined(OS_WIN) |
| 141 int damage_buffer_size, |
| 142 #endif |
| 143 int width, |
| 144 int height, |
| 145 bool resize_pending, |
| 146 float scale_factor) { |
| 147 BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); |
| 148 if (!guest) |
| 149 return; |
| 150 WebContentsImpl* guest_web_contents = |
| 151 static_cast<WebContentsImpl*>(guest->web_contents()); |
| 152 guest->SetDamageBuffer(damage_buffer, |
| 153 #if defined(OS_WIN) |
| 154 damage_buffer_size, |
| 155 #endif |
| 156 gfx::Size(width, height), |
| 157 scale_factor); |
| 158 if (!resize_pending) |
| 159 guest_web_contents->GetView()->SizeContents(gfx::Size(width, height)); |
| 160 } |
| 161 |
| 162 void BrowserPluginEmbedder::SetFocus(int instance_id, |
| 163 bool focused) { |
| 164 BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); |
| 165 if (guest) |
| 166 guest->SetFocus(focused); |
| 167 } |
| 168 |
| 169 void BrowserPluginEmbedder::DestroyGuests() { |
| 170 STLDeleteContainerPairSecondPointers( |
| 171 guest_web_contents_by_instance_id_.begin(), |
| 172 guest_web_contents_by_instance_id_.end()); |
| 173 guest_web_contents_by_instance_id_.clear(); |
| 174 } |
| 175 |
| 176 void BrowserPluginEmbedder::HandleInputEvent(int instance_id, |
| 177 RenderViewHost* render_view_host, |
| 178 const gfx::Rect& guest_rect, |
| 179 const WebKit::WebInputEvent& event, |
| 180 IPC::Message* reply_message) { |
| 181 BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); |
| 182 if (guest) |
| 183 guest->HandleInputEvent(render_view_host, guest_rect, event, reply_message); |
| 184 } |
| 185 |
| 186 void BrowserPluginEmbedder::DestroyGuestByInstanceID(int instance_id) { |
| 187 BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); |
| 188 if (guest) { |
| 189 WebContents* guest_web_contents = guest->web_contents(); |
| 190 |
| 191 // Destroy the guest's web_contents. |
| 192 delete guest_web_contents; |
| 193 guest_web_contents_by_instance_id_.erase(instance_id); |
| 194 } |
| 195 } |
| 196 |
| 197 void BrowserPluginEmbedder::RenderViewDeleted( |
| 198 RenderViewHost* render_view_host) { |
| 199 DestroyGuests(); |
| 200 } |
| 201 |
| 202 void BrowserPluginEmbedder::RenderViewGone(base::TerminationStatus status) { |
| 203 DestroyGuests(); |
| 204 } |
| 205 |
| 206 void BrowserPluginEmbedder::WebContentsVisibilityChanged(bool visible) { |
| 207 // If the embedder is hidden we need to hide the guests as well. |
| 208 for (ContainerInstanceMap::const_iterator it = |
| 209 guest_web_contents_by_instance_id_.begin(); |
| 210 it != guest_web_contents_by_instance_id_.end(); ++it) { |
| 211 WebContents* web_contents = it->second; |
| 212 if (visible) |
| 213 web_contents->WasShown(); |
| 214 else |
| 215 web_contents->WasHidden(); |
| 216 } |
| 217 } |
| 218 |
| 219 void BrowserPluginEmbedder::PluginDestroyed(int instance_id) { |
| 220 DestroyGuestByInstanceID(instance_id); |
| 221 } |
| 222 |
| 223 void BrowserPluginEmbedder::Observe(int type, |
| 224 const NotificationSource& source, |
| 225 const NotificationDetails& details) { |
| 226 switch (type) { |
| 227 case NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED: { |
| 228 bool visible = *Details<bool>(details).ptr(); |
| 229 WebContentsVisibilityChanged(visible); |
| 230 break; |
| 231 } |
| 232 default: |
| 233 NOTREACHED() << "Unexpected notification type: " << type; |
| 234 } |
| 235 } |
| 236 |
| 237 } // namespace content |
OLD | NEW |