| 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 SessionCommand; |
| 21 class SessionServiceCommandsDelegate; |
| 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 // The passed delegate remains owned by the caller. |
| 39 explicit SessionServiceCommands(SessionServiceCommandsDelegate* delegate); |
| 40 virtual ~SessionServiceCommands(); |
| 41 |
| 42 // Methods to create the various commands. It is up to the caller to delete |
| 43 // the returned SessionCommand* object. |
| 44 scoped_ptr<SessionCommand>CreateSetSelectedTabInWindowCommand( |
| 45 const SessionID& window_id, |
| 46 int index); |
| 47 scoped_ptr<SessionCommand> CreateSetTabWindowCommand( |
| 48 const SessionID& window_id, |
| 49 const SessionID& tab_id); |
| 50 scoped_ptr<SessionCommand>CreateSetWindowBoundsCommand( |
| 51 const SessionID& window_id, |
| 52 const gfx::Rect& bounds, |
| 53 ui::WindowShowState show_state); |
| 54 scoped_ptr<SessionCommand> CreateSetTabIndexInWindowCommand( |
| 55 const SessionID& tab_id, |
| 56 int new_index); |
| 57 scoped_ptr<SessionCommand> CreateTabClosedCommand(SessionID::id_type tab_id); |
| 58 scoped_ptr<SessionCommand> CreateWindowClosedCommand( |
| 59 SessionID::id_type tab_id); |
| 60 scoped_ptr<SessionCommand> CreateSetSelectedNavigationIndexCommand( |
| 61 const SessionID& tab_id, |
| 62 int index); |
| 63 scoped_ptr<SessionCommand> CreateSetWindowTypeCommand( |
| 64 const SessionID& window_id, |
| 65 SessionWindow::WindowType type); |
| 66 scoped_ptr<SessionCommand> CreatePinnedStateCommand(const SessionID& tab_id, |
| 67 bool is_pinned); |
| 68 scoped_ptr<SessionCommand> CreateSessionStorageAssociatedCommand( |
| 69 const SessionID& tab_id, |
| 70 const std::string& session_storage_persistent_id); |
| 71 scoped_ptr<SessionCommand> CreateSetActiveWindowCommand( |
| 72 const SessionID& window_id); |
| 73 scoped_ptr<SessionCommand> CreateTabNavigationPathPrunedFromBackCommand( |
| 74 const SessionID& tab_id, |
| 75 int count); |
| 76 scoped_ptr<SessionCommand> CreateTabNavigationPathPrunedFromFrontCommand( |
| 77 const SessionID& tab_id, |
| 78 int count); |
| 79 scoped_ptr<SessionCommand> CreateUpdateTabNavigationCommand( |
| 80 const SessionID& tab_id, |
| 81 const sessions::SerializedNavigationEntry& navigation); |
| 82 scoped_ptr<SessionCommand> CreateSetTabExtensionAppIDCommand( |
| 83 const SessionID& tab_id, |
| 84 const std::string& extension_id); |
| 85 scoped_ptr<SessionCommand> CreateSetTabUserAgentOverrideCommand( |
| 86 const SessionID& tab_id, |
| 87 const std::string& user_agent_override); |
| 88 scoped_ptr<SessionCommand> CreateSetWindowAppNameCommand( |
| 89 const SessionID& window_id, |
| 90 const std::string& app_name); |
| 91 |
| 92 // Searches for a pending command in |pending_commands| that can be replaced |
| 93 // with |command|. If one is found, pending command is removed, the command |
| 94 // is added to the pending commands (taken ownership) and true is returned. |
| 95 bool ReplacePendingCommand( |
| 96 scoped_ptr<SessionCommand>* command, |
| 97 ScopedVector<SessionCommand>& pending_commands); |
| 98 |
| 99 // Returns true if provided |command| either closes a window or a tab. |
| 100 bool IsClosingCommand(SessionCommand* command) const; |
| 101 |
| 102 // Converts the commands into SessionWindows. On return any valid |
| 103 // windows are added to valid_windows. It is up to the caller to delete |
| 104 // the windows added to valid_windows. |active_window_id| will be set with the |
| 105 // id of the last active window, but it's only valid when this id corresponds |
| 106 // to the id of one of the windows in valid_windows. |
| 107 void RestoreSessionFromCommands( |
| 108 const ScopedVector<SessionCommand>& commands, |
| 109 std::vector<SessionWindow*>* valid_windows, |
| 110 SessionID::id_type* active_window_id); |
| 111 |
| 112 private: |
| 113 typedef std::map<SessionID::id_type, SessionTab*> IdToSessionTab; |
| 114 typedef std::map<SessionID::id_type, SessionWindow*> IdToSessionWindow; |
| 115 |
| 116 // Iterates through the vector updating the selected_tab_index of each |
| 117 // SessionWindow based on the actual tabs that were restored. |
| 118 void UpdateSelectedTabIndex(std::vector<SessionWindow*>* windows); |
| 119 |
| 120 // Returns the window in windows with the specified id. If a window does |
| 121 // not exist, one is created. |
| 122 SessionWindow* GetWindow(SessionID::id_type window_id, |
| 123 IdToSessionWindow* windows); |
| 124 |
| 125 // Returns the tab with the specified id in tabs. If a tab does not exist, |
| 126 // it is created. |
| 127 SessionTab* GetTab(SessionID::id_type tab_id, |
| 128 IdToSessionTab* tabs); |
| 129 |
| 130 // Returns an iterator into navigations pointing to the navigation whose |
| 131 // index matches |index|. If no navigation index matches |index|, the first |
| 132 // navigation with an index > |index| is returned. |
| 133 // |
| 134 // This assumes the navigations are ordered by index in ascending order. |
| 135 std::vector<sessions::SerializedNavigationEntry>::iterator |
| 136 FindClosestNavigationWithIndex( |
| 137 std::vector<sessions::SerializedNavigationEntry>* navigations, |
| 138 int index); |
| 139 |
| 140 // Does the following: |
| 141 // . Deletes and removes any windows with no tabs or windows with types other |
| 142 // than tabbed_browser or browser. NOTE: constrained windows that have |
| 143 // been dragged out are of type browser. As such, this preserves any dragged |
| 144 // out constrained windows (aka popups that have been dragged out). |
| 145 // . Sorts the tabs in windows with valid tabs based on the tabs |
| 146 // visual order, and adds the valid windows to windows. |
| 147 void SortTabsBasedOnVisualOrderAndPrune( |
| 148 std::map<int, SessionWindow*>* windows, |
| 149 std::vector<SessionWindow*>* valid_windows); |
| 150 |
| 151 // Adds tabs to their parent window based on the tab's window_id. This |
| 152 // ignores tabs with no navigations. |
| 153 void AddTabsToWindows(std::map<int, SessionTab*>* tabs, |
| 154 std::map<int, SessionWindow*>* windows); |
| 155 |
| 156 // Creates tabs and windows from the commands specified in |data|. The created |
| 157 // tabs and windows are added to |tabs| and |windows| respectively, with the |
| 158 // id of the active window set in |active_window_id|. It is up to the caller |
| 159 // to delete the tabs and windows added to |tabs| and |windows|. |
| 160 // |
| 161 // This does NOT add any created SessionTabs to SessionWindow.tabs, that is |
| 162 // done by AddTabsToWindows. |
| 163 bool CreateTabsAndWindows(const ScopedVector<SessionCommand>& data, |
| 164 std::map<int, SessionTab*>* tabs, |
| 165 std::map<int, SessionWindow*>* windows, |
| 166 SessionID::id_type* active_window_id); |
| 167 |
| 168 SessionServiceCommandsDelegate* delegate_; |
| 169 |
| 170 DISALLOW_COPY_AND_ASSIGN(SessionServiceCommands); |
| 171 }; |
| 172 |
| 173 // SessionServiceCommandsDelegate ---------------------------------------------- |
| 174 |
| 175 // This delegate gets passed to the SessionServiceCommands constructor. It is |
| 176 // used to determine if a window of a certain type / app has to be used for a |
| 177 // restore operation or not. |
| 178 class SessionServiceCommandsDelegate { |
| 179 public: |
| 180 // Returns true if a window of given |window_type| and |app_type| should get |
| 181 // restored upon session restore. |
| 182 virtual bool ShouldRestoreWindowOfType( |
| 183 SessionWindow::WindowType type, |
| 184 SessionServiceCommands::AppType app_type) const = 0; |
| 185 }; |
| 186 |
| 187 #endif // CHROME_BROWSER_SESSIONS_SESSION_SERVICE_COMMANDS_H_ |
| OLD | NEW |