| OLD | NEW |
| (Empty) |
| 1 // Copyright 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 COMPONENTS_SESSIONS_BASE_SESSION_SERVICE_H_ | |
| 6 #define COMPONENTS_SESSIONS_BASE_SESSION_SERVICE_H_ | |
| 7 | |
| 8 #include "base/basictypes.h" | |
| 9 #include "base/callback.h" | |
| 10 #include "base/files/file_path.h" | |
| 11 #include "base/gtest_prod_util.h" | |
| 12 #include "base/memory/scoped_vector.h" | |
| 13 #include "base/memory/weak_ptr.h" | |
| 14 #include "base/task/cancelable_task_tracker.h" | |
| 15 #include "components/sessions/sessions_export.h" | |
| 16 #include "url/gurl.h" | |
| 17 | |
| 18 | |
| 19 namespace sessions { | |
| 20 class BaseSessionServiceDelegate; | |
| 21 class SerializedNavigationEntry; | |
| 22 class SessionCommand; | |
| 23 class SessionBackend; | |
| 24 | |
| 25 // BaseSessionService is the super class of both tab restore service and | |
| 26 // session service. It contains commonality needed by both, in particular | |
| 27 // it manages a set of SessionCommands that are periodically sent to a | |
| 28 // SessionBackend. | |
| 29 class SESSIONS_EXPORT BaseSessionService { | |
| 30 public: | |
| 31 // Identifies the type of session service this is. This is used by the | |
| 32 // backend to determine the name of the files. | |
| 33 enum SessionType { | |
| 34 SESSION_RESTORE, | |
| 35 TAB_RESTORE | |
| 36 }; | |
| 37 | |
| 38 typedef base::Callback<void(ScopedVector<SessionCommand>)> | |
| 39 GetCommandsCallback; | |
| 40 | |
| 41 // Creates a new BaseSessionService. After creation you need to invoke | |
| 42 // Init. |delegate| will remain owned by the creator and it is guaranteed | |
| 43 // that its lifetime surpasses this class. | |
| 44 // |type| gives the type of session service, |path| the path to save files to. | |
| 45 BaseSessionService(SessionType type, | |
| 46 const base::FilePath& path, | |
| 47 BaseSessionServiceDelegate* delegate); | |
| 48 ~BaseSessionService(); | |
| 49 | |
| 50 // Moves the current session to the last session. | |
| 51 void MoveCurrentSessionToLastSession(); | |
| 52 | |
| 53 // Deletes the last session. | |
| 54 void DeleteLastSession(); | |
| 55 | |
| 56 // Returns the set of commands which were scheduled to be written. Once | |
| 57 // committed to the backend, the commands are removed from here. | |
| 58 const ScopedVector<SessionCommand>& pending_commands() { | |
| 59 return pending_commands_; | |
| 60 } | |
| 61 | |
| 62 // Whether the next save resets the file before writing to it. | |
| 63 void set_pending_reset(bool value) { pending_reset_ = value; } | |
| 64 bool pending_reset() const { return pending_reset_; } | |
| 65 | |
| 66 // Returns the number of commands sent down since the last reset. | |
| 67 int commands_since_reset() const { return commands_since_reset_; } | |
| 68 | |
| 69 // Schedules a command. This adds |command| to pending_commands_ and | |
| 70 // invokes StartSaveTimer to start a timer that invokes Save at a later | |
| 71 // time. | |
| 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(SessionCommand* old_command); | |
| 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(SessionCommand* old_command, | |
| 86 scoped_ptr<SessionCommand> new_command); | |
| 87 | |
| 88 // Clears all commands from the list. | |
| 89 void ClearPendingCommands(); | |
| 90 | |
| 91 // Starts the timer that invokes Save (if timer isn't already running). | |
| 92 void StartSaveTimer(); | |
| 93 | |
| 94 // Passes all pending commands to the backend for saving. | |
| 95 void Save(); | |
| 96 | |
| 97 // Uses the backend to load the last session commands from disc. |callback| | |
| 98 // gets called once the data has arrived. | |
| 99 base::CancelableTaskTracker::TaskId ScheduleGetLastSessionCommands( | |
| 100 const GetCommandsCallback& callback, | |
| 101 base::CancelableTaskTracker* tracker); | |
| 102 | |
| 103 private: | |
| 104 friend class BaseSessionServiceTestHelper; | |
| 105 | |
| 106 // This posts the task to the SequencedWorkerPool, or run immediately | |
| 107 // if the SequencedWorkerPool has been shutdown. | |
| 108 void RunTaskOnBackendThread(const tracked_objects::Location& from_here, | |
| 109 const base::Closure& task); | |
| 110 | |
| 111 // The backend object which reads and saves commands. | |
| 112 scoped_refptr<SessionBackend> backend_; | |
| 113 | |
| 114 // Commands we need to send over to the backend. | |
| 115 ScopedVector<SessionCommand> pending_commands_; | |
| 116 | |
| 117 // Whether the backend file should be recreated the next time we send | |
| 118 // over the commands. | |
| 119 bool pending_reset_; | |
| 120 | |
| 121 // The number of commands sent to the backend before doing a reset. | |
| 122 int commands_since_reset_; | |
| 123 | |
| 124 BaseSessionServiceDelegate* delegate_; | |
| 125 | |
| 126 // A token to make sure that all tasks will be serialized. | |
| 127 base::SequencedWorkerPool::SequenceToken sequence_token_; | |
| 128 | |
| 129 // Used to invoke Save. | |
| 130 base::WeakPtrFactory<BaseSessionService> weak_factory_; | |
| 131 | |
| 132 DISALLOW_COPY_AND_ASSIGN(BaseSessionService); | |
| 133 }; | |
| 134 | |
| 135 } // namespace sessions | |
| 136 | |
| 137 #endif // COMPONENTS_SESSIONS_BASE_SESSION_SERVICE_H_ | |
| OLD | NEW |