Home | History | Annotate | Download | only in sessions
      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_TAB_RESTORE_SERVICE_H_
      6 #define CHROME_BROWSER_SESSIONS_TAB_RESTORE_SERVICE_H_
      7 
      8 #include <list>
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "base/memory/ref_counted.h"
     13 #include "base/time/time.h"
     14 #include "chrome/browser/sessions/session_id.h"
     15 #include "chrome/browser/sessions/session_types.h"
     16 #include "chrome/browser/ui/host_desktop.h"
     17 #include "components/keyed_service/core/keyed_service.h"
     18 #include "components/sessions/serialized_navigation_entry.h"
     19 #include "content/public/browser/session_storage_namespace.h"
     20 #include "ui/base/window_open_disposition.h"
     21 
     22 class TabRestoreServiceDelegate;
     23 class TabRestoreServiceObserver;
     24 
     25 namespace content {
     26 class SessionStorageNamespace;
     27 class WebContents;
     28 }
     29 
     30 // TabRestoreService is responsible for maintaining the most recently closed
     31 // tabs and windows. When a tab is closed
     32 // TabRestoreService::CreateHistoricalTab is invoked and a Tab is created to
     33 // represent the tab. Similarly, when a browser is closed, BrowserClosing is
     34 // invoked and a Window is created to represent the window.
     35 //
     36 // To restore a tab/window from the TabRestoreService invoke RestoreEntryById
     37 // or RestoreMostRecentEntry.
     38 //
     39 // To listen for changes to the set of entries managed by the TabRestoreService
     40 // add an observer.
     41 class TabRestoreService : public KeyedService {
     42  public:
     43   // Interface used to allow the test to provide a custom time.
     44   class TimeFactory {
     45    public:
     46     virtual ~TimeFactory();
     47     virtual base::Time TimeNow() = 0;
     48   };
     49 
     50   // The type of entry.
     51   enum Type {
     52     TAB,
     53     WINDOW
     54   };
     55 
     56   struct Entry {
     57     Entry();
     58     explicit Entry(Type type);
     59     virtual ~Entry();
     60 
     61     // Unique id for this entry. The id is guaranteed to be unique for a
     62     // session.
     63     SessionID::id_type id;
     64 
     65     // The type of the entry.
     66     Type type;
     67 
     68     // The time when the window or tab was closed.
     69     base::Time timestamp;
     70 
     71     // Is this entry from the last session? This is set to true for entries that
     72     // were closed during the last session, and false for entries that were
     73     // closed during this session.
     74     bool from_last_session;
     75   };
     76 
     77   // Represents a previously open tab.
     78   struct Tab : public Entry {
     79     Tab();
     80     virtual ~Tab();
     81 
     82     bool has_browser() const { return browser_id > 0; }
     83 
     84     // The navigations.
     85     std::vector<sessions::SerializedNavigationEntry> navigations;
     86 
     87     // Index of the selected navigation in navigations.
     88     int current_navigation_index;
     89 
     90     // The ID of the browser to which this tab belonged, so it can be restored
     91     // there. May be 0 (an invalid SessionID) when restoring an entire session.
     92     SessionID::id_type browser_id;
     93 
     94     // Index within the tab strip. May be -1 for an unknown index.
     95     int tabstrip_index;
     96 
     97     // True if the tab was pinned.
     98     bool pinned;
     99 
    100     // If non-empty gives the id of the extension for the tab.
    101     std::string extension_app_id;
    102 
    103     // The associated session storage namespace (if any).
    104     scoped_refptr<content::SessionStorageNamespace> session_storage_namespace;
    105 
    106     // The user agent override used for the tab's navigations (if applicable).
    107     std::string user_agent_override;
    108   };
    109 
    110   // Represents a previously open window.
    111   struct Window : public Entry {
    112     Window();
    113     virtual ~Window();
    114 
    115     // The tabs that comprised the window, in order.
    116     std::vector<Tab> tabs;
    117 
    118     // Index of the selected tab.
    119     int selected_tab_index;
    120 
    121     // If an application window, the name of the app.
    122     std::string app_name;
    123   };
    124 
    125   typedef std::list<Entry*> Entries;
    126 
    127   virtual ~TabRestoreService();
    128 
    129   // Adds/removes an observer. TabRestoreService does not take ownership of
    130   // the observer.
    131   virtual void AddObserver(TabRestoreServiceObserver* observer) = 0;
    132   virtual void RemoveObserver(TabRestoreServiceObserver* observer) = 0;
    133 
    134   // Creates a Tab to represent |contents| and notifies observers the list of
    135   // entries has changed.
    136   virtual void CreateHistoricalTab(content::WebContents* contents,
    137                                    int index) = 0;
    138 
    139   // Invoked when a browser is closing. If |delegate| is a tabbed browser with
    140   // at least one tab, a Window is created, added to entries and observers are
    141   // notified.
    142   virtual void BrowserClosing(TabRestoreServiceDelegate* delegate) = 0;
    143 
    144   // Invoked when the browser is done closing.
    145   virtual void BrowserClosed(TabRestoreServiceDelegate* delegate) = 0;
    146 
    147   // Removes all entries from the list and notifies observers the list
    148   // of tabs has changed.
    149   virtual void ClearEntries() = 0;
    150 
    151   // Returns the entries, ordered with most recently closed entries at the
    152   // front.
    153   virtual const Entries& entries() const = 0;
    154 
    155   // Restores the most recently closed entry. Does nothing if there are no
    156   // entries to restore. If the most recently restored entry is a tab, it is
    157   // added to |delegate|. If a new browser needs to be created for this entry,
    158   // it will be created on the desktop specified by |host_desktop_type|. Returns
    159   // the WebContents of the restored tab(s).
    160   virtual std::vector<content::WebContents*> RestoreMostRecentEntry(
    161       TabRestoreServiceDelegate* delegate,
    162       chrome::HostDesktopType host_desktop_type) = 0;
    163 
    164   // Removes the Tab with id |id| from the list and returns it; ownership is
    165   // passed to the caller.
    166   virtual Tab* RemoveTabEntryById(SessionID::id_type id) = 0;
    167 
    168   // Restores an entry by id. If there is no entry with an id matching |id|,
    169   // this does nothing. If |delegate| is NULL, this creates a new window for the
    170   // entry. |disposition| is respected, but the attributes (tabstrip index,
    171   // browser window) of the tab when it was closed will be respected if
    172   // disposition is UNKNOWN. If a new browser needs to be created for this
    173   // entry, it will be created on the desktop specified by |host_desktop_type|.
    174   // Returns the WebContents of the restored tab(s).
    175   virtual std::vector<content::WebContents*> RestoreEntryById(
    176       TabRestoreServiceDelegate* delegate,
    177       SessionID::id_type id,
    178       chrome::HostDesktopType host_desktop_type,
    179       WindowOpenDisposition disposition) = 0;
    180 
    181   // Loads the tabs and previous session. This does nothing if the tabs
    182   // from the previous session have already been loaded.
    183   virtual void LoadTabsFromLastSession() = 0;
    184 
    185   // Returns true if the tab entries have been loaded.
    186   virtual bool IsLoaded() const = 0;
    187 
    188   // Deletes the last session.
    189   virtual void DeleteLastSession() = 0;
    190 };
    191 
    192 #endif  // CHROME_BROWSER_SESSIONS_TAB_RESTORE_SERVICE_H_
    193