diff options
author | hidehiko@chromium.org <hidehiko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-02-04 08:03:24 +0000 |
---|---|---|
committer | hidehiko@chromium.org <hidehiko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-02-04 08:03:24 +0000 |
commit | 6e27016d11bce9018f7f1464269fa966ab11658e (patch) | |
tree | e292b1c3482a2f8f67c5c707907a24f63e54a2cb | |
parent | acfad61b296c5caa9fda037af77aefad7a224c60 (diff) | |
download | chromium_src-6e27016d11bce9018f7f1464269fa966ab11658e.zip chromium_src-6e27016d11bce9018f7f1464269fa966ab11658e.tar.gz chromium_src-6e27016d11bce9018f7f1464269fa966ab11658e.tar.bz2 |
Implement RenameResourceOperation on Drive API v2.
By this CL, now the DriveAPIService supports renaming a resource.
BUG=148631
TEST=Ran unit_tests
Review URL: https://chromiumcodereview.appspot.com/12093085
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@180358 0039d316-1c4b-4281-b951-d872f2087c98
4 files changed, 130 insertions, 2 deletions
diff --git a/chrome/browser/google_apis/drive_api_operations.cc b/chrome/browser/google_apis/drive_api_operations.cc index 1bb4152..c32124a 100644 --- a/chrome/browser/google_apis/drive_api_operations.cc +++ b/chrome/browser/google_apis/drive_api_operations.cc @@ -13,6 +13,17 @@ namespace { const char kContentTypeApplicationJson[] = "application/json"; const char kDirectoryMimeType[] = "application/vnd.google-apps.folder"; +// This is an annotation to use PATCH request documented here: +// https://developers.google.com/drive/performance#patch +// Now, UrlFetcher doesn't support PATCH comment, and this is a workaround +// in such a case. +// TODO(hidehiko): Use PATCH command directly, when it is supported. +// crbug.com/173315 +const char kHttpMethodOverridePatchHeader[] = "X-HTTP-Method-Override: PATCH"; + +// etag matching header. +const char kIfMatchAllHeader[] = "If-Match: *"; + } // namespace //============================== GetAboutOperation ============================= @@ -168,5 +179,53 @@ bool CreateDirectoryOperation::GetContentData(std::string* upload_content_type, return true; } +//=========================== RenameResourceOperation ========================== + +RenameResourceOperation::RenameResourceOperation( + OperationRegistry* registry, + net::URLRequestContextGetter* url_request_context_getter, + const DriveApiUrlGenerator& url_generator, + const std::string& resource_id, + const std::string& new_name, + const EntryActionCallback& callback) + : EntryActionOperation(registry, url_request_context_getter, callback), + url_generator_(url_generator), + resource_id_(resource_id), + new_name_(new_name) { + DCHECK(!callback.is_null()); +} + +RenameResourceOperation::~RenameResourceOperation() {} + +net::URLFetcher::RequestType RenameResourceOperation::GetRequestType() const { + // TODO(hidehiko): Use PATCH operation, when it is supported. + return net::URLFetcher::POST; +} + +std::vector<std::string> +RenameResourceOperation::GetExtraRequestHeaders() const { + std::vector<std::string> headers; + headers.push_back(kHttpMethodOverridePatchHeader); + headers.push_back(kIfMatchAllHeader); + return headers; +} + +GURL RenameResourceOperation::GetURL() const { + return url_generator_.GetFileUrl(resource_id_); +} + +bool RenameResourceOperation::GetContentData(std::string* upload_content_type, + std::string* upload_content) { + *upload_content_type = kContentTypeApplicationJson; + + base::DictionaryValue root; + root.SetString("title", new_name_); + base::JSONWriter::Write(&root, upload_content); + + DVLOG(1) << "RenameResource data: " << *upload_content_type << ", [" + << *upload_content << "]"; + return true; +} + } // namespace drive } // namespace google_apis diff --git a/chrome/browser/google_apis/drive_api_operations.h b/chrome/browser/google_apis/drive_api_operations.h index 0c52fb2..b28e3ef 100644 --- a/chrome/browser/google_apis/drive_api_operations.h +++ b/chrome/browser/google_apis/drive_api_operations.h @@ -174,6 +174,37 @@ class CreateDirectoryOperation : public GetDataOperation { DISALLOW_COPY_AND_ASSIGN(CreateDirectoryOperation); }; +//=========================== RenameResourceOperation ========================== + +// This class performs the operation for renaming a document/file/directory. +class RenameResourceOperation : public EntryActionOperation { + public: + // |callback| must not be null. + RenameResourceOperation( + OperationRegistry* registry, + net::URLRequestContextGetter* url_request_context_getter, + const DriveApiUrlGenerator& url_generator, + const std::string& resource_id, + const std::string& new_name, + const EntryActionCallback& callback); + virtual ~RenameResourceOperation(); + + protected: + // UrlFetchOperationBase overrides. + virtual net::URLFetcher::RequestType GetRequestType() const OVERRIDE; + virtual std::vector<std::string> GetExtraRequestHeaders() const OVERRIDE; + virtual GURL GetURL() const OVERRIDE; + virtual bool GetContentData(std::string* upload_content_type, + std::string* upload_content) OVERRIDE; + + private: + const DriveApiUrlGenerator url_generator_; + const std::string resource_id_; + const std::string new_name_; + + DISALLOW_COPY_AND_ASSIGN(RenameResourceOperation); +}; + } // namespace drive } // namespace google_apis diff --git a/chrome/browser/google_apis/drive_api_operations_unittest.cc b/chrome/browser/google_apis/drive_api_operations_unittest.cc index f6f16bb..9da7fca 100644 --- a/chrome/browser/google_apis/drive_api_operations_unittest.cc +++ b/chrome/browser/google_apis/drive_api_operations_unittest.cc @@ -178,4 +178,36 @@ TEST_F(DriveApiOperationsTest, CreateDirectoryOperation) { http_request_.content); } +TEST_F(DriveApiOperationsTest, RenameResourceOperation) { + // Set an expected data file containing the directory's entry data. + // It'd be returned if we rename a directory. + expected_data_file_path_ = + test_util::GetTestFilePath("drive/directory_entry.json"); + + GDataErrorCode error = GDATA_OTHER_ERROR; + + // Create "new directory" in the root directory. + drive::RenameResourceOperation* operation = + new drive::RenameResourceOperation( + &operation_registry_, + request_context_getter_.get(), + *url_generator_, + "resource_id", + "new name", + base::Bind(&test_util::CopyResultFromEntryActionCallbackAndQuit, + &error)); + operation->Start(kTestDriveApiAuthToken, kTestUserAgent, + base::Bind(&test_util::DoNothingForReAuthenticateCallback)); + MessageLoop::current()->Run(); + + EXPECT_EQ(HTTP_SUCCESS, error); + EXPECT_EQ(test_server::METHOD_POST, http_request_.method); + EXPECT_EQ("/drive/v2/files/resource_id", http_request_.relative_url); + EXPECT_EQ("PATCH", http_request_.headers["X-HTTP-Method-Override"]); + EXPECT_EQ("application/json", http_request_.headers["Content-Type"]); + + EXPECT_TRUE(http_request_.has_content); + EXPECT_EQ("{\"title\":\"new name\"}", http_request_.content); +} + } // namespace google_apis diff --git a/chrome/browser/google_apis/drive_api_service.cc b/chrome/browser/google_apis/drive_api_service.cc index 3b8cd13..2c37597 100644 --- a/chrome/browser/google_apis/drive_api_service.cc +++ b/chrome/browser/google_apis/drive_api_service.cc @@ -392,8 +392,14 @@ void DriveAPIService::RenameResource( DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(!callback.is_null()); - // TODO(kochi): Implement this. - NOTREACHED(); + runner_->StartOperationWithRetry( + new drive::RenameResourceOperation( + operation_registry(), + url_request_context_getter_, + url_generator_, + resource_id, + new_name, + callback)); } void DriveAPIService::AddResourceToDirectory( |