Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: chrome/browser/sessions/in_memory_tab_restore_service.h

Issue 10989027: Split TabRestoreService into InMemoryTRS and PersistentTRS (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Split TRS into InMemoryTRS and PersistentTRS Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 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.
4
5 #ifndef CHROME_BROWSER_SESSIONS_IN_MEMORY_TAB_RESTORE_SERVICE_H_
6 #define CHROME_BROWSER_SESSIONS_IN_MEMORY_TAB_RESTORE_SERVICE_H_
7
8 #include <map>
9 #include <set>
10
11 #include "base/gtest_prod_util.h"
12 #include "base/observer_list.h"
13 #include "base/time.h"
14 #include "chrome/browser/profiles/profile_keyed_service.h"
15 #include "chrome/browser/sessions/base_session_service.h"
16 #include "chrome/browser/sessions/session_id.h"
17 #include "chrome/browser/sessions/session_types.h"
18 #include "chrome/browser/sessions/tab_restore_service.h"
19
20 class Profile;
21 class TabRestoreServiceDelegate;
22 class TabRestoreServiceObserver;
23 struct SessionWindow;
24
25 namespace content {
26 class NavigationController;
27 class SessionStorageNamespace;
28 class WebContents;
29 }
30
31 // TODO
32 class InMemoryTabRestoreService : public TabRestoreService {
33 public:
34 // Creates a new TabRestoreService and provides an object that provides the
35 // current time. The TabRestoreService does not take ownership of the
36 // |time_factory_|.
37 InMemoryTabRestoreService(Profile* profile,
38 TimeFactory* time_factory_ = NULL);
39
40 virtual ~InMemoryTabRestoreService();
41
42 // TabRestoreService:
43 virtual void AddObserver(TabRestoreServiceObserver* observer) OVERRIDE;
44 virtual void RemoveObserver(TabRestoreServiceObserver* observer) OVERRIDE;
45 virtual void CreateHistoricalTab(content::WebContents* contents,
46 int index) OVERRIDE;
47 virtual void BrowserClosing(TabRestoreServiceDelegate* delegate) OVERRIDE;
48 virtual void BrowserClosed(TabRestoreServiceDelegate* delegate) OVERRIDE;
49 virtual void ClearEntries() OVERRIDE;
50 virtual const Entries& entries() const OVERRIDE;
51 virtual void RestoreMostRecentEntry(
52 TabRestoreServiceDelegate* delegate) OVERRIDE;
53 virtual Tab* RemoveTabEntryById(SessionID::id_type id) OVERRIDE;
54 virtual void RestoreEntryById(TabRestoreServiceDelegate* delegate,
55 SessionID::id_type id,
56 WindowOpenDisposition disposition) OVERRIDE;
57 virtual void LoadTabsFromLastSession() OVERRIDE;
58 virtual bool IsLoaded() const OVERRIDE;
59 virtual void DeleteLastSession() OVERRIDE;
60 virtual void Shutdown() OVERRIDE;
61
62 protected:
63 typedef std::map<SessionID::id_type, TabRestoreService::Entry*> IDToEntry;
64
65 // Max number of entries we'll keep around.
66 enum {
67 kMaxEntries = 25,
68 };
69
70 // Returns an iterator into entries_ whose id matches |id|. If |id| identifies
71 // a Window, then its iterator position will be returned. If it identifies a
72 // tab, then the iterator position of the Window in which the Tab resides is
73 // returned.
74 Entries::iterator GetEntryIteratorById(SessionID::id_type id);
75
76 // Adds |entry| to the list of entries and takes ownership. If |prune| is true
77 // |PruneAndNotify| is invoked. If |to_front| is true the entry is added to
78 // the front, otherwise the back. Normal closes go to the front, but
79 // tab/window closes from the previous session are added to the back.
80 void AddEntry(Entry* entry, bool prune, bool to_front);
81
82 // Prunes entries_ to contain only kMaxEntries, and removes uninteresting
83 // entries.
84 void PruneEntries();
85
86 // Notifies observers the tabs have changed.
87 void NotifyTabsChanged();
88
89 // Converts a SessionWindow into a Window, returning true on success. We use 0
90 // as the timestamp here since we do not know when the window/tab was closed.
91 bool ConvertSessionWindowToWindow(
92 SessionWindow* session_window,
93 Window* window);
94
95 // Calls either ValidateTab or ValidateWindow as appropriate.
96 static bool ValidateEntry(Entry* entry);
97
98 // Methods than can be overridden by subclasses to add some behavior (e.g.
99 // persistence).
100 virtual void OnClearEntries() {}
101 virtual void OnRestoreEntryById(SessionID::id_type id,
102 Entries::const_iterator entry_iterator) {}
103 virtual void OnAddEntry() {}
104
105 private:
106 friend class TabRestoreServiceTest;
107 FRIEND_TEST_ALL_PREFIXES(TabRestoreServiceTest, PruneEntries);
108 FRIEND_TEST_ALL_PREFIXES(TabRestoreServiceTest, PruneIsCalled);
109
110 // Populates the tab's navigations from the NavigationController, and its
111 // browser_id and pinned state from the browser.
112 void PopulateTab(Tab* tab,
113 int index,
114 TabRestoreServiceDelegate* delegate,
115 content::NavigationController* controller);
116
117 // This is a helper function for RestoreEntryById() for restoring a single
118 // tab. If |delegate| is NULL, this creates a new window for the entry. This
119 // returns the TabRestoreServiceDelegate into which the tab was restored.
120 // |disposition| will be respected, but if it is UNKNOWN then the tab's
121 // original attributes will be respected instead.
122 TabRestoreServiceDelegate* RestoreTab(const Tab& tab,
123 TabRestoreServiceDelegate* delegate,
124 WindowOpenDisposition disposition);
125
126 // Returns true if |tab| has more than one navigation. If |tab| has more
127 // than one navigation |tab->current_navigation_index| is constrained based
128 // on the number of navigations.
129 static bool ValidateTab(Tab* tab);
130
131 // Validates all the tabs in a window, plus the window's active tab index.
132 static bool ValidateWindow(Window* window);
133
134 // Returns true if |tab| is one we care about restoring.
135 static bool IsTabInteresting(const Tab* tab);
136
137 // Checks whether |window| is interesting --- if it only contains a single,
138 // uninteresting tab, it's not interesting.
139 static bool IsWindowInteresting(const Window* window);
140
141 // Validates and checks |entry| for interesting.
142 static bool FilterEntry(Entry* entry);
143
144 // Finds tab entries with the old browser_id and sets it to the new one.
145 void UpdateTabBrowserIDs(SessionID::id_type old_id,
146 SessionID::id_type new_id);
147
148 // Invoked when previous tabs or session is loaded. If both have finished
149 // loading the entries in staging_entries_ are added to entries_ and
150 // observers are notified.
151 void LoadStateChanged();
152
153 // Gets the current time. This uses the time_factory_ if there is one.
154 base::Time TimeNow() const;
155
156 Profile* const profile_;
157
158 // Set of entries. They are ordered from most to least recent.
159 Entries entries_;
160
161 // Are we restoring a tab? If this is true we ignore requests to create a
162 // historical tab.
163 bool restoring_;
164
165 ObserverList<TabRestoreServiceObserver> observer_list_;
166
167 // Set of delegates that we've received a BrowserClosing method for but no
168 // corresponding BrowserClosed. We cache the set of delegates closing to
169 // avoid creating historical tabs for them.
170 std::set<TabRestoreServiceDelegate*> closing_delegates_;
171
172 // Used when loading open tabs/session when recovering from a crash.
173 CancelableRequestConsumer crash_consumer_;
174
175 TimeFactory* time_factory_;
176
177 DISALLOW_COPY_AND_ASSIGN(InMemoryTabRestoreService);
178 };
179
180 #endif // CHROME_BROWSER_SESSIONS_IN_MEMORY_TAB_RESTORE_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698