Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1185)

Unified Diff: content/renderer/browser_plugin/browser_plugin.cc

Issue 11093080: <webview>: First stab at implementing media permission request for guests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Plugin can be destroyed before we get Weak Callback, fixed. Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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 0dbc53fe80a50dc306b41a135f9f866d77bab3ff..6e8725c2d615ea96b9f0ddfcd23789b54fbaffb6 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));
}
@@ -134,6 +136,8 @@ bool BrowserPlugin::OnMessageReceived(const IPC::Message& message) {
OnShouldAcceptTouchEvents)
IPC_MESSAGE_HANDLER(BrowserPluginMsg_UpdatedName, OnUpdatedName)
IPC_MESSAGE_HANDLER(BrowserPluginMsg_UpdateRect, OnUpdateRect)
+ IPC_MESSAGE_HANDLER(BrowserPluginMsg_RequestMediaAccess,
Charlie Reis 2013/02/11 22:20:56 nit: Alphabetize
lazyboy 2013/02/12 05:03:45 Done.
+ OnRequestMediaAccess)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
@@ -535,6 +539,43 @@ void BrowserPlugin::OnUpdatedName(int instance_id, const std::string& name) {
UpdateDOMAttribute(browser_plugin::kAttributeName, name);
}
+void BrowserPlugin::OnRequestMediaAccess(int instance_id,
+ int request_id,
+ const GURL& security_origin) {
+ if (!HasEventListeners(browser_plugin::kEventRequestPermission)) {
+ // Automatically deny the request if there are no event listeners for
+ // permissionrequest.
+ RespondMediaAccess(request_id, false /* allow */);
+ return;
+ }
+ DCHECK(!media_access_pending_request_ids_.count(request_id));
+ media_access_pending_request_ids_.insert(request_id);
+
+ 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);
+ props[browser_plugin::kURL] =
+ base::Value::CreateStringValue(security_origin.spec());
+ 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
+ // window.open CL, so bubbling is supported.
Charlie Reis 2013/02/11 22:20:56 Is this still planned?
lazyboy 2013/02/12 05:03:45 Yes.
+ 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 +823,58 @@ void BrowserPlugin::TriggerEvent(const std::string& event_name,
container()->element().dispatchEvent(event);
}
+void BrowserPlugin::OnMediaRequestGarbageCollected(int request_id) {
+ // Remove from alive objects.
+ std::map<int, MediaAccessAliveV8RequestItem*>::iterator iter =
+ media_access_alive_v8_request_objects_.find(request_id);
+ if (iter != media_access_alive_v8_request_objects_.end()) {
+ DCHECK(iter->second->first == request_id);
+ media_access_alive_v8_request_objects_.erase(iter);
+ }
+
+ // If a decision has not been made for this request yet, deny it.
+ RespondMediaAccessIfPending(request_id, false /* allow */);
+}
+
+void BrowserPlugin::PersistRequestObject(const NPVariant* request, int id) {
+ DCHECK(media_access_alive_v8_request_objects_.find(id) ==
+ media_access_alive_v8_request_objects_.end());
+ if (media_access_alive_v8_request_objects_.find(id) ==
+ media_access_alive_v8_request_objects_.end()) {
+ v8::Persistent<v8::Value> weak_request =
+ v8::Persistent<v8::Value>::New(WebKit::WebBindings::toV8Value(request));
+
+ MediaAccessAliveV8RequestItem* new_item =
+ new std::pair<int, base::WeakPtr<BrowserPlugin> >(
+ id, weak_ptr_factory_.GetWeakPtr());
+
+ std::pair<std::map<int, MediaAccessAliveV8RequestItem*>::iterator, bool>
+ result = media_access_alive_v8_request_objects_.insert(
+ std::make_pair(id, new_item));
+ DCHECK(result.second); // Inserted in the map.
+ if (result.second) {
+ MediaAccessAliveV8RequestItem* request_item = result.first->second;
+ weak_request.MakeWeak(request_item, WeakCallbackForPersistObject);
+ }
+ }
+}
+
+void BrowserPlugin::WeakCallbackForPersistObject(
+ v8::Persistent<v8::Value> object, void* param) {
+ v8::Persistent<v8::Object> persistent_object =
+ v8::Persistent<v8::Object>::Cast(object);
+
+ MediaAccessAliveV8RequestItem* item_ptr =
+ static_cast<MediaAccessAliveV8RequestItem*>(param);
+ int request_id = item_ptr->first;
+ base::WeakPtr<BrowserPlugin> plugin = item_ptr->second;
+ if (plugin)
+ plugin->OnMediaRequestGarbageCollected(request_id);
+ delete item_ptr;
+
+ persistent_object.Dispose();
+}
+
void BrowserPlugin::Back() {
if (!navigate_src_sent_)
return;
@@ -852,6 +945,26 @@ WebKit::WebPluginContainer* BrowserPlugin::container() const {
return container_;
}
+void BrowserPlugin::RespondMediaAccess(int request_id, bool allow) {
+ browser_plugin_manager()->Send(
+ new BrowserPluginHostMsg_AllowPermissionAccess(
+ render_view_->GetRoutingID(), instance_id_,
+ browser_plugin::kPermissionTypeMedia, request_id, allow));
+}
+
+void BrowserPlugin::RespondMediaAccessIfPending(int request_id, bool allow) {
+ MediaAccessPendingRequestIds::iterator iter =
+ media_access_pending_request_ids_.find(request_id);
+ if (iter == media_access_pending_request_ids_.end())
+ return;
+ media_access_pending_request_ids_.erase(iter);
+ RespondMediaAccess(request_id, allow);
+}
+
+void BrowserPlugin::OnListenerCallMediaAccess(int request_id, bool allow) {
Charlie Reis 2013/02/11 22:20:56 Can we find a clearer name for this? There's no f
lazyboy 2013/02/12 05:03:45 Changed to OnEmbedderDecidedPermission since now i
+ RespondMediaAccessIfPending(request_id, allow);
+}
+
bool BrowserPlugin::initialize(WebPluginContainer* container) {
container_ = container;
container_->setWantsWheelEvents(true);

Powered by Google App Engine
This is Rietveld 408576698