-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
session_service_base.cc
769 lines (652 loc) · 28.6 KB
/
session_service_base.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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
// Copyright 2021 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/sessions/session_service_base.h"
#include <stddef.h>
#include <set>
#include <utility>
#include <vector>
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/task/sequenced_task_runner.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/apps/app_service/web_contents_app_id_utils.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sessions/session_service_log.h"
#include "chrome/browser/sessions/session_service_utils.h"
#include "chrome/browser/sessions/tab_restore_service_factory.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/startup/startup_browser_creator.h"
#include "chrome/browser/ui/tabs/saved_tab_groups/saved_tab_group_keyed_service.h"
#include "chrome/browser/ui/tabs/saved_tab_groups/saved_tab_group_service_factory.h"
#include "chrome/browser/ui/tabs/tab_group.h"
#include "chrome/browser/ui/tabs/tab_group_model.h"
#include "chrome/browser/ui/ui_features.h"
#include "components/sessions/content/content_serialized_navigation_builder.h"
#include "components/sessions/content/session_tab_helper.h"
#include "components/sessions/core/command_storage_manager.h"
#include "components/sessions/core/session_command.h"
#include "components/sessions/core/session_constants.h"
#include "components/sessions/core/session_types.h"
#include "content/public/browser/navigation_details.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/session_storage_namespace.h"
#if BUILDFLAG(IS_MAC)
#include "chrome/browser/app_controller_mac.h"
#endif
// TODO(crbug.com/1424800): Remove once the restore issue has been resolved.
#if BUILDFLAG(IS_CHROMEOS_LACROS)
#undef ENABLED_VLOG_LEVEL
#define ENABLED_VLOG_LEVEL 1
#endif
using base::Time;
using content::NavigationEntry;
using content::WebContents;
using sessions::ContentSerializedNavigationBuilder;
using sessions::SerializedNavigationEntry;
namespace {
// Every kWritesPerReset commands triggers recreating the file.
const int kWritesPerReset = 250;
// User data key for WebContents to derive their types.
const void* const kSessionServiceBaseUserDataKey =
&kSessionServiceBaseUserDataKey;
// User data key for BrowserContextData.
const void* const kProfileTaskRunnerKey = &kProfileTaskRunnerKey;
const void* const kProfileTaskRunnerKeyForApps = &kProfileTaskRunnerKeyForApps;
// Tracks the SequencedTaskRunner that SessionService uses for a particular
// profile. At certain points SessionService may be destroyed, and then
// recreated. This class ensures that when this happens, the same
// SequencedTaskRunner is used. Without this, each instance would have its
// own SequencedTaskRunner, which is problematic as it might then be possible
// for the newly created SessionService to attempt to read from a file the
// previous SessionService was still writing to. No two instances of
// SessionService for a particular profile and type combination can exist at the
// same time, but the backend (CommandStorageBackend) is destroyed on the
// SequencedTaskRunner, meaning without this, it might be possible for two
// CommandStorageBackends to exist at the same time and attempt to use the same
// file.
class TaskRunnerData : public base::SupportsUserData::Data {
public:
TaskRunnerData()
: task_runner_(
sessions::CommandStorageManager::CreateDefaultBackendTaskRunner()) {
}
TaskRunnerData(const TaskRunnerData&) = delete;
TaskRunnerData& operator=(const TaskRunnerData&) = delete;
~TaskRunnerData() override = default;
static scoped_refptr<base::SequencedTaskRunner>
GetBackendTaskRunnerForProfile(Profile* profile,
SessionServiceBase::SessionServiceType type) {
const void* key =
type == SessionServiceBase::SessionServiceType::kAppRestore
? kProfileTaskRunnerKeyForApps
: kProfileTaskRunnerKey;
TaskRunnerData* data =
static_cast<TaskRunnerData*>(profile->GetUserData(key));
if (!data) {
profile->SetUserData(key, std::make_unique<TaskRunnerData>());
data = static_cast<TaskRunnerData*>(profile->GetUserData(key));
}
return data->task_runner_;
}
private:
scoped_refptr<base::SequencedTaskRunner> task_runner_;
};
// SessionServiceBaseUserData
// -------------------------------------------------------------
class SessionServiceBaseUserData : public base::SupportsUserData::Data {
public:
explicit SessionServiceBaseUserData(Browser::Type type) : type_(type) {}
~SessionServiceBaseUserData() override = default;
SessionServiceBaseUserData(const SessionServiceBaseUserData&) = delete;
SessionServiceBaseUserData& operator=(const SessionServiceBaseUserData&) =
delete;
Browser::Type type() const { return type_; }
private:
const Browser::Type type_;
};
} // namespace
// SessionServiceBase
// -------------------------------------------------------------
// Note: This is protected as it is only intended to be called by child classes
SessionServiceBase::SessionServiceBase(Profile* profile,
SessionServiceType type)
: profile_(profile) {
// Covert SessionServiceType to backend CSM::SessionType enum.
sessions::CommandStorageManager::SessionType backend_type =
type == SessionServiceType::kSessionRestore
? sessions::CommandStorageManager::kSessionRestore
: sessions::CommandStorageManager::kAppRestore;
command_storage_manager_ = std::make_unique<sessions::CommandStorageManager>(
backend_type, profile->GetPath(), this,
/* enable_crypto */ false, std::vector<uint8_t>(),
TaskRunnerData::GetBackendTaskRunnerForProfile(profile, type));
// We should never be created when incognito.
DCHECK(!profile->IsOffTheRecord());
BrowserList::AddObserver(this);
}
SessionServiceBase::~SessionServiceBase() {
// The BrowserList should outlive the SessionService since it's static and
// the SessionService is a KeyedService.
BrowserList::RemoveObserver(this);
// command_storage_manager_->Save() should be called by child classes which
// should have destructed the command_storage_manager.
DCHECK(command_storage_manager_ == nullptr);
}
// static
Browser::Type SessionServiceBase::GetBrowserTypeFromWebContents(
content::WebContents* web_contents) {
SessionServiceBaseUserData* data = static_cast<SessionServiceBaseUserData*>(
web_contents->GetUserData(&kSessionServiceBaseUserDataKey));
// Browser tab WebContents will have the UserData set on them. However, it is
// possible that WebContents that are not tabs call into this code.
// In that case, data will be null and we just return TYPE_NORMAL.
if (!data)
return Browser::Type::TYPE_NORMAL;
return data->type();
}
void SessionServiceBase::SetWindowVisibleOnAllWorkspaces(
SessionID window_id,
bool visible_on_all_workspaces) {
if (!ShouldTrackChangesToWindow(window_id))
return;
ScheduleCommand(sessions::CreateSetWindowVisibleOnAllWorkspacesCommand(
window_id, visible_on_all_workspaces));
}
void SessionServiceBase::ResetFromCurrentBrowsers() {
if (is_saving_enabled_)
ScheduleResetCommands();
}
void SessionServiceBase::SetTabWindow(SessionID window_id, SessionID tab_id) {
if (!ShouldTrackChangesToWindow(window_id))
return;
ScheduleCommand(sessions::CreateSetTabWindowCommand(window_id, tab_id));
}
void SessionServiceBase::SetWindowBounds(SessionID window_id,
const gfx::Rect& bounds,
ui::WindowShowState show_state) {
if (!ShouldTrackChangesToWindow(window_id))
return;
ScheduleCommand(
sessions::CreateSetWindowBoundsCommand(window_id, bounds, show_state));
}
void SessionServiceBase::SetWindowWorkspace(SessionID window_id,
const std::string& workspace) {
if (!ShouldTrackChangesToWindow(window_id))
return;
ScheduleCommand(
sessions::CreateSetWindowWorkspaceCommand(window_id, workspace));
}
void SessionServiceBase::SetTabIndexInWindow(SessionID window_id,
SessionID tab_id,
int new_index) {
if (!ShouldTrackChangesToWindow(window_id))
return;
ScheduleCommand(
sessions::CreateSetTabIndexInWindowCommand(tab_id, new_index));
}
void SessionServiceBase::TabInserted(WebContents* contents) {
sessions::SessionTabHelper* session_tab_helper =
sessions::SessionTabHelper::FromWebContents(contents);
if (!ShouldTrackChangesToWindow(session_tab_helper->window_id()))
return;
SetTabWindow(session_tab_helper->window_id(),
session_tab_helper->session_id());
std::string app_id = apps::GetAppIdForWebContents(contents);
if (!app_id.empty()) {
SetTabExtensionAppID(session_tab_helper->window_id(),
session_tab_helper->session_id(), app_id);
}
// Record the association between the SessionStorageNamespace and the
// tab.
//
// TODO(ajwong): This should be processing the whole map rather than
// just the default. This in particular will not work for tabs with only
// isolated apps which won't have a default partition.
content::SessionStorageNamespace* session_storage_namespace =
contents->GetController().GetDefaultSessionStorageNamespace();
ScheduleCommand(sessions::CreateSessionStorageAssociatedCommand(
session_tab_helper->session_id(), session_storage_namespace->id()));
session_storage_namespace->SetShouldPersist(true);
// The tab being inserted is most likely already registered by
// Browser::WebContentsCreated, but just register the UserData again.
contents->SetUserData(&kSessionServiceBaseUserDataKey,
std::make_unique<SessionServiceBaseUserData>(
GetDesiredBrowserTypeForWebContents()));
}
void SessionServiceBase::TabClosing(WebContents* contents) {
// Allow the associated sessionStorage to get deleted; it won't be needed
// in the session restore.
content::SessionStorageNamespace* session_storage_namespace =
contents->GetController().GetDefaultSessionStorageNamespace();
session_storage_namespace->SetShouldPersist(false);
sessions::SessionTabHelper* session_tab_helper =
sessions::SessionTabHelper::FromWebContents(contents);
TabClosed(session_tab_helper->window_id(), session_tab_helper->session_id());
}
void SessionServiceBase::TabRestored(WebContents* tab, bool pinned) {
if (!is_saving_enabled_)
return;
sessions::SessionTabHelper* session_tab_helper =
sessions::SessionTabHelper::FromWebContents(tab);
if (!ShouldTrackChangesToWindow(session_tab_helper->window_id()))
return;
BuildCommandsForTab(session_tab_helper->window_id(), tab, -1, absl::nullopt,
pinned, nullptr);
command_storage_manager()->StartSaveTimer();
}
void SessionServiceBase::SetSelectedTabInWindow(SessionID window_id,
int index) {
if (!ShouldTrackChangesToWindow(window_id))
return;
auto it = last_selected_tab_in_window_.find(window_id);
if (it != last_selected_tab_in_window_.end() && it->second == index)
return;
last_selected_tab_in_window_[window_id] = index;
ScheduleCommand(
sessions::CreateSetSelectedTabInWindowCommand(window_id, index));
}
void SessionServiceBase::SetTabExtensionAppID(
SessionID window_id,
SessionID tab_id,
const std::string& extension_app_id) {
if (!ShouldTrackChangesToWindow(window_id))
return;
ScheduleCommand(
sessions::CreateSetTabExtensionAppIDCommand(tab_id, extension_app_id));
}
void SessionServiceBase::SetLastActiveTime(SessionID window_id,
SessionID tab_id,
base::TimeTicks last_active_time) {
if (!ShouldTrackChangesToWindow(window_id))
return;
ScheduleCommand(
sessions::CreateLastActiveTimeCommand(tab_id, last_active_time));
}
void SessionServiceBase::GetLastSession(
sessions::GetLastSessionCallback callback) {
// OnGotSessionCommands maps the SessionCommands to browser state, then run
// the callback.
VLOG(1) << "SessionServiceBase::GetLastSession, getting session restore "
"commands.";
return command_storage_manager_->GetLastSessionCommands(
base::BindOnce(&SessionServiceBase::OnGotSessionCommands,
weak_factory_.GetWeakPtr(), std::move(callback)));
}
void SessionServiceBase::SetWindowAppName(SessionID window_id,
const std::string& app_name) {
if (!ShouldTrackChangesToWindow(window_id))
return;
ScheduleCommand(sessions::CreateSetWindowAppNameCommand(window_id, app_name));
}
void SessionServiceBase::SetPinnedState(SessionID window_id,
SessionID tab_id,
bool is_pinned) {
if (!ShouldTrackChangesToWindow(window_id))
return;
ScheduleCommand(sessions::CreatePinnedStateCommand(tab_id, is_pinned));
}
bool SessionServiceBase::ShouldUseDelayedSave() {
return should_use_delayed_save_;
}
void SessionServiceBase::OnWillSaveCommands() {
if (!is_saving_enabled_) {
// There should be no commands scheduled, otherwise data will be written,
// potentially clobbering the last file.
DCHECK(command_storage_manager_->pending_commands().empty());
return;
}
RebuildCommandsIfRequired();
did_save_commands_at_least_once_ |=
!command_storage_manager()->pending_commands().empty();
}
void SessionServiceBase::OnErrorWritingSessionCommands() {
LogSessionServiceWriteErrorEvent(profile_, false);
rebuild_on_next_save_ = true;
RebuildCommandsIfRequired();
}
void SessionServiceBase::SetTabUserAgentOverride(
SessionID window_id,
SessionID tab_id,
const sessions::SerializedUserAgentOverride& user_agent_override) {
// This is overridden by session_service implementation.
// We still need it here because we derive from
// sessions::SessionTabHelperDelegate.
NOTREACHED();
return;
}
void SessionServiceBase::SetSelectedNavigationIndex(SessionID window_id,
SessionID tab_id,
int index) {
if (!ShouldTrackChangesToWindow(window_id))
return;
if (tab_to_available_range_.find(tab_id) != tab_to_available_range_.end()) {
if (index < tab_to_available_range_[tab_id].first ||
index > tab_to_available_range_[tab_id].second) {
// The new index is outside the range of what we've archived, schedule
// a reset.
ResetFromCurrentBrowsers();
return;
}
}
ScheduleCommand(
sessions::CreateSetSelectedNavigationIndexCommand(tab_id, index));
}
void SessionServiceBase::UpdateTabNavigation(
SessionID window_id,
SessionID tab_id,
const SerializedNavigationEntry& navigation) {
if (!ShouldTrackURLForRestore(navigation.virtual_url()) ||
!ShouldTrackChangesToWindow(window_id)) {
return;
}
if (tab_to_available_range_.find(tab_id) != tab_to_available_range_.end()) {
std::pair<int, int>& range = tab_to_available_range_[tab_id];
range.first = std::min(navigation.index(), range.first);
range.second = std::max(navigation.index(), range.second);
}
ScheduleCommand(CreateUpdateTabNavigationCommand(tab_id, navigation));
}
void SessionServiceBase::TabNavigationPathPruned(SessionID window_id,
SessionID tab_id,
int index,
int count) {
if (!ShouldTrackChangesToWindow(window_id))
return;
DCHECK_GE(index, 0);
DCHECK_GT(count, 0);
// Update the range of available indices.
if (tab_to_available_range_.find(tab_id) != tab_to_available_range_.end()) {
std::pair<int, int>& range = tab_to_available_range_[tab_id];
// if both range.first and range.second are also deleted.
if (range.second >= index && range.second < index + count &&
range.first >= index && range.first < index + count) {
range.first = range.second = 0;
} else {
// Update range.first
if (range.first >= index + count)
range.first = range.first - count;
else if (range.first >= index && range.first < index + count)
range.first = index;
// Update range.second
if (range.second >= index + count)
range.second = std::max(range.first, range.second - count);
else if (range.second >= index && range.second < index + count)
range.second = std::max(range.first, index - 1);
}
}
return ScheduleCommand(
sessions::CreateTabNavigationPathPrunedCommand(tab_id, index, count));
}
void SessionServiceBase::TabNavigationPathEntriesDeleted(SessionID window_id,
SessionID tab_id) {
if (!ShouldTrackChangesToWindow(window_id))
return;
// Multiple tabs might be affected by this deletion, so the rebuild is
// delayed until next save.
rebuild_on_next_save_ = true;
command_storage_manager_->StartSaveTimer();
}
void SessionServiceBase::DestroyCommandStorageManager() {
command_storage_manager_.reset(nullptr);
}
void SessionServiceBase::RemoveUnusedRestoreWindows(
std::vector<std::unique_ptr<sessions::SessionWindow>>* window_list) {
auto i = window_list->begin();
while (i != window_list->end()) {
sessions::SessionWindow* window = i->get();
if (!ShouldRestoreWindowOfType(window->type)) {
i = window_list->erase(i);
} else {
++i;
}
}
}
void SessionServiceBase::OnBrowserSetLastActive(Browser* browser) {
if (ShouldTrackBrowser(browser))
ScheduleCommand(
sessions::CreateSetActiveWindowCommand(browser->session_id()));
}
void SessionServiceBase::OnGotSessionCommands(
sessions::GetLastSessionCallback callback,
std::vector<std::unique_ptr<sessions::SessionCommand>> commands,
bool read_error) {
std::vector<std::unique_ptr<sessions::SessionWindow>> valid_windows;
SessionID active_window_id = SessionID::InvalidValue();
sessions::RestoreSessionFromCommands(commands, &valid_windows,
&active_window_id);
RemoveUnusedRestoreWindows(&valid_windows);
std::move(callback).Run(std::move(valid_windows), active_window_id,
read_error);
}
void SessionServiceBase::BuildCommandsForTab(
SessionID window_id,
WebContents* tab,
int index_in_window,
absl::optional<tab_groups::TabGroupId> group,
bool is_pinned,
IdToRange* tab_to_available_range) {
DCHECK(tab);
DCHECK(window_id.is_valid());
sessions::SessionTabHelper* session_tab_helper =
sessions::SessionTabHelper::FromWebContents(tab);
const SessionID session_id(session_tab_helper->session_id());
command_storage_manager()->AppendRebuildCommand(
sessions::CreateSetTabWindowCommand(window_id, session_id));
const int current_index = tab->GetController().GetCurrentEntryIndex();
const int min_index =
std::max(current_index - sessions::gMaxPersistNavigationCount, 0);
const int max_index =
std::min(current_index + sessions::gMaxPersistNavigationCount,
tab->GetController().GetEntryCount());
const int pending_index = tab->GetController().GetPendingEntryIndex();
if (tab_to_available_range) {
(*tab_to_available_range)[session_id] =
std::pair<int, int>(min_index, max_index);
}
command_storage_manager()->AppendRebuildCommand(
sessions::CreateLastActiveTimeCommand(session_id,
tab->GetLastActiveTime()));
// TODO(stahon@microsoft.com) This might be movable to SessionService
// when Chrome OS uses AppSessionService for app restores.
// For now it needs to stay to support SessionService restoring apps.
std::string app_id = apps::GetAppIdForWebContents(tab);
if (!app_id.empty()) {
command_storage_manager()->AppendRebuildCommand(
sessions::CreateSetTabExtensionAppIDCommand(session_id, app_id));
}
for (int i = min_index; i < max_index; ++i) {
NavigationEntry* entry = (i == pending_index)
? tab->GetController().GetPendingEntry()
: tab->GetController().GetEntryAtIndex(i);
DCHECK(entry);
if (ShouldTrackURLForRestore(entry->GetVirtualURL()) &&
!entry->IsInitialEntry()) {
// Don't try to persist initial NavigationEntry, as it is not actually
// associated with any navigation and will just result in about:blank on
// session restore.
const SerializedNavigationEntry navigation =
ContentSerializedNavigationBuilder::FromNavigationEntry(i, entry);
command_storage_manager()->AppendRebuildCommand(
CreateUpdateTabNavigationCommand(session_id, navigation));
}
}
command_storage_manager()->AppendRebuildCommand(
sessions::CreateSetSelectedNavigationIndexCommand(session_id,
current_index));
if (index_in_window != -1) {
command_storage_manager()->AppendRebuildCommand(
sessions::CreateSetTabIndexInWindowCommand(session_id,
index_in_window));
}
if (is_pinned) {
command_storage_manager()->AppendRebuildCommand(
sessions::CreatePinnedStateCommand(session_id, true));
}
// Record the association between the sessionStorage namespace and the tab.
content::SessionStorageNamespace* session_storage_namespace =
tab->GetController().GetDefaultSessionStorageNamespace();
ScheduleCommand(sessions::CreateSessionStorageAssociatedCommand(
session_tab_helper->session_id(), session_storage_namespace->id()));
}
void SessionServiceBase::BuildCommandsForBrowser(
Browser* browser,
IdToRange* tab_to_available_range,
std::set<SessionID>* windows_to_track) {
DCHECK(is_saving_enabled_);
DCHECK(browser);
DCHECK(browser->session_id().is_valid());
command_storage_manager()->AppendRebuildCommand(
sessions::CreateSetWindowBoundsCommand(
browser->session_id(), browser->window()->GetRestoredBounds(),
browser->window()->GetRestoredState()));
command_storage_manager()->AppendRebuildCommand(
sessions::CreateSetWindowTypeCommand(
browser->session_id(), WindowTypeForBrowserType(browser->type())));
if (!browser->app_name().empty()) {
command_storage_manager()->AppendRebuildCommand(
sessions::CreateSetWindowAppNameCommand(browser->session_id(),
browser->app_name()));
}
if (!browser->user_title().empty()) {
command_storage_manager()->AppendRebuildCommand(
sessions::CreateSetWindowUserTitleCommand(browser->session_id(),
browser->user_title()));
}
command_storage_manager()->AppendRebuildCommand(
sessions::CreateSetWindowWorkspaceCommand(
browser->session_id(), browser->window()->GetWorkspace()));
command_storage_manager()->AppendRebuildCommand(
sessions::CreateSetWindowVisibleOnAllWorkspacesCommand(
browser->session_id(),
browser->window()->IsVisibleOnAllWorkspaces()));
command_storage_manager()->AppendRebuildCommand(
sessions::CreateSetSelectedTabInWindowCommand(
browser->session_id(), browser->tab_strip_model()->active_index()));
// Set the visual data for each tab group.
TabStripModel* tab_strip = browser->tab_strip_model();
if (tab_strip->SupportsTabGroups()) {
TabGroupModel* group_model = tab_strip->group_model();
const SavedTabGroupKeyedService* const saved_tab_group_keyed_service =
base::FeatureList::IsEnabled(features::kTabGroupsSave)
? SavedTabGroupServiceFactory::GetForProfile(browser->profile())
: nullptr;
for (const tab_groups::TabGroupId& group_id :
group_model->ListTabGroups()) {
const tab_groups::TabGroupVisualData* visual_data =
group_model->GetTabGroup(group_id)->visual_data();
absl::optional<std::string> saved_guid;
if (saved_tab_group_keyed_service) {
const SavedTabGroup* const saved_group =
saved_tab_group_keyed_service->model()->Get(group_id);
if (saved_group) {
saved_guid = saved_group->saved_guid().AsLowercaseString();
}
}
command_storage_manager()->AppendRebuildCommand(
sessions::CreateTabGroupMetadataUpdateCommand(group_id, visual_data,
std::move(saved_guid)));
}
}
for (int i = 0; i < tab_strip->count(); ++i) {
WebContents* tab = tab_strip->GetWebContentsAt(i);
DCHECK(tab);
const absl::optional<tab_groups::TabGroupId> group_id =
tab_strip->GetTabGroupForTab(i);
BuildCommandsForTab(browser->session_id(), tab, i, group_id,
tab_strip->IsTabPinned(i), tab_to_available_range);
}
windows_to_track->insert(browser->session_id());
}
void SessionServiceBase::BuildCommandsFromBrowsers(
IdToRange* tab_to_available_range,
std::set<SessionID>* windows_to_track) {
DCHECK(is_saving_enabled_);
for (auto* browser : *BrowserList::GetInstance()) {
// Make sure the browser has tabs and a window. Browser's destructor
// removes itself from the BrowserList. When a browser is closed the
// destructor is not necessarily run immediately. This means it's possible
// for us to get a handle to a browser that is about to be removed. If
// the tab count is 0 or the window is NULL, the browser is about to be
// deleted, so we ignore it.
if (ShouldTrackBrowser(browser) && browser->tab_strip_model()->count() &&
browser->window()) {
BuildCommandsForBrowser(browser, tab_to_available_range,
windows_to_track);
}
}
}
void SessionServiceBase::ScheduleCommand(
std::unique_ptr<sessions::SessionCommand> command) {
if (!is_saving_enabled_)
return;
DCHECK(command);
if (ReplacePendingCommand(command_storage_manager_.get(), &command))
return;
bool is_closing_command = IsClosingCommand(command.get());
command_storage_manager_->ScheduleCommand(std::move(command));
// Don't schedule a reset on tab closed/window closed. Otherwise we may
// lose tabs/windows we want to restore from if we exit right after this.
if (!command_storage_manager_->pending_reset() &&
command_storage_manager_->commands_since_reset() >= kWritesPerReset &&
!is_closing_command) {
ScheduleResetCommands();
}
DidScheduleCommand();
}
bool SessionServiceBase::ShouldTrackChangesToWindow(SessionID window_id) const {
return windows_tracking_.find(window_id) != windows_tracking_.end();
}
bool SessionServiceBase::ShouldTrackBrowser(Browser* browser) const {
if (browser->profile() != profile())
return false;
if (browser->omit_from_session_restore())
return false;
// Never track app popup windows that do not have a trusted source (i.e.
// popup windows spawned by an app). If this logic changes, be sure to also
// change SessionRestoreImpl::CreateRestoredBrowser().
if ((browser->is_type_app() || browser->is_type_app_popup()) &&
!browser->is_trusted_source()) {
return false;
}
return ShouldRestoreWindowOfType(WindowTypeForBrowserType(browser->type()));
}
sessions::CommandStorageManager*
SessionServiceBase::GetCommandStorageManagerForTest() {
return command_storage_manager_.get();
}
void SessionServiceBase::SetAvailableRangeForTest(
SessionID tab_id,
const std::pair<int, int>& range) {
tab_to_available_range_[tab_id] = range;
}
bool SessionServiceBase::GetAvailableRangeForTest(SessionID tab_id,
std::pair<int, int>* range) {
auto i = tab_to_available_range_.find(tab_id);
if (i == tab_to_available_range_.end())
return false;
*range = i->second;
return true;
}
void SessionServiceBase::SetSavingEnabled(bool enabled) {
if (is_saving_enabled_ == enabled)
return;
is_saving_enabled_ = enabled;
if (!is_saving_enabled_) {
// Transitioning from enabled to disabled should happen very early on,
// before any commands are actually written. If commands are written, then
// the purpose of disabling will have failed (because by writing some
// commands the previous session is going to be lost on exit).
DCHECK(!did_save_commands_at_least_once_);
command_storage_manager()->ClearPendingCommands();
} else {
ScheduleResetCommands();
}
}