OLD | NEW |
---|---|
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/browser_plugin/browser_plugin.h" | 5 #include "content/renderer/browser_plugin/browser_plugin.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 27 matching lines...) Expand all Loading... | |
38 using blink::WebPoint; | 38 using blink::WebPoint; |
39 using blink::WebRect; | 39 using blink::WebRect; |
40 using blink::WebURL; | 40 using blink::WebURL; |
41 using blink::WebVector; | 41 using blink::WebVector; |
42 | 42 |
43 namespace { | 43 namespace { |
44 using PluginContainerMap = | 44 using PluginContainerMap = |
45 std::map<blink::WebPluginContainer*, content::BrowserPlugin*>; | 45 std::map<blink::WebPluginContainer*, content::BrowserPlugin*>; |
46 static base::LazyInstance<PluginContainerMap> g_plugin_container_map = | 46 static base::LazyInstance<PluginContainerMap> g_plugin_container_map = |
47 LAZY_INSTANCE_INITIALIZER; | 47 LAZY_INSTANCE_INITIALIZER; |
48 | |
49 void UpdateInternalInstanceIdAndBindDelegateToGC( | |
50 int browser_plugin_instance_id, | |
51 const base::WeakPtr<content::BrowserPlugin>& plugin, | |
52 content::BrowserPluginDelegate* delegate) { | |
53 // If the plugin is gone, then delete the delegate to avoid leaking it. | |
54 if (!plugin.get()) { | |
55 delete delegate; | |
56 return; | |
57 } | |
58 | |
59 // Updating "internalinstanceid" will bind the |delegate|'s lifetime to | |
60 // <webview> element's garbage collection. | |
61 // | |
62 // This is a way to notify observers of our attributes that this plugin is | |
63 // available in render tree. | |
64 // TODO(lazyboy): This should be done through the delegate instead. Perhaps | |
65 // by firing an event from there. | |
66 plugin->UpdateDOMAttribute( | |
67 "internalinstanceid", | |
68 base::UTF8ToUTF16(base::IntToString(browser_plugin_instance_id))); | |
69 } | |
70 | |
48 } // namespace | 71 } // namespace |
49 | 72 |
50 namespace content { | 73 namespace content { |
51 | 74 |
52 // static | 75 // static |
53 BrowserPlugin* BrowserPlugin::GetFromNode(blink::WebNode& node) { | 76 BrowserPlugin* BrowserPlugin::GetFromNode(blink::WebNode& node) { |
54 blink::WebPluginContainer* container = node.pluginContainer(); | 77 blink::WebPluginContainer* container = node.pluginContainer(); |
55 if (!container) | 78 if (!container) |
56 return nullptr; | 79 return nullptr; |
57 | 80 |
58 PluginContainerMap* browser_plugins = g_plugin_container_map.Pointer(); | 81 PluginContainerMap* browser_plugins = g_plugin_container_map.Pointer(); |
59 PluginContainerMap::iterator it = browser_plugins->find(container); | 82 PluginContainerMap::iterator it = browser_plugins->find(container); |
60 return it == browser_plugins->end() ? nullptr : it->second; | 83 return it == browser_plugins->end() ? nullptr : it->second; |
61 } | 84 } |
62 | 85 |
63 BrowserPlugin::BrowserPlugin(RenderFrame* render_frame, | 86 BrowserPlugin::BrowserPlugin(RenderFrame* render_frame, |
64 scoped_ptr<BrowserPluginDelegate> delegate) | 87 const std::string& mime_type, |
Fady Samuel
2015/06/05 15:10:41
Why did you make this change?
lazyboy
2015/06/05 16:56:55
We don't want to own BPDelegate by BrowserPlugin,
| |
88 const GURL& original_url) | |
65 : attached_(false), | 89 : attached_(false), |
66 render_frame_routing_id_(render_frame->GetRoutingID()), | 90 render_frame_routing_id_(render_frame->GetRoutingID()), |
67 container_(nullptr), | 91 container_(nullptr), |
68 sad_guest_(nullptr), | 92 sad_guest_(nullptr), |
69 guest_crashed_(false), | 93 guest_crashed_(false), |
70 plugin_focused_(false), | 94 plugin_focused_(false), |
71 visible_(true), | 95 visible_(true), |
72 mouse_locked_(false), | 96 mouse_locked_(false), |
73 ready_(false), | 97 ready_(false), |
74 browser_plugin_instance_id_(browser_plugin::kInstanceIDNone), | 98 browser_plugin_instance_id_(browser_plugin::kInstanceIDNone), |
75 contents_opaque_(true), | 99 contents_opaque_(true), |
76 delegate_(delegate.Pass()), | 100 delegate_(nullptr), |
101 mime_type_(mime_type), | |
102 original_url_(original_url), | |
77 weak_ptr_factory_(this) { | 103 weak_ptr_factory_(this) { |
78 browser_plugin_instance_id_ = | 104 browser_plugin_instance_id_ = |
79 BrowserPluginManager::Get()->GetNextInstanceID(); | 105 BrowserPluginManager::Get()->GetNextInstanceID(); |
80 | |
81 if (delegate_) | |
82 delegate_->SetElementInstanceID(browser_plugin_instance_id_); | |
83 } | 106 } |
84 | 107 |
85 BrowserPlugin::~BrowserPlugin() { | 108 BrowserPlugin::~BrowserPlugin() { |
86 if (compositing_helper_.get()) | 109 if (compositing_helper_.get()) |
87 compositing_helper_->OnContainerDestroy(); | 110 compositing_helper_->OnContainerDestroy(); |
88 | 111 |
112 if (delegate_) | |
113 delegate_->WillDestroy(); | |
114 delegate_ = nullptr; | |
115 | |
89 BrowserPluginManager::Get()->RemoveBrowserPlugin(browser_plugin_instance_id_); | 116 BrowserPluginManager::Get()->RemoveBrowserPlugin(browser_plugin_instance_id_); |
90 } | 117 } |
91 | 118 |
92 bool BrowserPlugin::OnMessageReceived(const IPC::Message& message) { | 119 bool BrowserPlugin::OnMessageReceived(const IPC::Message& message) { |
93 bool handled = true; | 120 bool handled = true; |
94 IPC_BEGIN_MESSAGE_MAP(BrowserPlugin, message) | 121 IPC_BEGIN_MESSAGE_MAP(BrowserPlugin, message) |
95 IPC_MESSAGE_HANDLER(BrowserPluginMsg_AdvanceFocus, OnAdvanceFocus) | 122 IPC_MESSAGE_HANDLER(BrowserPluginMsg_AdvanceFocus, OnAdvanceFocus) |
96 IPC_MESSAGE_HANDLER_GENERIC(BrowserPluginMsg_CompositorFrameSwapped, | 123 IPC_MESSAGE_HANDLER_GENERIC(BrowserPluginMsg_CompositorFrameSwapped, |
97 OnCompositorFrameSwapped(message)) | 124 OnCompositorFrameSwapped(message)) |
98 IPC_MESSAGE_HANDLER(BrowserPluginMsg_GuestGone, OnGuestGone) | 125 IPC_MESSAGE_HANDLER(BrowserPluginMsg_GuestGone, OnGuestGone) |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
292 return false; | 319 return false; |
293 | 320 |
294 container_ = container; | 321 container_ = container; |
295 container_->setWantsWheelEvents(true); | 322 container_->setWantsWheelEvents(true); |
296 | 323 |
297 g_plugin_container_map.Get().insert(std::make_pair(container_, this)); | 324 g_plugin_container_map.Get().insert(std::make_pair(container_, this)); |
298 | 325 |
299 BrowserPluginManager::Get()->AddBrowserPlugin( | 326 BrowserPluginManager::Get()->AddBrowserPlugin( |
300 browser_plugin_instance_id_, this); | 327 browser_plugin_instance_id_, this); |
301 | 328 |
329 delegate_ = | |
330 content::GetContentClient()->renderer()->CreateBrowserPluginDelegate( | |
Fady Samuel
2015/06/05 15:10:41
Why move this code here?
lazyboy
2015/06/05 16:56:55
See ^^^
| |
331 RenderFrameImpl::FromRoutingID(render_frame_routing_id()), mime_type_, | |
332 original_url_); | |
333 delegate_->SetElementInstanceID(browser_plugin_instance_id_); | |
334 | |
302 // Defer attach call so that if there's any pending browser plugin | 335 // Defer attach call so that if there's any pending browser plugin |
303 // destruction, then it can progress first. | 336 // destruction, then it can progress first. |
304 base::ThreadTaskRunnerHandle::Get()->PostTask( | 337 base::ThreadTaskRunnerHandle::Get()->PostTask( |
305 FROM_HERE, base::Bind(&BrowserPlugin::UpdateInternalInstanceId, | 338 FROM_HERE, base::Bind(&UpdateInternalInstanceIdAndBindDelegateToGC, |
306 weak_ptr_factory_.GetWeakPtr())); | 339 browser_plugin_instance_id_, |
340 weak_ptr_factory_.GetWeakPtr(), delegate_)); | |
307 return true; | 341 return true; |
308 } | 342 } |
309 | 343 |
310 void BrowserPlugin::EnableCompositing(bool enable) { | 344 void BrowserPlugin::EnableCompositing(bool enable) { |
311 bool enabled = !!compositing_helper_.get(); | 345 bool enabled = !!compositing_helper_.get(); |
312 if (enabled == enable) | 346 if (enabled == enable) |
313 return; | 347 return; |
314 | 348 |
315 if (enable) { | 349 if (enable) { |
316 DCHECK(!compositing_helper_.get()); | 350 DCHECK(!compositing_helper_.get()); |
317 if (!compositing_helper_.get()) { | 351 if (!compositing_helper_.get()) { |
318 compositing_helper_ = ChildFrameCompositingHelper::CreateForBrowserPlugin( | 352 compositing_helper_ = ChildFrameCompositingHelper::CreateForBrowserPlugin( |
319 weak_ptr_factory_.GetWeakPtr()); | 353 weak_ptr_factory_.GetWeakPtr()); |
320 } | 354 } |
321 } | 355 } |
322 compositing_helper_->EnableCompositing(enable); | 356 compositing_helper_->EnableCompositing(enable); |
323 compositing_helper_->SetContentsOpaque(contents_opaque_); | 357 compositing_helper_->SetContentsOpaque(contents_opaque_); |
324 | 358 |
325 if (!enable) { | 359 if (!enable) { |
326 DCHECK(compositing_helper_.get()); | 360 DCHECK(compositing_helper_.get()); |
327 compositing_helper_->OnContainerDestroy(); | 361 compositing_helper_->OnContainerDestroy(); |
328 compositing_helper_ = nullptr; | 362 compositing_helper_ = nullptr; |
329 } | 363 } |
330 } | 364 } |
331 | 365 |
332 void BrowserPlugin::UpdateInternalInstanceId() { | |
333 // This is a way to notify observers of our attributes that this plugin is | |
334 // available in render tree. | |
335 // TODO(lazyboy): This should be done through the delegate instead. Perhaps | |
336 // by firing an event from there. | |
337 UpdateDOMAttribute( | |
338 "internalinstanceid", | |
339 base::UTF8ToUTF16(base::IntToString(browser_plugin_instance_id_))); | |
340 } | |
341 | |
342 void BrowserPlugin::destroy() { | 366 void BrowserPlugin::destroy() { |
343 if (container_) { | 367 if (container_) { |
344 // The BrowserPlugin's WebPluginContainer is deleted immediately after this | 368 // The BrowserPlugin's WebPluginContainer is deleted immediately after this |
345 // call returns, so let's not keep a reference to it around. | 369 // call returns, so let's not keep a reference to it around. |
346 g_plugin_container_map.Get().erase(container_); | 370 g_plugin_container_map.Get().erase(container_); |
347 } | 371 } |
348 | 372 |
349 container_ = nullptr; | 373 container_ = nullptr; |
350 // Will be a no-op if the mouse is not currently locked. | 374 // Will be a no-op if the mouse is not currently locked. |
351 auto render_frame = RenderFrameImpl::FromRoutingID(render_frame_routing_id()); | 375 auto render_frame = RenderFrameImpl::FromRoutingID(render_frame_routing_id()); |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
610 bool BrowserPlugin::HandleMouseLockedInputEvent( | 634 bool BrowserPlugin::HandleMouseLockedInputEvent( |
611 const blink::WebMouseEvent& event) { | 635 const blink::WebMouseEvent& event) { |
612 BrowserPluginManager::Get()->Send( | 636 BrowserPluginManager::Get()->Send( |
613 new BrowserPluginHostMsg_HandleInputEvent(browser_plugin_instance_id_, | 637 new BrowserPluginHostMsg_HandleInputEvent(browser_plugin_instance_id_, |
614 view_rect_, | 638 view_rect_, |
615 &event)); | 639 &event)); |
616 return true; | 640 return true; |
617 } | 641 } |
618 | 642 |
619 } // namespace content | 643 } // namespace content |
OLD | NEW |