Chromium Code Reviews| 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> | |
| 9 | 8 |
| 10 #include "base/message_loop_proxy.h" | 9 #include "base/message_loop_proxy.h" |
| 11 #include "base/platform_file.h" | 10 #include "base/platform_file.h" |
| 12 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
| 13 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 14 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
| 15 #include "base/sequenced_task_runner.h" | 14 #include "base/sequenced_task_runner.h" |
| 16 #include "base/tracked_objects.h" | 15 #include "base/tracked_objects.h" |
| 17 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
| 18 #include "chrome/browser/chromeos/gdata/gdata.pb.h" | 17 #include "chrome/browser/chromeos/gdata/gdata.pb.h" |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 247 | 246 |
| 248 void GDataDirectory::InitFromDocumentEntry(DocumentEntry* doc) { | 247 void GDataDirectory::InitFromDocumentEntry(DocumentEntry* doc) { |
| 249 GDataEntry::InitFromDocumentEntry(doc); | 248 GDataEntry::InitFromDocumentEntry(doc); |
| 250 | 249 |
| 251 const Link* upload_link = doc->GetLinkByType(Link::RESUMABLE_CREATE_MEDIA); | 250 const Link* upload_link = doc->GetLinkByType(Link::RESUMABLE_CREATE_MEDIA); |
| 252 if (upload_link) | 251 if (upload_link) |
| 253 upload_url_ = upload_link->href(); | 252 upload_url_ = upload_link->href(); |
| 254 } | 253 } |
| 255 | 254 |
| 256 void GDataDirectory::AddEntry(GDataEntry* entry) { | 255 void GDataDirectory::AddEntry(GDataEntry* entry) { |
| 256 DCHECK(!entry->parent()); | |
| 257 | |
| 257 // The entry name may have been changed due to prior name de-duplication. | 258 // The entry name may have been changed due to prior name de-duplication. |
| 258 // We need to first restore the file name based on the title before going | 259 // We need to first restore the file name based on the title before going |
| 259 // through name de-duplication again when it is added to another directory. | 260 // through name de-duplication again when it is added to another directory. |
| 260 entry->SetBaseNameFromTitle(); | 261 entry->SetBaseNameFromTitle(); |
| 261 | 262 |
| 262 // Do file name de-duplication - find files with the same name and | 263 // Do file name de-duplication - find files with the same name and |
| 263 // append a name modifier to the name. | 264 // append a name modifier to the name. |
| 264 int max_modifier = 1; | 265 int max_modifier = 1; |
| 265 FilePath full_file_name(entry->base_name()); | 266 FilePath full_file_name(entry->base_name()); |
| 266 const std::string extension = full_file_name.Extension(); | 267 const std::string extension = full_file_name.Extension(); |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 289 directory_service_->AddEntryToResourceMap(entry); | 290 directory_service_->AddEntryToResourceMap(entry); |
| 290 | 291 |
| 291 // Setup child and parent links. | 292 // Setup child and parent links. |
| 292 AddChild(entry); | 293 AddChild(entry); |
| 293 entry->SetParent(this); | 294 entry->SetParent(this); |
| 294 } | 295 } |
| 295 | 296 |
| 296 bool GDataDirectory::TakeOverEntries(GDataDirectory* dir) { | 297 bool GDataDirectory::TakeOverEntries(GDataDirectory* dir) { |
| 297 for (GDataFileCollection::iterator iter = dir->child_files_.begin(); | 298 for (GDataFileCollection::iterator iter = dir->child_files_.begin(); |
| 298 iter != dir->child_files_.end(); ++iter) { | 299 iter != dir->child_files_.end(); ++iter) { |
| 300 iter->second->SetParent(NULL); | |
|
satorux1
2012/08/10 16:14:44
iter->second looks cryptic. maybe add:
GDataFi
achuithb
2012/08/11 00:37:45
Done.
| |
| 299 AddEntry(iter->second); | 301 AddEntry(iter->second); |
| 300 } | 302 } |
| 301 dir->child_files_.clear(); | 303 dir->child_files_.clear(); |
| 302 | 304 |
| 303 for (GDataDirectoryCollection::iterator iter = | 305 for (GDataDirectoryCollection::iterator iter = |
| 304 dir->child_directories_.begin(); | 306 dir->child_directories_.begin(); |
| 305 iter != dir->child_directories_.end(); ++iter) { | 307 iter != dir->child_directories_.end(); ++iter) { |
| 308 iter->second->SetParent(NULL); | |
|
satorux1
2012/08/10 16:14:44
ditto
achuithb
2012/08/11 00:37:45
Done.
| |
| 306 AddEntry(iter->second); | 309 AddEntry(iter->second); |
| 307 } | 310 } |
| 308 dir->child_directories_.clear(); | 311 dir->child_directories_.clear(); |
| 309 return true; | 312 return true; |
| 310 } | 313 } |
| 311 | 314 |
| 312 void GDataDirectory::RemoveEntry(GDataEntry* entry) { | 315 void GDataDirectory::RemoveEntry(GDataEntry* entry) { |
| 313 DCHECK(entry); | 316 DCHECK(entry); |
| 314 | 317 |
| 315 RemoveChild(entry); | 318 RemoveChild(entry); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 348 const std::string& base_name(entry->base_name()); | 351 const std::string& base_name(entry->base_name()); |
| 349 // entry must be present in this directory. | 352 // entry must be present in this directory. |
| 350 DCHECK_EQ(entry, FindChild(base_name)); | 353 DCHECK_EQ(entry, FindChild(base_name)); |
| 351 // Remove entry from resource map first. | 354 // Remove entry from resource map first. |
| 352 if (directory_service_) | 355 if (directory_service_) |
| 353 directory_service_->RemoveEntryFromResourceMap(entry); | 356 directory_service_->RemoveEntryFromResourceMap(entry); |
| 354 | 357 |
| 355 // Then delete it from tree. | 358 // Then delete it from tree. |
| 356 child_files_.erase(base_name); | 359 child_files_.erase(base_name); |
| 357 child_directories_.erase(base_name); | 360 child_directories_.erase(base_name); |
| 361 | |
| 362 entry->SetParent(NULL); | |
| 358 } | 363 } |
| 359 | 364 |
| 360 void GDataDirectory::RemoveChildren() { | 365 void GDataDirectory::RemoveChildren() { |
| 361 RemoveChildFiles(); | 366 RemoveChildFiles(); |
| 362 RemoveChildDirectories(); | 367 RemoveChildDirectories(); |
| 363 } | 368 } |
| 364 | 369 |
| 365 void GDataDirectory::RemoveChildFiles() { | 370 void GDataDirectory::RemoveChildFiles() { |
| 366 for (GDataFileCollection::const_iterator iter = child_files_.begin(); | 371 for (GDataFileCollection::const_iterator iter = child_files_.begin(); |
| 367 iter != child_files_.end(); ++iter) { | 372 iter != child_files_.end(); ++iter) { |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 556 void GDataDirectoryService::ClearRoot() { | 561 void GDataDirectoryService::ClearRoot() { |
| 557 // Note that children have a reference to root_, | 562 // Note that children have a reference to root_, |
| 558 // so we need to delete them here. | 563 // so we need to delete them here. |
| 559 root_->RemoveChildren(); | 564 root_->RemoveChildren(); |
| 560 RemoveEntryFromResourceMap(root_.get()); | 565 RemoveEntryFromResourceMap(root_.get()); |
| 561 DCHECK(resource_map_.empty()); | 566 DCHECK(resource_map_.empty()); |
| 562 resource_map_.clear(); | 567 resource_map_.clear(); |
| 563 root_.reset(); | 568 root_.reset(); |
| 564 } | 569 } |
| 565 | 570 |
| 571 void GDataDirectoryService::AddEntryToDirectory( | |
| 572 GDataDirectory* directory, | |
| 573 GDataEntry* new_entry, | |
| 574 const base::Closure& callback) { | |
|
satorux1
2012/08/10 16:14:44
As of now, this function won't fail as we are deal
achuithb
2012/08/11 00:37:45
This is an excellent suggestion, however we want t
| |
| 575 DCHECK(!callback.is_null()); | |
|
satorux1
2012/08/10 16:14:44
maybe add DCHECK(directory) and DCHECK(new_entry)
achuithb
2012/08/11 00:37:45
Done.
| |
| 576 directory->AddEntry(new_entry); | |
| 577 base::MessageLoopProxy::current()->PostTask(FROM_HERE, callback); | |
| 578 } | |
| 579 | |
| 580 | |
| 566 void GDataDirectoryService::MoveEntryToDirectory( | 581 void GDataDirectoryService::MoveEntryToDirectory( |
| 567 const FilePath& directory_path, | 582 const FilePath& directory_path, |
| 568 GDataEntry* entry, | 583 GDataEntry* entry, |
| 569 const FileMoveCallback& callback) { | 584 const FileMoveCallback& callback) { |
| 570 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 585 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 571 DCHECK(entry); | 586 DCHECK(entry); |
| 572 | 587 |
| 573 if (entry->parent()) | 588 if (entry->parent()) |
| 574 entry->parent()->RemoveChild(entry); | 589 entry->parent()->RemoveChild(entry); |
| 575 | 590 |
| 576 GDataEntry* destination = FindEntryByPathSync(directory_path); | 591 GDataEntry* destination = FindEntryByPathSync(directory_path); |
| 577 FilePath moved_file_path; | 592 FilePath moved_file_path; |
| 578 GDataFileError error = GDATA_FILE_ERROR_FAILED; | 593 GDataFileError error = GDATA_FILE_ERROR_FAILED; |
| 579 if (!destination) { | 594 if (!destination) { |
| 580 error = GDATA_FILE_ERROR_NOT_FOUND; | 595 error = GDATA_FILE_ERROR_NOT_FOUND; |
| 581 } else if (!destination->AsGDataDirectory()) { | 596 } else if (!destination->AsGDataDirectory()) { |
| 582 error = GDATA_FILE_ERROR_NOT_A_DIRECTORY; | 597 error = GDATA_FILE_ERROR_NOT_A_DIRECTORY; |
| 583 } else { | 598 } else { |
| 584 destination->AsGDataDirectory()->AddEntry(entry); | 599 destination->AsGDataDirectory()->AddEntry(entry); |
| 585 moved_file_path = entry->GetFilePath(); | 600 moved_file_path = entry->GetFilePath(); |
| 586 error = GDATA_FILE_OK; | 601 error = GDATA_FILE_OK; |
| 587 } | 602 } |
| 588 if (!callback.is_null()) { | 603 if (!callback.is_null()) { |
| 589 base::MessageLoopProxy::current()->PostTask( | 604 base::MessageLoopProxy::current()->PostTask( |
| 590 FROM_HERE, base::Bind(callback, error, moved_file_path)); | 605 FROM_HERE, base::Bind(callback, error, moved_file_path)); |
| 591 } | 606 } |
| 592 } | 607 } |
| 593 | 608 |
| 609 void GDataDirectoryService::RemoveEntryFromParent( | |
| 610 GDataEntry* entry, | |
| 611 const base::Closure& callback) { | |
|
satorux1
2012/08/10 16:14:44
ditto. FileOperationCallback?
achuithb
2012/08/11 00:37:45
Done. FileMoveCallback here and also in RefreshDir
| |
| 612 entry->parent()->RemoveEntry(entry); | |
| 613 if (!callback.is_null()) { | |
| 614 base::MessageLoopProxy::current()->PostTask(FROM_HERE, callback); | |
| 615 } | |
| 616 } | |
| 617 | |
| 618 | |
| 594 void GDataDirectoryService::AddEntryToResourceMap(GDataEntry* entry) { | 619 void GDataDirectoryService::AddEntryToResourceMap(GDataEntry* entry) { |
| 595 // GDataFileSystem has already locked. | 620 // GDataFileSystem has already locked. |
| 596 DVLOG(1) << "AddEntryToResourceMap " << entry->resource_id(); | 621 DVLOG(1) << "AddEntryToResourceMap " << entry->resource_id(); |
| 597 resource_map_.insert(std::make_pair(entry->resource_id(), entry)); | 622 resource_map_.insert(std::make_pair(entry->resource_id(), entry)); |
| 598 } | 623 } |
| 599 | 624 |
| 600 void GDataDirectoryService::RemoveEntryFromResourceMap(GDataEntry* entry) { | 625 void GDataDirectoryService::RemoveEntryFromResourceMap(GDataEntry* entry) { |
| 601 // GDataFileSystem has already locked. | 626 // GDataFileSystem has already locked. |
| 602 resource_map_.erase(entry->resource_id()); | 627 resource_map_.erase(entry->resource_id()); |
| 603 } | 628 } |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 727 GDataDirectory* entry_parent = old_entry ? old_entry->parent() : NULL; | 752 GDataDirectory* entry_parent = old_entry ? old_entry->parent() : NULL; |
| 728 if (entry_parent) { | 753 if (entry_parent) { |
| 729 DCHECK_EQ(fresh_file->resource_id(), old_entry->resource_id()); | 754 DCHECK_EQ(fresh_file->resource_id(), old_entry->resource_id()); |
| 730 DCHECK(old_entry->AsGDataFile()); | 755 DCHECK(old_entry->AsGDataFile()); |
| 731 | 756 |
| 732 entry_parent->RemoveEntry(old_entry); | 757 entry_parent->RemoveEntry(old_entry); |
| 733 entry_parent->AddEntry(fresh_file.release()); | 758 entry_parent->AddEntry(fresh_file.release()); |
| 734 } | 759 } |
| 735 } | 760 } |
| 736 | 761 |
| 762 void GDataDirectoryService::RefreshDirectory( | |
| 763 const std::string& directory_resource_id, | |
| 764 const ResourceMap& file_map, | |
| 765 const base::Closure& callback) { | |
|
satorux1
2012/08/10 16:14:44
DCHECK(!callback.is_null())
achuithb
2012/08/11 00:37:45
Done.
| |
| 766 GetEntryByResourceIdAsync(directory_resource_id, | |
|
satorux1
2012/08/10 16:14:44
nit: please move directory_resource_id to the next
achuithb
2012/08/11 00:37:45
Done.
| |
| 767 base::Bind(&GDataDirectoryService::RefreshDirectoryInternal, | |
| 768 file_map, | |
| 769 callback)); | |
| 770 } | |
| 771 | |
| 772 // static | |
| 773 void GDataDirectoryService::RefreshDirectoryInternal( | |
| 774 const ResourceMap& file_map, | |
| 775 const base::Closure& callback, | |
| 776 GDataEntry* directory_entry) { | |
| 777 DCHECK(!callback.is_null()); | |
|
satorux1
2012/08/10 16:14:44
nit: add a blank line like we do elsewhere
achuithb
2012/08/11 00:37:45
Done.
| |
| 778 if (!directory_entry || !directory_entry->AsGDataDirectory()) { | |
| 779 LOG(ERROR) << "RefreshDirectoryInternal: Failed to find directory"; | |
| 780 return; | |
| 781 } | |
| 782 | |
| 783 GDataDirectory* directory = directory_entry->AsGDataDirectory(); | |
| 784 | |
| 785 directory->RemoveChildFiles(); | |
| 786 // Add files from file_map. | |
| 787 for (ResourceMap::const_iterator it = file_map.begin(); | |
| 788 it != file_map.end(); ++it) { | |
| 789 scoped_ptr<GDataEntry> entry(it->second); | |
| 790 // Skip if it's not a file (i.e. directory). | |
| 791 if (!entry->AsGDataFile()) | |
| 792 continue; | |
| 793 directory->AddEntry(entry.release()); | |
| 794 } | |
| 795 | |
| 796 callback.Run(); | |
| 797 } | |
| 798 | |
| 737 void GDataDirectoryService::InitFromDB( | 799 void GDataDirectoryService::InitFromDB( |
| 738 const FilePath& db_path, | 800 const FilePath& db_path, |
| 739 base::SequencedTaskRunner* blocking_task_runner, | 801 base::SequencedTaskRunner* blocking_task_runner, |
| 740 const FileOperationCallback& callback) { | 802 const FileOperationCallback& callback) { |
| 741 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 803 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 742 DCHECK(!db_path.empty()); | 804 DCHECK(!db_path.empty()); |
| 743 DCHECK(blocking_task_runner); | 805 DCHECK(blocking_task_runner); |
| 744 | 806 |
| 745 if (directory_service_db_.get()) { | 807 if (directory_service_db_.get()) { |
| 746 if (!callback.is_null()) | 808 if (!callback.is_null()) |
| (...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1195 DCHECK(result.get()); | 1257 DCHECK(result.get()); |
| 1196 | 1258 |
| 1197 result->second.path = second_path; | 1259 result->second.path = second_path; |
| 1198 result->second.error = error; | 1260 result->second.error = error; |
| 1199 result->second.proto = entry_proto.Pass(); | 1261 result->second.proto = entry_proto.Pass(); |
| 1200 | 1262 |
| 1201 callback.Run(result.Pass()); | 1263 callback.Run(result.Pass()); |
| 1202 } | 1264 } |
| 1203 | 1265 |
| 1204 } // namespace gdata | 1266 } // namespace gdata |
| OLD | NEW |