Chromium Code Reviews| 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" |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 32 // backend to determine the name of the files. | 32 // backend to determine the name of the files. |
| 33 enum SessionType { | 33 enum SessionType { |
| 34 SESSION_RESTORE, | 34 SESSION_RESTORE, |
| 35 TAB_RESTORE | 35 TAB_RESTORE |
| 36 }; | 36 }; |
| 37 | 37 |
| 38 typedef base::Callback<void(ScopedVector<SessionCommand>)> | 38 typedef base::Callback<void(ScopedVector<SessionCommand>)> |
| 39 GetCommandsCallback; | 39 GetCommandsCallback; |
| 40 | 40 |
| 41 // Creates a new BaseSessionService. After creation you need to invoke | 41 // Creates a new BaseSessionService. After creation you need to invoke |
| 42 // Init. | 42 // Init. |delegate| will remain owned by the creator and it is guaranteed |
| 43 // that its lifetime surpasses this class. | |
| 43 // |type| gives the type of session service, |path| the path to save files to. | 44 // |type| gives the type of session service, |path| the path to save files to. |
| 44 BaseSessionService(SessionType type, | 45 BaseSessionService(SessionType type, |
| 45 const base::FilePath& path, | 46 const base::FilePath& path, |
| 46 scoped_ptr<BaseSessionServiceDelegate> delegate); | 47 BaseSessionServiceDelegate* delegate); |
| 48 ~BaseSessionService(); | |
| 49 | |
| 50 // Moves the current session to the last session. | |
| 51 void MoveCurrentSessionToLastSession(); | |
| 47 | 52 |
| 48 // Deletes the last session. | 53 // Deletes the last session. |
| 49 void DeleteLastSession(); | 54 void DeleteLastSession(); |
| 50 | 55 |
| 51 typedef base::Callback<void(ScopedVector<SessionCommand>)> | |
| 52 InternalGetCommandsCallback; | |
| 53 | |
| 54 protected: | |
| 55 virtual ~BaseSessionService(); | |
| 56 | |
| 57 // Returns the backend. | |
| 58 SessionBackend* backend() const { return backend_.get(); } | |
| 59 | |
| 60 // Returns the set of commands which were scheduled to be written. Once | 56 // Returns the set of commands which were scheduled to be written. Once |
| 61 // committed to the backend, the commands are removed from here. | 57 // committed to the backend, the commands are removed from here. |
| 62 ScopedVector<SessionCommand>& pending_commands() { | 58 const ScopedVector<SessionCommand>& pending_commands() { |
| 63 return pending_commands_; | 59 return pending_commands_; |
| 64 } | 60 } |
| 65 | 61 |
| 66 // Whether the next save resets the file before writing to it. | 62 // Whether the next save resets the file before writing to it. |
| 67 void set_pending_reset(bool value) { pending_reset_ = value; } | 63 void set_pending_reset(bool value) { pending_reset_ = value; } |
| 68 bool pending_reset() const { return pending_reset_; } | 64 bool pending_reset() const { return pending_reset_; } |
| 69 | 65 |
| 70 // Returns the number of commands sent down since the last reset. | 66 // Returns the number of commands sent down since the last reset. |
| 71 int commands_since_reset() const { return commands_since_reset_; } | 67 int commands_since_reset() const { return commands_since_reset_; } |
| 72 | 68 |
| 73 // Schedules a command. This adds |command| to pending_commands_ and | 69 // Schedules a command. This adds |command| to pending_commands_ and |
| 74 // invokes StartSaveTimer to start a timer that invokes Save at a later | 70 // invokes StartSaveTimer to start a timer that invokes Save at a later |
| 75 // time. | 71 // time. |
| 76 virtual void ScheduleCommand(scoped_ptr<SessionCommand> command); | 72 void ScheduleCommand(scoped_ptr<SessionCommand> command); |
| 73 | |
| 74 // Appends a command as part of a general rebuild. This will neither count | |
| 75 // against a rebuild, nor will it trigger a save of commands. | |
| 76 void AppendRebuildCommand(scoped_ptr<SessionCommand> command); | |
| 77 | |
| 78 // Erase the |old_command| from the list of commands. | |
| 79 // The passed command will automatically be deleted. | |
| 80 void EraseCommand(scoped_ptr<SessionCommand> old_command); | |
|
sky
2014/11/03 22:48:03
It doesn't make sense for this to take a scoped_pt
Mr4D (OOO till 08-26)
2014/11/03 23:04:53
I had it this way first but then thought that sinc
| |
| 81 | |
| 82 // Swap a |new_command| into the list of queued commands at the location of | |
| 83 // the |old_command|. The |old_command| will be automatically deleted in the | |
| 84 // process. | |
| 85 void SwapCommand(scoped_ptr<SessionCommand> old_command, | |
|
sky
2014/11/03 22:48:02
old_command shouldn't be scoped here either, new_c
Mr4D (OOO till 08-26)
2014/11/03 23:04:53
Done.
| |
| 86 scoped_ptr<SessionCommand> new_command); | |
| 87 | |
| 88 // Clears all commands from the list. | |
| 89 void ClearPendingCommands(); | |
| 77 | 90 |
| 78 // Starts the timer that invokes Save (if timer isn't already running). | 91 // Starts the timer that invokes Save (if timer isn't already running). |
| 79 void StartSaveTimer(); | 92 void StartSaveTimer(); |
| 80 | 93 |
| 81 // Saves pending commands to the backend. This is invoked from the timer | 94 // Passes all pending commands to the backend for saving. |
| 82 // scheduled by StartSaveTimer. | 95 void Save(); |
| 83 virtual void Save(); | |
| 84 | 96 |
| 85 // Returns true if the entry at specified |url| should be written to disk. | 97 // Uses the backend to load the last session commands from disc. |callback| |
| 86 bool ShouldTrackEntry(const GURL& url); | 98 // gets called once the data has arrived. |
| 99 base::CancelableTaskTracker::TaskId ScheduleGetLastSessionCommands( | |
| 100 const GetCommandsCallback& callback, | |
| 101 base::CancelableTaskTracker* tracker); | |
| 87 | 102 |
| 88 // Invokes SessionBackend::ReadLastSessionCommands with callback on the | 103 private: |
| 89 // backend thread. | 104 friend class BetterSessionRestoreCrashTest; |
| 90 // If testing, SessionBackend::ReadLastSessionCommands is invoked directly. | 105 friend class SessionServiceTestHelper; |
| 91 base::CancelableTaskTracker::TaskId ScheduleGetLastSessionCommands( | 106 friend class NoStartupWindowTest; |
| 92 const InternalGetCommandsCallback& callback, | |
| 93 base::CancelableTaskTracker* tracker); | |
| 94 | 107 |
| 95 // This posts the task to the SequencedWorkerPool, or run immediately | 108 // This posts the task to the SequencedWorkerPool, or run immediately |
| 96 // if the SequencedWorkerPool has been shutdown. | 109 // if the SequencedWorkerPool has been shutdown. |
| 97 void RunTaskOnBackendThread(const tracked_objects::Location& from_here, | 110 void RunTaskOnBackendThread(const tracked_objects::Location& from_here, |
| 98 const base::Closure& task); | 111 const base::Closure& task); |
| 99 | 112 |
| 100 // Max number of navigation entries in each direction we'll persist. | 113 // Used by unit tests it returns true when the backend got initialized. |
| 101 static const int max_persist_navigation_count; | 114 bool BackendInitializedForTest(); |
| 102 | 115 |
| 103 private: | 116 // Returns true if any commands got processed yet (used by unit tests). |
| 104 friend class BetterSessionRestoreCrashTest; | 117 bool ProcessedAnyCommandsForTest(); |
| 105 | 118 |
| 106 // The backend. | 119 // Read the last session commands directly from file. |
| 120 bool ReadLastSessionCommandsForTest(ScopedVector<SessionCommand>* commands); | |
| 121 | |
| 122 // The backend object which reads and saves commands. | |
| 107 scoped_refptr<SessionBackend> backend_; | 123 scoped_refptr<SessionBackend> backend_; |
| 108 | 124 |
| 109 // Commands we need to send over to the backend. | 125 // Commands we need to send over to the backend. |
| 110 ScopedVector<SessionCommand> pending_commands_; | 126 ScopedVector<SessionCommand> pending_commands_; |
| 111 | 127 |
| 112 // Whether the backend file should be recreated the next time we send | 128 // Whether the backend file should be recreated the next time we send |
| 113 // over the commands. | 129 // over the commands. |
| 114 bool pending_reset_; | 130 bool pending_reset_; |
| 115 | 131 |
| 116 // The number of commands sent to the backend before doing a reset. | 132 // The number of commands sent to the backend before doing a reset. |
| 117 int commands_since_reset_; | 133 int commands_since_reset_; |
| 118 | 134 |
| 119 scoped_ptr<BaseSessionServiceDelegate> delegate_; | 135 BaseSessionServiceDelegate* delegate_; |
| 120 | 136 |
| 121 // A token to make sure that all tasks will be serialized. | 137 // A token to make sure that all tasks will be serialized. |
| 122 base::SequencedWorkerPool::SequenceToken sequence_token_; | 138 base::SequencedWorkerPool::SequenceToken sequence_token_; |
| 123 | 139 |
| 124 // Used to invoke Save. | 140 // Used to invoke Save. |
| 125 base::WeakPtrFactory<BaseSessionService> weak_factory_; | 141 base::WeakPtrFactory<BaseSessionService> weak_factory_; |
| 126 | 142 |
| 127 DISALLOW_COPY_AND_ASSIGN(BaseSessionService); | 143 DISALLOW_COPY_AND_ASSIGN(BaseSessionService); |
| 128 }; | 144 }; |
| 129 | 145 |
| 130 #endif // CHROME_BROWSER_SESSIONS_BASE_SESSION_SERVICE_H_ | 146 #endif // CHROME_BROWSER_SESSIONS_BASE_SESSION_SERVICE_H_ |
| OLD | NEW |