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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/renderer/browser_plugin/browser_plugin.h" 5 #include "content/renderer/browser_plugin/browser_plugin.h"
6 6
7 #include "base/json/json_string_value_serializer.h" 7 #include "base/json/json_string_value_serializer.h"
8 #include "base/message_loop.h" 8 #include "base/message_loop.h"
9 #include "base/string_number_conversions.h" 9 #include "base/string_number_conversions.h"
10 #include "base/string_util.h" 10 #include "base/string_util.h"
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 persist_storage_(false), 91 persist_storage_(false),
92 valid_partition_id_(true), 92 valid_partition_id_(true),
93 content_window_routing_id_(MSG_ROUTING_NONE), 93 content_window_routing_id_(MSG_ROUTING_NONE),
94 plugin_focused_(false), 94 plugin_focused_(false),
95 visible_(true), 95 visible_(true),
96 size_changed_in_flight_(false), 96 size_changed_in_flight_(false),
97 allocate_instance_id_sent_(false), 97 allocate_instance_id_sent_(false),
98 browser_plugin_manager_(render_view->browser_plugin_manager()), 98 browser_plugin_manager_(render_view->browser_plugin_manager()),
99 current_nav_entry_index_(0), 99 current_nav_entry_index_(0),
100 nav_entry_count_(0), 100 nav_entry_count_(0),
101 compositing_enabled_(false) { 101 compositing_enabled_(false),
102 ALLOW_THIS_IN_INITIALIZER_LIST(
103 weak_ptr_factory_(this)) {
102 bindings_.reset(new BrowserPluginBindings(this)); 104 bindings_.reset(new BrowserPluginBindings(this));
103 } 105 }
104 106
105 BrowserPlugin::~BrowserPlugin() { 107 BrowserPlugin::~BrowserPlugin() {
106 // If the BrowserPlugin has never navigated then the browser process and 108 // If the BrowserPlugin has never navigated then the browser process and
107 // BrowserPluginManager don't know about it and so there is nothing to do 109 // BrowserPluginManager don't know about it and so there is nothing to do
108 // here. 110 // here.
109 if (!navigate_src_sent_) 111 if (!navigate_src_sent_)
110 return; 112 return;
111 browser_plugin_manager()->RemoveBrowserPlugin(instance_id_); 113 browser_plugin_manager()->RemoveBrowserPlugin(instance_id_);
(...skipping 15 matching lines...) Expand all
127 IPC_MESSAGE_HANDLER(BrowserPluginMsg_LoadAbort, OnLoadAbort) 129 IPC_MESSAGE_HANDLER(BrowserPluginMsg_LoadAbort, OnLoadAbort)
128 IPC_MESSAGE_HANDLER(BrowserPluginMsg_LoadCommit, OnLoadCommit) 130 IPC_MESSAGE_HANDLER(BrowserPluginMsg_LoadCommit, OnLoadCommit)
129 IPC_MESSAGE_HANDLER(BrowserPluginMsg_LoadRedirect, OnLoadRedirect) 131 IPC_MESSAGE_HANDLER(BrowserPluginMsg_LoadRedirect, OnLoadRedirect)
130 IPC_MESSAGE_HANDLER(BrowserPluginMsg_LoadStart, OnLoadStart) 132 IPC_MESSAGE_HANDLER(BrowserPluginMsg_LoadStart, OnLoadStart)
131 IPC_MESSAGE_HANDLER(BrowserPluginMsg_LoadStop, OnLoadStop) 133 IPC_MESSAGE_HANDLER(BrowserPluginMsg_LoadStop, OnLoadStop)
132 IPC_MESSAGE_HANDLER(BrowserPluginMsg_SetCursor, OnSetCursor) 134 IPC_MESSAGE_HANDLER(BrowserPluginMsg_SetCursor, OnSetCursor)
133 IPC_MESSAGE_HANDLER(BrowserPluginMsg_ShouldAcceptTouchEvents, 135 IPC_MESSAGE_HANDLER(BrowserPluginMsg_ShouldAcceptTouchEvents,
134 OnShouldAcceptTouchEvents) 136 OnShouldAcceptTouchEvents)
135 IPC_MESSAGE_HANDLER(BrowserPluginMsg_UpdatedName, OnUpdatedName) 137 IPC_MESSAGE_HANDLER(BrowserPluginMsg_UpdatedName, OnUpdatedName)
136 IPC_MESSAGE_HANDLER(BrowserPluginMsg_UpdateRect, OnUpdateRect) 138 IPC_MESSAGE_HANDLER(BrowserPluginMsg_UpdateRect, OnUpdateRect)
139 IPC_MESSAGE_HANDLER(BrowserPluginMsg_RequestMediaAccess,
Charlie Reis 2013/02/11 22:20:56 nit: Alphabetize
lazyboy 2013/02/12 05:03:45 Done.
140 OnRequestMediaAccess)
137 IPC_MESSAGE_UNHANDLED(handled = false) 141 IPC_MESSAGE_UNHANDLED(handled = false)
138 IPC_END_MESSAGE_MAP() 142 IPC_END_MESSAGE_MAP()
139 return handled; 143 return handled;
140 } 144 }
141 145
142 void BrowserPlugin::UpdateDOMAttribute(const std::string& attribute_name, 146 void BrowserPlugin::UpdateDOMAttribute(const std::string& attribute_name,
143 const std::string& attribute_value) { 147 const std::string& attribute_value) {
144 if (!container()) 148 if (!container())
145 return; 149 return;
146 150
(...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after
528 container()->requestTouchEventType(accept ? 532 container()->requestTouchEventType(accept ?
529 WebKit::WebPluginContainer::TouchEventRequestTypeRaw : 533 WebKit::WebPluginContainer::TouchEventRequestTypeRaw :
530 WebKit::WebPluginContainer::TouchEventRequestTypeNone); 534 WebKit::WebPluginContainer::TouchEventRequestTypeNone);
531 } 535 }
532 } 536 }
533 537
534 void BrowserPlugin::OnUpdatedName(int instance_id, const std::string& name) { 538 void BrowserPlugin::OnUpdatedName(int instance_id, const std::string& name) {
535 UpdateDOMAttribute(browser_plugin::kAttributeName, name); 539 UpdateDOMAttribute(browser_plugin::kAttributeName, name);
536 } 540 }
537 541
542 void BrowserPlugin::OnRequestMediaAccess(int instance_id,
543 int request_id,
544 const GURL& security_origin) {
545 if (!HasEventListeners(browser_plugin::kEventRequestPermission)) {
546 // Automatically deny the request if there are no event listeners for
547 // permissionrequest.
548 RespondMediaAccess(request_id, false /* allow */);
549 return;
550 }
551 DCHECK(!media_access_pending_request_ids_.count(request_id));
552 media_access_pending_request_ids_.insert(request_id);
553
554 std::map<std::string, base::Value*> props;
555 props[browser_plugin::kPermission] =
556 base::Value::CreateStringValue(browser_plugin::kPermissionTypeMedia);
557 props[browser_plugin::kRequestId] =
558 base::Value::CreateIntegerValue(request_id);
559 props[browser_plugin::kURL] =
560 base::Value::CreateStringValue(security_origin.spec());
561 TriggerEvent(browser_plugin::kEventRequestPermission, &props);
562 }
563
564 bool BrowserPlugin::HasEventListeners(const std::string& event_name) {
565 if (!container())
566 return false;
567
568 // TODO(lazyboy): Fix before submitting: use ancestor list instead similar to
569 // 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.
570 WebKit::WebNode parent = container()->element().parentNode();
571 if (!parent.isNull() && !parent.shadowHost().isNull()) {
572 WebKit::WebElement shadow_host = parent.shadowHost();
573 return shadow_host.hasEventListeners(
574 WebKit::WebString::fromUTF8(event_name));
575 }
576 return false;
577 }
578
538 void BrowserPlugin::OnUpdateRect( 579 void BrowserPlugin::OnUpdateRect(
539 int instance_id, 580 int instance_id,
540 const BrowserPluginMsg_UpdateRect_Params& params) { 581 const BrowserPluginMsg_UpdateRect_Params& params) {
541 bool use_new_damage_buffer = !backing_store_; 582 bool use_new_damage_buffer = !backing_store_;
542 BrowserPluginHostMsg_AutoSize_Params auto_size_params; 583 BrowserPluginHostMsg_AutoSize_Params auto_size_params;
543 BrowserPluginHostMsg_ResizeGuest_Params resize_guest_params; 584 BrowserPluginHostMsg_ResizeGuest_Params resize_guest_params;
544 // If we have a pending damage buffer, and the guest has begun to use the 585 // If we have a pending damage buffer, and the guest has begun to use the
545 // damage buffer then we know the guest will no longer use the current 586 // damage buffer then we know the guest will no longer use the current
546 // damage buffer. At this point, we drop the current damage buffer, and 587 // damage buffer. At this point, we drop the current damage buffer, and
547 // mark the pending damage buffer as the current damage buffer. 588 // mark the pending damage buffer as the current damage buffer.
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
775 // wrapper/shim (e.g. <webview> tag) should receive these events, and expose a 816 // wrapper/shim (e.g. <webview> tag) should receive these events, and expose a
776 // more appropriate (and stable) event to the consumers as part of the API. 817 // more appropriate (and stable) event to the consumers as part of the API.
777 event.initCustomEvent( 818 event.initCustomEvent(
778 WebKit::WebString::fromUTF8(GetInternalEventName(event_name.c_str())), 819 WebKit::WebString::fromUTF8(GetInternalEventName(event_name.c_str())),
779 false, false, 820 false, false,
780 WebKit::WebSerializedScriptValue::serialize( 821 WebKit::WebSerializedScriptValue::serialize(
781 v8::String::New(json_string.c_str(), json_string.size()))); 822 v8::String::New(json_string.c_str(), json_string.size())));
782 container()->element().dispatchEvent(event); 823 container()->element().dispatchEvent(event);
783 } 824 }
784 825
826 void BrowserPlugin::OnMediaRequestGarbageCollected(int request_id) {
827 // Remove from alive objects.
828 std::map<int, MediaAccessAliveV8RequestItem*>::iterator iter =
829 media_access_alive_v8_request_objects_.find(request_id);
830 if (iter != media_access_alive_v8_request_objects_.end()) {
831 DCHECK(iter->second->first == request_id);
832 media_access_alive_v8_request_objects_.erase(iter);
833 }
834
835 // If a decision has not been made for this request yet, deny it.
836 RespondMediaAccessIfPending(request_id, false /* allow */);
837 }
838
839 void BrowserPlugin::PersistRequestObject(const NPVariant* request, int id) {
840 DCHECK(media_access_alive_v8_request_objects_.find(id) ==
841 media_access_alive_v8_request_objects_.end());
842 if (media_access_alive_v8_request_objects_.find(id) ==
843 media_access_alive_v8_request_objects_.end()) {
844 v8::Persistent<v8::Value> weak_request =
845 v8::Persistent<v8::Value>::New(WebKit::WebBindings::toV8Value(request));
846
847 MediaAccessAliveV8RequestItem* new_item =
848 new std::pair<int, base::WeakPtr<BrowserPlugin> >(
849 id, weak_ptr_factory_.GetWeakPtr());
850
851 std::pair<std::map<int, MediaAccessAliveV8RequestItem*>::iterator, bool>
852 result = media_access_alive_v8_request_objects_.insert(
853 std::make_pair(id, new_item));
854 DCHECK(result.second); // Inserted in the map.
855 if (result.second) {
856 MediaAccessAliveV8RequestItem* request_item = result.first->second;
857 weak_request.MakeWeak(request_item, WeakCallbackForPersistObject);
858 }
859 }
860 }
861
862 void BrowserPlugin::WeakCallbackForPersistObject(
863 v8::Persistent<v8::Value> object, void* param) {
864 v8::Persistent<v8::Object> persistent_object =
865 v8::Persistent<v8::Object>::Cast(object);
866
867 MediaAccessAliveV8RequestItem* item_ptr =
868 static_cast<MediaAccessAliveV8RequestItem*>(param);
869 int request_id = item_ptr->first;
870 base::WeakPtr<BrowserPlugin> plugin = item_ptr->second;
871 if (plugin)
872 plugin->OnMediaRequestGarbageCollected(request_id);
873 delete item_ptr;
874
875 persistent_object.Dispose();
876 }
877
785 void BrowserPlugin::Back() { 878 void BrowserPlugin::Back() {
786 if (!navigate_src_sent_) 879 if (!navigate_src_sent_)
787 return; 880 return;
788 browser_plugin_manager()->Send( 881 browser_plugin_manager()->Send(
789 new BrowserPluginHostMsg_Go(render_view_routing_id_, 882 new BrowserPluginHostMsg_Go(render_view_routing_id_,
790 instance_id_, -1)); 883 instance_id_, -1));
791 } 884 }
792 885
793 void BrowserPlugin::Forward() { 886 void BrowserPlugin::Forward() {
794 if (!navigate_src_sent_) 887 if (!navigate_src_sent_)
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
845 bool embedder_focused = false; 938 bool embedder_focused = false;
846 if (render_view_) 939 if (render_view_)
847 embedder_focused = render_view_->has_focus(); 940 embedder_focused = render_view_->has_focus();
848 return plugin_focused_ && embedder_focused; 941 return plugin_focused_ && embedder_focused;
849 } 942 }
850 943
851 WebKit::WebPluginContainer* BrowserPlugin::container() const { 944 WebKit::WebPluginContainer* BrowserPlugin::container() const {
852 return container_; 945 return container_;
853 } 946 }
854 947
948 void BrowserPlugin::RespondMediaAccess(int request_id, bool allow) {
949 browser_plugin_manager()->Send(
950 new BrowserPluginHostMsg_AllowPermissionAccess(
951 render_view_->GetRoutingID(), instance_id_,
952 browser_plugin::kPermissionTypeMedia, request_id, allow));
953 }
954
955 void BrowserPlugin::RespondMediaAccessIfPending(int request_id, bool allow) {
956 MediaAccessPendingRequestIds::iterator iter =
957 media_access_pending_request_ids_.find(request_id);
958 if (iter == media_access_pending_request_ids_.end())
959 return;
960 media_access_pending_request_ids_.erase(iter);
961 RespondMediaAccess(request_id, allow);
962 }
963
964 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
965 RespondMediaAccessIfPending(request_id, allow);
966 }
967
855 bool BrowserPlugin::initialize(WebPluginContainer* container) { 968 bool BrowserPlugin::initialize(WebPluginContainer* container) {
856 container_ = container; 969 container_ = container;
857 container_->setWantsWheelEvents(true); 970 container_->setWantsWheelEvents(true);
858 ParseAttributes(); 971 ParseAttributes();
859 return true; 972 return true;
860 } 973 }
861 974
862 void BrowserPlugin::EnableCompositing(bool enable) { 975 void BrowserPlugin::EnableCompositing(bool enable) {
863 if (compositing_enabled_ == enable) 976 if (compositing_enabled_ == enable)
864 return; 977 return;
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after
1167 void* notify_data) { 1280 void* notify_data) {
1168 } 1281 }
1169 1282
1170 void BrowserPlugin::didFailLoadingFrameRequest( 1283 void BrowserPlugin::didFailLoadingFrameRequest(
1171 const WebKit::WebURL& url, 1284 const WebKit::WebURL& url,
1172 void* notify_data, 1285 void* notify_data,
1173 const WebKit::WebURLError& error) { 1286 const WebKit::WebURLError& error) {
1174 } 1287 }
1175 1288
1176 } // namespace content 1289 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698