forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestore_data.cc
303 lines (251 loc) · 9.67 KB
/
restore_data.cc
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
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/app_restore/restore_data.h"
#include <utility>
#include "base/i18n/number_formatting.h"
#include "base/strings/string_number_conversions.h"
#include "base/values.h"
#include "components/app_constants/constants.h"
#include "components/app_restore/app_launch_info.h"
#include "components/app_restore/window_info.h"
namespace app_restore {
namespace {
// Used to generate unique window IDs for desk template launches. Numbers lower
// than this are reserved for full restore.
int32_t g_desk_template_window_restore_id = 1000000000;
} // namespace
RestoreData::RestoreData() = default;
RestoreData::RestoreData(std::unique_ptr<base::Value> restore_data_value) {
if (!restore_data_value || !restore_data_value->is_dict()) {
DVLOG(0) << "Fail to parse full restore data. "
<< "Cannot find the full restore data dict.";
return;
}
for (auto iter : restore_data_value->DictItems()) {
const std::string& app_id = iter.first;
base::Value* value = restore_data_value->FindDictKey(app_id);
if (!value || !value->is_dict()) {
DVLOG(0) << "Fail to parse full restore data. "
<< "Cannot find the app restore data dict.";
continue;
}
for (auto data_iter : value->DictItems()) {
int window_id = 0;
if (!base::StringToInt(data_iter.first, &window_id)) {
DVLOG(0) << "Fail to parse full restore data. "
<< "Cannot find the valid id.";
continue;
}
app_id_to_launch_list_[app_id][window_id] =
std::make_unique<AppRestoreData>(
std::move(*value->FindDictKey(data_iter.first)));
}
}
}
RestoreData::~RestoreData() = default;
std::unique_ptr<RestoreData> RestoreData::Clone() const {
std::unique_ptr<RestoreData> restore_data = std::make_unique<RestoreData>();
for (const auto& it : app_id_to_launch_list_) {
for (const auto& data_it : it.second) {
restore_data->app_id_to_launch_list_[it.first][data_it.first] =
data_it.second->Clone();
}
}
return restore_data;
}
base::Value RestoreData::ConvertToValue() const {
base::Value restore_data_dict(base::Value::Type::DICTIONARY);
for (const auto& it : app_id_to_launch_list_) {
if (it.second.empty())
continue;
base::Value info_dict(base::Value::Type::DICTIONARY);
for (const auto& data : it.second) {
info_dict.SetKey(base::NumberToString(data.first),
data.second->ConvertToValue());
}
restore_data_dict.SetKey(it.first, std::move(info_dict));
}
return restore_data_dict;
}
bool RestoreData::HasAppTypeBrowser() const {
auto it = app_id_to_launch_list_.find(app_constants::kChromeAppId);
if (it == app_id_to_launch_list_.end())
return false;
for (const auto& data : it->second) {
if (data.second->app_type_browser.has_value() &&
data.second->app_type_browser.value()) {
return true;
}
}
return false;
}
bool RestoreData::HasBrowser() const {
auto it = app_id_to_launch_list_.find(app_constants::kChromeAppId);
if (it == app_id_to_launch_list_.end())
return false;
for (const auto& data : it->second) {
if (!data.second->app_type_browser.has_value() ||
!data.second->app_type_browser.value()) {
return true;
}
}
return false;
}
bool RestoreData::HasAppRestoreData(const std::string& app_id,
int32_t window_id) {
return GetAppRestoreData(app_id, window_id) != nullptr;
}
void RestoreData::AddAppLaunchInfo(
std::unique_ptr<AppLaunchInfo> app_launch_info) {
if (!app_launch_info || !app_launch_info->window_id.has_value())
return;
const std::string app_id = app_launch_info->app_id;
const int32_t window_id = app_launch_info->window_id.value();
app_id_to_launch_list_[app_id][window_id] =
std::make_unique<AppRestoreData>(std::move(app_launch_info));
}
void RestoreData::ModifyWindowId(const std::string& app_id,
int32_t old_window_id,
int32_t new_window_id) {
auto it = app_id_to_launch_list_.find(app_id);
if (it == app_id_to_launch_list_.end())
return;
auto data_it = it->second.find(old_window_id);
if (data_it == it->second.end())
return;
it->second[new_window_id] = std::move(data_it->second);
it->second.erase(data_it);
}
void RestoreData::ModifyWindowInfo(const std::string& app_id,
int32_t window_id,
const WindowInfo& window_info) {
auto* app_restore_data = GetAppRestoreDataMutable(app_id, window_id);
if (app_restore_data)
app_restore_data->ModifyWindowInfo(window_info);
}
void RestoreData::ModifyThemeColor(const std::string& app_id,
int32_t window_id,
uint32_t primary_color,
uint32_t status_bar_color) {
auto* app_restore_data = GetAppRestoreDataMutable(app_id, window_id);
if (app_restore_data)
app_restore_data->ModifyThemeColor(primary_color, status_bar_color);
}
void RestoreData::SetNextRestoreWindowIdForChromeApp(
const std::string& app_id) {
auto it = app_id_to_launch_list_.find(app_id);
if (it == app_id_to_launch_list_.end())
return;
chrome_app_id_to_current_window_id_[app_id] = it->second.begin()->first;
if (it->second.size() == 1)
return;
// When a chrome app has multiple windows, all windows will be sent to the
// background.
for (auto& data_it : it->second)
data_it.second->activation_index = INT32_MAX;
}
void RestoreData::RemoveAppRestoreData(const std::string& app_id,
int window_id) {
if (app_id_to_launch_list_.find(app_id) == app_id_to_launch_list_.end())
return;
app_id_to_launch_list_[app_id].erase(window_id);
if (app_id_to_launch_list_[app_id].empty())
app_id_to_launch_list_.erase(app_id);
}
void RestoreData::SendWindowToBackground(const std::string& app_id,
int window_id) {
auto* app_restore_data = GetAppRestoreDataMutable(app_id, window_id);
if (app_restore_data)
app_restore_data->activation_index = INT32_MAX;
}
void RestoreData::RemoveApp(const std::string& app_id) {
app_id_to_launch_list_.erase(app_id);
chrome_app_id_to_current_window_id_.erase(app_id);
}
std::unique_ptr<AppLaunchInfo> RestoreData::GetAppLaunchInfo(
const std::string& app_id,
int window_id) {
auto* app_restore_data = GetAppRestoreData(app_id, window_id);
return app_restore_data
? app_restore_data->GetAppLaunchInfo(app_id, window_id)
: nullptr;
}
std::unique_ptr<WindowInfo> RestoreData::GetWindowInfo(
const std::string& app_id,
int window_id) {
auto* app_restore_data = GetAppRestoreData(app_id, window_id);
return app_restore_data ? app_restore_data->GetWindowInfo() : nullptr;
}
int32_t RestoreData::FetchRestoreWindowId(const std::string& app_id) {
auto it = app_id_to_launch_list_.find(app_id);
if (it == app_id_to_launch_list_.end())
return 0;
if (chrome_app_id_to_current_window_id_.find(app_id) ==
chrome_app_id_to_current_window_id_.end()) {
return 0;
}
int window_id = chrome_app_id_to_current_window_id_[app_id];
// Move to the next window_id.
auto data_it = it->second.find(window_id);
DCHECK(data_it != it->second.end());
++data_it;
if (data_it == it->second.end())
chrome_app_id_to_current_window_id_.erase(app_id);
else
chrome_app_id_to_current_window_id_[app_id] = data_it->first;
return window_id;
}
const AppRestoreData* RestoreData::GetAppRestoreData(const std::string& app_id,
int window_id) const {
auto it = app_id_to_launch_list_.find(app_id);
if (it == app_id_to_launch_list_.end())
return nullptr;
auto data_it = it->second.find(window_id);
if (data_it == it->second.end())
return nullptr;
return data_it->second.get();
}
void RestoreData::SetDeskIndex(int desk_index) {
for (auto& [app_id, launch_list] : app_id_to_launch_list_) {
for (auto& [window_id, app_restore_data] : launch_list) {
app_restore_data->desk_id = desk_index;
}
}
}
void RestoreData::MakeWindowIdsUniqueForDeskTemplate() {
for (auto& [app_id, launch_list] : app_id_to_launch_list_) {
// We don't want to do in-place updates of the launch list since we could
// have collisions. We'll therefore build a new LaunchList and pilfer the
// old one for AppRestoreData.
LaunchList new_launch_list;
for (auto& [window_id, app_restore_data] : launch_list) {
new_launch_list[g_desk_template_window_restore_id++] =
std::move(app_restore_data);
}
launch_list = std::move(new_launch_list);
}
}
std::string RestoreData::ToString() const {
if (app_id_to_launch_list_.empty())
return "empty";
std::string result = "( ";
for (const auto& entry : app_id_to_launch_list_) {
result += base::StringPrintf(
"(App ID: %s, Count: %s)", entry.first.c_str(),
base::UTF16ToUTF8(base::FormatNumber(entry.second.size())).c_str());
for (const auto& windows : entry.second) {
result +=
base::StringPrintf(
"(Window ID: %s)",
base::UTF16ToUTF8(base::FormatNumber(windows.first)).c_str()) +
windows.second->GetWindowInfo()->ToString();
}
}
return result + " )";
}
AppRestoreData* RestoreData::GetAppRestoreDataMutable(const std::string& app_id,
int window_id) {
return const_cast<AppRestoreData*>(GetAppRestoreData(app_id, window_id));
}
} // namespace app_restore