Chromium Code Reviews| 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 { | |
| 20 | |
| 21 // Returns true if we're in a Public Session. | |
| 22 bool IsPublicSession() { | |
| 23 return chromeos::LoginState::IsInitialized() && | |
| 24 chromeos::LoginState::Get()->IsPublicSessionUser(); | |
| 25 } | |
| 26 | |
| 27 } // namespace | |
| 28 | |
| 29 PublicSessionTabCaptureAccessHandler::PublicSessionTabCaptureAccessHandler() {} | 21 PublicSessionTabCaptureAccessHandler::PublicSessionTabCaptureAccessHandler() {} |
| 30 | 22 |
| 31 PublicSessionTabCaptureAccessHandler::~PublicSessionTabCaptureAccessHandler() {} | 23 PublicSessionTabCaptureAccessHandler::~PublicSessionTabCaptureAccessHandler() {} |
| 32 | 24 |
| 33 bool PublicSessionTabCaptureAccessHandler::SupportsStreamType( | 25 bool PublicSessionTabCaptureAccessHandler::SupportsStreamType( |
| 34 const content::MediaStreamType type, | 26 const content::MediaStreamType type, |
| 35 const extensions::Extension* extension) { | 27 const extensions::Extension* extension) { |
| 36 return tab_capture_access_handler_.SupportsStreamType(type, extension); | 28 return tab_capture_access_handler_.SupportsStreamType(type, extension); |
| 37 } | 29 } |
| 38 | 30 |
| 39 bool PublicSessionTabCaptureAccessHandler::CheckMediaAccessPermission( | 31 bool PublicSessionTabCaptureAccessHandler::CheckMediaAccessPermission( |
| 40 content::WebContents* web_contents, | 32 content::WebContents* web_contents, |
| 41 const GURL& security_origin, | 33 const GURL& security_origin, |
| 42 content::MediaStreamType type, | 34 content::MediaStreamType type, |
| 43 const extensions::Extension* extension) { | 35 const extensions::Extension* extension) { |
| 44 return tab_capture_access_handler_.CheckMediaAccessPermission( | 36 return tab_capture_access_handler_.CheckMediaAccessPermission( |
| 45 web_contents, security_origin, type, extension); | 37 web_contents, security_origin, type, extension); |
| 46 } | 38 } |
| 47 | 39 |
| 48 void PublicSessionTabCaptureAccessHandler::HandleRequest( | 40 void PublicSessionTabCaptureAccessHandler::HandleRequest( |
| 49 content::WebContents* web_contents, | 41 content::WebContents* web_contents, |
| 50 const content::MediaStreamRequest& request, | 42 const content::MediaStreamRequest& request, |
| 51 const content::MediaResponseCallback& callback, | 43 const content::MediaResponseCallback& callback, |
| 52 const extensions::Extension* extension) { | 44 const extensions::Extension* extension) { |
| 53 // This class handles requests for Public Sessions only, outside of them just | 45 // This class handles requests for Public Sessions only, outside of them just |
| 54 // pass the request through to the original class. | 46 // pass the request through to the original class. |
| 55 if (!IsPublicSession() || !extension) { | 47 if (!profiles::IsPublicSession() || !extension || |
| 48 (request.audio_type != content::MEDIA_TAB_AUDIO_CAPTURE && | |
| 49 request.video_type != content::MEDIA_TAB_VIDEO_CAPTURE)) { | |
| 56 return tab_capture_access_handler_.HandleRequest(web_contents, request, | 50 return tab_capture_access_handler_.HandleRequest(web_contents, request, |
| 57 callback, extension); | 51 callback, extension); |
| 58 } | 52 } |
| 59 | 53 |
| 60 UserChoice& user_choice = user_choice_cache_[extension->id()]; | 54 // This Unretained is safe because the lifetime of this object is until |
| 55 // process exit (living inside a base::Singleton object). | |
| 56 auto prompt_resolved_callback = base::Bind( | |
| 57 &PublicSessionTabCaptureAccessHandler::ChainHandleRequest, | |
| 58 base::Unretained(this), web_contents, request, callback, extension); | |
| 61 | 59 |
| 62 if ((request.audio_type != content::MEDIA_TAB_AUDIO_CAPTURE && | 60 extensions::permission_helper::HandlePermissionRequest( |
| 63 request.video_type != content::MEDIA_TAB_VIDEO_CAPTURE) || | 61 *extension, {extensions::APIPermission::kTabCapture}, web_contents, |
| 64 !user_choice.NeedsPrompting()) { | 62 prompt_resolved_callback); |
| 65 return ChainHandleRequest(web_contents, request, callback, extension); | |
| 66 } | |
| 67 | |
| 68 user_choice.SetPrompted(); | |
| 69 | |
| 70 extensions::APIPermissionSet new_apis; | |
| 71 new_apis.insert(extensions::APIPermission::kTabCapture); | |
| 72 auto permission_set = base::MakeUnique<extensions::PermissionSet>( | |
| 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 } | 63 } |
| 90 | 64 |
| 91 void PublicSessionTabCaptureAccessHandler::ChainHandleRequest( | 65 void PublicSessionTabCaptureAccessHandler::ChainHandleRequest( |
| 92 content::WebContents* web_contents, | 66 content::WebContents* web_contents, |
| 93 const content::MediaStreamRequest& request, | 67 const content::MediaStreamRequest& request, |
| 94 const content::MediaResponseCallback& callback, | 68 const content::MediaResponseCallback& callback, |
| 95 const extensions::Extension* extension) { | 69 const extensions::Extension* extension, |
| 96 DCHECK(IsPublicSession() && extension); | 70 extensions::PermissionIDSet allowed_permissions) { |
| 97 const UserChoice& user_choice = user_choice_cache_[extension->id()]; | 71 DCHECK(profiles::IsPublicSession() && extension); |
|
Andrew T Wilson (Slow)
2017/02/07 14:26:51
BTW, if this DCHECK is hit, it won't be clear whic
Ivan Šandrk
2017/02/07 20:41:32
Good point. Though I've decided to completely remo
| |
| 98 content::MediaStreamRequest request_copy(request); | 72 content::MediaStreamRequest request_copy(request); |
| 99 | 73 |
| 100 // If the user denied tab capture, here the request gets filtered out before | 74 // If the user denied tab capture, here the request gets filtered out before |
| 101 // being passed on to the actual implementation. | 75 // being passed on to the actual implementation. |
| 102 if (!user_choice.IsAllowed()) { | 76 if (!allowed_permissions.ContainsID(extensions::APIPermission::kTabCapture)) { |
| 103 request_copy.audio_type = content::MEDIA_NO_SERVICE; | 77 request_copy.audio_type = content::MEDIA_NO_SERVICE; |
| 104 request_copy.video_type = content::MEDIA_NO_SERVICE; | 78 request_copy.video_type = content::MEDIA_NO_SERVICE; |
| 105 } | 79 } |
| 106 | 80 |
| 107 // Pass the request through to the original class. | 81 // Pass the request through to the original class. |
| 108 tab_capture_access_handler_.HandleRequest(web_contents, request_copy, | 82 tab_capture_access_handler_.HandleRequest(web_contents, request_copy, |
| 109 callback, extension); | 83 callback, extension); |
| 110 } | 84 } |
| 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 |