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 <leveldb/db.h> | 7 #include <leveldb/db.h> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/message_loop_proxy.h" | 10 #include "base/message_loop_proxy.h" |
(...skipping 659 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
670 return iter == resource_map_.end() ? NULL : iter->second; | 670 return iter == resource_map_.end() ? NULL : iter->second; |
671 } | 671 } |
672 | 672 |
673 void GDataDirectoryService::GetEntryByResourceIdAsync( | 673 void GDataDirectoryService::GetEntryByResourceIdAsync( |
674 const std::string& resource_id, | 674 const std::string& resource_id, |
675 const GetEntryByResourceIdCallback& callback) { | 675 const GetEntryByResourceIdCallback& callback) { |
676 GDataEntry* entry = GetEntryByResourceId(resource_id); | 676 GDataEntry* entry = GetEntryByResourceId(resource_id); |
677 callback.Run(entry); | 677 callback.Run(entry); |
678 } | 678 } |
679 | 679 |
680 void GDataDirectoryService::GetEntryInfoByPath( | |
681 const FilePath& path, | |
682 const GetEntryInfoCallback& callback) { | |
683 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
684 DCHECK(!callback.is_null()); | |
685 | |
686 scoped_ptr<GDataEntryProto> entry_proto; | |
687 GDataFileError error = GDATA_FILE_ERROR_FAILED; | |
688 | |
689 GDataEntry* entry = FindEntryByPathSync(path); | |
690 if (entry) { | |
691 entry_proto.reset(new GDataEntryProto); | |
692 entry->ToProtoFull(entry_proto.get()); | |
693 error = GDATA_FILE_OK; | |
694 } else { | |
695 error = GDATA_FILE_ERROR_NOT_FOUND; | |
696 } | |
697 | |
698 base::MessageLoopProxy::current()->PostTask( | |
699 FROM_HERE, | |
700 base::Bind(callback, error, base::Passed(&entry_proto))); | |
701 } | |
702 | |
703 void GDataDirectoryService::ReadDirectoryByPath( | |
704 const FilePath& path, | |
705 const ReadDirectoryCallback& callback) { | |
706 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
707 DCHECK(!callback.is_null()); | |
708 | |
709 scoped_ptr<GDataEntryProtoVector> entries; | |
710 GDataFileError error = GDATA_FILE_ERROR_FAILED; | |
711 | |
712 GDataEntry* entry = FindEntryByPathSync(path); | |
713 if (entry && entry->AsGDataDirectory()) { | |
714 GDataDirectory* directory = entry->AsGDataDirectory(); | |
achuithb
2012/08/07 21:39:20
nit: Is this temporary necessary?
satorux1
2012/08/07 22:57:05
Good catch. no longer needed.
| |
715 entries = directory->ToProtoVector(); | |
716 error = GDATA_FILE_OK; | |
717 } else if (entry && !entry->AsGDataDirectory()) { | |
718 error = GDATA_FILE_ERROR_NOT_A_DIRECTORY; | |
719 } else { | |
720 error = GDATA_FILE_ERROR_NOT_FOUND; | |
721 } | |
722 | |
723 base::MessageLoopProxy::current()->PostTask( | |
724 FROM_HERE, | |
725 base::Bind(callback, error, base::Passed(&entries))); | |
726 } | |
727 | |
680 void GDataDirectoryService::RefreshFile(scoped_ptr<GDataFile> fresh_file) { | 728 void GDataDirectoryService::RefreshFile(scoped_ptr<GDataFile> fresh_file) { |
681 DCHECK(fresh_file.get()); | 729 DCHECK(fresh_file.get()); |
682 | 730 |
683 // Need to get a reference here because Passed() could get evaluated first. | 731 // Need to get a reference here because Passed() could get evaluated first. |
684 const std::string& resource_id = fresh_file->resource_id(); | 732 const std::string& resource_id = fresh_file->resource_id(); |
685 GetEntryByResourceIdAsync( | 733 GetEntryByResourceIdAsync( |
686 resource_id, | 734 resource_id, |
687 base::Bind(&GDataDirectoryService::RefreshFileInternal, | 735 base::Bind(&GDataDirectoryService::RefreshFileInternal, |
688 base::Passed(&fresh_file))); | 736 base::Passed(&fresh_file))); |
689 } | 737 } |
(...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1017 file->ToProto(proto->add_child_files()); | 1065 file->ToProto(proto->add_child_files()); |
1018 } | 1066 } |
1019 for (GDataDirectoryCollection::const_iterator iter = | 1067 for (GDataDirectoryCollection::const_iterator iter = |
1020 child_directories_.begin(); | 1068 child_directories_.begin(); |
1021 iter != child_directories_.end(); ++iter) { | 1069 iter != child_directories_.end(); ++iter) { |
1022 GDataDirectory* dir = iter->second; | 1070 GDataDirectory* dir = iter->second; |
1023 dir->ToProto(proto->add_child_directories()); | 1071 dir->ToProto(proto->add_child_directories()); |
1024 } | 1072 } |
1025 } | 1073 } |
1026 | 1074 |
1075 scoped_ptr<GDataEntryProtoVector> GDataDirectory::ToProtoVector() const { | |
1076 scoped_ptr<GDataEntryProtoVector> entries(new GDataEntryProtoVector); | |
1077 for (GDataFileCollection::const_iterator iter = child_files().begin(); | |
1078 iter != child_files().end(); ++iter) { | |
1079 GDataEntryProto proto; | |
1080 iter->second->ToProto(&proto); | |
1081 entries->push_back(proto); | |
1082 } | |
1083 for (GDataDirectoryCollection::const_iterator iter = | |
1084 child_directories().begin(); | |
1085 iter != child_directories().end(); ++iter) { | |
1086 GDataEntryProto proto; | |
1087 // Convert to GDataEntry, as we don't want to include children in |proto|. | |
1088 static_cast<const GDataEntry*>(iter->second)->ToProtoFull(&proto); | |
1089 entries->push_back(proto); | |
1090 } | |
1091 | |
1092 return entries.Pass(); | |
1093 } | |
1094 | |
1027 void GDataEntry::SerializeToString(std::string* serialized_proto) const { | 1095 void GDataEntry::SerializeToString(std::string* serialized_proto) const { |
1028 const GDataFile* file = AsGDataFileConst(); | 1096 const GDataFile* file = AsGDataFileConst(); |
1029 const GDataDirectory* dir = AsGDataDirectoryConst(); | 1097 const GDataDirectory* dir = AsGDataDirectoryConst(); |
1030 | 1098 |
1031 if (file) { | 1099 if (file) { |
1032 GDataEntryProto entry_proto; | 1100 GDataEntryProto entry_proto; |
1033 file->ToProto(&entry_proto); | 1101 file->ToProto(&entry_proto); |
1034 const bool ok = entry_proto.SerializeToString(serialized_proto); | 1102 const bool ok = entry_proto.SerializeToString(serialized_proto); |
1035 DCHECK(ok); | 1103 DCHECK(ok); |
1036 } else if (dir) { | 1104 } else if (dir) { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1097 if (file->FromProto(entry_proto)) { | 1165 if (file->FromProto(entry_proto)) { |
1098 entry.reset(file.release()); | 1166 entry.reset(file.release()); |
1099 } else { | 1167 } else { |
1100 NOTREACHED() << "FromProto (file) failed"; | 1168 NOTREACHED() << "FromProto (file) failed"; |
1101 } | 1169 } |
1102 } | 1170 } |
1103 return entry.Pass(); | 1171 return entry.Pass(); |
1104 } | 1172 } |
1105 | 1173 |
1106 } // namespace gdata | 1174 } // namespace gdata |
OLD | NEW |