chromium/components/sessions/core/session_service_commands.cc

// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/sessions/core/session_service_commands.h"

#include <stdint.h>
#include <string.h>

#include <map>
#include <tuple>
#include <utility>
#include <vector>

#include "base/containers/flat_set.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/pickle.h"
#include "base/token.h"
#include "base/uuid.h"
#include "base/values.h"
#include "components/sessions/core/base_session_service_commands.h"
#include "components/tab_groups/tab_group_color.h"

namespace sessions {

// Identifier for commands written to file.
static const SessionCommand::id_type kCommandSetTabWindow =;
// OBSOLETE Superseded by kCommandSetWindowBounds3.
// static const SessionCommand::id_type kCommandSetWindowBounds = 1;
static const SessionCommand::id_type kCommandSetTabIndexInWindow =;

// OBSOLETE: Preserved for backward compatibility. Using
// kCommandTabNavigationPathPruned instead
static const SessionCommand::id_type
    kCommandTabNavigationPathPrunedFromBack =;

static const SessionCommand::id_type kCommandUpdateTabNavigation =;
static const SessionCommand::id_type kCommandSetSelectedNavigationIndex =;
static const SessionCommand::id_type kCommandSetSelectedTabInIndex =;
static const SessionCommand::id_type kCommandSetWindowType =;
// OBSOLETE Superseded by kCommandSetWindowBounds3. Except for data migration.
// static const SessionCommand::id_type kCommandSetWindowBounds2 = 10;

// OBSOLETE: Preserved for backward compatibility. Using
// kCommandTabNavigationPathPruned instead
static const SessionCommand::id_type
    kCommandTabNavigationPathPrunedFromFront =;

static const SessionCommand::id_type kCommandSetPinnedState =;
static const SessionCommand::id_type kCommandSetExtensionAppID =;
static const SessionCommand::id_type kCommandSetWindowBounds3 =;
static const SessionCommand::id_type kCommandSetWindowAppName =;
static const SessionCommand::id_type kCommandTabClosed =;
static const SessionCommand::id_type kCommandWindowClosed =;
// OBSOLETE: Superseded by kCommandSetTabUserAgentOverride2.
static const SessionCommand::id_type kCommandSetTabUserAgentOverride =;
static const SessionCommand::id_type kCommandSessionStorageAssociated =;
static const SessionCommand::id_type kCommandSetActiveWindow =;
static const SessionCommand::id_type kCommandLastActiveTime =;
// OBSOLETE Superseded by kCommandSetWindowWorkspace2.
// static const SessionCommand::id_type kCommandSetWindowWorkspace = 22;
static const SessionCommand::id_type kCommandSetWindowWorkspace2 =;
static const SessionCommand::id_type kCommandTabNavigationPathPruned =;
static const SessionCommand::id_type kCommandSetTabGroup =;
// OBSOLETE Superseded by kCommandSetTabGroupMetadata2.
// static const SessionCommand::id_type kCommandSetTabGroupMetadata = 26;
static const SessionCommand::id_type kCommandSetTabGroupMetadata2 =;
static const SessionCommand::id_type kCommandSetTabGuid =;
static const SessionCommand::id_type kCommandSetTabUserAgentOverride2 =;
static const SessionCommand::id_type kCommandSetTabData =;
static const SessionCommand::id_type kCommandSetWindowUserTitle =;
static const SessionCommand::id_type kCommandSetWindowVisibleOnAllWorkspaces =;
static const SessionCommand::id_type kCommandAddTabExtraData =;
static const SessionCommand::id_type kCommandAddWindowExtraData =;
// ID 255 is used by CommandStorageBackend.

namespace {

// Various payload structures.
struct ClosedPayload {};

struct WindowBoundsPayload2 {};

struct WindowBoundsPayload3 {};

ActiveWindowPayload;

struct IDAndIndexPayload {};

TabIndexInWindowPayload;

TabNavigationPathPrunedFromBackPayload;

SelectedNavigationIndexPayload;

SelectedTabInIndexPayload;

WindowTypePayload;

TabNavigationPathPrunedFromFrontPayload;

struct TabNavigationPathPrunedPayload {};

struct SerializedToken {};

struct TabGroupPayload {};

struct PinnedStatePayload {};

struct LastActiveTimePayload {};

struct VisibleOnAllWorkspacesPayload {};

// Persisted versions of ui::WindowShowState that are written to disk and can
// never change.
enum PersistedWindowShowState {};

// TODO(crbug.com/40946710): Remove this around December 2024. This is part of a
// workaround added to support the transition from storing the last_active_time
// as TimeTicks to Time that was added in December 2023. This is the threshold
// at which we consider that if a tab is so far in the past, it must be a tab
// serialized with TimeTicks and not Time.
const base::TimeDelta kLastActiveWorkaroundThreshold =;

// Assert to ensure PersistedWindowShowState is updated if ui::WindowShowState
// is changed.
static_assert;
// Returns the show state to store to disk based |state|.
PersistedWindowShowState ShowStateToPersistedShowState(
    ui::WindowShowState state) {}

// Lints show state values when read back from persited disk.
ui::WindowShowState PersistedShowStateToShowState(int state) {}

// Iterates through the vector updating the selected_tab_index of each
// SessionWindow based on the actual tabs that were restored.
void UpdateSelectedTabIndex(
    std::vector<std::unique_ptr<SessionWindow>>* windows) {}

IdToSessionTab;
IdToSessionWindow;
GroupIdToSessionTabGroup;

// Returns the window in windows with the specified id. If a window does
// not exist, one is created.
SessionWindow* GetWindow(SessionID window_id, IdToSessionWindow* windows) {}

// Returns the tab with the specified id in tabs. If a tab does not exist,
// it is created.
SessionTab* GetTab(SessionID tab_id, IdToSessionTab* tabs) {}

SessionTabGroup* GetTabGroup(tab_groups::TabGroupId group_id,
                             GroupIdToSessionTabGroup* groups) {}

// Returns an iterator into navigations pointing to the navigation whose
// index matches |index|. If no navigation index matches |index|, the first
// navigation with an index > |index| is returned.
//
// This assumes the navigations are ordered by index in ascending order.
std::vector<sessions::SerializedNavigationEntry>::iterator
  FindClosestNavigationWithIndex(
    std::vector<sessions::SerializedNavigationEntry>* navigations,
    int index) {}

// Function used in sorting windows. Sorting is done based on window id. As
// window ids increment for each new window, this effectively sorts by creation
// time.
static bool WindowOrderSortFunction(const std::unique_ptr<SessionWindow>& w1,
                                    const std::unique_ptr<SessionWindow>& w2) {}

// Compares the two tabs based on visual index.
static bool TabVisualIndexSortFunction(const std::unique_ptr<SessionTab>& t1,
                                       const std::unique_ptr<SessionTab>& t2) {}

// Does the following:
// . Deletes and removes any windows with no tabs. NOTE: constrained windows
//   that have been dragged out are of type browser. As such, this preserves any
//   dragged out constrained windows (aka popups that have been dragged out).
// . Sorts the tabs in windows with valid tabs based on the tabs;
//   visual order, and adds the valid windows to |valid_windows|.
void SortTabsBasedOnVisualOrderAndClear(
    IdToSessionWindow* windows,
    std::vector<std::unique_ptr<SessionWindow>>* valid_windows) {}

// Adds tabs to their parent window based on the tab's window_id. This
// ignores tabs with no navigations.
void AddTabsToWindows(IdToSessionTab* tabs,
                      GroupIdToSessionTabGroup* tab_groups,
                      IdToSessionWindow* windows) {}

void ProcessTabNavigationPathPrunedCommand(
    TabNavigationPathPrunedPayload& payload,
    SessionTab* tab) {}

// Creates tabs and windows from the commands specified in |data|. The created
// tabs and windows are added to |tabs| and |windows| respectively, with the
// id of the active window set in |active_window_id|. It is up to the caller
// to delete the tabs and windows added to |tabs| and |windows|.
//
// This does NOT add any created SessionTabs to SessionWindow.tabs, that is
// done by AddTabsToWindows.
void CreateTabsAndWindows(
    const std::vector<std::unique_ptr<SessionCommand>>& data,
    IdToSessionTab* tabs,
    GroupIdToSessionTabGroup* tab_groups,
    IdToSessionWindow* windows,
    SessionID* active_window_id) {}

template <typename Payload>
std::unique_ptr<SessionCommand> CreateSessionCommandForPayload(
    SessionCommand::id_type id,
    const Payload& payload) {}

}  // namespace

std::unique_ptr<SessionCommand> CreateSetSelectedTabInWindowCommand(
    SessionID window_id,
    int index) {}

std::unique_ptr<SessionCommand> CreateSetTabWindowCommand(SessionID window_id,
                                                          SessionID tab_id) {}

std::unique_ptr<SessionCommand> CreateSetWindowBoundsCommand(
    SessionID window_id,
    const gfx::Rect& bounds,
    ui::WindowShowState show_state) {}

std::unique_ptr<SessionCommand> CreateSetTabIndexInWindowCommand(
    SessionID tab_id,
    int new_index) {}

std::unique_ptr<SessionCommand> CreateTabClosedCommand(const SessionID tab_id) {}

std::unique_ptr<SessionCommand> CreateWindowClosedCommand(
    const SessionID window_id) {}

std::unique_ptr<SessionCommand> CreateSetSelectedNavigationIndexCommand(
    SessionID tab_id,
    int index) {}

std::unique_ptr<SessionCommand> CreateSetWindowTypeCommand(
    SessionID window_id,
    SessionWindow::WindowType type) {}

std::unique_ptr<SessionCommand> CreateTabGroupCommand(
    SessionID tab_id,
    std::optional<tab_groups::TabGroupId> group) {}

std::unique_ptr<SessionCommand> CreateTabGroupMetadataUpdateCommand(
    const tab_groups::TabGroupId group,
    const tab_groups::TabGroupVisualData* visual_data,
    const std::optional<std::string> saved_guid) {}

std::unique_ptr<SessionCommand> CreatePinnedStateCommand(SessionID tab_id,
                                                         bool is_pinned) {}

std::unique_ptr<SessionCommand> CreateSessionStorageAssociatedCommand(
    SessionID tab_id,
    const std::string& session_storage_persistent_id) {}

std::unique_ptr<SessionCommand> CreateSetActiveWindowCommand(
    SessionID window_id) {}

std::unique_ptr<SessionCommand> CreateLastActiveTimeCommand(
    SessionID tab_id,
    base::Time last_active_time) {}

std::unique_ptr<SessionCommand> CreateSetWindowWorkspaceCommand(
    SessionID window_id,
    const std::string& workspace) {}

std::unique_ptr<SessionCommand> CreateSetWindowVisibleOnAllWorkspacesCommand(
    SessionID window_id,
    bool visible_on_all_workspaces) {}

std::unique_ptr<SessionCommand>
CreateTabNavigationPathPrunedCommand(SessionID tab_id, int index, int count) {}

std::unique_ptr<SessionCommand> CreateUpdateTabNavigationCommand(
    SessionID tab_id,
    const sessions::SerializedNavigationEntry& navigation) {}

std::unique_ptr<SessionCommand> CreateSetTabExtensionAppIDCommand(
    SessionID tab_id,
    const std::string& extension_id) {}

std::unique_ptr<SessionCommand> CreateSetTabUserAgentOverrideCommand(
    SessionID tab_id,
    const SerializedUserAgentOverride& user_agent_override) {}

std::unique_ptr<SessionCommand> CreateSetWindowAppNameCommand(
    SessionID window_id,
    const std::string& app_name) {}

std::unique_ptr<SessionCommand> CreateSetWindowUserTitleCommand(
    SessionID window_id,
    const std::string& user_title) {}

std::unique_ptr<SessionCommand> CreateSetTabGuidCommand(
    SessionID tab_id,
    const std::string& guid) {}

std::unique_ptr<SessionCommand> CreateSetTabDataCommand(
    SessionID tab_id,
    const std::map<std::string, std::string>& data) {}

std::unique_ptr<SessionCommand> CreateAddTabExtraDataCommand(
    SessionID tab_id,
    const std::string& key,
    const std::string& data) {}

std::unique_ptr<SessionCommand> CreateAddWindowExtraDataCommand(
    SessionID window_id,
    const std::string& key,
    const std::string& data) {}

bool ReplacePendingCommand(CommandStorageManager* command_storage_manager,
                           std::unique_ptr<SessionCommand>* command) {}

bool IsClosingCommand(SessionCommand* command) {}

void RestoreSessionFromCommands(
    const std::vector<std::unique_ptr<SessionCommand>>& commands,
    std::vector<std::unique_ptr<SessionWindow>>* valid_windows,
    SessionID* active_window_id) {}

}  // namespace sessions