| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_SESSIONS_BASE_SESSION_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_SESSIONS_BASE_SESSION_SERVICE_H_ |
| 6 #define CHROME_BROWSER_SESSIONS_BASE_SESSION_SERVICE_H_ | 6 #define CHROME_BROWSER_SESSIONS_BASE_SESSION_SERVICE_H_ |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
| 12 #include "base/location.h" | |
| 13 #include "base/memory/ref_counted.h" | |
| 14 #include "base/memory/scoped_vector.h" | 12 #include "base/memory/scoped_vector.h" |
| 15 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 16 #include "base/task/cancelable_task_tracker.h" | 14 #include "base/task/cancelable_task_tracker.h" |
| 17 #include "base/threading/sequenced_worker_pool.h" | |
| 18 #include "components/sessions/session_id.h" | |
| 19 #include "url/gurl.h" | 15 #include "url/gurl.h" |
| 20 | 16 |
| 21 class BaseSessionServiceDelegate; | 17 class BaseSessionServiceDelegate; |
| 22 class SessionBackend; | 18 class SessionBackend; |
| 23 class SessionCommand; | 19 class SessionCommand; |
| 24 | 20 |
| 25 namespace sessions { | 21 namespace sessions { |
| 26 class SerializedNavigationEntry; | 22 class SerializedNavigationEntry; |
| 27 } | 23 } |
| 28 | 24 |
| 29 // BaseSessionService is the super class of both tab restore service and | 25 // BaseSessionService is the super class of both tab restore service and |
| 30 // session service. It contains commonality needed by both, in particular | 26 // session service. It contains commonality needed by both, in particular |
| 31 // it manages a set of SessionCommands that are periodically sent to a | 27 // it manages a set of SessionCommands that are periodically sent to a |
| 32 // SessionBackend. | 28 // SessionBackend. |
| 33 class BaseSessionService { | 29 class BaseSessionService { |
| 34 public: | 30 public: |
| 35 // Identifies the type of session service this is. This is used by the | 31 // Identifies the type of session service this is. This is used by the |
| 36 // backend to determine the name of the files. | 32 // backend to determine the name of the files. |
| 37 enum SessionType { | 33 enum SessionType { |
| 38 SESSION_RESTORE, | 34 SESSION_RESTORE, |
| 39 TAB_RESTORE | 35 TAB_RESTORE |
| 40 }; | 36 }; |
| 41 | 37 |
| 38 typedef base::Callback<void(ScopedVector<SessionCommand>)> |
| 39 GetCommandsCallback; |
| 40 |
| 41 // Max number of navigation entries in each direction we'll persist. |
| 42 static const int max_persist_navigation_count; |
| 43 |
| 42 // Creates a new BaseSessionService. After creation you need to invoke | 44 // Creates a new BaseSessionService. After creation you need to invoke |
| 43 // Init. | 45 // Init. |delegate| will remain owned by the creator and it is guaranteed |
| 46 // that its lifetime surpasses this class. |
| 44 // |type| gives the type of session service, |path| the path to save files to. | 47 // |type| gives the type of session service, |path| the path to save files to. |
| 45 BaseSessionService(SessionType type, | 48 BaseSessionService(SessionType type, |
| 46 const base::FilePath& path, | 49 const base::FilePath& path, |
| 47 scoped_ptr<BaseSessionServiceDelegate> delegate); | 50 BaseSessionServiceDelegate* delegate); |
| 51 virtual ~BaseSessionService(); |
| 52 |
| 53 // Schedules a command. This adds |command| to pending_commands_ and |
| 54 // invokes StartSaveTimer to start a timer that invokes Save at a later |
| 55 // time. |
| 56 virtual void ScheduleCommand(scoped_ptr<SessionCommand> command); |
| 57 |
| 58 // Starts the timer that invokes Save (if timer isn't already running). |
| 59 void StartSaveTimer(); |
| 48 | 60 |
| 49 // Deletes the last session. | 61 // Deletes the last session. |
| 50 void DeleteLastSession(); | 62 void DeleteLastSession(); |
| 51 | 63 |
| 52 typedef base::Callback<void(ScopedVector<SessionCommand>)> | 64 // Moves the current session to the last session. |
| 53 InternalGetCommandsCallback; | 65 void MoveCurrentSessionToLastSession(); |
| 54 | 66 |
| 55 protected: | 67 // Schedules to save all pending commands to the backend immediately (if a |
| 56 virtual ~BaseSessionService(); | 68 // backend exists). |
| 69 virtual void SaveNow(); |
| 57 | 70 |
| 58 // Returns the backend. | 71 // Uses the backend to load the last session commands from disc. |callback| |
| 59 SessionBackend* backend() const { return backend_.get(); } | 72 // gets called once the data has arrived. |
| 60 | 73 base::CancelableTaskTracker::TaskId ScheduleGetLastSessionCommands( |
| 61 // Returns the set of commands that needed to be scheduled. The commands | 74 const GetCommandsCallback& callback, |
| 62 // in the vector are owned by BaseSessionService, until they are scheduled | 75 base::CancelableTaskTracker* tracker); |
| 63 // on the backend at which point the backend owns the commands. | |
| 64 std::vector<SessionCommand*>& pending_commands() { | |
| 65 return pending_commands_; | |
| 66 } | |
| 67 | 76 |
| 68 // Whether the next save resets the file before writing to it. | 77 // Whether the next save resets the file before writing to it. |
| 69 void set_pending_reset(bool value) { pending_reset_ = value; } | 78 void set_pending_reset(bool value) { pending_reset_ = value; } |
| 70 bool pending_reset() const { return pending_reset_; } | 79 bool pending_reset() const { return pending_reset_; } |
| 71 | 80 |
| 72 // Returns the number of commands sent down since the last reset. | 81 // Returns the number of commands sent down since the last reset. |
| 73 int commands_since_reset() const { return commands_since_reset_; } | 82 int commands_since_reset() const { return commands_since_reset_; } |
| 74 | 83 |
| 75 // Schedules a command. This adds |command| to pending_commands_ and | 84 // Returns the set of commands which were scheduled to be written. Once |
| 76 // invokes StartSaveTimer to start a timer that invokes Save at a later | 85 // committed to the backend, the commands are removed from here. |
| 77 // time. | 86 ScopedVector<SessionCommand>& pending_commands() { |
| 78 virtual void ScheduleCommand(SessionCommand* command); | 87 return pending_commands_; |
| 88 } |
| 79 | 89 |
| 80 // Starts the timer that invokes Save (if timer isn't already running). | 90 // Returns true if any commands got processed yet (used by unit tests). |
| 81 void StartSaveTimer(); | 91 bool ProcessedAnyCommandsForTest(); |
| 82 | 92 |
| 83 // Saves pending commands to the backend. This is invoked from the timer | 93 private: |
| 84 // scheduled by StartSaveTimer. | 94 friend class BetterSessionRestoreCrashTest; |
| 85 virtual void Save(); | 95 friend class SessionServiceTestHelper; |
| 86 | 96 |
| 87 // Creates a SessionCommand that represents a navigation. | 97 // Saves pending commands to the backend immediately. This gets invoked by the |
| 88 SessionCommand* CreateUpdateTabNavigationCommand( | 98 // timer scheduled by StartSaveTimer - or via SaveNow(). |
| 89 SessionID::id_type command_id, | 99 void SaveInternal(); |
| 90 SessionID::id_type tab_id, | |
| 91 const sessions::SerializedNavigationEntry& navigation); | |
| 92 | 100 |
| 93 // Creates a SessionCommand that represents marking a tab as an application. | 101 // Returns the backend. |
| 94 SessionCommand* CreateSetTabExtensionAppIDCommand( | 102 SessionBackend* backend() const { return backend_.get(); } |
| 95 SessionID::id_type command_id, | |
| 96 SessionID::id_type tab_id, | |
| 97 const std::string& extension_id); | |
| 98 | |
| 99 // Creates a SessionCommand that containing user agent override used by a | |
| 100 // tab's navigations. | |
| 101 SessionCommand* CreateSetTabUserAgentOverrideCommand( | |
| 102 SessionID::id_type command_id, | |
| 103 SessionID::id_type tab_id, | |
| 104 const std::string& user_agent_override); | |
| 105 | |
| 106 // Creates a SessionCommand stores a browser window's app name. | |
| 107 SessionCommand* CreateSetWindowAppNameCommand( | |
| 108 SessionID::id_type command_id, | |
| 109 SessionID::id_type window_id, | |
| 110 const std::string& app_name); | |
| 111 | |
| 112 // Converts a SessionCommand previously created by | |
| 113 // CreateUpdateTabNavigationCommand into a | |
| 114 // sessions::SerializedNavigationEntry. Returns true on success. If | |
| 115 // successful |tab_id| is set to the id of the restored tab. | |
| 116 bool RestoreUpdateTabNavigationCommand( | |
| 117 const SessionCommand& command, | |
| 118 sessions::SerializedNavigationEntry* navigation, | |
| 119 SessionID::id_type* tab_id); | |
| 120 | |
| 121 // Extracts a SessionCommand as previously created by | |
| 122 // CreateSetTabExtensionAppIDCommand into the tab id and application | |
| 123 // extension id. | |
| 124 bool RestoreSetTabExtensionAppIDCommand( | |
| 125 const SessionCommand& command, | |
| 126 SessionID::id_type* tab_id, | |
| 127 std::string* extension_app_id); | |
| 128 | |
| 129 // Extracts a SessionCommand as previously created by | |
| 130 // CreateSetTabUserAgentOverrideCommand into the tab id and user agent. | |
| 131 bool RestoreSetTabUserAgentOverrideCommand( | |
| 132 const SessionCommand& command, | |
| 133 SessionID::id_type* tab_id, | |
| 134 std::string* user_agent_override); | |
| 135 | |
| 136 // Extracts a SessionCommand as previously created by | |
| 137 // CreateSetWindowAppNameCommand into the window id and application name. | |
| 138 bool RestoreSetWindowAppNameCommand( | |
| 139 const SessionCommand& command, | |
| 140 SessionID::id_type* window_id, | |
| 141 std::string* app_name); | |
| 142 | |
| 143 // Returns true if the entry at specified |url| should be written to disk. | |
| 144 bool ShouldTrackEntry(const GURL& url); | |
| 145 | |
| 146 // Invokes SessionBackend::ReadLastSessionCommands with callback on the | |
| 147 // backend thread. | |
| 148 // If testing, SessionBackend::ReadLastSessionCommands is invoked directly. | |
| 149 base::CancelableTaskTracker::TaskId ScheduleGetLastSessionCommands( | |
| 150 const InternalGetCommandsCallback& callback, | |
| 151 base::CancelableTaskTracker* tracker); | |
| 152 | 103 |
| 153 // This posts the task to the SequencedWorkerPool, or run immediately | 104 // This posts the task to the SequencedWorkerPool, or run immediately |
| 154 // if the SequencedWorkerPool has been shutdown. | 105 // if the SequencedWorkerPool has been shutdown. |
| 155 void RunTaskOnBackendThread(const tracked_objects::Location& from_here, | 106 void RunTaskOnBackendThread(const tracked_objects::Location& from_here, |
| 156 const base::Closure& task); | 107 const base::Closure& task); |
| 157 | 108 |
| 158 // Max number of navigation entries in each direction we'll persist. | 109 // The backend object which reads and saves commands. |
| 159 static const int max_persist_navigation_count; | |
| 160 | |
| 161 private: | |
| 162 friend class BetterSessionRestoreCrashTest; | |
| 163 | |
| 164 // The backend. | |
| 165 scoped_refptr<SessionBackend> backend_; | 110 scoped_refptr<SessionBackend> backend_; |
| 166 | 111 |
| 167 // Commands we need to send over to the backend. | 112 // Commands we need to send over to the backend. |
| 168 std::vector<SessionCommand*> pending_commands_; | 113 ScopedVector<SessionCommand> pending_commands_; |
| 169 | 114 |
| 170 // Whether the backend file should be recreated the next time we send | 115 // Whether the backend file should be recreated the next time we send |
| 171 // over the commands. | 116 // over the commands. |
| 172 bool pending_reset_; | 117 bool pending_reset_; |
| 173 | 118 |
| 174 // The number of commands sent to the backend before doing a reset. | 119 // The number of commands sent to the backend before doing a reset. |
| 175 int commands_since_reset_; | 120 int commands_since_reset_; |
| 176 | 121 |
| 177 scoped_ptr<BaseSessionServiceDelegate> delegate_; | 122 BaseSessionServiceDelegate* delegate_; |
| 178 | 123 |
| 179 // A token to make sure that all tasks will be serialized. | 124 // A token to make sure that all tasks will be serialized. |
| 180 base::SequencedWorkerPool::SequenceToken sequence_token_; | 125 base::SequencedWorkerPool::SequenceToken sequence_token_; |
| 181 | 126 |
| 182 // Used to invoke Save. | 127 // Used to invoke Save. |
| 183 base::WeakPtrFactory<BaseSessionService> weak_factory_; | 128 base::WeakPtrFactory<BaseSessionService> weak_factory_; |
| 184 | 129 |
| 185 DISALLOW_COPY_AND_ASSIGN(BaseSessionService); | 130 DISALLOW_COPY_AND_ASSIGN(BaseSessionService); |
| 186 }; | 131 }; |
| 187 | 132 |
| 188 #endif // CHROME_BROWSER_SESSIONS_BASE_SESSION_SERVICE_H_ | 133 #endif // CHROME_BROWSER_SESSIONS_BASE_SESSION_SERVICE_H_ |
| OLD | NEW |