Linux: Depend on liberation-fonts package for RPMs.
[chromium-blink-merge.git] / components / sessions / core / tab_restore_service_helper.h
blobc530645d7d9303f1499ac1f4f9df76012ef7ac20
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_
8 #include <set>
9 #include <vector>
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 namespace sessions {
21 class TabRestoreService;
22 class TabRestoreServiceClient;
23 class TabRestoreServiceDelegate;
24 class TabRestoreServiceObserver;
25 class TimeFactory;
27 // Helper class used to implement InMemoryTabRestoreService and
28 // PersistentTabRestoreService. See tab_restore_service.h for method-level
29 // comments.
30 class SESSIONS_EXPORT TabRestoreServiceHelper {
31 public:
32 typedef TabRestoreService::Entries Entries;
33 typedef TabRestoreService::Entry Entry;
34 typedef TabRestoreService::Tab Tab;
35 typedef TabRestoreService::TimeFactory TimeFactory;
36 typedef TabRestoreService::Window Window;
38 // Provides a way for the client to add behavior to the tab restore service
39 // helper (e.g. implementing tabs persistence).
40 class Observer {
41 public:
42 // Invoked before the entries are cleared.
43 virtual void OnClearEntries();
45 // Invoked before the entry is restored. |entry_iterator| points to the
46 // entry corresponding to the session identified by |id|.
47 virtual void OnRestoreEntryById(SessionID::id_type id,
48 Entries::const_iterator entry_iterator);
50 // Invoked after an entry was added.
51 virtual void OnAddEntry();
53 protected:
54 virtual ~Observer();
57 enum {
58 // Max number of entries we'll keep around.
59 kMaxEntries = 25,
62 // Creates a new TabRestoreServiceHelper and provides an object that provides
63 // the current time. The TabRestoreServiceHelper does not take ownership of
64 // |time_factory| and |observer|. Note that |observer| can also be NULL.
65 TabRestoreServiceHelper(TabRestoreService* tab_restore_service,
66 Observer* observer,
67 TabRestoreServiceClient* client,
68 TimeFactory* time_factory);
70 ~TabRestoreServiceHelper();
72 // Helper methods used to implement TabRestoreService.
73 void AddObserver(TabRestoreServiceObserver* observer);
74 void RemoveObserver(TabRestoreServiceObserver* observer);
75 void CreateHistoricalTab(LiveTab* live_tab, int index);
76 void BrowserClosing(TabRestoreServiceDelegate* delegate);
77 void BrowserClosed(TabRestoreServiceDelegate* delegate);
78 void ClearEntries();
79 const Entries& entries() const;
80 std::vector<LiveTab*> RestoreMostRecentEntry(
81 TabRestoreServiceDelegate* delegate,
82 int host_desktop_type);
83 Tab* RemoveTabEntryById(SessionID::id_type id);
84 std::vector<LiveTab*> RestoreEntryById(TabRestoreServiceDelegate* delegate,
85 SessionID::id_type id,
86 int host_desktop_type,
87 WindowOpenDisposition disposition);
89 // Notifies observers the tabs have changed.
90 void NotifyTabsChanged();
92 // Notifies observers the service has loaded.
93 void NotifyLoaded();
95 // Adds |entry| to the list of entries and takes ownership. If |prune| is true
96 // |PruneAndNotify| is invoked. If |to_front| is true the entry is added to
97 // the front, otherwise the back. Normal closes go to the front, but
98 // tab/window closes from the previous session are added to the back.
99 void AddEntry(Entry* entry, bool prune, bool to_front);
101 // Prunes |entries_| to contain only kMaxEntries, and removes uninteresting
102 // entries.
103 void PruneEntries();
105 // Returns an iterator into |entries_| whose id matches |id|. If |id|
106 // identifies a Window, then its iterator position will be returned. If it
107 // identifies a tab, then the iterator position of the Window in which the Tab
108 // resides is returned.
109 Entries::iterator GetEntryIteratorById(SessionID::id_type id);
111 // Calls either ValidateTab or ValidateWindow as appropriate.
112 static bool ValidateEntry(Entry* entry);
114 private:
115 friend class PersistentTabRestoreService;
117 // Populates the tab's navigations from the LiveTab, and its browser_id and
118 // pinned state from the delegate.
119 void PopulateTab(Tab* tab,
120 int index,
121 TabRestoreServiceDelegate* delegate,
122 LiveTab* live_tab);
124 // This is a helper function for RestoreEntryById() for restoring a single
125 // tab. If |delegate| is NULL, this creates a new window for the entry. This
126 // returns the TabRestoreServiceDelegate into which the tab was restored.
127 // |disposition| will be respected, but if it is UNKNOWN then the tab's
128 // original attributes will be respected instead. If a new
129 // TabRestoreServiceDelegate needs to be created for this tab,
130 // |host_desktop_type| will be passed to
131 // TabRestoreServiceClient::CreateTabRestoreServiceDelegate(). If present,
132 // |live_tab| will be populated with the LiveTab of the restored tab.
133 TabRestoreServiceDelegate* RestoreTab(const Tab& tab,
134 TabRestoreServiceDelegate* delegate,
135 int host_desktop_type,
136 WindowOpenDisposition disposition,
137 LiveTab** live_tab);
139 // Returns true if |tab| has more than one navigation. If |tab| has more
140 // than one navigation |tab->current_navigation_index| is constrained based
141 // on the number of navigations.
142 static bool ValidateTab(Tab* tab);
144 // Validates all the tabs in a window, plus the window's active tab index.
145 static bool ValidateWindow(Window* window);
147 // Returns true if |tab| is one we care about restoring.
148 bool IsTabInteresting(const Tab* tab);
150 // Checks whether |window| is interesting --- if it only contains a single,
151 // uninteresting tab, it's not interesting.
152 bool IsWindowInteresting(const Window* window);
154 // Validates and checks |entry| for interesting.
155 bool FilterEntry(Entry* entry);
157 // Finds tab entries with the old browser_id and sets it to the new one.
158 void UpdateTabBrowserIDs(SessionID::id_type old_id,
159 SessionID::id_type new_id);
161 // Gets the current time. This uses the time_factory_ if there is one.
162 base::Time TimeNow() const;
164 TabRestoreService* const tab_restore_service_;
166 Observer* const observer_;
168 TabRestoreServiceClient* client_;
170 // Set of entries. They are ordered from most to least recent.
171 Entries entries_;
173 // Are we restoring a tab? If this is true we ignore requests to create a
174 // historical tab.
175 bool restoring_;
177 base::ObserverList<TabRestoreServiceObserver> observer_list_;
179 // Set of delegates that we've received a BrowserClosing method for but no
180 // corresponding BrowserClosed. We cache the set of delegates closing to
181 // avoid creating historical tabs for them.
182 std::set<TabRestoreServiceDelegate*> closing_delegates_;
184 TimeFactory* const time_factory_;
186 DISALLOW_COPY_AND_ASSIGN(TabRestoreServiceHelper);
189 } // namespace sessions
191 #endif // COMPONENTS_SESSIONS_CORE_TAB_RESTORE_SERVICE_HELPER_H_