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 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 | 329 |
330 // Add entry to resource map. | 330 // Add entry to resource map. |
331 if (directory_service_) | 331 if (directory_service_) |
332 directory_service_->AddEntryToResourceMap(entry); | 332 directory_service_->AddEntryToResourceMap(entry); |
333 | 333 |
334 // Setup child and parent links. | 334 // Setup child and parent links. |
335 AddChild(entry); | 335 AddChild(entry); |
336 entry->SetParent(this); | 336 entry->SetParent(this); |
337 } | 337 } |
338 | 338 |
339 bool GDataDirectory::TakeEntry(GDataEntry* entry) { | |
340 DCHECK(entry); | |
341 DCHECK(entry->parent()); | |
342 | |
343 entry->parent()->RemoveChild(entry); | |
344 AddEntry(entry); | |
345 | |
346 return true; | |
347 } | |
348 | |
349 bool GDataDirectory::TakeOverEntries(GDataDirectory* dir) { | 339 bool GDataDirectory::TakeOverEntries(GDataDirectory* dir) { |
350 for (GDataFileCollection::iterator iter = dir->child_files_.begin(); | 340 for (GDataFileCollection::iterator iter = dir->child_files_.begin(); |
351 iter != dir->child_files_.end(); ++iter) { | 341 iter != dir->child_files_.end(); ++iter) { |
352 AddEntry(iter->second); | 342 AddEntry(iter->second); |
353 } | 343 } |
354 dir->child_files_.clear(); | 344 dir->child_files_.clear(); |
355 | 345 |
356 for (GDataDirectoryCollection::iterator iter = | 346 for (GDataDirectoryCollection::iterator iter = |
357 dir->child_directories_.begin(); | 347 dir->child_directories_.begin(); |
358 iter != dir->child_directories_.end(); ++iter) { | 348 iter != dir->child_directories_.end(); ++iter) { |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
582 void GDataDirectoryService::ClearRoot() { | 572 void GDataDirectoryService::ClearRoot() { |
583 // Note that children have a reference to root_, | 573 // Note that children have a reference to root_, |
584 // so we need to delete them here. | 574 // so we need to delete them here. |
585 root_->RemoveChildren(); | 575 root_->RemoveChildren(); |
586 RemoveEntryFromResourceMap(root_.get()); | 576 RemoveEntryFromResourceMap(root_.get()); |
587 DCHECK(resource_map_.empty()); | 577 DCHECK(resource_map_.empty()); |
588 resource_map_.clear(); | 578 resource_map_.clear(); |
589 root_.reset(); | 579 root_.reset(); |
590 } | 580 } |
591 | 581 |
592 void GDataDirectoryService::AddEntryToDirectory( | 582 void GDataDirectoryService::MoveEntryToDirectory( |
593 const FilePath& directory_path, | 583 const FilePath& directory_path, |
594 GDataEntry* entry, | 584 GDataEntry* entry, |
595 const FileOperationCallback& callback) { | 585 const FileMoveCallback& callback) { |
| 586 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 587 DCHECK(entry); |
| 588 |
| 589 if (entry->parent()) |
| 590 entry->parent()->RemoveChild(entry); |
| 591 |
596 GDataEntry* destination = FindEntryByPathSync(directory_path); | 592 GDataEntry* destination = FindEntryByPathSync(directory_path); |
| 593 FilePath moved_file_path; |
597 GDataFileError error = GDATA_FILE_ERROR_FAILED; | 594 GDataFileError error = GDATA_FILE_ERROR_FAILED; |
598 if (!destination) { | 595 if (!destination) { |
599 error = GDATA_FILE_ERROR_NOT_FOUND; | 596 error = GDATA_FILE_ERROR_NOT_FOUND; |
600 } else if (!destination->AsGDataDirectory()) { | 597 } else if (!destination->AsGDataDirectory()) { |
601 error = GDATA_FILE_ERROR_NOT_A_DIRECTORY; | 598 error = GDATA_FILE_ERROR_NOT_A_DIRECTORY; |
602 } else { | 599 } else { |
603 destination->AsGDataDirectory()->AddEntry(entry); | 600 destination->AsGDataDirectory()->AddEntry(entry); |
| 601 moved_file_path = entry->GetFilePath(); |
604 error = GDATA_FILE_OK; | 602 error = GDATA_FILE_OK; |
605 } | 603 } |
606 if (!callback.is_null()) { | 604 if (!callback.is_null()) { |
607 base::MessageLoopProxy::current()->PostTask( | 605 base::MessageLoopProxy::current()->PostTask( |
608 FROM_HERE, base::Bind(callback, error)); | 606 FROM_HERE, base::Bind(callback, error, moved_file_path)); |
609 } | 607 } |
610 } | 608 } |
611 | 609 |
612 void GDataDirectoryService::AddEntryToResourceMap(GDataEntry* entry) { | 610 void GDataDirectoryService::AddEntryToResourceMap(GDataEntry* entry) { |
613 // GDataFileSystem has already locked. | 611 // GDataFileSystem has already locked. |
614 DVLOG(1) << "AddEntryToResourceMap " << entry->resource_id(); | 612 DVLOG(1) << "AddEntryToResourceMap " << entry->resource_id(); |
615 resource_map_.insert(std::make_pair(entry->resource_id(), entry)); | 613 resource_map_.insert(std::make_pair(entry->resource_id(), entry)); |
616 } | 614 } |
617 | 615 |
618 void GDataDirectoryService::RemoveEntryFromResourceMap(GDataEntry* entry) { | 616 void GDataDirectoryService::RemoveEntryFromResourceMap(GDataEntry* entry) { |
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1025 file_specific_info->set_file_md5(file_md5_); | 1023 file_specific_info->set_file_md5(file_md5_); |
1026 file_specific_info->set_document_extension(document_extension_); | 1024 file_specific_info->set_document_extension(document_extension_); |
1027 file_specific_info->set_is_hosted_document(is_hosted_document_); | 1025 file_specific_info->set_is_hosted_document(is_hosted_document_); |
1028 } | 1026 } |
1029 | 1027 |
1030 bool GDataDirectory::FromProto(const GDataDirectoryProto& proto) { | 1028 bool GDataDirectory::FromProto(const GDataDirectoryProto& proto) { |
1031 DCHECK(proto.gdata_entry().file_info().is_directory()); | 1029 DCHECK(proto.gdata_entry().file_info().is_directory()); |
1032 DCHECK(!proto.gdata_entry().has_file_specific_info()); | 1030 DCHECK(!proto.gdata_entry().has_file_specific_info()); |
1033 | 1031 |
1034 for (int i = 0; i < proto.child_files_size(); ++i) { | 1032 for (int i = 0; i < proto.child_files_size(); ++i) { |
1035 scoped_ptr<GDataFile> file(new GDataFile(this, directory_service_)); | 1033 scoped_ptr<GDataFile> file(new GDataFile(NULL, directory_service_)); |
1036 if (!file->FromProto(proto.child_files(i))) { | 1034 if (!file->FromProto(proto.child_files(i))) { |
1037 RemoveChildren(); | 1035 RemoveChildren(); |
1038 return false; | 1036 return false; |
1039 } | 1037 } |
1040 AddEntry(file.release()); | 1038 AddEntry(file.release()); |
1041 } | 1039 } |
1042 for (int i = 0; i < proto.child_directories_size(); ++i) { | 1040 for (int i = 0; i < proto.child_directories_size(); ++i) { |
1043 scoped_ptr<GDataDirectory> dir(new GDataDirectory(this, | 1041 scoped_ptr<GDataDirectory> dir(new GDataDirectory(NULL, |
1044 directory_service_)); | 1042 directory_service_)); |
1045 if (!dir->FromProto(proto.child_directories(i))) { | 1043 if (!dir->FromProto(proto.child_directories(i))) { |
1046 RemoveChildren(); | 1044 RemoveChildren(); |
1047 return false; | 1045 return false; |
1048 } | 1046 } |
1049 AddEntry(dir.release()); | 1047 AddEntry(dir.release()); |
1050 } | 1048 } |
1051 | 1049 |
1052 // The states of the directory should be updated after children are | 1050 // The states of the directory should be updated after children are |
1053 // handled successfully, so that incomplete states are not left. | 1051 // handled successfully, so that incomplete states are not left. |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1214 DCHECK(result.get()); | 1212 DCHECK(result.get()); |
1215 | 1213 |
1216 result->second.path = second_path; | 1214 result->second.path = second_path; |
1217 result->second.error = error; | 1215 result->second.error = error; |
1218 result->second.proto = entry_proto.Pass(); | 1216 result->second.proto = entry_proto.Pass(); |
1219 | 1217 |
1220 callback.Run(result.Pass()); | 1218 callback.Run(result.Pass()); |
1221 } | 1219 } |
1222 | 1220 |
1223 } // namespace gdata | 1221 } // namespace gdata |
OLD | NEW |