OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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_SESSION_SERVICE_COMMANDS_H_ |
| 6 #define CHROME_BROWSER_SESSIONS_SESSION_SERVICE_COMMANDS_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <string> |
| 10 |
| 11 #include "base/basictypes.h" |
| 12 #include "base/callback.h" |
| 13 #include "base/memory/scoped_vector.h" |
| 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/task/cancelable_task_tracker.h" |
| 16 #include "chrome/browser/sessions/base_session_service.h" |
| 17 #include "chrome/browser/sessions/session_types.h" |
| 18 #include "ui/base/ui_base_types.h" |
| 19 |
| 20 class BaseSessionServiceCommands; |
| 21 class SessionCommand; |
| 22 |
| 23 // SessionServiceCommands ------------------------------------------------------ |
| 24 |
| 25 // SessionServiceCommands supplies functions to create sequentialized change |
| 26 // commands which are required to reconstruct the current session state. It |
| 27 // furthermore offers a |RestoreSessionFromCommands| function which will create |
| 28 // |SessionWindow| and |SessionTab| data structures from a list of commands. |
| 29 // |
| 30 class SessionServiceCommands { |
| 31 public: |
| 32 // Used to distinguish an application from a ordinary content window. |
| 33 enum AppType { |
| 34 TYPE_APP, |
| 35 TYPE_NORMAL |
| 36 }; |
| 37 |
| 38 SessionServiceCommands(); |
| 39 virtual ~SessionServiceCommands(); |
| 40 |
| 41 // Callback from GetLastSession. |
| 42 // The second parameter is the id of the window that was last active. |
| 43 typedef base::Callback<void(ScopedVector<SessionWindow>, SessionID::id_type)> |
| 44 SessionCallback; |
| 45 |
| 46 // Fetches the contents of the last session, notifying the callback when |
| 47 // done. If the callback is supplied an empty vector of SessionWindows |
| 48 // it means the session could not be restored. |
| 49 base::CancelableTaskTracker::TaskId GetLastSession( |
| 50 const SessionCallback& callback, |
| 51 base::CancelableTaskTracker* tracker); |
| 52 |
| 53 // Returns true if a window of given |window_type| and |app_type| should get |
| 54 // restored upon session restore. |
| 55 virtual bool ShouldRestoreWindowOfType( |
| 56 SessionWindow::WindowType type, |
| 57 SessionServiceCommands::AppType app_type) const = 0; |
| 58 |
| 59 // Methods to create the various commands. It is up to the caller to delete |
| 60 // the returned SessionCommand* object. |
| 61 SessionCommand* CreateSetSelectedTabInWindowCommand( |
| 62 const SessionID& window_id, |
| 63 int index); |
| 64 SessionCommand* CreateSetTabWindowCommand(const SessionID& window_id, |
| 65 const SessionID& tab_id); |
| 66 SessionCommand* CreateSetWindowBoundsCommand(const SessionID& window_id, |
| 67 const gfx::Rect& bounds, |
| 68 ui::WindowShowState show_state); |
| 69 SessionCommand* CreateSetTabIndexInWindowCommand(const SessionID& tab_id, |
| 70 int new_index); |
| 71 SessionCommand* CreateTabClosedCommand(SessionID::id_type tab_id); |
| 72 SessionCommand* CreateWindowClosedCommand(SessionID::id_type tab_id); |
| 73 SessionCommand* CreateSetSelectedNavigationIndexCommand( |
| 74 const SessionID& tab_id, |
| 75 int index); |
| 76 SessionCommand* CreateSetWindowTypeCommand(const SessionID& window_id, |
| 77 SessionWindow::WindowType type); |
| 78 SessionCommand* CreatePinnedStateCommand(const SessionID& tab_id, |
| 79 bool is_pinned); |
| 80 SessionCommand* CreateSessionStorageAssociatedCommand( |
| 81 const SessionID& tab_id, |
| 82 const std::string& session_storage_persistent_id); |
| 83 SessionCommand* CreateSetActiveWindowCommand(const SessionID& window_id); |
| 84 SessionCommand* CreateTabNavigationPathPrunedFromBackCommand( |
| 85 const SessionID& tab_id, |
| 86 int count); |
| 87 SessionCommand* CreateTabNavigationPathPrunedFromFrontCommand( |
| 88 const SessionID& tab_id, |
| 89 int count); |
| 90 SessionCommand* CreateUpdateTabNavigationCommand( |
| 91 const SessionID& tab_id, |
| 92 const sessions::SerializedNavigationEntry& navigation); |
| 93 SessionCommand* CreateSetTabExtensionAppIDCommand( |
| 94 const SessionID& tab_id, |
| 95 const std::string& extension_id); |
| 96 SessionCommand* CreateSetTabUserAgentOverrideCommand( |
| 97 const SessionID& tab_id, |
| 98 const std::string& user_agent_override); |
| 99 SessionCommand* CreateSetWindowAppNameCommand(const SessionID& window_id, |
| 100 const std::string& app_name); |
| 101 |
| 102 // Searches for a pending command in |pending_commands| that can be replaced |
| 103 // with |command|. If one is found, pending command is removed, command is |
| 104 // added to the pending commands and true is returned. |
| 105 bool ReplacePendingCommand(SessionCommand* command, |
| 106 std::vector<SessionCommand*>& pending_commands); |
| 107 |
| 108 // Returns true if provided |command| either closes a window or a tab. |
| 109 bool IsClosingCommand(SessionCommand* command) const; |
| 110 |
| 111 // Converts the commands into SessionWindows. On return any valid |
| 112 // windows are added to valid_windows. It is up to the caller to delete |
| 113 // the windows added to valid_windows. |active_window_id| will be set with the |
| 114 // id of the last active window, but it's only valid when this id corresponds |
| 115 // to the id of one of the windows in valid_windows. |
| 116 void RestoreSessionFromCommands(const std::vector<SessionCommand*>& commands, |
| 117 std::vector<SessionWindow*>* valid_windows, |
| 118 SessionID::id_type* active_window_id); |
| 119 |
| 120 private: |
| 121 typedef std::map<SessionID::id_type, SessionTab*> IdToSessionTab; |
| 122 typedef std::map<SessionID::id_type, SessionWindow*> IdToSessionWindow; |
| 123 |
| 124 // Iterates through the vector updating the selected_tab_index of each |
| 125 // SessionWindow based on the actual tabs that were restored. |
| 126 void UpdateSelectedTabIndex(std::vector<SessionWindow*>* windows); |
| 127 |
| 128 // Returns the window in windows with the specified id. If a window does |
| 129 // not exist, one is created. |
| 130 SessionWindow* GetWindow(SessionID::id_type window_id, |
| 131 IdToSessionWindow* windows); |
| 132 |
| 133 // Returns the tab with the specified id in tabs. If a tab does not exist, |
| 134 // it is created. |
| 135 SessionTab* GetTab(SessionID::id_type tab_id, |
| 136 IdToSessionTab* tabs); |
| 137 |
| 138 // Returns an iterator into navigations pointing to the navigation whose |
| 139 // index matches |index|. If no navigation index matches |index|, the first |
| 140 // navigation with an index > |index| is returned. |
| 141 // |
| 142 // This assumes the navigations are ordered by index in ascending order. |
| 143 std::vector<sessions::SerializedNavigationEntry>::iterator |
| 144 FindClosestNavigationWithIndex( |
| 145 std::vector<sessions::SerializedNavigationEntry>* navigations, |
| 146 int index); |
| 147 |
| 148 // Does the following: |
| 149 // . Deletes and removes any windows with no tabs or windows with types other |
| 150 // than tabbed_browser or browser. NOTE: constrained windows that have |
| 151 // been dragged out are of type browser. As such, this preserves any dragged |
| 152 // out constrained windows (aka popups that have been dragged out). |
| 153 // . Sorts the tabs in windows with valid tabs based on the tabs |
| 154 // visual order, and adds the valid windows to windows. |
| 155 void SortTabsBasedOnVisualOrderAndPrune( |
| 156 std::map<int, SessionWindow*>* windows, |
| 157 std::vector<SessionWindow*>* valid_windows); |
| 158 |
| 159 // Adds tabs to their parent window based on the tab's window_id. This |
| 160 // ignores tabs with no navigations. |
| 161 void AddTabsToWindows(std::map<int, SessionTab*>* tabs, |
| 162 std::map<int, SessionWindow*>* windows); |
| 163 |
| 164 // Creates tabs and windows from the commands specified in |data|. The created |
| 165 // tabs and windows are added to |tabs| and |windows| respectively, with the |
| 166 // id of the active window set in |active_window_id|. It is up to the caller |
| 167 // to delete the tabs and windows added to |tabs| and |windows|. |
| 168 // |
| 169 // This does NOT add any created SessionTabs to SessionWindow.tabs, that is |
| 170 // done by AddTabsToWindows. |
| 171 bool CreateTabsAndWindows(const std::vector<SessionCommand*>& data, |
| 172 std::map<int, SessionTab*>* tabs, |
| 173 std::map<int, SessionWindow*>* windows, |
| 174 SessionID::id_type* active_window_id); |
| 175 |
| 176 scoped_ptr<BaseSessionServiceCommands> base_commands_; |
| 177 |
| 178 DISALLOW_COPY_AND_ASSIGN(SessionServiceCommands); |
| 179 }; |
| 180 |
| 181 #endif // CHROME_BROWSER_SESSIONS_SESSION_SERVICE_COMMANDS_H_ |
OLD | NEW |