OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/sessions/session_backend.h" | 5 #include "chrome/browser/sessions/session_backend.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/memory/scoped_vector.h" | 10 #include "base/memory/scoped_vector.h" |
(...skipping 30 matching lines...) Expand all Loading... |
41 typedef SessionCommand::id_type id_type; | 41 typedef SessionCommand::id_type id_type; |
42 typedef SessionCommand::size_type size_type; | 42 typedef SessionCommand::size_type size_type; |
43 | 43 |
44 explicit SessionFileReader(const FilePath& path) | 44 explicit SessionFileReader(const FilePath& path) |
45 : errored_(false), | 45 : errored_(false), |
46 buffer_(SessionBackend::kFileReadBufferSize, 0), | 46 buffer_(SessionBackend::kFileReadBufferSize, 0), |
47 buffer_position_(0), | 47 buffer_position_(0), |
48 available_count_(0) { | 48 available_count_(0) { |
49 file_.reset(new net::FileStream(NULL)); | 49 file_.reset(new net::FileStream(NULL)); |
50 if (file_util::PathExists(path)) | 50 if (file_util::PathExists(path)) |
51 file_->Open(path, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ); | 51 file_->OpenSync(path, |
| 52 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ); |
52 } | 53 } |
53 // Reads the contents of the file specified in the constructor, returning | 54 // Reads the contents of the file specified in the constructor, returning |
54 // true on success. It is up to the caller to free all SessionCommands | 55 // true on success. It is up to the caller to free all SessionCommands |
55 // added to commands. | 56 // added to commands. |
56 bool Read(BaseSessionService::SessionType type, | 57 bool Read(BaseSessionService::SessionType type, |
57 std::vector<SessionCommand*>* commands); | 58 std::vector<SessionCommand*>* commands); |
58 | 59 |
59 private: | 60 private: |
60 // Reads a single command, returning it. A return value of NULL indicates | 61 // Reads a single command, returning it. A return value of NULL indicates |
61 // either there are no commands, or there was an error. Use errored_ to | 62 // either there are no commands, or there was an error. Use errored_ to |
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 } | 341 } |
341 } | 342 } |
342 file->Flush(); | 343 file->Flush(); |
343 return true; | 344 return true; |
344 } | 345 } |
345 | 346 |
346 SessionBackend::~SessionBackend() { | 347 SessionBackend::~SessionBackend() { |
347 if (current_session_file_.get()) { | 348 if (current_session_file_.get()) { |
348 // Close() performs file IO. crbug.com/112512. | 349 // Close() performs file IO. crbug.com/112512. |
349 base::ThreadRestrictions::ScopedAllowIO allow_io; | 350 base::ThreadRestrictions::ScopedAllowIO allow_io; |
350 current_session_file_->Close(); | 351 current_session_file_->CloseSync(); |
351 } | 352 } |
352 } | 353 } |
353 | 354 |
354 void SessionBackend::ResetFile() { | 355 void SessionBackend::ResetFile() { |
355 DCHECK(inited_); | 356 DCHECK(inited_); |
356 if (current_session_file_.get()) { | 357 if (current_session_file_.get()) { |
357 // File is already open, truncate it. We truncate instead of closing and | 358 // File is already open, truncate it. We truncate instead of closing and |
358 // reopening to avoid the possibility of scanners locking the file out | 359 // reopening to avoid the possibility of scanners locking the file out |
359 // from under us once we close it. If truncation fails, we'll try to | 360 // from under us once we close it. If truncation fails, we'll try to |
360 // recreate. | 361 // recreate. |
361 const int header_size = static_cast<int>(sizeof(FileHeader)); | 362 const int header_size = static_cast<int>(sizeof(FileHeader)); |
362 if (current_session_file_->Truncate(header_size) != header_size) | 363 if (current_session_file_->Truncate(header_size) != header_size) |
363 current_session_file_.reset(NULL); | 364 current_session_file_.reset(NULL); |
364 } | 365 } |
365 if (!current_session_file_.get()) | 366 if (!current_session_file_.get()) |
366 current_session_file_.reset(OpenAndWriteHeader(GetCurrentSessionPath())); | 367 current_session_file_.reset(OpenAndWriteHeader(GetCurrentSessionPath())); |
367 empty_file_ = true; | 368 empty_file_ = true; |
368 } | 369 } |
369 | 370 |
370 net::FileStream* SessionBackend::OpenAndWriteHeader(const FilePath& path) { | 371 net::FileStream* SessionBackend::OpenAndWriteHeader(const FilePath& path) { |
371 DCHECK(!path.empty()); | 372 DCHECK(!path.empty()); |
372 scoped_ptr<net::FileStream> file(new net::FileStream(NULL)); | 373 scoped_ptr<net::FileStream> file(new net::FileStream(NULL)); |
373 if (file->Open(path, base::PLATFORM_FILE_CREATE_ALWAYS | | 374 if (file->OpenSync(path, base::PLATFORM_FILE_CREATE_ALWAYS | |
374 base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_EXCLUSIVE_WRITE | | 375 base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_EXCLUSIVE_WRITE | |
375 base::PLATFORM_FILE_EXCLUSIVE_READ) != net::OK) | 376 base::PLATFORM_FILE_EXCLUSIVE_READ) != net::OK) |
376 return NULL; | 377 return NULL; |
377 FileHeader header; | 378 FileHeader header; |
378 header.signature = kFileSignature; | 379 header.signature = kFileSignature; |
379 header.version = kFileCurrentVersion; | 380 header.version = kFileCurrentVersion; |
380 int wrote = file->Write(reinterpret_cast<char*>(&header), | 381 int wrote = file->Write(reinterpret_cast<char*>(&header), |
381 sizeof(header), net::CompletionCallback()); | 382 sizeof(header), net::CompletionCallback()); |
382 if (wrote != sizeof(header)) | 383 if (wrote != sizeof(header)) |
383 return NULL; | 384 return NULL; |
(...skipping 10 matching lines...) Expand all Loading... |
394 } | 395 } |
395 | 396 |
396 FilePath SessionBackend::GetCurrentSessionPath() { | 397 FilePath SessionBackend::GetCurrentSessionPath() { |
397 FilePath path = path_to_dir_; | 398 FilePath path = path_to_dir_; |
398 if (type_ == BaseSessionService::TAB_RESTORE) | 399 if (type_ == BaseSessionService::TAB_RESTORE) |
399 path = path.AppendASCII(kCurrentTabSessionFileName); | 400 path = path.AppendASCII(kCurrentTabSessionFileName); |
400 else | 401 else |
401 path = path.AppendASCII(kCurrentSessionFileName); | 402 path = path.AppendASCII(kCurrentSessionFileName); |
402 return path; | 403 return path; |
403 } | 404 } |
OLD | NEW |