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
|
// 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/app_session_service.h"
#include <algorithm>
#include <set>
#include <utility>
#include "build/build_config.h"
#include "chrome/browser/apps/app_service/launch_utils.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/defaults.h"
#include "chrome/browser/prefs/session_startup_pref.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sessions/session_common_utils.h"
#include "chrome/browser/sessions/session_data_deleter.h"
#include "chrome/browser/sessions/session_service_utils.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_window.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/session_storage_namespace.h"
#include "content/public/browser/web_contents.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/ash/crostini/crostini_util.h"
#endif
#if BUILDFLAG(IS_MAC)
#include "chrome/browser/app_controller_mac.h"
#endif
using content::NavigationEntry;
using content::WebContents;
using sessions::ContentSerializedNavigationBuilder;
using sessions::SerializedNavigationEntry;
AppSessionService::AppSessionService(Profile* profile)
: SessionServiceBase(profile,
SessionServiceBase::SessionServiceType::kAppRestore) {}
AppSessionService::~AppSessionService() {
// The BrowserList should outlive the SessionService since it's static and
// the SessionService is a KeyedService.
// BrowserList is subscribed to by SessionServiceBase's constructor.
BrowserList::RemoveObserver(this);
command_storage_manager()->Save();
DestroyCommandStorageManager();
}
void AppSessionService::TabClosed(SessionID window_id, SessionID tab_id) {
if (!tab_id.id())
return; // Happens when the tab is replaced.
if (!ShouldTrackChangesToWindow(window_id))
return;
auto i = tab_to_available_range()->find(tab_id);
if (i != tab_to_available_range()->end())
tab_to_available_range()->erase(i);
// If an individual tab is being closed or a secondary window is being
// closed, just mark the tab as closed now.
ScheduleCommand(sessions::CreateTabClosedCommand(tab_id));
}
void AppSessionService::WindowOpened(Browser* browser) {
if (!ShouldTrackBrowser(browser))
return;
SetWindowType(browser->session_id(), browser->type());
SetWindowAppName(browser->session_id(), browser->app_name());
// Save a browser workspace after window is created in `Browser()`.
// Bento desks restore feature in ash requires this line to restore correctly
// after creating a new browser window in a particular desk.
SetWindowWorkspace(browser->session_id(), browser->window()->GetWorkspace());
}
void AppSessionService::WindowClosing(SessionID window_id) {
if (!ShouldTrackChangesToWindow(window_id))
return;
// If Chrome is closed immediately after a history deletion, we have to
// rebuild commands before this window is closed, otherwise these tabs would
// be lost.
RebuildCommandsIfRequired();
}
void AppSessionService::WindowClosed(SessionID window_id) {
if (!ShouldTrackChangesToWindow(window_id)) {
return;
}
windows_tracking()->erase(window_id);
tab_to_available_range()->erase(window_id);
ScheduleCommand(sessions::CreateWindowClosedCommand(window_id));
}
void AppSessionService::SetWindowType(SessionID window_id, Browser::Type type) {
sessions::SessionWindow::WindowType window_type =
WindowTypeForBrowserType(type);
if (!ShouldRestoreWindowOfType(window_type))
return;
windows_tracking()->insert(window_id);
ScheduleCommand(CreateSetWindowTypeCommand(window_id, window_type));
}
Browser::Type AppSessionService::GetDesiredBrowserTypeForWebContents() {
return Browser::Type::TYPE_APP;
}
bool AppSessionService::ShouldRestoreWindowOfType(
sessions::SessionWindow::WindowType window_type) const {
if (window_type == sessions::SessionWindow::TYPE_APP ||
window_type == sessions::SessionWindow::TYPE_APP_POPUP) {
return true;
}
return false;
}
void AppSessionService::ScheduleResetCommands() {
command_storage_manager()->set_pending_reset(true);
command_storage_manager()->ClearPendingCommands();
tab_to_available_range()->clear();
windows_tracking()->clear();
set_rebuild_on_next_save(false);
BuildCommandsFromBrowsers(tab_to_available_range(), windows_tracking());
command_storage_manager()->StartSaveTimer();
}
void AppSessionService::RebuildCommandsIfRequired() {
if (rebuild_on_next_save())
ScheduleResetCommands();
}
|