Chromium Code Reviews| 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 7d681717658760729164c36398fcab40be94aa9f..ed75d6c7301bc8967c78e8cc9988f20a73b2a696 100644 |
| --- a/content/renderer/browser_plugin/browser_plugin.cc |
| +++ b/content/renderer/browser_plugin/browser_plugin.cc |
| @@ -98,7 +98,9 @@ BrowserPlugin::BrowserPlugin( |
| browser_plugin_manager_(render_view->browser_plugin_manager()), |
| current_nav_entry_index_(0), |
| nav_entry_count_(0), |
| - compositing_enabled_(false) { |
| + compositing_enabled_(false), |
| + ALLOW_THIS_IN_INITIALIZER_LIST( |
| + weak_ptr_factory_(this)) { |
| bindings_.reset(new BrowserPluginBindings(this)); |
| } |
| @@ -129,6 +131,7 @@ bool BrowserPlugin::OnMessageReceived(const IPC::Message& message) { |
| IPC_MESSAGE_HANDLER(BrowserPluginMsg_LoadRedirect, OnLoadRedirect) |
| IPC_MESSAGE_HANDLER(BrowserPluginMsg_LoadStart, OnLoadStart) |
| IPC_MESSAGE_HANDLER(BrowserPluginMsg_LoadStop, OnLoadStop) |
| + IPC_MESSAGE_HANDLER(BrowserPluginMsg_RequestPermission, OnRequestPermission) |
| IPC_MESSAGE_HANDLER(BrowserPluginMsg_SetCursor, OnSetCursor) |
| IPC_MESSAGE_HANDLER(BrowserPluginMsg_ShouldAcceptTouchEvents, |
| OnShouldAcceptTouchEvents) |
| @@ -535,6 +538,57 @@ void BrowserPlugin::OnUpdatedName(int instance_id, const std::string& name) { |
| UpdateDOMAttribute(browser_plugin::kAttributeName, name); |
| } |
| +void BrowserPlugin::OnRequestPermission( |
|
Fady Samuel
2013/02/12 16:13:20
Alphabetize with other message handlers.
lazyboy
2013/02/12 18:57:51
Done.
|
| + int instance_id, |
| + const std::string& permission_type, |
| + int request_id, |
| + const base::DictionaryValue& request_info) { |
| + if (permission_type == "media") |
| + OnRequestMediaPermission(request_id, request_info); |
|
Fady Samuel
2013/02/12 16:13:20
How about just RequestMedia permission?
lazyboy
2013/02/12 18:57:51
Done.
|
| +} |
| + |
| +void BrowserPlugin::OnRequestMediaPermission( |
| + int request_id, const base::DictionaryValue& request_info) { |
| + if (!HasEventListeners(browser_plugin::kEventRequestPermission)) { |
| + // Automatically deny the request if there are no event listeners for |
| + // permissionrequest. |
| + RespondPermission(BrowserPlugin::MEDIA, request_id, false /* allow */); |
| + return; |
| + } |
| + DCHECK(!pending_permission_request_ids_.count(request_id)); |
| + pending_permission_request_ids_.insert(request_id); |
|
Fady Samuel
2013/02/12 16:13:20
I'm confused. Why do we have a pending_permission_
lazyboy
2013/02/12 18:57:51
Merged them, thanks.
|
| + pending_request_id_to_type_.insert( |
| + std::make_pair(request_id, BrowserPlugin::MEDIA)); |
| + |
| + std::map<std::string, base::Value*> props; |
| + props[browser_plugin::kPermission] = |
| + base::Value::CreateStringValue(browser_plugin::kPermissionTypeMedia); |
| + props[browser_plugin::kRequestId] = |
| + base::Value::CreateIntegerValue(request_id); |
| + |
| + // Fill in the info provided by the browser. |
| + for (DictionaryValue::Iterator iter(request_info); !iter.IsAtEnd(); |
| + iter.Advance()) { |
| + props[iter.key()] = iter.value().DeepCopy(); |
| + } |
| + TriggerEvent(browser_plugin::kEventRequestPermission, &props); |
| +} |
| + |
| +bool BrowserPlugin::HasEventListeners(const std::string& event_name) { |
| + if (!container()) |
| + return false; |
| + |
| + // TODO(lazyboy): Fix before submitting: use ancestor list instead similar to |
|
Fady Samuel
2013/02/12 16:13:20
You can probably copy and paste my code in the win
lazyboy
2013/02/12 18:57:51
Done.
|
| + // window.open CL, so bubbling is supported. |
| + WebKit::WebNode parent = container()->element().parentNode(); |
| + if (!parent.isNull() && !parent.shadowHost().isNull()) { |
| + WebKit::WebElement shadow_host = parent.shadowHost(); |
| + return shadow_host.hasEventListeners( |
| + WebKit::WebString::fromUTF8(event_name)); |
| + } |
| + return false; |
| +} |
| + |
| void BrowserPlugin::OnUpdateRect( |
| int instance_id, |
| const BrowserPluginMsg_UpdateRect_Params& params) { |
| @@ -782,6 +836,59 @@ void BrowserPlugin::TriggerEvent(const std::string& event_name, |
| container()->element().dispatchEvent(event); |
| } |
| +void BrowserPlugin::OnRequestObjectGarbageCollected(int request_id) { |
| + // Remove from alive objects. |
| + std::map<int, AliveV8PermissionRequestItem*>::iterator iter = |
| + alive_v8_permission_request_objects.find(request_id); |
|
Fady Samuel
2013/02/12 16:13:20
I feel like fundamentally, we want a map that's:
lazyboy
2013/02/12 18:57:51
The V8_*_requests container is not parallel to the
|
| + if (iter != alive_v8_permission_request_objects.end()) { |
| + DCHECK(iter->second->first == request_id); |
| + alive_v8_permission_request_objects.erase(iter); |
| + } |
| + |
| + // If a decision has not been made for this request yet, deny it. |
| + RespondPermissionIfPending(request_id, false /*allow*/); |
| +} |
| + |
| +void BrowserPlugin::PersistRequestObject( |
| + const NPVariant* request, const std::string& type, int id) { |
| + DCHECK(alive_v8_permission_request_objects.find(id) == |
| + alive_v8_permission_request_objects.end()); |
| + if (alive_v8_permission_request_objects.find(id) == |
|
Fady Samuel
2013/02/12 16:13:20
Early exit preferred.
if (alive_v8_permission_req
lazyboy
2013/02/12 18:57:51
Done.
|
| + alive_v8_permission_request_objects.end()) { |
| + v8::Persistent<v8::Value> weak_request = |
| + v8::Persistent<v8::Value>::New(WebKit::WebBindings::toV8Value(request)); |
| + |
| + AliveV8PermissionRequestItem* new_item = |
| + new std::pair<int, base::WeakPtr<BrowserPlugin> >( |
| + id, weak_ptr_factory_.GetWeakPtr()); |
| + |
| + std::pair<std::map<int, AliveV8PermissionRequestItem*>::iterator, bool> |
| + result = alive_v8_permission_request_objects.insert( |
| + std::make_pair(id, new_item)); |
| + DCHECK(result.second); // Inserted in the map. |
| + if (result.second) { |
| + AliveV8PermissionRequestItem* request_item = result.first->second; |
| + weak_request.MakeWeak(request_item, WeakCallbackForPersistObject); |
| + } |
| + } |
| +} |
| + |
| +void BrowserPlugin::WeakCallbackForPersistObject( |
|
Fady Samuel
2013/02/12 16:13:20
// static
lazyboy
2013/02/12 18:57:51
Done.
|
| + v8::Persistent<v8::Value> object, void* param) { |
| + v8::Persistent<v8::Object> persistent_object = |
| + v8::Persistent<v8::Object>::Cast(object); |
| + |
| + AliveV8PermissionRequestItem* item_ptr = |
| + static_cast<AliveV8PermissionRequestItem*>(param); |
| + int request_id = item_ptr->first; |
| + base::WeakPtr<BrowserPlugin> plugin = item_ptr->second; |
| + if (plugin) |
| + plugin->OnRequestObjectGarbageCollected(request_id); |
| + delete item_ptr; |
| + |
| + persistent_object.Dispose(); |
| +} |
| + |
| void BrowserPlugin::Back() { |
| if (!navigate_src_sent_) |
| return; |
| @@ -852,6 +959,42 @@ WebKit::WebPluginContainer* BrowserPlugin::container() const { |
| return container_; |
| } |
| +void BrowserPlugin::RespondPermission( |
| + BrowserPlugin::PermissionRequestType type, int request_id, bool allow) { |
| + if (type == BrowserPlugin::INVALID) { |
|
Fady Samuel
2013/02/12 16:13:20
This can probably be a switch statement. Only two
lazyboy
2013/02/12 18:57:51
Done.
|
| + LOG(INFO) << "Not a valid permission type"; |
| + return; |
| + } |
| + |
| + if (type == BrowserPlugin::MEDIA) { |
| + browser_plugin_manager()->Send( |
| + new BrowserPluginHostMsg_AllowPermission( |
| + render_view_->GetRoutingID(), instance_id_, |
| + browser_plugin::kPermissionTypeMedia, request_id, allow)); |
| + } |
| +} |
| + |
| +void BrowserPlugin::RespondPermissionIfPending(int request_id, bool allow) { |
| + PendingPermissionRequestIds::iterator iter = |
| + pending_permission_request_ids_.find(request_id); |
| + std::map<int, BrowserPlugin::PermissionRequestType>::iterator type_iter = |
| + pending_request_id_to_type_.find(request_id); |
| + |
| + if (iter == pending_permission_request_ids_.end()) { |
| + DCHECK(type_iter == pending_request_id_to_type_.end()); |
| + return; |
| + } |
| + |
| + BrowserPlugin::PermissionRequestType type = type_iter->second; |
| + pending_permission_request_ids_.erase(iter); |
| + pending_request_id_to_type_.erase(request_id); |
| + RespondPermission(type, request_id, allow); |
| +} |
| + |
| +void BrowserPlugin::OnEmbedderDecidedPermission(int request_id, bool allow) { |
|
Fady Samuel
2013/02/12 16:13:20
I'm not too keen on the On* method names for thing
lazyboy
2013/02/12 18:57:51
I see. But I think this name makes it clearer to u
|
| + RespondPermissionIfPending(request_id, allow); |
| +} |
| + |
| bool BrowserPlugin::initialize(WebPluginContainer* container) { |
| container_ = container; |
| container_->setWantsWheelEvents(true); |