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
|
// Copyright 2012 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.
#ifndef COMPONENTS_SESSIONS_CORE_TAB_RESTORE_SERVICE_H_
#define COMPONENTS_SESSIONS_CORE_TAB_RESTORE_SERVICE_H_
#include <list>
#include <string>
#include <vector>
#include "base/memory/ref_counted.h"
#include "base/time/time.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/sessions/core/serialized_navigation_entry.h"
#include "components/sessions/core/session_id.h"
#include "components/sessions/core/session_types.h"
#include "components/sessions/core/sessions_export.h"
#include "ui/base/window_open_disposition.h"
namespace sessions {
class LiveTab;
class PlatformSpecificTabData;
class LiveTabContext;
class TabRestoreServiceObserver;
// TabRestoreService is responsible for maintaining the most recently closed
// tabs and windows. When a tab is closed
// TabRestoreService::CreateHistoricalTab is invoked and a Tab is created to
// represent the tab. Similarly, when a browser is closed, BrowserClosing is
// invoked and a Window is created to represent the window.
//
// To restore a tab/window from the TabRestoreService invoke RestoreEntryById
// or RestoreMostRecentEntry.
//
// To listen for changes to the set of entries managed by the TabRestoreService
// add an observer.
class SESSIONS_EXPORT TabRestoreService : public KeyedService {
public:
// Interface used to allow the test to provide a custom time.
class SESSIONS_EXPORT TimeFactory {
public:
virtual ~TimeFactory();
virtual base::Time TimeNow() = 0;
};
// The type of entry.
enum Type {
TAB,
WINDOW
};
struct SESSIONS_EXPORT Entry {
virtual ~Entry();
// Unique id for this entry. The id is guaranteed to be unique for a
// session.
SessionID::id_type id;
// The type of the entry.
const Type type;
// The time when the window or tab was closed.
base::Time timestamp;
// Is this entry from the last session? This is set to true for entries that
// were closed during the last session, and false for entries that were
// closed during this session.
bool from_last_session = false;
protected:
explicit Entry(Type type);
private:
DISALLOW_COPY_AND_ASSIGN(Entry);
};
// Represents a previously open tab.
struct SESSIONS_EXPORT Tab : public Entry {
Tab();
~Tab() override;
// The navigations.
std::vector<SerializedNavigationEntry> navigations;
// Index of the selected navigation in navigations.
int current_navigation_index = -1;
// The ID of the browser to which this tab belonged, so it can be restored
// there. May be 0 (an invalid SessionID) when restoring an entire session.
SessionID::id_type browser_id = 0;
// Index within the tab strip. May be -1 for an unknown index.
int tabstrip_index = -1;
// True if the tab was pinned.
bool pinned = false;
// If non-empty gives the id of the extension for the tab.
std::string extension_app_id;
// The associated client data.
std::unique_ptr<PlatformSpecificTabData> platform_data;
// The user agent override used for the tab's navigations (if applicable).
std::string user_agent_override;
};
// Represents a previously open window.
struct SESSIONS_EXPORT Window : public Entry {
Window();
~Window() override;
// The tabs that comprised the window, in order.
std::vector<std::unique_ptr<Tab>> tabs;
// Index of the selected tab.
int selected_tab_index = -1;
// If an application window, the name of the app.
std::string app_name;
};
typedef std::list<std::unique_ptr<Entry>> Entries;
~TabRestoreService() override;
// Adds/removes an observer. TabRestoreService does not take ownership of
// the observer.
virtual void AddObserver(TabRestoreServiceObserver* observer) = 0;
virtual void RemoveObserver(TabRestoreServiceObserver* observer) = 0;
// Creates a Tab to represent |live_tab| and notifies observers the list of
// entries has changed.
virtual void CreateHistoricalTab(LiveTab* live_tab, int index) = 0;
// TODO(blundell): Rename and fix comment.
// Invoked when a browser is closing. If |context| is a tabbed browser with
// at least one tab, a Window is created, added to entries and observers are
// notified.
virtual void BrowserClosing(LiveTabContext* context) = 0;
// TODO(blundell): Rename and fix comment.
// Invoked when the browser is done closing.
virtual void BrowserClosed(LiveTabContext* context) = 0;
// Removes all entries from the list and notifies observers the list
// of tabs has changed.
virtual void ClearEntries() = 0;
// Returns the entries, ordered with most recently closed entries at the
// front.
virtual const Entries& entries() const = 0;
// Restores the most recently closed entry. Does nothing if there are no
// entries to restore. If the most recently restored entry is a tab, it is
// added to |context|. Returns the LiveTab instances of the restored tab(s).
virtual std::vector<LiveTab*> RestoreMostRecentEntry(
LiveTabContext* context) = 0;
// Removes the Tab with id |id| from the list and returns it.
virtual std::unique_ptr<Tab> RemoveTabEntryById(SessionID::id_type id) = 0;
// Restores an entry by id. If there is no entry with an id matching |id|,
// this does nothing. If |context| is NULL, this creates a new window for the
// entry. |disposition| is respected, but the attributes (tabstrip index,
// browser window) of the tab when it was closed will be respected if
// disposition is UNKNOWN. Returns the LiveTab instances of the restored
// tab(s).
virtual std::vector<LiveTab*> RestoreEntryById(
LiveTabContext* context,
SessionID::id_type id,
WindowOpenDisposition disposition) = 0;
// Loads the tabs and previous session. This does nothing if the tabs
// from the previous session have already been loaded.
virtual void LoadTabsFromLastSession() = 0;
// Returns true if the tab entries have been loaded.
virtual bool IsLoaded() const = 0;
// Deletes the last session.
virtual void DeleteLastSession() = 0;
// Returns true if we're in the process of restoring some entries.
virtual bool IsRestoring() const = 0;
};
// A class that is used to associate platform-specific data with
// TabRestoreService::Tab. See LiveTab::GetPlatformSpecificTabData().
class SESSIONS_EXPORT PlatformSpecificTabData {
public:
virtual ~PlatformSpecificTabData();
};
} // namespace sessions
#endif // COMPONENTS_SESSIONS_CORE_TAB_RESTORE_SERVICE_H_
|