Commit cef6df5f authored by hidehiko@chromium.org's avatar hidehiko@chromium.org

Split InitiateUpload method into two.

This is the preparation to replace upload urls to resource ids.
After this CL, InitiateUploadXXX methods can be supported on Drive API v2.

BUG=148632
TEST=Ran unit_tests


Review URL: https://chromiumcodereview.appspot.com/12207075

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@182356 0039d316-1c4b-4281-b951-d872f2087c98
parent ebc7ea23
...@@ -374,26 +374,6 @@ void GetDataOperation::RunCallbackOnSuccess(GDataErrorCode fetch_error_code, ...@@ -374,26 +374,6 @@ void GetDataOperation::RunCallbackOnSuccess(GDataErrorCode fetch_error_code,
//========================= InitiateUploadOperationBase ======================== //========================= InitiateUploadOperationBase ========================
InitiateUploadParams::InitiateUploadParams(
UploadMode upload_mode,
const std::string& title,
const std::string& content_type,
int64 content_length,
const GURL& upload_location,
const base::FilePath& drive_file_path,
const std::string& etag)
: upload_mode(upload_mode),
title(title),
content_type(content_type),
content_length(content_length),
upload_location(upload_location),
drive_file_path(drive_file_path),
etag(etag) {
}
InitiateUploadParams::~InitiateUploadParams() {
}
InitiateUploadOperationBase::InitiateUploadOperationBase( InitiateUploadOperationBase::InitiateUploadOperationBase(
OperationRegistry* registry, OperationRegistry* registry,
net::URLRequestContextGetter* url_request_context_getter, net::URLRequestContextGetter* url_request_context_getter,
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
#include "base/callback.h" #include "base/callback.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "chrome/browser/google_apis/drive_upload_mode.h"
#include "chrome/browser/google_apis/gdata_errorcode.h" #include "chrome/browser/google_apis/gdata_errorcode.h"
#include "chrome/browser/google_apis/operation_registry.h" #include "chrome/browser/google_apis/operation_registry.h"
#include "googleurl/src/gurl.h" #include "googleurl/src/gurl.h"
...@@ -259,43 +258,6 @@ class GetDataOperation : public UrlFetchOperationBase { ...@@ -259,43 +258,6 @@ class GetDataOperation : public UrlFetchOperationBase {
//=========================== InitiateUploadOperation ========================== //=========================== InitiateUploadOperation ==========================
// Struct for passing params needed for DriveServiceInterface::InitiateUpload()
// calls.
//
// When uploading a new file (UPLOAD_NEW_FILE):
// - |title| should be set.
// - |upload_location| should be the upload_url() of the parent directory.
// (resumable-create-media URL)
// - |etag| is ignored.
//
// When updating an existing file (UPLOAD_EXISTING_FILE):
// - |title| should be empty
// - |upload_location| should be the upload_url() of the existing file.
// (resumable-edit-media URL)
// - If |etag| should be empty or should match the etag() of the destination
// file.
// TODO(hidehiko): Get rid of this struct by splitting the method
// InitiateUpload into two methods, InitiateUploadNewFile and
// InitiateUploadExistingFile.
struct InitiateUploadParams {
InitiateUploadParams(UploadMode upload_mode,
const std::string& title,
const std::string& content_type,
int64 content_length,
const GURL& upload_location,
const base::FilePath& drive_file_path,
const std::string& etag);
~InitiateUploadParams();
const UploadMode upload_mode;
const std::string title;
const std::string content_type;
const int64 content_length;
const GURL upload_location;
const base::FilePath drive_file_path;
const std::string etag;
};
// Callback type for DocumentServiceInterface::InitiateUpload. // Callback type for DocumentServiceInterface::InitiateUpload.
typedef base::Callback<void(GDataErrorCode error, typedef base::Callback<void(GDataErrorCode error,
const GURL& upload_url)> InitiateUploadCallback; const GURL& upload_url)> InitiateUploadCallback;
......
...@@ -447,13 +447,31 @@ void DriveAPIService::RemoveResourceFromDirectory( ...@@ -447,13 +447,31 @@ void DriveAPIService::RemoveResourceFromDirectory(
callback)); callback));
} }
void DriveAPIService::InitiateUpload( void DriveAPIService::InitiateUploadNewFile(
const InitiateUploadParams& params, const FilePath& drive_file_path,
const std::string& content_type,
int64 content_length,
const GURL& parent_upload_url,
const std::string& title,
const InitiateUploadCallback& callback) { const InitiateUploadCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!callback.is_null()); DCHECK(!callback.is_null());
// TODO(kochi): Implement this. // TODO(hidehiko): Implement this.
NOTREACHED();
}
void DriveAPIService::InitiateUploadExistingFile(
const FilePath& drive_file_path,
const std::string& content_type,
int64 content_length,
const GURL& upload_url,
const std::string& etag,
const InitiateUploadCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!callback.is_null());
// TODO(hidehiko): Implement this.
NOTREACHED(); NOTREACHED();
} }
......
...@@ -104,8 +104,19 @@ class DriveAPIService : public DriveServiceInterface, ...@@ -104,8 +104,19 @@ class DriveAPIService : public DriveServiceInterface,
const std::string& parent_resource_id, const std::string& parent_resource_id,
const std::string& directory_name, const std::string& directory_name,
const GetResourceEntryCallback& callback) OVERRIDE; const GetResourceEntryCallback& callback) OVERRIDE;
virtual void InitiateUpload( virtual void InitiateUploadNewFile(
const InitiateUploadParams& params, const FilePath& drive_file_path,
const std::string& content_type,
int64 content_length,
const GURL& parent_upload_url,
const std::string& title,
const InitiateUploadCallback& callback) OVERRIDE;
virtual void InitiateUploadExistingFile(
const FilePath& drive_file_path,
const std::string& content_type,
int64 content_length,
const GURL& upload_url,
const std::string& etag,
const InitiateUploadCallback& callback) OVERRIDE; const InitiateUploadCallback& callback) OVERRIDE;
virtual void ResumeUpload( virtual void ResumeUpload(
const ResumeUploadParams& params, const ResumeUploadParams& params,
......
...@@ -232,10 +232,31 @@ class DriveServiceInterface { ...@@ -232,10 +232,31 @@ class DriveServiceInterface {
const DownloadActionCallback& download_action_callback, const DownloadActionCallback& download_action_callback,
const GetContentCallback& get_content_callback) = 0; const GetContentCallback& get_content_callback) = 0;
// Initiates uploading of a document/file. // Initiates uploading of a new document/file.
// |content_type| and |content_length| should be the ones of the file to be
// uploaded.
// |callback| must not be null. // |callback| must not be null.
virtual void InitiateUpload(const InitiateUploadParams& params, // TODO(hidehiko): Replace |parent_upload_url| by resource id.
const InitiateUploadCallback& callback) = 0; virtual void InitiateUploadNewFile(
const FilePath& drive_file_path,
const std::string& content_type,
int64 content_length,
const GURL& parent_upload_url,
const std::string& title,
const InitiateUploadCallback& callback) = 0;
// Initiates uploading of an existing document/file.
// |content_type| and |content_length| should be the ones of the file to be
// uploaded.
// |callback| must not be null.
// TODO(hidehiko): Replace |upload_url| by resource id.
virtual void InitiateUploadExistingFile(
const FilePath& drive_file_path,
const std::string& content_type,
int64 content_length,
const GURL& upload_url,
const std::string& etag,
const InitiateUploadCallback& callback) = 0;
// Resumes uploading of a document/file on the calling thread. // Resumes uploading of a document/file on the calling thread.
// |callback| must not be null. // |callback| must not be null.
......
...@@ -45,20 +45,14 @@ namespace google_apis { ...@@ -45,20 +45,14 @@ namespace google_apis {
struct DriveUploader::UploadFileInfo { struct DriveUploader::UploadFileInfo {
UploadFileInfo(scoped_refptr<base::SequencedTaskRunner> task_runner, UploadFileInfo(scoped_refptr<base::SequencedTaskRunner> task_runner,
UploadMode upload_mode, UploadMode upload_mode,
const GURL& initial_upload_location, const FilePath& drive_path,
const base::FilePath& drive_path, const FilePath& local_path,
const base::FilePath& local_path,
const std::string& title,
const std::string& content_type, const std::string& content_type,
const std::string& etag,
const UploadCompletionCallback& callback) const UploadCompletionCallback& callback)
: upload_mode(upload_mode), : upload_mode(upload_mode),
initial_upload_location(initial_upload_location),
drive_path(drive_path), drive_path(drive_path),
file_path(local_path), file_path(local_path),
title(title),
content_type(content_type), content_type(content_type),
etag(etag),
completion_callback(callback), completion_callback(callback),
content_length(0), content_length(0),
next_send_position(0), next_send_position(0),
...@@ -82,8 +76,7 @@ struct DriveUploader::UploadFileInfo { ...@@ -82,8 +76,7 @@ struct DriveUploader::UploadFileInfo {
// Useful for printf debugging. // Useful for printf debugging.
std::string DebugString() const { std::string DebugString() const {
return "title=[" + title + return "file_path=[" + file_path.AsUTF8Unsafe() +
"], file_path=[" + file_path.AsUTF8Unsafe() +
"], content_type=[" + content_type + "], content_type=[" + content_type +
"], content_length=[" + base::UintToString(content_length) + "], content_length=[" + base::UintToString(content_length) +
"], drive_path=[" + drive_path.AsUTF8Unsafe() + "], drive_path=[" + drive_path.AsUTF8Unsafe() +
...@@ -93,23 +86,15 @@ struct DriveUploader::UploadFileInfo { ...@@ -93,23 +86,15 @@ struct DriveUploader::UploadFileInfo {
// Whether this is uploading a new file or updating an existing file. // Whether this is uploading a new file or updating an existing file.
const UploadMode upload_mode; const UploadMode upload_mode;
// Location URL used to get |upload_location| with InitiateUpload.
const GURL initial_upload_location;
// Final path in gdata. Looks like /special/drive/MyFolder/MyFile. // Final path in gdata. Looks like /special/drive/MyFolder/MyFile.
const base::FilePath drive_path; const base::FilePath drive_path;
// The local file path of the file to be uploaded. // The local file path of the file to be uploaded.
const base::FilePath file_path; const base::FilePath file_path;
// Title to be used for file to be uploaded.
const std::string title;
// Content-Type of file. // Content-Type of file.
const std::string content_type; const std::string content_type;
const std::string etag;
// Callback to be invoked once the upload has finished. // Callback to be invoked once the upload has finished.
const UploadCompletionCallback completion_callback; const UploadCompletionCallback completion_callback;
...@@ -152,62 +137,63 @@ DriveUploader::DriveUploader(DriveServiceInterface* drive_service) ...@@ -152,62 +137,63 @@ DriveUploader::DriveUploader(DriveServiceInterface* drive_service)
DriveUploader::~DriveUploader() {} DriveUploader::~DriveUploader() {}
void DriveUploader::UploadNewFile(const GURL& upload_location, void DriveUploader::UploadNewFile(const GURL& parent_upload_url,
const base::FilePath& drive_file_path, const base::FilePath& drive_file_path,
const base::FilePath& local_file_path, const base::FilePath& local_file_path,
const std::string& title, const std::string& title,
const std::string& content_type, const std::string& content_type,
const UploadCompletionCallback& callback) { const UploadCompletionCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!upload_location.is_empty()); DCHECK(!parent_upload_url.is_empty());
DCHECK(!drive_file_path.empty()); DCHECK(!drive_file_path.empty());
DCHECK(!local_file_path.empty()); DCHECK(!local_file_path.empty());
DCHECK(!title.empty()); DCHECK(!title.empty());
DCHECK(!content_type.empty()); DCHECK(!content_type.empty());
DCHECK(!callback.is_null()); DCHECK(!callback.is_null());
StartUploadFile(scoped_ptr<UploadFileInfo>(new UploadFileInfo( StartUploadFile(
blocking_task_runner_, scoped_ptr<UploadFileInfo>(new UploadFileInfo(blocking_task_runner_,
UPLOAD_NEW_FILE, UPLOAD_NEW_FILE,
upload_location, drive_file_path,
drive_file_path, local_file_path,
local_file_path, content_type,
title, callback)),
content_type, base::Bind(&DriveUploader::StartInitiateUploadNewFile,
"", // etag weak_ptr_factory_.GetWeakPtr(),
callback parent_upload_url,
))); title));
} }
void DriveUploader::UploadExistingFile( void DriveUploader::UploadExistingFile(
const GURL& upload_location, const GURL& upload_url,
const base::FilePath& drive_file_path, const base::FilePath& drive_file_path,
const base::FilePath& local_file_path, const base::FilePath& local_file_path,
const std::string& content_type, const std::string& content_type,
const std::string& etag, const std::string& etag,
const UploadCompletionCallback& callback) { const UploadCompletionCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!upload_location.is_empty()); DCHECK(!upload_url.is_empty());
DCHECK(!drive_file_path.empty()); DCHECK(!drive_file_path.empty());
DCHECK(!local_file_path.empty()); DCHECK(!local_file_path.empty());
DCHECK(!content_type.empty()); DCHECK(!content_type.empty());
DCHECK(!callback.is_null()); DCHECK(!callback.is_null());
StartUploadFile(scoped_ptr<UploadFileInfo>(new UploadFileInfo( StartUploadFile(
blocking_task_runner_, scoped_ptr<UploadFileInfo>(new UploadFileInfo(blocking_task_runner_,
UPLOAD_EXISTING_FILE, UPLOAD_EXISTING_FILE,
upload_location, drive_file_path,
drive_file_path, local_file_path,
local_file_path, content_type,
"", // title : not necessary for update of an existing file. callback)),
content_type, base::Bind(&DriveUploader::StartInitiateUploadExistingFile,
etag, weak_ptr_factory_.GetWeakPtr(),
callback upload_url,
))); etag));
} }
void DriveUploader::StartUploadFile( void DriveUploader::StartUploadFile(
scoped_ptr<UploadFileInfo> upload_file_info) { scoped_ptr<UploadFileInfo> upload_file_info,
const StartInitiateUploadCallback& start_initiate_upload_callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DVLOG(1) << "Uploading file: " << upload_file_info->DebugString(); DVLOG(1) << "Uploading file: " << upload_file_info->DebugString();
...@@ -222,11 +208,14 @@ void DriveUploader::StartUploadFile( ...@@ -222,11 +208,14 @@ void DriveUploader::StartUploadFile(
info_ptr->file_path), info_ptr->file_path),
base::Bind(&DriveUploader::OpenCompletionCallback, base::Bind(&DriveUploader::OpenCompletionCallback,
weak_ptr_factory_.GetWeakPtr(), weak_ptr_factory_.GetWeakPtr(),
base::Passed(&upload_file_info))); base::Passed(&upload_file_info),
start_initiate_upload_callback));
} }
void DriveUploader::OpenCompletionCallback( void DriveUploader::OpenCompletionCallback(
scoped_ptr<UploadFileInfo> upload_file_info, int64 file_size) { scoped_ptr<UploadFileInfo> upload_file_info,
const StartInitiateUploadCallback& start_initiate_upload_callback,
int64 file_size) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (file_size < 0) { if (file_size < 0) {
...@@ -237,15 +226,40 @@ void DriveUploader::OpenCompletionCallback( ...@@ -237,15 +226,40 @@ void DriveUploader::OpenCompletionCallback(
upload_file_info->content_length = file_size; upload_file_info->content_length = file_size;
// Open succeeded, initiate the upload. // Open succeeded, initiate the upload.
start_initiate_upload_callback.Run(upload_file_info.Pass());
}
void DriveUploader::StartInitiateUploadNewFile(
const GURL& parent_upload_url,
const std::string& title,
scoped_ptr<UploadFileInfo> upload_file_info) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
UploadFileInfo* info_ptr = upload_file_info.get();
drive_service_->InitiateUploadNewFile(
info_ptr->drive_path,
info_ptr->content_type,
info_ptr->content_length,
parent_upload_url,
title,
base::Bind(&DriveUploader::OnUploadLocationReceived,
weak_ptr_factory_.GetWeakPtr(),
base::Passed(&upload_file_info)));
}
void DriveUploader::StartInitiateUploadExistingFile(
const GURL& upload_url,
const std::string& etag,
scoped_ptr<UploadFileInfo> upload_file_info) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
UploadFileInfo* info_ptr = upload_file_info.get(); UploadFileInfo* info_ptr = upload_file_info.get();
drive_service_->InitiateUpload( drive_service_->InitiateUploadExistingFile(
InitiateUploadParams(info_ptr->upload_mode, info_ptr->drive_path,
info_ptr->title, info_ptr->content_type,
info_ptr->content_type, info_ptr->content_length,
info_ptr->content_length, upload_url,
info_ptr->initial_upload_location, etag,
info_ptr->drive_path,
info_ptr->etag),
base::Bind(&DriveUploader::OnUploadLocationReceived, base::Bind(&DriveUploader::OnUploadLocationReceived,
weak_ptr_factory_.GetWeakPtr(), weak_ptr_factory_.GetWeakPtr(),
base::Passed(&upload_file_info))); base::Passed(&upload_file_info)));
...@@ -258,7 +272,7 @@ void DriveUploader::OnUploadLocationReceived( ...@@ -258,7 +272,7 @@ void DriveUploader::OnUploadLocationReceived(
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DVLOG(1) << "Got upload location [" << upload_location.spec() DVLOG(1) << "Got upload location [" << upload_location.spec()
<< "] for [" << upload_file_info->title << "]"; << "] for [" << upload_file_info->drive_path.value() << "]";
if (code != HTTP_SUCCESS) { if (code != HTTP_SUCCESS) {
// TODO(achuith): Handle error codes from Google Docs server. // TODO(achuith): Handle error codes from Google Docs server.
...@@ -358,7 +372,7 @@ void DriveUploader::OnUploadRangeResponseReceived( ...@@ -358,7 +372,7 @@ void DriveUploader::OnUploadRangeResponseReceived(
if ((upload_mode == UPLOAD_NEW_FILE && response.code == HTTP_CREATED) || if ((upload_mode == UPLOAD_NEW_FILE && response.code == HTTP_CREATED) ||
(upload_mode == UPLOAD_EXISTING_FILE && response.code == HTTP_SUCCESS)) { (upload_mode == UPLOAD_EXISTING_FILE && response.code == HTTP_SUCCESS)) {
DVLOG(1) << "Successfully created uploaded file=[" DVLOG(1) << "Successfully created uploaded file=["
<< upload_file_info->title << "]"; << upload_file_info->drive_path.value() << "]";
// Done uploading. // Done uploading.
upload_file_info->completion_callback.Run(DRIVE_UPLOAD_OK, upload_file_info->completion_callback.Run(DRIVE_UPLOAD_OK,
...@@ -395,7 +409,7 @@ void DriveUploader::OnUploadRangeResponseReceived( ...@@ -395,7 +409,7 @@ void DriveUploader::OnUploadRangeResponseReceived(
DVLOG(1) << "Received range " << response.start_position_received DVLOG(1) << "Received range " << response.start_position_received
<< "-" << response.end_position_received << "-" << response.end_position_received
<< " for [" << upload_file_info->title << "]"; << " for [" << upload_file_info->drive_path.value() << "]";
// Continue uploading. // Continue uploading.
UploadNextChunk(upload_file_info.Pass()); UploadNextChunk(upload_file_info.Pass());
......
...@@ -86,14 +86,14 @@ class DriveUploader : public DriveUploaderInterface { ...@@ -86,14 +86,14 @@ class DriveUploader : public DriveUploaderInterface {
virtual ~DriveUploader(); virtual ~DriveUploader();
// DriveUploaderInterface overrides. // DriveUploaderInterface overrides.
virtual void UploadNewFile(const GURL& upload_location, virtual void UploadNewFile(const GURL& parent_upload_url,
const base::FilePath& drive_file_path, const base::FilePath& drive_file_path,
const base::FilePath& local_file_path, const base::FilePath& local_file_path,
const std::string& title, const std::string& title,
const std::string& content_type, const std::string& content_type,
const UploadCompletionCallback& callback) OVERRIDE; const UploadCompletionCallback& callback) OVERRIDE;
virtual void UploadExistingFile( virtual void UploadExistingFile(
const GURL& upload_location, const GURL& upload_url,
const base::FilePath& drive_file_path, const base::FilePath& drive_file_path,
const base::FilePath& local_file_path, const base::FilePath& local_file_path,
const std::string& content_type, const std::string& content_type,
...@@ -102,14 +102,34 @@ class DriveUploader : public DriveUploaderInterface { ...@@ -102,14 +102,34 @@ class DriveUploader : public DriveUploaderInterface {
private: private:
struct UploadFileInfo; struct UploadFileInfo;
typedef base::Callback<void(scoped_ptr<UploadFileInfo> upload_file_info)>
StartInitiateUploadCallback;
// Starts uploading a file with |upload_file_info|. // Starts uploading a file with |upload_file_info|.
void StartUploadFile(scoped_ptr<UploadFileInfo> upload_file_info); 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 // net::FileStream::Open completion callback. The result of the file open
// operation is passed as |result|, and the size is stored in |file_size|. // operation is passed as |result|, and the size is stored in |file_size|.
void OpenCompletionCallback(scoped_ptr<UploadFileInfo> upload_file_info, void OpenCompletionCallback(
int64 file_size); scoped_ptr<UploadFileInfo> upload_file_info,
const StartInitiateUploadCallback& start_initiate_upload_callback,
int64 file_size);
// Starts to initate the new file uploading.
// Upon completion, OnUploadLocationReceived should be called.
void StartInitiateUploadNewFile(
const GURL& parent_upload_url,
const std::string& title,
scoped_ptr<UploadFileInfo> upload_file_info);
// Starts to initate the existing file uploading.
// Upon completion, OnUploadLocationReceived should be called.
void StartInitiateUploadExistingFile(
const GURL& upload_url,
const std::string& etag,
scoped_ptr<UploadFileInfo> upload_file_info);
// DriveService callback for InitiateUpload. // DriveService callback for InitiateUpload.
void OnUploadLocationReceived(scoped_ptr<UploadFileInfo> upload_file_info, void OnUploadLocationReceived(scoped_ptr<UploadFileInfo> upload_file_info,
......
...@@ -67,20 +67,38 @@ class MockDriveServiceWithUploadExpectation : public DummyDriveService { ...@@ -67,20 +67,38 @@ class MockDriveServiceWithUploadExpectation : public DummyDriveService {
private: private:
// DriveServiceInterface overrides. // DriveServiceInterface overrides.
// Handles a request for obtaining an upload location URL. // Handles a request for obtaining an upload location URL.
virtual void InitiateUpload(const InitiateUploadParams& params, virtual void InitiateUploadNewFile(
const InitiateUploadCallback& callback) OVERRIDE { const FilePath& drive_file_path,
const std::string& content_type,
int64 content_length,
const GURL& parent_upload_url,
const std::string& title,
const InitiateUploadCallback& callback) OVERRIDE {
EXPECT_EQ(kTestDocumentTitle, title);
EXPECT_EQ(kTestMimeType, content_type);
const int64 expected_size = expected_upload_content_.size(); const int64 expected_size = expected_upload_content_.size();
EXPECT_EQ(expected_size, content_length);
EXPECT_EQ(GURL(kTestInitialUploadURL), parent_upload_url);
// Verify that the expected parameters are passed. // Calls back the upload URL for subsequent ResumeUpload operations.
if (params.upload_mode == UPLOAD_NEW_FILE) // InitiateUpload is an asynchronous function, so don't callback directly.
EXPECT_EQ(kTestDocumentTitle, params.title); MessageLoop::current()->PostTask(FROM_HERE,
else base::Bind(callback, HTTP_SUCCESS, GURL(kTestUploadURL)));
EXPECT_EQ("", params.title); }
EXPECT_EQ(kTestMimeType, params.content_type);
EXPECT_EQ(expected_size, params.content_length); virtual void InitiateUploadExistingFile(
EXPECT_EQ(GURL(kTestInitialUploadURL), params.upload_location); const FilePath& drive_file_path,
const std::string& content_type,
int64 content_length,
const GURL& upload_url,
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(GURL(kTestInitialUploadURL), upload_url);
if (!params.etag.empty() && params.etag != kTestETag) { if (!etag.empty() && etag != kTestETag) {
MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(callback, HTTP_PRECONDITION, GURL())); base::Bind(callback, HTTP_PRECONDITION, GURL()));
return; return;
...@@ -152,8 +170,24 @@ class MockDriveServiceWithUploadExpectation : public DummyDriveService { ...@@ -152,8 +170,24 @@ class MockDriveServiceWithUploadExpectation : public DummyDriveService {
// Mock DriveService that returns a failure at InitiateUpload(). // Mock DriveService that returns a failure at InitiateUpload().
class MockDriveServiceNoConnectionAtInitiate : public DummyDriveService { class MockDriveServiceNoConnectionAtInitiate : public DummyDriveService {
// Returns error. // Returns error.
virtual void InitiateUpload(const InitiateUploadParams& params, virtual void InitiateUploadNewFile(
const InitiateUploadCallback& callback) OVERRIDE { const FilePath& drive_file_path,
const std::string& content_type,
int64 content_length,
const GURL& parent_upload_url,
const std::string& title,
const InitiateUploadCallback& callback) OVERRIDE {
MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(callback, GDATA_NO_CONNECTION, GURL()));
}
virtual void InitiateUploadExistingFile(
const FilePath& drive_file_path,
const std::string& content_type,
int64 content_length,
const GURL& upload_url,
const std::string& etag,
const InitiateUploadCallback& callback) OVERRIDE {
MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(callback, GDATA_NO_CONNECTION, GURL())); base::Bind(callback, GDATA_NO_CONNECTION, GURL()));
} }
...@@ -168,8 +202,24 @@ class MockDriveServiceNoConnectionAtInitiate : public DummyDriveService { ...@@ -168,8 +202,24 @@ class MockDriveServiceNoConnectionAtInitiate : public DummyDriveService {
// Mock DriveService that returns a failure at ResumeUpload(). // Mock DriveService that returns a failure at ResumeUpload().
class MockDriveServiceNoConnectionAtResume : public DummyDriveService { class MockDriveServiceNoConnectionAtResume : public DummyDriveService {
// Succeeds and returns an upload location URL. // Succeeds and returns an upload location URL.
virtual void InitiateUpload(const InitiateUploadParams& params, virtual void InitiateUploadNewFile(
const InitiateUploadCallback& callback) OVERRIDE { const FilePath& drive_file_path,
const std::string& content_type,
int64 content_length,
const GURL& parent_upload_url,
const std::string& title,
const InitiateUploadCallback& callback) OVERRIDE {
MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(callback, HTTP_SUCCESS, GURL(kTestInitialUploadURL)));
}
virtual void InitiateUploadExistingFile(
const FilePath& drive_file_path,
const std::string& content_type,
int64 content_length,
const GURL& upload_url,
const std::string& etag,
const InitiateUploadCallback& callback) OVERRIDE {
MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(callback, HTTP_SUCCESS, GURL(kTestInitialUploadURL))); base::Bind(callback, HTTP_SUCCESS, GURL(kTestInitialUploadURL)));
} }
......
...@@ -92,9 +92,21 @@ void DummyDriveService::AddNewDirectory( ...@@ -92,9 +92,21 @@ void DummyDriveService::AddNewDirectory(
const std::string& directory_name, const std::string& directory_name,
const GetResourceEntryCallback& callback) {} const GetResourceEntryCallback& callback) {}
void DummyDriveService::InitiateUpload(const InitiateUploadParams& params, void DummyDriveService::InitiateUploadNewFile(
const InitiateUploadCallback& callback) { const FilePath& drive_file_path,
} const std::string& content_type,
int64 content_length,
const GURL& parent_upload_url,
const std::string& title,
const InitiateUploadCallback& callback) {}
void DummyDriveService::InitiateUploadExistingFile(
const FilePath& drive_file_path,
const std::string& content_type,
int64 content_length,
const GURL& upload_url,
const std::string& etag,
const InitiateUploadCallback& callback) {}
void DummyDriveService::ResumeUpload(const ResumeUploadParams& params, void DummyDriveService::ResumeUpload(const ResumeUploadParams& params,
const UploadRangeCallback& callback) {} const UploadRangeCallback& callback) {}
......
...@@ -70,8 +70,20 @@ class DummyDriveService : public DriveServiceInterface { ...@@ -70,8 +70,20 @@ class DummyDriveService : public DriveServiceInterface {
const std::string& parent_resource_id, const std::string& parent_resource_id,
const std::string& directory_name, const std::string& directory_name,
const GetResourceEntryCallback& callback) OVERRIDE; const GetResourceEntryCallback& callback) OVERRIDE;
virtual void InitiateUpload(const InitiateUploadParams& params, virtual void InitiateUploadNewFile(
const InitiateUploadCallback& callback) OVERRIDE; const FilePath& drive_file_path,
const std::string& content_type,
int64 content_length,
const GURL& parent_upload_url,
const std::string& title,
const InitiateUploadCallback& callback) OVERRIDE;
virtual void InitiateUploadExistingFile(
const FilePath& drive_file_path,
const std::string& content_type,
int64 content_length,
const GURL& upload_url,
const std::string& etag,
const InitiateUploadCallback& callback) OVERRIDE;
virtual void ResumeUpload(const ResumeUploadParams& params, virtual void ResumeUpload(const ResumeUploadParams& params,
const UploadRangeCallback& callback) OVERRIDE; const UploadRangeCallback& callback) OVERRIDE;
virtual void GetUploadStatus( virtual void GetUploadStatus(
......
...@@ -777,8 +777,12 @@ void FakeDriveService::AddNewDirectory( ...@@ -777,8 +777,12 @@ void FakeDriveService::AddNewDirectory(
base::Bind(callback, HTTP_NOT_FOUND, base::Passed(&null))); base::Bind(callback, HTTP_NOT_FOUND, base::Passed(&null)));
} }
void FakeDriveService::InitiateUpload( void FakeDriveService::InitiateUploadNewFile(
const InitiateUploadParams& params, const FilePath& drive_file_path,
const std::string& content_type,
int64 content_length,
const GURL& parent_upload_url,
const std::string& title,
const InitiateUploadCallback& callback) { const InitiateUploadCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!callback.is_null()); DCHECK(!callback.is_null());
...@@ -790,7 +794,7 @@ void FakeDriveService::InitiateUpload( ...@@ -790,7 +794,7 @@ void FakeDriveService::InitiateUpload(
return; return;
} }
DictionaryValue* entry = FindEntryByUploadUrl(params.upload_location); DictionaryValue* entry = FindEntryByUploadUrl(parent_upload_url);
if (!entry) { if (!entry) {
MessageLoop::current()->PostTask( MessageLoop::current()->PostTask(
FROM_HERE, FROM_HERE,
...@@ -798,21 +802,6 @@ void FakeDriveService::InitiateUpload( ...@@ -798,21 +802,6 @@ void FakeDriveService::InitiateUpload(
return; return;
} }
if (params.upload_mode == UPLOAD_EXISTING_FILE) {
std::string etag;
entry->GetString("gd$etag", &etag);
if (params.etag != etag) {
MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(callback, HTTP_PRECONDITION, GURL()));
return;
}
MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(callback, HTTP_SUCCESS, params.upload_location));
return;
}
// If the title was set, the upload_location is the location of the parent // If the title was set, the upload_location is the location of the parent
// directory of the file that will be uploaded. The file does not yet exist // directory of the file that will be uploaded. The file does not yet exist
// and it must be created. Its title will be the passed title param. // and it must be created. Its title will be the passed title param.
...@@ -825,8 +814,8 @@ void FakeDriveService::InitiateUpload( ...@@ -825,8 +814,8 @@ void FakeDriveService::InitiateUpload(
scoped_ptr<base::DictionaryValue> new_entry(new base::DictionaryValue); scoped_ptr<base::DictionaryValue> new_entry(new base::DictionaryValue);
// Set the resource ID and the title // Set the resource ID and the title
new_entry->SetString("gd$resourceId.$t", resource_id); new_entry->SetString("gd$resourceId.$t", resource_id);
new_entry->SetString("title.$t", params.title); new_entry->SetString("title.$t", title);
new_entry->SetString("docs$filename", params.title); new_entry->SetString("docs$filename", title);
new_entry->SetString("docs$size", "0"); new_entry->SetString("docs$size", "0");
new_entry->SetString("docs$md5Checksum.$t", new_entry->SetString("docs$md5Checksum.$t",
"3b4385ebefec6e743574c76bbd0575de"); "3b4385ebefec6e743574c76bbd0575de");
...@@ -843,7 +832,7 @@ void FakeDriveService::InitiateUpload( ...@@ -843,7 +832,7 @@ void FakeDriveService::InitiateUpload(
// Add "content" which sets the content URL. // Add "content" which sets the content URL.
base::DictionaryValue* content = new base::DictionaryValue; base::DictionaryValue* content = new base::DictionaryValue;
content->SetString("src", "https://xxx/content/" + resource_id); content->SetString("src", "https://xxx/content/" + resource_id);
content->SetString("type", params.content_type); content->SetString("type", content_type);
new_entry->Set("content", content); new_entry->Set("content", content);
// Add "link" which sets the parent URL, the edit URL and the upload URL. // Add "link" which sets the parent URL, the edit URL and the upload URL.
...@@ -890,6 +879,44 @@ void FakeDriveService::InitiateUpload( ...@@ -890,6 +879,44 @@ void FakeDriveService::InitiateUpload(
base::Bind(callback, HTTP_SUCCESS, upload_url)); base::Bind(callback, HTTP_SUCCESS, upload_url));
} }
void FakeDriveService::InitiateUploadExistingFile(
const FilePath& drive_file_path,
const std::string& content_type,
int64 content_length,
const GURL& upload_url,
const std::string& etag,
const InitiateUploadCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!callback.is_null());
if (offline_) {
MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(callback, GDATA_NO_CONNECTION, GURL()));
return;
}
DictionaryValue* entry = FindEntryByUploadUrl(upload_url);
if (!entry) {
MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(callback, HTTP_NOT_FOUND, GURL()));
return;
}
std::string entry_etag;
entry->GetString("gd$etag", &entry_etag);
if (etag != entry_etag) {
MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(callback, HTTP_PRECONDITION, GURL()));
return;
}
MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(callback, HTTP_SUCCESS, upload_url));
}
void FakeDriveService::GetUploadStatus( void FakeDriveService::GetUploadStatus(
UploadMode upload_mode, UploadMode upload_mode,
const base::FilePath& drive_file_path, const base::FilePath& drive_file_path,
......
...@@ -119,8 +119,20 @@ class FakeDriveService : public DriveServiceInterface { ...@@ -119,8 +119,20 @@ class FakeDriveService : public DriveServiceInterface {
const std::string& parent_resource_id, const std::string& parent_resource_id,
const std::string& directory_name, const std::string& directory_name,
const GetResourceEntryCallback& callback) OVERRIDE; const GetResourceEntryCallback& callback) OVERRIDE;
virtual void InitiateUpload(const InitiateUploadParams& params, virtual void InitiateUploadNewFile(
const InitiateUploadCallback& callback) OVERRIDE; const FilePath& drive_file_path,
const std::string& content_type,
int64 content_length,
const GURL& parent_upload_url,
const std::string& title,
const InitiateUploadCallback& callback) OVERRIDE;
virtual void InitiateUploadExistingFile(
const FilePath& drive_file_path,
const std::string& content_type,
int64 content_length,
const GURL& upload_url,
const std::string& etag,
const InitiateUploadCallback& callback) OVERRIDE;
virtual void ResumeUpload(const ResumeUploadParams& params, virtual void ResumeUpload(const ResumeUploadParams& params,
const UploadRangeCallback& callback) OVERRIDE; const UploadRangeCallback& callback) OVERRIDE;
virtual void GetUploadStatus( virtual void GetUploadStatus(
......
...@@ -1042,21 +1042,18 @@ TEST_F(FakeDriveServiceTest, AddNewDirectory_Offline) { ...@@ -1042,21 +1042,18 @@ TEST_F(FakeDriveServiceTest, AddNewDirectory_Offline) {
EXPECT_FALSE(resource_entry); EXPECT_FALSE(resource_entry);
} }
TEST_F(FakeDriveServiceTest, InitiateUpload_Offline) { TEST_F(FakeDriveServiceTest, InitiateUploadNewFile_Offline) {
ASSERT_TRUE(fake_service_.LoadResourceListForWapi("gdata/root_feed.json")); ASSERT_TRUE(fake_service_.LoadResourceListForWapi("gdata/root_feed.json"));
fake_service_.set_offline(true); fake_service_.set_offline(true);
GDataErrorCode error = GDATA_OTHER_ERROR; GDataErrorCode error = GDATA_OTHER_ERROR;
GURL upload_location; GURL upload_location;
fake_service_.InitiateUpload( fake_service_.InitiateUploadNewFile(
InitiateUploadParams( base::FilePath(FILE_PATH_LITERAL("drive/Directory 1")),
UPLOAD_NEW_FILE, "test/foo",
"new file.foo", 13,
"test/foo", GURL("https://1_folder_resumable_create_media_link"),
13, "new file.foo",
GURL("https://1_folder_resumable_create_media_link"),
base::FilePath(FILE_PATH_LITERAL("drive/Directory 1")),
"etag_ignored"),
base::Bind(&test_util::CopyResultsFromInitiateUploadCallback, base::Bind(&test_util::CopyResultsFromInitiateUploadCallback,
&error, &error,
&upload_location)); &upload_location));
...@@ -1066,20 +1063,17 @@ TEST_F(FakeDriveServiceTest, InitiateUpload_Offline) { ...@@ -1066,20 +1063,17 @@ TEST_F(FakeDriveServiceTest, InitiateUpload_Offline) {
EXPECT_TRUE(upload_location.is_empty()); EXPECT_TRUE(upload_location.is_empty());
} }
TEST_F(FakeDriveServiceTest, InitiateUpload_NotFound) { TEST_F(FakeDriveServiceTest, InitiateUploadNewFile_NotFound) {
ASSERT_TRUE(fake_service_.LoadResourceListForWapi("gdata/root_feed.json")); ASSERT_TRUE(fake_service_.LoadResourceListForWapi("gdata/root_feed.json"));
GDataErrorCode error = GDATA_OTHER_ERROR; GDataErrorCode error = GDATA_OTHER_ERROR;
GURL upload_location; GURL upload_location;
fake_service_.InitiateUpload( fake_service_.InitiateUploadNewFile(
InitiateUploadParams( base::FilePath(FILE_PATH_LITERAL("drive/Directory 1")),
UPLOAD_NEW_FILE, "test/foo",
"new file.foo", 13,
"test/foo", GURL("https://non_existent"),
13, "new file.foo",
GURL("https://non_existent"),
base::FilePath(FILE_PATH_LITERAL("drive/Directory 1")),
"etag_ignored"),
base::Bind(&test_util::CopyResultsFromInitiateUploadCallback, base::Bind(&test_util::CopyResultsFromInitiateUploadCallback,
&error, &error,
&upload_location)); &upload_location));
...@@ -1089,20 +1083,17 @@ TEST_F(FakeDriveServiceTest, InitiateUpload_NotFound) { ...@@ -1089,20 +1083,17 @@ TEST_F(FakeDriveServiceTest, InitiateUpload_NotFound) {
EXPECT_TRUE(upload_location.is_empty()); EXPECT_TRUE(upload_location.is_empty());
} }
TEST_F(FakeDriveServiceTest, InitiateUpload_NewFile) { TEST_F(FakeDriveServiceTest, InitiateUploadNewFile) {
ASSERT_TRUE(fake_service_.LoadResourceListForWapi("gdata/root_feed.json")); ASSERT_TRUE(fake_service_.LoadResourceListForWapi("gdata/root_feed.json"));
GDataErrorCode error = GDATA_OTHER_ERROR; GDataErrorCode error = GDATA_OTHER_ERROR;
GURL upload_location; GURL upload_location;
fake_service_.InitiateUpload( fake_service_.InitiateUploadNewFile(
InitiateUploadParams( base::FilePath(FILE_PATH_LITERAL("drive/Directory 1")),
UPLOAD_NEW_FILE, "test/foo",
"new file.foo", 13,
"test/foo", GURL("https://1_folder_resumable_create_media_link"),
13, "new file.foo",
GURL("https://1_folder_resumable_create_media_link"),
base::FilePath(FILE_PATH_LITERAL("drive/Directory 1")),
"etag_ignored"),
base::Bind(&test_util::CopyResultsFromInitiateUploadCallback, base::Bind(&test_util::CopyResultsFromInitiateUploadCallback,
&error, &error,
&upload_location)); &upload_location));
...@@ -1114,20 +1105,58 @@ TEST_F(FakeDriveServiceTest, InitiateUpload_NewFile) { ...@@ -1114,20 +1105,58 @@ TEST_F(FakeDriveServiceTest, InitiateUpload_NewFile) {
upload_location); upload_location);
} }
TEST_F(FakeDriveServiceTest, InitiateUpload_WrongETag) { TEST_F(FakeDriveServiceTest, InitiateUploadExistingFile_Offline) {
ASSERT_TRUE(fake_service_.LoadResourceListForWapi("gdata/root_feed.json"));
fake_service_.set_offline(true);
GDataErrorCode error = GDATA_OTHER_ERROR;
GURL upload_location;
fake_service_.InitiateUploadExistingFile(
base::FilePath(FILE_PATH_LITERAL("drive/Directory 1")),
"test/foo",
13,
GURL("https://1_folder_resumable_create_media_link"),
"", // etag
base::Bind(&test_util::CopyResultsFromInitiateUploadCallback,
&error,
&upload_location));
message_loop_.RunUntilIdle();
EXPECT_EQ(GDATA_NO_CONNECTION, error);
EXPECT_TRUE(upload_location.is_empty());
}
TEST_F(FakeDriveServiceTest, InitiateUploadExistingFile_NotFound) {
ASSERT_TRUE(fake_service_.LoadResourceListForWapi("gdata/root_feed.json"));
GDataErrorCode error = GDATA_OTHER_ERROR;
GURL upload_location;
fake_service_.InitiateUploadExistingFile(
base::FilePath(FILE_PATH_LITERAL("drive/Directory 1")),
"test/foo",
13,
GURL("https://non_existent"),
"", // etag
base::Bind(&test_util::CopyResultsFromInitiateUploadCallback,
&error,
&upload_location));
message_loop_.RunUntilIdle();
EXPECT_EQ(HTTP_NOT_FOUND, error);
EXPECT_TRUE(upload_location.is_empty());
}
TEST_F(FakeDriveServiceTest, InitiateUploadExistingFile_WrongETag) {
ASSERT_TRUE(fake_service_.LoadResourceListForWapi("gdata/root_feed.json")); ASSERT_TRUE(fake_service_.LoadResourceListForWapi("gdata/root_feed.json"));
GDataErrorCode error = GDATA_OTHER_ERROR; GDataErrorCode error = GDATA_OTHER_ERROR;
GURL upload_location; GURL upload_location;
fake_service_.InitiateUpload( fake_service_.InitiateUploadExistingFile(
InitiateUploadParams( base::FilePath(FILE_PATH_LITERAL("drive/File 1.txt")),
UPLOAD_EXISTING_FILE, "text/plain",
"name_ignored", 13,
"text/plain", GURL("https://2_file_link_resumable_create_media"),
13, "invalid_etag",
GURL("https://2_file_link_resumable_create_media"),
base::FilePath(FILE_PATH_LITERAL("drive/File 1.txt")),
"invalid_etag"),
base::Bind(&test_util::CopyResultsFromInitiateUploadCallback, base::Bind(&test_util::CopyResultsFromInitiateUploadCallback,
&error, &error,
&upload_location)); &upload_location));
...@@ -1142,15 +1171,12 @@ TEST_F(FakeDriveServiceTest, InitiateUpload_ExistingFile) { ...@@ -1142,15 +1171,12 @@ TEST_F(FakeDriveServiceTest, InitiateUpload_ExistingFile) {
GDataErrorCode error = GDATA_OTHER_ERROR; GDataErrorCode error = GDATA_OTHER_ERROR;
GURL upload_location; GURL upload_location;
fake_service_.InitiateUpload( fake_service_.InitiateUploadExistingFile(
InitiateUploadParams( base::FilePath(FILE_PATH_LITERAL("drive/File 1.txt")),
UPLOAD_EXISTING_FILE, "text/plain",
"name_ignored", 13,
"text/plain", GURL("https://2_file_link_resumable_create_media"),
13, "\"HhMOFgxXHit7ImBr\"",
GURL("https://2_file_link_resumable_create_media"),
base::FilePath(FILE_PATH_LITERAL("drive/File 1.txt")),
"\"HhMOFgxXHit7ImBr\""),
base::Bind(&test_util::CopyResultsFromInitiateUploadCallback, base::Bind(&test_util::CopyResultsFromInitiateUploadCallback,
&error, &error,
&upload_location)); &upload_location));
...@@ -1166,15 +1192,12 @@ TEST_F(FakeDriveServiceTest, ResumeUpload_Offline) { ...@@ -1166,15 +1192,12 @@ TEST_F(FakeDriveServiceTest, ResumeUpload_Offline) {
GDataErrorCode error = GDATA_OTHER_ERROR; GDataErrorCode error = GDATA_OTHER_ERROR;
GURL upload_location; GURL upload_location;
fake_service_.InitiateUpload( fake_service_.InitiateUploadNewFile(
InitiateUploadParams( base::FilePath(FILE_PATH_LITERAL("drive/Directory 1/new file.foo")),
UPLOAD_NEW_FILE, "test/foo",
"new file.foo", 15,
"test/foo", GURL("https://1_folder_resumable_create_media_link"),
15, "new file.foo",
GURL("https://1_folder_resumable_create_media_link"),
base::FilePath(FILE_PATH_LITERAL("drive/Directory 1/new file.foo")),
"etag_ignored"),
base::Bind(&test_util::CopyResultsFromInitiateUploadCallback, base::Bind(&test_util::CopyResultsFromInitiateUploadCallback,
&error, &error,
&upload_location)); &upload_location));
...@@ -1209,15 +1232,12 @@ TEST_F(FakeDriveServiceTest, ResumeUpload_NotFound) { ...@@ -1209,15 +1232,12 @@ TEST_F(FakeDriveServiceTest, ResumeUpload_NotFound) {
GDataErrorCode error = GDATA_OTHER_ERROR; GDataErrorCode error = GDATA_OTHER_ERROR;
GURL upload_location; GURL upload_location;
fake_service_.InitiateUpload( fake_service_.InitiateUploadNewFile(
InitiateUploadParams( base::FilePath(FILE_PATH_LITERAL("drive/Directory 1/new file.foo")),
UPLOAD_NEW_FILE, "test/foo",
"new file.foo", 15,
"test/foo", GURL("https://1_folder_resumable_create_media_link"),
15, "new file.foo",
GURL("https://1_folder_resumable_create_media_link"),
base::FilePath(FILE_PATH_LITERAL("drive/Directory 1/new file.foo")),
"etag_ignored"),
base::Bind(&test_util::CopyResultsFromInitiateUploadCallback, base::Bind(&test_util::CopyResultsFromInitiateUploadCallback,
&error, &error,
&upload_location)); &upload_location));
...@@ -1247,15 +1267,12 @@ TEST_F(FakeDriveServiceTest, ResumeUpload_ExistingFile) { ...@@ -1247,15 +1267,12 @@ TEST_F(FakeDriveServiceTest, ResumeUpload_ExistingFile) {
GDataErrorCode error = GDATA_OTHER_ERROR; GDataErrorCode error = GDATA_OTHER_ERROR;
GURL upload_location; GURL upload_location;
fake_service_.InitiateUpload( fake_service_.InitiateUploadExistingFile(
InitiateUploadParams( base::FilePath(FILE_PATH_LITERAL("drive/File 1.txt")),
UPLOAD_EXISTING_FILE, "text/plain",
"name_ignored", 15,
"text/plain", GURL("https://2_file_link_resumable_create_media"),
15, "\"HhMOFgxXHit7ImBr\"",
GURL("https://2_file_link_resumable_create_media"),
base::FilePath(FILE_PATH_LITERAL("drive/File 1.txt")),
"\"HhMOFgxXHit7ImBr\""),
base::Bind(&test_util::CopyResultsFromInitiateUploadCallback, base::Bind(&test_util::CopyResultsFromInitiateUploadCallback,
&error, &error,
&upload_location)); &upload_location));
...@@ -1301,15 +1318,12 @@ TEST_F(FakeDriveServiceTest, ResumeUpload_NewFile) { ...@@ -1301,15 +1318,12 @@ TEST_F(FakeDriveServiceTest, ResumeUpload_NewFile) {
GDataErrorCode error = GDATA_OTHER_ERROR; GDataErrorCode error = GDATA_OTHER_ERROR;
GURL upload_location; GURL upload_location;
fake_service_.InitiateUpload( fake_service_.InitiateUploadNewFile(
InitiateUploadParams( base::FilePath(FILE_PATH_LITERAL("drive/Directory 1/new file.foo")),
UPLOAD_NEW_FILE, "test/foo",
"new file.foo", 15,
"test/foo", GURL("https://1_folder_resumable_create_media_link"),
15, "new file.foo",
GURL("https://1_folder_resumable_create_media_link"),
base::FilePath(FILE_PATH_LITERAL("drive/Directory 1/new file.foo")),
"etag_ignored"),
base::Bind(&test_util::CopyResultsFromInitiateUploadCallback, base::Bind(&test_util::CopyResultsFromInitiateUploadCallback,
&error, &error,
&upload_location)); &upload_location));
......
...@@ -408,43 +408,56 @@ void GDataWapiService::RemoveResourceFromDirectory( ...@@ -408,43 +408,56 @@ void GDataWapiService::RemoveResourceFromDirectory(
resource_id)); resource_id));
} }
void GDataWapiService::InitiateUpload( void GDataWapiService::InitiateUploadNewFile(
const InitiateUploadParams& params, const FilePath& drive_file_path,
const std::string& content_type,
int64 content_length,
const GURL& parent_upload_url,
const std::string& title,
const InitiateUploadCallback& callback) { const InitiateUploadCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!callback.is_null()); DCHECK(!callback.is_null());
if (params.upload_location.is_empty()) { if (parent_upload_url.is_empty()) {
callback.Run(HTTP_BAD_REQUEST, GURL()); callback.Run(HTTP_BAD_REQUEST, GURL());
return; return;
} }
// TODO(hidehiko): Remove this if-statement by splitting the InitiateUpload runner_->StartOperationWithRetry(
// method into two InitiateUploadNewFile and InitiateUploadExistingFile. new InitiateUploadNewFileOperation(operation_registry(),
if (params.upload_mode == UPLOAD_NEW_FILE) { url_request_context_getter_,
runner_->StartOperationWithRetry( callback,
new InitiateUploadNewFileOperation( drive_file_path,
operation_registry(), content_type,
url_request_context_getter_, content_length,
callback, parent_upload_url,
params.drive_file_path, title));
params.content_type, }
params.content_length,
params.upload_location, // Here, upload_location has parent's URL. void GDataWapiService::InitiateUploadExistingFile(
params.title)); const FilePath& drive_file_path,
} else { const std::string& content_type,
DCHECK_EQ(params.upload_mode, UPLOAD_EXISTING_FILE); int64 content_length,
runner_->StartOperationWithRetry( const GURL& upload_url,
new InitiateUploadExistingFileOperation( const std::string& etag,
operation_registry(), const InitiateUploadCallback& callback) {
url_request_context_getter_, DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
callback, DCHECK(!callback.is_null());
params.drive_file_path,
params.content_type, if (upload_url.is_empty()) {
params.content_length, callback.Run(HTTP_BAD_REQUEST, GURL());
params.upload_location, // Here, upload_location has file's URL. return;
params.etag));
} }
runner_->StartOperationWithRetry(
new InitiateUploadExistingFileOperation(operation_registry(),
url_request_context_getter_,
callback,
drive_file_path,
content_type,
content_length,
upload_url,
etag));
} }
void GDataWapiService::ResumeUpload(const ResumeUploadParams& params, void GDataWapiService::ResumeUpload(const ResumeUploadParams& params,
......
...@@ -107,8 +107,19 @@ class GDataWapiService : public DriveServiceInterface, ...@@ -107,8 +107,19 @@ class GDataWapiService : public DriveServiceInterface,
const std::string& parent_resource_id, const std::string& parent_resource_id,
const std::string& directory_name, const std::string& directory_name,
const GetResourceEntryCallback& callback) OVERRIDE; const GetResourceEntryCallback& callback) OVERRIDE;
virtual void InitiateUpload( virtual void InitiateUploadNewFile(
const InitiateUploadParams& params, const FilePath& drive_file_path,
const std::string& content_type,
int64 content_length,
const GURL& parent_upload_url,
const std::string& title,
const InitiateUploadCallback& callback) OVERRIDE;
virtual void InitiateUploadExistingFile(
const FilePath& drive_file_path,
const std::string& content_type,
int64 content_length,
const GURL& upload_url,
const std::string& etag,
const InitiateUploadCallback& callback) OVERRIDE; const InitiateUploadCallback& callback) OVERRIDE;
virtual void ResumeUpload( virtual void ResumeUpload(
const ResumeUploadParams& params, const ResumeUploadParams& params,
......
...@@ -80,8 +80,19 @@ class MockDriveService : public DriveServiceInterface { ...@@ -80,8 +80,19 @@ class MockDriveService : public DriveServiceInterface {
const GURL& download_url, const GURL& download_url,
const DownloadActionCallback& donwload_action_callback, const DownloadActionCallback& donwload_action_callback,
const GetContentCallback& get_content_callback)); const GetContentCallback& get_content_callback));
MOCK_METHOD2(InitiateUpload, MOCK_METHOD6(InitiateUploadNewFile,
void(const InitiateUploadParams& upload_file_info, void(const FilePath& drive_file_path,
const std::string& content_type,
int64 content_length,
const GURL& parent_upload_url,
const std::string& title,
const InitiateUploadCallback& callback));
MOCK_METHOD6(InitiateUploadExistingFile,
void(const FilePath& drive_file_path,
const std::string& content_type,
int64 content_length,
const GURL& upload_url,
const std::string& etag,
const InitiateUploadCallback& callback)); const InitiateUploadCallback& callback));
MOCK_METHOD2(ResumeUpload, MOCK_METHOD2(ResumeUpload,
void(const ResumeUploadParams& upload_file_info, void(const ResumeUploadParams& upload_file_info,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment