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 3962ccfcab2c2aedc83fb37745842bbd9f78155e..fa155e718a2caf618f125396dc9b5dd7cbbf2e09 100644 |
--- a/content/renderer/browser_plugin/browser_plugin.cc |
+++ b/content/renderer/browser_plugin/browser_plugin.cc |
@@ -40,6 +40,14 @@ using blink::WebRect; |
using blink::WebURL; |
using blink::WebVector; |
+namespace { |
+typedef std::map<blink::WebPluginContainer*, content::BrowserPlugin*> |
+ PluginContainerMap; |
+static base::LazyInstance<PluginContainerMap> g_plugin_container_map = |
+ LAZY_INSTANCE_INITIALIZER; |
+ |
+} // namespace |
+ |
namespace content { |
BrowserPlugin::BrowserPlugin(RenderViewImpl* render_view, |
@@ -47,6 +55,7 @@ BrowserPlugin::BrowserPlugin(RenderViewImpl* render_view, |
bool auto_navigate) |
: guest_instance_id_(browser_plugin::kInstanceIDNone), |
attached_(false), |
+ attach_pending_(false), |
render_view_(render_view->AsWeakPtr()), |
render_view_routing_id_(render_view->GetRoutingID()), |
container_(NULL), |
@@ -61,14 +70,18 @@ BrowserPlugin::BrowserPlugin(RenderViewImpl* render_view, |
auto_navigate_(auto_navigate), |
mouse_locked_(false), |
browser_plugin_manager_(render_view->GetBrowserPluginManager()), |
+ in_render_tree_(false), |
weak_ptr_factory_(this) { |
} |
BrowserPlugin::~BrowserPlugin() { |
+ if (in_render_tree_) { |
+ browser_plugin_manager()->RemoveBrowserPluginInternal(instance_id_); |
+ } |
// If the BrowserPlugin has never navigated then the browser process and |
// BrowserPluginManager don't know about it and so there is nothing to do |
// here. |
- if (!HasGuestInstanceID()) |
+ if (!attached()) |
return; |
browser_plugin_manager()->RemoveBrowserPlugin(guest_instance_id_); |
browser_plugin_manager()->Send( |
@@ -205,7 +218,7 @@ int BrowserPlugin::GetAdjustedMinWidth() const { |
} |
void BrowserPlugin::ParseAllowTransparencyAttribute() { |
- if (!HasGuestInstanceID()) |
+ if (!attached()) |
return; |
bool opaque = !GetAllowTransparencyAttribute(); |
@@ -246,7 +259,7 @@ void BrowserPlugin::UpdateGuestAutoSizeState(bool auto_size_enabled) { |
// If we haven't yet heard back from the guest about the last resize request, |
// then we don't issue another request until we do in |
// BrowserPlugin::OnUpdateRect. |
- if (!HasGuestInstanceID() || !paint_ack_received_) |
+ if (!attached() || !paint_ack_received_) |
return; |
BrowserPluginHostMsg_AutoSize_Params auto_size_params; |
@@ -264,14 +277,12 @@ void BrowserPlugin::UpdateGuestAutoSizeState(bool auto_size_enabled) { |
resize_guest_params)); |
} |
-void BrowserPlugin::Attach(int guest_instance_id, |
- scoped_ptr<base::DictionaryValue> extra_params) { |
- CHECK(guest_instance_id != browser_plugin::kInstanceIDNone); |
+void BrowserPlugin::Attach() { |
+ if (attach_pending_) |
+ return; |
// If this BrowserPlugin is already attached to a guest, then kill the guest. |
- if (HasGuestInstanceID()) { |
- if (guest_instance_id == guest_instance_id_) |
- return; |
+ if (attached()) { |
guest_crashed_ = false; |
EnableCompositing(false); |
if (compositing_helper_) { |
@@ -283,11 +294,6 @@ void BrowserPlugin::Attach(int guest_instance_id, |
render_view_routing_id_, guest_instance_id_)); |
} |
- // This API may be called directly without setting the src attribute. |
- // In that case, we need to make sure we don't allocate another instance ID. |
- guest_instance_id_ = guest_instance_id; |
- browser_plugin_manager()->AddBrowserPlugin(guest_instance_id, this); |
- |
BrowserPluginHostMsg_Attach_Params attach_params; |
attach_params.focused = ShouldGuestBeFocused(); |
attach_params.visible = visible_; |
@@ -297,10 +303,12 @@ void BrowserPlugin::Attach(int guest_instance_id, |
&attach_params.resize_guest_params, |
false); |
- browser_plugin_manager()->Send( |
- new BrowserPluginHostMsg_Attach(render_view_routing_id_, |
- guest_instance_id_, attach_params, |
- *extra_params)); |
+ browser_plugin_manager()->Send(new BrowserPluginHostMsg_Attach( |
+ render_view_routing_id_, |
+ instance_id_, |
+ attach_params)); |
+ |
+ attach_pending_ = true; |
} |
void BrowserPlugin::DidCommitCompositorFrame() { |
@@ -313,8 +321,13 @@ void BrowserPlugin::OnAdvanceFocus(int guest_instance_id, bool reverse) { |
render_view_->GetWebView()->advanceFocus(reverse); |
} |
-void BrowserPlugin::OnAttachACK(int guest_instance_id) { |
+void BrowserPlugin::OnAttachACK(int browser_plugin_instance_id, |
+ int guest_instance_id) { |
+ DCHECK(!attached()); |
+ guest_instance_id_ = guest_instance_id; |
+ browser_plugin_manager()->AddBrowserPlugin(guest_instance_id, this); |
attached_ = true; |
+ attach_pending_ = false; |
} |
void BrowserPlugin::OnBuffersSwapped( |
@@ -407,9 +420,9 @@ void BrowserPlugin::OnSetMouseLock(int guest_instance_id, |
void BrowserPlugin::OnShouldAcceptTouchEvents(int guest_instance_id, |
bool accept) { |
if (container()) { |
- container()->requestTouchEventType(accept ? |
- blink::WebPluginContainer::TouchEventRequestTypeRaw : |
- blink::WebPluginContainer::TouchEventRequestTypeNone); |
+ container()->requestTouchEventType( |
+ accept ? WebPluginContainer::TouchEventRequestTypeRaw |
+ : WebPluginContainer::TouchEventRequestTypeNone); |
} |
} |
@@ -480,10 +493,6 @@ NPObject* BrowserPlugin::GetContentWindow() const { |
return guest_frame->windowObject(); |
} |
-bool BrowserPlugin::HasGuestInstanceID() const { |
- return guest_instance_id_ != browser_plugin::kInstanceIDNone; |
-} |
- |
void BrowserPlugin::ShowSadGraphic() { |
// If the BrowserPlugin is scheduled to be deleted, then container_ will be |
// NULL so we shouldn't attempt to access it. |
@@ -510,7 +519,7 @@ void BrowserPlugin::UpdateDeviceScaleFactor(float device_scale_factor) { |
} |
void BrowserPlugin::UpdateGuestFocusState() { |
- if (!HasGuestInstanceID()) |
+ if (!attached()) |
return; |
bool should_be_focused = ShouldGuestBeFocused(); |
browser_plugin_manager()->Send(new BrowserPluginHostMsg_SetFocus( |
@@ -526,7 +535,7 @@ bool BrowserPlugin::ShouldGuestBeFocused() const { |
return plugin_focused_ && embedder_focused; |
} |
-blink::WebPluginContainer* BrowserPlugin::container() const { |
+WebPluginContainer* BrowserPlugin::container() const { |
return container_; |
} |
@@ -541,10 +550,18 @@ bool BrowserPlugin::initialize(WebPluginContainer* container) { |
bindings_.reset(new BrowserPluginBindings(this)); |
container_ = container; |
container_->setWantsWheelEvents(true); |
- // This is a way to notify observers of our attributes that we have the |
- // bindings ready. This also means that this plugin is available in render |
- // tree. |
- UpdateDOMAttribute("internalbindings", "true"); |
+ |
+ g_plugin_container_map.Get().insert(std::make_pair(container_, this)); |
+ |
+ // This is a way to notify observers of our attributes that this plugin is |
+ // available in render tree. |
+ const int next_instance_id = browser_plugin_manager()->GetNextInstanceID(); |
+ instance_id_ = next_instance_id; |
+ UpdateDOMAttribute("internalinstanceid", |
+ base::StringPrintf("%d", next_instance_id)); |
+ in_render_tree_ = true; |
+ |
+ browser_plugin_manager()->AddBrowserPluginInternal(instance_id_, this); |
return true; |
} |
@@ -577,6 +594,10 @@ void BrowserPlugin::destroy() { |
if (container_) |
container_->clearScriptObjects(); |
+ // The BrowserPlugin's WebPluginContainer is deleted immediately after this |
+ // call returns, so let's not keep a reference to it around. |
+ g_plugin_container_map.Get().erase(container_); |
+ |
if (compositing_helper_.get()) |
compositing_helper_->OnContainerDestroy(); |
container_ = NULL; |
@@ -644,12 +665,22 @@ void BrowserPlugin::paint(WebCanvas* canvas, const WebRect& rect) { |
canvas->drawRect(image_data_rect, paint); |
} |
+// static. |
+BrowserPlugin* BrowserPlugin::FromNode(blink::WebNode& node) { |
+ blink::WebPluginContainer* container = node.pluginContainer(); |
+ if (!container) |
+ return NULL; |
+ |
+ PluginContainerMap* browser_plugins = g_plugin_container_map.Pointer(); |
+ PluginContainerMap::iterator it = browser_plugins->find(container); |
+ return it == browser_plugins->end() ? NULL : it->second; |
+} |
+ |
// static |
bool BrowserPlugin::ShouldForwardToBrowserPlugin( |
const IPC::Message& message) { |
switch (message.type()) { |
case BrowserPluginMsg_AdvanceFocus::ID: |
- case BrowserPluginMsg_Attach_ACK::ID: |
case BrowserPluginMsg_BuffersSwapped::ID: |
case BrowserPluginMsg_CompositorFrameSwapped::ID: |
case BrowserPluginMsg_CopyFromCompositingSurface::ID: |
@@ -742,7 +773,7 @@ void BrowserPlugin::updateVisibility(bool visible) { |
return; |
visible_ = visible; |
- if (!HasGuestInstanceID()) |
+ if (!attached()) |
return; |
if (compositing_helper_.get()) |
@@ -760,7 +791,7 @@ bool BrowserPlugin::acceptsInputEvents() { |
bool BrowserPlugin::handleInputEvent(const blink::WebInputEvent& event, |
blink::WebCursorInfo& cursor_info) { |
- if (guest_crashed_ || !HasGuestInstanceID()) |
+ if (guest_crashed_ || !attached()) |
return false; |
if (event.type == blink::WebInputEvent::ContextMenu) |
@@ -833,7 +864,7 @@ bool BrowserPlugin::handleDragStatusUpdate(blink::WebDragStatus drag_status, |
blink::WebDragOperationsMask mask, |
const blink::WebPoint& position, |
const blink::WebPoint& screen) { |
- if (guest_crashed_ || !HasGuestInstanceID()) |
+ if (guest_crashed_ || !attached()) |
return false; |
browser_plugin_manager()->Send( |
new BrowserPluginHostMsg_DragStatusUpdate( |
@@ -899,7 +930,7 @@ bool BrowserPlugin::setComposition( |
const blink::WebVector<blink::WebCompositionUnderline>& underlines, |
int selectionStart, |
int selectionEnd) { |
- if (!HasGuestInstanceID()) |
+ if (!attached()) |
return false; |
std::vector<blink::WebCompositionUnderline> std_underlines; |
for (size_t i = 0; i < underlines.size(); ++i) { |
@@ -919,7 +950,7 @@ bool BrowserPlugin::setComposition( |
bool BrowserPlugin::confirmComposition( |
const blink::WebString& text, |
blink::WebWidget::ConfirmCompositionBehavior selectionBehavior) { |
- if (!HasGuestInstanceID()) |
+ if (!attached()) |
return false; |
bool keep_selection = (selectionBehavior == blink::WebWidget::KeepSelection); |
browser_plugin_manager()->Send(new BrowserPluginHostMsg_ImeConfirmComposition( |
@@ -932,7 +963,7 @@ bool BrowserPlugin::confirmComposition( |
} |
void BrowserPlugin::extendSelectionAndDelete(int before, int after) { |
- if (!HasGuestInstanceID()) |
+ if (!attached()) |
return; |
browser_plugin_manager()->Send( |
new BrowserPluginHostMsg_ExtendSelectionAndDelete( |