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
|
// Copyright 2023 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/extensions/api/document_scan/start_scan_runner.h"
#include "base/containers/contains.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/extensions/extensions_dialogs.h"
#include "chrome/common/pref_names.h"
#include "chromeos/crosapi/mojom/document_scan.mojom.h"
#include "components/prefs/pref_service.h"
#include "extensions/browser/image_loader.h"
#include "extensions/common/extension.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/views/native_window_tracker.h"
namespace extensions {
namespace {
// Icon size for confirmation dialogs.
constexpr int kIconSize = 64;
// There is no easy way to interact with UI dialogs that are generated by Chrome
// itself, so we need to have a way to bypass this for testing.
std::optional<bool> g_start_scan_confirmation_result = std::nullopt;
bool CanSkipConfirmation(content::BrowserContext* browser_context,
const ExtensionId& extension_id) {
const base::Value::List& list =
Profile::FromBrowserContext(browser_context)
->GetPrefs()
->GetList(prefs::kDocumentScanAPITrustedExtensions);
return base::Contains(list, base::Value(extension_id));
// TODO(b/312740272): Add a way for the user to make their consent permanent.
// Note that this needs to be per device.
}
} // namespace
StartScanRunner::StartScanRunner(gfx::NativeWindow native_window,
content::BrowserContext* browser_context,
scoped_refptr<const Extension> extension,
crosapi::mojom::DocumentScan* document_scan)
: native_window_(native_window),
browser_context_(browser_context),
extension_(std::move(extension)),
document_scan_(document_scan),
approved_(false) {
CHECK(extension_);
if (native_window_) {
native_window_tracker_ = views::NativeWindowTracker::Create(native_window_);
}
}
StartScanRunner::~StartScanRunner() = default;
// static
base::AutoReset<std::optional<bool>>
StartScanRunner::SetStartScanConfirmationResultForTesting(bool val) {
return base::AutoReset<std::optional<bool>>(&g_start_scan_confirmation_result,
val);
}
void StartScanRunner::Start(bool is_approved,
const std::string& scanner_name,
const std::string& scanner_handle,
crosapi::mojom::StartScanOptionsPtr options,
StartScanCallback callback) {
CHECK(!callback_) << "start scan call already in progress";
callback_ = std::move(callback);
options_ = std::move(options);
scanner_handle_ = std::move(scanner_handle);
// TODO(b/312740272): Skip confirmation prompt if previous consent was within
// the recent past (specific timeout TBD). Note that confirmation needs to be
// per device.
if (is_approved || CanSkipConfirmation(browser_context_, extension_->id())) {
SendStartScanRequest();
return;
}
// If a test has set the confirmation result, go directly to the end handler
// instead of displaying the dialog.
if (g_start_scan_confirmation_result) {
OnConfirmationDialogClosed(g_start_scan_confirmation_result.value());
return;
}
ImageLoader::Get(browser_context_)
->LoadImageAtEveryScaleFactorAsync(
extension_.get(), gfx::Size(kIconSize, kIconSize),
base::BindOnce(&StartScanRunner::ShowStartScanDialog,
weak_ptr_factory_.GetWeakPtr(), scanner_name));
}
const ExtensionId& StartScanRunner::extension_id() const {
return extension_->id();
}
void StartScanRunner::ShowStartScanDialog(const std::string& scanner_name,
const gfx::Image& icon) {
// If the browser window was closed during API request handling, treat it the
// same as if the user denied the request.
if (native_window_tracker_ &&
native_window_tracker_->WasNativeWindowDestroyed()) {
OnConfirmationDialogClosed(false);
return;
}
ShowDocumentScannerStartScanConfirmationDialog(
native_window_, extension_->id(), base::UTF8ToUTF16(extension_->name()),
base::UTF8ToUTF16(scanner_name), icon.AsImageSkia(),
base::BindOnce(&StartScanRunner::OnConfirmationDialogClosed,
weak_ptr_factory_.GetWeakPtr()));
}
void StartScanRunner::OnConfirmationDialogClosed(bool approved) {
if (approved) {
SendStartScanRequest();
return;
}
auto response = crosapi::mojom::StartPreparedScanResponse::New();
response->result = crosapi::mojom::ScannerOperationResult::kAccessDenied;
response->scanner_handle = scanner_handle_;
std::move(callback_).Run(std::move(response));
}
void StartScanRunner::SendStartScanRequest() {
approved_ = true;
document_scan_->StartPreparedScan(
scanner_handle_, std::move(options_),
base::BindOnce(&StartScanRunner::OnStartScanResponse,
weak_ptr_factory_.GetWeakPtr()));
// TODO(b/312757530): Clean up the pending call if the DocumentScan service
// goes away without running our callback.
}
void StartScanRunner::OnStartScanResponse(
crosapi::mojom::StartPreparedScanResponsePtr response) {
std::move(callback_).Run(std::move(response));
}
} // namespace extensions
|