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/drive/drive_resource_metadata.h" | 5 #include "chrome/browser/chromeos/drive/drive_resource_metadata.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/stringprintf.h" | 8 #include "base/stringprintf.h" |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "chrome/browser/chromeos/drive/drive.pb.h" | 10 #include "chrome/browser/chromeos/drive/drive.pb.h" |
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 // Initialize the storage. | 286 // Initialize the storage. |
287 if (!storage_->Initialize()) | 287 if (!storage_->Initialize()) |
288 return DRIVE_FILE_ERROR_FAILED; | 288 return DRIVE_FILE_ERROR_FAILED; |
289 | 289 |
290 // Remove unneeded proto file. | 290 // Remove unneeded proto file. |
291 if (storage_->IsPersistentStorage()) { | 291 if (storage_->IsPersistentStorage()) { |
292 file_util::Delete(data_directory_path_.Append(kProtoFileName), | 292 file_util::Delete(data_directory_path_.Append(kProtoFileName), |
293 true /* recursive */); | 293 true /* recursive */); |
294 } | 294 } |
295 | 295 |
296 // Initialize the root entry. | 296 // Initialize the grand root and "other" entries. "/drive" and "/drive/other". |
297 if (!storage_->GetEntry(root_resource_id_)) { | 297 // As an intermediate change, "/drive/root" is also added here. |
| 298 // TODO(haruki): Move this initialization to change_list_loader where we |
| 299 // can retrieve the root folder ID from the server. |
| 300 if (!storage_->GetEntry(util::kDriveGrandRootSpecialResourceId)) { |
298 DriveEntryProto root; | 301 DriveEntryProto root; |
299 root.mutable_file_info()->set_is_directory(true); | 302 root.mutable_file_info()->set_is_directory(true); |
300 root.set_resource_id(root_resource_id_); | 303 root.set_resource_id(util::kDriveGrandRootSpecialResourceId); |
301 root.set_title(util::kDriveGrandRootDirName); | 304 root.set_title(util::kDriveGrandRootDirName); |
302 storage_->PutEntry(CreateEntryWithProperBaseName(root)); | 305 storage_->PutEntry(CreateEntryWithProperBaseName(root)); |
303 } | 306 } |
| 307 if (!storage_->GetEntry(util::kDriveOtherDirSpecialResourceId)) { |
| 308 DriveEntryProto other_dir; |
| 309 other_dir.mutable_file_info()->set_is_directory(true); |
| 310 other_dir.set_resource_id(util::kDriveOtherDirSpecialResourceId); |
| 311 other_dir.set_parent_resource_id(util::kDriveGrandRootSpecialResourceId); |
| 312 other_dir.set_title(util::kDriveOtherDirName); |
| 313 AddEntryToDirectory(other_dir); |
| 314 } |
| 315 if (!storage_->GetEntry(root_resource_id_)) { |
| 316 DriveEntryProto mydrive_root; |
| 317 mydrive_root.mutable_file_info()->set_is_directory(true); |
| 318 mydrive_root.set_resource_id(root_resource_id_); |
| 319 mydrive_root.set_parent_resource_id(util::kDriveGrandRootSpecialResourceId); |
| 320 mydrive_root.set_title(util::kDriveMyDriveRootDirName); |
| 321 AddEntryToDirectory(mydrive_root); |
| 322 } |
304 return DRIVE_FILE_OK; | 323 return DRIVE_FILE_OK; |
305 } | 324 } |
306 | 325 |
307 void DriveResourceMetadata::DestroyOnBlockingPool() { | 326 void DriveResourceMetadata::DestroyOnBlockingPool() { |
308 DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); | 327 DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); |
309 delete this; | 328 delete this; |
310 } | 329 } |
311 | 330 |
312 void DriveResourceMetadata::ResetOnBlockingPool() { | 331 void DriveResourceMetadata::ResetOnBlockingPool() { |
313 DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); | 332 DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); |
314 | 333 |
315 RemoveDirectoryChildren(root_resource_id_); | 334 RemoveDirectoryChildren(util::kDriveGrandRootSpecialResourceId); |
| 335 InitializeOnBlockingPool(); |
316 last_serialized_ = base::Time(); | 336 last_serialized_ = base::Time(); |
317 serialized_size_ = 0; | 337 serialized_size_ = 0; |
318 storage_->SetLargestChangestamp(0); | 338 storage_->SetLargestChangestamp(0); |
319 } | 339 } |
320 | 340 |
321 void DriveResourceMetadata::GetLargestChangestamp( | 341 void DriveResourceMetadata::GetLargestChangestamp( |
322 const GetChangestampCallback& callback) { | 342 const GetChangestampCallback& callback) { |
323 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 343 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
324 DCHECK(!callback.is_null()); | 344 DCHECK(!callback.is_null()); |
325 base::PostTaskAndReplyWithResult( | 345 base::PostTaskAndReplyWithResult( |
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
592 scoped_ptr<GetEntryInfoWithFilePathResult> result = | 612 scoped_ptr<GetEntryInfoWithFilePathResult> result = |
593 RefreshEntryOnBlockingPool(*entry); | 613 RefreshEntryOnBlockingPool(*entry); |
594 return FileMoveResult(result->error, result->path); | 614 return FileMoveResult(result->error, result->path); |
595 } | 615 } |
596 | 616 |
597 DriveResourceMetadata::FileMoveResult | 617 DriveResourceMetadata::FileMoveResult |
598 DriveResourceMetadata::RemoveEntryOnBlockingPool( | 618 DriveResourceMetadata::RemoveEntryOnBlockingPool( |
599 const std::string& resource_id) { | 619 const std::string& resource_id) { |
600 DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); | 620 DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); |
601 | 621 |
602 // Disallow deletion of root. | 622 // Disallow deletion of special entries "/drive" and "/drive/other". |
603 if (resource_id == root_resource_id_) | 623 if (util::IsSpecialResourceId(resource_id)) |
604 return FileMoveResult(DRIVE_FILE_ERROR_ACCESS_DENIED); | 624 return FileMoveResult(DRIVE_FILE_ERROR_ACCESS_DENIED); |
605 | 625 |
606 scoped_ptr<DriveEntryProto> entry = storage_->GetEntry(resource_id); | 626 scoped_ptr<DriveEntryProto> entry = storage_->GetEntry(resource_id); |
607 if (!entry) | 627 if (!entry) |
608 return FileMoveResult(DRIVE_FILE_ERROR_NOT_FOUND); | 628 return FileMoveResult(DRIVE_FILE_ERROR_NOT_FOUND); |
609 | 629 |
610 RemoveDirectoryChild(entry->resource_id()); | 630 RemoveDirectoryChild(entry->resource_id()); |
611 return FileMoveResult(DRIVE_FILE_OK, | 631 return FileMoveResult(DRIVE_FILE_OK, |
612 GetFilePath(entry->parent_resource_id())); | 632 GetFilePath(entry->parent_resource_id())); |
613 } | 633 } |
614 | 634 |
615 scoped_ptr<DriveEntryProto> DriveResourceMetadata::FindEntryByPathSync( | 635 scoped_ptr<DriveEntryProto> DriveResourceMetadata::FindEntryByPathSync( |
616 const base::FilePath& file_path) { | 636 const base::FilePath& file_path) { |
617 DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); | 637 DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); |
618 scoped_ptr<DriveEntryProto> current_dir = | 638 scoped_ptr<DriveEntryProto> current_dir = |
619 storage_->GetEntry(root_resource_id_); | 639 storage_->GetEntry(util::kDriveGrandRootSpecialResourceId); |
620 DCHECK(current_dir); | 640 DCHECK(current_dir); |
621 | 641 |
622 if (file_path == GetFilePath(current_dir->resource_id())) | 642 if (file_path == GetFilePath(current_dir->resource_id())) |
623 return current_dir.Pass(); | 643 return current_dir.Pass(); |
624 | 644 |
625 std::vector<base::FilePath::StringType> components; | 645 std::vector<base::FilePath::StringType> components; |
626 file_path.GetComponents(&components); | 646 file_path.GetComponents(&components); |
627 | 647 |
628 for (size_t i = 1; i < components.size() && current_dir; ++i) { | 648 for (size_t i = 1; i < components.size() && current_dir; ++i) { |
629 const std::string component = base::FilePath(components[i]).AsUTF8Unsafe(); | 649 const std::string component = base::FilePath(components[i]).AsUTF8Unsafe(); |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
726 } | 746 } |
727 | 747 |
728 // Reject incompatible input. | 748 // Reject incompatible input. |
729 if (entry->file_info().is_directory() != | 749 if (entry->file_info().is_directory() != |
730 entry_proto.file_info().is_directory()) { | 750 entry_proto.file_info().is_directory()) { |
731 return make_scoped_ptr( | 751 return make_scoped_ptr( |
732 new GetEntryInfoWithFilePathResult(DRIVE_FILE_ERROR_INVALID_OPERATION)); | 752 new GetEntryInfoWithFilePathResult(DRIVE_FILE_ERROR_INVALID_OPERATION)); |
733 } | 753 } |
734 | 754 |
735 // Update data. | 755 // Update data. |
736 if (entry->resource_id() != root_resource_id_) { | 756 if (!util::IsSpecialResourceId(entry->resource_id())) { |
737 scoped_ptr<DriveEntryProto> new_parent = | 757 scoped_ptr<DriveEntryProto> new_parent = |
738 GetDirectory(entry_proto.parent_resource_id()); | 758 GetDirectory(entry_proto.parent_resource_id()); |
739 | 759 |
740 if (!new_parent) { | 760 if (!new_parent) { |
741 return make_scoped_ptr( | 761 return make_scoped_ptr( |
742 new GetEntryInfoWithFilePathResult(DRIVE_FILE_ERROR_NOT_FOUND)); | 762 new GetEntryInfoWithFilePathResult(DRIVE_FILE_ERROR_NOT_FOUND)); |
743 } | 763 } |
744 | 764 |
745 // Remove from the old parent, update the entry, and add it to the new | 765 // Remove from the old parent, update the entry, and add it to the new |
746 // parent. | 766 // parent. |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
891 if (entry->file_info().is_directory()) { | 911 if (entry->file_info().is_directory()) { |
892 child_directories->insert(GetFilePath(entry->resource_id())); | 912 child_directories->insert(GetFilePath(entry->resource_id())); |
893 GetDescendantDirectoryPaths(entry->resource_id(), child_directories); | 913 GetDescendantDirectoryPaths(entry->resource_id(), child_directories); |
894 } | 914 } |
895 } | 915 } |
896 } | 916 } |
897 | 917 |
898 void DriveResourceMetadata::RemoveAllOnBlockingPool() { | 918 void DriveResourceMetadata::RemoveAllOnBlockingPool() { |
899 DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); | 919 DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); |
900 | 920 |
901 RemoveDirectoryChildren(root_resource_id_); | 921 RemoveDirectoryChildren(util::kDriveGrandRootSpecialResourceId); |
| 922 InitializeOnBlockingPool(); |
902 } | 923 } |
903 | 924 |
904 void DriveResourceMetadata::MaybeSaveOnBlockingPool() { | 925 void DriveResourceMetadata::MaybeSaveOnBlockingPool() { |
905 DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); | 926 DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); |
906 | 927 |
907 if (storage_->IsPersistentStorage() || | 928 if (storage_->IsPersistentStorage() || |
908 !ShouldSerializeFileSystemNow(serialized_size_, last_serialized_)) | 929 !ShouldSerializeFileSystemNow(serialized_size_, last_serialized_)) |
909 return; | 930 return; |
910 | 931 |
911 const base::FilePath path = data_directory_path_.Append(kProtoFileName); | 932 const base::FilePath path = data_directory_path_.Append(kProtoFileName); |
912 | 933 |
913 DriveRootDirectoryProto proto; | 934 DriveRootDirectoryProto proto; |
914 DirectoryToProto(root_resource_id_, proto.mutable_drive_directory()); | 935 DirectoryToProto(util::kDriveGrandRootSpecialResourceId, |
| 936 proto.mutable_drive_directory()); |
915 proto.set_largest_changestamp(storage_->GetLargestChangestamp()); | 937 proto.set_largest_changestamp(storage_->GetLargestChangestamp()); |
916 proto.set_version(kProtoVersion); | 938 proto.set_version(kProtoVersion); |
917 | 939 |
918 scoped_ptr<std::string> serialized_proto(new std::string()); | 940 scoped_ptr<std::string> serialized_proto(new std::string()); |
919 const bool ok = proto.SerializeToString(serialized_proto.get()); | 941 const bool ok = proto.SerializeToString(serialized_proto.get()); |
920 DCHECK(ok); | 942 DCHECK(ok); |
921 | 943 |
922 last_serialized_ = base::Time::Now(); | 944 last_serialized_ = base::Time::Now(); |
923 serialized_size_ = serialized_proto->size(); | 945 serialized_size_ = serialized_proto->size(); |
924 WriteStringToFile(path, serialized_proto.Pass()); | 946 WriteStringToFile(path, serialized_proto.Pass()); |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1045 | 1067 |
1046 void DriveResourceMetadata::AddDescendantsFromProto( | 1068 void DriveResourceMetadata::AddDescendantsFromProto( |
1047 const DriveDirectoryProto& proto) { | 1069 const DriveDirectoryProto& proto) { |
1048 DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); | 1070 DCHECK(blocking_task_runner_->RunsTasksOnCurrentThread()); |
1049 DCHECK(proto.drive_entry().file_info().is_directory()); | 1071 DCHECK(proto.drive_entry().file_info().is_directory()); |
1050 DCHECK(!proto.drive_entry().has_file_specific_info()); | 1072 DCHECK(!proto.drive_entry().has_file_specific_info()); |
1051 | 1073 |
1052 #if !defined(NDEBUG) | 1074 #if !defined(NDEBUG) |
1053 std::vector<std::string> children; | 1075 std::vector<std::string> children; |
1054 storage_->GetChildren(proto.drive_entry().resource_id(), &children); | 1076 storage_->GetChildren(proto.drive_entry().resource_id(), &children); |
1055 DCHECK(children.empty()); | 1077 DCHECK(proto.drive_entry().resource_id() == |
| 1078 util::kDriveGrandRootSpecialResourceId || |
| 1079 children.empty()); |
1056 #endif | 1080 #endif |
1057 | 1081 |
1058 // Add child files. | 1082 // Add child files. |
1059 for (int i = 0; i < proto.child_files_size(); ++i) { | 1083 for (int i = 0; i < proto.child_files_size(); ++i) { |
1060 DriveEntryProto file(CreateEntryWithProperBaseName(proto.child_files(i))); | 1084 DriveEntryProto file(CreateEntryWithProperBaseName(proto.child_files(i))); |
1061 DCHECK_EQ(proto.drive_entry().resource_id(), file.parent_resource_id()); | 1085 DCHECK_EQ(proto.drive_entry().resource_id(), file.parent_resource_id()); |
1062 AddEntryToDirectory(file); | 1086 AddEntryToDirectory(file); |
1063 } | 1087 } |
1064 // Add child directories recursively. | 1088 // Add child directories recursively. |
1065 for (int i = 0; i < proto.child_directories_size(); ++i) { | 1089 for (int i = 0; i < proto.child_directories_size(); ++i) { |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1154 | 1178 |
1155 // An old proto file might not have per-directory changestamps. Add them if | 1179 // An old proto file might not have per-directory changestamps. Add them if |
1156 // needed. | 1180 // needed. |
1157 const DriveDirectoryProto& root = proto.drive_directory(); | 1181 const DriveDirectoryProto& root = proto.drive_directory(); |
1158 if (!root.drive_entry().directory_specific_info().has_changestamp()) { | 1182 if (!root.drive_entry().directory_specific_info().has_changestamp()) { |
1159 AddPerDirectoryChangestamps(proto.mutable_drive_directory(), | 1183 AddPerDirectoryChangestamps(proto.mutable_drive_directory(), |
1160 proto.largest_changestamp()); | 1184 proto.largest_changestamp()); |
1161 } | 1185 } |
1162 | 1186 |
1163 if (proto.drive_directory().drive_entry().resource_id() != | 1187 if (proto.drive_directory().drive_entry().resource_id() != |
1164 root_resource_id_) { | 1188 util::kDriveGrandRootSpecialResourceId) { |
1165 LOG(ERROR) << "Incompatible proto detected (incompatible root ID): " | 1189 LOG(ERROR) << "Incompatible proto detected (incompatible root ID): " |
1166 << proto.drive_directory().drive_entry().resource_id(); | 1190 << proto.drive_directory().drive_entry().resource_id(); |
1167 return false; | 1191 return false; |
1168 } | 1192 } |
1169 | 1193 |
1170 storage_->PutEntry( | 1194 storage_->PutEntry( |
1171 CreateEntryWithProperBaseName(proto.drive_directory().drive_entry())); | 1195 CreateEntryWithProperBaseName(proto.drive_directory().drive_entry())); |
1172 AddDescendantsFromProto(proto.drive_directory()); | 1196 AddDescendantsFromProto(proto.drive_directory()); |
1173 | 1197 |
1174 storage_->SetLargestChangestamp(proto.largest_changestamp()); | 1198 storage_->SetLargestChangestamp(proto.largest_changestamp()); |
1175 | 1199 |
1176 return true; | 1200 return true; |
1177 } | 1201 } |
1178 | 1202 |
1179 } // namespace drive | 1203 } // namespace drive |
OLD | NEW |