| Index: chrome/browser/chromeos/gdata/gdata_files.cc
|
| ===================================================================
|
| --- chrome/browser/chromeos/gdata/gdata_files.cc (revision 133648)
|
| +++ chrome/browser/chromeos/gdata/gdata_files.cc (working copy)
|
| @@ -10,6 +10,7 @@
|
| #include "base/platform_file.h"
|
| #include "base/stringprintf.h"
|
| #include "base/string_util.h"
|
| +#include "chrome/browser/chromeos/gdata/find_entry_delegate.h"
|
| #include "chrome/browser/chromeos/gdata/gdata.pb.h"
|
| #include "chrome/browser/chromeos/gdata/gdata_parser.h"
|
| #include "net/base/escape.h"
|
| @@ -78,10 +79,20 @@
|
| return NULL;
|
| }
|
|
|
| -FilePath GDataEntry::GetFilePath() {
|
| +const GDataFile* GDataEntry::AsGDataFileConst() const {
|
| + // cast away const and call the non-const version. This is safe.
|
| + return const_cast<GDataEntry*>(this)->AsGDataFile();
|
| +}
|
| +
|
| +const GDataDirectory* GDataEntry::AsGDataDirectoryConst() const {
|
| + // cast away const and call the non-const version. This is safe.
|
| + return const_cast<GDataEntry*>(this)->AsGDataDirectory();
|
| +}
|
| +
|
| +FilePath GDataEntry::GetFilePath() const {
|
| FilePath path;
|
| std::vector<FilePath::StringType> parts;
|
| - for (GDataEntry* entry = this; entry != NULL; entry = entry->parent())
|
| + for (const GDataEntry* entry = this; entry != NULL; entry = entry->parent_)
|
| parts.push_back(entry->file_name());
|
|
|
| // Paste paths parts back together in reverse order from upward tree
|
| @@ -133,6 +144,7 @@
|
| : GDataEntry(parent, root),
|
| kind_(gdata::DocumentEntry::UNKNOWN),
|
| is_hosted_document_(false) {
|
| + file_info_.is_directory = false;
|
| }
|
|
|
| GDataFile::~GDataFile() {
|
| @@ -409,6 +421,55 @@
|
| return this;
|
| }
|
|
|
| +void GDataRootDirectory::FindEntryByPath(const FilePath& file_path,
|
| + FindEntryDelegate* delegate) {
|
| + // GDataFileSystem has already locked.
|
| + DCHECK(delegate);
|
| + std::vector<FilePath::StringType> components;
|
| + file_path.GetComponents(&components);
|
| +
|
| + GDataDirectory* current_dir = this;
|
| + FilePath directory_path;
|
| + for (size_t i = 0; i < components.size() && current_dir; i++) {
|
| + directory_path = directory_path.Append(current_dir->file_name());
|
| +
|
| + // Last element must match, if not last then it must be a directory.
|
| + if (i == components.size() - 1) {
|
| + if (current_dir->file_name() == components[i])
|
| + delegate->OnDone(base::PLATFORM_FILE_OK, directory_path, current_dir);
|
| + else
|
| + delegate->OnDone(base::PLATFORM_FILE_ERROR_NOT_FOUND, FilePath(), NULL);
|
| +
|
| + return;
|
| + }
|
| +
|
| + // Not the last part of the path, search for the next segment.
|
| + GDataFileCollection::const_iterator file_iter =
|
| + current_dir->children().find(components[i + 1]);
|
| + if (file_iter == current_dir->children().end()) {
|
| + delegate->OnDone(base::PLATFORM_FILE_ERROR_NOT_FOUND, FilePath(), NULL);
|
| + return;
|
| + }
|
| +
|
| + // Found file, must be the last segment.
|
| + if (file_iter->second->file_info().is_directory) {
|
| + // Found directory, continue traversal.
|
| + current_dir = file_iter->second->AsGDataDirectory();
|
| + } else {
|
| + if ((i + 1) == (components.size() - 1)) {
|
| + delegate->OnDone(base::PLATFORM_FILE_OK,
|
| + directory_path,
|
| + file_iter->second);
|
| + } else {
|
| + delegate->OnDone(base::PLATFORM_FILE_ERROR_NOT_FOUND, FilePath(), NULL);
|
| + }
|
| +
|
| + return;
|
| + }
|
| + }
|
| + delegate->OnDone(base::PLATFORM_FILE_ERROR_NOT_FOUND, FilePath(), NULL);
|
| +}
|
| +
|
| void GDataRootDirectory::AddEntryToResourceMap(GDataEntry* entry) {
|
| // GDataFileSystem has already locked.
|
| resource_map_.insert(std::make_pair(entry->resource_id(), entry));
|
| @@ -581,6 +642,7 @@
|
| }
|
|
|
| void GDataFile::FromProto(const GDataFileProto& proto) {
|
| + DCHECK(!proto.gdata_entry().file_info().is_directory());
|
| GDataEntry::FromProto(proto.gdata_entry());
|
| kind_ = DocumentEntry::EntryKind(proto.kind());
|
| thumbnail_url_ = GURL(proto.thumbnail_url());
|
| @@ -595,6 +657,7 @@
|
|
|
| void GDataFile::ToProto(GDataFileProto* proto) const {
|
| GDataEntry::ToProto(proto->mutable_gdata_entry());
|
| + DCHECK(!proto->gdata_entry().file_info().is_directory());
|
| proto->set_kind(kind_);
|
| proto->set_thumbnail_url(thumbnail_url_.spec());
|
| proto->set_alternate_url(alternate_url_.spec());
|
| @@ -607,6 +670,7 @@
|
| }
|
|
|
| void GDataDirectory::FromProto(const GDataDirectoryProto& proto) {
|
| + DCHECK(proto.gdata_entry().file_info().is_directory());
|
| GDataEntry::FromProto(proto.gdata_entry());
|
| refresh_time_ = base::Time::FromInternalValue(proto.refresh_time());
|
| start_feed_url_ = GURL(proto.start_feed_url());
|
| @@ -627,6 +691,7 @@
|
|
|
| void GDataDirectory::ToProto(GDataDirectoryProto* proto) const {
|
| GDataEntry::ToProto(proto->mutable_gdata_entry());
|
| + DCHECK(proto->gdata_entry().file_info().is_directory());
|
| proto->set_refresh_time(refresh_time_.ToInternalValue());
|
| proto->set_start_feed_url(start_feed_url_.spec());
|
| proto->set_next_feed_url(next_feed_url_.spec());
|
| @@ -654,18 +719,61 @@
|
| proto->set_largest_changestamp(largest_changestamp_);
|
| }
|
|
|
| -void GDataRootDirectory::SerializeToString(std::string* proto_string) const {
|
| +void GDataEntry::SerializeToString(std::string* serialized_proto) const {
|
| + const GDataFile* file = AsGDataFileConst();
|
| + const GDataDirectory* dir = AsGDataDirectoryConst();
|
| +
|
| + if (file) {
|
| + scoped_ptr<GDataFileProto> proto(new GDataFileProto());
|
| + file->ToProto(proto.get());
|
| + const bool ok = proto->SerializeToString(serialized_proto);
|
| + DCHECK(ok);
|
| + } else if (dir) {
|
| + scoped_ptr<GDataDirectoryProto> proto(new GDataDirectoryProto());
|
| + dir->ToProto(proto.get());
|
| + const bool ok = proto->SerializeToString(serialized_proto);
|
| + DCHECK(ok);
|
| + }
|
| +}
|
| +
|
| +// static
|
| +scoped_ptr<GDataEntry> GDataEntry::FromProtoString(
|
| + const std::string& serialized_proto) {
|
| + // First try to parse as GDataDirectoryProto. Note that this can succeed for
|
| + // a serialized_proto that's really a GDataFileProto - we have to check
|
| + // is_directory to be sure.
|
| + scoped_ptr<GDataDirectoryProto> dir_proto(new GDataDirectoryProto());
|
| + bool ok = dir_proto->ParseFromString(serialized_proto);
|
| + if (ok && dir_proto->gdata_entry().file_info().is_directory()) {
|
| + GDataDirectory* dir = new GDataDirectory(NULL, NULL);
|
| + dir->FromProto(*dir_proto);
|
| + return scoped_ptr<GDataEntry>(dir);
|
| + }
|
| +
|
| + scoped_ptr<GDataFileProto> file_proto(new GDataFileProto());
|
| + ok = file_proto->ParseFromString(serialized_proto);
|
| + if (ok) {
|
| + DCHECK(!file_proto->gdata_entry().file_info().is_directory());
|
| + GDataFile* file = new GDataFile(NULL, NULL);
|
| + file->FromProto(*file_proto);
|
| + return scoped_ptr<GDataEntry>(file);
|
| + }
|
| + return scoped_ptr<GDataEntry>(NULL);
|
| +}
|
| +
|
| +void GDataRootDirectory::SerializeToString(
|
| + std::string* serialized_proto) const {
|
| scoped_ptr<GDataRootDirectoryProto> proto(
|
| new GDataRootDirectoryProto());
|
| ToProto(proto.get());
|
| - const bool ok = proto->SerializeToString(proto_string);
|
| + const bool ok = proto->SerializeToString(serialized_proto);
|
| DCHECK(ok);
|
| }
|
|
|
| -bool GDataRootDirectory::ParseFromString(const std::string& proto_string) {
|
| +bool GDataRootDirectory::ParseFromString(const std::string& serialized_proto) {
|
| scoped_ptr<GDataRootDirectoryProto> proto(
|
| new GDataRootDirectoryProto());
|
| - bool ok = proto->ParseFromString(proto_string);
|
| + bool ok = proto->ParseFromString(serialized_proto);
|
| if (ok) {
|
| FromProto(*proto.get());
|
| set_origin(FROM_CACHE);
|
|
|