Skip to content

Commit

Permalink
drive: Make FileResource copyable
Browse files Browse the repository at this point in the history
To implement FakeDriveService without GData WAPI classes.

BUG=334544
TEST=unit_tests

Review URL: https://codereview.chromium.org/216433002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@260182 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
hashimoto@chromium.org committed Mar 28, 2014
1 parent f77f0ca commit fed98f8
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 80 deletions.
16 changes: 7 additions & 9 deletions chrome/browser/drive/drive_api_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -343,21 +343,20 @@ scoped_ptr<google_apis::FileResource> ConvertResourceEntryToFileResource(
image_media_metadata->set_height(entry.image_height());
image_media_metadata->set_rotation(entry.image_rotation());

ScopedVector<google_apis::ParentReference> parents;
std::vector<google_apis::ParentReference>* parents = file->mutable_parents();
for (size_t i = 0; i < entry.links().size(); ++i) {
using google_apis::Link;
const Link& link = *entry.links()[i];
switch (link.type()) {
case Link::LINK_PARENT: {
scoped_ptr<google_apis::ParentReference> parent(
new google_apis::ParentReference);
parent->set_parent_link(link.href());
google_apis::ParentReference parent;
parent.set_parent_link(link.href());

std::string file_id =
drive::util::ExtractResourceIdFromUrl(link.href());
parent->set_file_id(file_id);
parent->set_is_root(file_id == kWapiRootDirectoryResourceId);
parents.push_back(parent.release());
parent.set_file_id(file_id);
parent.set_is_root(file_id == kWapiRootDirectoryResourceId);
parents->push_back(parent);
break;
}
case Link::LINK_ALTERNATE:
Expand All @@ -367,7 +366,6 @@ scoped_ptr<google_apis::FileResource> ConvertResourceEntryToFileResource(
break;
}
}
file->set_parents(parents.Pass());

file->set_modified_date(entry.updated_time());
file->set_last_viewed_by_me_date(entry.last_viewed_time());
Expand Down Expand Up @@ -449,7 +447,7 @@ ConvertFileResourceToResourceEntry(
for (size_t i = 0; i < file_resource.parents().size(); ++i) {
google_apis::Link* link = new google_apis::Link;
link->set_type(google_apis::Link::LINK_PARENT);
link->set_href(file_resource.parents()[i]->parent_link());
link->set_href(file_resource.parents()[i].parent_link());
links.push_back(link);
}
if (!file_resource.alternate_link().is_empty()) {
Expand Down
8 changes: 3 additions & 5 deletions chrome/browser/drive/drive_api_util_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,11 @@ TEST(FileSystemUtilTest, ConvertFileResourceToResource_Parents) {
expected_links.push_back(GURL("http://server/id2"));
expected_links.push_back(GURL("http://server/id3"));

ScopedVector<google_apis::ParentReference> parents;
for (size_t i = 0; i < expected_links.size(); ++i) {
google_apis::ParentReference* parent = new google_apis::ParentReference;
parent->set_parent_link(expected_links[i]);
parents.push_back(parent);
google_apis::ParentReference parent;
parent.set_parent_link(expected_links[i]);
file_resource.mutable_parents()->push_back(parent);
}
file_resource.set_parents(parents.Pass());

scoped_ptr<google_apis::ResourceEntry> entry(
ConvertFileResourceToResourceEntry(file_resource));
Expand Down
66 changes: 31 additions & 35 deletions chrome/browser/drive/fake_drive_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -724,12 +724,8 @@ CancelCallback FakeDriveService::CopyResource(
scoped_ptr<EntryInfo> copied_entry(new EntryInfo);
copied_entry->content_data = entry->content_data;
copied_entry->share_url = entry->share_url;

// TODO(hashimoto): Implement a proper way to copy FileResource.
scoped_ptr<ResourceEntry> copied_resource_entry =
util::ConvertChangeResourceToResourceEntry(entry->change_resource);
copied_entry->change_resource.set_file(
util::ConvertResourceEntryToFileResource(*copied_resource_entry));
make_scoped_ptr(new FileResource(*entry->change_resource.file())));

ChangeResource* new_change = &copied_entry->change_resource;
FileResource* new_file = new_change->mutable_file();
Expand All @@ -738,13 +734,13 @@ CancelCallback FakeDriveService::CopyResource(
new_file->set_file_id(new_resource_id);
new_file->set_title(new_title);

scoped_ptr<ParentReference> parent(new ParentReference);
parent->set_file_id(parent_resource_id);
parent->set_parent_link(GetFakeLinkUrl(parent_resource_id));
parent->set_is_root(parent_resource_id == GetRootResourceId());
ScopedVector<ParentReference> parents;
parents.push_back(parent.release());
new_file->set_parents(parents.Pass());
ParentReference parent;
parent.set_file_id(parent_resource_id);
parent.set_parent_link(GetFakeLinkUrl(parent_resource_id));
parent.set_is_root(parent_resource_id == GetRootResourceId());
std::vector<ParentReference> parents;
parents.push_back(parent);
*new_file->mutable_parents() = parents;

if (!last_modified.is_null())
new_file->set_modified_date(last_modified);
Expand Down Expand Up @@ -797,14 +793,14 @@ CancelCallback FakeDriveService::UpdateResource(

// Set parent if necessary.
if (!parent_resource_id.empty()) {
scoped_ptr<ParentReference> parent(new ParentReference);
parent->set_file_id(parent_resource_id);
parent->set_parent_link(GetFakeLinkUrl(parent_resource_id));
parent->set_is_root(parent_resource_id == GetRootResourceId());

ScopedVector<ParentReference> parents;
parents.push_back(parent.release());
file->set_parents(parents.Pass());
ParentReference parent;
parent.set_file_id(parent_resource_id);
parent.set_parent_link(GetFakeLinkUrl(parent_resource_id));
parent.set_is_root(parent_resource_id == GetRootResourceId());

std::vector<ParentReference> parents;
parents.push_back(parent);
*file->mutable_parents() = parents;
}

if (!last_modified.is_null())
Expand Down Expand Up @@ -863,11 +859,11 @@ CancelCallback FakeDriveService::AddResourceToDirectory(
// structure. That is, each resource can have multiple parent.
// We mimic the behavior here; AddResourceToDirectoy just adds
// one more parent, not overwriting old ones.
scoped_ptr<ParentReference> parent(new ParentReference);
parent->set_file_id(parent_resource_id);
parent->set_parent_link(GetFakeLinkUrl(parent_resource_id));
parent->set_is_root(parent_resource_id == GetRootResourceId());
change->mutable_file()->mutable_parents()->push_back(parent.release());
ParentReference parent;
parent.set_file_id(parent_resource_id);
parent.set_parent_link(GetFakeLinkUrl(parent_resource_id));
parent.set_is_root(parent_resource_id == GetRootResourceId());
change->mutable_file()->mutable_parents()->push_back(parent);

AddNewChangestamp(change);
base::MessageLoop::current()->PostTask(
Expand Down Expand Up @@ -897,9 +893,9 @@ CancelCallback FakeDriveService::RemoveResourceFromDirectory(
if (entry) {
ChangeResource* change = &entry->change_resource;
FileResource* file = change->mutable_file();
ScopedVector<ParentReference>* parents = file->mutable_parents();
std::vector<ParentReference>* parents = file->mutable_parents();
for (size_t i = 0; i < parents->size(); ++i) {
if ((*parents)[i]->file_id() == parent_resource_id) {
if ((*parents)[i].file_id() == parent_resource_id) {
parents->erase(parents->begin() + i);
AddNewChangestamp(change);
base::MessageLoop::current()->PostTask(
Expand Down Expand Up @@ -1422,16 +1418,16 @@ const FakeDriveService::EntryInfo* FakeDriveService::AddNewEntry(
new_file->set_mime_type(content_type);

// Set parents.
scoped_ptr<ParentReference> parent(new ParentReference);
ParentReference parent;
if (parent_resource_id.empty())
parent->set_file_id(GetRootResourceId());
parent.set_file_id(GetRootResourceId());
else
parent->set_file_id(parent_resource_id);
parent->set_parent_link(GetFakeLinkUrl(parent->file_id()));
parent->set_is_root(parent->file_id() == GetRootResourceId());
ScopedVector<ParentReference> parents;
parents.push_back(parent.release());
new_file->set_parents(parents.Pass());
parent.set_file_id(parent_resource_id);
parent.set_parent_link(GetFakeLinkUrl(parent.file_id()));
parent.set_is_root(parent.file_id() == GetRootResourceId());
std::vector<ParentReference> parents;
parents.push_back(parent);
*new_file->mutable_parents() = parents;

new_entry->share_url = net::AppendOrReplaceQueryParameter(
share_url_base_, "name", title);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ void PopulateFileDetailsByFileResource(
const google_apis::FileResource& file_resource,
FileDetails* details) {
details->clear_parent_folder_ids();
for (ScopedVector<google_apis::ParentReference>::const_iterator itr =
for (std::vector<google_apis::ParentReference>::const_iterator itr =
file_resource.parents().begin();
itr != file_resource.parents().end();
++itr) {
details->add_parent_folder_ids((*itr)->file_id());
details->add_parent_folder_ids(itr->file_id());
}
details->set_title(file_resource.title());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,16 +376,13 @@ class MetadataDatabaseTest : public testing::Test {
const FileMetadata& file) {
scoped_ptr<google_apis::FileResource> file_resource(
new google_apis::FileResource);
ScopedVector<google_apis::ParentReference> parents;
for (int i = 0; i < file.details().parent_folder_ids_size(); ++i) {
scoped_ptr<google_apis::ParentReference> parent(
new google_apis::ParentReference);
parent->set_file_id(file.details().parent_folder_ids(i));
parents.push_back(parent.release());
google_apis::ParentReference parent;
parent.set_file_id(file.details().parent_folder_ids(i));
file_resource->mutable_parents()->push_back(parent);
}

file_resource->set_file_id(file.file_id());
file_resource->set_parents(parents.Pass());
file_resource->set_title(file.details().title());
if (file.details().file_kind() == FILE_KIND_FOLDER)
file_resource->set_mime_type("application/vnd.google-apps.folder");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class SyncEngineInitializerTest : public testing::Test {
for (size_t i = 0; i < sync_root->parents().size(); ++i) {
google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
fake_drive_service_.RemoveResourceFromDirectory(
sync_root->parents()[i]->file_id(),
sync_root->parents()[i].file_id(),
sync_root->file_id(),
CreateResultReceiver(&error));
base::RunLoop().RunUntilIdle();
Expand Down
28 changes: 26 additions & 2 deletions google_apis/drive/drive_api_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,28 @@ bool GetGURLFromString(const base::StringPiece& url_string, GURL* result) {
return true;
}

// Converts |value| to |result|.
bool GetParentsFromValue(const base::Value* value,
std::vector<ParentReference>* result) {
DCHECK(value);
DCHECK(result);

const base::ListValue* list_value = NULL;
if (!value->GetAsList(&list_value))
return false;

base::JSONValueConverter<ParentReference> converter;
result->resize(list_value->GetSize());
for (size_t i = 0; i < list_value->GetSize(); ++i) {
const base::Value* parent_value = NULL;
if (!list_value->Get(i, &parent_value) ||
!converter.Convert(*parent_value, &(*result)[i]))
return false;
}

return true;
}

// Converts |value| to |result|. The key of |value| is app_id, and its value
// is URL to open the resource on the web app.
bool GetOpenWithLinksFromDictionaryValue(
Expand Down Expand Up @@ -469,8 +491,10 @@ void FileResource::RegisterJSONConverter(
converter->RegisterCustomField<GURL>(kAlternateLink,
&FileResource::alternate_link_,
GetGURLFromString);
converter->RegisterRepeatedMessage<ParentReference>(kParents,
&FileResource::parents_);
converter->RegisterCustomValueField<std::vector<ParentReference> >(
kParents,
&FileResource::parents_,
GetParentsFromValue);
converter->RegisterCustomValueField<std::vector<OpenWithLink> >(
kOpenWithLinks,
&FileResource::open_with_links_,
Expand Down
20 changes: 5 additions & 15 deletions google_apis/drive/drive_api_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -365,17 +365,13 @@ class ParentReference {
void set_is_root(bool is_root) { is_root_ = is_root; }

private:
friend class base::internal::RepeatedMessageConverter<ParentReference>;

// Parses and initializes data members from content of |value|.
// Return false if parsing fails.
bool Parse(const base::Value& value);

std::string file_id_;
GURL parent_link_;
bool is_root_;

DISALLOW_COPY_AND_ASSIGN(ParentReference);
};

// FileLabels represents labels for file or folder.
Expand Down Expand Up @@ -422,8 +418,6 @@ class FileLabels {
bool trashed_;
bool restricted_;
bool viewed_;

DISALLOW_COPY_AND_ASSIGN(FileLabels);
};

// ImageMediaMetadata represents image metadata for a file.
Expand Down Expand Up @@ -462,8 +456,6 @@ class ImageMediaMetadata {
int width_;
int height_;
int rotation_;

DISALLOW_COPY_AND_ASSIGN(ImageMediaMetadata);
};


Expand Down Expand Up @@ -549,8 +541,7 @@ class FileResource {
const GURL& alternate_link() const { return alternate_link_; }

// Returns parent references (directories) of this file.
const ScopedVector<ParentReference>& parents() const { return parents_; }
ScopedVector<ParentReference>* mutable_parents() { return &parents_; }
const std::vector<ParentReference>& parents() const { return parents_; }

// Returns the list of links to open the resource with a web app.
const std::vector<OpenWithLink>& open_with_links() const {
Expand Down Expand Up @@ -605,8 +596,9 @@ class FileResource {
void set_alternate_link(const GURL& alternate_link) {
alternate_link_ = alternate_link;
}
void set_parents(ScopedVector<ParentReference> parents) {
parents_ = parents.Pass();
std::vector<ParentReference>* mutable_parents() { return &parents_; }
std::vector<OpenWithLink>* mutable_open_with_links() {
return &open_with_links_;
}

private:
Expand Down Expand Up @@ -634,10 +626,8 @@ class FileResource {
std::string md5_checksum_;
int64 file_size_;
GURL alternate_link_;
ScopedVector<ParentReference> parents_;
std::vector<ParentReference> parents_;
std::vector<OpenWithLink> open_with_links_;

DISALLOW_COPY_AND_ASSIGN(FileResource);
};

// FileList represents a collection of files and folders.
Expand Down
10 changes: 5 additions & 5 deletions google_apis/drive/drive_api_parser_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,11 @@ TEST(DriveAPIParserTest, FileListParser) {
EXPECT_EQ(modified_time, file1.modified_by_me_date());

ASSERT_EQ(1U, file1.parents().size());
EXPECT_EQ("0B4v7G8yEYAWHYW1OcExsUVZLABC", file1.parents()[0]->file_id());
EXPECT_EQ("0B4v7G8yEYAWHYW1OcExsUVZLABC", file1.parents()[0].file_id());
EXPECT_EQ(GURL("https://www.googleapis.com/drive/v2/files/"
"0B4v7G8yEYAWHYW1OcExsUVZLABC"),
file1.parents()[0]->parent_link());
EXPECT_FALSE(file1.parents()[0]->is_root());
file1.parents()[0].parent_link());
EXPECT_FALSE(file1.parents()[0].is_root());

EXPECT_EQ("ext", file1.file_extension());
EXPECT_EQ("d41d8cd98f00b204e9800998ecf8427e", file1.md5_checksum());
Expand Down Expand Up @@ -223,8 +223,8 @@ TEST(DriveAPIParserTest, FileListParser) {
EXPECT_FALSE(file3.shared());

ASSERT_EQ(1U, file3.parents().size());
EXPECT_EQ("0AIv7G8yEYAWHUk9ABC", file3.parents()[0]->file_id());
EXPECT_TRUE(file3.parents()[0]->is_root());
EXPECT_EQ("0AIv7G8yEYAWHUk9ABC", file3.parents()[0].file_id());
EXPECT_TRUE(file3.parents()[0].is_root());
EXPECT_EQ(0U, file3.open_with_links().size());
}

Expand Down

0 comments on commit fed98f8

Please sign in to comment.