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
|
// Copyright 2024 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/ash/policy/skyvault/skyvault_capture_upload_notification.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/strings/grit/ash_strings.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "chrome/browser/notifications/system_notification_helper.h"
#include "components/strings/grit/components_strings.h"
#include "ui/base/l10n/l10n_util.h"
namespace policy::skyvault {
namespace {
constexpr char kUploadNotificationId[] = "skyvault_capture_upload_notification";
} // namespace
SkyvaultCaptureUploadNotification::SkyvaultCaptureUploadNotification(
const base::FilePath& filename,
bool for_video) {
message_center::RichNotificationData options;
options.buttons = {message_center::ButtonInfo(l10n_util::GetStringUTF16(
IDS_POLICY_SKYVAULT_SCREENCAPTURE_UPLOAD_CANCEL_BUTTON))};
options.progress_status = l10n_util::GetStringUTF16(
IDS_POLICY_SKYVAULT_SCREENCAPTURE_UPLOAD_ONEDRIVE_MESSAGE);
const auto title_id =
for_video ? IDS_POLICY_SKYVAULT_SCREENCAPTURE_RECORDING_UPLOAD_TITLE
: IDS_POLICY_SKYVAULT_SCREENCAPTURE_UPLOAD_TITLE;
notification_ = std::make_unique<message_center::Notification>(
message_center::NOTIFICATION_TYPE_PROGRESS, kUploadNotificationId,
l10n_util::GetStringUTF16(title_id),
/*message=*/std::u16string(), ui::ImageModel(),
l10n_util::GetStringUTF16(IDS_ASH_SCREEN_CAPTURE_DISPLAY_SOURCE), GURL(),
message_center::NotifierId(message_center::NotifierType::SYSTEM_COMPONENT,
kUploadNotificationId,
ash::NotificationCatalogName::kScreenCapture),
options,
base::MakeRefCounted<message_center::HandleNotificationClickDelegate>(
base::BindRepeating(
&SkyvaultCaptureUploadNotification::OnButtonPressed,
GetWeakPtr())));
// Set up indefinite progress first.
notification_->set_progress(-1);
notification_->set_vector_small_image(ash::kCaptureModeIcon);
SystemNotificationHelper::GetInstance()->Display(*notification_);
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, {base::MayBlock()}, base::GetFileSizeCallback(filename),
base::BindOnce(&SkyvaultCaptureUploadNotification::OnFileSizeRetrieved,
weak_ptr_factory_.GetWeakPtr()));
}
SkyvaultCaptureUploadNotification::~SkyvaultCaptureUploadNotification() {
SystemNotificationHelper::GetInstance()->Close(kUploadNotificationId);
}
void SkyvaultCaptureUploadNotification::UpdateProgress(int64_t bytes_so_far) {
if (file_size_.has_value()) {
const auto percent = 100.0 * bytes_so_far / file_size_.value();
notification_->set_progress(static_cast<int>(percent));
SystemNotificationHelper::GetInstance()->Display(*notification_);
}
}
void SkyvaultCaptureUploadNotification::SetCancelClosure(
base::OnceClosure cancel_closure) {
cancel_closure_ = std::move(cancel_closure);
}
void SkyvaultCaptureUploadNotification::OnButtonPressed(
std::optional<int> button_index) {
if (!button_index || button_index.value() != 0) {
return;
}
if (cancel_closure_) {
std::move(cancel_closure_).Run();
}
}
void SkyvaultCaptureUploadNotification::OnFileSizeRetrieved(
std::optional<int64_t> file_size) {
file_size_ = file_size;
UpdateProgress(0);
}
} // namespace policy::skyvault
|