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

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: Address comments from fsamuel@ 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 7d681717658760729164c36398fcab40be94aa9f..14c651b9c6c9f10fd95fa46eb4e96aa794c02db0 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)
@@ -387,6 +390,22 @@ void BrowserPlugin::SetInstanceID(int instance_id) {
navigate_src_sent_ = true;
}
+void BrowserPlugin::PopulateAncestorList() {
+ if (!container())
+ return;
+
+ WebKit::WebNode ancestor = container()->element();
+ // Escape the <webview> shim if this BrowserPlugin has one.
+ WebKit::WebElement shim = ancestor.shadowHost();
+ if (!shim.isNull())
+ ancestor = shim;
+ ancestors_.clear();
+ while (!ancestor.isNull()) {
+ ancestors_.push_back(ancestor);
+ ancestor = ancestor.parentNode();
+ }
+}
+
void BrowserPlugin::OnAdvanceFocus(int instance_id, bool reverse) {
DCHECK(render_view_);
render_view_->GetWebView()->advanceFocus(reverse);
@@ -519,6 +538,15 @@ void BrowserPlugin::OnLoadStop(int instance_id) {
TriggerEvent(browser_plugin::kEventLoadStop, NULL);
}
+void BrowserPlugin::OnRequestPermission(
Fady Samuel 2013/02/12 19:13:09 It looks like this should be placed after OnLoadSt
lazyboy 2013/02/12 22:14:33 It is.
+ int instance_id,
+ const std::string& permission_type,
+ int request_id,
+ const base::DictionaryValue& request_info) {
+ if (permission_type == "media")
Fady Samuel 2013/02/12 19:13:09 "media" should probably be a constant.
lazyboy 2013/02/12 22:14:33 Done.
+ RequestMediaPermission(request_id, request_info);
+}
+
void BrowserPlugin::OnSetCursor(int instance_id, const WebCursor& cursor) {
cursor_ = cursor;
}
@@ -535,6 +563,46 @@ void BrowserPlugin::OnUpdatedName(int instance_id, const std::string& name) {
UpdateDOMAttribute(browser_plugin::kAttributeName, name);
}
+void BrowserPlugin::RequestMediaPermission(
+ 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_requests_.count(request_id));
+ pending_permission_requests_.insert(
+ std::make_pair(request_id,
+ 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;
+
+ for (size_t i = 0; i < ancestors_.size(); ++i) {
+ if (ancestors_[i].hasEventListeners(
+ WebKit::WebString::fromUTF8(event_name))) {
+ return true;
+ }
+ }
+ return false;
+}
+
void BrowserPlugin::OnUpdateRect(
int instance_id,
const BrowserPluginMsg_UpdateRect_Params& params) {
@@ -782,6 +850,66 @@ 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);
+ 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) !=
+ alive_v8_permission_request_objects.end()) {
+ return;
+ }
+ if (pending_permission_requests_.find(id) ==
+ pending_permission_requests_.end()) {
+ return;
+ }
+
+ 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);
+ }
+}
+
+// static
+void BrowserPlugin::WeakCallbackForPersistObject(
+ 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,9 +980,43 @@ WebKit::WebPluginContainer* BrowserPlugin::container() const {
return container_;
}
+void BrowserPlugin::RespondPermission(
+ BrowserPlugin::PermissionRequestType type, int request_id, bool allow) {
+ switch (type) {
+ case BrowserPlugin::MEDIA:
+ browser_plugin_manager()->Send(
+ new BrowserPluginHostMsg_AllowPermission(
+ render_view_->GetRoutingID(), instance_id_,
+ browser_plugin::kPermissionTypeMedia, request_id, allow));
+ break;
+ case BrowserPlugin::INVALID:
+ default:
+ LOG(INFO) << "Not a valid permission type";
+ break;
+ }
+}
+
+void BrowserPlugin::RespondPermissionIfPending(int request_id, bool allow) {
+ PendingPermissionRequests::iterator iter =
+ pending_permission_requests_.find(request_id);
+ if (iter == pending_permission_requests_.end())
+ return;
+
+ BrowserPlugin::PermissionRequestType type = iter->second.second;
+ pending_permission_requests_.erase(iter);
+ RespondPermission(type, request_id, allow);
+}
+
+void BrowserPlugin::OnEmbedderDecidedPermission(int request_id, bool allow) {
+ RespondPermissionIfPending(request_id, allow);
+}
+
bool BrowserPlugin::initialize(WebPluginContainer* container) {
container_ = container;
container_->setWantsWheelEvents(true);
+ // This ancestor set is valid for the lifetime of BrowserPlugin as this object
+ // does not survive reparenting in the DOM tree.
+ PopulateAncestorList();
ParseAttributes();
return true;
}

Powered by Google App Engine
This is Rietveld 408576698