Commit 105170c5 authored by hidehiko@chromium.org's avatar hidehiko@chromium.org

Refactor TrashResourceRequest.

The operation is mapped to "Files: trash" on Drive API v2.
To adapt the API more, renames and changes the style.

BUG=277253
TEST=Ran unit_tests and tested manually.

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@220545 0039d316-1c4b-4281-b951-d872f2087c98
parent 0061fe75
......@@ -62,12 +62,12 @@ using google_apis::drive::FilesGetRequest;
using google_apis::drive::FilesInsertRequest;
using google_apis::drive::FilesPatchRequest;
using google_apis::drive::FilesListRequest;
using google_apis::drive::FilesTrashRequest;
using google_apis::drive::GetUploadStatusRequest;
using google_apis::drive::InitiateUploadExistingFileRequest;
using google_apis::drive::InitiateUploadNewFileRequest;
using google_apis::drive::InsertResourceRequest;
using google_apis::drive::ResumeUploadRequest;
using google_apis::drive::TrashResourceRequest;
namespace drive {
......@@ -592,11 +592,11 @@ CancelCallback DriveAPIService::DeleteResource(
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!callback.is_null());
return sender_->StartRequestWithRetry(new TrashResourceRequest(
sender_.get(),
url_generator_,
resource_id,
callback));
FilesTrashRequest* request = new FilesTrashRequest(
sender_.get(), url_generator_,
base::Bind(&EntryActionCallbackAdapter, callback));
request->set_file_id(resource_id);
return sender_->StartRequestWithRetry(request);
}
CancelCallback DriveAPIService::AddNewDirectory(
......
......@@ -326,6 +326,28 @@ GURL FilesListRequest::GetURL() const {
return url_generator_.GetFilesListUrl(max_results_, page_token_, q_);
}
//============================ FilesTrashRequest =============================
FilesTrashRequest::FilesTrashRequest(
RequestSender* sender,
const DriveApiUrlGenerator& url_generator,
const FileResourceCallback& callback)
: GetDataRequest(sender,
base::Bind(&ParseJsonAndRun<FileResource>, callback)),
url_generator_(url_generator) {
DCHECK(!callback.is_null());
}
FilesTrashRequest::~FilesTrashRequest() {}
net::URLFetcher::RequestType FilesTrashRequest::GetRequestType() const {
return net::URLFetcher::POST;
}
GURL FilesTrashRequest::GetURL() const {
return url_generator_.GetFilesTrashUrl(file_id_);
}
//============================== AboutGetRequest =============================
AboutGetRequest::AboutGetRequest(
......@@ -518,29 +540,6 @@ bool MoveResourceRequest::GetContentData(std::string* upload_content_type,
return true;
}
//=========================== TrashResourceRequest ===========================
TrashResourceRequest::TrashResourceRequest(
RequestSender* sender,
const DriveApiUrlGenerator& url_generator,
const std::string& resource_id,
const EntryActionCallback& callback)
: EntryActionRequest(sender, callback),
url_generator_(url_generator),
resource_id_(resource_id) {
DCHECK(!callback.is_null());
}
TrashResourceRequest::~TrashResourceRequest() {}
GURL TrashResourceRequest::GetURL() const {
return url_generator_.GetFileTrashUrl(resource_id_);
}
net::URLFetcher::RequestType TrashResourceRequest::GetRequestType() const {
return net::URLFetcher::POST;
}
//========================== InsertResourceRequest ===========================
InsertResourceRequest::InsertResourceRequest(
......
......@@ -258,6 +258,35 @@ class FilesListRequest : public GetDataRequest {
DISALLOW_COPY_AND_ASSIGN(FilesListRequest);
};
//=========================== TrashResourceRequest ===========================
// This class performs the request for trashing a resource.
// This request is mapped to
// https://developers.google.com/drive/v2/reference/files/trash
class FilesTrashRequest : public GetDataRequest {
public:
FilesTrashRequest(RequestSender* sender,
const DriveApiUrlGenerator& url_generator,
const FileResourceCallback& callback);
virtual ~FilesTrashRequest();
// Required parameter.
const std::string& file_id() const { return file_id_; }
void set_file_id(const std::string& file_id) { file_id_ = file_id; }
protected:
// UrlFetchRequestBase overrides.
virtual net::URLFetcher::RequestType GetRequestType() const OVERRIDE;
virtual GURL GetURL() const OVERRIDE;
private:
const DriveApiUrlGenerator url_generator_;
std::string file_id_;
DISALLOW_COPY_AND_ASSIGN(FilesTrashRequest);
};
//============================== AboutGetRequest =============================
// This class performs the request for fetching About data.
......@@ -441,40 +470,6 @@ class MoveResourceRequest : public GetDataRequest {
DISALLOW_COPY_AND_ASSIGN(MoveResourceRequest);
};
//=========================== TrashResourceRequest ===========================
// This class performs the request for trashing a resource.
//
// According to the document:
// https://developers.google.com/drive/v2/reference/files/trash
// the file resource will be returned from the server, which is not in the
// response from WAPI server. For the transition, we simply ignore the result,
// because now we do not handle resources in trash.
// Note for the naming: the name "trash" comes from the server's request
// name. In order to be consistent with the server, we chose "trash" here,
// although we are preferring the term "remove" in drive/google_api code.
// TODO(hidehiko): Replace the base class to GetDataRequest.
class TrashResourceRequest : public EntryActionRequest {
public:
// |callback| must not be null.
TrashResourceRequest(RequestSender* sender,
const DriveApiUrlGenerator& url_generator,
const std::string& resource_id,
const EntryActionCallback& callback);
virtual ~TrashResourceRequest();
protected:
// UrlFetchRequestBase overrides.
virtual GURL GetURL() const OVERRIDE;
virtual net::URLFetcher::RequestType GetRequestType() const OVERRIDE;
private:
const DriveApiUrlGenerator url_generator_;
const std::string resource_id_;
DISALLOW_COPY_AND_ASSIGN(TrashResourceRequest);
};
//========================== InsertResourceRequest ===========================
// This class performs the request for inserting a resource to a directory.
......
......@@ -681,6 +681,36 @@ TEST_F(DriveApiRequestsTest, ContinueGetFileListRequest) {
EXPECT_TRUE(result);
}
TEST_F(DriveApiRequestsTest, FilesTrashRequest) {
// Set data for the expected result. Directory entry should be returned
// if the trashing entry is a directory, so using it here should be fine.
expected_data_file_path_ =
test_util::GetTestFilePath("drive/directory_entry.json");
GDataErrorCode error = GDATA_OTHER_ERROR;
scoped_ptr<FileResource> file_resource;
// Trash a resource with the given resource id.
{
base::RunLoop run_loop;
drive::FilesTrashRequest* request = new drive::FilesTrashRequest(
request_sender_.get(),
*url_generator_,
test_util::CreateQuitCallback(
&run_loop,
test_util::CreateCopyResultCallback(&error, &file_resource)));
request->set_file_id("resource_id");
request_sender_->StartRequestWithRetry(request);
run_loop.Run();
}
EXPECT_EQ(HTTP_SUCCESS, error);
EXPECT_EQ(net::test_server::METHOD_POST, http_request_.method);
EXPECT_EQ("/drive/v2/files/resource_id/trash", http_request_.relative_url);
EXPECT_TRUE(http_request_.has_content);
EXPECT_TRUE(http_request_.content.empty());
}
TEST_F(DriveApiRequestsTest, TouchResourceRequest) {
// Set an expected data file containing the directory's entry data.
// It'd be returned if we rename a directory.
......@@ -797,36 +827,6 @@ TEST_F(DriveApiRequestsTest, MoveResourceRequest_EmptyParentResourceId) {
EXPECT_TRUE(file_resource);
}
TEST_F(DriveApiRequestsTest, TrashResourceRequest) {
// Set data for the expected result. Directory entry should be returned
// if the trashing entry is a directory, so using it here should be fine.
expected_data_file_path_ =
test_util::GetTestFilePath("drive/directory_entry.json");
GDataErrorCode error = GDATA_OTHER_ERROR;
// Trash a resource with the given resource id.
{
base::RunLoop run_loop;
drive::TrashResourceRequest* request =
new drive::TrashResourceRequest(
request_sender_.get(),
*url_generator_,
"resource_id",
test_util::CreateQuitCallback(
&run_loop,
test_util::CreateCopyResultCallback(&error)));
request_sender_->StartRequestWithRetry(request);
run_loop.Run();
}
EXPECT_EQ(HTTP_SUCCESS, error);
EXPECT_EQ(net::test_server::METHOD_POST, http_request_.method);
EXPECT_EQ("/drive/v2/files/resource_id/trash", http_request_.relative_url);
EXPECT_TRUE(http_request_.has_content);
EXPECT_TRUE(http_request_.content.empty());
}
TEST_F(DriveApiRequestsTest, InsertResourceRequest) {
// Set an expected data file containing the children entry.
expected_content_type_ = "application/json";
......
......@@ -115,6 +115,11 @@ GURL DriveApiUrlGenerator::GetFilesListUrl(int max_results,
return url;
}
GURL DriveApiUrlGenerator::GetFilesTrashUrl(const std::string& file_id) const {
return base_url_.Resolve(base::StringPrintf(
kDriveV2FileTrashUrlFormat, net::EscapePath(file_id).c_str()));
}
GURL DriveApiUrlGenerator::GetFileTouchUrl(
const std::string& resource_id) const {
GURL url = base_url_.Resolve(
......@@ -130,12 +135,6 @@ GURL DriveApiUrlGenerator::GetFileTouchUrl(
return url;
}
GURL DriveApiUrlGenerator::GetFileTrashUrl(const std::string& file_id) const {
return base_url_.Resolve(
base::StringPrintf(kDriveV2FileTrashUrlFormat,
net::EscapePath(file_id).c_str()));
}
GURL DriveApiUrlGenerator::GetChangesListUrl(bool include_deleted,
int max_results,
const std::string& page_token,
......
......@@ -51,15 +51,12 @@ class DriveApiUrlGenerator {
const std::string& page_token,
const std::string& q) const;
// Returns a URL to trash a resource with the given |file_id|.
GURL GetFilesTrashUrl(const std::string& file_id) const;
// Returns a URL to touch a resource specified by |resource_id|.
GURL GetFileTouchUrl(const std::string& resource_id) const;
// Returns a URL to trash a resource with the given |resource_id|.
// Note that the |resource_id| is corresponding to the "file id" in the
// document: https://developers.google.com/drive/v2/reference/files/trash
// but we use the term "resource" for consistency in our code.
GURL GetFileTrashUrl(const std::string& resource_id) const;
// Returns a URL to fetch a list of changes.
GURL GetChangesListUrl(bool include_deleted,
int max_results,
......
......@@ -186,6 +186,23 @@ TEST_F(DriveApiUrlGeneratorTest, GetFilesListUrl) {
}
}
TEST_F(DriveApiUrlGeneratorTest, GetFilesTrashUrl) {
// |file_id| should be embedded into the url.
EXPECT_EQ("https://www.googleapis.com/drive/v2/files/0ADK06pfg/trash",
url_generator_.GetFilesTrashUrl("0ADK06pfg").spec());
EXPECT_EQ("https://www.googleapis.com/drive/v2/files/0Bz0bd074/trash",
url_generator_.GetFilesTrashUrl("0Bz0bd074").spec());
EXPECT_EQ("https://www.googleapis.com/drive/v2/files/file%3Afile_id/trash",
url_generator_.GetFilesTrashUrl("file:file_id").spec());
EXPECT_EQ("http://127.0.0.1:12345/drive/v2/files/0ADK06pfg/trash",
test_url_generator_.GetFilesTrashUrl("0ADK06pfg").spec());
EXPECT_EQ("http://127.0.0.1:12345/drive/v2/files/0Bz0bd074/trash",
test_url_generator_.GetFilesTrashUrl("0Bz0bd074").spec());
EXPECT_EQ("http://127.0.0.1:12345/drive/v2/files/file%3Afile_id/trash",
test_url_generator_.GetFilesTrashUrl("file:file_id").spec());
}
TEST_F(DriveApiUrlGeneratorTest, GetFileTouchUrl) {
// |file_id| should be embedded into the url.
EXPECT_EQ("https://www.googleapis.com/drive/v2/files/0ADK06pfg"
......@@ -328,23 +345,6 @@ TEST_F(DriveApiUrlGeneratorTest, GetChildrenUrlForRemoval) {
"file:folder_id", "file:child_id").spec());
}
TEST_F(DriveApiUrlGeneratorTest, GetFileTrashUrl) {
// |file_id| should be embedded into the url.
EXPECT_EQ("https://www.googleapis.com/drive/v2/files/0ADK06pfg/trash",
url_generator_.GetFileTrashUrl("0ADK06pfg").spec());
EXPECT_EQ("https://www.googleapis.com/drive/v2/files/0Bz0bd074/trash",
url_generator_.GetFileTrashUrl("0Bz0bd074").spec());
EXPECT_EQ("https://www.googleapis.com/drive/v2/files/file%3Afile_id/trash",
url_generator_.GetFileTrashUrl("file:file_id").spec());
EXPECT_EQ("http://127.0.0.1:12345/drive/v2/files/0ADK06pfg/trash",
test_url_generator_.GetFileTrashUrl("0ADK06pfg").spec());
EXPECT_EQ("http://127.0.0.1:12345/drive/v2/files/0Bz0bd074/trash",
test_url_generator_.GetFileTrashUrl("0Bz0bd074").spec());
EXPECT_EQ("http://127.0.0.1:12345/drive/v2/files/file%3Afile_id/trash",
test_url_generator_.GetFileTrashUrl("file:file_id").spec());
}
TEST_F(DriveApiUrlGeneratorTest, GetInitiateUploadNewFileUrl) {
EXPECT_EQ(
"https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable",
......
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