diff options
author | jhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-14 01:33:02 +0000 |
---|---|---|
committer | jhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-14 01:33:02 +0000 |
commit | 5a82010ab774e803a0a69328fdf56a37dee91e86 (patch) | |
tree | d408294cb73ec4577395b301b91fbd71f32a3e1d /chrome/browser/sessions | |
parent | 1871ae373abc3a867280d61b7b069aed5bc3b0fe (diff) | |
download | chromium_src-5a82010ab774e803a0a69328fdf56a37dee91e86.zip chromium_src-5a82010ab774e803a0a69328fdf56a37dee91e86.tar.gz chromium_src-5a82010ab774e803a0a69328fdf56a37dee91e86.tar.bz2 |
Port session_backend.cc to Posix.
* Using FilePath for paths instead of wstring.
* File operations using cross-platform FileStream instead of win32 API.
Review URL: http://codereview.chromium.org/20327
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@9811 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/sessions')
-rw-r--r-- | chrome/browser/sessions/base_session_service.cc | 4 | ||||
-rw-r--r-- | chrome/browser/sessions/base_session_service.h | 5 | ||||
-rw-r--r-- | chrome/browser/sessions/session_backend.cc | 115 | ||||
-rw-r--r-- | chrome/browser/sessions/session_backend.h | 25 | ||||
-rw-r--r-- | chrome/browser/sessions/session_backend_unittest.cc | 11 | ||||
-rw-r--r-- | chrome/browser/sessions/session_service.cc | 6 | ||||
-rw-r--r-- | chrome/browser/sessions/session_service.h | 2 | ||||
-rw-r--r-- | chrome/browser/sessions/session_service_unittest.cc | 10 | ||||
-rw-r--r-- | chrome/browser/sessions/tab_restore_service.cc | 2 |
9 files changed, 95 insertions, 85 deletions
diff --git a/chrome/browser/sessions/base_session_service.cc b/chrome/browser/sessions/base_session_service.cc index bf21c63..5ab3282 100644 --- a/chrome/browser/sessions/base_session_service.cc +++ b/chrome/browser/sessions/base_session_service.cc @@ -61,7 +61,7 @@ const int BaseSessionService::max_persist_navigation_count = 6; BaseSessionService::BaseSessionService(SessionType type, Profile* profile, - const std::wstring& path) + const FilePath& path) : profile_(profile), path_(path), backend_thread_(NULL), @@ -74,7 +74,7 @@ BaseSessionService::BaseSessionService(SessionType type, DCHECK(!profile->IsOffTheRecord()); } backend_ = new SessionBackend(type, - profile_ ? profile_->GetPath().ToWStringHack() : path_); + profile_ ? profile_->GetPath() : path_); DCHECK(backend_.get()); backend_thread_ = g_browser_process->file_thread(); if (!backend_thread_) diff --git a/chrome/browser/sessions/base_session_service.h b/chrome/browser/sessions/base_session_service.h index 49c70c4..b6e69e1 100644 --- a/chrome/browser/sessions/base_session_service.h +++ b/chrome/browser/sessions/base_session_service.h @@ -6,6 +6,7 @@ #define CHROME_BROWSER_SESSIONS_BASE_SESSION_SERVICE_H_ #include "base/basictypes.h" +#include "base/file_path.h" #include "base/ref_counted.h" #include "base/task.h" #include "chrome/browser/cancelable_request.h" @@ -43,7 +44,7 @@ class BaseSessionService : public CancelableRequestProvider, // ignored and instead the path comes from the profile. BaseSessionService(SessionType type, Profile* profile, - const std::wstring& path); + const FilePath& path); virtual ~BaseSessionService(); @@ -141,7 +142,7 @@ class BaseSessionService : public CancelableRequestProvider, Profile* profile_; // Path to read from. This is only used if profile_ is NULL. - const std::wstring& path_; + const FilePath& path_; // The backend. scoped_refptr<SessionBackend> backend_; diff --git a/chrome/browser/sessions/session_backend.cc b/chrome/browser/sessions/session_backend.cc index dc259d1..2fbbe40 100644 --- a/chrome/browser/sessions/session_backend.cc +++ b/chrome/browser/sessions/session_backend.cc @@ -32,13 +32,13 @@ class SessionFileReader { typedef SessionCommand::id_type id_type; typedef SessionCommand::size_type size_type; - SessionFileReader(const std::wstring& path) - : handle_(CreateFile(path.c_str(), GENERIC_READ, 0, NULL, - OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)), - buffer_(SessionBackend::kFileReadBufferSize, 0), + explicit SessionFileReader(const FilePath& path) + : errored_(false), + buffer_(SessionBackend::kFileReadBufferSize, 0), buffer_position_(0), - available_count_(0), - errored_(false) { + 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 @@ -66,7 +66,7 @@ class SessionFileReader { std::string buffer_; // The file. - ScopedHandle handle_; + scoped_ptr<net::FileStream> file_; // Position in buffer_ of the data. size_t buffer_position_; @@ -79,13 +79,14 @@ class SessionFileReader { bool SessionFileReader::Read(BaseSessionService::SessionType type, std::vector<SessionCommand*>* commands) { - if (!handle_.IsValid()) + if (!file_->IsOpen()) return false; int32 header[2]; - DWORD read_count; + int read_count; TimeTicks start_time = TimeTicks::Now(); - if (!ReadFile(handle_, &header, sizeof(header), &read_count, NULL) || - read_count != sizeof(header) || header[0] != kFileSignature || + read_count = file_->ReadUntilComplete(reinterpret_cast<char*>(&header), + sizeof(header)); + if (read_count != sizeof(header) || header[0] != kFileSignature || header[1] != kFileCurrentVersion) return false; @@ -158,16 +159,16 @@ bool SessionFileReader::FillBuffer() { } buffer_position_ = 0; DCHECK(buffer_position_ + available_count_ < buffer_.size()); - DWORD read_count; - if (!ReadFile(handle_, &(buffer_[available_count_]), - static_cast<DWORD>(buffer_.size() - available_count_), - &read_count, NULL)) { + 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_ += static_cast<int>(read_count); + available_count_ += read_count; return true; } @@ -176,18 +177,18 @@ bool SessionFileReader::FillBuffer() { // SessionBackend ------------------------------------------------------------- // File names (current and previous) for a type of TAB. -static const wchar_t* const kCurrentTabSessionFileName = L"Current Tabs"; -static const wchar_t* const kLastTabSessionFileName = L"Last Tabs"; +static const char* kCurrentTabSessionFileName = "Current Tabs"; +static const char* kLastTabSessionFileName = "Last Tabs"; // File names (current and previous) for a type of SESSION. -static const wchar_t* const kCurrentSessionFileName = L"Current Session"; -static const wchar_t* const kLastSessionFileName = L"Last 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 std::wstring& path_to_dir) + const FilePath& path_to_dir) : type_(type), path_to_dir_(path_to_dir), last_session_valid_(false), @@ -212,11 +213,11 @@ void SessionBackend::AppendCommands( std::vector<SessionCommand*>* commands, bool reset_first) { Init(); - if ((reset_first && !empty_file_) || !current_session_handle_.IsValid()) + if ((reset_first && !empty_file_) || !current_session_file_->IsOpen()) ResetFile(); - if (current_session_handle_.IsValid() && - !AppendCommandsToFile(current_session_handle_, *commands)) { - current_session_handle_.Set(NULL); + if (current_session_file_->IsOpen() && + !AppendCommandsToFile(current_session_file_.get(), *commands)) { + current_session_file_.reset(NULL); } empty_file_ = false; STLDeleteElements(commands); @@ -248,10 +249,10 @@ void SessionBackend::DeleteLastSession() { void SessionBackend::MoveCurrentSessionToLastSession() { Init(); - current_session_handle_.Set(NULL); + current_session_file_.reset(NULL); - const std::wstring current_session_path = GetCurrentSessionPath(); - const std::wstring last_session_path = GetLastSessionPath(); + 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)) { @@ -276,31 +277,33 @@ void SessionBackend::MoveCurrentSessionToLastSession() { ResetFile(); } -bool SessionBackend::AppendCommandsToFile( - HANDLE handle, +bool SessionBackend::AppendCommandsToFile(net::FileStream* file, const std::vector<SessionCommand*>& commands) { for (std::vector<SessionCommand*>::const_iterator i = commands.begin(); i != commands.end(); ++i) { - DWORD wrote; + 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(L"TabRestore.command_size", total_size); else UMA_HISTOGRAM_COUNTS(L"SessionRestore.command_size", total_size); - if (!WriteFile(handle, &total_size, sizeof(total_size), &wrote, NULL) || - wrote != sizeof(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(); - if (!WriteFile(handle, &command_id, sizeof(command_id), &wrote, NULL) || - wrote != sizeof(command_id)) { + wrote = file->Write(reinterpret_cast<char*>(&command_id), + sizeof(command_id), NULL); + if (wrote != sizeof(command_id)) { NOTREACHED() << "error writing"; return false; } - if (!WriteFile(handle, (*i)->contents(), content_size, &wrote, NULL) || - wrote != content_size) { + wrote = file->Write(reinterpret_cast<char*>((*i)->contents()), + content_size, NULL); + if (wrote != content_size) { NOTREACHED() << "error writing"; return false; } @@ -310,43 +313,41 @@ bool SessionBackend::AppendCommandsToFile( void SessionBackend::ResetFile() { DCHECK(inited_); - // Do Set(NULL) first to make sure we close current file (if open). - current_session_handle_.Set(NULL); - current_session_handle_.Set(OpenAndWriteHeader(GetCurrentSessionPath())); + current_session_file_.reset(OpenAndWriteHeader(GetCurrentSessionPath())); empty_file_ = true; } -HANDLE SessionBackend::OpenAndWriteHeader(const std::wstring& path) { +net::FileStream* SessionBackend::OpenAndWriteHeader(const FilePath& path) { DCHECK(!path.empty()); - ScopedHandle hfile( - CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, - CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)); - if (!hfile.IsValid()) + net::FileStream* file = new net::FileStream(); + file->Open(path, base::PLATFORM_FILE_CREATE_ALWAYS | + base::PLATFORM_FILE_WRITE); + if (!file->IsOpen()) return NULL; int32 header[2]; header[0] = kFileSignature; header[1] = kFileCurrentVersion; - DWORD wrote; - if (!WriteFile(hfile, &header, sizeof(header), &wrote, NULL) || - wrote != sizeof(header)) + int wrote = file->Write(reinterpret_cast<char*>(&header), + sizeof(header), NULL); + if (wrote != sizeof(header)) return NULL; - return hfile.Take(); + return file; } -std::wstring SessionBackend::GetLastSessionPath() { - std::wstring path = path_to_dir_; +FilePath SessionBackend::GetLastSessionPath() { + FilePath path = path_to_dir_; if (type_ == BaseSessionService::TAB_RESTORE) - file_util::AppendToPath(&path, kLastTabSessionFileName); + path = path.AppendASCII(kLastTabSessionFileName); else - file_util::AppendToPath(&path, kLastSessionFileName); + path = path.AppendASCII(kLastSessionFileName); return path; } -std::wstring SessionBackend::GetCurrentSessionPath() { - std::wstring path = path_to_dir_; +FilePath SessionBackend::GetCurrentSessionPath() { + FilePath path = path_to_dir_; if (type_ == BaseSessionService::TAB_RESTORE) - file_util::AppendToPath(&path, kCurrentTabSessionFileName); + path = path.AppendASCII(kCurrentTabSessionFileName); else - file_util::AppendToPath(&path, kCurrentSessionFileName); + path = path.AppendASCII(kCurrentSessionFileName); return path; } diff --git a/chrome/browser/sessions/session_backend.h b/chrome/browser/sessions/session_backend.h index 1519d3f..5535cb2 100644 --- a/chrome/browser/sessions/session_backend.h +++ b/chrome/browser/sessions/session_backend.h @@ -8,9 +8,16 @@ #include <vector> #include "base/ref_counted.h" -#include "base/scoped_handle.h" -#include "chrome/browser/sessions/base_session_service.h" +#include "base/scoped_ptr.h" #include "chrome/browser/sessions/session_command.h" +#include "net/base/file_stream.h" + +// TODO(port): Get rid of this section and finish porting. +#if defined(OS_WIN) +#include "chrome/browser/sessions/base_session_service.h" +#else +#include "chrome/common/temp_scaffolding_stubs.h" +#endif class Pickle; @@ -44,7 +51,7 @@ class SessionBackend : public base::RefCountedThreadSafe<SessionBackend> { // indicates which service is using this backend. |type| is used to determine // the name of the files to use as well as for logging. SessionBackend(BaseSessionService::SessionType type, - const std::wstring& path_to_dir); + const FilePath& path_to_dir); // Moves the current file to the last file, and recreates the current file. // @@ -86,28 +93,28 @@ class SessionBackend : public base::RefCountedThreadSafe<SessionBackend> { // Opens the current file and writes the header. On success a handle to // the file is returned. - HANDLE OpenAndWriteHeader(const std::wstring& path); + net::FileStream* OpenAndWriteHeader(const FilePath& path); // Appends the specified commands to the specified file. - bool AppendCommandsToFile(HANDLE handle, + bool AppendCommandsToFile(net::FileStream* file, const std::vector<SessionCommand*>& commands); const BaseSessionService::SessionType type_; // Returns the path to the last file. - std::wstring GetLastSessionPath(); + FilePath GetLastSessionPath(); // Returns the path to the current file. - std::wstring GetCurrentSessionPath(); + FilePath GetCurrentSessionPath(); // Directory files are relative to. - const std::wstring path_to_dir_; + const FilePath path_to_dir_; // Whether the previous target file is valid. bool last_session_valid_; // Handle to the target file. - ScopedHandle current_session_handle_; + scoped_ptr<net::FileStream> current_session_file_; // Whether we've inited. Remember, the constructor is run on the // Main thread, all others on the IO thread, hence lazy initialization. diff --git a/chrome/browser/sessions/session_backend_unittest.cc b/chrome/browser/sessions/session_backend_unittest.cc index b7bfd92..5400c17 100644 --- a/chrome/browser/sessions/session_backend_unittest.cc +++ b/chrome/browser/sessions/session_backend_unittest.cc @@ -2,7 +2,9 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "base/file_path.h" #include "base/file_util.h" +#include "base/string_util.h" #include "base/path_service.h" #include "base/time.h" #include "chrome/browser/sessions/session_backend.h" @@ -33,13 +35,12 @@ SessionCommand* CreateCommandFromData(const TestData& data) { class SessionBackendTest : public testing::Test { protected: virtual void SetUp() { - wchar_t b[32]; - _itow_s(static_cast<int>(GetTickCount()), b, arraysize(b), 10); + std::wstring b = IntToWString(static_cast<int>(GetTickCount())); PathService::Get(base::DIR_TEMP, &path_); - file_util::AppendToPath(&path_, L"SessionTestDirs"); + path_ = path_.Append(FILE_PATH_LITERAL("SessionTestDirs")); file_util::CreateDirectory(path_); - file_util::AppendToPath(&path_, b); + path_ = path_.Append(b); } virtual void TearDown() { @@ -54,7 +55,7 @@ class SessionBackendTest : public testing::Test { } // Path used in testing. - std::wstring path_; + FilePath path_; }; TEST_F(SessionBackendTest, SimpleReadWrite) { diff --git a/chrome/browser/sessions/session_service.cc b/chrome/browser/sessions/session_service.cc index 86a6b19..9521a88 100644 --- a/chrome/browser/sessions/session_service.cc +++ b/chrome/browser/sessions/session_service.cc @@ -16,6 +16,7 @@ #include "chrome/browser/browser_window.h" #include "chrome/browser/profile.h" #include "chrome/browser/session_startup_pref.h" +#include "chrome/browser/sessions/session_backend.h" #include "chrome/browser/sessions/session_command.h" #include "chrome/browser/sessions/session_types.h" #include "chrome/browser/tab_contents/navigation_controller.h" @@ -26,7 +27,6 @@ // TODO(port): Get rid of this section and finish porting. #if defined(OS_WIN) -#include "chrome/browser/sessions/session_backend.h" #include "chrome/browser/sessions/session_restore.h" #include "chrome/browser/tab_contents/tab_contents.h" #include "chrome/common/win_util.h" @@ -115,13 +115,13 @@ typedef IDAndIndexPayload TabNavigationPathPrunedFromFrontPayload; // SessionService ------------------------------------------------------------- SessionService::SessionService(Profile* profile) - : BaseSessionService(SESSION_RESTORE, profile, std::wstring()), + : BaseSessionService(SESSION_RESTORE, profile, FilePath()), has_open_tabbed_browsers_(false), move_on_new_browser_(false) { Init(); } -SessionService::SessionService(const std::wstring& save_path) +SessionService::SessionService(const FilePath& save_path) : BaseSessionService(SESSION_RESTORE, NULL, save_path), has_open_tabbed_browsers_(false), move_on_new_browser_(false) { diff --git a/chrome/browser/sessions/session_service.h b/chrome/browser/sessions/session_service.h index 015401c..6a0e63a 100644 --- a/chrome/browser/sessions/session_service.h +++ b/chrome/browser/sessions/session_service.h @@ -58,7 +58,7 @@ class SessionService : public BaseSessionService, // Creates a SessionService for the specified profile. explicit SessionService(Profile* profile); // For testing. - explicit SessionService(const std::wstring& save_path); + explicit SessionService(const FilePath& save_path); virtual ~SessionService(); diff --git a/chrome/browser/sessions/session_service_unittest.cc b/chrome/browser/sessions/session_service_unittest.cc index 059f307e..593fa48 100644 --- a/chrome/browser/sessions/session_service_unittest.cc +++ b/chrome/browser/sessions/session_service_unittest.cc @@ -5,6 +5,7 @@ #include <windows.h> #include "base/file_util.h" +#include "base/string_util.h" #include "base/path_service.h" #include "chrome/browser/sessions/session_backend.h" #include "chrome/browser/sessions/session_service.h" @@ -22,13 +23,12 @@ class SessionServiceTest : public testing::Test { protected: virtual void SetUp() { - wchar_t b[32]; - _itow_s(static_cast<int>(GetTickCount()), b, arraysize(b), 10); + std::wstring b = IntToWString(static_cast<int>(GetTickCount())); PathService::Get(base::DIR_TEMP, &path_); - file_util::AppendToPath(&path_, L"SessionTestDirs"); + path_ = path_.Append(FILE_PATH_LITERAL("SessionTestDirs")); file_util::CreateDirectory(path_); - file_util::AppendToPath(&path_, b); + path_ = path_.Append(b); SessionService* session_service = new SessionService(path_); helper_.set_service(session_service); @@ -78,7 +78,7 @@ class SessionServiceTest : public testing::Test { SessionID window_id; // Path used in testing. - std::wstring path_; + FilePath path_; SessionServiceTestHelper helper_; }; diff --git a/chrome/browser/sessions/tab_restore_service.cc b/chrome/browser/sessions/tab_restore_service.cc index 9a7a5bf..134a898 100644 --- a/chrome/browser/sessions/tab_restore_service.cc +++ b/chrome/browser/sessions/tab_restore_service.cc @@ -92,7 +92,7 @@ void RemoveEntryByID(SessionID::id_type id, TabRestoreService::TabRestoreService(Profile* profile) : BaseSessionService(BaseSessionService::TAB_RESTORE, profile, - std::wstring()), + FilePath()), load_state_(NOT_LOADED), restoring_(false), reached_max_(false), |