Makes the view manager client lib remove children when embedding
[chromium-blink-merge.git] / components / sessions / base_session_service.h
blob2f4d32edb83ff18db03360562eb5406d251b13dd
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.
5 #ifndef COMPONENTS_SESSIONS_BASE_SESSION_SERVICE_H_
6 #define COMPONENTS_SESSIONS_BASE_SESSION_SERVICE_H_
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"
19 namespace sessions {
20 class BaseSessionServiceDelegate;
21 class SerializedNavigationEntry;
22 class SessionCommand;
23 class SessionBackend;
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
38 typedef base::Callback<void(ScopedVector<SessionCommand>)>
39 GetCommandsCallback;
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();
50 // Moves the current session to the last session.
51 void MoveCurrentSessionToLastSession();
53 // Deletes the last session.
54 void DeleteLastSession();
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_;
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_; }
66 // Returns the number of commands sent down since the last reset.
67 int commands_since_reset() const { return commands_since_reset_; }
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);
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);
78 // Erase the |old_command| from the list of commands.
79 // The passed command will automatically be deleted.
80 void EraseCommand(SessionCommand* old_command);
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);
88 // Clears all commands from the list.
89 void ClearPendingCommands();
91 // Starts the timer that invokes Save (if timer isn't already running).
92 void StartSaveTimer();
94 // Passes all pending commands to the backend for saving.
95 void Save();
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);
103 private:
104 friend class BaseSessionServiceTestHelper;
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);
111 // The backend object which reads and saves commands.
112 scoped_refptr<SessionBackend> backend_;
114 // Commands we need to send over to the backend.
115 ScopedVector<SessionCommand> pending_commands_;
117 // Whether the backend file should be recreated the next time we send
118 // over the commands.
119 bool pending_reset_;
121 // The number of commands sent to the backend before doing a reset.
122 int commands_since_reset_;
124 BaseSessionServiceDelegate* delegate_;
126 // A token to make sure that all tasks will be serialized.
127 base::SequencedWorkerPool::SequenceToken sequence_token_;
129 // Used to invoke Save.
130 base::WeakPtrFactory<BaseSessionService> weak_factory_;
132 DISALLOW_COPY_AND_ASSIGN(BaseSessionService);
135 } // namespace sessions
137 #endif // COMPONENTS_SESSIONS_BASE_SESSION_SERVICE_H_