summaryrefslogtreecommitdiffstats
path: root/chrome/browser/tab_restore_service.h
blob: 17cca7fd1e1969a2534468db2a7ab4c22ded78fb (plain)
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
// Copyright 2008, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//    * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//    * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//    * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef CHROME_BROWSER_TAB_RESTORE_SERVICE_H__
#define CHROME_BROWSER_TAB_RESTORE_SERVICE_H__

#include <list>

#include "base/observer_list.h"
#include "base/time.h"
#include "chrome/browser/session_service.h"

class NavigationController;
class Profile;

// TabRestoreService is responsible for maintaining the most recently closed
// tabs. When the user closes a tab TabRestoreService::CreateHistoricalTab is
// invoked and a HistoricalTab is created to represent the tab.
//
// TabRestoreService can recreate tabs from the previous session as well.
// LoadPreviousSessionTabs loads (asynchronously) the tabs from the previous
// session. When done, the observer is notified.
//
// To restore a tab from the TabRestoreService invoke AddRestoredTab on the
// Browser you want to restore the tab to, followed by RemoveHistoricalTabById
// to remove the tab from the restore service.
//
// To listen for changes to the set of tabs managed by the TabRestoreService
// add an observer.

class TabRestoreService {
 public:
  // Observer is notified when the set of tabs managed by TabRestoreService
  // changes in some way.
  class Observer {
   public:
    // Sent when the internal tab state changed
    virtual void TabRestoreServiceChanged(TabRestoreService* service) = 0;

    // Sent to all remaining Observers when TabRestoreService's
    // destructor is run.
    virtual void TabRestoreServiceDestroyed(TabRestoreService* service) = 0;
  };

  // Represents a previously open tab.
  struct HistoricalTab {
    HistoricalTab();

    // Time the tab was closed.
    Time close_time;

    // If true, this is a historical session and not a closed tab.
    bool from_last_session;

    // The navigations.
    // WARNING: navigations may be empty.
    std::vector<TabNavigation> navigations;

    // Index of the selected navigation in navigations.
    int current_navigation_index;

    // Unique id for the closed tab. This is guaranteed to be unique for
    // a session.
    const int id;
  };

  typedef std::list<HistoricalTab> Tabs;

  // Creates a new TabRestoreService. This does not load tabs from the last
  // session, you must explicitly invoke LoadPreviousSessionTabs to do that.
  explicit TabRestoreService(Profile* profile);
  ~TabRestoreService();

  // Adds/removes an observer. TabRestoreService does not take ownership of
  // the observer.
  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

  // If the previous session has not been loaded, it is loaded and the tabs
  // from it are placed at the end of the queue.
  void LoadPreviousSessionTabs();

  // Returns true if loading the previous sessions tabs.
  bool IsLoadingPreviousSessionTabs();

  // Returns true if LoadPreviousSessionTabs will attempt to load any tabs.
  bool WillLoadPreviousSessionTabs();

  // Creates a HistoricalTab to represent the tab and notifies observers the
  // list of tabs has changed.
  void CreateHistoricalTab(NavigationController* tab);

  // Removes the HistoricalTab with the specified id and notifies observers.
  // Does nothing if id does not identify a valid historical tab id.
  void RemoveHistoricalTabById(int id);

  // Removes all HistoricalTabs from the list and notifies observers the list
  // of tabs has changed.
  void ClearHistoricalTabs();

  // Returns the tabs, ordered with most recently closed tabs at the front.
  const Tabs& tabs() const { return tabs_; }

 private:
  // Callback from loading the last session. As necessary adds the tabs to
  // tabs_.
  void OnGotLastSession(SessionService::Handle handle,
                        std::vector<SessionWindow*>* windows);

  // Invoked from OnGotLastSession to add the necessary tabs from windows
  // to tabs_.
  void AddHistoricalTabs(std::vector<SessionWindow*>* windows);

  // Creates a HistoricalTab from the tab.
  void AppendHistoricalTabFromSessionTab(SessionTab* tab);

  // Populates tabs->navigations from the NavigationController.
  void PopulateTabFromController(NavigationController* controller,
                                 HistoricalTab* tab);

  // Populates tab->navigations from a previous sessions navigations.
  void PopulateTabFromSessionTab(SessionTab* session_tab,
                                 HistoricalTab* tab);

  // Notifies observers the tabs have changed.
  void NotifyTabsChanged();

  Profile* profile_;

  // Whether we've loaded the last session.
  bool loaded_last_session_;

  // Set of tabs. We own the NavigationControllers in this list.
  Tabs tabs_;

  // Used in requesting the last session.
  CancelableRequestConsumer cancelable_consumer_;

  ObserverList<Observer> observer_list_;

  DISALLOW_EVIL_CONSTRUCTORS(TabRestoreService);
};

#endif  // CHROME_BROWSER_TAB_RESTORE_SERVICE_H__