| 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 "chrome/browser/media/media_stream_devices_controller.h" | 5 #include "chrome/browser/media/media_stream_devices_controller.h" |
| 6 | 6 |
| 7 #include "base/metrics/histogram.h" | 7 #include "base/metrics/histogram.h" |
| 8 #include "base/prefs/scoped_user_pref_update.h" | 8 #include "base/prefs/scoped_user_pref_update.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 } | 77 } |
| 78 | 78 |
| 79 enum DevicePermissionActions { | 79 enum DevicePermissionActions { |
| 80 kAllowHttps = 0, | 80 kAllowHttps = 0, |
| 81 kAllowHttp, | 81 kAllowHttp, |
| 82 kDeny, | 82 kDeny, |
| 83 kCancel, | 83 kCancel, |
| 84 kPermissionActionsMax // Must always be last! | 84 kPermissionActionsMax // Must always be last! |
| 85 }; | 85 }; |
| 86 | 86 |
| 87 // This is a wrapper around the call to |
| 88 // TabSpecificContentSettings::OnMediaStreamPermissionSet, precomputing the |
| 89 // information from |request_permissions| to a form which is understood by |
| 90 // TabSpecificContentSettings. |
| 91 void OnMediaStreamPermissionSet( |
| 92 TabSpecificContentSettings* content_settings, |
| 93 content::WebContents* web_contents, |
| 94 const GURL& request_origin, |
| 95 const MediaStreamDevicesController::MediaStreamTypeSettingsMap& |
| 96 request_permissions) { |
| 97 TabSpecificContentSettings::MicrophoneCameraState microphone_camera_state = |
| 98 TabSpecificContentSettings::MICROPHONE_CAMERA_NOT_ACCESSED; |
| 99 std::string selected_audio_device; |
| 100 std::string selected_video_device; |
| 101 std::string requested_audio_device; |
| 102 std::string requested_video_device; |
| 103 |
| 104 PrefService* prefs = Profile::FromBrowserContext( |
| 105 web_contents->GetBrowserContext())->GetPrefs(); |
| 106 auto it = request_permissions.find(content::MEDIA_DEVICE_AUDIO_CAPTURE); |
| 107 if (it != request_permissions.end()) { |
| 108 requested_audio_device = it->second.requested_device_id; |
| 109 selected_audio_device = requested_audio_device.empty() ? |
| 110 prefs->GetString(prefs::kDefaultAudioCaptureDevice) : |
| 111 requested_audio_device; |
| 112 DCHECK_NE(MediaStreamDevicesController::MEDIA_NONE, it->second.permission); |
| 113 bool mic_allowed = |
| 114 it->second.permission == MediaStreamDevicesController::MEDIA_ALLOWED; |
| 115 microphone_camera_state |= |
| 116 TabSpecificContentSettings::MICROPHONE_ACCESSED | |
| 117 (mic_allowed ? 0 : TabSpecificContentSettings::MICROPHONE_BLOCKED); |
| 118 } |
| 119 |
| 120 it = request_permissions.find(content::MEDIA_DEVICE_VIDEO_CAPTURE); |
| 121 if (it != request_permissions.end()) { |
| 122 requested_video_device = it->second.requested_device_id; |
| 123 selected_video_device = requested_video_device.empty() ? |
| 124 prefs->GetString(prefs::kDefaultVideoCaptureDevice) : |
| 125 requested_video_device; |
| 126 DCHECK_NE(MediaStreamDevicesController::MEDIA_NONE, it->second.permission); |
| 127 bool cam_allowed = |
| 128 it->second.permission == MediaStreamDevicesController::MEDIA_ALLOWED; |
| 129 microphone_camera_state |= |
| 130 TabSpecificContentSettings::CAMERA_ACCESSED | |
| 131 (cam_allowed ? 0 : TabSpecificContentSettings::CAMERA_BLOCKED); |
| 132 } |
| 133 |
| 134 content_settings->OnMediaStreamPermissionSet(request_origin, |
| 135 microphone_camera_state, |
| 136 selected_audio_device, |
| 137 selected_video_device, |
| 138 requested_audio_device, |
| 139 requested_video_device); |
| 140 } |
| 141 |
| 87 } // namespace | 142 } // namespace |
| 88 | 143 |
| 89 MediaStreamDevicesController::MediaStreamTypeSettings::MediaStreamTypeSettings( | 144 MediaStreamDevicesController::MediaStreamTypeSettings::MediaStreamTypeSettings( |
| 90 Permission permission, const std::string& requested_device_id): | 145 Permission permission, const std::string& requested_device_id): |
| 91 permission(permission), requested_device_id(requested_device_id) {} | 146 permission(permission), requested_device_id(requested_device_id) {} |
| 92 | 147 |
| 93 MediaStreamDevicesController::MediaStreamTypeSettings:: | 148 MediaStreamDevicesController::MediaStreamTypeSettings:: |
| 94 MediaStreamTypeSettings(): permission(MEDIA_NONE) {} | 149 MediaStreamTypeSettings(): permission(MEDIA_NONE) {} |
| 95 | 150 |
| 96 MediaStreamDevicesController::MediaStreamTypeSettings:: | 151 MediaStreamDevicesController::MediaStreamTypeSettings:: |
| (...skipping 475 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 572 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA, | 627 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA, |
| 573 std::string(), | 628 std::string(), |
| 574 content_setting); | 629 content_setting); |
| 575 } | 630 } |
| 576 } | 631 } |
| 577 | 632 |
| 578 void MediaStreamDevicesController::NotifyUIRequestAccepted() const { | 633 void MediaStreamDevicesController::NotifyUIRequestAccepted() const { |
| 579 if (!content_settings_) | 634 if (!content_settings_) |
| 580 return; | 635 return; |
| 581 | 636 |
| 582 content_settings_->OnMediaStreamPermissionSet(request_.security_origin, | 637 OnMediaStreamPermissionSet(content_settings_, |
| 583 request_permissions_); | 638 web_contents_, |
| 639 request_.security_origin, |
| 640 request_permissions_); |
| 584 } | 641 } |
| 585 | 642 |
| 586 void MediaStreamDevicesController::NotifyUIRequestDenied() { | 643 void MediaStreamDevicesController::NotifyUIRequestDenied() { |
| 587 if (!content_settings_) | 644 if (!content_settings_) |
| 588 return; | 645 return; |
| 589 | 646 |
| 590 if (IsDeviceAudioCaptureRequestedAndAllowed()) { | 647 if (IsDeviceAudioCaptureRequestedAndAllowed()) { |
| 591 request_permissions_[content::MEDIA_DEVICE_AUDIO_CAPTURE].permission = | 648 request_permissions_[content::MEDIA_DEVICE_AUDIO_CAPTURE].permission = |
| 592 MEDIA_BLOCKED_BY_USER; | 649 MEDIA_BLOCKED_BY_USER; |
| 593 } | 650 } |
| 594 if (IsDeviceVideoCaptureRequestedAndAllowed()) { | 651 if (IsDeviceVideoCaptureRequestedAndAllowed()) { |
| 595 request_permissions_[content::MEDIA_DEVICE_VIDEO_CAPTURE].permission = | 652 request_permissions_[content::MEDIA_DEVICE_VIDEO_CAPTURE].permission = |
| 596 MEDIA_BLOCKED_BY_USER; | 653 MEDIA_BLOCKED_BY_USER; |
| 597 } | 654 } |
| 598 | 655 |
| 599 content_settings_->OnMediaStreamPermissionSet(request_.security_origin, | 656 OnMediaStreamPermissionSet(content_settings_, |
| 600 request_permissions_); | 657 web_contents_, |
| 658 request_.security_origin, |
| 659 request_permissions_); |
| 601 } | 660 } |
| 602 | 661 |
| 603 bool MediaStreamDevicesController::IsDeviceAudioCaptureRequestedAndAllowed() | 662 bool MediaStreamDevicesController::IsDeviceAudioCaptureRequestedAndAllowed() |
| 604 const { | 663 const { |
| 605 MediaStreamTypeSettingsMap::const_iterator it = | 664 MediaStreamTypeSettingsMap::const_iterator it = |
| 606 request_permissions_.find(content::MEDIA_DEVICE_AUDIO_CAPTURE); | 665 request_permissions_.find(content::MEDIA_DEVICE_AUDIO_CAPTURE); |
| 607 return (it != request_permissions_.end() && IsCaptureDeviceRequestAllowed() && | 666 return (it != request_permissions_.end() && IsCaptureDeviceRequestAllowed() && |
| 608 it->second.permission == MEDIA_ALLOWED); | 667 it->second.permission == MEDIA_ALLOWED); |
| 609 } | 668 } |
| 610 | 669 |
| 611 bool MediaStreamDevicesController::IsDeviceVideoCaptureRequestedAndAllowed() | 670 bool MediaStreamDevicesController::IsDeviceVideoCaptureRequestedAndAllowed() |
| 612 const { | 671 const { |
| 613 MediaStreamTypeSettingsMap::const_iterator it = | 672 MediaStreamTypeSettingsMap::const_iterator it = |
| 614 request_permissions_.find(content::MEDIA_DEVICE_VIDEO_CAPTURE); | 673 request_permissions_.find(content::MEDIA_DEVICE_VIDEO_CAPTURE); |
| 615 return (it != request_permissions_.end() && IsCaptureDeviceRequestAllowed() && | 674 return (it != request_permissions_.end() && IsCaptureDeviceRequestAllowed() && |
| 616 it->second.permission == MEDIA_ALLOWED); | 675 it->second.permission == MEDIA_ALLOWED); |
| 617 } | 676 } |
| 618 | 677 |
| 619 bool MediaStreamDevicesController::IsCaptureDeviceRequestAllowed() const { | 678 bool MediaStreamDevicesController::IsCaptureDeviceRequestAllowed() const { |
| 620 #if defined(OS_ANDROID) | 679 #if defined(OS_ANDROID) |
| 621 // Don't approve device requests if the tab was hidden. | 680 // Don't approve device requests if the tab was hidden. |
| 622 // TODO(qinmin): Add a test for this. http://crbug.com/396869. | 681 // TODO(qinmin): Add a test for this. http://crbug.com/396869. |
| 623 return web_contents_->GetRenderWidgetHostView()->IsShowing(); | 682 return web_contents_->GetRenderWidgetHostView()->IsShowing(); |
| 624 #endif | 683 #endif |
| 625 return true; | 684 return true; |
| 626 } | 685 } |
| OLD | NEW |