1ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Use of this source code is governed by a BSD-style license that can be
3c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// found in the LICENSE file.
4c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
5c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#ifndef CHROME_BROWSER_SESSIONS_SESSION_SERVICE_H_
6c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#define CHROME_BROWSER_SESSIONS_SESSION_SERVICE_H_
73345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#pragma once
8c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
9c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include <map>
10c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include <string>
11c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
12c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "base/basictypes.h"
13c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "base/callback.h"
14c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "base/time.h"
15c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/browser/defaults.h"
16c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/browser/sessions/base_session_service.h"
17c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/browser/sessions/session_id.h"
184a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch#include "chrome/browser/ui/browser.h"
194a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch#include "chrome/browser/ui/browser_list.h"
20ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "content/common/notification_observer.h"
21ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "content/common/notification_registrar.h"
22c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
23c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass NavigationController;
24c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass NavigationEntry;
25c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass Profile;
26c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass SessionCommand;
27c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochstruct SessionTab;
28c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochstruct SessionWindow;
29c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
30c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// SessionService ------------------------------------------------------------
31c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
32c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// SessionService is responsible for maintaining the state of open windows
33c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// and tabs so that they can be restored at a later date. The state of the
34c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// currently open browsers is referred to as the current session.
35c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch//
36ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// SessionService supports restoring from the last session. The last session
37ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// typically corresponds to the last run of the browser, but not always. For
38ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// example, if the user has a tabbed browser and app window running, closes the
39ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// tabbed browser, then creates a new tabbed browser the current session is made
40ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// the last session and the current session reset. This is done to provide the
41ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// illusion that app windows run in separate processes. Similar behavior occurs
42ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// with incognito windows.
43c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch//
44c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// SessionService itself maintains a set of SessionCommands that allow
45c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// SessionService to rebuild the open state of the browser (as
46c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// SessionWindow, SessionTab and TabNavigation). The commands are periodically
47c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// flushed to SessionBackend and written to a file. Every so often
48c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// SessionService rebuilds the contents of the file from the open state
49c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// of the browser.
50c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass SessionService : public BaseSessionService,
51c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                       public NotificationObserver {
52c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  friend class SessionServiceTestHelper;
53c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch public:
54c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Creates a SessionService for the specified profile.
55c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  explicit SessionService(Profile* profile);
56c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // For testing.
57c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  explicit SessionService(const FilePath& save_path);
58c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
59c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Invoke at a point when you think session restore might occur. For example,
60c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // during startup and window creation this is invoked to see if a session
61c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // needs to be restored. If a session needs to be restored it is done so
62c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // asynchronously and true is returned. If false is returned the session was
63c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // not restored and the caller needs to create a new window.
64c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool RestoreIfNecessary(const std::vector<GURL>& urls_to_open);
65c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
66c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Resets the contents of the file from the current state of all open
67c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // browsers whose profile matches our profile.
68c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void ResetFromCurrentBrowsers();
69c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
70c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Moves the current session to the last session. This is useful when a
71c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // checkpoint occurs, such as when the user launches the app and no tabbed
72c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // browsers are running.
73c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void MoveCurrentSessionToLastSession();
74c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
75c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Associates a tab with a window.
76c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void SetTabWindow(const SessionID& window_id,
77c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                    const SessionID& tab_id);
78c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
79c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Sets the bounds of a window.
80c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void SetWindowBounds(const SessionID& window_id,
81c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                       const gfx::Rect& bounds,
82c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                       bool is_maximized);
83c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
84c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Sets the visual index of the tab in its parent window.
85c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void SetTabIndexInWindow(const SessionID& window_id,
86c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                           const SessionID& tab_id,
87c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                           int new_index);
88c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
89c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Sets the pinned state of the tab.
90c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void SetPinnedState(const SessionID& window_id,
91c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                      const SessionID& tab_id,
92c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                      bool is_pinned);
93c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
94c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Notification that a tab has been closed. |closed_by_user_gesture| comes
95c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // from |TabContents::closed_by_user_gesture|; see it for details.
96c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  //
97c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Note: this is invoked from the NavigationController's destructor, which is
98c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // after the actual tab has been removed.
99c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void TabClosed(const SessionID& window_id,
100c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                 const SessionID& tab_id,
101c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                 bool closed_by_user_gesture);
102c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
103c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Notification the window is about to close.
104c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void WindowClosing(const SessionID& window_id);
105c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
106c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Notification a window has finished closing.
107c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void WindowClosed(const SessionID& window_id);
108c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
109c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Sets the type of window. In order for the contents of a window to be
110c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // tracked SetWindowType must be invoked with a type we track
111c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // (should_track_changes_for_browser_type returns true).
112c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void SetWindowType(const SessionID& window_id, Browser::Type type);
113c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
114c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Invoked when the NavigationController has removed entries from the back of
115c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // the list. |count| gives the number of entries in the navigation controller.
116c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void TabNavigationPathPrunedFromBack(const SessionID& window_id,
117c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                       const SessionID& tab_id,
118c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                       int count);
119c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
120c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Invoked when the NavigationController has removed entries from the front of
121c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // the list. |count| gives the number of entries that were removed.
122c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void TabNavigationPathPrunedFromFront(const SessionID& window_id,
123c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                        const SessionID& tab_id,
124c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                        int count);
125c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
126c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Updates the navigation entry for the specified tab.
127c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void UpdateTabNavigation(const SessionID& window_id,
128c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                           const SessionID& tab_id,
129c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                           int index,
130c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                           const NavigationEntry& entry);
131c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
132c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Notification that a tab has restored its entries or a closed tab is being
133c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // reused.
134c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void TabRestored(NavigationController* controller,
135c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                   bool pinned);
136c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
137c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Sets the index of the selected entry in the navigation controller for the
138c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // specified tab.
139c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void SetSelectedNavigationIndex(const SessionID& window_id,
140c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                  const SessionID& tab_id,
141c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                  int index);
142c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
143c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Sets the index of the selected tab in the specified window.
144c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void SetSelectedTabInWindow(const SessionID& window_id, int index);
145c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
146c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Callback from GetSavedSession of GetLastSession.
147c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  //
148c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // The contents of the supplied vector are deleted after the callback is
149c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // notified. To take ownership of the vector clear it before returning.
150c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  //
151c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // The time gives the time the session was closed.
152c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  typedef Callback2<Handle, std::vector<SessionWindow*>*>::Type
153c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      SessionCallback;
154c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
155c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Fetches the contents of the last session, notifying the callback when
156c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // done. If the callback is supplied an empty vector of SessionWindows
157c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // it means the session could not be restored.
158c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  //
159c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // The created request does NOT directly invoke the callback, rather the
160c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // callback invokes OnGotSessionCommands from which we map the
161c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // SessionCommands to browser state, then notify the callback.
162c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  Handle GetLastSession(CancelableRequestConsumerBase* consumer,
163c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                        SessionCallback* callback);
164c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
165c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Fetches the contents of the current session, notifying the callback when
166c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // done. If the callback is supplied an empty vector of SessionWindows
167c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // it means the session could not be restored.
168c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  //
169c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // The created request does NOT directly invoke the callback, rather the
170c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // callback invokes OnGotSessionCommands from which we map the
171c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // SessionCommands to browser state, then notify the callback.
172c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  Handle GetCurrentSession(CancelableRequestConsumerBase* consumer,
173c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                           SessionCallback* callback);
174c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
175c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Overridden from BaseSessionService because we want some UMA reporting on
176c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // session update activities.
177c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  virtual void Save();
178c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
179c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch private:
180c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  typedef std::map<SessionID::id_type, std::pair<int, int> > IdToRange;
181c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  typedef std::map<SessionID::id_type, SessionTab*> IdToSessionTab;
182c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  typedef std::map<SessionID::id_type, SessionWindow*> IdToSessionWindow;
183c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
184c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
185c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  virtual ~SessionService();
186c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
187c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // These types mirror Browser::Type, but are re-defined here because these
188c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // specific enumeration _values_ are written into the session database and
189c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // are needed to maintain forward compatibility.
190c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  enum WindowType {
191c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    TYPE_NORMAL = 0,
192c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    TYPE_POPUP = 1,
193c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    TYPE_APP = 2,
194c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    TYPE_APP_POPUP = TYPE_APP + TYPE_POPUP,
195c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    TYPE_DEVTOOLS = TYPE_APP + 4,
196c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    TYPE_APP_PANEL = TYPE_APP + 8
197c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  };
198c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
199c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void Init();
200c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
201c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Implementation of RestoreIfNecessary. If |browser| is non-null and we need
202c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // to restore, the tabs are added to it, otherwise a new browser is created.
203c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool RestoreIfNecessary(const std::vector<GURL>& urls_to_open,
204c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                          Browser* browser);
205c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
206c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  virtual void Observe(NotificationType type,
207c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                       const NotificationSource& source,
208c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                       const NotificationDetails& details);
209c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
210c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Sets the application extension id of the specified tab.
211c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void SetTabExtensionAppID(const SessionID& window_id,
212c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                            const SessionID& tab_id,
213c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                            const std::string& extension_app_id);
214c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
215c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Methods to create the various commands. It is up to the caller to delete
216c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // the returned the SessionCommand* object.
217c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  SessionCommand* CreateSetSelectedTabInWindow(const SessionID& window_id,
218c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                               int index);
219c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
220c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  SessionCommand* CreateSetTabWindowCommand(const SessionID& window_id,
221c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                            const SessionID& tab_id);
222c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
223c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  SessionCommand* CreateSetWindowBoundsCommand(const SessionID& window_id,
224c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                               const gfx::Rect& bounds,
225c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                               bool is_maximized);
226c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
227c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  SessionCommand* CreateSetTabIndexInWindowCommand(const SessionID& tab_id,
228c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                                   int new_index);
229c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
230c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  SessionCommand* CreateTabClosedCommand(SessionID::id_type tab_id);
231c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
232c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  SessionCommand* CreateWindowClosedCommand(SessionID::id_type tab_id);
233c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
234c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  SessionCommand* CreateSetSelectedNavigationIndexCommand(
235c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      const SessionID& tab_id,
236c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      int index);
237c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
238c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  SessionCommand* CreateSetWindowTypeCommand(const SessionID& window_id,
239c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                             WindowType type);
240c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
241c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  SessionCommand* CreatePinnedStateCommand(const SessionID& tab_id,
242c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                           bool is_pinned);
243c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
244ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Callback from the backend for getting the commands from the save file.
245ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Converts the commands in SessionWindows and notifies the real callback.
246c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void OnGotSessionCommands(
247c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      Handle handle,
248c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      scoped_refptr<InternalGetCommandsRequest> request);
249c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
250c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Converts the commands into SessionWindows. On return any valid
251c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // windows are added to valid_windows. It is up to the caller to delete
252c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // the windows added to valid_windows.
253c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  //
254c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // If ignore_recent_closes is true, any window/tab closes within in a certain
255c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // time frame are ignored.
256c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void RestoreSessionFromCommands(const std::vector<SessionCommand*>& commands,
257c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                  std::vector<SessionWindow*>* valid_windows);
258c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
259c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Iterates through the vector updating the selected_tab_index of each
260c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // SessionWindow based on the actual tabs that were restored.
261c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void UpdateSelectedTabIndex(std::vector<SessionWindow*>* windows);
262c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
263c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Returns the window in windows with the specified id. If a window does
264c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // not exist, one is created.
265c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  SessionWindow* GetWindow(SessionID::id_type window_id,
266c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                           IdToSessionWindow* windows);
267c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
268c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Returns the tab with the specified id in tabs. If a tab does not exist,
269c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // it is created.
270c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  SessionTab* GetTab(SessionID::id_type tab_id,
271c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                     IdToSessionTab* tabs);
272c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
273c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Returns an iterator into navigations pointing to the navigation whose
274c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // index matches |index|. If no navigation index matches |index|, the first
275c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // navigation with an index > |index| is returned.
276c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  //
277c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // This assumes the navigations are ordered by index in ascending order.
278c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  std::vector<TabNavigation>::iterator FindClosestNavigationWithIndex(
279c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      std::vector<TabNavigation>* navigations,
280c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      int index);
281c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
282c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Does the following:
283c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // . Deletes and removes any windows with no tabs or windows with types other
284c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  //   than tabbed_browser or browser. NOTE: constrained windows that have
285c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  //   been dragged out are of type browser. As such, this preserves any dragged
286c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  //   out constrained windows (aka popups that have been dragged out).
287c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // . Sorts the tabs in windows with valid tabs based on the tabs
288c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  //   visual order, and adds the valid windows to windows.
289c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void SortTabsBasedOnVisualOrderAndPrune(
290c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      std::map<int, SessionWindow*>* windows,
291c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      std::vector<SessionWindow*>* valid_windows);
292c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
293c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Adds tabs to their parent window based on the tab's window_id. This
294c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // ignores tabs with no navigations.
295c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void AddTabsToWindows(std::map<int, SessionTab*>* tabs,
296c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                        std::map<int, SessionWindow*>* windows);
297c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
298c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Creates tabs and windows from the specified commands. The created tabs
299c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // and windows are added to |tabs| and |windows| respectively. It is up to
300c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // the caller to delete the tabs and windows added to |tabs| and |windows|.
301c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  //
302c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // This does NOT add any created SessionTabs to SessionWindow.tabs, that is
303c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // done by AddTabsToWindows.
304c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool CreateTabsAndWindows(const std::vector<SessionCommand*>& data,
305c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                            std::map<int, SessionTab*>* tabs,
306c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                            std::map<int, SessionWindow*>* windows);
307c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
308c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Adds commands to commands that will recreate the state of the specified
309c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // NavigationController. This adds at most kMaxNavigationCountToPersist
310c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // navigations (in each direction from the current navigation index).
311c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // A pair is added to tab_to_available_range indicating the range of
312c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // indices that were written.
313c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void BuildCommandsForTab(
314c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      const SessionID& window_id,
315c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      NavigationController* controller,
316c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      int index_in_window,
317c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      bool is_pinned,
318c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      std::vector<SessionCommand*>* commands,
319c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      IdToRange* tab_to_available_range);
320c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
321c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Adds commands to create the specified browser, and invokes
322c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // BuildCommandsForTab for each of the tabs in the browser. This ignores
323c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // any tabs not in the profile we were created with.
324c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void BuildCommandsForBrowser(
325c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      Browser* browser,
326c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      std::vector<SessionCommand*>* commands,
327c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      IdToRange* tab_to_available_range,
328c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      std::set<SessionID::id_type>* windows_to_track);
329c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
330c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Iterates over all the known browsers invoking BuildCommandsForBrowser.
331c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // This only adds browsers that should be tracked
332c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // (should_track_changes_for_browser_type returns true). All browsers that
333c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // are tracked are added to windows_to_track (as long as it is non-null).
334c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void BuildCommandsFromBrowsers(
335c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      std::vector<SessionCommand*>* commands,
336c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      IdToRange* tab_to_available_range,
337c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      std::set<SessionID::id_type>* windows_to_track);
338c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
339c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Schedules a reset. A reset means the contents of the file are recreated
340c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // from the state of the browser.
341c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void ScheduleReset();
342c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
343c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Searches for a pending command that can be replaced with command.
344c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // If one is found, pending command is removed, command is added to
345c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // the pending commands and true is returned.
346c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool ReplacePendingCommand(SessionCommand* command);
347c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
348c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Schedules the specified command. This method takes ownership of the
349c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // command.
35021d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen  virtual void ScheduleCommand(SessionCommand* command);
351c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
352c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Converts all pending tab/window closes to commands and schedules them.
353c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void CommitPendingCloses();
354c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
355c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Returns true if there is only one window open with a single tab that shares
356c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // our profile.
357c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool IsOnlyOneTabLeft();
358c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
359c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Returns true if there are open trackable browser windows whose ids do
360c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // match |window_id| with our profile. A trackable window is a window from
361c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // which |should_track_changes_for_browser_type| returns true. See
362c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // |should_track_changes_for_browser_type| for details.
363c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool HasOpenTrackableBrowsers(const SessionID& window_id);
364c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
365c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Returns true if changes to tabs in the specified window should be tracked.
366c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool ShouldTrackChangesToWindow(const SessionID& window_id);
367c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
368c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Returns true if we track changes to the specified browser type.
369c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  static bool should_track_changes_for_browser_type(Browser::Type type) {
370c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    return type == Browser::TYPE_NORMAL ||
371c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch        (type == Browser::TYPE_POPUP && browser_defaults::kRestorePopups);
372c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  }
373c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
374c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Returns true if we should record a window close as pending.
375c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // |has_open_trackable_browsers_| must be up-to-date before calling this.
376c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool should_record_close_as_pending() const {
377c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    // When this is called, the browser window being closed is still open, hence
378c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    // still in the browser list. If there is a browser window other than the
379c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    // one being closed but no trackable windows, then the others must be App
380c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    // windows or similar. In this case, we record the close as pending.
381c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    return !has_open_trackable_browsers_ &&
382c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch        (!browser_defaults::kBrowserAliveWithNoWindows ||
383c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch         BrowserList::size() > 1);
384c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  }
385c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
386c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Call when certain session relevant notifications
387c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // (tab_closed, nav_list_pruned) occur.  In addition, this is
388c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // currently called when Save() is called to compare how often the
389c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // session data is currently saved verses when we may want to save it.
390c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // It records the data in UMA stats.
391c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void RecordSessionUpdateHistogramData(NotificationType type,
392c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    base::TimeTicks* last_updated_time);
393c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
394c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Helper methods to record the histogram data
395c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void RecordUpdatedTabClosed(base::TimeDelta delta, bool use_long_period);
396c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void RecordUpdatedNavListPruned(base::TimeDelta delta, bool use_long_period);
397c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void RecordUpdatedNavEntryCommit(base::TimeDelta delta, bool use_long_period);
398c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void RecordUpdatedSaveTime(base::TimeDelta delta, bool use_long_period);
399c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void RecordUpdatedSessionNavigationOrTab(base::TimeDelta delta,
400c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                           bool use_long_period);
401c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
402c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Convert back/forward between the Browser and SessionService DB window
403c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // types.
404c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  static WindowType WindowTypeForBrowserType(Browser::Type type);
405c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  static Browser::Type BrowserTypeForWindowType(WindowType type);
406c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
407c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  NotificationRegistrar registrar_;
408c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
409c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Maps from session tab id to the range of navigation entries that has
410c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // been written to disk.
411c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  //
412c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // This is only used if not all the navigation entries have been
413c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // written.
414c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  IdToRange tab_to_available_range_;
415c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
416c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // When the user closes the last window, where the last window is the
417c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // last tabbed browser and no more tabbed browsers are open with the same
418c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // profile, the window ID is added here. These IDs are only committed (which
419c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // marks them as closed) if the user creates a new tabbed browser.
420c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  typedef std::set<SessionID::id_type> PendingWindowCloseIDs;
421c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  PendingWindowCloseIDs pending_window_close_ids_;
422c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
423c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Set of tabs that have been closed by way of the last window or last tab
424c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // closing, but not yet committed.
425c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  typedef std::set<SessionID::id_type> PendingTabCloseIDs;
426c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  PendingTabCloseIDs pending_tab_close_ids_;
427c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
428c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // When a window other than the last window (see description of
429c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // pending_window_close_ids) is closed, the id is added to this set.
430c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  typedef std::set<SessionID::id_type> WindowClosingIDs;
431c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  WindowClosingIDs window_closing_ids_;
432c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
433c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Set of windows we're tracking changes to. This is only browsers that
434c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // return true from should_track_changes_for_browser_type.
435c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  typedef std::set<SessionID::id_type> WindowsTracking;
436c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  WindowsTracking windows_tracking_;
437c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
438c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Are there any open trackable browsers?
439c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool has_open_trackable_browsers_;
440c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
441c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // If true and a new tabbed browser is created and there are no opened tabbed
442c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // browser (has_open_trackable_browsers_ is false), then the current session
443ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // is made the last session. See description above class for details on
444ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // current/last session.
445c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool move_on_new_browser_;
446c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
44772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // Used for reporting frequency of session altering operations.
448c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  base::TimeTicks last_updated_tab_closed_time_;
449c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  base::TimeTicks last_updated_nav_list_pruned_time_;
450c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  base::TimeTicks last_updated_nav_entry_commit_time_;
451c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  base::TimeTicks last_updated_save_time_;
452c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
453c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Constants used in calculating histogram data.
454c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  const base::TimeDelta save_delay_in_millis_;
455c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  const base::TimeDelta save_delay_in_mins_;
456c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  const base::TimeDelta save_delay_in_hrs_;
457c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
458c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  DISALLOW_COPY_AND_ASSIGN(SessionService);
459c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch};
460c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
461c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#endif  // CHROME_BROWSER_SESSIONS_SESSION_SERVICE_H_
462