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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
ash / public / cpp / holding_space / holding_space_item.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/public/cpp/holding_space/holding_space_item.h"
#include "ash/public/cpp/holding_space/holding_space_image.h"
#include "ash/public/cpp/holding_space/holding_space_util.h"
#include "ash/strings/grit/ash_strings.h"
#include "base/json/values_util.h"
#include "base/memory/ptr_util.h"
#include "base/ranges/algorithm.h"
#include "base/unguessable_token.h"
#include "ui/base/l10n/l10n_util.h"
namespace ash {
namespace {
// Used to indicate which version of serialization is being used. When
// intentionally breaking backwards compatibility, increment this value and
// perform any necessary conversions in `Deserialize()`.
constexpr int kVersion = 1;
// Preference paths.
// NOTE: As these paths are written to preferences, changes must ensure
// backwards compatibility. When intentionally breaking backwards compatibility,
// increment `kVersion` and perform any needed conversions in `Deserialize()`.
constexpr char kFilePathPath[] = "filePath";
constexpr char kIdPath[] = "id";
constexpr char kTypePath[] = "type";
constexpr char kVersionPath[] = "version";
} // namespace
// HoldingSpaceItem::InProgressCommand -----------------------------------------
HoldingSpaceItem::InProgressCommand::InProgressCommand(
HoldingSpaceCommandId command_id,
int label_id,
const gfx::VectorIcon* icon,
Handler handler)
: command_id(command_id),
label_id(label_id),
icon(icon),
handler(std::move(handler)) {
DCHECK(holding_space_util::IsInProgressCommand(command_id));
}
HoldingSpaceItem::InProgressCommand::InProgressCommand(
const InProgressCommand& other)
: command_id(other.command_id),
label_id(other.label_id),
icon(other.icon),
handler(other.handler) {}
HoldingSpaceItem::InProgressCommand&
HoldingSpaceItem::InProgressCommand::operator=(const InProgressCommand&) =
default;
HoldingSpaceItem::InProgressCommand::~InProgressCommand() = default;
bool HoldingSpaceItem::InProgressCommand::operator==(
const InProgressCommand&) const = default;
// HoldingSpaceItem ------------------------------------------------------------
HoldingSpaceItem::~HoldingSpaceItem() {
deletion_callback_list_.Notify();
}
bool HoldingSpaceItem::operator==(const HoldingSpaceItem& rhs) const {
return type_ == rhs.type_ && id_ == rhs.id_ && file_ == rhs.file_ &&
text_ == rhs.text_ && secondary_text_ == rhs.secondary_text_ &&
secondary_text_color_variant_ == rhs.secondary_text_color_variant_ &&
*image_ == *rhs.image_ && progress_ == rhs.progress_ &&
in_progress_commands_ == rhs.in_progress_commands_;
}
// static
std::unique_ptr<HoldingSpaceItem> HoldingSpaceItem::CreateFileBackedItem(
Type type,
const HoldingSpaceFile& file,
ImageResolver image_resolver) {
return CreateFileBackedItem(type, file, HoldingSpaceProgress(),
std::move(image_resolver));
}
// static
std::unique_ptr<HoldingSpaceItem> HoldingSpaceItem::CreateFileBackedItem(
Type type,
const HoldingSpaceFile& file,
const HoldingSpaceProgress& progress,
ImageResolver image_resolver) {
DCHECK(!file.file_system_url.is_empty());
// Note: std::make_unique does not work with private constructors.
return base::WrapUnique(new HoldingSpaceItem(
type, /*id=*/base::UnguessableToken::Create().ToString(), file,
std::move(image_resolver).Run(type, file.file_path), progress));
}
// static
bool HoldingSpaceItem::IsDownloadType(HoldingSpaceItem::Type type) {
switch (type) {
case Type::kArcDownload:
case Type::kDownload:
return true;
case Type::kDiagnosticsLog:
case Type::kDriveSuggestion:
case Type::kLocalSuggestion:
case Type::kNearbyShare:
case Type::kPhoneHubCameraRoll:
case Type::kPhotoshopWeb:
case Type::kPinnedFile:
case Type::kPrintedPdf:
case Type::kScan:
case Type::kScreenRecording:
case Type::kScreenRecordingGif:
case Type::kScreenshot:
return false;
}
}
// static
bool HoldingSpaceItem::IsScreenCaptureType(HoldingSpaceItem::Type type) {
switch (type) {
case Type::kScreenRecording:
case Type::kScreenRecordingGif:
case Type::kScreenshot:
return true;
case Type::kArcDownload:
case Type::kDiagnosticsLog:
case Type::kDownload:
case Type::kDriveSuggestion:
case Type::kLocalSuggestion:
case Type::kNearbyShare:
case Type::kPhoneHubCameraRoll:
case Type::kPhotoshopWeb:
case Type::kPinnedFile:
case Type::kPrintedPdf:
case Type::kScan:
return false;
}
}
// static
bool HoldingSpaceItem::IsSuggestionType(HoldingSpaceItem::Type type) {
switch (type) {
case Type::kDriveSuggestion:
case Type::kLocalSuggestion:
return true;
case Type::kArcDownload:
case Type::kDiagnosticsLog:
case Type::kDownload:
case Type::kNearbyShare:
case Type::kPhoneHubCameraRoll:
case Type::kPhotoshopWeb:
case Type::kPinnedFile:
case Type::kPrintedPdf:
case Type::kScan:
case Type::kScreenRecording:
case Type::kScreenRecordingGif:
case Type::kScreenshot:
return false;
}
}
// static
// NOTE: This method must remain in sync with `Serialize()`. If multiple
// serialization versions are supported, care must be taken to handle each.
std::unique_ptr<HoldingSpaceItem> HoldingSpaceItem::Deserialize(
const base::Value::Dict& dict,
ImageResolver image_resolver) {
const std::optional<int> version = dict.FindInt(kVersionPath);
DCHECK(version.has_value() && version.value() == kVersion);
const Type type = DeserializeType(dict);
const base::FilePath file_path = DeserializeFilePath(dict);
// NOTE: `std::make_unique` does not work with private constructors.
return base::WrapUnique(new HoldingSpaceItem(
type, DeserializeId(dict),
HoldingSpaceFile(file_path, HoldingSpaceFile::FileSystemType::kUnknown,
/*file_system_url=*/GURL()),
std::move(image_resolver).Run(type, file_path), HoldingSpaceProgress()));
}
// static
// NOTE: This method must remain in sync with `Serialize()`. If multiple
// serialization versions are supported, care must be taken to handle each.
const std::string& HoldingSpaceItem::DeserializeId(
const base::Value::Dict& dict) {
const std::optional<int> version = dict.FindInt(kVersionPath);
DCHECK(version.has_value() && version.value() == kVersion);
const std::string* id = dict.FindString(kIdPath);
DCHECK(id);
return *id;
}
// static
// NOTE: This method must remain in sync with `Serialize()`. If multiple
// serialization versions are supported, care must be taken to handle each.
base::FilePath HoldingSpaceItem::DeserializeFilePath(
const base::Value::Dict& dict) {
const std::optional<int> version = dict.FindInt(kVersionPath);
DCHECK(version.has_value() && version.value() == kVersion);
const std::optional<base::FilePath> file_path =
base::ValueToFilePath(dict.Find(kFilePathPath));
DCHECK(file_path.has_value());
return file_path.value();
}
// static
// NOTE: This method must remain in sync with `Serialize()`. If multiple
// serialization versions are supported, care must be taken to handle each.
HoldingSpaceItem::Type HoldingSpaceItem::DeserializeType(
const base::Value::Dict& dict) {
const std::optional<int> version = dict.FindInt(kVersionPath);
DCHECK(version.has_value() && version.value() == kVersion);
return static_cast<Type>(dict.FindInt(kTypePath).value());
}
// NOTE: This method must remain in sync with `Deserialize()`. The
// return value will be written to preferences so this implementation must
// maintain backwards compatibility so long as `kVersion` remains unchanged.
base::Value::Dict HoldingSpaceItem::Serialize() const {
base::Value::Dict dict;
dict.Set(kVersionPath, kVersion);
dict.Set(kTypePath, static_cast<int>(type_));
dict.Set(kIdPath, id_);
dict.Set(kFilePathPath, base::FilePathToValue(file_.file_path));
return dict;
}
base::CallbackListSubscription HoldingSpaceItem::AddDeletionCallback(
base::RepeatingClosureList::CallbackType callback) const {
return deletion_callback_list_.Add(std::move(callback));
}
bool HoldingSpaceItem::IsInitialized() const {
return !file_.file_system_url.is_empty();
}
void HoldingSpaceItem::Initialize(const HoldingSpaceFile& file) {
DCHECK(!IsInitialized());
DCHECK(!file.file_system_url.is_empty());
file_ = file;
}
std::optional<HoldingSpaceFile> HoldingSpaceItem::SetBackingFile(
const HoldingSpaceFile& file) {
if (file_ == file) {
return std::nullopt;
}
auto previous_file = std::move(file_);
file_ = file;
image_->UpdateBackingFilePath(file_.file_path);
return previous_file;
}
std::u16string HoldingSpaceItem::GetText() const {
return text_.value_or(file_.file_path.BaseName().LossyDisplayName());
}
std::optional<std::optional<std::u16string>> HoldingSpaceItem::SetText(
const std::optional<std::u16string>& text) {
if (text_ == text) {
return std::nullopt;
}
auto previous_text = std::move(text_);
text_ = text;
return previous_text;
}
std::optional<std::optional<std::u16string>> HoldingSpaceItem::SetSecondaryText(
const std::optional<std::u16string>& secondary_text) {
if (secondary_text_ == secondary_text) {
return std::nullopt;
}
auto previous_secondary_text = std::move(secondary_text_);
secondary_text_ = secondary_text;
return previous_secondary_text;
}
std::optional<std::optional<HoldingSpaceColorVariant>>
HoldingSpaceItem::SetSecondaryTextColorVariant(
const std::optional<HoldingSpaceColorVariant>&
secondary_text_color_variant) {
if (secondary_text_color_variant_ == secondary_text_color_variant) {
return std::nullopt;
}
auto previous_secondary_text_color_variant = secondary_text_color_variant_;
secondary_text_color_variant_ = secondary_text_color_variant;
return previous_secondary_text_color_variant;
}
std::u16string HoldingSpaceItem::GetAccessibleName() const {
if (accessible_name_)
return accessible_name_.value();
const std::u16string text = GetText();
if (!secondary_text_)
return text;
return l10n_util::GetStringFUTF16(
IDS_ASH_HOLDING_SPACE_ITEM_A11Y_NAME_AND_TOOLTIP, text,
secondary_text_.value());
}
std::optional<std::optional<std::u16string>>
HoldingSpaceItem::SetAccessibleName(
const std::optional<std::u16string>& accessible_name) {
if (accessible_name_ == accessible_name) {
return std::nullopt;
}
auto previous_accessible_name = std::move(accessible_name_);
accessible_name_ = accessible_name;
return previous_accessible_name;
}
std::optional<HoldingSpaceProgress> HoldingSpaceItem::SetProgress(
const HoldingSpaceProgress& progress) {
// NOTE: Progress can only be updated for in progress items.
if (progress_ == progress || progress_.IsComplete()) {
return std::nullopt;
}
auto previous_progress = progress_;
progress_ = progress;
if (progress_.IsComplete())
in_progress_commands_.clear();
return previous_progress;
}
std::optional<std::vector<HoldingSpaceItem::InProgressCommand>>
HoldingSpaceItem::SetInProgressCommands(
std::vector<InProgressCommand> in_progress_commands) {
DCHECK(base::ranges::all_of(in_progress_commands,
[](const InProgressCommand& in_progress_command) {
return holding_space_util::IsInProgressCommand(
in_progress_command.command_id);
}));
if (progress_.IsComplete() || in_progress_commands_ == in_progress_commands) {
return std::nullopt;
}
auto previous_in_progress_commands = std::move(in_progress_commands_);
in_progress_commands_ = in_progress_commands;
return previous_in_progress_commands;
}
void HoldingSpaceItem::InvalidateImage() {
if (image_)
image_->Invalidate();
}
HoldingSpaceItem::HoldingSpaceItem(Type type,
const std::string& id,
const HoldingSpaceFile& file,
std::unique_ptr<HoldingSpaceImage> image,
const HoldingSpaceProgress& progress)
: type_(type),
id_(id),
file_(file),
image_(std::move(image)),
progress_(progress) {}
} // namespace ash