diff options
author | hidehiko@chromium.org <hidehiko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-05-13 07:14:34 +0000 |
---|---|---|
committer | hidehiko@chromium.org <hidehiko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-05-13 07:14:34 +0000 |
commit | 17807c635ba750e0de0168e9f83877bc5ae306e7 (patch) | |
tree | 99c0aef562d2b0440c016bdf573075b8ec0f0a68 | |
parent | 9896e7e665f82882ae2aaa7c1c856a1920166405 (diff) | |
download | chromium_src-17807c635ba750e0de0168e9f83877bc5ae306e7.zip chromium_src-17807c635ba750e0de0168e9f83877bc5ae306e7.tar.gz chromium_src-17807c635ba750e0de0168e9f83877bc5ae306e7.tar.bz2 |
Get rid of IOBuffer from ResumeUploadOperation.
Now, UrlFetcher supports to upload a local file directly. This CL replaces
IOBuffer based uploading with the local file direct uploading.
BUG=234178
TEST=Ran unit_tests
R=hashimoto@chromium.org, kinaba@chromium.org
Review URL: https://codereview.chromium.org/14556011
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@199677 0039d316-1c4b-4281-b951-d872f2087c98
22 files changed, 199 insertions, 221 deletions
diff --git a/chrome/browser/google_apis/base_operations.cc b/chrome/browser/google_apis/base_operations.cc index 8558d68..640102e 100644 --- a/chrome/browser/google_apis/base_operations.cc +++ b/chrome/browser/google_apis/base_operations.cc @@ -11,7 +11,6 @@ #include "base/threading/sequenced_worker_pool.h" #include "base/values.h" #include "content/public/browser/browser_thread.h" -#include "net/base/io_buffer.h" #include "net/base/load_flags.h" #include "net/http/http_byte_range.h" #include "net/http/http_response_headers.h" @@ -176,16 +175,29 @@ void UrlFetchOperationBase::Start(const std::string& access_token, if (GetContentData(&upload_content_type, &upload_content)) { url_fetcher_->SetUploadData(upload_content_type, upload_content); } else { - // Even if there is no content data, UrlFetcher requires to set empty - // upload data string for POST, PUT and PATCH methods, explicitly. - // It is because that most requests of those methods have non-empty - // body, and UrlFetcher checks whether it is actually not forgotten. - if (request_type == URLFetcher::POST || - request_type == URLFetcher::PUT || - request_type == URLFetcher::PATCH) { - // Set empty upload content-type and upload content, so that - // the request will have no "Content-type: " header and no content. - url_fetcher_->SetUploadData(std::string(), std::string()); + base::FilePath local_file_path; + int64 range_offset = 0; + int64 range_length = 0; + if (GetContentFile(&local_file_path, &range_offset, &range_length, + &upload_content_type)) { + url_fetcher_->SetUploadFilePath( + upload_content_type, + local_file_path, + range_offset, + range_length, + BrowserThread::GetBlockingPool()); + } else { + // Even if there is no content data, UrlFetcher requires to set empty + // upload data string for POST, PUT and PATCH methods, explicitly. + // It is because that most requests of those methods have non-empty + // body, and UrlFetcher checks whether it is actually not forgotten. + if (request_type == URLFetcher::POST || + request_type == URLFetcher::PUT || + request_type == URLFetcher::PATCH) { + // Set empty upload content-type and upload content, so that + // the request will have no "Content-type: " header and no content. + url_fetcher_->SetUploadData(std::string(), std::string()); + } } } @@ -209,6 +221,13 @@ bool UrlFetchOperationBase::GetContentData(std::string* upload_content_type, return false; } +bool UrlFetchOperationBase::GetContentFile(base::FilePath* local_file_path, + int64* range_offset, + int64* range_length, + std::string* upload_content_type) { + return false; +} + void UrlFetchOperationBase::DoCancel() { url_fetcher_.reset(NULL); RunCallbackOnPrematureFailure(GDATA_CANCELLED); @@ -588,7 +607,7 @@ ResumeUploadOperationBase::ResumeUploadOperationBase( int64 end_position, int64 content_length, const std::string& content_type, - const scoped_refptr<net::IOBuffer>& buf) + const base::FilePath& local_file_path) : UploadRangeOperationBase(registry, url_request_context_getter, upload_mode, @@ -598,7 +617,7 @@ ResumeUploadOperationBase::ResumeUploadOperationBase( end_position_(end_position), content_length_(content_length), content_type_(content_type), - buf_(buf) { + local_file_path_(local_file_path) { DCHECK_LE(start_position_, end_position_); } @@ -631,11 +650,20 @@ ResumeUploadOperationBase::GetExtraRequestHeaders() const { return headers; } -bool ResumeUploadOperationBase::GetContentData( - std::string* upload_content_type, - std::string* upload_content) { +bool ResumeUploadOperationBase::GetContentFile( + base::FilePath* local_file_path, + int64* range_offset, + int64* range_length, + std::string* upload_content_type) { + if (start_position_ == end_position_) { + // No content data. + return false; + } + + *local_file_path = local_file_path_; + *range_offset = start_position_; + *range_length = end_position_ - start_position_; *upload_content_type = content_type_; - *upload_content = std::string(buf_->data(), end_position_ - start_position_); return true; } diff --git a/chrome/browser/google_apis/base_operations.h b/chrome/browser/google_apis/base_operations.h index 7d17db5..4aa5dab 100644 --- a/chrome/browser/google_apis/base_operations.h +++ b/chrome/browser/google_apis/base_operations.h @@ -24,7 +24,6 @@ class Value; } // namespace base namespace net { -class IOBuffer; class URLRequestContextGetter; } // namespace net @@ -123,9 +122,20 @@ class UrlFetchOperationBase : public AuthenticatedOperationInterface, // Used by a derived class to add any content data to the request. // Returns true if |upload_content_type| and |upload_content| are updated // with the content type and data for the request. + // Note that this and GetContentFile() cannot be used together. virtual bool GetContentData(std::string* upload_content_type, std::string* upload_content); + // Used by a derived class to add content data which is the whole file or + // a part of the file at |local_file_path|. + // Returns true if all the arguments are updated for the content being + // uploaded. + // Note that this and GetContentData() cannot be used together. + virtual bool GetContentFile(base::FilePath* local_file_path, + int64* range_offset, + int64* range_length, + std::string* upload_content_type); + // Invoked by OnURLFetchComplete when the operation completes without an // authentication error. Must be implemented by a derived class. virtual void ProcessURLFetchResults(const net::URLFetcher* source) = 0; @@ -419,13 +429,15 @@ class ResumeUploadOperationBase : public UploadRangeOperationBase { int64 end_position, int64 content_length, const std::string& content_type, - const scoped_refptr<net::IOBuffer>& buf); + const base::FilePath& local_file_path); virtual ~ResumeUploadOperationBase(); // UrlFetchOperationBase overrides. virtual std::vector<std::string> GetExtraRequestHeaders() const OVERRIDE; - virtual bool GetContentData(std::string* upload_content_type, - std::string* upload_content) OVERRIDE; + virtual bool GetContentFile(base::FilePath* local_file_path, + int64* range_offset, + int64* range_length, + std::string* upload_content_type) OVERRIDE; private: // The parameters for the request. See ResumeUploadParams for the details. @@ -433,7 +445,7 @@ class ResumeUploadOperationBase : public UploadRangeOperationBase { const int64 end_position_; const int64 content_length_; const std::string content_type_; - const scoped_refptr<net::IOBuffer> buf_; + const base::FilePath local_file_path_; DISALLOW_COPY_AND_ASSIGN(ResumeUploadOperationBase); }; diff --git a/chrome/browser/google_apis/drive_api_operations.cc b/chrome/browser/google_apis/drive_api_operations.cc index cd6ab78..663820d 100644 --- a/chrome/browser/google_apis/drive_api_operations.cc +++ b/chrome/browser/google_apis/drive_api_operations.cc @@ -541,7 +541,7 @@ ResumeUploadOperation::ResumeUploadOperation( int64 end_position, int64 content_length, const std::string& content_type, - const scoped_refptr<net::IOBuffer>& buf, + const base::FilePath& local_file_path, const UploadRangeCallback& callback, const ProgressCallback& progress_callback) : ResumeUploadOperationBase(registry, @@ -553,7 +553,7 @@ ResumeUploadOperation::ResumeUploadOperation( end_position, content_length, content_type, - buf), + local_file_path), callback_(callback), progress_callback_(progress_callback) { DCHECK(!callback_.is_null()); diff --git a/chrome/browser/google_apis/drive_api_operations.h b/chrome/browser/google_apis/drive_api_operations.h index 24a9e13..8f7d682 100644 --- a/chrome/browser/google_apis/drive_api_operations.h +++ b/chrome/browser/google_apis/drive_api_operations.h @@ -488,7 +488,7 @@ class ResumeUploadOperation : public ResumeUploadOperationBase { int64 end_position, int64 content_length, const std::string& content_type, - const scoped_refptr<net::IOBuffer>& buf, + const base::FilePath& local_file_path, const UploadRangeCallback& callback, const ProgressCallback& progress_callback); virtual ~ResumeUploadOperation(); diff --git a/chrome/browser/google_apis/drive_api_operations_unittest.cc b/chrome/browser/google_apis/drive_api_operations_unittest.cc index e706754..66e8e57 100644 --- a/chrome/browser/google_apis/drive_api_operations_unittest.cc +++ b/chrome/browser/google_apis/drive_api_operations_unittest.cc @@ -3,7 +3,9 @@ // found in the LICENSE file. #include "base/bind.h" +#include "base/file_util.h" #include "base/files/file_path.h" +#include "base/files/scoped_temp_dir.h" #include "base/message_loop/message_loop_proxy.h" #include "base/strings/string_number_conversions.h" #include "base/values.h" @@ -59,6 +61,8 @@ class DriveApiOperationsTest : public testing::Test { content::BrowserThread::GetMessageLoopProxyForThread( content::BrowserThread::IO)); + ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); + ASSERT_TRUE(test_server_.InitializeAndWaitUntilReady()); test_server_.RegisterRequestHandler( base::Bind(&DriveApiOperationsTest::HandleChildrenDeleteRequest, @@ -99,6 +103,7 @@ class DriveApiOperationsTest : public testing::Test { OperationRegistry operation_registry_; scoped_ptr<DriveApiUrlGenerator> url_generator_; scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; + base::ScopedTempDir temp_dir_; // This is a path to the file which contains expected response from // the server. See also HandleDataFileRequest below. @@ -670,6 +675,9 @@ TEST_F(DriveApiOperationsTest, UploadNewFileOperation) { const char kTestContentType[] = "text/plain"; const std::string kTestContent(100, 'a'); + const base::FilePath kTestFilePath = + temp_dir_.path().AppendASCII("upload_file.txt"); + ASSERT_TRUE(test_util::WriteStringToFile(kTestFilePath, kTestContent)); GDataErrorCode error = GDATA_OTHER_ERROR; GURL upload_url; @@ -712,8 +720,6 @@ TEST_F(DriveApiOperationsTest, UploadNewFileOperation) { http_request_.content); // 2) Upload the content to the upload URL. - scoped_refptr<net::IOBuffer> buffer = new net::StringIOBuffer(kTestContent); - UploadRangeResponse response; scoped_ptr<FileResource> new_entry; @@ -728,7 +734,7 @@ TEST_F(DriveApiOperationsTest, UploadNewFileOperation) { kTestContent.size(), // end_position (exclusive) kTestContent.size(), // content_length, kTestContentType, - buffer, + kTestFilePath, CreateComposedCallback( base::Bind(&test_util::RunAndQuit), test_util::CreateCopyResultCallback(&response, &new_entry)), @@ -764,6 +770,9 @@ TEST_F(DriveApiOperationsTest, UploadNewEmptyFileOperation) { const char kTestContentType[] = "text/plain"; const char kTestContent[] = ""; + const base::FilePath kTestFilePath = + temp_dir_.path().AppendASCII("empty_file.txt"); + ASSERT_TRUE(test_util::WriteStringToFile(kTestFilePath, kTestContent)); GDataErrorCode error = GDATA_OTHER_ERROR; GURL upload_url; @@ -805,8 +814,6 @@ TEST_F(DriveApiOperationsTest, UploadNewEmptyFileOperation) { http_request_.content); // 2) Upload the content to the upload URL. - scoped_refptr<net::IOBuffer> buffer = new net::StringIOBuffer(kTestContent); - UploadRangeResponse response; scoped_ptr<FileResource> new_entry; @@ -821,7 +828,7 @@ TEST_F(DriveApiOperationsTest, UploadNewEmptyFileOperation) { 0, // end_position (exclusive) 0, // content_length, kTestContentType, - buffer, + kTestFilePath, CreateComposedCallback( base::Bind(&test_util::RunAndQuit), test_util::CreateCopyResultCallback(&response, &new_entry)), @@ -855,6 +862,9 @@ TEST_F(DriveApiOperationsTest, UploadNewLargeFileOperation) { const char kTestContentType[] = "text/plain"; const size_t kNumChunkBytes = 10; // Num bytes in a chunk. const std::string kTestContent(100, 'a'); + const base::FilePath kTestFilePath = + temp_dir_.path().AppendASCII("upload_file.txt"); + ASSERT_TRUE(test_util::WriteStringToFile(kTestFilePath, kTestContent)); GDataErrorCode error = GDATA_OTHER_ERROR; GURL upload_url; @@ -903,7 +913,6 @@ TEST_F(DriveApiOperationsTest, UploadNewLargeFileOperation) { start_position, std::min(kNumChunkBytes, kTestContent.size() - start_position)); const size_t end_position = start_position + payload.size(); - scoped_refptr<net::IOBuffer> buffer = new net::StringIOBuffer(payload); UploadRangeResponse response; scoped_ptr<FileResource> new_entry; @@ -919,7 +928,7 @@ TEST_F(DriveApiOperationsTest, UploadNewLargeFileOperation) { end_position, kTestContent.size(), // content_length, kTestContentType, - buffer, + kTestFilePath, CreateComposedCallback( base::Bind(&test_util::RunAndQuit), test_util::CreateCopyResultCallback(&response, &new_entry)), @@ -966,6 +975,9 @@ TEST_F(DriveApiOperationsTest, UploadExistingFileOperation) { const char kTestContentType[] = "text/plain"; const std::string kTestContent(100, 'a'); + const base::FilePath kTestFilePath = + temp_dir_.path().AppendASCII("upload_file.txt"); + ASSERT_TRUE(test_util::WriteStringToFile(kTestFilePath, kTestContent)); GDataErrorCode error = GDATA_OTHER_ERROR; GURL upload_url; @@ -1002,8 +1014,6 @@ TEST_F(DriveApiOperationsTest, UploadExistingFileOperation) { EXPECT_TRUE(http_request_.content.empty()); // 2) Upload the content to the upload URL. - scoped_refptr<net::IOBuffer> buffer = new net::StringIOBuffer(kTestContent); - UploadRangeResponse response; scoped_ptr<FileResource> new_entry; @@ -1018,7 +1028,7 @@ TEST_F(DriveApiOperationsTest, UploadExistingFileOperation) { kTestContent.size(), // end_position (exclusive) kTestContent.size(), // content_length, kTestContentType, - buffer, + kTestFilePath, CreateComposedCallback( base::Bind(&test_util::RunAndQuit), test_util::CreateCopyResultCallback(&response, &new_entry)), @@ -1054,6 +1064,9 @@ TEST_F(DriveApiOperationsTest, UploadExistingFileOperationWithETag) { const char kTestContentType[] = "text/plain"; const std::string kTestContent(100, 'a'); + const base::FilePath kTestFilePath = + temp_dir_.path().AppendASCII("upload_file.txt"); + ASSERT_TRUE(test_util::WriteStringToFile(kTestFilePath, kTestContent)); GDataErrorCode error = GDATA_OTHER_ERROR; GURL upload_url; @@ -1090,8 +1103,6 @@ TEST_F(DriveApiOperationsTest, UploadExistingFileOperationWithETag) { EXPECT_TRUE(http_request_.content.empty()); // 2) Upload the content to the upload URL. - scoped_refptr<net::IOBuffer> buffer = new net::StringIOBuffer(kTestContent); - UploadRangeResponse response; scoped_ptr<FileResource> new_entry; @@ -1106,7 +1117,7 @@ TEST_F(DriveApiOperationsTest, UploadExistingFileOperationWithETag) { kTestContent.size(), // end_position (exclusive) kTestContent.size(), // content_length, kTestContentType, - buffer, + kTestFilePath, CreateComposedCallback( base::Bind(&test_util::RunAndQuit), test_util::CreateCopyResultCallback(&response, &new_entry)), diff --git a/chrome/browser/google_apis/drive_api_service.cc b/chrome/browser/google_apis/drive_api_service.cc index 98587f55..09661c4 100644 --- a/chrome/browser/google_apis/drive_api_service.cc +++ b/chrome/browser/google_apis/drive_api_service.cc @@ -594,7 +594,7 @@ void DriveAPIService::ResumeUpload( int64 end_position, int64 content_length, const std::string& content_type, - const scoped_refptr<net::IOBuffer>& buf, + const base::FilePath& local_file_path, const UploadRangeCallback& callback, const ProgressCallback& progress_callback) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); @@ -611,7 +611,7 @@ void DriveAPIService::ResumeUpload( end_position, content_length, content_type, - buf, + local_file_path, base::Bind(&ParseResourceEntryForUploadRangeAndRun, callback), progress_callback)); } diff --git a/chrome/browser/google_apis/drive_api_service.h b/chrome/browser/google_apis/drive_api_service.h index c20b6fd..07e5884 100644 --- a/chrome/browser/google_apis/drive_api_service.h +++ b/chrome/browser/google_apis/drive_api_service.h @@ -137,7 +137,7 @@ class DriveAPIService : public DriveServiceInterface, int64 end_position, int64 content_length, const std::string& content_type, - const scoped_refptr<net::IOBuffer>& buf, + const base::FilePath& local_file_path, const UploadRangeCallback& callback, const ProgressCallback& progress_callback) OVERRIDE; virtual void GetUploadStatus( diff --git a/chrome/browser/google_apis/drive_service_interface.h b/chrome/browser/google_apis/drive_service_interface.h index 93dd51a..ca8f780 100644 --- a/chrome/browser/google_apis/drive_service_interface.h +++ b/chrome/browser/google_apis/drive_service_interface.h @@ -14,10 +14,6 @@ class Profile; -namespace net { -class IOBuffer; -} // namespace net - namespace google_apis { class AboutResource; @@ -314,7 +310,7 @@ class DriveServiceInterface { int64 end_position, int64 content_length, const std::string& content_type, - const scoped_refptr<net::IOBuffer>& buf, + const base::FilePath& local_file_path, const UploadRangeCallback& callback, const ProgressCallback& progress_callback) = 0; diff --git a/chrome/browser/google_apis/drive_uploader.cc b/chrome/browser/google_apis/drive_uploader.cc index 2a46471..aabfbc7 100644 --- a/chrome/browser/google_apis/drive_uploader.cc +++ b/chrome/browser/google_apis/drive_uploader.cc @@ -8,15 +8,17 @@ #include "base/bind.h" #include "base/callback.h" +#include "base/file_util.h" #include "base/message_loop.h" #include "base/strings/string_number_conversions.h" +#include "base/task_runner_util.h" +#include "base/threading/sequenced_worker_pool.h" #include "chrome/browser/google_apis/drive_service_interface.h" #include "chrome/browser/google_apis/drive_upload_mode.h" #include "chrome/browser/google_apis/gdata_wapi_parser.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/power_save_blocker.h" #include "net/base/file_stream.h" -#include "net/base/io_buffer.h" #include "net/base/net_errors.h" using content::BrowserThread; @@ -44,8 +46,7 @@ namespace google_apis { // Structure containing current upload information of file, passed between // DriveServiceInterface methods and callbacks. struct DriveUploader::UploadFileInfo { - UploadFileInfo(scoped_refptr<base::SequencedTaskRunner> task_runner, - UploadMode upload_mode, + UploadFileInfo(UploadMode upload_mode, const base::FilePath& drive_path, const base::FilePath& local_path, const std::string& content_type, @@ -59,16 +60,12 @@ struct DriveUploader::UploadFileInfo { progress_callback(progress_callback), content_length(0), next_send_position(0), - file_stream(new net::FileStream(NULL)), - buf(new net::IOBuffer(kUploadChunkSize)), - blocking_task_runner(task_runner), power_save_blocker(content::PowerSaveBlocker::Create( content::PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, "Upload in progress")) { } ~UploadFileInfo() { - blocking_task_runner->DeleteSoon(FROM_HERE, file_stream.release()); } // Bytes left to upload. @@ -114,21 +111,6 @@ struct DriveUploader::UploadFileInfo { // The start position of the contents to be sent as the next upload chunk. int64 next_send_position; - // For opening and reading from physical file. - // - // File operations are posted to |blocking_task_runner|, while the ownership - // of the stream is held in UI thread. At the point when this UploadFileInfo - // is destroyed, the ownership of the stream is passed to the worker pool. - // TODO(kinaba): We should switch to async API of FileStream once - // crbug.com/164312 is fixed. - scoped_ptr<net::FileStream> file_stream; - - // Holds current content to be uploaded. - const scoped_refptr<net::IOBuffer> buf; - - // Runner for net::FileStream tasks. - const scoped_refptr<base::SequencedTaskRunner> blocking_task_runner; - // Blocks system suspend while upload is in progress. scoped_ptr<content::PowerSaveBlocker> power_save_blocker; }; @@ -136,9 +118,6 @@ struct DriveUploader::UploadFileInfo { DriveUploader::DriveUploader(DriveServiceInterface* drive_service) : drive_service_(drive_service), weak_ptr_factory_(this) { - base::SequencedWorkerPool* blocking_pool = BrowserThread::GetBlockingPool(); - blocking_task_runner_ = blocking_pool->GetSequencedTaskRunner( - blocking_pool->GetSequenceToken()); } DriveUploader::~DriveUploader() {} @@ -159,8 +138,7 @@ void DriveUploader::UploadNewFile(const std::string& parent_resource_id, DCHECK(!callback.is_null()); StartUploadFile( - scoped_ptr<UploadFileInfo>(new UploadFileInfo(blocking_task_runner_, - UPLOAD_NEW_FILE, + scoped_ptr<UploadFileInfo>(new UploadFileInfo(UPLOAD_NEW_FILE, drive_file_path, local_file_path, content_type, @@ -188,8 +166,7 @@ void DriveUploader::UploadExistingFile( DCHECK(!callback.is_null()); StartUploadFile( - scoped_ptr<UploadFileInfo>(new UploadFileInfo(blocking_task_runner_, - UPLOAD_EXISTING_FILE, + scoped_ptr<UploadFileInfo>(new UploadFileInfo(UPLOAD_EXISTING_FILE, drive_file_path, local_file_path, content_type, @@ -211,31 +188,28 @@ void DriveUploader::StartUploadFile( // owned by |upload_file_info| in the reply callback. UploadFileInfo* info_ptr = upload_file_info.get(); base::PostTaskAndReplyWithResult( - blocking_task_runner_.get(), + BrowserThread::GetBlockingPool(), FROM_HERE, - base::Bind(&OpenFileStreamAndGetSizeOnBlockingPool, - info_ptr->file_stream.get(), - info_ptr->file_path), - base::Bind(&DriveUploader::OpenCompletionCallback, + base::Bind(&file_util::GetFileSize, info_ptr->file_path, + &info_ptr->content_length), + base::Bind(&DriveUploader::StartUploadFileAfterGetFileSize, weak_ptr_factory_.GetWeakPtr(), base::Passed(&upload_file_info), start_initiate_upload_callback)); } -void DriveUploader::OpenCompletionCallback( +void DriveUploader::StartUploadFileAfterGetFileSize( scoped_ptr<UploadFileInfo> upload_file_info, const StartInitiateUploadCallback& start_initiate_upload_callback, - int64 file_size) { + bool get_file_size_result) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); - if (file_size < 0) { + if (!get_file_size_result) { UploadFailed(upload_file_info.Pass(), HTTP_NOT_FOUND); return; } + DCHECK_GE(upload_file_info->content_length, 0); - upload_file_info->content_length = file_size; - - // Open succeeded, initiate the upload. start_initiate_upload_callback.Run(upload_file_info.Pass()); } @@ -298,7 +272,14 @@ void DriveUploader::OnUploadLocationReceived( upload_file_info->upload_location = upload_location; // Start the upload from the beginning of the file. - UploadNextChunk(upload_file_info.Pass()); + // PostTask is necessary because we have to finish + // InitiateUpload's callback before calling ResumeUpload, due to the + // implementation of OperationRegistry. (http://crbug.com/134814) + MessageLoop::current()->PostTask( + FROM_HERE, + base::Bind(&DriveUploader::UploadNextChunk, + weak_ptr_factory_.GetWeakPtr(), + base::Passed(&upload_file_info))); } void DriveUploader::UploadNextChunk( @@ -306,55 +287,11 @@ void DriveUploader::UploadNextChunk( DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); // Determine number of bytes to read for this upload iteration. - const int bytes_to_read = std::min(upload_file_info->SizeRemaining(), - kUploadChunkSize); - - if (bytes_to_read == 0) { - // net::FileStream doesn't allow to read 0 bytes, so directly proceed to the - // completion callback. PostTask is necessary because we have to finish - // InitiateUpload's callback before calling ResumeUpload, due to the - // implementation of OperationRegistry. (http://crbug.com/134814) - MessageLoop::current()->PostTask( - FROM_HERE, - base::Bind(&DriveUploader::ReadCompletionCallback, - weak_ptr_factory_.GetWeakPtr(), - base::Passed(&upload_file_info), 0, 0)); - return; - } - - // Passing a raw |file_stream| and |buf| to the blocking pool is safe, because - // they are owned by |upload_file_info| in the reply callback. - UploadFileInfo* info_ptr = upload_file_info.get(); - base::PostTaskAndReplyWithResult( - blocking_task_runner_.get(), - FROM_HERE, - base::Bind(&net::FileStream::ReadUntilComplete, - base::Unretained(info_ptr->file_stream.get()), - info_ptr->buf->data(), - bytes_to_read), - base::Bind(&DriveUploader::ReadCompletionCallback, - weak_ptr_factory_.GetWeakPtr(), - base::Passed(&upload_file_info), - bytes_to_read)); -} - -void DriveUploader::ReadCompletionCallback( - scoped_ptr<UploadFileInfo> upload_file_info, - int bytes_to_read, - int bytes_read) { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); - DCHECK_EQ(bytes_to_read, bytes_read); - DVLOG(1) << "ReadCompletionCallback bytes read=" << bytes_read; - - if (bytes_read < 0) { - LOG(ERROR) << "Error reading from file " - << upload_file_info->file_path.value(); - UploadFailed(upload_file_info.Pass(), GDATA_FILE_ERROR); - return; - } + const int num_upload_bytes = std::min(upload_file_info->SizeRemaining(), + kUploadChunkSize); int64 start_position = upload_file_info->next_send_position; - upload_file_info->next_send_position += bytes_read; + upload_file_info->next_send_position += num_upload_bytes; int64 end_position = upload_file_info->next_send_position; UploadFileInfo* info_ptr = upload_file_info.get(); @@ -366,7 +303,7 @@ void DriveUploader::ReadCompletionCallback( end_position, info_ptr->content_length, info_ptr->content_type, - info_ptr->buf, + info_ptr->file_path, base::Bind(&DriveUploader::OnUploadRangeResponseReceived, weak_ptr_factory_.GetWeakPtr(), base::Passed(&upload_file_info)), @@ -434,7 +371,14 @@ void DriveUploader::OnUploadRangeResponseReceived( << " for [" << upload_file_info->drive_path.value() << "]"; // Continue uploading. - UploadNextChunk(upload_file_info.Pass()); + // PostTask is necessary because we have to finish previous ResumeUpload's + // callback before calling ResumeUpload again, due to the implementation of + // OperationRegistry. (http://crbug.com/134814) + MessageLoop::current()->PostTask( + FROM_HERE, + base::Bind(&DriveUploader::UploadNextChunk, + weak_ptr_factory_.GetWeakPtr(), + base::Passed(&upload_file_info))); } void DriveUploader::OnUploadProgress(const ProgressCallback& callback, diff --git a/chrome/browser/google_apis/drive_uploader.h b/chrome/browser/google_apis/drive_uploader.h index e8d254c..a42214d 100644 --- a/chrome/browser/google_apis/drive_uploader.h +++ b/chrome/browser/google_apis/drive_uploader.h @@ -10,7 +10,6 @@ #include "base/basictypes.h" #include "base/callback_forward.h" #include "base/memory/weak_ptr.h" -#include "base/threading/sequenced_worker_pool.h" #include "chrome/browser/google_apis/drive_service_interface.h" #include "chrome/browser/google_apis/gdata_errorcode.h" #include "chrome/browser/google_apis/gdata_wapi_parser.h" @@ -122,13 +121,10 @@ class DriveUploader : public DriveUploaderInterface { void StartUploadFile( scoped_ptr<UploadFileInfo> upload_file_info, const StartInitiateUploadCallback& start_initiate_upload_callback); - - // net::FileStream::Open completion callback. The result of the file open - // operation is passed as |result|, and the size is stored in |file_size|. - void OpenCompletionCallback( + void StartUploadFileAfterGetFileSize( scoped_ptr<UploadFileInfo> upload_file_info, const StartInitiateUploadCallback& start_initiate_upload_callback, - int64 file_size); + bool get_file_size_result); // Starts to initate the new file uploading. // Upon completion, OnUploadLocationReceived should be called. @@ -152,15 +148,6 @@ class DriveUploader : public DriveUploaderInterface { // Uploads the next chunk of data from the file. void UploadNextChunk(scoped_ptr<UploadFileInfo> upload_file_info); - // net::FileStream::Read completion callback. - void ReadCompletionCallback(scoped_ptr<UploadFileInfo> upload_file_info, - int bytes_to_read, - int bytes_read); - - // Calls DriveService's ResumeUpload with the current upload info. - void ResumeUpload(scoped_ptr<UploadFileInfo> upload_file_info, - int bytes_to_send); - // DriveService callback for ResumeUpload. void OnUploadRangeResponseReceived( scoped_ptr<UploadFileInfo> upload_file_info, @@ -181,13 +168,9 @@ class DriveUploader : public DriveUploaderInterface { // DriveUploader instance. DriveServiceInterface* drive_service_; - // TaskRunner for opening, reading, and closing files. - scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; - // Note: This should remain the last member so it'll be destroyed and // invalidate its weak pointers before any other members are destroyed. base::WeakPtrFactory<DriveUploader> weak_ptr_factory_; - DISALLOW_COPY_AND_ASSIGN(DriveUploader); }; diff --git a/chrome/browser/google_apis/drive_uploader_unittest.cc b/chrome/browser/google_apis/drive_uploader_unittest.cc index 1addade..3f1a691 100644 --- a/chrome/browser/google_apis/drive_uploader_unittest.cc +++ b/chrome/browser/google_apis/drive_uploader_unittest.cc @@ -19,7 +19,6 @@ #include "chrome/browser/google_apis/dummy_drive_service.h" #include "chrome/browser/google_apis/test_util.h" #include "content/public/test/test_browser_thread.h" -#include "net/base/io_buffer.h" #include "testing/gtest/include/gtest/gtest.h" namespace google_apis { @@ -42,9 +41,11 @@ class MockDriveServiceWithUploadExpectation : public DummyDriveService { public: // Sets up an expected upload content. InitiateUpload and ResumeUpload will // verify that the specified data is correctly uploaded. - explicit MockDriveServiceWithUploadExpectation( - const std::string& expected_upload_content) - : expected_upload_content_(expected_upload_content), + MockDriveServiceWithUploadExpectation( + const base::FilePath& expected_upload_file, + int64 expected_content_length) + : expected_upload_file_(expected_upload_file), + expected_content_length_(expected_content_length), received_bytes_(0), resume_upload_call_count_(0) {} @@ -64,8 +65,7 @@ class MockDriveServiceWithUploadExpectation : public DummyDriveService { const InitiateUploadCallback& callback) OVERRIDE { EXPECT_EQ(kTestDocumentTitle, title); EXPECT_EQ(kTestMimeType, content_type); - const int64 expected_size = expected_upload_content_.size(); - EXPECT_EQ(expected_size, content_length); + EXPECT_EQ(expected_content_length_, content_length); EXPECT_EQ(kTestInitiateUploadParentResourceId, parent_resource_id); // Calls back the upload URL for subsequent ResumeUpload operations. @@ -82,8 +82,7 @@ class MockDriveServiceWithUploadExpectation : public DummyDriveService { const std::string& etag, const InitiateUploadCallback& callback) OVERRIDE { EXPECT_EQ(kTestMimeType, content_type); - const int64 expected_size = expected_upload_content_.size(); - EXPECT_EQ(expected_size, content_length); + EXPECT_EQ(expected_content_length_, content_length); EXPECT_EQ(kTestInitiateUploadResourceId, resource_id); if (!etag.empty() && etag != kTestETag) { @@ -107,31 +106,23 @@ class MockDriveServiceWithUploadExpectation : public DummyDriveService { int64 end_position, int64 content_length, const std::string& content_type, - const scoped_refptr<net::IOBuffer>& buf, + const base::FilePath& local_file_path, const UploadRangeCallback& callback, const ProgressCallback& progress_callback) OVERRIDE { - const int64 expected_size = expected_upload_content_.size(); - // The upload range should start from the current first unreceived byte. EXPECT_EQ(received_bytes_, start_position); + EXPECT_EQ(expected_upload_file_, local_file_path); // The upload data must be split into 512KB chunks. const int64 expected_chunk_end = - std::min(received_bytes_ + kUploadChunkSize, expected_size); + std::min(received_bytes_ + kUploadChunkSize, expected_content_length_); EXPECT_EQ(expected_chunk_end, end_position); - const int64 expected_chunk_size = expected_chunk_end - received_bytes_; - const std::string expected_chunk_data( - expected_upload_content_.substr(received_bytes_, - expected_chunk_size)); - std::string uploading_data(buf->data(), buf->data() + expected_chunk_size); - EXPECT_EQ(expected_chunk_data, uploading_data); - // The upload URL returned by InitiateUpload() must be used. EXPECT_EQ(GURL(kTestUploadURL), upload_url); // Other parameters should be the exact values passed to DriveUploader. - EXPECT_EQ(expected_size, content_length); + EXPECT_EQ(expected_content_length_, content_length); EXPECT_EQ(kTestMimeType, content_type); // Update the internal status of the current upload session. @@ -142,9 +133,9 @@ class MockDriveServiceWithUploadExpectation : public DummyDriveService { if (!progress_callback.is_null()) { // For the testing purpose, it always notifies the progress at the end of // each chunk uploading. + int64 chunk_size = end_position - start_position; MessageLoop::current()->PostTask(FROM_HERE, - base::Bind(progress_callback, expected_chunk_size, - expected_chunk_size)); + base::Bind(progress_callback, chunk_size, chunk_size)); } // Callback with response. @@ -166,7 +157,8 @@ class MockDriveServiceWithUploadExpectation : public DummyDriveService { base::Bind(callback, response, base::Passed(&entry))); } - std::string expected_upload_content_; + const base::FilePath expected_upload_file_; + const int64 expected_content_length_; int64 received_bytes_; int64 resume_upload_call_count_; }; @@ -205,7 +197,7 @@ class MockDriveServiceNoConnectionAtInitiate : public DummyDriveService { int64 end_position, int64 content_length, const std::string& content_type, - const scoped_refptr<net::IOBuffer>& buf, + const base::FilePath& local_file_path, const UploadRangeCallback& callback, const ProgressCallback& progress_callback) OVERRIDE { NOTREACHED(); @@ -246,7 +238,7 @@ class MockDriveServiceNoConnectionAtResume : public DummyDriveService { int64 end_position, int64 content_length, const std::string& content_type, - const scoped_refptr<net::IOBuffer>& buf, + const base::FilePath& local_file_path, const UploadRangeCallback& callback, const ProgressCallback& progress_callback) OVERRIDE { MessageLoop::current()->PostTask(FROM_HERE, @@ -289,7 +281,7 @@ TEST_F(DriveUploaderTest, UploadExisting0KB) { base::FilePath file_path; scoped_ptr<ResourceEntry> resource_entry; - MockDriveServiceWithUploadExpectation mock_service(data); + MockDriveServiceWithUploadExpectation mock_service(local_path, data.size()); DriveUploader uploader(&mock_service); std::vector<test_util::ProgressInfo> upload_progress_values; uploader.UploadExistingFile( @@ -326,7 +318,7 @@ TEST_F(DriveUploaderTest, UploadExisting512KB) { base::FilePath file_path; scoped_ptr<ResourceEntry> resource_entry; - MockDriveServiceWithUploadExpectation mock_service(data); + MockDriveServiceWithUploadExpectation mock_service(local_path, data.size()); DriveUploader uploader(&mock_service); std::vector<test_util::ProgressInfo> upload_progress_values; uploader.UploadExistingFile( @@ -365,7 +357,7 @@ TEST_F(DriveUploaderTest, UploadExisting1234KB) { base::FilePath file_path; scoped_ptr<ResourceEntry> resource_entry; - MockDriveServiceWithUploadExpectation mock_service(data); + MockDriveServiceWithUploadExpectation mock_service(local_path, data.size()); DriveUploader uploader(&mock_service); std::vector<test_util::ProgressInfo> upload_progress_values; uploader.UploadExistingFile( @@ -409,7 +401,7 @@ TEST_F(DriveUploaderTest, UploadNew1234KB) { base::FilePath file_path; scoped_ptr<ResourceEntry> resource_entry; - MockDriveServiceWithUploadExpectation mock_service(data); + MockDriveServiceWithUploadExpectation mock_service(local_path, data.size()); DriveUploader uploader(&mock_service); uploader.UploadNewFile( kTestInitiateUploadParentResourceId, @@ -470,7 +462,7 @@ TEST_F(DriveUploaderTest, InitiateUploadNoConflict) { base::FilePath file_path; scoped_ptr<ResourceEntry> resource_entry; - MockDriveServiceWithUploadExpectation mock_service(data); + MockDriveServiceWithUploadExpectation mock_service(local_path, data.size()); DriveUploader uploader(&mock_service); uploader.UploadExistingFile( kTestInitiateUploadResourceId, @@ -498,7 +490,7 @@ TEST_F(DriveUploaderTest, InitiateUploadConflict) { base::FilePath file_path; scoped_ptr<ResourceEntry> resource_entry; - MockDriveServiceWithUploadExpectation mock_service(data); + MockDriveServiceWithUploadExpectation mock_service(local_path, data.size()); DriveUploader uploader(&mock_service); uploader.UploadExistingFile( kTestInitiateUploadResourceId, diff --git a/chrome/browser/google_apis/dummy_drive_service.cc b/chrome/browser/google_apis/dummy_drive_service.cc index 4024d74..750f5a6 100644 --- a/chrome/browser/google_apis/dummy_drive_service.cc +++ b/chrome/browser/google_apis/dummy_drive_service.cc @@ -129,7 +129,7 @@ void DummyDriveService::ResumeUpload( int64 end_position, int64 content_length, const std::string& content_type, - const scoped_refptr<net::IOBuffer>& buf, + const base::FilePath& local_file_path, const UploadRangeCallback& callback, const ProgressCallback& progress_callback) {} diff --git a/chrome/browser/google_apis/dummy_drive_service.h b/chrome/browser/google_apis/dummy_drive_service.h index e560cf1..e29e7db 100644 --- a/chrome/browser/google_apis/dummy_drive_service.h +++ b/chrome/browser/google_apis/dummy_drive_service.h @@ -103,7 +103,7 @@ class DummyDriveService : public DriveServiceInterface { int64 end_position, int64 content_length, const std::string& content_type, - const scoped_refptr<net::IOBuffer>& buf, + const base::FilePath& local_file_path, const UploadRangeCallback& callback, const ProgressCallback& progress_callback) OVERRIDE; virtual void GetUploadStatus( diff --git a/chrome/browser/google_apis/fake_drive_service.cc b/chrome/browser/google_apis/fake_drive_service.cc index 58f9941..123c1c1 100644 --- a/chrome/browser/google_apis/fake_drive_service.cc +++ b/chrome/browser/google_apis/fake_drive_service.cc @@ -853,7 +853,7 @@ void FakeDriveService::ResumeUpload( int64 end_position, int64 content_length, const std::string& content_type, - const scoped_refptr<net::IOBuffer>& buf, + const base::FilePath& local_file_path, const UploadRangeCallback& callback, const ProgressCallback& progress_callback) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); diff --git a/chrome/browser/google_apis/fake_drive_service.h b/chrome/browser/google_apis/fake_drive_service.h index 0da5aaa..57f9116 100644 --- a/chrome/browser/google_apis/fake_drive_service.h +++ b/chrome/browser/google_apis/fake_drive_service.h @@ -167,7 +167,7 @@ class FakeDriveService : public DriveServiceInterface { int64 end_position, int64 content_length, const std::string& content_type, - const scoped_refptr<net::IOBuffer>& buf, + const base::FilePath& local_file_path, const UploadRangeCallback& callback, const ProgressCallback& progress_callback) OVERRIDE; virtual void GetUploadStatus( diff --git a/chrome/browser/google_apis/fake_drive_service_unittest.cc b/chrome/browser/google_apis/fake_drive_service_unittest.cc index 98dcbb0..bc6dacc 100644 --- a/chrome/browser/google_apis/fake_drive_service_unittest.cc +++ b/chrome/browser/google_apis/fake_drive_service_unittest.cc @@ -1502,7 +1502,7 @@ TEST_F(FakeDriveServiceTest, ResumeUpload_Offline) { base::FilePath(FILE_PATH_LITERAL("drive/Directory 1/new file.foo")), upload_location, 0, 13, 15, "test/foo", - scoped_refptr<net::IOBuffer>(), + base::FilePath(), test_util::CreateCopyResultCallback(&response, &entry), ProgressCallback()); message_loop_.RunUntilIdle(); @@ -1535,7 +1535,7 @@ TEST_F(FakeDriveServiceTest, ResumeUpload_NotFound) { base::FilePath(FILE_PATH_LITERAL("drive/Directory 1/new file.foo")), GURL("https://foo.com/"), 0, 13, 15, "test/foo", - scoped_refptr<net::IOBuffer>(), + base::FilePath(), test_util::CreateCopyResultCallback(&response, &entry), ProgressCallback()); message_loop_.RunUntilIdle(); @@ -1569,7 +1569,7 @@ TEST_F(FakeDriveServiceTest, ResumeUpload_ExistingFile) { base::FilePath(FILE_PATH_LITERAL("drive/File 1.txt")), upload_location, 0, 13, 15, "text/plain", - scoped_refptr<net::IOBuffer>(), + base::FilePath(), test_util::CreateCopyResultCallback(&response, &entry), base::Bind(&test_util::AppendProgressCallbackResult, &upload_progress_values)); @@ -1588,7 +1588,7 @@ TEST_F(FakeDriveServiceTest, ResumeUpload_ExistingFile) { base::FilePath(FILE_PATH_LITERAL("drive/File 1.txt")), upload_location, 13, 15, 15, "text/plain", - scoped_refptr<net::IOBuffer>(), + base::FilePath(), test_util::CreateCopyResultCallback(&response, &entry), base::Bind(&test_util::AppendProgressCallbackResult, &upload_progress_values)); @@ -1632,7 +1632,7 @@ TEST_F(FakeDriveServiceTest, ResumeUpload_NewFile) { base::FilePath(FILE_PATH_LITERAL("drive/Directory 1/new file.foo")), upload_location, 0, 13, 15, "test/foo", - scoped_refptr<net::IOBuffer>(), + base::FilePath(), test_util::CreateCopyResultCallback(&response, &entry), base::Bind(&test_util::AppendProgressCallbackResult, &upload_progress_values)); @@ -1651,7 +1651,7 @@ TEST_F(FakeDriveServiceTest, ResumeUpload_NewFile) { base::FilePath(FILE_PATH_LITERAL("drive/Directory 1/new file.foo")), upload_location, 13, 15, 15, "test/foo", - scoped_refptr<net::IOBuffer>(), + base::FilePath(), test_util::CreateCopyResultCallback(&response, &entry), base::Bind(&test_util::AppendProgressCallbackResult, &upload_progress_values)); diff --git a/chrome/browser/google_apis/gdata_wapi_operations.cc b/chrome/browser/google_apis/gdata_wapi_operations.cc index 35af2f2..1d2d965 100644 --- a/chrome/browser/google_apis/gdata_wapi_operations.cc +++ b/chrome/browser/google_apis/gdata_wapi_operations.cc @@ -688,7 +688,7 @@ ResumeUploadOperation::ResumeUploadOperation( int64 end_position, int64 content_length, const std::string& content_type, - const scoped_refptr<net::IOBuffer>& buf) + const base::FilePath& local_file_path) : ResumeUploadOperationBase(registry, url_request_context_getter, upload_mode, @@ -698,7 +698,7 @@ ResumeUploadOperation::ResumeUploadOperation( end_position, content_length, content_type, - buf), + local_file_path), callback_(callback), progress_callback_(progress_callback) { DCHECK(!callback_.is_null()); diff --git a/chrome/browser/google_apis/gdata_wapi_operations.h b/chrome/browser/google_apis/gdata_wapi_operations.h index 6ae2dde..9057b76 100644 --- a/chrome/browser/google_apis/gdata_wapi_operations.h +++ b/chrome/browser/google_apis/gdata_wapi_operations.h @@ -12,7 +12,6 @@ #include "chrome/browser/google_apis/drive_service_interface.h" #include "chrome/browser/google_apis/drive_upload_mode.h" #include "chrome/browser/google_apis/gdata_wapi_url_generator.h" -#include "net/base/io_buffer.h" namespace net { class URLRequestContextGetter; @@ -483,7 +482,7 @@ class ResumeUploadOperation : public ResumeUploadOperationBase { int64 end_position, int64 content_length, const std::string& content_type, - const scoped_refptr<net::IOBuffer>& buf); + const base::FilePath& local_file_path); virtual ~ResumeUploadOperation(); protected: diff --git a/chrome/browser/google_apis/gdata_wapi_operations_unittest.cc b/chrome/browser/google_apis/gdata_wapi_operations_unittest.cc index 5668e8f5..c3c5464 100644 --- a/chrome/browser/google_apis/gdata_wapi_operations_unittest.cc +++ b/chrome/browser/google_apis/gdata_wapi_operations_unittest.cc @@ -8,6 +8,7 @@ #include "base/bind.h" #include "base/file_util.h" #include "base/files/file_path.h" +#include "base/files/scoped_temp_dir.h" #include "base/json/json_reader.h" #include "base/json/json_writer.h" #include "base/message_loop/message_loop_proxy.h" @@ -57,6 +58,8 @@ class GDataWapiOperationsTest : public testing::Test { content::BrowserThread::GetMessageLoopProxyForThread( content::BrowserThread::IO)); + ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); + ASSERT_TRUE(test_server_.InitializeAndWaitUntilReady()); test_server_.RegisterRequestHandler( base::Bind(&test_util::HandleDownloadRequest, @@ -302,6 +305,7 @@ class GDataWapiOperationsTest : public testing::Test { OperationRegistry operation_registry_; scoped_ptr<GDataWapiUrlGenerator> url_generator_; scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; + base::ScopedTempDir temp_dir_; // These fields are used to keep the current upload state during a // test case. These values are updated by the request from @@ -862,6 +866,10 @@ TEST_F(GDataWapiOperationsTest, RemoveResourceFromDirectoryOperation) { // ResumeUploadOperation for a scenario of uploading a new file. TEST_F(GDataWapiOperationsTest, UploadNewFile) { const std::string kUploadContent = "hello"; + const base::FilePath kTestFilePath = + temp_dir_.path().AppendASCII("upload_file.txt"); + ASSERT_TRUE(test_util::WriteStringToFile(kTestFilePath, kUploadContent)); + GDataErrorCode result_code = GDATA_OTHER_ERROR; GURL upload_url; @@ -907,8 +915,6 @@ TEST_F(GDataWapiOperationsTest, UploadNewFile) { http_request_.content); // 2) Upload the content to the upload URL. - scoped_refptr<net::IOBuffer> buffer = new net::StringIOBuffer(kUploadContent); - UploadRangeResponse response; scoped_ptr<ResourceEntry> new_entry; @@ -926,7 +932,7 @@ TEST_F(GDataWapiOperationsTest, UploadNewFile) { kUploadContent.size(), // end_position (exclusive) kUploadContent.size(), // content_length, "text/plain", // content_type - buffer); + kTestFilePath); resume_operation->Start( kTestGDataAuthToken, kTestUserAgent, @@ -964,6 +970,10 @@ TEST_F(GDataWapiOperationsTest, UploadNewLargeFile) { // So, sending "kMaxNumBytes * 2 + 1" bytes ensures three // ResumeUploadOperations, which are start, middle and last operations. const std::string kUploadContent(kMaxNumBytes * 2 + 1, 'a'); + const base::FilePath kTestFilePath = + temp_dir_.path().AppendASCII("upload_file.txt"); + ASSERT_TRUE(test_util::WriteStringToFile(kTestFilePath, kUploadContent)); + GDataErrorCode result_code = GDATA_OTHER_ERROR; GURL upload_url; @@ -1062,8 +1072,6 @@ TEST_F(GDataWapiOperationsTest, UploadNewLargeFile) { // The end position is exclusive. const size_t end_position = start_position + payload.size(); - scoped_refptr<net::IOBuffer> buffer = new net::StringIOBuffer(payload); - UploadRangeResponse response; scoped_ptr<ResourceEntry> new_entry; @@ -1081,7 +1089,7 @@ TEST_F(GDataWapiOperationsTest, UploadNewLargeFile) { end_position, kUploadContent.size(), // content_length, "text/plain", // content_type - buffer); + kTestFilePath); resume_operation->Start( kTestGDataAuthToken, kTestUserAgent, @@ -1162,6 +1170,10 @@ TEST_F(GDataWapiOperationsTest, UploadNewLargeFile) { // expectation for the Content-Range header. TEST_F(GDataWapiOperationsTest, UploadNewEmptyFile) { const std::string kUploadContent; + const base::FilePath kTestFilePath = + temp_dir_.path().AppendASCII("empty_file.txt"); + ASSERT_TRUE(test_util::WriteStringToFile(kTestFilePath, kUploadContent)); + GDataErrorCode result_code = GDATA_OTHER_ERROR; GURL upload_url; @@ -1207,8 +1219,6 @@ TEST_F(GDataWapiOperationsTest, UploadNewEmptyFile) { http_request_.content); // 2) Upload the content to the upload URL. - scoped_refptr<net::IOBuffer> buffer = new net::StringIOBuffer(kUploadContent); - UploadRangeResponse response; scoped_ptr<ResourceEntry> new_entry; @@ -1226,7 +1236,7 @@ TEST_F(GDataWapiOperationsTest, UploadNewEmptyFile) { kUploadContent.size(), // end_position (exclusive) kUploadContent.size(), // content_length, "text/plain", // content_type - buffer); + kTestFilePath); resume_operation->Start( kTestGDataAuthToken, kTestUserAgent, @@ -1255,6 +1265,10 @@ TEST_F(GDataWapiOperationsTest, UploadNewEmptyFile) { // ResumeUploadOperation for a scenario of updating an existing file. TEST_F(GDataWapiOperationsTest, UploadExistingFile) { const std::string kUploadContent = "hello"; + const base::FilePath kTestFilePath = + temp_dir_.path().AppendASCII("upload_file.txt"); + ASSERT_TRUE(test_util::WriteStringToFile(kTestFilePath, kUploadContent)); + GDataErrorCode result_code = GDATA_OTHER_ERROR; GURL upload_url; @@ -1299,8 +1313,6 @@ TEST_F(GDataWapiOperationsTest, UploadExistingFile) { EXPECT_EQ("*", http_request_.headers["If-Match"]); // 2) Upload the content to the upload URL. - scoped_refptr<net::IOBuffer> buffer = new net::StringIOBuffer(kUploadContent); - UploadRangeResponse response; scoped_ptr<ResourceEntry> new_entry; @@ -1318,7 +1330,7 @@ TEST_F(GDataWapiOperationsTest, UploadExistingFile) { kUploadContent.size(), // end_position (exclusive) kUploadContent.size(), // content_length, "text/plain", // content_type - buffer); + kTestFilePath); resume_operation->Start( kTestGDataAuthToken, kTestUserAgent, @@ -1349,6 +1361,10 @@ TEST_F(GDataWapiOperationsTest, UploadExistingFile) { // ResumeUploadOperation for a scenario of updating an existing file. TEST_F(GDataWapiOperationsTest, UploadExistingFileWithETag) { const std::string kUploadContent = "hello"; + const base::FilePath kTestFilePath = + temp_dir_.path().AppendASCII("upload_file.txt"); + ASSERT_TRUE(test_util::WriteStringToFile(kTestFilePath, kUploadContent)); + GDataErrorCode result_code = GDATA_OTHER_ERROR; GURL upload_url; @@ -1393,8 +1409,6 @@ TEST_F(GDataWapiOperationsTest, UploadExistingFileWithETag) { EXPECT_EQ(kTestETag, http_request_.headers["If-Match"]); // 2) Upload the content to the upload URL. - scoped_refptr<net::IOBuffer> buffer = new net::StringIOBuffer(kUploadContent); - UploadRangeResponse response; scoped_ptr<ResourceEntry> new_entry; @@ -1412,7 +1426,7 @@ TEST_F(GDataWapiOperationsTest, UploadExistingFileWithETag) { kUploadContent.size(), // end_position (exclusive) kUploadContent.size(), // content_length, "text/plain", // content_type - buffer); + kTestFilePath); resume_operation->Start( kTestGDataAuthToken, kTestUserAgent, diff --git a/chrome/browser/google_apis/gdata_wapi_service.cc b/chrome/browser/google_apis/gdata_wapi_service.cc index 041911d..3c0b574 100644 --- a/chrome/browser/google_apis/gdata_wapi_service.cc +++ b/chrome/browser/google_apis/gdata_wapi_service.cc @@ -483,7 +483,7 @@ void GDataWapiService::ResumeUpload( int64 end_position, int64 content_length, const std::string& content_type, - const scoped_refptr<net::IOBuffer>& buf, + const base::FilePath& local_file_path, const UploadRangeCallback& callback, const ProgressCallback& progress_callback) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); @@ -501,7 +501,7 @@ void GDataWapiService::ResumeUpload( end_position, content_length, content_type, - buf)); + local_file_path)); } void GDataWapiService::GetUploadStatus( diff --git a/chrome/browser/google_apis/gdata_wapi_service.h b/chrome/browser/google_apis/gdata_wapi_service.h index a67c9dd..8e335f4 100644 --- a/chrome/browser/google_apis/gdata_wapi_service.h +++ b/chrome/browser/google_apis/gdata_wapi_service.h @@ -136,7 +136,7 @@ class GDataWapiService : public DriveServiceInterface, int64 end_position, int64 content_length, const std::string& content_type, - const scoped_refptr<net::IOBuffer>& buf, + const base::FilePath& local_file_path, const UploadRangeCallback& callback, const ProgressCallback& progress_callback) OVERRIDE; virtual void GetUploadStatus( diff --git a/chrome/browser/google_apis/mock_drive_service.h b/chrome/browser/google_apis/mock_drive_service.h index 0950453..8b7b549 100644 --- a/chrome/browser/google_apis/mock_drive_service.h +++ b/chrome/browser/google_apis/mock_drive_service.h @@ -11,7 +11,6 @@ #include "base/memory/scoped_ptr.h" #include "chrome/browser/google_apis/drive_service_interface.h" -#include "net/base/io_buffer.h" #include "testing/gmock/include/gmock/gmock.h" namespace base { @@ -112,7 +111,7 @@ class MockDriveService : public DriveServiceInterface { int64 end_position, int64 content_length, const std::string& content_type, - const scoped_refptr<net::IOBuffer>& buf, + const base::FilePath& local_file_path, const UploadRangeCallback& callback, const ProgressCallback& progress_callback)); MOCK_METHOD5(GetUploadStatus, |