diff options
author | ahendrickson@chromium.org <ahendrickson@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-16 22:31:39 +0000 |
---|---|---|
committer | ahendrickson@chromium.org <ahendrickson@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-16 22:31:39 +0000 |
commit | f6c2b53147fb1fecbad82da4993670542c4404e2 (patch) | |
tree | 860d7927148aabaae012ccc2b010937704d36b9e /content/browser | |
parent | d8c8749b93af8d5b8b2a761313673539234680ef (diff) | |
download | chromium_src-f6c2b53147fb1fecbad82da4993670542c4404e2.zip chromium_src-f6c2b53147fb1fecbad82da4993670542c4404e2.tar.gz chromium_src-f6c2b53147fb1fecbad82da4993670542c4404e2.tar.bz2 |
Created an interface for DownloadFile, for use in unit tests.
Bug=None
Test=None
Review URL: http://codereview.chromium.org/8372034
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@110377 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content/browser')
-rw-r--r-- | content/browser/download/base_file.h | 2 | ||||
-rw-r--r-- | content/browser/download/download_create_info.h | 1 | ||||
-rw-r--r-- | content/browser/download/download_file.cc | 70 | ||||
-rw-r--r-- | content/browser/download/download_file.h | 85 | ||||
-rw-r--r-- | content/browser/download/download_file_impl.cc | 109 | ||||
-rw-r--r-- | content/browser/download/download_file_impl.h | 66 | ||||
-rw-r--r-- | content/browser/download/download_file_manager.cc | 25 | ||||
-rw-r--r-- | content/browser/download/download_file_unittest.cc | 28 | ||||
-rw-r--r-- | content/browser/download/download_resource_handler.h | 1 | ||||
-rw-r--r-- | content/browser/download/drag_download_file.cc | 1 | ||||
-rwxr-xr-x | content/browser/download/mock_download_file.cc | 134 | ||||
-rwxr-xr-x | content/browser/download/mock_download_file.h | 116 | ||||
-rw-r--r-- | content/browser/download/save_file.cc | 50 | ||||
-rw-r--r-- | content/browser/download/save_file.h | 17 | ||||
-rw-r--r-- | content/browser/download/save_file_manager.cc | 20 | ||||
-rw-r--r-- | content/browser/renderer_host/buffered_resource_handler.cc | 1 |
16 files changed, 595 insertions, 131 deletions
diff --git a/content/browser/download/base_file.h b/content/browser/download/base_file.h index 8fbcdd0..2a09943 100644 --- a/content/browser/download/base_file.h +++ b/content/browser/download/base_file.h @@ -15,6 +15,7 @@ #include "content/browser/power_save_blocker.h" #include "content/common/content_export.h" #include "googleurl/src/gurl.h" +#include "net/base/file_stream.h" #include "net/base/net_errors.h" namespace crypto { @@ -86,7 +87,6 @@ class CONTENT_EXPORT BaseFile { private: friend class BaseFileTest; - friend class DownloadFileWithMockStream; FRIEND_TEST_ALL_PREFIXES(BaseFileTest, IsEmptySha256Hash); static const size_t kSha256HashLen = 32; diff --git a/content/browser/download/download_create_info.h b/content/browser/download/download_create_info.h index ed05294..652de4f 100644 --- a/content/browser/download/download_create_info.h +++ b/content/browser/download/download_create_info.h @@ -14,6 +14,7 @@ #include "base/time.h" #include "content/browser/download/download_file.h" #include "content/browser/download/download_id.h" +#include "content/browser/download/download_types.h" #include "content/common/content_export.h" #include "content/public/common/page_transition_types.h" #include "googleurl/src/gurl.h" diff --git a/content/browser/download/download_file.cc b/content/browser/download/download_file.cc index 3e29fd8..7d65a82 100644 --- a/content/browser/download/download_file.cc +++ b/content/browser/download/download_file.cc @@ -4,15 +4,8 @@ #include "content/browser/download/download_file.h" -#include <string> - #include "base/file_util.h" #include "base/stringprintf.h" -#include "content/browser/download/download_create_info.h" -#include "content/browser/download/download_manager.h" -#include "content/public/browser/browser_thread.h" - -using content::BrowserThread; namespace { @@ -25,49 +18,23 @@ static const int kMaxUniqueFiles = 100; } -DownloadFile::DownloadFile(const DownloadCreateInfo* info, - DownloadRequestHandleInterface* request_handle, - DownloadManager* download_manager) - : BaseFile(info->save_info.file_path, - info->url(), - info->referrer_url, - info->received_bytes, - info->save_info.file_stream), - id_(info->download_id), - request_handle_(request_handle), - download_manager_(download_manager) { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); -} - -DownloadFile::~DownloadFile() { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); -} - -void DownloadFile::CancelDownloadRequest() { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); - request_handle_->CancelRequest(); -} - -DownloadManager* DownloadFile::GetDownloadManager() { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); - return download_manager_.get(); -} - -std::string DownloadFile::DebugString() const { - return base::StringPrintf("{" - " id_ = " "%d" - " request_handle = %s" - " Base File = %s" - " }", - id_.local(), - request_handle_->DebugString().c_str(), - BaseFile::DebugString().c_str()); -} - +// static void DownloadFile::AppendNumberToPath(FilePath* path, int number) { *path = path->InsertBeforeExtensionASCII(StringPrintf(" (%d)", number)); } +// static +FilePath DownloadFile::AppendSuffixToPath( + const FilePath& path, + const FilePath::StringType& suffix) { + FilePath::StringType file_name; + base::SStringPrintf( + &file_name, PRFilePathLiteral PRFilePathLiteral, path.value().c_str(), + suffix.c_str()); + return FilePath(file_name); +} + +// static int DownloadFile::GetUniquePathNumber(const FilePath& path) { if (!file_util::PathExists(path)) return 0; @@ -84,16 +51,7 @@ int DownloadFile::GetUniquePathNumber(const FilePath& path) { return -1; } -FilePath DownloadFile::AppendSuffixToPath( - const FilePath& path, - const FilePath::StringType& suffix) { - FilePath::StringType file_name; - base::SStringPrintf( - &file_name, PRFilePathLiteral PRFilePathLiteral, path.value().c_str(), - suffix.c_str()); - return FilePath(file_name); -} - +// static int DownloadFile::GetUniquePathNumberWithSuffix( const FilePath& path, const FilePath::StringType& suffix) { diff --git a/content/browser/download/download_file.h b/content/browser/download/download_file.h index 15047f3..dbba1dd 100644 --- a/content/browser/download/download_file.h +++ b/content/browser/download/download_file.h @@ -9,49 +9,76 @@ #include <string> #include "base/basictypes.h" -#include "base/memory/ref_counted.h" #include "content/browser/download/base_file.h" #include "content/browser/download/download_id.h" -#include "content/browser/download/download_request_handle.h" -#include "content/browser/download/download_types.h" #include "content/common/content_export.h" +#include "net/base/net_errors.h" -struct DownloadCreateInfo; class DownloadManager; -// These objects live exclusively on the download thread and handle the writing +// These objects live exclusively on the file thread and handle the writing // operations for one download. These objects live only for the duration that // the download is 'in progress': once the download has been completed or // cancelled, the DownloadFile is destroyed. -class CONTENT_EXPORT DownloadFile : public BaseFile { +class CONTENT_EXPORT DownloadFile { public: - // Takes ownership of the object pointed to by |request_handle|. - DownloadFile(const DownloadCreateInfo* info, - DownloadRequestHandleInterface* request_handle, - DownloadManager* download_manager); - virtual ~DownloadFile(); + virtual ~DownloadFile() {} + + // If calculate_hash is true, sha256 hash will be calculated. + // Returns net::OK on success, or a network error code on failure. + virtual net::Error Initialize(bool calculate_hash) = 0; + + // Write a new chunk of data to the file. + // Returns net::OK on success (all bytes written to the file), + // or a network error code on failure. + virtual net::Error AppendDataToFile(const char* data, size_t data_len) = 0; + + // Rename the download file. + // Returns net::OK on success, or a network error code on failure. + virtual net::Error Rename(const FilePath& full_path) = 0; + + // Detach the file so it is not deleted on destruction. + virtual void Detach() = 0; + + // Abort the download and automatically close the file. + virtual void Cancel() = 0; + + // Indicate that the download has finished. No new data will be received. + virtual void Finish() = 0; + + // Informs the OS that this file came from the internet. + virtual void AnnotateWithSourceInformation() = 0; + + virtual FilePath FullPath() const = 0; + virtual bool InProgress() const = 0; + virtual int64 BytesSoFar() const = 0; + + // Set |hash| with sha256 digest for the file. + // Returns true if digest is successfully calculated. + virtual bool GetSha256Hash(std::string* hash) = 0; // Cancels the download request associated with this file. - void CancelDownloadRequest(); + virtual void CancelDownloadRequest() = 0; - int id() const { return id_.local(); } - DownloadManager* GetDownloadManager(); - const DownloadId& global_id() const { return id_; } + virtual int Id() const = 0; + virtual DownloadManager* GetDownloadManager() = 0; + virtual const DownloadId& GlobalId() const = 0; - virtual std::string DebugString() const; + virtual std::string DebugString() const = 0; - // Appends the passed the number between parenthesis the path before the - // extension. + // Appends the passed-in |number| between parenthesis to the |path| before + // the file extension. static void AppendNumberToPath(FilePath* path, int number); - // Attempts to find a number that can be appended to that path to make it + // Appends the passed-in |suffix| to the |path|. + static FilePath AppendSuffixToPath(const FilePath& path, + const FilePath::StringType& suffix); + + // Attempts to find a number that can be appended to the |path| to make it // unique. If |path| does not exist, 0 is returned. If it fails to find such // a number, -1 is returned. static int GetUniquePathNumber(const FilePath& path); - static FilePath AppendSuffixToPath(const FilePath& path, - const FilePath::StringType& suffix); - // Same as GetUniquePathNumber, except that it also checks the existence // of it with the given suffix. // If |path| does not exist, 0 is returned. If it fails to find such @@ -59,20 +86,6 @@ class CONTENT_EXPORT DownloadFile : public BaseFile { static int GetUniquePathNumberWithSuffix( const FilePath& path, const FilePath::StringType& suffix); - - private: - // The unique identifier for this download, assigned at creation by - // the DownloadFileManager for its internal record keeping. - DownloadId id_; - - // The handle to the request information. Used for operations outside the - // download system, specifically canceling a download. - scoped_ptr<DownloadRequestHandleInterface> request_handle_; - - // DownloadManager this download belongs to. - scoped_refptr<DownloadManager> download_manager_; - - DISALLOW_COPY_AND_ASSIGN(DownloadFile); }; #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_H_ diff --git a/content/browser/download/download_file_impl.cc b/content/browser/download/download_file_impl.cc new file mode 100644 index 0000000..f998432 --- /dev/null +++ b/content/browser/download/download_file_impl.cc @@ -0,0 +1,109 @@ +// 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 "content/browser/download/download_file_impl.h" + +#include <string> + +#include "base/stringprintf.h" +#include "content/browser/download/download_create_info.h" +#include "content/browser/download/download_manager.h" +#include "content/public/browser/browser_thread.h" + +using content::BrowserThread; + +DownloadFileImpl::DownloadFileImpl( + const DownloadCreateInfo* info, + DownloadRequestHandleInterface* request_handle, + DownloadManager* download_manager) + : file_(info->save_info.file_path, + info->url(), + info->referrer_url, + info->received_bytes, + info->save_info.file_stream), + id_(info->download_id), + request_handle_(request_handle), + download_manager_(download_manager) { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); +} + +DownloadFileImpl::~DownloadFileImpl() { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); +} + +// BaseFile delegated functions. +net::Error DownloadFileImpl::Initialize(bool calculate_hash) { + return file_.Initialize(calculate_hash); +} + +net::Error DownloadFileImpl::AppendDataToFile(const char* data, + size_t data_len) { + return file_.AppendDataToFile(data, data_len); +} + +net::Error DownloadFileImpl::Rename(const FilePath& full_path) { + return file_.Rename(full_path); +} + +void DownloadFileImpl::Detach() { + file_.Detach(); +} + +void DownloadFileImpl::Cancel() { + file_.Cancel(); +} + +void DownloadFileImpl::Finish() { + file_.Finish(); +} + +void DownloadFileImpl::AnnotateWithSourceInformation() { + file_.AnnotateWithSourceInformation(); +} + +FilePath DownloadFileImpl::FullPath() const { + return file_.full_path(); +} + +bool DownloadFileImpl::InProgress() const { + return file_.in_progress(); +} + +int64 DownloadFileImpl::BytesSoFar() const { + return file_.bytes_so_far(); +} + +bool DownloadFileImpl::GetSha256Hash(std::string* hash) { + return file_.GetSha256Hash(hash); +} + +// DownloadFileInterface implementation. +void DownloadFileImpl::CancelDownloadRequest() { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); + request_handle_->CancelRequest(); +} + +int DownloadFileImpl::Id() const { + return id_.local(); +} + +DownloadManager* DownloadFileImpl::GetDownloadManager() { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); + return download_manager_.get(); +} + +const DownloadId& DownloadFileImpl::GlobalId() const { + return id_; +} + +std::string DownloadFileImpl::DebugString() const { + return base::StringPrintf("{" + " id_ = " "%d" + " request_handle = %s" + " Base File = %s" + " }", + id_.local(), + request_handle_->DebugString().c_str(), + file_.DebugString().c_str()); +} diff --git a/content/browser/download/download_file_impl.h b/content/browser/download/download_file_impl.h new file mode 100644 index 0000000..31073c5 --- /dev/null +++ b/content/browser/download/download_file_impl.h @@ -0,0 +1,66 @@ +// 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. + +#ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_ +#define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_ +#pragma once + +#include "content/browser/download/download_file.h" + +#include <string> + +#include "base/basictypes.h" +#include "base/memory/ref_counted.h" +#include "content/browser/download/download_request_handle.h" +#include "content/common/content_export.h" + +struct DownloadCreateInfo; +class DownloadManager; + +class CONTENT_EXPORT DownloadFileImpl : virtual public DownloadFile { + public: + // Takes ownership of the object pointed to by |request_handle|. + DownloadFileImpl(const DownloadCreateInfo* info, + DownloadRequestHandleInterface* request_handle, + DownloadManager* download_manager); + virtual ~DownloadFileImpl(); + + // DownloadFile functions. + virtual net::Error Initialize(bool calculate_hash) OVERRIDE; + virtual net::Error AppendDataToFile(const char* data, + size_t data_len) OVERRIDE; + virtual net::Error Rename(const FilePath& full_path) OVERRIDE; + virtual void Detach() OVERRIDE; + virtual void Cancel() OVERRIDE; + virtual void Finish() OVERRIDE; + virtual void AnnotateWithSourceInformation() OVERRIDE; + virtual FilePath FullPath() const OVERRIDE; + virtual bool InProgress() const OVERRIDE; + virtual int64 BytesSoFar() const OVERRIDE; + virtual bool GetSha256Hash(std::string* hash) OVERRIDE; + virtual void CancelDownloadRequest() OVERRIDE; + virtual int Id() const OVERRIDE; + virtual DownloadManager* GetDownloadManager() OVERRIDE; + virtual const DownloadId& GlobalId() const OVERRIDE; + virtual std::string DebugString() const OVERRIDE; + + private: + // The base file instance. + BaseFile file_; + + // The unique identifier for this download, assigned at creation by + // the DownloadFileManager for its internal record keeping. + DownloadId id_; + + // The handle to the request information. Used for operations outside the + // download system, specifically canceling a download. + scoped_ptr<DownloadRequestHandleInterface> request_handle_; + + // DownloadManager this download belongs to. + scoped_refptr<DownloadManager> download_manager_; + + DISALLOW_COPY_AND_ASSIGN(DownloadFileImpl); +}; + +#endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_ diff --git a/content/browser/download/download_file_manager.cc b/content/browser/download/download_file_manager.cc index 97a4280..55d4097 100644 --- a/content/browser/download/download_file_manager.cc +++ b/content/browser/download/download_file_manager.cc @@ -15,7 +15,7 @@ #include "content/browser/download/base_file.h" #include "content/browser/download/download_buffer.h" #include "content/browser/download/download_create_info.h" -#include "content/browser/download/download_file.h" +#include "content/browser/download/download_file_impl.h" #include "content/browser/download/download_manager.h" #include "content/browser/download/download_request_handle.h" #include "content/browser/download/download_stats.h" @@ -67,10 +67,10 @@ void DownloadFileManager::CreateDownloadFile( // Life of |info| ends here. No more references to it after this method. scoped_ptr<DownloadCreateInfo> infop(info); - scoped_ptr<DownloadFile> - download_file(new DownloadFile(info, - new DownloadRequestHandle(request_handle), - download_manager)); + scoped_ptr<DownloadFile> download_file( + new DownloadFileImpl(info, + new DownloadRequestHandle(request_handle), + download_manager)); if (net::OK != download_file->Initialize(get_hash)) { request_handle.CancelRequest(); return; @@ -90,7 +90,8 @@ void DownloadFileManager::CreateDownloadFile( info->download_id.local())); } -DownloadFile* DownloadFileManager::GetDownloadFile(DownloadId global_id) { +DownloadFile* DownloadFileManager::GetDownloadFile( + DownloadId global_id) { DownloadFileMap::iterator it = downloads_.find(global_id); return it == downloads_.end() ? NULL : it->second; } @@ -119,7 +120,7 @@ void DownloadFileManager::UpdateInProgressDownloads() { if (manager) { BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(&DownloadManager::UpdateDownload, manager, - global_id.local(), download_file->bytes_so_far())); + global_id.local(), download_file->BytesSoFar())); } } } @@ -173,7 +174,7 @@ void DownloadFileManager::UpdateDownload( DownloadManager* download_manager = download_file->GetDownloadManager(); had_error = true; - int64 bytes_downloaded = download_file->bytes_so_far(); + int64 bytes_downloaded = download_file->BytesSoFar(); // Calling this here in case we get more data, to avoid // processing data after an error. That could lead to // files that are corrupted if the later processing succeeded. @@ -223,13 +224,13 @@ void DownloadFileManager::OnResponseCompleted( BrowserThread::UI, FROM_HERE, base::Bind(&DownloadManager::OnResponseCompleted, download_manager, global_id.local(), - download_file->bytes_so_far(), hash)); + download_file->BytesSoFar(), hash)); } else { BrowserThread::PostTask( BrowserThread::UI, FROM_HERE, base::Bind(&DownloadManager::OnDownloadInterrupted, download_manager, global_id.local(), - download_file->bytes_so_far(), reason)); + download_file->BytesSoFar(), reason)); } // We need to keep the download around until the UI thread has finalized // the name. @@ -287,7 +288,7 @@ void DownloadFileManager::OnDownloadManagerShutdown(DownloadManager* manager) { for (std::set<DownloadFile*>::iterator i = to_remove.begin(); i != to_remove.end(); ++i) { - downloads_.erase((*i)->global_id()); + downloads_.erase((*i)->GlobalId()); delete *i; } } @@ -407,7 +408,7 @@ void DownloadFileManager::CancelDownloadOnRename( BrowserThread::UI, FROM_HERE, base::Bind(&DownloadManager::OnDownloadInterrupted, download_manager, global_id.local(), - download_file->bytes_so_far(), + download_file->BytesSoFar(), ConvertNetErrorToInterruptReason( rename_error, DOWNLOAD_INTERRUPT_FROM_DISK))); } diff --git a/content/browser/download/download_file_unittest.cc b/content/browser/download/download_file_unittest.cc index 0091125..2fcb51c 100644 --- a/content/browser/download/download_file_unittest.cc +++ b/content/browser/download/download_file_unittest.cc @@ -7,7 +7,7 @@ #include "base/string_number_conversions.h" #include "content/browser/browser_thread_impl.h" #include "content/browser/download/download_create_info.h" -#include "content/browser/download/download_file.h" +#include "content/browser/download/download_file_impl.h" #include "content/browser/download/download_id.h" #include "content/browser/download/download_id_factory.h" #include "content/browser/download/download_manager.h" @@ -72,20 +72,20 @@ class DownloadFileTest : public testing::Test { // info.request_handle default constructed to null. info.save_info.file_stream = file_stream_; file->reset( - new DownloadFile(&info, new DownloadRequestHandle(), - download_manager_)); + new DownloadFileImpl(&info, new DownloadRequestHandle(), + download_manager_)); } virtual void DestroyDownloadFile(scoped_ptr<DownloadFile>* file, int offset) { - EXPECT_EQ(kDummyDownloadId + offset, (*file)->id()); + EXPECT_EQ(kDummyDownloadId + offset, (*file)->Id()); EXPECT_EQ(download_manager_, (*file)->GetDownloadManager()); - EXPECT_FALSE((*file)->in_progress()); + EXPECT_FALSE((*file)->InProgress()); EXPECT_EQ(static_cast<int64>(expected_data_.size()), - (*file)->bytes_so_far()); + (*file)->BytesSoFar()); // Make sure the data has been properly written to disk. std::string disk_data; - EXPECT_TRUE(file_util::ReadFileToString((*file)->full_path(), + EXPECT_TRUE(file_util::ReadFileToString((*file)->FullPath(), &disk_data)); EXPECT_EQ(expected_data_, disk_data); @@ -96,11 +96,11 @@ class DownloadFileTest : public testing::Test { void AppendDataToFile(scoped_ptr<DownloadFile>* file, const std::string& data) { - EXPECT_TRUE((*file)->in_progress()); + EXPECT_TRUE((*file)->InProgress()); (*file)->AppendDataToFile(data.data(), data.size()); expected_data_ += data; EXPECT_EQ(static_cast<int64>(expected_data_.size()), - (*file)->bytes_so_far()); + (*file)->BytesSoFar()); } protected: @@ -141,7 +141,7 @@ const int DownloadFileTest::kDummyRequestId = 67; TEST_F(DownloadFileTest, RenameFileFinal) { CreateDownloadFile(&download_file_, 0); ASSERT_EQ(net::OK, download_file_->Initialize(true)); - FilePath initial_path(download_file_->full_path()); + FilePath initial_path(download_file_->FullPath()); EXPECT_TRUE(file_util::PathExists(initial_path)); FilePath path_1(initial_path.InsertBeforeExtensionASCII("_1")); FilePath path_2(initial_path.InsertBeforeExtensionASCII("_2")); @@ -150,7 +150,7 @@ TEST_F(DownloadFileTest, RenameFileFinal) { // Rename the file before downloading any data. EXPECT_EQ(net::OK, download_file_->Rename(path_1)); - FilePath renamed_path = download_file_->full_path(); + FilePath renamed_path = download_file_->FullPath(); EXPECT_EQ(path_1, renamed_path); // Check the files. @@ -163,7 +163,7 @@ TEST_F(DownloadFileTest, RenameFileFinal) { // Rename the file after downloading some data. EXPECT_EQ(net::OK, download_file_->Rename(path_2)); - renamed_path = download_file_->full_path(); + renamed_path = download_file_->FullPath(); EXPECT_EQ(path_2, renamed_path); // Check the files. @@ -174,7 +174,7 @@ TEST_F(DownloadFileTest, RenameFileFinal) { // Rename the file after downloading all the data. EXPECT_EQ(net::OK, download_file_->Rename(path_3)); - renamed_path = download_file_->full_path(); + renamed_path = download_file_->FullPath(); EXPECT_EQ(path_3, renamed_path); // Check the files. @@ -189,7 +189,7 @@ TEST_F(DownloadFileTest, RenameFileFinal) { // Rename the file after downloading all the data and closing the file. EXPECT_EQ(net::OK, download_file_->Rename(path_4)); - renamed_path = download_file_->full_path(); + renamed_path = download_file_->FullPath(); EXPECT_EQ(path_4, renamed_path); // Check the files. diff --git a/content/browser/download/download_resource_handler.h b/content/browser/download/download_resource_handler.h index 016756c..07cfd13 100644 --- a/content/browser/download/download_resource_handler.h +++ b/content/browser/download/download_resource_handler.h @@ -13,6 +13,7 @@ #include "base/timer.h" #include "content/browser/download/download_file.h" #include "content/browser/download/download_id.h" +#include "content/browser/download/download_types.h" #include "content/browser/renderer_host/global_request_id.h" #include "content/browser/renderer_host/resource_handler.h" diff --git a/content/browser/download/drag_download_file.cc b/content/browser/download/drag_download_file.cc index b8bccb6..56898d5 100644 --- a/content/browser/download/drag_download_file.cc +++ b/content/browser/download/drag_download_file.cc @@ -10,6 +10,7 @@ #include "content/browser/browser_context.h" #include "content/browser/download/download_item.h" #include "content/browser/download/download_stats.h" +#include "content/browser/download/download_types.h" #include "content/browser/tab_contents/tab_contents.h" #include "content/public/browser/browser_thread.h" #include "net/base/file_stream.h" diff --git a/content/browser/download/mock_download_file.cc b/content/browser/download/mock_download_file.cc new file mode 100755 index 0000000..8165bc0 --- /dev/null +++ b/content/browser/download/mock_download_file.cc @@ -0,0 +1,134 @@ +// 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 "content/browser/download/mock_download_file.h" + +#include "content/browser/download/download_create_info.h" +#include "testing/gtest/include/gtest/gtest.h" + +MockDownloadFile::StatisticsRecorder::StatisticsRecorder() { +} + +MockDownloadFile::StatisticsRecorder::~StatisticsRecorder() { +} + +void MockDownloadFile::StatisticsRecorder::Record(StatisticsIndex index) { + Add(index, 1); +} + +void MockDownloadFile::StatisticsRecorder::Add(StatisticsIndex index, + int count) { + map_[index] = map_[index] + count; +} + +int MockDownloadFile::StatisticsRecorder::Count(StatisticsIndex index) { + if (map_.find(index) == map_.end()) + return 0; + return map_[index]; +} + +MockDownloadFile::MockDownloadFile( + const DownloadCreateInfo* info, + const DownloadRequestHandle& request_handle, + DownloadManager* download_manager, + StatisticsRecorder* recorder) + : id_(info->download_id), + request_handle_(request_handle), + download_manager_(download_manager), + recorder_(recorder), + rename_count_(0), + in_progress_(true) { +} + +MockDownloadFile::~MockDownloadFile() { +} + +net::Error MockDownloadFile::Initialize(bool calculate_hash) { + in_progress_ = true; + if (recorder_) + recorder_->Record(StatisticsRecorder::STAT_INITIALIZE); + return net::OK; +} + +net::Error MockDownloadFile::AppendDataToFile( + const char* data, size_t data_len) { + data_.append(data, data_len); + if (recorder_) { + recorder_->Record(StatisticsRecorder::STAT_APPEND); + recorder_->Add(StatisticsRecorder::STAT_BYTES, data_len); + } + return net::OK; +} + +net::Error MockDownloadFile::Rename(const FilePath& full_path) { + EXPECT_LT(rename_count_, expected_rename_path_list_.size()); + EXPECT_STREQ(expected_rename_path_list_[rename_count_].value().c_str(), + full_path.value().c_str()); + ++rename_count_; + if (recorder_) + recorder_->Record(StatisticsRecorder::STAT_RENAME); + return net::OK; +} + +void MockDownloadFile::Detach() { + if (recorder_) + recorder_->Record(StatisticsRecorder::STAT_DETACH); +} + +void MockDownloadFile::Cancel() { + in_progress_ = false; + if (recorder_) + recorder_->Record(StatisticsRecorder::STAT_CANCEL); +} + +void MockDownloadFile::Finish() { + in_progress_ = false; + if (recorder_) + recorder_->Record(StatisticsRecorder::STAT_FINISH); +} + +void MockDownloadFile::AnnotateWithSourceInformation() { +} + +FilePath MockDownloadFile::FullPath() const { + return FilePath(); +} + +bool MockDownloadFile::InProgress() const { + return in_progress_; +} + +int64 MockDownloadFile::BytesSoFar() const { + return data_.length(); +} + +bool MockDownloadFile::GetSha256Hash(std::string* hash) { + return false; +} + +// DownloadFileInterface implementation. +void MockDownloadFile::CancelDownloadRequest() { +} + +int MockDownloadFile::Id() const { + return id_.local(); +} + +DownloadManager* MockDownloadFile::GetDownloadManager() { + return download_manager_; +} + +const DownloadId& MockDownloadFile::GlobalId() const { + return id_; +} + +std::string MockDownloadFile::DebugString() const { + return ""; +} + +void MockDownloadFile::SetExpectedPath(size_t index, const FilePath& path) { + if (expected_rename_path_list_.size() < index + 1) + expected_rename_path_list_.resize(index + 1); + expected_rename_path_list_[index] = path; +} diff --git a/content/browser/download/mock_download_file.h b/content/browser/download/mock_download_file.h new file mode 100755 index 0000000..ed20203 --- /dev/null +++ b/content/browser/download/mock_download_file.h @@ -0,0 +1,116 @@ +// 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. + +#ifndef CONTENT_BROWSER_DOWNLOAD_MOCK_DOWNLOAD_FILE_H_ +#define CONTENT_BROWSER_DOWNLOAD_MOCK_DOWNLOAD_FILE_H_ +#pragma once + +#include <string> +#include <map> + +#include "base/file_path.h" +#include "base/memory/ref_counted.h" +#include "content/browser/download/download_file.h" +#include "content/browser/download/download_id.h" +#include "content/browser/download/download_manager.h" +#include "content/browser/download/download_request_handle.h" +#include "net/base/net_errors.h" + +struct DownloadCreateInfo; + +class MockDownloadFile : virtual public DownloadFile { + public: + // Class to extract statistics from the usage of |MockDownloadFile|. + class StatisticsRecorder { + public: + enum StatisticsIndex { + STAT_INITIALIZE, + STAT_APPEND, + STAT_RENAME, + STAT_DETACH, + STAT_CANCEL, + STAT_FINISH, + STAT_BYTES + }; + + StatisticsRecorder(); + ~StatisticsRecorder(); + + // Records the statistic. + // |index| indicates what statistic to use. + void Record(StatisticsIndex index); + + // Adds to the statistic. + // |index| indicates what statistic to use. + void Add(StatisticsIndex index, int count); + + // Returns the statistic value. + // |index| indicates what statistic to use. + int Count(StatisticsIndex index); + + private: + typedef std::map<StatisticsIndex, int> StatisticsMap; + + StatisticsMap map_; + }; + + MockDownloadFile(const DownloadCreateInfo* info, + const DownloadRequestHandle& request_handle, + DownloadManager* download_manager, + StatisticsRecorder* recorder); + virtual ~MockDownloadFile(); + + // DownloadFile functions. + virtual net::Error Initialize(bool calculate_hash) OVERRIDE; + virtual net::Error AppendDataToFile(const char* data, + size_t data_len) OVERRIDE; + virtual net::Error Rename(const FilePath& full_path) OVERRIDE; + virtual void Detach() OVERRIDE; + virtual void Cancel() OVERRIDE; + virtual void Finish() OVERRIDE; + virtual void AnnotateWithSourceInformation() OVERRIDE; + virtual FilePath FullPath() const OVERRIDE; + virtual bool InProgress() const OVERRIDE; + virtual int64 BytesSoFar() const OVERRIDE; + virtual bool GetSha256Hash(std::string* hash) OVERRIDE; + virtual void CancelDownloadRequest() OVERRIDE; + virtual int Id() const OVERRIDE; + virtual DownloadManager* GetDownloadManager() OVERRIDE; + virtual const DownloadId& GlobalId() const OVERRIDE; + virtual std::string DebugString() const OVERRIDE; + + // Functions relating to setting and checking expectations. + size_t rename_count() const { return rename_count_; } + void SetExpectedPath(size_t index, const FilePath& path); + + private: + // The unique identifier for this download, assigned at creation by + // the DownloadFileManager for its internal record keeping. + DownloadId id_; + + // The handle to the request information. Used for operations outside the + // download system, specifically canceling a download. + DownloadRequestHandle request_handle_; + + // DownloadManager this download belongs to. + scoped_refptr<DownloadManager> download_manager_; + + // Records usage statistics. Not owned by this class (survives destruction). + StatisticsRecorder* recorder_; + + // The number of times |Rename()| has been called. + // Used instead of checking |recorder_| because the latter can be NULL. + size_t rename_count_; + + // A vector of paths that we expect to call |Rename()| with. + std::vector<FilePath> expected_rename_path_list_; + + // A buffer to hold the data we write. + std::string data_; + + // Dummy in-progress flag. + bool in_progress_; +}; + +#endif // CONTENT_BROWSER_DOWNLOAD_MOCK_DOWNLOAD_FILE_H_ diff --git a/content/browser/download/save_file.cc b/content/browser/download/save_file.cc index 980f46e..599ad76 100644 --- a/content/browser/download/save_file.cc +++ b/content/browser/download/save_file.cc @@ -11,7 +11,7 @@ using content::BrowserThread; SaveFile::SaveFile(const SaveFileCreateInfo* info) - : BaseFile(FilePath(), info->url, GURL(), 0, linked_ptr<net::FileStream>()), + : file_(FilePath(), info->url, GURL(), 0, linked_ptr<net::FileStream>()), info_(info) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); @@ -22,3 +22,51 @@ SaveFile::SaveFile(const SaveFileCreateInfo* info) SaveFile::~SaveFile() { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); } + +net::Error SaveFile::Initialize(bool calculate_hash) { + return file_.Initialize(calculate_hash); +} + +net::Error SaveFile::AppendDataToFile(const char* data, size_t data_len) { + return file_.AppendDataToFile(data, data_len); +} + +net::Error SaveFile::Rename(const FilePath& full_path) { + return file_.Rename(full_path); +} + +void SaveFile::Detach() { + file_.Detach(); +} + +void SaveFile::Cancel() { + file_.Cancel(); +} + +void SaveFile::Finish() { + file_.Finish(); +} + +void SaveFile::AnnotateWithSourceInformation() { + file_.AnnotateWithSourceInformation(); +} + +FilePath SaveFile::FullPath() const { + return file_.full_path(); +} + +bool SaveFile::InProgress() const { + return file_.in_progress(); +} + +int64 SaveFile::BytesSoFar() const { + return file_.bytes_so_far(); +} + +bool SaveFile::GetSha256Hash(std::string* hash) { + return file_.GetSha256Hash(hash); +} + +std::string SaveFile::DebugString() const { + return file_.DebugString(); +} diff --git a/content/browser/download/save_file.h b/content/browser/download/save_file.h index e7faaeb..baa863d 100644 --- a/content/browser/download/save_file.h +++ b/content/browser/download/save_file.h @@ -19,11 +19,25 @@ // the saving job is 'in progress': once the saving job has been completed or // canceled, the SaveFile is destroyed. One SaveFile object represents one item // in a save session. -class SaveFile : public BaseFile { +class SaveFile { public: explicit SaveFile(const SaveFileCreateInfo* info); virtual ~SaveFile(); + // BaseFile delegated functions. + net::Error Initialize(bool calculate_hash); + net::Error AppendDataToFile(const char* data, size_t data_len); + net::Error Rename(const FilePath& full_path); + void Detach(); + void Cancel(); + void Finish(); + void AnnotateWithSourceInformation(); + FilePath FullPath() const; + bool InProgress() const; + int64 BytesSoFar() const; + bool GetSha256Hash(std::string* hash); + std::string DebugString() const; + // Accessors. int save_id() const { return info_->save_id; } int render_process_id() const { return info_->render_process_id; } @@ -34,6 +48,7 @@ class SaveFile : public BaseFile { } private: + BaseFile file_; scoped_ptr<const SaveFileCreateInfo> info_; DISALLOW_COPY_AND_ASSIGN(SaveFile); diff --git a/content/browser/download/save_file_manager.cc b/content/browser/download/save_file_manager.cc index 5325336..99a9f49 100644 --- a/content/browser/download/save_file_manager.cc +++ b/content/browser/download/save_file_manager.cc @@ -220,7 +220,7 @@ void SaveFileManager::StartSave(SaveFileCreateInfo* info) { DCHECK(!LookupSaveFile(info->save_id)); save_file_map_[info->save_id] = save_file; - info->path = save_file->full_path(); + info->path = save_file->FullPath(); BrowserThread::PostTask( BrowserThread::UI, FROM_HERE, @@ -244,7 +244,7 @@ void SaveFileManager::UpdateSaveProgress(int save_id, base::Bind(&SaveFileManager::OnUpdateSaveProgress, this, save_file->save_id(), - save_file->bytes_so_far(), + save_file->BytesSoFar(), write_success == net::OK)); } } @@ -272,7 +272,7 @@ void SaveFileManager::SaveFinished(int save_id, BrowserThread::PostTask( BrowserThread::UI, FROM_HERE, base::Bind(&SaveFileManager::OnSaveFinished, this, save_id, - save_file->bytes_so_far(), is_success)); + save_file->BytesSoFar(), is_success)); save_file->Finish(); save_file->Detach(); @@ -413,7 +413,7 @@ void SaveFileManager::CancelSave(int save_id) { // data from other sources or have finished. DCHECK(save_file->save_source() != SaveFileCreateInfo::SAVE_FILE_FROM_NET || - !save_file->in_progress()); + !save_file->InProgress()); } // Whatever the save file is renamed or not, just delete it. save_file_map_.erase(it); @@ -432,7 +432,7 @@ void SaveFileManager::SaveLocalFile(const GURL& original_file_url, if (!save_file) return; // If it has finished, just return. - if (!save_file->in_progress()) + if (!save_file->InProgress()) return; // Close the save file before the copy operation. @@ -449,9 +449,9 @@ void SaveFileManager::SaveLocalFile(const GURL& original_file_url, // Copy the local file to the temporary file. It will be renamed to its // final name later. - bool success = file_util::CopyFile(file_path, save_file->full_path()); + bool success = file_util::CopyFile(file_path, save_file->FullPath()); if (!success) - file_util::Delete(save_file->full_path(), false); + file_util::Delete(save_file->FullPath(), false); SaveFinished(save_id, original_file_url, render_process_id, success); } @@ -479,7 +479,7 @@ void SaveFileManager::RenameAllFiles( SaveFileMap::iterator it = save_file_map_.find(i->first); if (it != save_file_map_.end()) { SaveFile* save_file = it->second; - DCHECK(!save_file->in_progress()); + DCHECK(!save_file->InProgress()); save_file->Rename(i->second); delete save_file; save_file_map_.erase(it); @@ -513,8 +513,8 @@ void SaveFileManager::RemoveSavedFileFromFileMap( SaveFileMap::iterator it = save_file_map_.find(*i); if (it != save_file_map_.end()) { SaveFile* save_file = it->second; - DCHECK(!save_file->in_progress()); - file_util::Delete(save_file->full_path(), false); + DCHECK(!save_file->InProgress()); + file_util::Delete(save_file->FullPath(), false); delete save_file; save_file_map_.erase(it); } diff --git a/content/browser/renderer_host/buffered_resource_handler.cc b/content/browser/renderer_host/buffered_resource_handler.cc index f639a03..826b156 100644 --- a/content/browser/renderer_host/buffered_resource_handler.cc +++ b/content/browser/renderer_host/buffered_resource_handler.cc @@ -12,6 +12,7 @@ #include "base/string_util.h" #include "content/browser/download/download_id_factory.h" #include "content/browser/download/download_resource_handler.h" +#include "content/browser/download/download_types.h" #include "content/browser/plugin_service.h" #include "content/browser/renderer_host/resource_dispatcher_host.h" #include "content/browser/renderer_host/resource_dispatcher_host_request_info.h" |