1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/sessions/base_session_service.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/pickle.h"
#include "base/stl_util.h"
#include "base/threading/thread.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sessions/session_backend.h"
#include "chrome/browser/sessions/session_types.h"
#include "chrome/common/chrome_switches.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/common/referrer.h"
#include "webkit/glue/webkit_glue.h"
using content::BrowserThread;
using content::NavigationEntry;
// BaseSessionService ---------------------------------------------------------
namespace {
// Helper used by CreateUpdateTabNavigationCommand(). It writes |str| to
// |pickle|, if and only if |str| fits within (|max_bytes| - |*bytes_written|).
// |bytes_written| is incremented to reflect the data written.
void WriteStringToPickle(Pickle& pickle, int* bytes_written, int max_bytes,
const std::string& str) {
int num_bytes = str.size() * sizeof(char);
if (*bytes_written + num_bytes < max_bytes) {
*bytes_written += num_bytes;
pickle.WriteString(str);
} else {
pickle.WriteString(std::string());
}
}
// Helper used by ScheduleGetLastSessionCommands. It runs callback on TaskRunner
// thread if it's not canceled.
void RunIfNotCanceled(
const CancelableTaskTracker::IsCanceledCallback& is_canceled,
base::TaskRunner* task_runner,
const BaseSessionService::InternalGetCommandsCallback& callback,
ScopedVector<SessionCommand> commands) {
if (is_canceled.Run())
return;
if (task_runner->RunsTasksOnCurrentThread()) {
callback.Run(commands.Pass());
} else {
task_runner->PostTask(FROM_HERE,
base::Bind(callback, base::Passed(&commands)));
}
}
} // namespace
// Delay between when a command is received, and when we save it to the
// backend.
static const int kSaveDelayMS = 2500;
// static
const int BaseSessionService::max_persist_navigation_count = 6;
BaseSessionService::BaseSessionService(SessionType type,
Profile* profile,
const FilePath& path)
: profile_(profile),
ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
pending_reset_(false),
commands_since_reset_(0) {
if (profile) {
// We should never be created when incognito.
DCHECK(!profile->IsOffTheRecord());
}
backend_ = new SessionBackend(type, profile_ ? profile_->GetPath() : path);
DCHECK(backend_.get());
// SessionBackend::Init() cannot be scheduled to be called here. There are
// service processes which create the BaseSessionService, but they should not
// initialize the backend. If they do, the backend will cycle the session
// restore files. That in turn prevents the session restore from working when
// the normal chromium process is launched. Normally, the backend will be
// initialized before it's actually used. However, if we're running as a part
// of a test, it must be initialized now.
if (!RunningInProduction())
backend_->Init();
}
BaseSessionService::~BaseSessionService() {
}
void BaseSessionService::DeleteLastSession() {
RunTaskOnBackendThread(
FROM_HERE,
base::Bind(&SessionBackend::DeleteLastSession, backend()));
}
void BaseSessionService::ScheduleCommand(SessionCommand* command) {
DCHECK(command);
commands_since_reset_++;
pending_commands_.push_back(command);
StartSaveTimer();
}
void BaseSessionService::StartSaveTimer() {
// Don't start a timer when testing (profile == NULL or
// MessageLoop::current() is NULL).
if (MessageLoop::current() && profile() && !weak_factory_.HasWeakPtrs()) {
MessageLoop::current()->PostDelayedTask(
FROM_HERE,
base::Bind(&BaseSessionService::Save, weak_factory_.GetWeakPtr()),
base::TimeDelta::FromMilliseconds(kSaveDelayMS));
}
}
void BaseSessionService::Save() {
DCHECK(backend());
if (pending_commands_.empty())
return;
RunTaskOnBackendThread(
FROM_HERE,
base::Bind(&SessionBackend::AppendCommands, backend(),
new std::vector<SessionCommand*>(pending_commands_),
pending_reset_));
// Backend took ownership of commands.
pending_commands_.clear();
if (pending_reset_) {
commands_since_reset_ = 0;
pending_reset_ = false;
}
}
SessionCommand* BaseSessionService::CreateUpdateTabNavigationCommand(
SessionID::id_type command_id,
SessionID::id_type tab_id,
const TabNavigation& navigation) {
// Use pickle to handle marshalling.
Pickle pickle;
pickle.WriteInt(tab_id);
navigation.WriteToPickle(&pickle);
return new SessionCommand(command_id, pickle);
}
SessionCommand* BaseSessionService::CreateSetTabExtensionAppIDCommand(
SessionID::id_type command_id,
SessionID::id_type tab_id,
const std::string& extension_id) {
// Use pickle to handle marshalling.
Pickle pickle;
pickle.WriteInt(tab_id);
// Enforce a max for ids. They should never be anywhere near this size.
static const SessionCommand::size_type max_id_size =
std::numeric_limits<SessionCommand::size_type>::max() - 1024;
int bytes_written = 0;
WriteStringToPickle(pickle, &bytes_written, max_id_size, extension_id);
return new SessionCommand(command_id, pickle);
}
SessionCommand* BaseSessionService::CreateSetTabUserAgentOverrideCommand(
SessionID::id_type command_id,
SessionID::id_type tab_id,
const std::string& user_agent_override) {
// Use pickle to handle marshalling.
Pickle pickle;
pickle.WriteInt(tab_id);
// Enforce a max for the user agent length. They should never be anywhere
// near this size.
static const SessionCommand::size_type max_user_agent_size =
std::numeric_limits<SessionCommand::size_type>::max() - 1024;
int bytes_written = 0;
WriteStringToPickle(pickle, &bytes_written, max_user_agent_size,
user_agent_override);
return new SessionCommand(command_id, pickle);
}
SessionCommand* BaseSessionService::CreateSetWindowAppNameCommand(
SessionID::id_type command_id,
SessionID::id_type window_id,
const std::string& app_name) {
// Use pickle to handle marshalling.
Pickle pickle;
pickle.WriteInt(window_id);
// Enforce a max for ids. They should never be anywhere near this size.
static const SessionCommand::size_type max_id_size =
std::numeric_limits<SessionCommand::size_type>::max() - 1024;
int bytes_written = 0;
WriteStringToPickle(pickle, &bytes_written, max_id_size, app_name);
return new SessionCommand(command_id, pickle);
}
bool BaseSessionService::RestoreUpdateTabNavigationCommand(
const SessionCommand& command,
TabNavigation* navigation,
SessionID::id_type* tab_id) {
scoped_ptr<Pickle> pickle(command.PayloadAsPickle());
if (!pickle.get())
return false;
PickleIterator iterator(*pickle);
return
pickle->ReadInt(&iterator, tab_id) &&
navigation->ReadFromPickle(&iterator);
}
bool BaseSessionService::RestoreSetTabExtensionAppIDCommand(
const SessionCommand& command,
SessionID::id_type* tab_id,
std::string* extension_app_id) {
scoped_ptr<Pickle> pickle(command.PayloadAsPickle());
if (!pickle.get())
return false;
PickleIterator iterator(*pickle);
return pickle->ReadInt(&iterator, tab_id) &&
pickle->ReadString(&iterator, extension_app_id);
}
bool BaseSessionService::RestoreSetTabUserAgentOverrideCommand(
const SessionCommand& command,
SessionID::id_type* tab_id,
std::string* user_agent_override) {
scoped_ptr<Pickle> pickle(command.PayloadAsPickle());
if (!pickle.get())
return false;
PickleIterator iterator(*pickle);
return pickle->ReadInt(&iterator, tab_id) &&
pickle->ReadString(&iterator, user_agent_override);
}
bool BaseSessionService::RestoreSetWindowAppNameCommand(
const SessionCommand& command,
SessionID::id_type* window_id,
std::string* app_name) {
scoped_ptr<Pickle> pickle(command.PayloadAsPickle());
if (!pickle.get())
return false;
PickleIterator iterator(*pickle);
return pickle->ReadInt(&iterator, window_id) &&
pickle->ReadString(&iterator, app_name);
}
bool BaseSessionService::ShouldTrackEntry(const GURL& url) {
return url.is_valid();
}
CancelableTaskTracker::TaskId
BaseSessionService::ScheduleGetLastSessionCommands(
const InternalGetCommandsCallback& callback,
CancelableTaskTracker* tracker) {
CancelableTaskTracker::IsCanceledCallback is_canceled;
CancelableTaskTracker::TaskId id = tracker->NewTrackedTaskId(&is_canceled);
InternalGetCommandsCallback callback_runner =
base::Bind(&RunIfNotCanceled,
is_canceled, base::MessageLoopProxy::current(), callback);
RunTaskOnBackendThread(
FROM_HERE,
base::Bind(&SessionBackend::ReadLastSessionCommands, backend(),
is_canceled, callback_runner));
return id;
}
bool BaseSessionService::RunTaskOnBackendThread(
const tracked_objects::Location& from_here,
const base::Closure& task) {
if (profile_ && BrowserThread::IsMessageLoopValid(BrowserThread::FILE)) {
return BrowserThread::PostTask(BrowserThread::FILE, from_here, task);
} else {
// Fall back to executing on the main thread if the file thread
// has gone away (around shutdown time) or if we're running as
// part of a unit test that does not set profile_.
task.Run();
return true;
}
}
bool BaseSessionService::RunningInProduction() const {
return profile_ && BrowserThread::IsMessageLoopValid(BrowserThread::FILE);
}
|