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

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

Issue 10828083: gdata: Add GDataDirectoryService::AddEntryToDirectory() (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address comments 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 <vector> 7 #include <vector>
8 8
9 #include "base/message_loop_proxy.h"
9 #include "base/platform_file.h" 10 #include "base/platform_file.h"
10 #include "base/string_util.h" 11 #include "base/string_util.h"
11 #include "base/stringprintf.h" 12 #include "base/stringprintf.h"
13 #include "base/tracked_objects.h"
12 #include "base/utf_string_conversions.h" 14 #include "base/utf_string_conversions.h"
13 #include "chrome/browser/chromeos/gdata/gdata.pb.h" 15 #include "chrome/browser/chromeos/gdata/gdata.pb.h"
14 #include "chrome/browser/chromeos/gdata/gdata_util.h" 16 #include "chrome/browser/chromeos/gdata/gdata_util.h"
15 #include "chrome/browser/chromeos/gdata/gdata_wapi_parser.h" 17 #include "chrome/browser/chromeos/gdata/gdata_wapi_parser.h"
16 #include "net/base/escape.h" 18 #include "net/base/escape.h"
17 19
18 namespace gdata { 20 namespace gdata {
19 namespace { 21 namespace {
20 22
21 const char kSlash[] = "/"; 23 const char kSlash[] = "/";
22 const char kEscapedSlash[] = "\xE2\x88\x95"; 24 const char kEscapedSlash[] = "\xE2\x88\x95";
23 25
24 // Extracts resource_id out of edit url. 26 // Extracts resource_id out of edit url.
25 std::string ExtractResourceId(const GURL& url) { 27 std::string ExtractResourceId(const GURL& url) {
26 return net::UnescapeURLComponent(url.ExtractFileName(), 28 return net::UnescapeURLComponent(url.ExtractFileName(),
27 net::UnescapeRule::URL_SPECIAL_CHARS); 29 net::UnescapeRule::URL_SPECIAL_CHARS);
28 } 30 }
29 31
30 // Replaces file entry |old_entry| with its fresh value |fresh_file|.
31 void RefreshFileInternal(scoped_ptr<GDataFile> fresh_file,
32 GDataEntry* old_entry) {
33 GDataDirectory* entry_parent = old_entry ? old_entry->parent() : NULL;
34 if (entry_parent) {
35 DCHECK_EQ(fresh_file->resource_id(), old_entry->resource_id());
36 DCHECK(old_entry->AsGDataFile());
37
38 entry_parent->RemoveEntry(old_entry);
39 entry_parent->AddEntry(fresh_file.release());
40 }
41 }
42
43 // Returns true if |proto| is a valid proto as the root directory. 32 // Returns true if |proto| is a valid proto as the root directory.
44 // Used to reject incompatible proto. 33 // Used to reject incompatible proto.
45 bool IsValidRootDirectoryProto(const GDataDirectoryProto& proto) { 34 bool IsValidRootDirectoryProto(const GDataDirectoryProto& proto) {
46 const GDataEntryProto& entry_proto = proto.gdata_entry(); 35 const GDataEntryProto& entry_proto = proto.gdata_entry();
47 // The title field for the root directory was originally empty, then 36 // The title field for the root directory was originally empty, then
48 // changed to "gdata", then changed to "drive". Discard the proto data if 37 // changed to "gdata", then changed to "drive". Discard the proto data if
49 // the older formats are detected. See crbug.com/128133 for details. 38 // the older formats are detected. See crbug.com/128133 for details.
50 if (entry_proto.title() != "drive") { 39 if (entry_proto.title() != "drive") {
51 LOG(ERROR) << "Incompatible proto detected (bad title): " 40 LOG(ERROR) << "Incompatible proto detected (bad title): "
52 << entry_proto.title(); 41 << entry_proto.title();
(...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after
449 438
450 GDataDirectoryService::~GDataDirectoryService() { 439 GDataDirectoryService::~GDataDirectoryService() {
451 // Note that children have a reference to root_, 440 // Note that children have a reference to root_,
452 // so we need to delete them here. 441 // so we need to delete them here.
453 root_->RemoveChildren(); 442 root_->RemoveChildren();
454 RemoveEntryFromResourceMap(root_.get()); 443 RemoveEntryFromResourceMap(root_.get());
455 DCHECK(resource_map_.empty()); 444 DCHECK(resource_map_.empty());
456 resource_map_.clear(); 445 resource_map_.clear();
457 } 446 }
458 447
448 void GDataDirectoryService::AddEntryToDirectory(
449 const FilePath& directory_path,
450 GDataEntry* entry,
451 const FileOperationCallback& callback) {
452 GDataEntry* destination = FindEntryByPathSync(directory_path);
453 GDataFileError error = GDATA_FILE_ERROR_FAILED;
454 if (!destination) {
455 error = GDATA_FILE_ERROR_NOT_FOUND;
456 } else if (!destination->AsGDataDirectory()) {
457 error = GDATA_FILE_ERROR_NOT_A_DIRECTORY;
458 } else {
459 destination->AsGDataDirectory()->AddEntry(entry);
460 error = GDATA_FILE_OK;
461 }
462 if (!callback.is_null()) {
463 base::MessageLoopProxy::current()->PostTask(
464 FROM_HERE, base::Bind(callback, error));
465 }
466 }
467
459 void GDataDirectoryService::AddEntryToResourceMap(GDataEntry* entry) { 468 void GDataDirectoryService::AddEntryToResourceMap(GDataEntry* entry) {
460 // GDataFileSystem has already locked. 469 // GDataFileSystem has already locked.
461 DVLOG(1) << "AddEntryToResourceMap " << entry->resource_id(); 470 DVLOG(1) << "AddEntryToResourceMap " << entry->resource_id();
462 resource_map_.insert(std::make_pair(entry->resource_id(), entry)); 471 resource_map_.insert(std::make_pair(entry->resource_id(), entry));
463 } 472 }
464 473
465 void GDataDirectoryService::RemoveEntryFromResourceMap(GDataEntry* entry) { 474 void GDataDirectoryService::RemoveEntryFromResourceMap(GDataEntry* entry) {
466 // GDataFileSystem has already locked. 475 // GDataFileSystem has already locked.
467 resource_map_.erase(entry->resource_id()); 476 resource_map_.erase(entry->resource_id());
468 } 477 }
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
518 const GetEntryByResourceIdCallback& callback) { 527 const GetEntryByResourceIdCallback& callback) {
519 GDataEntry* entry = GetEntryByResourceId(resource_id); 528 GDataEntry* entry = GetEntryByResourceId(resource_id);
520 callback.Run(entry); 529 callback.Run(entry);
521 } 530 }
522 531
523 void GDataDirectoryService::RefreshFile(scoped_ptr<GDataFile> fresh_file) { 532 void GDataDirectoryService::RefreshFile(scoped_ptr<GDataFile> fresh_file) {
524 DCHECK(fresh_file.get()); 533 DCHECK(fresh_file.get());
525 534
526 // Need to get a reference here because Passed() could get evaluated first. 535 // Need to get a reference here because Passed() could get evaluated first.
527 const std::string& resource_id = fresh_file->resource_id(); 536 const std::string& resource_id = fresh_file->resource_id();
528 GetEntryByResourceIdAsync(resource_id, 537 GetEntryByResourceIdAsync(
529 base::Bind(&RefreshFileInternal, base::Passed(&fresh_file))); 538 resource_id,
539 base::Bind(&GDataDirectoryService::RefreshFileInternal,
540 base::Passed(&fresh_file)));
541 }
542
543 // static
544 void GDataDirectoryService::RefreshFileInternal(
545 scoped_ptr<GDataFile> fresh_file,
546 GDataEntry* old_entry) {
547 GDataDirectory* entry_parent = old_entry ? old_entry->parent() : NULL;
548 if (entry_parent) {
549 DCHECK_EQ(fresh_file->resource_id(), old_entry->resource_id());
550 DCHECK(old_entry->AsGDataFile());
551
552 entry_parent->RemoveEntry(old_entry);
553 entry_parent->AddEntry(fresh_file.release());
554 }
530 } 555 }
531 556
532 // Convert to/from proto. 557 // Convert to/from proto.
533 558
534 // static 559 // static
535 void GDataEntry::ConvertProtoToPlatformFileInfo( 560 void GDataEntry::ConvertProtoToPlatformFileInfo(
536 const PlatformFileInfoProto& proto, 561 const PlatformFileInfoProto& proto,
537 base::PlatformFileInfo* file_info) { 562 base::PlatformFileInfo* file_info) {
538 file_info->size = proto.size(); 563 file_info->size = proto.size();
539 file_info->is_directory = proto.is_directory(); 564 file_info->is_directory = proto.is_directory();
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after
757 if (!root_->FromProto(proto.gdata_directory())) 782 if (!root_->FromProto(proto.gdata_directory()))
758 return false; 783 return false;
759 784
760 origin_ = FROM_CACHE; 785 origin_ = FROM_CACHE;
761 largest_changestamp_ = proto.largest_changestamp(); 786 largest_changestamp_ = proto.largest_changestamp();
762 787
763 return true; 788 return true;
764 } 789 }
765 790
766 } // namespace gdata 791 } // namespace gdata
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/gdata/gdata_files.h ('k') | chrome/browser/chromeos/gdata/gdata_files_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698