1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/ash/capture_mode/chrome_capture_mode_delegate.h"
#include <memory>
#include "ash/strings/grit/ash_strings.h"
#include "base/check.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/i18n/time_formatting.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/apps/app_service/launch_utils.h"
#include "chrome/browser/ash/crosapi/crosapi_ash.h"
#include "chrome/browser/ash/crosapi/crosapi_manager.h"
#include "chrome/browser/ash/drive/drive_integration_service.h"
#include "chrome/browser/ash/file_manager/path_util.h"
#include "chrome/browser/ash/policy/dlp/dlp_content_manager_ash.h"
#include "chrome/browser/ash/video_conference/video_conference_manager_ash.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/download/download_prefs.h"
#include "chrome/browser/platform_util.h"
#include "chrome/browser/policy/system_features_disable_list_policy_handler.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/ash/capture_mode/recording_overlay_view_impl.h"
#include "chrome/browser/ui/ash/screenshot_area.h"
#include "chrome/browser/ui/ash/system_web_apps/system_web_app_ui_utils.h"
#include "chrome/browser/web_applications/web_app_id_constants.h"
#include "chrome/common/pref_names.h"
#include "chromeos/ash/components/login/login_state/login_state.h"
#include "chromeos/ash/services/recording/public/mojom/recording_service.mojom.h"
#include "components/drive/file_errors.h"
#include "components/prefs/pref_service.h"
#include "components/services/app_service/public/cpp/app_launch_util.h"
#include "content/public/browser/audio_service.h"
#include "content/public/browser/download_manager.h"
#include "content/public/browser/service_process_host.h"
#include "content/public/browser/video_capture_service.h"
#include "services/video_capture/public/mojom/video_capture_service.mojom.h"
#include "ui/aura/window.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/window_open_disposition.h"
namespace {
ChromeCaptureModeDelegate* g_instance = nullptr;
ScreenshotArea ConvertToScreenshotArea(const aura::Window* window,
const gfx::Rect& bounds) {
return window->IsRootWindow()
? ScreenshotArea::CreateForPartialWindow(window, bounds)
: ScreenshotArea::CreateForWindow(window);
}
bool IsScreenCaptureDisabledByPolicy() {
return g_browser_process->local_state()->GetBoolean(
prefs::kDisableScreenshots);
}
} // namespace
ChromeCaptureModeDelegate::ChromeCaptureModeDelegate() {
DCHECK_EQ(g_instance, nullptr);
g_instance = this;
}
ChromeCaptureModeDelegate::~ChromeCaptureModeDelegate() {
DCHECK_EQ(g_instance, this);
g_instance = nullptr;
}
// static
ChromeCaptureModeDelegate* ChromeCaptureModeDelegate::Get() {
DCHECK(g_instance);
return g_instance;
}
void ChromeCaptureModeDelegate::SetIsScreenCaptureLocked(bool locked) {
is_screen_capture_locked_ = locked;
if (is_screen_capture_locked_)
InterruptVideoRecordingIfAny();
}
bool ChromeCaptureModeDelegate::InterruptVideoRecordingIfAny() {
if (interrupt_video_recording_callback_) {
std::move(interrupt_video_recording_callback_).Run();
return true;
}
return false;
}
base::FilePath ChromeCaptureModeDelegate::GetUserDefaultDownloadsFolder()
const {
DCHECK(ash::LoginState::Get()->IsUserLoggedIn());
auto* profile = ProfileManager::GetActiveUserProfile();
DCHECK(profile);
if (!profile->GetDownloadManager()->GetBrowserContext()) {
// Some browser tests use a |content::MockDownloadManager| which doesn't
// have a browser context. In this case, just return an empty path.
return base::FilePath();
}
DownloadPrefs* download_prefs =
DownloadPrefs::FromBrowserContext(ProfileManager::GetActiveUserProfile());
// We use the default downloads directory instead of the one that can be
// configured from the browser's settings, since it can point to an invalid
// location, which the browser handles by prompting the user to select
// another one when accessed, but Capture Mode doesn't have this capability.
// We also decided that this browser setting should not affect where the OS
// saves the captured files. https://crbug.com/1192406.
return download_prefs->GetDefaultDownloadDirectoryForProfile();
}
void ChromeCaptureModeDelegate::ShowScreenCaptureItemInFolder(
const base::FilePath& file_path) {
platform_util::ShowItemInFolder(ProfileManager::GetActiveUserProfile(),
file_path);
}
void ChromeCaptureModeDelegate::OpenScreenshotInImageEditor(
const base::FilePath& file_path) {
Profile* profile = ProfileManager::GetActiveUserProfile();
if (!profile)
return;
ash::SystemAppLaunchParams params;
params.launch_paths = {file_path};
params.launch_source = apps::LaunchSource::kFromFileManager;
ash::LaunchSystemWebAppAsync(profile, ash::SystemWebAppType::MEDIA, params);
}
bool ChromeCaptureModeDelegate::Uses24HourFormat() const {
Profile* profile = ProfileManager::GetActiveUserProfile();
// TODO(afakhry): Consider moving |prefs::kUse24HourClock| to ash/public so
// we can do this entirely in ash.
if (profile)
return profile->GetPrefs()->GetBoolean(prefs::kUse24HourClock);
return base::GetHourClockType() == base::k24HourClock;
}
void ChromeCaptureModeDelegate::CheckCaptureModeInitRestrictionByDlp(
ash::OnCaptureModeDlpRestrictionChecked callback) {
policy::DlpContentManagerAsh::Get()->CheckCaptureModeInitRestriction(
std::move(callback));
}
void ChromeCaptureModeDelegate::CheckCaptureOperationRestrictionByDlp(
const aura::Window* window,
const gfx::Rect& bounds,
ash::OnCaptureModeDlpRestrictionChecked callback) {
const ScreenshotArea area = ConvertToScreenshotArea(window, bounds);
policy::DlpContentManagerAsh::Get()->CheckScreenshotRestriction(
area, std::move(callback));
}
bool ChromeCaptureModeDelegate::IsCaptureAllowedByPolicy() const {
return !is_screen_capture_locked_ && !IsScreenCaptureDisabledByPolicy();
}
void ChromeCaptureModeDelegate::StartObservingRestrictedContent(
const aura::Window* window,
const gfx::Rect& bounds,
base::OnceClosure stop_callback) {
// The order here matters, since DlpContentManagerAsh::OnVideoCaptureStarted()
// may call InterruptVideoRecordingIfAny() right away, so the callback must be
// set first.
interrupt_video_recording_callback_ = std::move(stop_callback);
policy::DlpContentManagerAsh::Get()->OnVideoCaptureStarted(
ConvertToScreenshotArea(window, bounds));
}
void ChromeCaptureModeDelegate::StopObservingRestrictedContent(
ash::OnCaptureModeDlpRestrictionChecked callback) {
interrupt_video_recording_callback_.Reset();
policy::DlpContentManagerAsh::Get()->CheckStoppedVideoCapture(
std::move(callback));
}
void ChromeCaptureModeDelegate::OnCaptureImageAttempted(
const aura::Window* window,
const gfx::Rect& bounds) {
policy::DlpContentManagerAsh::Get()->OnImageCapture(
ConvertToScreenshotArea(window, bounds));
}
mojo::Remote<recording::mojom::RecordingService>
ChromeCaptureModeDelegate::LaunchRecordingService() {
return content::ServiceProcessHost::Launch<
recording::mojom::RecordingService>(
content::ServiceProcessHost::Options()
.WithDisplayName("Recording Service")
.Pass());
}
void ChromeCaptureModeDelegate::BindAudioStreamFactory(
mojo::PendingReceiver<media::mojom::AudioStreamFactory> receiver) {
content::GetAudioService().BindStreamFactory(std::move(receiver));
}
void ChromeCaptureModeDelegate::OnSessionStateChanged(bool started) {
is_session_active_ = started;
}
void ChromeCaptureModeDelegate::OnServiceRemoteReset() {}
bool ChromeCaptureModeDelegate::GetDriveFsMountPointPath(
base::FilePath* result) const {
if (!ash::LoginState::Get()->IsUserLoggedIn())
return false;
drive::DriveIntegrationService* integration_service =
drive::DriveIntegrationServiceFactory::FindForProfile(
ProfileManager::GetActiveUserProfile());
if (!integration_service || !integration_service->IsMounted())
return false;
*result = integration_service->GetMountPointPath();
return true;
}
base::FilePath ChromeCaptureModeDelegate::GetAndroidFilesPath() const {
return file_manager::util::GetAndroidFilesPath();
}
base::FilePath ChromeCaptureModeDelegate::GetLinuxFilesPath() const {
return file_manager::util::GetCrostiniMountDirectory(
ProfileManager::GetActiveUserProfile());
}
std::unique_ptr<ash::RecordingOverlayView>
ChromeCaptureModeDelegate::CreateRecordingOverlayView() const {
return std::make_unique<RecordingOverlayViewImpl>(
ProfileManager::GetActiveUserProfile());
}
void ChromeCaptureModeDelegate::ConnectToVideoSourceProvider(
mojo::PendingReceiver<video_capture::mojom::VideoSourceProvider> receiver) {
content::GetVideoCaptureService().ConnectToVideoSourceProvider(
std::move(receiver));
}
void ChromeCaptureModeDelegate::GetDriveFsFreeSpaceBytes(
ash::OnGotDriveFsFreeSpace callback) {
DCHECK(ash::LoginState::Get()->IsUserLoggedIn());
drive::DriveIntegrationService* integration_service =
drive::DriveIntegrationServiceFactory::FindForProfile(
ProfileManager::GetActiveUserProfile());
if (!integration_service) {
std::move(callback).Run(std::numeric_limits<int64_t>::max());
return;
}
integration_service->GetQuotaUsage(
base::BindOnce(&ChromeCaptureModeDelegate::OnGetDriveQuotaUsage,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
bool ChromeCaptureModeDelegate::IsCameraDisabledByPolicy() const {
return policy::SystemFeaturesDisableListPolicyHandler::
IsSystemFeatureDisabled(policy::SystemFeature::kCamera,
g_browser_process->local_state());
}
bool ChromeCaptureModeDelegate::IsAudioCaptureDisabledByPolicy() const {
return !ProfileManager::GetActiveUserProfile()->GetPrefs()->GetBoolean(
prefs::kAudioCaptureAllowed);
}
void ChromeCaptureModeDelegate::RegisterVideoConferenceManagerClient(
crosapi::mojom::VideoConferenceManagerClient* client,
const base::UnguessableToken& client_id) {
crosapi::CrosapiManager::Get()
->crosapi_ash()
->video_conference_manager_ash()
->RegisterCppClient(client, client_id);
}
void ChromeCaptureModeDelegate::UnregisterVideoConferenceManagerClient(
const base::UnguessableToken& client_id) {
crosapi::CrosapiManager::Get()
->crosapi_ash()
->video_conference_manager_ash()
->UnregisterClient(client_id);
}
void ChromeCaptureModeDelegate::UpdateVideoConferenceManager(
crosapi::mojom::VideoConferenceMediaUsageStatusPtr status) {
crosapi::CrosapiManager::Get()
->crosapi_ash()
->video_conference_manager_ash()
->NotifyMediaUsageUpdate(std::move(status), base::DoNothing());
}
void ChromeCaptureModeDelegate::NotifyDeviceUsedWhileDisabled(
crosapi::mojom::VideoConferenceMediaDevice device) {
crosapi::CrosapiManager::Get()
->crosapi_ash()
->video_conference_manager_ash()
->NotifyDeviceUsedWhileDisabled(
device,
l10n_util::GetStringUTF16(IDS_ASH_SCREEN_CAPTURE_DISPLAY_SOURCE),
base::DoNothing());
}
void ChromeCaptureModeDelegate::OnGetDriveQuotaUsage(
ash::OnGotDriveFsFreeSpace callback,
drive::FileError error,
drivefs::mojom::QuotaUsagePtr usage) {
if (error != drive::FileError::FILE_ERROR_OK) {
std::move(callback).Run(-1);
return;
}
std::move(callback).Run(usage->free_cloud_bytes);
}
|