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

Use CopyResourceOnServer to upload JSON GDoc file on Drive API v2.

Drive API v2 can copy a file not only into root directory but also other
directory directly, and it should more efficient and simpler.
This CL uses it to upload JSON GDoc file.

BUG=277353
TEST=Ran unit_tests and tested manually.

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@221697 0039d316-1c4b-4281-b951-d872f2087c98
parent c2ef94e8
......@@ -127,7 +127,8 @@ FileError PrepareTransferFileFromLocalToRemote(
internal::ResourceMetadata* metadata,
const base::FilePath& local_src_path,
const base::FilePath& remote_dest_path,
std::string* gdoc_resource_id) {
std::string* gdoc_resource_id,
std::string* parent_resource_id) {
ResourceEntry parent_entry;
FileError error = metadata->GetResourceEntryByPath(
remote_dest_path.DirName(), &parent_entry);
......@@ -140,8 +141,10 @@ FileError PrepareTransferFileFromLocalToRemote(
// Try to parse GDoc File and extract the resource id, if necessary.
// Failing isn't problem. It'd be handled as a regular file, then.
if (util::HasGDocFileExtension(local_src_path))
if (util::HasGDocFileExtension(local_src_path)) {
*gdoc_resource_id = util::ReadResourceIdFromGDocFile(local_src_path);
*parent_resource_id = parent_entry.resource_id();
}
return FILE_ERROR_OK;
}
......@@ -284,17 +287,19 @@ void CopyOperation::TransferFileFromLocalToRemote(
DCHECK(!callback.is_null());
std::string* gdoc_resource_id = new std::string;
std::string* parent_resource_id = new std::string;
base::PostTaskAndReplyWithResult(
blocking_task_runner_.get(),
FROM_HERE,
base::Bind(
&PrepareTransferFileFromLocalToRemote,
metadata_, local_src_path, remote_dest_path, gdoc_resource_id),
metadata_, local_src_path, remote_dest_path,
gdoc_resource_id, parent_resource_id),
base::Bind(
&CopyOperation::TransferFileFromLocalToRemoteAfterPrepare,
weak_ptr_factory_.GetWeakPtr(),
local_src_path, remote_dest_path, callback,
base::Owned(gdoc_resource_id)));
base::Owned(gdoc_resource_id), base::Owned(parent_resource_id)));
}
void CopyOperation::TransferFileFromLocalToRemoteAfterPrepare(
......@@ -302,6 +307,7 @@ void CopyOperation::TransferFileFromLocalToRemoteAfterPrepare(
const base::FilePath& remote_dest_path,
const FileOperationCallback& callback,
std::string* gdoc_resource_id,
std::string* parent_resource_id,
FileError error) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!callback.is_null());
......@@ -324,7 +330,16 @@ void CopyOperation::TransferFileFromLocalToRemoteAfterPrepare(
const std::string canonicalized_resource_id =
drive_service_->CanonicalizeResourceId(*gdoc_resource_id);
// TODO(hidehiko): Use CopyResource for Drive API v2.
// If Drive API v2 is enabled, we can copy resources on server side.
if (util::IsDriveV2ApiEnabled()) {
CopyResourceOnServer(
*gdoc_resource_id, *parent_resource_id,
// Drop the document extension, which should not be in the title.
// TODO(yoshiki): Remove this code with crbug.com/223304.
remote_dest_path.BaseName().RemoveExtension().AsUTF8Unsafe(),
callback);
return;
}
CopyHostedDocument(
canonicalized_resource_id,
......
......@@ -88,12 +88,14 @@ class CopyOperation {
scoped_ptr<ResourceEntry> entry);
// Part of TransferFileFromLocalToRemote(). Called after preparation is done.
// |gdoc_resource_id| is available only if the file is JSON GDoc file.
// |gdoc_resource_id| and |parent_resource_id| is available only if the file
// is JSON GDoc file.
void TransferFileFromLocalToRemoteAfterPrepare(
const base::FilePath& local_src_path,
const base::FilePath& remote_dest_path,
const FileOperationCallback& callback,
std::string* gdoc_resource_id,
std::string* parent_resource_id,
FileError error);
// Copies resource with |resource_id| into the directory |parent_resource_id|
......
......@@ -7,6 +7,7 @@
#include "base/file_util.h"
#include "chrome/browser/chromeos/drive/file_system/operation_test_base.h"
#include "chrome/browser/chromeos/drive/file_system_util.h"
#include "chrome/browser/drive/drive_api_util.h"
#include "chrome/browser/drive/fake_drive_service.h"
#include "chrome/browser/google_apis/test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
......@@ -137,11 +138,17 @@ TEST_F(CopyOperationTest, TransferFileFromLocalToRemote_HostedDocument) {
EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(remote_dest_path, &entry));
// We added a file to the Drive root and then moved to "Directory 1".
EXPECT_EQ(2U, observer()->get_changed_paths().size());
EXPECT_TRUE(observer()->get_changed_paths().count(
base::FilePath(FILE_PATH_LITERAL("drive/root"))));
EXPECT_TRUE(observer()->get_changed_paths().count(
remote_dest_path.DirName()));
if (util::IsDriveV2ApiEnabled()) {
EXPECT_EQ(1U, observer()->get_changed_paths().size());
EXPECT_TRUE(
observer()->get_changed_paths().count(remote_dest_path.DirName()));
} else {
EXPECT_EQ(2U, observer()->get_changed_paths().size());
EXPECT_TRUE(observer()->get_changed_paths().count(
base::FilePath(FILE_PATH_LITERAL("drive/root"))));
EXPECT_TRUE(observer()->get_changed_paths().count(
remote_dest_path.DirName()));
}
}
......
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