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 const GDataFile* GDataEntry::AsGDataFileConst() const { | 81 FilePath GDataEntry::GetFilePath() { |
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 { | |
92 FilePath path; | 82 FilePath path; |
93 std::vector<FilePath::StringType> parts; | 83 std::vector<FilePath::StringType> parts; |
94 for (const GDataEntry* entry = this; entry != NULL; entry = entry->parent_) | 84 for (GDataEntry* entry = this; entry != NULL; entry = entry->parent()) |
95 parts.push_back(entry->file_name()); | 85 parts.push_back(entry->file_name()); |
96 | 86 |
97 // Paste paths parts back together in reverse order from upward tree | 87 // Paste paths parts back together in reverse order from upward tree |
98 // traversal. | 88 // traversal. |
99 for (std::vector<FilePath::StringType>::reverse_iterator iter = | 89 for (std::vector<FilePath::StringType>::reverse_iterator iter = |
100 parts.rbegin(); | 90 parts.rbegin(); |
101 iter != parts.rend(); ++iter) { | 91 iter != parts.rend(); ++iter) { |
102 path = path.Append(*iter); | 92 path = path.Append(*iter); |
103 } | 93 } |
104 return path; | 94 return path; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 ReplaceSubstringsAfterOffset(&output, 0, std::string(kEscapedSlash), kSlash); | 126 ReplaceSubstringsAfterOffset(&output, 0, std::string(kEscapedSlash), kSlash); |
137 return output; | 127 return output; |
138 } | 128 } |
139 | 129 |
140 // GDataFile class implementation. | 130 // GDataFile class implementation. |
141 | 131 |
142 GDataFile::GDataFile(GDataDirectory* parent, GDataRootDirectory* root) | 132 GDataFile::GDataFile(GDataDirectory* parent, GDataRootDirectory* root) |
143 : GDataEntry(parent, root), | 133 : GDataEntry(parent, root), |
144 kind_(gdata::DocumentEntry::UNKNOWN), | 134 kind_(gdata::DocumentEntry::UNKNOWN), |
145 is_hosted_document_(false) { | 135 is_hosted_document_(false) { |
146 file_info_.is_directory = false; | |
147 } | 136 } |
148 | 137 |
149 GDataFile::~GDataFile() { | 138 GDataFile::~GDataFile() { |
150 } | 139 } |
151 | 140 |
152 GDataFile* GDataFile::AsGDataFile() { | 141 GDataFile* GDataFile::AsGDataFile() { |
153 return this; | 142 return this; |
154 } | 143 } |
155 | 144 |
156 void GDataFile::SetFileNameFromTitle() { | 145 void GDataFile::SetFileNameFromTitle() { |
(...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
585 file_info_.creation_time.ToInternalValue()); | 574 file_info_.creation_time.ToInternalValue()); |
586 | 575 |
587 proto->set_file_name(file_name_); | 576 proto->set_file_name(file_name_); |
588 proto->set_title(title_); | 577 proto->set_title(title_); |
589 proto->set_resource_id(resource_id_); | 578 proto->set_resource_id(resource_id_); |
590 proto->set_edit_url(edit_url_.spec()); | 579 proto->set_edit_url(edit_url_.spec()); |
591 proto->set_content_url(content_url_.spec()); | 580 proto->set_content_url(content_url_.spec()); |
592 } | 581 } |
593 | 582 |
594 void GDataFile::FromProto(const GDataFileProto& proto) { | 583 void GDataFile::FromProto(const GDataFileProto& proto) { |
595 DCHECK(!proto.gdata_entry().file_info().is_directory()); | |
596 GDataEntry::FromProto(proto.gdata_entry()); | 584 GDataEntry::FromProto(proto.gdata_entry()); |
597 kind_ = DocumentEntry::EntryKind(proto.kind()); | 585 kind_ = DocumentEntry::EntryKind(proto.kind()); |
598 thumbnail_url_ = GURL(proto.thumbnail_url()); | 586 thumbnail_url_ = GURL(proto.thumbnail_url()); |
599 alternate_url_ = GURL(proto.alternate_url()); | 587 alternate_url_ = GURL(proto.alternate_url()); |
600 content_mime_type_ = proto.content_mime_type(); | 588 content_mime_type_ = proto.content_mime_type(); |
601 etag_ = proto.etag(); | 589 etag_ = proto.etag(); |
602 id_ = proto.id(); | 590 id_ = proto.id(); |
603 file_md5_ = proto.file_md5(); | 591 file_md5_ = proto.file_md5(); |
604 document_extension_ = proto.document_extension(); | 592 document_extension_ = proto.document_extension(); |
605 is_hosted_document_ = proto.is_hosted_document(); | 593 is_hosted_document_ = proto.is_hosted_document(); |
606 } | 594 } |
607 | 595 |
608 void GDataFile::ToProto(GDataFileProto* proto) const { | 596 void GDataFile::ToProto(GDataFileProto* proto) const { |
609 GDataEntry::ToProto(proto->mutable_gdata_entry()); | 597 GDataEntry::ToProto(proto->mutable_gdata_entry()); |
610 DCHECK(!proto->gdata_entry().file_info().is_directory()); | |
611 proto->set_kind(kind_); | 598 proto->set_kind(kind_); |
612 proto->set_thumbnail_url(thumbnail_url_.spec()); | 599 proto->set_thumbnail_url(thumbnail_url_.spec()); |
613 proto->set_alternate_url(alternate_url_.spec()); | 600 proto->set_alternate_url(alternate_url_.spec()); |
614 proto->set_content_mime_type(content_mime_type_); | 601 proto->set_content_mime_type(content_mime_type_); |
615 proto->set_etag(etag_); | 602 proto->set_etag(etag_); |
616 proto->set_id(id_); | 603 proto->set_id(id_); |
617 proto->set_file_md5(file_md5_); | 604 proto->set_file_md5(file_md5_); |
618 proto->set_document_extension(document_extension_); | 605 proto->set_document_extension(document_extension_); |
619 proto->set_is_hosted_document(is_hosted_document_); | 606 proto->set_is_hosted_document(is_hosted_document_); |
620 } | 607 } |
621 | 608 |
622 void GDataDirectory::FromProto(const GDataDirectoryProto& proto) { | 609 void GDataDirectory::FromProto(const GDataDirectoryProto& proto) { |
623 DCHECK(proto.gdata_entry().file_info().is_directory()); | |
624 GDataEntry::FromProto(proto.gdata_entry()); | 610 GDataEntry::FromProto(proto.gdata_entry()); |
625 refresh_time_ = base::Time::FromInternalValue(proto.refresh_time()); | 611 refresh_time_ = base::Time::FromInternalValue(proto.refresh_time()); |
626 start_feed_url_ = GURL(proto.start_feed_url()); | 612 start_feed_url_ = GURL(proto.start_feed_url()); |
627 next_feed_url_ = GURL(proto.next_feed_url()); | 613 next_feed_url_ = GURL(proto.next_feed_url()); |
628 upload_url_ = GURL(proto.upload_url()); | 614 upload_url_ = GURL(proto.upload_url()); |
629 origin_ = ContentOrigin(proto.origin()); | 615 origin_ = ContentOrigin(proto.origin()); |
630 for (int i = 0; i < proto.child_files_size(); ++i) { | 616 for (int i = 0; i < proto.child_files_size(); ++i) { |
631 scoped_ptr<GDataFile> file(new GDataFile(this, root_)); | 617 scoped_ptr<GDataFile> file(new GDataFile(this, root_)); |
632 file->FromProto(proto.child_files(i)); | 618 file->FromProto(proto.child_files(i)); |
633 AddEntry(file.release()); | 619 AddEntry(file.release()); |
634 } | 620 } |
635 for (int i = 0; i < proto.child_directories_size(); ++i) { | 621 for (int i = 0; i < proto.child_directories_size(); ++i) { |
636 scoped_ptr<GDataDirectory> dir(new GDataDirectory(this, root_)); | 622 scoped_ptr<GDataDirectory> dir(new GDataDirectory(this, root_)); |
637 dir->FromProto(proto.child_directories(i)); | 623 dir->FromProto(proto.child_directories(i)); |
638 AddEntry(dir.release()); | 624 AddEntry(dir.release()); |
639 } | 625 } |
640 } | 626 } |
641 | 627 |
642 void GDataDirectory::ToProto(GDataDirectoryProto* proto) const { | 628 void GDataDirectory::ToProto(GDataDirectoryProto* proto) const { |
643 GDataEntry::ToProto(proto->mutable_gdata_entry()); | 629 GDataEntry::ToProto(proto->mutable_gdata_entry()); |
644 DCHECK(proto->gdata_entry().file_info().is_directory()); | |
645 proto->set_refresh_time(refresh_time_.ToInternalValue()); | 630 proto->set_refresh_time(refresh_time_.ToInternalValue()); |
646 proto->set_start_feed_url(start_feed_url_.spec()); | 631 proto->set_start_feed_url(start_feed_url_.spec()); |
647 proto->set_next_feed_url(next_feed_url_.spec()); | 632 proto->set_next_feed_url(next_feed_url_.spec()); |
648 proto->set_upload_url(upload_url_.spec()); | 633 proto->set_upload_url(upload_url_.spec()); |
649 proto->set_origin(origin_); | 634 proto->set_origin(origin_); |
650 for (GDataFileCollection::const_iterator iter = children_.begin(); | 635 for (GDataFileCollection::const_iterator iter = children_.begin(); |
651 iter != children_.end(); ++iter) { | 636 iter != children_.end(); ++iter) { |
652 GDataFile* file = iter->second->AsGDataFile(); | 637 GDataFile* file = iter->second->AsGDataFile(); |
653 GDataDirectory* dir = iter->second->AsGDataDirectory(); | 638 GDataDirectory* dir = iter->second->AsGDataDirectory(); |
654 if (file) | 639 if (file) |
655 file->ToProto(proto->add_child_files()); | 640 file->ToProto(proto->add_child_files()); |
656 if (dir) | 641 if (dir) |
657 dir->ToProto(proto->add_child_directories()); | 642 dir->ToProto(proto->add_child_directories()); |
658 } | 643 } |
659 } | 644 } |
660 | 645 |
661 void GDataRootDirectory::FromProto(const GDataRootDirectoryProto& proto) { | 646 void GDataRootDirectory::FromProto(const GDataRootDirectoryProto& proto) { |
662 root_ = this; | 647 root_ = this; |
663 GDataDirectory::FromProto(proto.gdata_directory()); | 648 GDataDirectory::FromProto(proto.gdata_directory()); |
664 largest_changestamp_ = proto.largest_changestamp(); | 649 largest_changestamp_ = proto.largest_changestamp(); |
665 } | 650 } |
666 | 651 |
667 void GDataRootDirectory::ToProto(GDataRootDirectoryProto* proto) const { | 652 void GDataRootDirectory::ToProto(GDataRootDirectoryProto* proto) const { |
668 GDataDirectory::ToProto(proto->mutable_gdata_directory()); | 653 GDataDirectory::ToProto(proto->mutable_gdata_directory()); |
669 proto->set_largest_changestamp(largest_changestamp_); | 654 proto->set_largest_changestamp(largest_changestamp_); |
670 } | 655 } |
671 | 656 |
672 void GDataEntry::SerializeToString(std::string* serialized_proto) const { | 657 void GDataRootDirectory::SerializeToString(std::string* proto_string) 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 { | |
716 scoped_ptr<GDataRootDirectoryProto> proto( | 658 scoped_ptr<GDataRootDirectoryProto> proto( |
717 new GDataRootDirectoryProto()); | 659 new GDataRootDirectoryProto()); |
718 ToProto(proto.get()); | 660 ToProto(proto.get()); |
719 const bool ok = proto->SerializeToString(serialized_proto); | 661 const bool ok = proto->SerializeToString(proto_string); |
720 DCHECK(ok); | 662 DCHECK(ok); |
721 } | 663 } |
722 | 664 |
723 bool GDataRootDirectory::ParseFromString(const std::string& serialized_proto) { | 665 bool GDataRootDirectory::ParseFromString(const std::string& proto_string) { |
724 scoped_ptr<GDataRootDirectoryProto> proto( | 666 scoped_ptr<GDataRootDirectoryProto> proto( |
725 new GDataRootDirectoryProto()); | 667 new GDataRootDirectoryProto()); |
726 bool ok = proto->ParseFromString(serialized_proto); | 668 bool ok = proto->ParseFromString(proto_string); |
727 if (ok) { | 669 if (ok) { |
728 FromProto(*proto.get()); | 670 FromProto(*proto.get()); |
729 set_origin(FROM_CACHE); | 671 set_origin(FROM_CACHE); |
730 set_refresh_time(base::Time::Now()); | 672 set_refresh_time(base::Time::Now()); |
731 } | 673 } |
732 return ok; | 674 return ok; |
733 } | 675 } |
734 | 676 |
735 } // namespace gdata | 677 } // namespace gdata |
OLD | NEW |