1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_SESSIONS_CORE_TAB_RESTORE_SERVICE_HELPER_H_
6 #define COMPONENTS_SESSIONS_CORE_TAB_RESTORE_SERVICE_HELPER_H_
11 #include "base/basictypes.h"
12 #include "base/observer_list.h"
13 #include "base/time/time.h"
14 #include "components/sessions/core/tab_restore_service.h"
15 #include "components/sessions/session_id.h"
16 #include "components/sessions/session_types.h"
17 #include "components/sessions/sessions_export.h"
19 class TabRestoreService
;
20 class TabRestoreServiceDelegate
;
21 class TabRestoreServiceObserver
;
25 class TabRestoreServiceClient
;
28 // Helper class used to implement InMemoryTabRestoreService and
29 // PersistentTabRestoreService. See tab_restore_service.h for method-level
31 class SESSIONS_EXPORT TabRestoreServiceHelper
{
33 typedef TabRestoreService::Entries Entries
;
34 typedef TabRestoreService::Entry Entry
;
35 typedef TabRestoreService::Tab Tab
;
36 typedef TabRestoreService::TimeFactory TimeFactory
;
37 typedef TabRestoreService::Window Window
;
39 // Provides a way for the client to add behavior to the tab restore service
40 // helper (e.g. implementing tabs persistence).
43 // Invoked before the entries are cleared.
44 virtual void OnClearEntries();
46 // Invoked before the entry is restored. |entry_iterator| points to the
47 // entry corresponding to the session identified by |id|.
48 virtual void OnRestoreEntryById(SessionID::id_type id
,
49 Entries::const_iterator entry_iterator
);
51 // Invoked after an entry was added.
52 virtual void OnAddEntry();
59 // Max number of entries we'll keep around.
63 // Creates a new TabRestoreServiceHelper and provides an object that provides
64 // the current time. The TabRestoreServiceHelper does not take ownership of
65 // |time_factory| and |observer|. Note that |observer| can also be NULL.
66 TabRestoreServiceHelper(TabRestoreService
* tab_restore_service
,
68 sessions::TabRestoreServiceClient
* client
,
69 TimeFactory
* time_factory
);
71 ~TabRestoreServiceHelper();
73 // Helper methods used to implement TabRestoreService.
74 void AddObserver(TabRestoreServiceObserver
* observer
);
75 void RemoveObserver(TabRestoreServiceObserver
* observer
);
76 void CreateHistoricalTab(sessions::LiveTab
* live_tab
, int index
);
77 void BrowserClosing(TabRestoreServiceDelegate
* delegate
);
78 void BrowserClosed(TabRestoreServiceDelegate
* delegate
);
80 const Entries
& entries() const;
81 std::vector
<sessions::LiveTab
*> RestoreMostRecentEntry(
82 TabRestoreServiceDelegate
* delegate
,
83 int host_desktop_type
);
84 Tab
* RemoveTabEntryById(SessionID::id_type id
);
85 std::vector
<sessions::LiveTab
*> RestoreEntryById(
86 TabRestoreServiceDelegate
* delegate
,
87 SessionID::id_type id
,
88 int host_desktop_type
,
89 WindowOpenDisposition disposition
);
91 // Notifies observers the tabs have changed.
92 void NotifyTabsChanged();
94 // Notifies observers the service has loaded.
97 // Adds |entry| to the list of entries and takes ownership. If |prune| is true
98 // |PruneAndNotify| is invoked. If |to_front| is true the entry is added to
99 // the front, otherwise the back. Normal closes go to the front, but
100 // tab/window closes from the previous session are added to the back.
101 void AddEntry(Entry
* entry
, bool prune
, bool to_front
);
103 // Prunes |entries_| to contain only kMaxEntries, and removes uninteresting
107 // Returns an iterator into |entries_| whose id matches |id|. If |id|
108 // identifies a Window, then its iterator position will be returned. If it
109 // identifies a tab, then the iterator position of the Window in which the Tab
110 // resides is returned.
111 Entries::iterator
GetEntryIteratorById(SessionID::id_type id
);
113 // Calls either ValidateTab or ValidateWindow as appropriate.
114 static bool ValidateEntry(Entry
* entry
);
117 friend class PersistentTabRestoreService
;
119 // Populates the tab's navigations from the LiveTab, and its browser_id and
120 // pinned state from the delegate.
121 void PopulateTab(Tab
* tab
,
123 TabRestoreServiceDelegate
* delegate
,
124 sessions::LiveTab
* live_tab
);
126 // This is a helper function for RestoreEntryById() for restoring a single
127 // tab. If |delegate| is NULL, this creates a new window for the entry. This
128 // returns the TabRestoreServiceDelegate into which the tab was restored.
129 // |disposition| will be respected, but if it is UNKNOWN then the tab's
130 // original attributes will be respected instead. If a new
131 // TabRestoreServiceDelegate needs to be created for this tab,
132 // |host_desktop_type| will be passed to
133 // TabRestoreServiceClient::CreateTabRestoreServiceDelegate(). If present,
134 // |live_tab| will be populated with the LiveTab of the restored tab.
135 TabRestoreServiceDelegate
* RestoreTab(const Tab
& tab
,
136 TabRestoreServiceDelegate
* delegate
,
137 int host_desktop_type
,
138 WindowOpenDisposition disposition
,
139 sessions::LiveTab
** live_tab
);
141 // Returns true if |tab| has more than one navigation. If |tab| has more
142 // than one navigation |tab->current_navigation_index| is constrained based
143 // on the number of navigations.
144 static bool ValidateTab(Tab
* tab
);
146 // Validates all the tabs in a window, plus the window's active tab index.
147 static bool ValidateWindow(Window
* window
);
149 // Returns true if |tab| is one we care about restoring.
150 bool IsTabInteresting(const Tab
* tab
);
152 // Checks whether |window| is interesting --- if it only contains a single,
153 // uninteresting tab, it's not interesting.
154 bool IsWindowInteresting(const Window
* window
);
156 // Validates and checks |entry| for interesting.
157 bool FilterEntry(Entry
* entry
);
159 // Finds tab entries with the old browser_id and sets it to the new one.
160 void UpdateTabBrowserIDs(SessionID::id_type old_id
,
161 SessionID::id_type new_id
);
163 // Gets the current time. This uses the time_factory_ if there is one.
164 base::Time
TimeNow() const;
166 TabRestoreService
* const tab_restore_service_
;
168 Observer
* const observer_
;
170 sessions::TabRestoreServiceClient
* client_
;
172 // Set of entries. They are ordered from most to least recent.
175 // Are we restoring a tab? If this is true we ignore requests to create a
179 base::ObserverList
<TabRestoreServiceObserver
> observer_list_
;
181 // Set of delegates that we've received a BrowserClosing method for but no
182 // corresponding BrowserClosed. We cache the set of delegates closing to
183 // avoid creating historical tabs for them.
184 std::set
<TabRestoreServiceDelegate
*> closing_delegates_
;
186 TimeFactory
* const time_factory_
;
188 DISALLOW_COPY_AND_ASSIGN(TabRestoreServiceHelper
);
191 #endif // COMPONENTS_SESSIONS_CORE_TAB_RESTORE_SERVICE_HELPER_H_