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 "chrome/browser/common/cancelable_request.h"
17 #include "chrome/browser/sessions/session_id.h"
18 #include "chrome/common/cancelable_task_tracker.h"
19 #include "googleurl/src/gurl.h"
26 // BaseSessionService is the super class of both tab restore service and
27 // session service. It contains commonality needed by both, in particular
28 // it manages a set of SessionCommands that are periodically sent to a
30 class BaseSessionService
: public CancelableRequestProvider
{
32 // Identifies the type of session service this is. This is used by the
33 // backend to determine the name of the files.
39 // Creates a new BaseSessionService. After creation you need to invoke
41 // |type| gives the type of session service, |profile| the profile and
42 // |path| the path to save files to. If |profile| is non-NULL, |path| is
43 // ignored and instead the path comes from the profile.
44 BaseSessionService(SessionType type
,
46 const base::FilePath
& path
);
48 Profile
* profile() const { return profile_
; }
50 // Deletes the last session.
51 void DeleteLastSession();
53 typedef base::Callback
<void(ScopedVector
<SessionCommand
>)>
54 InternalGetCommandsCallback
;
57 virtual ~BaseSessionService();
59 // Returns the backend.
60 SessionBackend
* backend() const { return backend_
; }
62 // Returns the set of commands that needed to be scheduled. The commands
63 // in the vector are owned by BaseSessionService, until they are scheduled
64 // on the backend at which point the backend owns the commands.
65 std::vector
<SessionCommand
*>& pending_commands() {
66 return pending_commands_
;
69 // Whether the next save resets the file before writing to it.
70 void set_pending_reset(bool value
) { pending_reset_
= value
; }
71 bool pending_reset() const { return pending_reset_
; }
73 // Returns the number of commands sent down since the last reset.
74 int commands_since_reset() const { return commands_since_reset_
; }
76 // Schedules a command. This adds |command| to pending_commands_ and
77 // invokes StartSaveTimer to start a timer that invokes Save at a later
79 virtual void ScheduleCommand(SessionCommand
* command
);
81 // Starts the timer that invokes Save (if timer isn't already running).
82 void StartSaveTimer();
84 // Saves pending commands to the backend. This is invoked from the timer
85 // scheduled by StartSaveTimer.
88 // Creates a SessionCommand that represents a navigation.
89 SessionCommand
* CreateUpdateTabNavigationCommand(
90 SessionID::id_type command_id
,
91 SessionID::id_type tab_id
,
92 const TabNavigation
& navigation
);
94 // Creates a SessionCommand that represents marking a tab as an application.
95 SessionCommand
* CreateSetTabExtensionAppIDCommand(
96 SessionID::id_type command_id
,
97 SessionID::id_type tab_id
,
98 const std::string
& extension_id
);
100 // Creates a SessionCommand that containing user agent override used by a
101 // tab's navigations.
102 SessionCommand
* CreateSetTabUserAgentOverrideCommand(
103 SessionID::id_type command_id
,
104 SessionID::id_type tab_id
,
105 const std::string
& user_agent_override
);
107 // Creates a SessionCommand stores a browser window's app name.
108 SessionCommand
* CreateSetWindowAppNameCommand(
109 SessionID::id_type command_id
,
110 SessionID::id_type window_id
,
111 const std::string
& app_name
);
113 // Converts a SessionCommand previously created by
114 // CreateUpdateTabNavigationCommand into a TabNavigation. Returns true
115 // on success. If successful |tab_id| is set to the id of the restored tab.
116 bool RestoreUpdateTabNavigationCommand(const SessionCommand
& command
,
117 TabNavigation
* navigation
,
118 SessionID::id_type
* tab_id
);
120 // Extracts a SessionCommand as previously created by
121 // CreateSetTabExtensionAppIDCommand into the tab id and application
123 bool RestoreSetTabExtensionAppIDCommand(
124 const SessionCommand
& command
,
125 SessionID::id_type
* tab_id
,
126 std::string
* extension_app_id
);
128 // Extracts a SessionCommand as previously created by
129 // CreateSetTabUserAgentOverrideCommand into the tab id and user agent.
130 bool RestoreSetTabUserAgentOverrideCommand(
131 const SessionCommand
& command
,
132 SessionID::id_type
* tab_id
,
133 std::string
* user_agent_override
);
135 // Extracts a SessionCommand as previously created by
136 // CreateSetWindowAppNameCommand into the window id and application name.
137 bool RestoreSetWindowAppNameCommand(
138 const SessionCommand
& command
,
139 SessionID::id_type
* window_id
,
140 std::string
* app_name
);
142 // Returns true if the entry at specified |url| should be written to disk.
143 bool ShouldTrackEntry(const GURL
& url
);
145 // Invokes SessionBackend::ReadLastSessionCommands with callback on the
147 // If testing, SessionBackend::ReadLastSessionCommands is invoked directly.
148 CancelableTaskTracker::TaskId
ScheduleGetLastSessionCommands(
149 const InternalGetCommandsCallback
& callback
,
150 CancelableTaskTracker
* tracker
);
152 // In production, this posts the task to the FILE thread. For
153 // tests, it immediately runs the specified task on the current
155 bool RunTaskOnBackendThread(const tracked_objects::Location
& from_here
,
156 const base::Closure
& task
);
158 // Returns true if we appear to be running in production, false if we appear
159 // to be running as part of a unit test or if the FILE thread has gone away.
160 bool RunningInProduction() const;
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 DISALLOW_COPY_AND_ASSIGN(BaseSessionService
);
190 #endif // CHROME_BROWSER_SESSIONS_BASE_SESSION_SERVICE_H_