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