| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/webrtc/public_session_tab_capture_access_handler.
h" | 5 #include "chrome/browser/media/webrtc/public_session_tab_capture_access_handler.
h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "chrome/browser/chromeos/extensions/public_session_permission_helper.h" |
| 13 #include "chrome/browser/profiles/profiles_state.h" |
| 12 #include "chromeos/login/login_state.h" | 14 #include "chromeos/login/login_state.h" |
| 13 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
| 14 #include "extensions/common/extension.h" | 16 #include "extensions/common/extension.h" |
| 15 #include "extensions/common/permissions/manifest_permission_set.h" | 17 #include "extensions/common/permissions/manifest_permission_set.h" |
| 16 #include "extensions/common/permissions/permission_set.h" | 18 #include "extensions/common/permissions/permission_set.h" |
| 17 #include "extensions/common/url_pattern_set.h" | 19 #include "extensions/common/url_pattern_set.h" |
| 18 | 20 |
| 19 namespace { | 21 using extensions::PermissionState; |
| 20 | 22 using PermissionHelper = |
| 21 // Returns true if we're in a Public Session. | 23 extensions::PublicSessionPermissionHelper; |
| 22 bool IsPublicSession() { | |
| 23 return chromeos::LoginState::IsInitialized() && | |
| 24 chromeos::LoginState::Get()->IsPublicSessionUser(); | |
| 25 } | |
| 26 | |
| 27 } // namespace | |
| 28 | 24 |
| 29 PublicSessionTabCaptureAccessHandler::PublicSessionTabCaptureAccessHandler() {} | 25 PublicSessionTabCaptureAccessHandler::PublicSessionTabCaptureAccessHandler() {} |
| 30 | 26 |
| 31 PublicSessionTabCaptureAccessHandler::~PublicSessionTabCaptureAccessHandler() {} | 27 PublicSessionTabCaptureAccessHandler::~PublicSessionTabCaptureAccessHandler() {} |
| 32 | 28 |
| 33 bool PublicSessionTabCaptureAccessHandler::SupportsStreamType( | 29 bool PublicSessionTabCaptureAccessHandler::SupportsStreamType( |
| 34 const content::MediaStreamType type, | 30 const content::MediaStreamType type, |
| 35 const extensions::Extension* extension) { | 31 const extensions::Extension* extension) { |
| 36 return tab_capture_access_handler_.SupportsStreamType(type, extension); | 32 return tab_capture_access_handler_.SupportsStreamType(type, extension); |
| 37 } | 33 } |
| 38 | 34 |
| 39 bool PublicSessionTabCaptureAccessHandler::CheckMediaAccessPermission( | 35 bool PublicSessionTabCaptureAccessHandler::CheckMediaAccessPermission( |
| 40 content::WebContents* web_contents, | 36 content::WebContents* web_contents, |
| 41 const GURL& security_origin, | 37 const GURL& security_origin, |
| 42 content::MediaStreamType type, | 38 content::MediaStreamType type, |
| 43 const extensions::Extension* extension) { | 39 const extensions::Extension* extension) { |
| 44 return tab_capture_access_handler_.CheckMediaAccessPermission( | 40 return tab_capture_access_handler_.CheckMediaAccessPermission( |
| 45 web_contents, security_origin, type, extension); | 41 web_contents, security_origin, type, extension); |
| 46 } | 42 } |
| 47 | 43 |
| 48 void PublicSessionTabCaptureAccessHandler::HandleRequest( | 44 void PublicSessionTabCaptureAccessHandler::HandleRequest( |
| 49 content::WebContents* web_contents, | 45 content::WebContents* web_contents, |
| 50 const content::MediaStreamRequest& request, | 46 const content::MediaStreamRequest& request, |
| 51 const content::MediaResponseCallback& callback, | 47 const content::MediaResponseCallback& callback, |
| 52 const extensions::Extension* extension) { | 48 const extensions::Extension* extension) { |
| 53 // This class handles requests for Public Sessions only, outside of them just | 49 // This class handles requests for Public Sessions only, outside of them just |
| 54 // pass the request through to the original class. | 50 // pass the request through to the original class. |
| 55 if (!IsPublicSession() || !extension) { | 51 if (!profiles::IsPublicSession() || !extension) { |
| 56 return tab_capture_access_handler_.HandleRequest(web_contents, request, | 52 return tab_capture_access_handler_.HandleRequest(web_contents, request, |
| 57 callback, extension); | 53 callback, extension); |
| 58 } | 54 } |
| 59 | 55 |
| 60 UserChoice& user_choice = user_choice_cache_[extension->id()]; | 56 if (request.audio_type != content::MEDIA_TAB_AUDIO_CAPTURE && |
| 61 | 57 request.video_type != content::MEDIA_TAB_VIDEO_CAPTURE) { |
| 62 if ((request.audio_type != content::MEDIA_TAB_AUDIO_CAPTURE && | |
| 63 request.video_type != content::MEDIA_TAB_VIDEO_CAPTURE) || | |
| 64 !user_choice.NeedsPrompting()) { | |
| 65 return ChainHandleRequest(web_contents, request, callback, extension); | 58 return ChainHandleRequest(web_contents, request, callback, extension); |
| 66 } | 59 } |
| 67 | 60 |
| 68 user_choice.SetPrompted(); | 61 auto prompt_resolved_callback = base::Bind( |
| 62 &PublicSessionTabCaptureAccessHandler::ChainHandleRequest, |
| 63 base::Unretained(this), web_contents, request, callback, extension); |
| 69 | 64 |
| 70 extensions::APIPermissionSet new_apis; | 65 PermissionHelper::Instance().HandlePermissionRequest( |
| 71 new_apis.insert(extensions::APIPermission::kTabCapture); | 66 extension, {extensions::APIPermission::kTabCapture}, web_contents, |
| 72 auto permission_set = base::MakeUnique<extensions::PermissionSet>( | 67 prompt_resolved_callback, prompt_resolved_callback); |
| 73 new_apis, extensions::ManifestPermissionSet(), | |
| 74 extensions::URLPatternSet(), extensions::URLPatternSet()); | |
| 75 auto prompt = base::MakeUnique<ExtensionInstallPrompt>(web_contents); | |
| 76 | |
| 77 prompt->ShowDialog( | |
| 78 base::Bind(&PublicSessionTabCaptureAccessHandler::ResolvePermissionPrompt, | |
| 79 base::Unretained(this), web_contents, request, callback, | |
| 80 extension), | |
| 81 extension, | |
| 82 nullptr, // Uses the extension icon. | |
| 83 base::MakeUnique<ExtensionInstallPrompt::Prompt>( | |
| 84 ExtensionInstallPrompt::PERMISSIONS_PROMPT), | |
| 85 std::move(permission_set), | |
| 86 ExtensionInstallPrompt::GetDefaultShowDialogCallback()); | |
| 87 | |
| 88 extension_install_prompt_map_[extension->id()] = std::move(prompt); | |
| 89 } | 68 } |
| 90 | 69 |
| 91 void PublicSessionTabCaptureAccessHandler::ChainHandleRequest( | 70 void PublicSessionTabCaptureAccessHandler::ChainHandleRequest( |
| 92 content::WebContents* web_contents, | 71 content::WebContents* web_contents, |
| 93 const content::MediaStreamRequest& request, | 72 const content::MediaStreamRequest& request, |
| 94 const content::MediaResponseCallback& callback, | 73 const content::MediaResponseCallback& callback, |
| 95 const extensions::Extension* extension) { | 74 const extensions::Extension* extension) { |
| 96 DCHECK(IsPublicSession() && extension); | 75 DCHECK(profiles::IsPublicSession() && extension); |
| 97 const UserChoice& user_choice = user_choice_cache_[extension->id()]; | 76 PermissionState permission_state = PermissionHelper::Instance().GetUserChoice( |
| 77 extension->id(), extensions::APIPermission::kTabCapture); |
| 98 content::MediaStreamRequest request_copy(request); | 78 content::MediaStreamRequest request_copy(request); |
| 99 | 79 |
| 100 // If the user denied tab capture, here the request gets filtered out before | 80 // If the user denied tab capture, here the request gets filtered out before |
| 101 // being passed on to the actual implementation. | 81 // being passed on to the actual implementation. |
| 102 if (!user_choice.IsAllowed()) { | 82 if (permission_state == PermissionState::DENIED) { |
| 103 request_copy.audio_type = content::MEDIA_NO_SERVICE; | 83 request_copy.audio_type = content::MEDIA_NO_SERVICE; |
| 104 request_copy.video_type = content::MEDIA_NO_SERVICE; | 84 request_copy.video_type = content::MEDIA_NO_SERVICE; |
| 105 } | 85 } |
| 106 | 86 |
| 107 // Pass the request through to the original class. | 87 // Pass the request through to the original class. |
| 108 tab_capture_access_handler_.HandleRequest(web_contents, request_copy, | 88 tab_capture_access_handler_.HandleRequest(web_contents, request_copy, |
| 109 callback, extension); | 89 callback, extension); |
| 110 } | 90 } |
| 111 | |
| 112 void PublicSessionTabCaptureAccessHandler::ResolvePermissionPrompt( | |
| 113 content::WebContents* web_contents, | |
| 114 const content::MediaStreamRequest& request, | |
| 115 const content::MediaResponseCallback& callback, | |
| 116 const extensions::Extension* extension, | |
| 117 ExtensionInstallPrompt::Result prompt_result) { | |
| 118 // Dispose of the prompt as it's not needed anymore. | |
| 119 extension_install_prompt_map_.erase(extension->id()); | |
| 120 | |
| 121 bool allowed = prompt_result == ExtensionInstallPrompt::Result::ACCEPTED; | |
| 122 UserChoice& user_choice = user_choice_cache_[extension->id()]; | |
| 123 | |
| 124 user_choice.Set(allowed); | |
| 125 | |
| 126 ChainHandleRequest(web_contents, request, callback, extension); | |
| 127 } | |
| 128 | |
| 129 bool PublicSessionTabCaptureAccessHandler::UserChoice::IsAllowed() const { | |
| 130 return tab_capture_allowed_; | |
| 131 } | |
| 132 | |
| 133 bool PublicSessionTabCaptureAccessHandler::UserChoice::NeedsPrompting() const { | |
| 134 return !tab_capture_prompted_; | |
| 135 } | |
| 136 | |
| 137 void PublicSessionTabCaptureAccessHandler::UserChoice::Set(bool allowed) { | |
| 138 tab_capture_allowed_ = allowed; | |
| 139 } | |
| 140 | |
| 141 void PublicSessionTabCaptureAccessHandler::UserChoice::SetPrompted() { | |
| 142 tab_capture_prompted_ = true; | |
| 143 } | |
| OLD | NEW |