| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/gdata/gdata_files.h" | 5 #include "chrome/browser/chromeos/gdata/gdata_files.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "base/platform_file.h" | 10 #include "base/platform_file.h" |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 } | 71 } |
| 72 | 72 |
| 73 GDataDirectory* GDataEntry::AsGDataDirectory() { | 73 GDataDirectory* GDataEntry::AsGDataDirectory() { |
| 74 return NULL; | 74 return NULL; |
| 75 } | 75 } |
| 76 | 76 |
| 77 GDataRootDirectory* GDataEntry::AsGDataRootDirectory() { | 77 GDataRootDirectory* GDataEntry::AsGDataRootDirectory() { |
| 78 return NULL; | 78 return NULL; |
| 79 } | 79 } |
| 80 | 80 |
| 81 FilePath GDataEntry::GetFilePath() { | 81 const GDataFile* GDataEntry::AsGDataFileConst() const { |
| 82 // cast away const and call the non-const version. This is safe. |
| 83 return const_cast<GDataEntry*>(this)->AsGDataFile(); |
| 84 } |
| 85 |
| 86 const GDataDirectory* GDataEntry::AsGDataDirectoryConst() const { |
| 87 // cast away const and call the non-const version. This is safe. |
| 88 return const_cast<GDataEntry*>(this)->AsGDataDirectory(); |
| 89 } |
| 90 |
| 91 FilePath GDataEntry::GetFilePath() const { |
| 82 FilePath path; | 92 FilePath path; |
| 83 std::vector<FilePath::StringType> parts; | 93 std::vector<FilePath::StringType> parts; |
| 84 for (GDataEntry* entry = this; entry != NULL; entry = entry->parent()) | 94 for (const GDataEntry* entry = this; entry != NULL; entry = entry->parent_) |
| 85 parts.push_back(entry->file_name()); | 95 parts.push_back(entry->file_name()); |
| 86 | 96 |
| 87 // Paste paths parts back together in reverse order from upward tree | 97 // Paste paths parts back together in reverse order from upward tree |
| 88 // traversal. | 98 // traversal. |
| 89 for (std::vector<FilePath::StringType>::reverse_iterator iter = | 99 for (std::vector<FilePath::StringType>::reverse_iterator iter = |
| 90 parts.rbegin(); | 100 parts.rbegin(); |
| 91 iter != parts.rend(); ++iter) { | 101 iter != parts.rend(); ++iter) { |
| 92 path = path.Append(*iter); | 102 path = path.Append(*iter); |
| 93 } | 103 } |
| 94 return path; | 104 return path; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 ReplaceSubstringsAfterOffset(&output, 0, std::string(kEscapedSlash), kSlash); | 136 ReplaceSubstringsAfterOffset(&output, 0, std::string(kEscapedSlash), kSlash); |
| 127 return output; | 137 return output; |
| 128 } | 138 } |
| 129 | 139 |
| 130 // GDataFile class implementation. | 140 // GDataFile class implementation. |
| 131 | 141 |
| 132 GDataFile::GDataFile(GDataDirectory* parent, GDataRootDirectory* root) | 142 GDataFile::GDataFile(GDataDirectory* parent, GDataRootDirectory* root) |
| 133 : GDataEntry(parent, root), | 143 : GDataEntry(parent, root), |
| 134 kind_(gdata::DocumentEntry::UNKNOWN), | 144 kind_(gdata::DocumentEntry::UNKNOWN), |
| 135 is_hosted_document_(false) { | 145 is_hosted_document_(false) { |
| 146 file_info_.is_directory = false; |
| 136 } | 147 } |
| 137 | 148 |
| 138 GDataFile::~GDataFile() { | 149 GDataFile::~GDataFile() { |
| 139 } | 150 } |
| 140 | 151 |
| 141 GDataFile* GDataFile::AsGDataFile() { | 152 GDataFile* GDataFile::AsGDataFile() { |
| 142 return this; | 153 return this; |
| 143 } | 154 } |
| 144 | 155 |
| 145 void GDataFile::SetFileNameFromTitle() { | 156 void GDataFile::SetFileNameFromTitle() { |
| (...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 574 file_info_.creation_time.ToInternalValue()); | 585 file_info_.creation_time.ToInternalValue()); |
| 575 | 586 |
| 576 proto->set_file_name(file_name_); | 587 proto->set_file_name(file_name_); |
| 577 proto->set_title(title_); | 588 proto->set_title(title_); |
| 578 proto->set_resource_id(resource_id_); | 589 proto->set_resource_id(resource_id_); |
| 579 proto->set_edit_url(edit_url_.spec()); | 590 proto->set_edit_url(edit_url_.spec()); |
| 580 proto->set_content_url(content_url_.spec()); | 591 proto->set_content_url(content_url_.spec()); |
| 581 } | 592 } |
| 582 | 593 |
| 583 void GDataFile::FromProto(const GDataFileProto& proto) { | 594 void GDataFile::FromProto(const GDataFileProto& proto) { |
| 595 DCHECK(!proto.gdata_entry().file_info().is_directory()); |
| 584 GDataEntry::FromProto(proto.gdata_entry()); | 596 GDataEntry::FromProto(proto.gdata_entry()); |
| 585 kind_ = DocumentEntry::EntryKind(proto.kind()); | 597 kind_ = DocumentEntry::EntryKind(proto.kind()); |
| 586 thumbnail_url_ = GURL(proto.thumbnail_url()); | 598 thumbnail_url_ = GURL(proto.thumbnail_url()); |
| 587 alternate_url_ = GURL(proto.alternate_url()); | 599 alternate_url_ = GURL(proto.alternate_url()); |
| 588 content_mime_type_ = proto.content_mime_type(); | 600 content_mime_type_ = proto.content_mime_type(); |
| 589 etag_ = proto.etag(); | 601 etag_ = proto.etag(); |
| 590 id_ = proto.id(); | 602 id_ = proto.id(); |
| 591 file_md5_ = proto.file_md5(); | 603 file_md5_ = proto.file_md5(); |
| 592 document_extension_ = proto.document_extension(); | 604 document_extension_ = proto.document_extension(); |
| 593 is_hosted_document_ = proto.is_hosted_document(); | 605 is_hosted_document_ = proto.is_hosted_document(); |
| 594 } | 606 } |
| 595 | 607 |
| 596 void GDataFile::ToProto(GDataFileProto* proto) const { | 608 void GDataFile::ToProto(GDataFileProto* proto) const { |
| 597 GDataEntry::ToProto(proto->mutable_gdata_entry()); | 609 GDataEntry::ToProto(proto->mutable_gdata_entry()); |
| 610 DCHECK(!proto->gdata_entry().file_info().is_directory()); |
| 598 proto->set_kind(kind_); | 611 proto->set_kind(kind_); |
| 599 proto->set_thumbnail_url(thumbnail_url_.spec()); | 612 proto->set_thumbnail_url(thumbnail_url_.spec()); |
| 600 proto->set_alternate_url(alternate_url_.spec()); | 613 proto->set_alternate_url(alternate_url_.spec()); |
| 601 proto->set_content_mime_type(content_mime_type_); | 614 proto->set_content_mime_type(content_mime_type_); |
| 602 proto->set_etag(etag_); | 615 proto->set_etag(etag_); |
| 603 proto->set_id(id_); | 616 proto->set_id(id_); |
| 604 proto->set_file_md5(file_md5_); | 617 proto->set_file_md5(file_md5_); |
| 605 proto->set_document_extension(document_extension_); | 618 proto->set_document_extension(document_extension_); |
| 606 proto->set_is_hosted_document(is_hosted_document_); | 619 proto->set_is_hosted_document(is_hosted_document_); |
| 607 } | 620 } |
| 608 | 621 |
| 609 void GDataDirectory::FromProto(const GDataDirectoryProto& proto) { | 622 void GDataDirectory::FromProto(const GDataDirectoryProto& proto) { |
| 623 DCHECK(proto.gdata_entry().file_info().is_directory()); |
| 610 GDataEntry::FromProto(proto.gdata_entry()); | 624 GDataEntry::FromProto(proto.gdata_entry()); |
| 611 refresh_time_ = base::Time::FromInternalValue(proto.refresh_time()); | 625 refresh_time_ = base::Time::FromInternalValue(proto.refresh_time()); |
| 612 start_feed_url_ = GURL(proto.start_feed_url()); | 626 start_feed_url_ = GURL(proto.start_feed_url()); |
| 613 next_feed_url_ = GURL(proto.next_feed_url()); | 627 next_feed_url_ = GURL(proto.next_feed_url()); |
| 614 upload_url_ = GURL(proto.upload_url()); | 628 upload_url_ = GURL(proto.upload_url()); |
| 615 origin_ = ContentOrigin(proto.origin()); | 629 origin_ = ContentOrigin(proto.origin()); |
| 616 for (int i = 0; i < proto.child_files_size(); ++i) { | 630 for (int i = 0; i < proto.child_files_size(); ++i) { |
| 617 scoped_ptr<GDataFile> file(new GDataFile(this, root_)); | 631 scoped_ptr<GDataFile> file(new GDataFile(this, root_)); |
| 618 file->FromProto(proto.child_files(i)); | 632 file->FromProto(proto.child_files(i)); |
| 619 AddEntry(file.release()); | 633 AddEntry(file.release()); |
| 620 } | 634 } |
| 621 for (int i = 0; i < proto.child_directories_size(); ++i) { | 635 for (int i = 0; i < proto.child_directories_size(); ++i) { |
| 622 scoped_ptr<GDataDirectory> dir(new GDataDirectory(this, root_)); | 636 scoped_ptr<GDataDirectory> dir(new GDataDirectory(this, root_)); |
| 623 dir->FromProto(proto.child_directories(i)); | 637 dir->FromProto(proto.child_directories(i)); |
| 624 AddEntry(dir.release()); | 638 AddEntry(dir.release()); |
| 625 } | 639 } |
| 626 } | 640 } |
| 627 | 641 |
| 628 void GDataDirectory::ToProto(GDataDirectoryProto* proto) const { | 642 void GDataDirectory::ToProto(GDataDirectoryProto* proto) const { |
| 629 GDataEntry::ToProto(proto->mutable_gdata_entry()); | 643 GDataEntry::ToProto(proto->mutable_gdata_entry()); |
| 644 DCHECK(proto->gdata_entry().file_info().is_directory()); |
| 630 proto->set_refresh_time(refresh_time_.ToInternalValue()); | 645 proto->set_refresh_time(refresh_time_.ToInternalValue()); |
| 631 proto->set_start_feed_url(start_feed_url_.spec()); | 646 proto->set_start_feed_url(start_feed_url_.spec()); |
| 632 proto->set_next_feed_url(next_feed_url_.spec()); | 647 proto->set_next_feed_url(next_feed_url_.spec()); |
| 633 proto->set_upload_url(upload_url_.spec()); | 648 proto->set_upload_url(upload_url_.spec()); |
| 634 proto->set_origin(origin_); | 649 proto->set_origin(origin_); |
| 635 for (GDataFileCollection::const_iterator iter = children_.begin(); | 650 for (GDataFileCollection::const_iterator iter = children_.begin(); |
| 636 iter != children_.end(); ++iter) { | 651 iter != children_.end(); ++iter) { |
| 637 GDataFile* file = iter->second->AsGDataFile(); | 652 GDataFile* file = iter->second->AsGDataFile(); |
| 638 GDataDirectory* dir = iter->second->AsGDataDirectory(); | 653 GDataDirectory* dir = iter->second->AsGDataDirectory(); |
| 639 if (file) | 654 if (file) |
| 640 file->ToProto(proto->add_child_files()); | 655 file->ToProto(proto->add_child_files()); |
| 641 if (dir) | 656 if (dir) |
| 642 dir->ToProto(proto->add_child_directories()); | 657 dir->ToProto(proto->add_child_directories()); |
| 643 } | 658 } |
| 644 } | 659 } |
| 645 | 660 |
| 646 void GDataRootDirectory::FromProto(const GDataRootDirectoryProto& proto) { | 661 void GDataRootDirectory::FromProto(const GDataRootDirectoryProto& proto) { |
| 647 root_ = this; | 662 root_ = this; |
| 648 GDataDirectory::FromProto(proto.gdata_directory()); | 663 GDataDirectory::FromProto(proto.gdata_directory()); |
| 649 largest_changestamp_ = proto.largest_changestamp(); | 664 largest_changestamp_ = proto.largest_changestamp(); |
| 650 } | 665 } |
| 651 | 666 |
| 652 void GDataRootDirectory::ToProto(GDataRootDirectoryProto* proto) const { | 667 void GDataRootDirectory::ToProto(GDataRootDirectoryProto* proto) const { |
| 653 GDataDirectory::ToProto(proto->mutable_gdata_directory()); | 668 GDataDirectory::ToProto(proto->mutable_gdata_directory()); |
| 654 proto->set_largest_changestamp(largest_changestamp_); | 669 proto->set_largest_changestamp(largest_changestamp_); |
| 655 } | 670 } |
| 656 | 671 |
| 657 void GDataRootDirectory::SerializeToString(std::string* proto_string) const { | 672 void GDataEntry::SerializeToString(std::string* serialized_proto) const { |
| 673 const GDataFile* file = AsGDataFileConst(); |
| 674 const GDataDirectory* dir = AsGDataDirectoryConst(); |
| 675 |
| 676 if (file) { |
| 677 scoped_ptr<GDataFileProto> proto(new GDataFileProto()); |
| 678 file->ToProto(proto.get()); |
| 679 const bool ok = proto->SerializeToString(serialized_proto); |
| 680 DCHECK(ok); |
| 681 } else if (dir) { |
| 682 scoped_ptr<GDataDirectoryProto> proto(new GDataDirectoryProto()); |
| 683 dir->ToProto(proto.get()); |
| 684 const bool ok = proto->SerializeToString(serialized_proto); |
| 685 DCHECK(ok); |
| 686 } |
| 687 } |
| 688 |
| 689 // static |
| 690 scoped_ptr<GDataEntry> GDataEntry::FromProtoString( |
| 691 const std::string& serialized_proto) { |
| 692 // First try to parse as GDataDirectoryProto. Note that this can succeed for |
| 693 // a serialized_proto that's really a GDataFileProto - we have to check |
| 694 // is_directory to be sure. |
| 695 scoped_ptr<GDataDirectoryProto> dir_proto(new GDataDirectoryProto()); |
| 696 bool ok = dir_proto->ParseFromString(serialized_proto); |
| 697 if (ok && dir_proto->gdata_entry().file_info().is_directory()) { |
| 698 GDataDirectory* dir = new GDataDirectory(NULL, NULL); |
| 699 dir->FromProto(*dir_proto); |
| 700 return scoped_ptr<GDataEntry>(dir); |
| 701 } |
| 702 |
| 703 scoped_ptr<GDataFileProto> file_proto(new GDataFileProto()); |
| 704 ok = file_proto->ParseFromString(serialized_proto); |
| 705 if (ok) { |
| 706 DCHECK(!file_proto->gdata_entry().file_info().is_directory()); |
| 707 GDataFile* file = new GDataFile(NULL, NULL); |
| 708 file->FromProto(*file_proto); |
| 709 return scoped_ptr<GDataEntry>(file); |
| 710 } |
| 711 return scoped_ptr<GDataEntry>(NULL); |
| 712 } |
| 713 |
| 714 void GDataRootDirectory::SerializeToString( |
| 715 std::string* serialized_proto) const { |
| 658 scoped_ptr<GDataRootDirectoryProto> proto( | 716 scoped_ptr<GDataRootDirectoryProto> proto( |
| 659 new GDataRootDirectoryProto()); | 717 new GDataRootDirectoryProto()); |
| 660 ToProto(proto.get()); | 718 ToProto(proto.get()); |
| 661 const bool ok = proto->SerializeToString(proto_string); | 719 const bool ok = proto->SerializeToString(serialized_proto); |
| 662 DCHECK(ok); | 720 DCHECK(ok); |
| 663 } | 721 } |
| 664 | 722 |
| 665 bool GDataRootDirectory::ParseFromString(const std::string& proto_string) { | 723 bool GDataRootDirectory::ParseFromString(const std::string& serialized_proto) { |
| 666 scoped_ptr<GDataRootDirectoryProto> proto( | 724 scoped_ptr<GDataRootDirectoryProto> proto( |
| 667 new GDataRootDirectoryProto()); | 725 new GDataRootDirectoryProto()); |
| 668 bool ok = proto->ParseFromString(proto_string); | 726 bool ok = proto->ParseFromString(serialized_proto); |
| 669 if (ok) { | 727 if (ok) { |
| 670 FromProto(*proto.get()); | 728 FromProto(*proto.get()); |
| 671 set_origin(FROM_CACHE); | 729 set_origin(FROM_CACHE); |
| 672 set_refresh_time(base::Time::Now()); | 730 set_refresh_time(base::Time::Now()); |
| 673 } | 731 } |
| 674 return ok; | 732 return ok; |
| 675 } | 733 } |
| 676 | 734 |
| 677 } // namespace gdata | 735 } // namespace gdata |
| OLD | NEW |