| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/media/public_session_media_access_handler.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" |
| 11 #include "base/memory/ptr_util.h" |
| 12 #include "chromeos/login/login_state.h" |
| 13 #include "content/public/browser/web_contents.h" |
| 14 #include "extensions/common/extension.h" |
| 15 #include "extensions/common/permissions/manifest_permission_set.h" |
| 16 #include "extensions/common/permissions/permission_set.h" |
| 17 #include "extensions/common/url_pattern_set.h" |
| 18 |
| 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 PublicSessionMediaAccessHandler::PublicSessionMediaAccessHandler() {} |
| 30 |
| 31 PublicSessionMediaAccessHandler::~PublicSessionMediaAccessHandler() {} |
| 32 |
| 33 bool PublicSessionMediaAccessHandler::SupportsStreamType( |
| 34 const content::MediaStreamType type, |
| 35 const extensions::Extension* extension) { |
| 36 return extension_media_access_handler_.SupportsStreamType(type, extension); |
| 37 } |
| 38 |
| 39 bool PublicSessionMediaAccessHandler::CheckMediaAccessPermission( |
| 40 content::WebContents* web_contents, |
| 41 const GURL& security_origin, |
| 42 content::MediaStreamType type, |
| 43 const extensions::Extension* extension) { |
| 44 return extension_media_access_handler_.CheckMediaAccessPermission( |
| 45 web_contents, security_origin, type, extension); |
| 46 } |
| 47 |
| 48 void PublicSessionMediaAccessHandler::HandleRequest( |
| 49 content::WebContents* web_contents, |
| 50 const content::MediaStreamRequest& request, |
| 51 const content::MediaResponseCallback& callback, |
| 52 const extensions::Extension* extension) { |
| 53 // This class handles requests for Public Sessions only, outside of them just |
| 54 // pass the request through to the original class. |
| 55 if (!IsPublicSession() || !extension->is_platform_app()) { |
| 56 return extension_media_access_handler_.HandleRequest(web_contents, request, |
| 57 callback, extension); |
| 58 } |
| 59 |
| 60 bool needs_prompt = false; |
| 61 extensions::APIPermissionSet new_apis; |
| 62 UserChoice& user_choice = user_choice_cache_[extension->id()]; |
| 63 |
| 64 if (user_choice.NeedsPrompting(request.audio_type)) { |
| 65 new_apis.insert(extensions::APIPermission::kAudioCapture); |
| 66 user_choice.SetPrompted(content::MEDIA_DEVICE_AUDIO_CAPTURE); |
| 67 needs_prompt = true; |
| 68 } |
| 69 if (user_choice.NeedsPrompting(request.video_type)) { |
| 70 new_apis.insert(extensions::APIPermission::kVideoCapture); |
| 71 user_choice.SetPrompted(content::MEDIA_DEVICE_VIDEO_CAPTURE); |
| 72 needs_prompt = true; |
| 73 } |
| 74 |
| 75 if (!needs_prompt) |
| 76 return ChainHandleRequest(web_contents, request, callback, extension); |
| 77 |
| 78 auto permission_set = base::MakeUnique<extensions::PermissionSet>( |
| 79 new_apis, extensions::ManifestPermissionSet(), |
| 80 extensions::URLPatternSet(), extensions::URLPatternSet()); |
| 81 |
| 82 auto prompt = base::MakeUnique<ExtensionInstallPrompt>(web_contents); |
| 83 |
| 84 prompt->ShowDialog( |
| 85 base::Bind(&PublicSessionMediaAccessHandler::ResolvePermissionPrompt, |
| 86 base::Unretained(this), web_contents, request, callback, |
| 87 extension), |
| 88 extension, |
| 89 nullptr, // Uses the extension icon. |
| 90 base::MakeUnique<ExtensionInstallPrompt::Prompt>( |
| 91 ExtensionInstallPrompt::PERMISSIONS_PROMPT), |
| 92 std::move(permission_set), |
| 93 ExtensionInstallPrompt::GetDefaultShowDialogCallback()); |
| 94 |
| 95 extension_install_prompt_map_[extension->id()] = std::move(prompt); |
| 96 } |
| 97 |
| 98 void PublicSessionMediaAccessHandler::ChainHandleRequest( |
| 99 content::WebContents* web_contents, |
| 100 const content::MediaStreamRequest& request, |
| 101 const content::MediaResponseCallback& callback, |
| 102 const extensions::Extension* extension) { |
| 103 DCHECK(IsPublicSession() && extension && extension->is_platform_app()); |
| 104 const UserChoice& user_choice = user_choice_cache_[extension->id()]; |
| 105 content::MediaStreamRequest request_copy(request); |
| 106 |
| 107 // If the user denies audio or video capture, here it gets filtered out from |
| 108 // the request before being passed on to the actual implementation. |
| 109 if (!user_choice.IsAllowed(content::MEDIA_DEVICE_AUDIO_CAPTURE)) |
| 110 request_copy.audio_type = content::MEDIA_NO_SERVICE; |
| 111 if (!user_choice.IsAllowed(content::MEDIA_DEVICE_VIDEO_CAPTURE)) |
| 112 request_copy.video_type = content::MEDIA_NO_SERVICE; |
| 113 |
| 114 // Pass the request through to the original class. |
| 115 extension_media_access_handler_.HandleRequest(web_contents, request_copy, |
| 116 callback, extension); |
| 117 } |
| 118 |
| 119 void PublicSessionMediaAccessHandler::ResolvePermissionPrompt( |
| 120 content::WebContents* web_contents, |
| 121 const content::MediaStreamRequest& request, |
| 122 const content::MediaResponseCallback& callback, |
| 123 const extensions::Extension* extension, |
| 124 ExtensionInstallPrompt::Result prompt_result) { |
| 125 // Dispose of the prompt as it's not needed anymore. |
| 126 extension_install_prompt_map_.erase(extension->id()); |
| 127 |
| 128 bool allowed = prompt_result == ExtensionInstallPrompt::Result::ACCEPTED; |
| 129 UserChoice& user_choice = user_choice_cache_[extension->id()]; |
| 130 |
| 131 if (request.audio_type == content::MEDIA_DEVICE_AUDIO_CAPTURE) |
| 132 user_choice.Set(content::MEDIA_DEVICE_AUDIO_CAPTURE, allowed); |
| 133 if (request.video_type == content::MEDIA_DEVICE_VIDEO_CAPTURE) |
| 134 user_choice.Set(content::MEDIA_DEVICE_VIDEO_CAPTURE, allowed); |
| 135 |
| 136 ChainHandleRequest(web_contents, request, callback, extension); |
| 137 } |
| 138 |
| 139 bool PublicSessionMediaAccessHandler::UserChoice::IsAllowed( |
| 140 content::MediaStreamType type) const { |
| 141 switch (type) { |
| 142 case content::MEDIA_DEVICE_AUDIO_CAPTURE: |
| 143 return !audio_prompted_ || audio_allowed_; |
| 144 case content::MEDIA_DEVICE_VIDEO_CAPTURE: |
| 145 return !video_prompted_ || video_allowed_; |
| 146 default: |
| 147 NOTREACHED(); |
| 148 return false; |
| 149 } |
| 150 } |
| 151 |
| 152 bool PublicSessionMediaAccessHandler::UserChoice::NeedsPrompting( |
| 153 content::MediaStreamType type) const { |
| 154 switch (type) { |
| 155 case content::MEDIA_DEVICE_AUDIO_CAPTURE: |
| 156 return !audio_prompted_; |
| 157 case content::MEDIA_DEVICE_VIDEO_CAPTURE: |
| 158 return !video_prompted_; |
| 159 default: |
| 160 return false; |
| 161 } |
| 162 } |
| 163 |
| 164 void PublicSessionMediaAccessHandler::UserChoice::Set( |
| 165 content::MediaStreamType type, |
| 166 bool allowed) { |
| 167 switch (type) { |
| 168 case content::MEDIA_DEVICE_AUDIO_CAPTURE: |
| 169 audio_allowed_ = allowed; |
| 170 break; |
| 171 case content::MEDIA_DEVICE_VIDEO_CAPTURE: |
| 172 video_allowed_ = allowed; |
| 173 break; |
| 174 default: |
| 175 NOTREACHED(); |
| 176 } |
| 177 } |
| 178 |
| 179 void PublicSessionMediaAccessHandler::UserChoice::SetPrompted( |
| 180 content::MediaStreamType type) { |
| 181 switch (type) { |
| 182 case content::MEDIA_DEVICE_AUDIO_CAPTURE: |
| 183 audio_prompted_ = true; |
| 184 break; |
| 185 case content::MEDIA_DEVICE_VIDEO_CAPTURE: |
| 186 video_prompted_ = true; |
| 187 break; |
| 188 default: |
| 189 NOTREACHED(); |
| 190 } |
| 191 } |
| OLD | NEW |