Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: chrome/browser/chromeos/gdata/gdata_files.cc

Issue 10825218: Simplify implementation of FindEntryByPathSync (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: remove FindChildAsync 2 Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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);
satorux1 2012/08/07 22:38:09 The new behavior may be rather scary. Previously i
achuithb 2012/08/07 22:48:35 We're using this function in 2 places in GDataFile
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& file_name(entry->base_name());
satorux1 2012/08/07 22:27:05 file_name -> base_name ?
achuithb 2012/08/07 22:29:43 Done.
393 GDataEntry* found_entry = FindChild(file_name);
394 if (!found_entry)
395 return false;
396
397 DCHECK_EQ(entry, found_entry);
398
399 // Remove entry from resource map first. 390 // Remove entry from resource map first.
400 if (directory_service_) 391 if (directory_service_)
401 directory_service_->RemoveEntryFromResourceMap(entry); 392 directory_service_->RemoveEntryFromResourceMap(entry);
402 393
403 // Then delete it from tree. 394 // Then delete it from tree.
404 child_files_.erase(file_name); 395 child_files_.erase(file_name);
405 child_directories_.erase(file_name); 396 child_directories_.erase(file_name);
406
407 return true;
408 } 397 }
409 398
410 void GDataDirectory::RemoveChildren() { 399 void GDataDirectory::RemoveChildren() {
411 RemoveChildFiles(); 400 RemoveChildFiles();
412 RemoveChildDirectories(); 401 RemoveChildDirectories();
413 } 402 }
414 403
415 void GDataDirectory::RemoveChildFiles() { 404 void GDataDirectory::RemoveChildFiles() {
416 for (GDataFileCollection::const_iterator iter = child_files_.begin(); 405 for (GDataFileCollection::const_iterator iter = child_files_.begin();
417 iter != child_files_.end(); ++iter) { 406 iter != child_files_.end(); ++iter) {
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
612 resource_map_.insert(std::make_pair(entry->resource_id(), entry)); 601 resource_map_.insert(std::make_pair(entry->resource_id(), entry));
613 } 602 }
614 603
615 void GDataDirectoryService::RemoveEntryFromResourceMap(GDataEntry* entry) { 604 void GDataDirectoryService::RemoveEntryFromResourceMap(GDataEntry* entry) {
616 // GDataFileSystem has already locked. 605 // GDataFileSystem has already locked.
617 resource_map_.erase(entry->resource_id()); 606 resource_map_.erase(entry->resource_id());
618 } 607 }
619 608
620 GDataEntry* GDataDirectoryService::FindEntryByPathSync( 609 GDataEntry* GDataDirectoryService::FindEntryByPathSync(
621 const FilePath& file_path) { 610 const FilePath& file_path) {
611 if (file_path == root_->GetFilePath())
612 return root_.get();
613
622 std::vector<FilePath::StringType> components; 614 std::vector<FilePath::StringType> components;
623 file_path.GetComponents(&components); 615 file_path.GetComponents(&components);
616 GDataDirectory* current_dir = root_.get();
624 617
625 GDataDirectory* current_dir = root_.get(); 618 for (size_t i = 1; i < components.size() && current_dir; ++i) {
626 FilePath directory_path; 619 GDataEntry* entry = current_dir->FindChild(components[i]);
620 if (!entry)
621 return NULL;
627 622
628 for (size_t i = 0; i < components.size() && current_dir; i++) { 623 if (i == components.size() - 1) // Last component.
629 directory_path = directory_path.Append(current_dir->base_name()); 624 return entry;
630 625 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(); 626 current_dir = entry->AsGDataDirectory();
649 } else {
650 if ((i + 1) == (components.size() - 1))
651 return entry;
652 else
653 return NULL;
654 }
655 } 627 }
satorux1 2012/08/07 22:27:05 Much simpler! Nice.
656 return NULL; 628 return NULL;
657 } 629 }
658 630
659 void GDataDirectoryService::FindEntryByPathAndRunSync( 631 void GDataDirectoryService::FindEntryByPathAndRunSync(
660 const FilePath& search_file_path, 632 const FilePath& search_file_path,
661 const FindEntryCallback& callback) { 633 const FindEntryCallback& callback) {
662 GDataEntry* entry = FindEntryByPathSync(search_file_path); 634 GDataEntry* entry = FindEntryByPathSync(search_file_path);
663 callback.Run(entry ? GDATA_FILE_OK : GDATA_FILE_ERROR_NOT_FOUND, entry); 635 callback.Run(entry ? GDATA_FILE_OK : GDATA_FILE_ERROR_NOT_FOUND, entry);
664 } 636 }
665 637
(...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after
1097 if (file->FromProto(entry_proto)) { 1069 if (file->FromProto(entry_proto)) {
1098 entry.reset(file.release()); 1070 entry.reset(file.release());
1099 } else { 1071 } else {
1100 NOTREACHED() << "FromProto (file) failed"; 1072 NOTREACHED() << "FromProto (file) failed";
1101 } 1073 }
1102 } 1074 }
1103 return entry.Pass(); 1075 return entry.Pass();
1104 } 1076 }
1105 1077
1106 } // namespace gdata 1078 } // namespace gdata
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698