chromium/chrome/browser/sessions/session_service_base.cc

// 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/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_utils.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/session_storage_namespace.h"

#if BUILDFLAG(IS_MAC)
#include "chrome/browser/app_controller_mac.h"
#endif

#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/ui/web_applications/app_browser_controller.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#endif  // BUILDFLAG(IS_CHROMEOS)

Time;
NavigationEntry;
WebContents;
ContentSerializedNavigationBuilder;
SerializedNavigationEntry;

namespace {

// Every kWritesPerReset commands triggers recreating the file.
const int kWritesPerReset =;

// User data key for WebContents to derive their types.
const void* const kSessionServiceBaseUserDataKey =;

// User data key for BrowserContextData.
const void* const kProfileTaskRunnerKey =;
const void* const 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 {};

// SessionServiceBaseUserData
// -------------------------------------------------------------
class SessionServiceBaseUserData : public base::SupportsUserData::Data {};

}  // namespace

// SessionServiceBase
// -------------------------------------------------------------
// Note: This is protected as it is only intended to be called by child classes
SessionServiceBase::SessionServiceBase(Profile* profile,
                                       SessionServiceType type)
    :{}

SessionServiceBase::~SessionServiceBase() {}

// static
Browser::Type SessionServiceBase::GetBrowserTypeFromWebContents(
    content::WebContents* web_contents) {}

void SessionServiceBase::SetWindowVisibleOnAllWorkspaces(
    SessionID window_id,
    bool visible_on_all_workspaces) {}

void SessionServiceBase::ResetFromCurrentBrowsers() {}

void SessionServiceBase::SetTabWindow(SessionID window_id, SessionID tab_id) {}

void SessionServiceBase::SetWindowBounds(SessionID window_id,
                                         const gfx::Rect& bounds,
                                         ui::WindowShowState show_state) {}

void SessionServiceBase::SetWindowWorkspace(SessionID window_id,
                                            const std::string& workspace) {}

void SessionServiceBase::SetTabIndexInWindow(SessionID window_id,
                                             SessionID tab_id,
                                             int new_index) {}

void SessionServiceBase::TabInserted(WebContents* contents) {}

void SessionServiceBase::TabClosing(WebContents* contents) {}

void SessionServiceBase::TabRestored(WebContents* tab, bool pinned) {}

void SessionServiceBase::SetSelectedTabInWindow(SessionID window_id,
                                                int index) {}

void SessionServiceBase::SetTabExtensionAppID(
    SessionID window_id,
    SessionID tab_id,
    const std::string& extension_app_id) {}

void SessionServiceBase::SetLastActiveTime(SessionID window_id,
                                           SessionID tab_id,
                                           base::Time last_active_time) {}

void SessionServiceBase::GetLastSession(
    sessions::GetLastSessionCallback callback) {}

void SessionServiceBase::SetWindowAppName(SessionID window_id,
                                          const std::string& app_name) {}

void SessionServiceBase::SetPinnedState(SessionID window_id,
                                        SessionID tab_id,
                                        bool is_pinned) {}

bool SessionServiceBase::ShouldUseDelayedSave() {}

void SessionServiceBase::OnWillSaveCommands() {}

void SessionServiceBase::OnErrorWritingSessionCommands() {}

void SessionServiceBase::SetTabUserAgentOverride(
    SessionID window_id,
    SessionID tab_id,
    const sessions::SerializedUserAgentOverride& user_agent_override) {}

void SessionServiceBase::SetSelectedNavigationIndex(SessionID window_id,
                                                    SessionID tab_id,
                                                    int index) {}

void SessionServiceBase::UpdateTabNavigation(
    SessionID window_id,
    SessionID tab_id,
    const SerializedNavigationEntry& navigation) {}

void SessionServiceBase::TabNavigationPathPruned(SessionID window_id,
                                                 SessionID tab_id,
                                                 int index,
                                                 int count) {}

void SessionServiceBase::TabNavigationPathEntriesDeleted(SessionID window_id,
                                                         SessionID tab_id) {}

void SessionServiceBase::DestroyCommandStorageManager() {}

void SessionServiceBase::RemoveUnusedRestoreWindows(
    std::vector<std::unique_ptr<sessions::SessionWindow>>* window_list) {}

void SessionServiceBase::OnBrowserSetLastActive(Browser* browser) {}

void SessionServiceBase::OnGotSessionCommands(
    sessions::GetLastSessionCallback callback,
    std::vector<std::unique_ptr<sessions::SessionCommand>> commands,
    bool read_error) {}

void SessionServiceBase::BuildCommandsForTab(
    SessionID window_id,
    WebContents* tab,
    int index_in_window,
    std::optional<tab_groups::TabGroupId> group,
    bool is_pinned,
    IdToRange* tab_to_available_range) {}

void SessionServiceBase::BuildCommandsForBrowser(
    Browser* browser,
    IdToRange* tab_to_available_range,
    std::set<SessionID>* windows_to_track) {}

void SessionServiceBase::BuildCommandsFromBrowsers(
    IdToRange* tab_to_available_range,
    std::set<SessionID>* windows_to_track) {}

void SessionServiceBase::ScheduleCommand(
    std::unique_ptr<sessions::SessionCommand> command) {}

bool SessionServiceBase::ShouldTrackChangesToWindow(SessionID window_id) const {}

bool SessionServiceBase::ShouldTrackBrowser(Browser* browser) const {}

sessions::CommandStorageManager*
SessionServiceBase::GetCommandStorageManagerForTest() {}

void SessionServiceBase::SetAvailableRangeForTest(
    SessionID tab_id,
    const std::pair<int, int>& range) {}

bool SessionServiceBase::GetAvailableRangeForTest(SessionID tab_id,
                                                  std::pair<int, int>* range) {}

void SessionServiceBase::SetSavingEnabled(bool enabled) {}