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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
|
// Copyright (c) 2011 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/session_backend.h"
#include <limits>
#include "base/file_util.h"
#include "base/memory/scoped_vector.h"
#include "base/metrics/histogram.h"
#include "net/base/file_stream.h"
#include "net/base/net_errors.h"
using base::TimeTicks;
// File version number.
static const int32 kFileCurrentVersion = 1;
// The signature at the beginning of the file = SSNS (Sessions).
static const int32 kFileSignature = 0x53534E53;
namespace {
// The file header is the first bytes written to the file,
// and is used to identify the file as one written by us.
struct FileHeader {
int32 signature;
int32 version;
};
// SessionFileReader ----------------------------------------------------------
// SessionFileReader is responsible for reading the set of SessionCommands that
// describe a Session back from a file. SessionFileRead does minimal error
// checking on the file (pretty much only that the header is valid).
class SessionFileReader {
public:
typedef SessionCommand::id_type id_type;
typedef SessionCommand::size_type size_type;
explicit SessionFileReader(const FilePath& path)
: errored_(false),
buffer_(SessionBackend::kFileReadBufferSize, 0),
buffer_position_(0),
available_count_(0) {
file_.reset(new net::FileStream());
file_->Open(path, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ);
}
// Reads the contents of the file specified in the constructor, returning
// true on success. It is up to the caller to free all SessionCommands
// added to commands.
bool Read(BaseSessionService::SessionType type,
std::vector<SessionCommand*>* commands);
private:
// Reads a single command, returning it. A return value of NULL indicates
// either there are no commands, or there was an error. Use errored_ to
// distinguish the two. If NULL is returned, and there is no error, it means
// the end of file was successfully reached.
SessionCommand* ReadCommand();
// Shifts the unused portion of buffer_ to the beginning and fills the
// remaining portion with data from the file. Returns false if the buffer
// couldn't be filled. A return value of false only signals an error if
// errored_ is set to true.
bool FillBuffer();
// Whether an error condition has been detected (
bool errored_;
// As we read from the file, data goes here.
std::string buffer_;
// The file.
scoped_ptr<net::FileStream> file_;
// Position in buffer_ of the data.
size_t buffer_position_;
// Number of available bytes; relative to buffer_position_.
size_t available_count_;
DISALLOW_COPY_AND_ASSIGN(SessionFileReader);
};
bool SessionFileReader::Read(BaseSessionService::SessionType type,
std::vector<SessionCommand*>* commands) {
if (!file_->IsOpen())
return false;
FileHeader header;
int read_count;
TimeTicks start_time = TimeTicks::Now();
read_count = file_->ReadUntilComplete(reinterpret_cast<char*>(&header),
sizeof(header));
if (read_count != sizeof(header) || header.signature != kFileSignature ||
header.version != kFileCurrentVersion)
return false;
ScopedVector<SessionCommand> read_commands;
SessionCommand* command;
while ((command = ReadCommand()) && !errored_)
read_commands->push_back(command);
if (!errored_)
read_commands->swap(*commands);
if (type == BaseSessionService::TAB_RESTORE) {
UMA_HISTOGRAM_TIMES("TabRestore.read_session_file_time",
TimeTicks::Now() - start_time);
} else {
UMA_HISTOGRAM_TIMES("SessionRestore.read_session_file_time",
TimeTicks::Now() - start_time);
}
return !errored_;
}
SessionCommand* SessionFileReader::ReadCommand() {
// Make sure there is enough in the buffer for the size of the next command.
if (available_count_ < sizeof(size_type)) {
if (!FillBuffer())
return NULL;
if (available_count_ < sizeof(size_type)) {
// Still couldn't read a valid size for the command, assume write was
// incomplete and return NULL.
return NULL;
}
}
// Get the size of the command.
size_type command_size;
memcpy(&command_size, &(buffer_[buffer_position_]), sizeof(command_size));
buffer_position_ += sizeof(command_size);
available_count_ -= sizeof(command_size);
if (command_size == 0) {
// Empty command. Shouldn't happen if write was successful, fail.
return NULL;
}
// Make sure buffer has the complete contents of the command.
if (command_size > available_count_) {
if (command_size > buffer_.size())
buffer_.resize((command_size / 1024 + 1) * 1024, 0);
if (!FillBuffer() || command_size > available_count_) {
// Again, assume the file was ok, and just the last chunk was lost.
return NULL;
}
}
const id_type command_id = buffer_[buffer_position_];
// NOTE: command_size includes the size of the id, which is not part of
// the contents of the SessionCommand.
SessionCommand* command =
new SessionCommand(command_id, command_size - sizeof(id_type));
if (command_size > sizeof(id_type)) {
memcpy(command->contents(),
&(buffer_[buffer_position_ + sizeof(id_type)]),
command_size - sizeof(id_type));
}
buffer_position_ += command_size;
available_count_ -= command_size;
return command;
}
bool SessionFileReader::FillBuffer() {
if (available_count_ > 0 && buffer_position_ > 0) {
// Shift buffer to beginning.
memmove(&(buffer_[0]), &(buffer_[buffer_position_]), available_count_);
}
buffer_position_ = 0;
DCHECK(buffer_position_ + available_count_ < buffer_.size());
int to_read = static_cast<int>(buffer_.size() - available_count_);
int read_count = file_->ReadUntilComplete(&(buffer_[available_count_]),
to_read);
if (read_count < 0) {
errored_ = true;
return false;
}
if (read_count == 0)
return false;
available_count_ += read_count;
return true;
}
} // namespace
// SessionBackend -------------------------------------------------------------
// File names (current and previous) for a type of TAB.
static const char* kCurrentTabSessionFileName = "Current Tabs";
static const char* kLastTabSessionFileName = "Last Tabs";
// File names (current and previous) for a type of SESSION.
static const char* kCurrentSessionFileName = "Current Session";
static const char* kLastSessionFileName = "Last Session";
// static
const int SessionBackend::kFileReadBufferSize = 1024;
SessionBackend::SessionBackend(BaseSessionService::SessionType type,
const FilePath& path_to_dir)
: type_(type),
path_to_dir_(path_to_dir),
last_session_valid_(false),
inited_(false),
empty_file_(true) {
// NOTE: this is invoked on the main thread, don't do file access here.
}
void SessionBackend::Init() {
if (inited_)
return;
inited_ = true;
// Create the directory for session info.
file_util::CreateDirectory(path_to_dir_);
MoveCurrentSessionToLastSession();
}
void SessionBackend::AppendCommands(
std::vector<SessionCommand*>* commands,
bool reset_first) {
Init();
// Make sure and check current_session_file_, if opening the file failed
// current_session_file_ will be NULL.
if ((reset_first && !empty_file_) || !current_session_file_.get() ||
!current_session_file_->IsOpen()) {
ResetFile();
}
// Need to check current_session_file_ again, ResetFile may fail.
if (current_session_file_.get() && current_session_file_->IsOpen() &&
!AppendCommandsToFile(current_session_file_.get(), *commands)) {
current_session_file_.reset(NULL);
}
empty_file_ = false;
STLDeleteElements(commands);
delete commands;
}
void SessionBackend::ReadLastSessionCommands(
scoped_refptr<BaseSessionService::InternalGetCommandsRequest> request) {
if (request->canceled())
return;
Init();
ReadLastSessionCommandsImpl(&(request->commands));
request->ForwardResult(
BaseSessionService::InternalGetCommandsRequest::TupleType(
request->handle(), request));
}
bool SessionBackend::ReadLastSessionCommandsImpl(
std::vector<SessionCommand*>* commands) {
Init();
SessionFileReader file_reader(GetLastSessionPath());
return file_reader.Read(type_, commands);
}
void SessionBackend::DeleteLastSession() {
Init();
file_util::Delete(GetLastSessionPath(), false);
}
void SessionBackend::MoveCurrentSessionToLastSession() {
Init();
current_session_file_.reset(NULL);
const FilePath current_session_path = GetCurrentSessionPath();
const FilePath last_session_path = GetLastSessionPath();
if (file_util::PathExists(last_session_path))
file_util::Delete(last_session_path, false);
if (file_util::PathExists(current_session_path)) {
int64 file_size;
if (file_util::GetFileSize(current_session_path, &file_size)) {
if (type_ == BaseSessionService::TAB_RESTORE) {
UMA_HISTOGRAM_COUNTS("TabRestore.last_session_file_size",
static_cast<int>(file_size / 1024));
} else {
UMA_HISTOGRAM_COUNTS("SessionRestore.last_session_file_size",
static_cast<int>(file_size / 1024));
}
}
last_session_valid_ = file_util::Move(current_session_path,
last_session_path);
}
if (file_util::PathExists(current_session_path))
file_util::Delete(current_session_path, false);
// Create and open the file for the current session.
ResetFile();
}
void SessionBackend::ReadCurrentSessionCommands(
scoped_refptr<BaseSessionService::InternalGetCommandsRequest> request) {
if (request->canceled())
return;
Init();
ReadCurrentSessionCommandsImpl(&(request->commands));
request->ForwardResult(
BaseSessionService::InternalGetCommandsRequest::TupleType(
request->handle(), request));
}
bool SessionBackend::ReadCurrentSessionCommandsImpl(
std::vector<SessionCommand*>* commands) {
Init();
SessionFileReader file_reader(GetCurrentSessionPath());
return file_reader.Read(type_, commands);
}
bool SessionBackend::AppendCommandsToFile(net::FileStream* file,
const std::vector<SessionCommand*>& commands) {
for (std::vector<SessionCommand*>::const_iterator i = commands.begin();
i != commands.end(); ++i) {
int wrote;
const size_type content_size = static_cast<size_type>((*i)->size());
const size_type total_size = content_size + sizeof(id_type);
if (type_ == BaseSessionService::TAB_RESTORE)
UMA_HISTOGRAM_COUNTS("TabRestore.command_size", total_size);
else
UMA_HISTOGRAM_COUNTS("SessionRestore.command_size", total_size);
wrote = file->Write(reinterpret_cast<const char*>(&total_size),
sizeof(total_size), NULL);
if (wrote != sizeof(total_size)) {
NOTREACHED() << "error writing";
return false;
}
id_type command_id = (*i)->id();
wrote = file->Write(reinterpret_cast<char*>(&command_id),
sizeof(command_id), NULL);
if (wrote != sizeof(command_id)) {
NOTREACHED() << "error writing";
return false;
}
if (content_size > 0) {
wrote = file->Write(reinterpret_cast<char*>((*i)->contents()),
content_size, NULL);
if (wrote != content_size) {
NOTREACHED() << "error writing";
return false;
}
}
}
return true;
}
SessionBackend::~SessionBackend() {
}
void SessionBackend::ResetFile() {
DCHECK(inited_);
if (current_session_file_.get()) {
// File is already open, truncate it. We truncate instead of closing and
// reopening to avoid the possibility of scanners locking the file out
// from under us once we close it. If truncation fails, we'll try to
// recreate.
const int header_size = static_cast<int>(sizeof(FileHeader));
if (current_session_file_->Truncate(header_size) != header_size)
current_session_file_.reset(NULL);
}
if (!current_session_file_.get())
current_session_file_.reset(OpenAndWriteHeader(GetCurrentSessionPath()));
empty_file_ = true;
}
net::FileStream* SessionBackend::OpenAndWriteHeader(const FilePath& path) {
DCHECK(!path.empty());
scoped_ptr<net::FileStream> file(new net::FileStream());
if (file->Open(path, base::PLATFORM_FILE_CREATE_ALWAYS |
base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_EXCLUSIVE_WRITE |
base::PLATFORM_FILE_EXCLUSIVE_READ) != net::OK)
return NULL;
FileHeader header;
header.signature = kFileSignature;
header.version = kFileCurrentVersion;
int wrote = file->Write(reinterpret_cast<char*>(&header),
sizeof(header), NULL);
if (wrote != sizeof(header))
return NULL;
return file.release();
}
FilePath SessionBackend::GetLastSessionPath() {
FilePath path = path_to_dir_;
if (type_ == BaseSessionService::TAB_RESTORE)
path = path.AppendASCII(kLastTabSessionFileName);
else
path = path.AppendASCII(kLastSessionFileName);
return path;
}
FilePath SessionBackend::GetCurrentSessionPath() {
FilePath path = path_to_dir_;
if (type_ == BaseSessionService::TAB_RESTORE)
path = path.AppendASCII(kCurrentTabSessionFileName);
else
path = path.AppendASCII(kCurrentSessionFileName);
return path;
}
|