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
ash / webui / scanning / scanning_handler.cc [blame]
// 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 "ash/webui/scanning/scanning_handler.h"
#include <utility>
#include "ash/constants/ash_features.h"
#include "ash/webui/scanning/scanning_app_delegate.h"
#include "base/check.h"
#include "base/check_op.h"
#include "base/feature_list.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "base/values.h"
#include "chromeos/strings/grit/chromeos_strings.h"
#include "content/public/browser/web_contents.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/shell_dialogs/select_file_policy.h"
#include "ui/shell_dialogs/selected_file_info.h"
namespace {
constexpr char kBaseName[] = "baseName";
constexpr char kFilePath[] = "filePath";
} // namespace
namespace ash {
ScanningHandler::ScanningHandler(
std::unique_ptr<ScanningAppDelegate> scanning_app_delegate)
: scanning_app_delegate_(std::move(scanning_app_delegate)),
task_runner_(base::ThreadPool::CreateSequencedTaskRunner(
{base::MayBlock(), base::TaskPriority::USER_VISIBLE,
base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN})) {}
ScanningHandler::~ScanningHandler() {
if (select_file_dialog_)
select_file_dialog_->ListenerDestroyed();
}
void ScanningHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback(
"initialize", base::BindRepeating(&ScanningHandler::HandleInitialize,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"requestScanToLocation",
base::BindRepeating(&ScanningHandler::HandleRequestScanToLocation,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"showFileInLocation",
base::BindRepeating(&ScanningHandler::HandleShowFileInLocation,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"getPluralString",
base::BindRepeating(&ScanningHandler::HandleGetPluralString,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"getMyFilesPath",
base::BindRepeating(&ScanningHandler::HandleGetMyFilesPath,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"openFilesInMediaApp",
base::BindRepeating(&ScanningHandler::HandleOpenFilesInMediaApp,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"saveScanSettings",
base::BindRepeating(&ScanningHandler::HandleSaveScanSettings,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"getScanSettings",
base::BindRepeating(&ScanningHandler::HandleGetScanSettings,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"ensureValidFilePath",
base::BindRepeating(&ScanningHandler::HandleEnsureValidFilePath,
base::Unretained(this)));
}
void ScanningHandler::FileSelected(const ui::SelectedFileInfo& file,
int index) {
DCHECK(IsJavascriptAllowed());
select_file_dialog_ = nullptr;
ResolveJavascriptCallback(base::Value(scan_location_callback_id_),
CreateSelectedPathValue(file.path()));
}
void ScanningHandler::FileSelectionCanceled() {
DCHECK(IsJavascriptAllowed());
select_file_dialog_ = nullptr;
ResolveJavascriptCallback(base::Value(scan_location_callback_id_),
CreateSelectedPathValue(base::FilePath()));
}
base::Value::Dict ScanningHandler::CreateSelectedPathValue(
const base::FilePath& path) {
base::Value::Dict selected_path;
selected_path.Set(kFilePath, path.value());
selected_path.Set(kBaseName,
scanning_app_delegate_->GetBaseNameFromPath(path));
return selected_path;
}
void ScanningHandler::AddStringToPluralMap(const std::string& name,
int string_id) {
string_id_map_[name] = string_id;
}
void ScanningHandler::SetWebUIForTest(content::WebUI* web_ui) {
set_web_ui(web_ui);
}
void ScanningHandler::HandleInitialize(const base::Value::List& args) {
DCHECK(args.empty());
AllowJavascript();
}
void ScanningHandler::HandleOpenFilesInMediaApp(const base::Value::List& args) {
if (!IsJavascriptAllowed())
return;
CHECK_EQ(1U, args.size());
const base::Value::List& value_list = args[0].GetList();
DCHECK(!value_list.empty());
std::vector<base::FilePath> file_paths;
file_paths.reserve(value_list.size());
for (const base::Value& file_path_value : value_list)
file_paths.push_back(base::FilePath(file_path_value.GetString()));
scanning_app_delegate_->OpenFilesInMediaApp(file_paths);
}
void ScanningHandler::HandleRequestScanToLocation(
const base::Value::List& args) {
if (!IsJavascriptAllowed())
return;
// Early return if the select file dialog is already active.
if (select_file_dialog_)
return;
CHECK_EQ(1U, args.size());
scan_location_callback_id_ = args[0].GetString();
content::WebContents* web_contents = web_ui()->GetWebContents();
gfx::NativeWindow owning_window =
web_contents ? web_contents->GetTopLevelNativeWindow()
: gfx::NativeWindow();
select_file_dialog_ = ui::SelectFileDialog::Create(
this, scanning_app_delegate_->CreateChromeSelectFilePolicy());
select_file_dialog_->SelectFile(
ui::SelectFileDialog::SELECT_FOLDER,
l10n_util::GetStringUTF16(IDS_SCANNING_APP_SELECT_DIALOG_TITLE),
base::FilePath() /* default_path */, nullptr /* file_types */,
0 /* file_type_index */,
base::FilePath::StringType() /* default_extension */, owning_window);
}
void ScanningHandler::HandleShowFileInLocation(const base::Value::List& args) {
if (!IsJavascriptAllowed())
return;
CHECK_EQ(2U, args.size());
const std::string callback = args[0].GetString();
const base::FilePath file_location(args[1].GetString());
scanning_app_delegate_->ShowFileInFilesApp(
file_location,
base::BindOnce(&ScanningHandler::OnShowFileInLocation,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
void ScanningHandler::OnShowFileInLocation(const std::string& callback,
const bool files_app_opened) {
ResolveJavascriptCallback(base::Value(callback),
base::Value(files_app_opened));
}
void ScanningHandler::HandleGetPluralString(const base::Value::List& args) {
if (!IsJavascriptAllowed())
return;
CHECK_EQ(3U, args.size());
const std::string callback = args[0].GetString();
const std::string name = args[1].GetString();
const int count = args[2].GetInt();
auto iter = string_id_map_.find(name);
if (iter == string_id_map_.end()) {
// Only reachable if the WebUI renderer is misbehaving.
return;
}
const std::u16string localized_string =
l10n_util::GetPluralStringFUTF16(iter->second, count);
ResolveJavascriptCallback(base::Value(callback),
base::Value(localized_string));
}
void ScanningHandler::HandleGetMyFilesPath(const base::Value::List& args) {
if (!IsJavascriptAllowed())
return;
CHECK_EQ(1U, args.size());
const std::string& callback = args[0].GetString();
const base::FilePath my_files_path = scanning_app_delegate_->GetMyFilesPath();
ResolveJavascriptCallback(base::Value(callback),
base::Value(my_files_path.value()));
}
void ScanningHandler::HandleSaveScanSettings(const base::Value::List& args) {
if (!IsJavascriptAllowed())
return;
CHECK_EQ(1U, args.size());
const std::string& scan_settings = args[0].GetString();
scanning_app_delegate_->SaveScanSettingsToPrefs(scan_settings);
}
void ScanningHandler::HandleGetScanSettings(const base::Value::List& args) {
if (!IsJavascriptAllowed())
return;
CHECK_EQ(1U, args.size());
const std::string& callback = args[0].GetString();
ResolveJavascriptCallback(
base::Value(callback),
base::Value(scanning_app_delegate_->GetScanSettingsFromPrefs()));
}
void ScanningHandler::HandleEnsureValidFilePath(const base::Value::List& args) {
if (!IsJavascriptAllowed())
return;
CHECK_EQ(2U, args.size());
const std::string callback = args[0].GetString();
const base::FilePath file_path(args[1].GetString());
task_runner_->PostTaskAndReplyWithResult(
FROM_HERE, base::BindOnce(&base::PathExists, file_path),
base::BindOnce(&ScanningHandler::OnPathExists,
weak_ptr_factory_.GetWeakPtr(), file_path, callback));
}
void ScanningHandler::OnPathExists(const base::FilePath& file_path,
const std::string& callback,
bool file_path_exists) {
// When |file_path| is not valid, return a dictionary with an empty file path.
const bool file_path_valid =
file_path_exists &&
scanning_app_delegate_->IsFilePathSupported(file_path);
ResolveJavascriptCallback(
base::Value(callback),
CreateSelectedPathValue(file_path_valid ? file_path : base::FilePath()));
}
} // namespace ash