| 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 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 | 343 |
| 344 for (GDataDirectoryCollection::iterator iter = | 344 for (GDataDirectoryCollection::iterator iter = |
| 345 dir->child_directories_.begin(); | 345 dir->child_directories_.begin(); |
| 346 iter != dir->child_directories_.end(); ++iter) { | 346 iter != dir->child_directories_.end(); ++iter) { |
| 347 AddEntry(iter->second); | 347 AddEntry(iter->second); |
| 348 } | 348 } |
| 349 dir->child_directories_.clear(); | 349 dir->child_directories_.clear(); |
| 350 return true; | 350 return true; |
| 351 } | 351 } |
| 352 | 352 |
| 353 bool GDataDirectory::RemoveEntry(GDataEntry* entry) { | 353 void GDataDirectory::RemoveEntry(GDataEntry* entry) { |
| 354 DCHECK(entry); | 354 DCHECK(entry); |
| 355 | 355 |
| 356 if (!RemoveChild(entry)) | 356 RemoveChild(entry); |
| 357 return false; | |
| 358 | |
| 359 delete entry; | 357 delete entry; |
| 360 return true; | |
| 361 } | 358 } |
| 362 | 359 |
| 363 GDataEntry* GDataDirectory::FindChild( | 360 GDataEntry* GDataDirectory::FindChild( |
| 364 const FilePath::StringType& file_name) const { | 361 const FilePath::StringType& file_name) const { |
| 365 GDataFileCollection::const_iterator it = child_files_.find(file_name); | 362 GDataFileCollection::const_iterator it = child_files_.find(file_name); |
| 366 if (it != child_files_.end()) | 363 if (it != child_files_.end()) |
| 367 return it->second; | 364 return it->second; |
| 368 | 365 |
| 369 GDataDirectoryCollection::const_iterator itd = | 366 GDataDirectoryCollection::const_iterator itd = |
| 370 child_directories_.find(file_name); | 367 child_directories_.find(file_name); |
| 371 if (itd != child_directories_.end()) | 368 if (itd != child_directories_.end()) |
| 372 return itd->second; | 369 return itd->second; |
| 373 | 370 |
| 374 return NULL; | 371 return NULL; |
| 375 } | 372 } |
| 376 | 373 |
| 377 void GDataDirectory::AddChild(GDataEntry* entry) { | 374 void GDataDirectory::AddChild(GDataEntry* entry) { |
| 378 DCHECK(entry); | 375 DCHECK(entry); |
| 379 | 376 |
| 380 GDataFile* file = entry->AsGDataFile(); | 377 GDataFile* file = entry->AsGDataFile(); |
| 381 if (file) | 378 if (file) |
| 382 child_files_.insert(std::make_pair(entry->base_name(), file)); | 379 child_files_.insert(std::make_pair(entry->base_name(), file)); |
| 383 | 380 |
| 384 GDataDirectory* directory = entry->AsGDataDirectory(); | 381 GDataDirectory* directory = entry->AsGDataDirectory(); |
| 385 if (directory) | 382 if (directory) |
| 386 child_directories_.insert(std::make_pair(entry->base_name(), directory)); | 383 child_directories_.insert(std::make_pair(entry->base_name(), directory)); |
| 387 } | 384 } |
| 388 | 385 |
| 389 bool GDataDirectory::RemoveChild(GDataEntry* entry) { | 386 void GDataDirectory::RemoveChild(GDataEntry* entry) { |
| 390 DCHECK(entry); | 387 DCHECK(entry); |
| 391 | 388 |
| 392 const std::string file_name(entry->base_name()); | 389 const std::string& base_name(entry->base_name()); |
| 393 GDataEntry* found_entry = FindChild(file_name); | 390 // entry must be present in this directory. |
| 394 if (!found_entry) | 391 DCHECK_EQ(entry, FindChild(base_name)); |
| 395 return false; | |
| 396 | |
| 397 DCHECK_EQ(entry, found_entry); | |
| 398 | |
| 399 // Remove entry from resource map first. | 392 // Remove entry from resource map first. |
| 400 if (directory_service_) | 393 if (directory_service_) |
| 401 directory_service_->RemoveEntryFromResourceMap(entry); | 394 directory_service_->RemoveEntryFromResourceMap(entry); |
| 402 | 395 |
| 403 // Then delete it from tree. | 396 // Then delete it from tree. |
| 404 child_files_.erase(file_name); | 397 child_files_.erase(base_name); |
| 405 child_directories_.erase(file_name); | 398 child_directories_.erase(base_name); |
| 406 | |
| 407 return true; | |
| 408 } | 399 } |
| 409 | 400 |
| 410 void GDataDirectory::RemoveChildren() { | 401 void GDataDirectory::RemoveChildren() { |
| 411 RemoveChildFiles(); | 402 RemoveChildFiles(); |
| 412 RemoveChildDirectories(); | 403 RemoveChildDirectories(); |
| 413 } | 404 } |
| 414 | 405 |
| 415 void GDataDirectory::RemoveChildFiles() { | 406 void GDataDirectory::RemoveChildFiles() { |
| 416 for (GDataFileCollection::const_iterator iter = child_files_.begin(); | 407 for (GDataFileCollection::const_iterator iter = child_files_.begin(); |
| 417 iter != child_files_.end(); ++iter) { | 408 iter != child_files_.end(); ++iter) { |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 612 resource_map_.insert(std::make_pair(entry->resource_id(), entry)); | 603 resource_map_.insert(std::make_pair(entry->resource_id(), entry)); |
| 613 } | 604 } |
| 614 | 605 |
| 615 void GDataDirectoryService::RemoveEntryFromResourceMap(GDataEntry* entry) { | 606 void GDataDirectoryService::RemoveEntryFromResourceMap(GDataEntry* entry) { |
| 616 // GDataFileSystem has already locked. | 607 // GDataFileSystem has already locked. |
| 617 resource_map_.erase(entry->resource_id()); | 608 resource_map_.erase(entry->resource_id()); |
| 618 } | 609 } |
| 619 | 610 |
| 620 GDataEntry* GDataDirectoryService::FindEntryByPathSync( | 611 GDataEntry* GDataDirectoryService::FindEntryByPathSync( |
| 621 const FilePath& file_path) { | 612 const FilePath& file_path) { |
| 613 if (file_path == root_->GetFilePath()) |
| 614 return root_.get(); |
| 615 |
| 622 std::vector<FilePath::StringType> components; | 616 std::vector<FilePath::StringType> components; |
| 623 file_path.GetComponents(&components); | 617 file_path.GetComponents(&components); |
| 618 GDataDirectory* current_dir = root_.get(); |
| 624 | 619 |
| 625 GDataDirectory* current_dir = root_.get(); | 620 for (size_t i = 1; i < components.size() && current_dir; ++i) { |
| 626 FilePath directory_path; | 621 GDataEntry* entry = current_dir->FindChild(components[i]); |
| 622 if (!entry) |
| 623 return NULL; |
| 627 | 624 |
| 628 for (size_t i = 0; i < components.size() && current_dir; i++) { | 625 if (i == components.size() - 1) // Last component. |
| 629 directory_path = directory_path.Append(current_dir->base_name()); | 626 return entry; |
| 630 | 627 else |
| 631 // Last element must match, if not last then it must be a directory. | |
| 632 if (i == components.size() - 1) { | |
| 633 if (current_dir->base_name() == components[i]) | |
| 634 return current_dir; | |
| 635 else | |
| 636 return NULL; | |
| 637 } | |
| 638 | |
| 639 // Not the last part of the path, search for the next segment. | |
| 640 GDataEntry* entry = current_dir->FindChild(components[i + 1]); | |
| 641 if (!entry) { | |
| 642 return NULL; | |
| 643 } | |
| 644 | |
| 645 // Found file, must be the last segment. | |
| 646 if (entry->file_info().is_directory) { | |
| 647 // Found directory, continue traversal. | |
| 648 current_dir = entry->AsGDataDirectory(); | 628 current_dir = entry->AsGDataDirectory(); |
| 649 } else { | |
| 650 if ((i + 1) == (components.size() - 1)) | |
| 651 return entry; | |
| 652 else | |
| 653 return NULL; | |
| 654 } | |
| 655 } | 629 } |
| 656 return NULL; | 630 return NULL; |
| 657 } | 631 } |
| 658 | 632 |
| 659 void GDataDirectoryService::FindEntryByPathAndRunSync( | 633 void GDataDirectoryService::FindEntryByPathAndRunSync( |
| 660 const FilePath& search_file_path, | 634 const FilePath& search_file_path, |
| 661 const FindEntryCallback& callback) { | 635 const FindEntryCallback& callback) { |
| 662 GDataEntry* entry = FindEntryByPathSync(search_file_path); | 636 GDataEntry* entry = FindEntryByPathSync(search_file_path); |
| 663 callback.Run(entry ? GDATA_FILE_OK : GDATA_FILE_ERROR_NOT_FOUND, entry); | 637 callback.Run(entry ? GDATA_FILE_OK : GDATA_FILE_ERROR_NOT_FOUND, entry); |
| 664 } | 638 } |
| (...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1097 if (file->FromProto(entry_proto)) { | 1071 if (file->FromProto(entry_proto)) { |
| 1098 entry.reset(file.release()); | 1072 entry.reset(file.release()); |
| 1099 } else { | 1073 } else { |
| 1100 NOTREACHED() << "FromProto (file) failed"; | 1074 NOTREACHED() << "FromProto (file) failed"; |
| 1101 } | 1075 } |
| 1102 } | 1076 } |
| 1103 return entry.Pass(); | 1077 return entry.Pass(); |
| 1104 } | 1078 } |
| 1105 | 1079 |
| 1106 } // namespace gdata | 1080 } // namespace gdata |
| OLD | NEW |