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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_SESSIONS_BASE_SESSION_SERVICE_H_
6 #define CHROME_BROWSER_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/location.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_vector.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/task/cancelable_task_tracker.h"
17 #include "base/threading/sequenced_worker_pool.h"
18 #include "components/sessions/session_id.h"
26 class SerializedNavigationEntry
;
29 // BaseSessionService is the super class of both tab restore service and
30 // session service. It contains commonality needed by both, in particular
31 // it manages a set of SessionCommands that are periodically sent to a
33 class BaseSessionService
{
35 // Identifies the type of session service this is. This is used by the
36 // backend to determine the name of the files.
42 // Creates a new BaseSessionService. After creation you need to invoke
44 // |type| gives the type of session service, |profile| the profile and
45 // |path| the path to save files to. If |profile| is non-NULL, |path| is
46 // ignored and instead the path comes from the profile.
47 BaseSessionService(SessionType type
,
49 const base::FilePath
& path
);
51 Profile
* profile() const { return profile_
; }
53 // Deletes the last session.
54 void DeleteLastSession();
56 typedef base::Callback
<void(ScopedVector
<SessionCommand
>)>
57 InternalGetCommandsCallback
;
60 virtual ~BaseSessionService();
62 // Returns the backend.
63 SessionBackend
* backend() const { return backend_
.get(); }
65 // Returns the set of commands that needed to be scheduled. The commands
66 // in the vector are owned by BaseSessionService, until they are scheduled
67 // on the backend at which point the backend owns the commands.
68 std::vector
<SessionCommand
*>& pending_commands() {
69 return pending_commands_
;
72 // Whether the next save resets the file before writing to it.
73 void set_pending_reset(bool value
) { pending_reset_
= value
; }
74 bool pending_reset() const { return pending_reset_
; }
76 // Returns the number of commands sent down since the last reset.
77 int commands_since_reset() const { return commands_since_reset_
; }
79 // Schedules a command. This adds |command| to pending_commands_ and
80 // invokes StartSaveTimer to start a timer that invokes Save at a later
82 virtual void ScheduleCommand(SessionCommand
* command
);
84 // Starts the timer that invokes Save (if timer isn't already running).
85 void StartSaveTimer();
87 // Saves pending commands to the backend. This is invoked from the timer
88 // scheduled by StartSaveTimer.
91 // Creates a SessionCommand that represents a navigation.
92 SessionCommand
* CreateUpdateTabNavigationCommand(
93 SessionID::id_type command_id
,
94 SessionID::id_type tab_id
,
95 const sessions::SerializedNavigationEntry
& navigation
);
97 // Creates a SessionCommand that represents marking a tab as an application.
98 SessionCommand
* CreateSetTabExtensionAppIDCommand(
99 SessionID::id_type command_id
,
100 SessionID::id_type tab_id
,
101 const std::string
& extension_id
);
103 // Creates a SessionCommand that containing user agent override used by a
104 // tab's navigations.
105 SessionCommand
* CreateSetTabUserAgentOverrideCommand(
106 SessionID::id_type command_id
,
107 SessionID::id_type tab_id
,
108 const std::string
& user_agent_override
);
110 // Creates a SessionCommand stores a browser window's app name.
111 SessionCommand
* CreateSetWindowAppNameCommand(
112 SessionID::id_type command_id
,
113 SessionID::id_type window_id
,
114 const std::string
& app_name
);
116 // Converts a SessionCommand previously created by
117 // CreateUpdateTabNavigationCommand into a
118 // sessions::SerializedNavigationEntry. Returns true on success. If
119 // successful |tab_id| is set to the id of the restored tab.
120 bool RestoreUpdateTabNavigationCommand(
121 const SessionCommand
& command
,
122 sessions::SerializedNavigationEntry
* navigation
,
123 SessionID::id_type
* tab_id
);
125 // Extracts a SessionCommand as previously created by
126 // CreateSetTabExtensionAppIDCommand into the tab id and application
128 bool RestoreSetTabExtensionAppIDCommand(
129 const SessionCommand
& command
,
130 SessionID::id_type
* tab_id
,
131 std::string
* extension_app_id
);
133 // Extracts a SessionCommand as previously created by
134 // CreateSetTabUserAgentOverrideCommand into the tab id and user agent.
135 bool RestoreSetTabUserAgentOverrideCommand(
136 const SessionCommand
& command
,
137 SessionID::id_type
* tab_id
,
138 std::string
* user_agent_override
);
140 // Extracts a SessionCommand as previously created by
141 // CreateSetWindowAppNameCommand into the window id and application name.
142 bool RestoreSetWindowAppNameCommand(
143 const SessionCommand
& command
,
144 SessionID::id_type
* window_id
,
145 std::string
* app_name
);
147 // Returns true if the entry at specified |url| should be written to disk.
148 bool ShouldTrackEntry(const GURL
& url
);
150 // Invokes SessionBackend::ReadLastSessionCommands with callback on the
152 // If testing, SessionBackend::ReadLastSessionCommands is invoked directly.
153 base::CancelableTaskTracker::TaskId
ScheduleGetLastSessionCommands(
154 const InternalGetCommandsCallback
& callback
,
155 base::CancelableTaskTracker
* tracker
);
157 // This posts the task to the SequencedWorkerPool, or run immediately
158 // if the SequencedWorkerPool has been shutdown.
159 bool RunTaskOnBackendThread(const tracked_objects::Location
& from_here
,
160 const base::Closure
& task
);
162 // Max number of navigation entries in each direction we'll persist.
163 static const int max_persist_navigation_count
;
166 friend class BetterSessionRestoreCrashTest
;
168 // The profile. This may be null during testing.
172 scoped_refptr
<SessionBackend
> backend_
;
174 // Used to invoke Save.
175 base::WeakPtrFactory
<BaseSessionService
> weak_factory_
;
177 // Commands we need to send over to the backend.
178 std::vector
<SessionCommand
*> pending_commands_
;
180 // Whether the backend file should be recreated the next time we send
181 // over the commands.
184 // The number of commands sent to the backend before doing a reset.
185 int commands_since_reset_
;
187 // A token to make sure that all tasks will be serialized.
188 base::SequencedWorkerPool::SequenceToken sequence_token_
;
190 DISALLOW_COPY_AND_ASSIGN(BaseSessionService
);
193 #endif // CHROME_BROWSER_SESSIONS_BASE_SESSION_SERVICE_H_