Index: content/renderer/browser_plugin/browser_plugin.cc |
diff --git a/content/renderer/browser_plugin/browser_plugin.cc b/content/renderer/browser_plugin/browser_plugin.cc |
index 6d0c17d56d720780021ec63911ec4499cdfb9cd7..67160c82116c9b596f409fe0d473577746293340 100644 |
--- a/content/renderer/browser_plugin/browser_plugin.cc |
+++ b/content/renderer/browser_plugin/browser_plugin.cc |
@@ -45,6 +45,29 @@ using PluginContainerMap = |
std::map<blink::WebPluginContainer*, content::BrowserPlugin*>; |
static base::LazyInstance<PluginContainerMap> g_plugin_container_map = |
LAZY_INSTANCE_INITIALIZER; |
+ |
+void UpdateInternalInstanceIdAndBindDelegateToGC( |
+ int browser_plugin_instance_id, |
+ const base::WeakPtr<content::BrowserPlugin>& plugin, |
+ content::BrowserPluginDelegate* delegate) { |
+ // If the plugin is gone, then delete the delegate to avoid leaking it. |
+ if (!plugin.get()) { |
+ delete delegate; |
+ return; |
+ } |
+ |
+ // Updating "internalinstanceid" will bind the |delegate|'s lifetime to |
+ // <webview> element's garbage collection. |
+ // |
+ // This is a way to notify observers of our attributes that this plugin is |
+ // available in render tree. |
+ // TODO(lazyboy): This should be done through the delegate instead. Perhaps |
+ // by firing an event from there. |
+ plugin->UpdateDOMAttribute( |
+ "internalinstanceid", |
+ base::UTF8ToUTF16(base::IntToString(browser_plugin_instance_id))); |
+} |
+ |
} // namespace |
namespace content { |
@@ -61,7 +84,8 @@ BrowserPlugin* BrowserPlugin::GetFromNode(blink::WebNode& node) { |
} |
BrowserPlugin::BrowserPlugin(RenderFrame* render_frame, |
- scoped_ptr<BrowserPluginDelegate> delegate) |
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,
|
+ const std::string& mime_type, |
+ const GURL& original_url) |
: attached_(false), |
render_frame_routing_id_(render_frame->GetRoutingID()), |
container_(nullptr), |
@@ -73,19 +97,22 @@ BrowserPlugin::BrowserPlugin(RenderFrame* render_frame, |
ready_(false), |
browser_plugin_instance_id_(browser_plugin::kInstanceIDNone), |
contents_opaque_(true), |
- delegate_(delegate.Pass()), |
+ delegate_(nullptr), |
+ mime_type_(mime_type), |
+ original_url_(original_url), |
weak_ptr_factory_(this) { |
browser_plugin_instance_id_ = |
BrowserPluginManager::Get()->GetNextInstanceID(); |
- |
- if (delegate_) |
- delegate_->SetElementInstanceID(browser_plugin_instance_id_); |
} |
BrowserPlugin::~BrowserPlugin() { |
if (compositing_helper_.get()) |
compositing_helper_->OnContainerDestroy(); |
+ if (delegate_) |
+ delegate_->WillDestroy(); |
+ delegate_ = nullptr; |
+ |
BrowserPluginManager::Get()->RemoveBrowserPlugin(browser_plugin_instance_id_); |
} |
@@ -299,11 +326,18 @@ bool BrowserPlugin::initialize(WebPluginContainer* container) { |
BrowserPluginManager::Get()->AddBrowserPlugin( |
browser_plugin_instance_id_, this); |
+ delegate_ = |
+ 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 ^^^
|
+ RenderFrameImpl::FromRoutingID(render_frame_routing_id()), mime_type_, |
+ original_url_); |
+ delegate_->SetElementInstanceID(browser_plugin_instance_id_); |
+ |
// Defer attach call so that if there's any pending browser plugin |
// destruction, then it can progress first. |
base::ThreadTaskRunnerHandle::Get()->PostTask( |
- FROM_HERE, base::Bind(&BrowserPlugin::UpdateInternalInstanceId, |
- weak_ptr_factory_.GetWeakPtr())); |
+ FROM_HERE, base::Bind(&UpdateInternalInstanceIdAndBindDelegateToGC, |
+ browser_plugin_instance_id_, |
+ weak_ptr_factory_.GetWeakPtr(), delegate_)); |
return true; |
} |
@@ -329,16 +363,6 @@ void BrowserPlugin::EnableCompositing(bool enable) { |
} |
} |
-void BrowserPlugin::UpdateInternalInstanceId() { |
- // This is a way to notify observers of our attributes that this plugin is |
- // available in render tree. |
- // TODO(lazyboy): This should be done through the delegate instead. Perhaps |
- // by firing an event from there. |
- UpdateDOMAttribute( |
- "internalinstanceid", |
- base::UTF8ToUTF16(base::IntToString(browser_plugin_instance_id_))); |
-} |
- |
void BrowserPlugin::destroy() { |
if (container_) { |
// The BrowserPlugin's WebPluginContainer is deleted immediately after this |