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 <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "base/platform_file.h" | 10 #include "base/platform_file.h" |
11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
13 #include "chrome/browser/chromeos/gdata/find_entry_delegate.h" | |
13 #include "chrome/browser/chromeos/gdata/gdata.pb.h" | 14 #include "chrome/browser/chromeos/gdata/gdata.pb.h" |
14 #include "chrome/browser/chromeos/gdata/gdata_parser.h" | 15 #include "chrome/browser/chromeos/gdata/gdata_parser.h" |
15 #include "net/base/escape.h" | 16 #include "net/base/escape.h" |
16 | 17 |
17 namespace { | 18 namespace { |
18 | 19 |
19 // Content refresh time. | 20 // Content refresh time. |
20 #ifndef NDEBUG | 21 #ifndef NDEBUG |
21 const int kRefreshTimeInSec = 10; | 22 const int kRefreshTimeInSec = 10; |
22 #else | 23 #else |
(...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
439 if (iter->second->AsGDataDirectory()) { | 440 if (iter->second->AsGDataDirectory()) { |
440 RemoveEntriesFromResourceMap( | 441 RemoveEntriesFromResourceMap( |
441 iter->second->AsGDataDirectory()->children()); | 442 iter->second->AsGDataDirectory()->children()); |
442 continue; | 443 continue; |
443 } | 444 } |
444 | 445 |
445 resource_map_.erase(iter->second->resource_id()); | 446 resource_map_.erase(iter->second->resource_id()); |
446 } | 447 } |
447 } | 448 } |
448 | 449 |
450 void GDataRootDirectory::FindEntryByPath( | |
451 const FilePath& file_path, | |
452 FindEntryDelegate* delegate) { | |
453 // GDataFileSystem has already locked. | |
454 DCHECK(delegate); | |
455 | |
456 std::vector<FilePath::StringType> components; | |
457 file_path.GetComponents(&components); | |
458 | |
459 GDataDirectory* current_dir = this; | |
460 FilePath directory_path; | |
461 for (size_t i = 0; i < components.size() && current_dir; i++) { | |
462 directory_path = directory_path.Append(current_dir->file_name()); | |
463 | |
464 // Last element must match, if not last then it must be a directory. | |
465 if (i == components.size() - 1) { | |
466 if (current_dir->file_name() == components[i]) | |
467 delegate->OnDone(base::PLATFORM_FILE_OK, directory_path, current_dir); | |
468 else | |
469 delegate->OnDone(base::PLATFORM_FILE_ERROR_NOT_FOUND, FilePath(), NULL); | |
470 | |
471 return; | |
472 } | |
473 | |
474 // Not the last part of the path, search for the next segment. | |
475 GDataFileCollection::const_iterator file_iter = | |
476 current_dir->children().find(components[i + 1]); | |
477 if (file_iter == current_dir->children().end()) { | |
478 delegate->OnDone(base::PLATFORM_FILE_ERROR_NOT_FOUND, FilePath(), NULL); | |
479 return; | |
480 } | |
481 | |
482 // Found file, must be the last segment. | |
483 if (file_iter->second->file_info().is_directory) { | |
484 // Found directory, continue traversal. | |
485 current_dir = file_iter->second->AsGDataDirectory(); | |
486 } else { | |
487 if ((i + 1) == (components.size() - 1)) { | |
488 delegate->OnDone(base::PLATFORM_FILE_OK, | |
489 directory_path, | |
490 file_iter->second); | |
491 } else { | |
492 delegate->OnDone(base::PLATFORM_FILE_ERROR_NOT_FOUND, FilePath(), NULL); | |
493 } | |
494 | |
495 return; | |
496 } | |
497 } | |
498 delegate->OnDone(base::PLATFORM_FILE_ERROR_NOT_FOUND, FilePath(), NULL); | |
499 } | |
satorux1
2012/04/25 06:01:37
would it make sense to write a unit test for this
achuithb
2012/04/25 10:05:35
I'm hoping to get rid of this entire function when
| |
500 | |
449 GDataEntry* GDataRootDirectory::GetEntryByResourceId( | 501 GDataEntry* GDataRootDirectory::GetEntryByResourceId( |
450 const std::string& resource) { | 502 const std::string& resource) { |
451 // GDataFileSystem has already locked. | 503 // GDataFileSystem has already locked. |
452 ResourceMap::const_iterator iter = resource_map_.find(resource); | 504 ResourceMap::const_iterator iter = resource_map_.find(resource); |
453 if (iter == resource_map_.end()) | 505 if (iter == resource_map_.end()) |
454 return NULL; | 506 return NULL; |
455 return iter->second; | 507 return iter->second; |
456 } | 508 } |
457 | 509 |
458 void GDataRootDirectory::SetCacheMap(const CacheMap& new_cache_map) { | 510 void GDataRootDirectory::SetCacheMap(const CacheMap& new_cache_map) { |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
726 bool ok = proto->ParseFromString(serialized_proto); | 778 bool ok = proto->ParseFromString(serialized_proto); |
727 if (ok) { | 779 if (ok) { |
728 FromProto(*proto.get()); | 780 FromProto(*proto.get()); |
729 set_origin(FROM_CACHE); | 781 set_origin(FROM_CACHE); |
730 set_refresh_time(base::Time::Now()); | 782 set_refresh_time(base::Time::Now()); |
731 } | 783 } |
732 return ok; | 784 return ok; |
733 } | 785 } |
734 | 786 |
735 } // namespace gdata | 787 } // namespace gdata |
OLD | NEW |