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
|
// Copyright 2014 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/document_scan_api.h"
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "base/functional/bind.h"
#include "chrome/browser/ash/crosapi/document_scan_ash.h"
#include "chrome/browser/extensions/api/document_scan/document_scan_api_handler.h"
#include "chrome/browser/extensions/chrome_extension_function_details.h"
namespace extensions {
namespace {
// Error messages that can be included in a response when scanning fails.
constexpr char kUserGestureRequiredError[] =
"User gesture required to perform scan";
constexpr char kScanImageError[] = "Failed to scan image";
} // namespace
DocumentScanScanFunction::DocumentScanScanFunction() = default;
DocumentScanScanFunction::~DocumentScanScanFunction() = default;
ExtensionFunction::ResponseAction DocumentScanScanFunction::Run() {
auto params = api::document_scan::Scan::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params);
if (!user_gesture()) {
return RespondNow(Error(kUserGestureRequiredError));
}
std::vector<std::string> mime_types;
if (params->options.mime_types) {
mime_types = std::move(*params->options.mime_types);
}
DocumentScanAPIHandler::Get(browser_context())
->SimpleScan(
extension_, mime_types,
base::BindOnce(&DocumentScanScanFunction::OnScanCompleted, this));
return RespondLater();
}
void DocumentScanScanFunction::OnScanCompleted(
std::optional<api::document_scan::ScanResults> scan_results,
std::optional<std::string> error) {
if (error) {
Respond(Error(*error));
return;
}
if (!scan_results) {
Respond(Error(kScanImageError));
return;
}
Respond(WithArguments(scan_results->ToValue()));
}
DocumentScanGetScannerListFunction::DocumentScanGetScannerListFunction() =
default;
DocumentScanGetScannerListFunction::~DocumentScanGetScannerListFunction() =
default;
ExtensionFunction::ResponseAction DocumentScanGetScannerListFunction::Run() {
auto params = api::document_scan::GetScannerList::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params);
DocumentScanAPIHandler::Get(browser_context())
->GetScannerList(
ChromeExtensionFunctionDetails(this).GetNativeWindowForUI(),
extension_, user_gesture(), std::move(params->filter),
base::BindOnce(
&DocumentScanGetScannerListFunction::OnScannerListReceived,
this));
return did_respond() ? AlreadyResponded() : RespondLater();
}
void DocumentScanGetScannerListFunction::OnScannerListReceived(
api::document_scan::GetScannerListResponse response) {
Respond(ArgumentList(
api::document_scan::GetScannerList::Results::Create(response)));
}
DocumentScanOpenScannerFunction::DocumentScanOpenScannerFunction() = default;
DocumentScanOpenScannerFunction::~DocumentScanOpenScannerFunction() = default;
ExtensionFunction::ResponseAction DocumentScanOpenScannerFunction::Run() {
auto params = api::document_scan::OpenScanner::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params);
DocumentScanAPIHandler::Get(browser_context())
->OpenScanner(
extension_, std::move(params->scanner_id),
base::BindOnce(&DocumentScanOpenScannerFunction::OnResponseReceived,
this));
return did_respond() ? AlreadyResponded() : RespondLater();
}
void DocumentScanOpenScannerFunction::OnResponseReceived(
api::document_scan::OpenScannerResponse response) {
Respond(
ArgumentList(api::document_scan::OpenScanner::Results::Create(response)));
}
DocumentScanGetOptionGroupsFunction::DocumentScanGetOptionGroupsFunction() =
default;
DocumentScanGetOptionGroupsFunction::~DocumentScanGetOptionGroupsFunction() =
default;
ExtensionFunction::ResponseAction DocumentScanGetOptionGroupsFunction::Run() {
auto params = api::document_scan::GetOptionGroups::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params);
DocumentScanAPIHandler::Get(browser_context())
->GetOptionGroups(
extension_, std::move(params->scanner_handle),
base::BindOnce(
&DocumentScanGetOptionGroupsFunction::OnResponseReceived, this));
return did_respond() ? AlreadyResponded() : RespondLater();
}
void DocumentScanGetOptionGroupsFunction::OnResponseReceived(
api::document_scan::GetOptionGroupsResponse response) {
Respond(ArgumentList(
api::document_scan::GetOptionGroups::Results::Create(response)));
}
DocumentScanCloseScannerFunction::DocumentScanCloseScannerFunction() = default;
DocumentScanCloseScannerFunction::~DocumentScanCloseScannerFunction() = default;
ExtensionFunction::ResponseAction DocumentScanCloseScannerFunction::Run() {
auto params = api::document_scan::CloseScanner::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params);
DocumentScanAPIHandler::Get(browser_context())
->CloseScanner(
extension_, std::move(params->scanner_handle),
base::BindOnce(&DocumentScanCloseScannerFunction::OnResponseReceived,
this));
return did_respond() ? AlreadyResponded() : RespondLater();
}
void DocumentScanCloseScannerFunction::OnResponseReceived(
api::document_scan::CloseScannerResponse response) {
Respond(ArgumentList(
api::document_scan::CloseScanner::Results::Create(response)));
}
DocumentScanSetOptionsFunction::DocumentScanSetOptionsFunction() = default;
DocumentScanSetOptionsFunction::~DocumentScanSetOptionsFunction() = default;
ExtensionFunction::ResponseAction DocumentScanSetOptionsFunction::Run() {
auto params = api::document_scan::SetOptions::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params);
DocumentScanAPIHandler::Get(browser_context())
->SetOptions(
extension_, std::move(params->scanner_handle),
std::move(params->options),
base::BindOnce(&DocumentScanSetOptionsFunction::OnResponseReceived,
this));
return did_respond() ? AlreadyResponded() : RespondLater();
}
void DocumentScanSetOptionsFunction::OnResponseReceived(
api::document_scan::SetOptionsResponse response) {
Respond(
ArgumentList(api::document_scan::SetOptions::Results::Create(response)));
}
DocumentScanStartScanFunction::DocumentScanStartScanFunction() = default;
DocumentScanStartScanFunction::~DocumentScanStartScanFunction() = default;
ExtensionFunction::ResponseAction DocumentScanStartScanFunction::Run() {
auto params = api::document_scan::StartScan::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params);
DocumentScanAPIHandler::Get(browser_context())
->StartScan(
ChromeExtensionFunctionDetails(this).GetNativeWindowForUI(),
extension_, user_gesture(), std::move(params->scanner_handle),
std::move(params->options),
base::BindOnce(&DocumentScanStartScanFunction::OnResponseReceived,
this));
return did_respond() ? AlreadyResponded() : RespondLater();
}
void DocumentScanStartScanFunction::OnResponseReceived(
api::document_scan::StartScanResponse response) {
Respond(
ArgumentList(api::document_scan::StartScan::Results::Create(response)));
}
DocumentScanCancelScanFunction::DocumentScanCancelScanFunction() = default;
DocumentScanCancelScanFunction::~DocumentScanCancelScanFunction() = default;
ExtensionFunction::ResponseAction DocumentScanCancelScanFunction::Run() {
auto params = api::document_scan::CancelScan::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params);
DocumentScanAPIHandler::Get(browser_context())
->CancelScan(
extension_, std::move(params->job),
base::BindOnce(&DocumentScanCancelScanFunction::OnResponseReceived,
this));
return did_respond() ? AlreadyResponded() : RespondLater();
}
void DocumentScanCancelScanFunction::OnResponseReceived(
api::document_scan::CancelScanResponse response) {
Respond(
ArgumentList(api::document_scan::CancelScan::Results::Create(response)));
}
DocumentScanReadScanDataFunction::DocumentScanReadScanDataFunction() = default;
DocumentScanReadScanDataFunction::~DocumentScanReadScanDataFunction() = default;
ExtensionFunction::ResponseAction DocumentScanReadScanDataFunction::Run() {
auto params = api::document_scan::ReadScanData::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params);
DocumentScanAPIHandler::Get(browser_context())
->ReadScanData(
extension_, std::move(params->job),
base::BindOnce(&DocumentScanReadScanDataFunction::OnResponseReceived,
this));
return did_respond() ? AlreadyResponded() : RespondLater();
}
void DocumentScanReadScanDataFunction::OnResponseReceived(
api::document_scan::ReadScanDataResponse response) {
Respond(ArgumentList(
api::document_scan::ReadScanData::Results::Create(response)));
}
} // namespace extensions
|