Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #if defined (OS_WIN) | 9 #if defined (OS_WIN) |
| 10 #include "base/sys_info.h" | 10 #include "base/sys_info.h" |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 35 using WebKit::WebPluginParams; | 35 using WebKit::WebPluginParams; |
| 36 using WebKit::WebPoint; | 36 using WebKit::WebPoint; |
| 37 using WebKit::WebString; | 37 using WebKit::WebString; |
| 38 using WebKit::WebRect; | 38 using WebKit::WebRect; |
| 39 using WebKit::WebURL; | 39 using WebKit::WebURL; |
| 40 using WebKit::WebVector; | 40 using WebKit::WebVector; |
| 41 | 41 |
| 42 namespace content { | 42 namespace content { |
| 43 | 43 |
| 44 namespace { | 44 namespace { |
| 45 const char kAllowMediaAccessCallbackName[] = "allow"; | |
| 45 const char kCrashEventName[] = "crash"; | 46 const char kCrashEventName[] = "crash"; |
| 47 const char kDenyMediaAccessCallbackName[] = "deny"; | |
| 48 const char kInstanceIdKey[] = "instance_id"; | |
| 46 const char kIsTopLevel[] = "isTopLevel"; | 49 const char kIsTopLevel[] = "isTopLevel"; |
| 47 const char kLoadAbortEventName[] = "loadAbort"; | 50 const char kLoadAbortEventName[] = "loadAbort"; |
| 48 const char kLoadRedirectEventName[] = "loadRedirect"; | 51 const char kLoadRedirectEventName[] = "loadRedirect"; |
| 49 const char kLoadStartEventName[] = "loadStart"; | 52 const char kLoadStartEventName[] = "loadStart"; |
| 50 const char kNavigationEventName[] = "navigation"; | 53 const char kNavigationEventName[] = "navigation"; |
| 51 const char kNewURL[] = "newUrl"; | 54 const char kNewURL[] = "newUrl"; |
| 52 const char kOldURL[] = "oldUrl"; | 55 const char kOldURL[] = "oldUrl"; |
| 53 const char kPartitionAttribute[] = "partition"; | 56 const char kPartitionAttribute[] = "partition"; |
| 54 const char kPersistPrefix[] = "persist:"; | 57 const char kPersistPrefix[] = "persist:"; |
| 58 const char kPermissionRequestEventName[] = "permissionRequest"; | |
|
Charlie Reis
2012/10/17 06:18:45
nit: permission < persist
lazyboy
2012/10/17 09:09:53
Done.
| |
| 59 const char kPermissionTypeMedia[] = "media"; | |
| 60 const char kRequestIdKey[] = "request_id"; | |
| 55 const char kSrcAttribute[] = "src"; | 61 const char kSrcAttribute[] = "src"; |
| 56 const char kType[] = "type"; | 62 const char kType[] = "type"; |
| 57 const char kURL[] = "url"; | 63 const char kURL[] = "url"; |
| 64 | |
| 65 void SetMediaAccessPermission(bool allow, const v8::Arguments& args) { | |
| 66 int instance_id = -1; | |
| 67 int request_id = -1; | |
| 68 v8::Local<v8::Value> instance_id_value = | |
| 69 args.Callee()->GetHiddenValue(v8::String::New(kInstanceIdKey)); | |
| 70 v8::Local<v8::Value> request_id_value = | |
| 71 args.Callee()->GetHiddenValue(v8::String::New(kRequestIdKey)); | |
| 72 if (instance_id_value->IsInt32()) | |
| 73 instance_id = instance_id_value->ToInt32()->Value(); | |
| 74 if (request_id_value->IsInt32()) | |
| 75 request_id = request_id_value->ToInt32()->Value(); | |
| 76 | |
| 77 DCHECK_NE(instance_id, -1); | |
| 78 DCHECK_NE(request_id, -1); | |
| 79 BrowserPlugin* plugin = | |
| 80 BrowserPluginManager::Get()->GetBrowserPlugin(instance_id); | |
| 81 if (plugin) | |
| 82 plugin->AllowMediaAccess(request_id, allow); | |
| 83 } | |
| 84 | |
| 85 v8::Handle<v8::Value> OnJsAllowMediaAccess(const v8::Arguments& args) { | |
| 86 SetMediaAccessPermission(true /* allow */, args); | |
| 87 return v8::Undefined(); | |
| 88 } | |
| 89 | |
| 90 v8::Handle<v8::Value> OnJsDenyMediaAccess(const v8::Arguments& args) { | |
| 91 SetMediaAccessPermission(false /* allow */, args); | |
| 92 return v8::Undefined(); | |
| 93 } | |
| 94 | |
| 58 } | 95 } |
| 59 | 96 |
| 60 BrowserPlugin::BrowserPlugin( | 97 BrowserPlugin::BrowserPlugin( |
| 61 int instance_id, | 98 int instance_id, |
| 62 RenderViewImpl* render_view, | 99 RenderViewImpl* render_view, |
| 63 WebKit::WebFrame* frame, | 100 WebKit::WebFrame* frame, |
| 64 const WebPluginParams& params) | 101 const WebPluginParams& params) |
| 65 : instance_id_(instance_id), | 102 : instance_id_(instance_id), |
| 66 render_view_(render_view), | 103 render_view_(render_view), |
| 67 container_(NULL), | 104 container_(NULL), |
| (...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 470 // We do not have a RenderView when we are testing. | 507 // We do not have a RenderView when we are testing. |
| 471 if (render_view_) | 508 if (render_view_) |
| 472 render_view_->GetWebView()->advanceFocus(reverse); | 509 render_view_->GetWebView()->advanceFocus(reverse); |
| 473 } | 510 } |
| 474 | 511 |
| 475 void BrowserPlugin::SetAcceptTouchEvents(bool accept) { | 512 void BrowserPlugin::SetAcceptTouchEvents(bool accept) { |
| 476 if (container()) | 513 if (container()) |
| 477 container()->setIsAcceptingTouchEvents(accept); | 514 container()->setIsAcceptingTouchEvents(accept); |
| 478 } | 515 } |
| 479 | 516 |
| 517 void BrowserPlugin::RequestMediaAccess(int request_id) { | |
| 518 if (!HasListeners(kPermissionRequestEventName)) { | |
| 519 // TODO(lazyboy): Automatically deny request when there is no listener. | |
|
Charlie Reis
2012/10/17 06:18:45
Can we go ahead and do this?
lazyboy
2012/10/17 09:09:53
Ah, it is easy to do so now.
Done.
| |
| 520 return; | |
| 521 } | |
| 522 EventListeners& listeners = event_listener_map_[kPermissionRequestEventName]; | |
| 523 EventListeners::iterator it = listeners.begin(); | |
| 524 | |
| 525 WebKit::WebElement plugin = container()->element(); | |
| 526 v8::HandleScope handle_scope; | |
| 527 v8::Context::Scope context_scope( | |
| 528 plugin.document().frame()->mainWorldScriptContext()); | |
| 529 | |
| 530 // Set callbacks. | |
| 531 // We also save the |instance_id_| and |request_id| in these callbacks. This | |
| 532 // is so that we know where to route the callback to when js calls the | |
| 533 // callback. | |
| 534 // | |
| 535 // Javascript: | |
| 536 // browser.addEventListener('permissionRequest', function(e) { | |
| 537 // e.allow(); // Calls OnJsAllowMediaAccess(). | |
| 538 // e.deny(); // Calls OnJsDenyMediaAccess(). | |
| 539 // }); | |
| 540 | |
| 541 // Allow callback. | |
| 542 v8::Local<v8::Function> allow_function = | |
| 543 v8::FunctionTemplate::New(OnJsAllowMediaAccess)->GetFunction(); | |
| 544 allow_function->SetHiddenValue(v8::String::New(kInstanceIdKey), | |
| 545 v8::Int32::New(instance_id_)); | |
| 546 allow_function->SetHiddenValue(v8::String::New(kRequestIdKey), | |
| 547 v8::Int32::New(request_id)); | |
| 548 // Deny callback. | |
| 549 v8::Local<v8::Function> deny_function = | |
| 550 v8::FunctionTemplate::New(OnJsDenyMediaAccess)->GetFunction(); | |
| 551 deny_function->SetHiddenValue(v8::String::New(kInstanceIdKey), | |
| 552 v8::Int32::New(instance_id_)); | |
| 553 deny_function->SetHiddenValue(v8::String::New(kRequestIdKey), | |
| 554 v8::Int32::New(request_id)); | |
| 555 | |
| 556 // Event object. | |
| 557 v8::Local<v8::Value> event = | |
| 558 v8::Local<v8::Object>::New(v8::Object::New()); | |
| 559 // TODO(lazyboy): Also set permission details. Permission details would | |
| 560 // include: whether the request was for audio(microphone)/video(webcam)/both. | |
| 561 v8::Local<v8::Object>::Cast(event)->Set( | |
| 562 v8::String::New(kType), v8::String::New(kPermissionTypeMedia)); | |
| 563 v8::Local<v8::Object>::Cast(event)->Set( | |
| 564 v8::String::New(kAllowMediaAccessCallbackName), allow_function); | |
| 565 v8::Local<v8::Object>::Cast(event)->Set( | |
| 566 v8::String::New(kDenyMediaAccessCallbackName), deny_function); | |
| 567 | |
| 568 for (; it != listeners.end(); ++it) { | |
| 569 // Fire the event listener. | |
| 570 container()->element().document().frame()-> | |
| 571 callFunctionEvenIfScriptDisabled(*it, | |
| 572 v8::Object::New(), | |
| 573 1, | |
| 574 &event); | |
| 575 } | |
| 576 } | |
| 577 | |
| 578 void BrowserPlugin::AllowMediaAccess(int request_id, bool allow) { | |
| 579 BrowserPluginManager::Get()->Send( | |
| 580 new BrowserPluginHostMsg_AllowMediaAccess(render_view_->GetRoutingID(), | |
| 581 instance_id_, | |
| 582 request_id, | |
| 583 allow)); | |
| 584 } | |
| 585 | |
| 480 bool BrowserPlugin::HasListeners(const std::string& event_name) { | 586 bool BrowserPlugin::HasListeners(const std::string& event_name) { |
| 481 return event_listener_map_.find(event_name) != event_listener_map_.end(); | 587 return event_listener_map_.find(event_name) != event_listener_map_.end(); |
| 482 } | 588 } |
| 483 | 589 |
| 484 bool BrowserPlugin::AddEventListener(const std::string& event_name, | 590 bool BrowserPlugin::AddEventListener(const std::string& event_name, |
| 485 v8::Local<v8::Function> function) { | 591 v8::Local<v8::Function> function) { |
| 486 EventListeners& listeners = event_listener_map_[event_name]; | 592 EventListeners& listeners = event_listener_map_[event_name]; |
| 487 for (unsigned int i = 0; i < listeners.size(); ++i) { | 593 for (unsigned int i = 0; i < listeners.size(); ++i) { |
| 488 if (listeners[i] == function) | 594 if (listeners[i] == function) |
| 489 return false; | 595 return false; |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 757 void* notify_data) { | 863 void* notify_data) { |
| 758 } | 864 } |
| 759 | 865 |
| 760 void BrowserPlugin::didFailLoadingFrameRequest( | 866 void BrowserPlugin::didFailLoadingFrameRequest( |
| 761 const WebKit::WebURL& url, | 867 const WebKit::WebURL& url, |
| 762 void* notify_data, | 868 void* notify_data, |
| 763 const WebKit::WebURLError& error) { | 869 const WebKit::WebURLError& error) { |
| 764 } | 870 } |
| 765 | 871 |
| 766 } // namespace content | 872 } // namespace content |
| OLD | NEW |