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

Side by Side Diff: chrome/browser/chromeos/drive/drive_resource_metadata.cc

Issue 11227020: Set root resource ID upon full feed update. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: make root_resource_id() work when root_ is not initialized. Created 8 years, 2 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/drive/drive_resource_metadata.h" 5 #include "chrome/browser/chromeos/drive/drive_resource_metadata.h"
6 6
7 #include <leveldb/db.h> 7 #include <leveldb/db.h>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/message_loop_proxy.h" 10 #include "base/message_loop_proxy.h"
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 } 178 }
179 179
180 // DriveResourceMetadata class implementation. 180 // DriveResourceMetadata class implementation.
181 181
182 DriveResourceMetadata::DriveResourceMetadata() 182 DriveResourceMetadata::DriveResourceMetadata()
183 : blocking_task_runner_(NULL), 183 : blocking_task_runner_(NULL),
184 serialized_size_(0), 184 serialized_size_(0),
185 largest_changestamp_(0), 185 largest_changestamp_(0),
186 origin_(UNINITIALIZED), 186 origin_(UNINITIALIZED),
187 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { 187 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {
188 root_ = CreateDriveDirectory().Pass();
189 if (!google_apis::util::IsDriveV2ApiEnabled())
190 InitializeRootEntry(kDriveRootDirectoryResourceId);
191 } 188 }
192 189
193 DriveResourceMetadata::~DriveResourceMetadata() { 190 DriveResourceMetadata::~DriveResourceMetadata() {
194 ClearRoot(); 191 ClearRoot();
195 192
196 // Ensure db is closed on the blocking pool. 193 // Ensure db is closed on the blocking pool.
197 if (blocking_task_runner_ && resource_metadata_db_.get()) 194 if (blocking_task_runner_ && resource_metadata_db_.get())
198 blocking_task_runner_->DeleteSoon(FROM_HERE, 195 blocking_task_runner_->DeleteSoon(FROM_HERE,
199 resource_metadata_db_.release()); 196 resource_metadata_db_.release());
200 } 197 }
(...skipping 13 matching lines...) Expand all
214 211
215 scoped_ptr<DriveFile> DriveResourceMetadata::CreateDriveFile() { 212 scoped_ptr<DriveFile> DriveResourceMetadata::CreateDriveFile() {
216 return scoped_ptr<DriveFile>(new DriveFile(this)); 213 return scoped_ptr<DriveFile>(new DriveFile(this));
217 } 214 }
218 215
219 scoped_ptr<DriveDirectory> DriveResourceMetadata::CreateDriveDirectory() { 216 scoped_ptr<DriveDirectory> DriveResourceMetadata::CreateDriveDirectory() {
220 return scoped_ptr<DriveDirectory>(new DriveDirectory(this)); 217 return scoped_ptr<DriveDirectory>(new DriveDirectory(this));
221 } 218 }
222 219
223 void DriveResourceMetadata::InitializeRootEntry(const std::string& root_id) { 220 void DriveResourceMetadata::InitializeRootEntry(const std::string& root_id) {
221 DCHECK(!root_.get());
224 root_ = CreateDriveDirectory().Pass(); 222 root_ = CreateDriveDirectory().Pass();
225 root_->set_title(kDriveRootDirectory); 223 root_->set_title(kDriveRootDirectory);
226 root_->SetBaseNameFromTitle(); 224 root_->SetBaseNameFromTitle();
227 root_->set_resource_id(root_id); 225 root_->set_resource_id(root_id);
228 AddEntryToResourceMap(root_.get()); 226 AddEntryToResourceMap(root_.get());
229 } 227 }
230 228
231 void DriveResourceMetadata::ClearRoot() { 229 void DriveResourceMetadata::ClearRoot() {
232 if (!root_.get()) 230 if (!root_.get())
233 return; 231 return;
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
346 MoveEntryToDirectory(file_path, entry->parent()->GetFilePath(), callback); 344 MoveEntryToDirectory(file_path, entry->parent()->GetFilePath(), callback);
347 } 345 }
348 346
349 void DriveResourceMetadata::RemoveEntryFromParent( 347 void DriveResourceMetadata::RemoveEntryFromParent(
350 const std::string& resource_id, 348 const std::string& resource_id,
351 const FileMoveCallback& callback) { 349 const FileMoveCallback& callback) {
352 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 350 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
353 DCHECK(!callback.is_null()); 351 DCHECK(!callback.is_null());
354 352
355 // Disallow deletion of root. 353 // Disallow deletion of root.
356 if (resource_id == kDriveRootDirectoryResourceId) { 354 if (resource_id == root_->resource_id()) {
357 PostFileMoveCallbackError(callback, DRIVE_FILE_ERROR_ACCESS_DENIED); 355 PostFileMoveCallbackError(callback, DRIVE_FILE_ERROR_ACCESS_DENIED);
358 return; 356 return;
359 } 357 }
360 358
361 DriveEntry* entry = GetEntryByResourceId(resource_id); 359 DriveEntry* entry = GetEntryByResourceId(resource_id);
362 if (!entry) { 360 if (!entry) {
363 PostFileMoveCallbackError(callback, DRIVE_FILE_ERROR_NOT_FOUND); 361 PostFileMoveCallbackError(callback, DRIVE_FILE_ERROR_NOT_FOUND);
364 return; 362 return;
365 } 363 }
366 364
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after
604 } 602 }
605 603
606 void DriveResourceMetadata::InitFromDB( 604 void DriveResourceMetadata::InitFromDB(
607 const FilePath& db_path, 605 const FilePath& db_path,
608 base::SequencedTaskRunner* blocking_task_runner, 606 base::SequencedTaskRunner* blocking_task_runner,
609 const FileOperationCallback& callback) { 607 const FileOperationCallback& callback) {
610 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 608 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
611 DCHECK(!db_path.empty()); 609 DCHECK(!db_path.empty());
612 DCHECK(blocking_task_runner); 610 DCHECK(blocking_task_runner);
613 DCHECK(!callback.is_null()); 611 DCHECK(!callback.is_null());
612 DCHECK(!root_->resource_id().empty());
614 613
615 if (resource_metadata_db_.get()) { 614 if (resource_metadata_db_.get()) {
616 callback.Run(DRIVE_FILE_ERROR_IN_USE); 615 callback.Run(DRIVE_FILE_ERROR_IN_USE);
617 return; 616 return;
618 } 617 }
619 618
620 blocking_task_runner_ = blocking_task_runner; 619 blocking_task_runner_ = blocking_task_runner;
621 620
622 DVLOG(1) << "InitFromDB " << db_path.value(); 621 DVLOG(1) << "InitFromDB " << db_path.value();
623 622
(...skipping 10 matching lines...) Expand all
634 } 633 }
635 634
636 void DriveResourceMetadata::InitResourceMap( 635 void DriveResourceMetadata::InitResourceMap(
637 CreateDBParams* create_params, 636 CreateDBParams* create_params,
638 const FileOperationCallback& callback) { 637 const FileOperationCallback& callback) {
639 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 638 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
640 DCHECK(create_params); 639 DCHECK(create_params);
641 DCHECK(!resource_metadata_db_.get()); 640 DCHECK(!resource_metadata_db_.get());
642 DCHECK(!callback.is_null()); 641 DCHECK(!callback.is_null());
643 642
644
645 SerializedMap* serialized_resources = &create_params->serialized_resources; 643 SerializedMap* serialized_resources = &create_params->serialized_resources;
646 resource_metadata_db_ = create_params->db.Pass(); 644 resource_metadata_db_ = create_params->db.Pass();
647 if (serialized_resources->empty()) { 645 if (serialized_resources->empty()) {
648 origin_ = INITIALIZING; 646 origin_ = INITIALIZING;
649 callback.Run(DRIVE_FILE_ERROR_NOT_FOUND); 647 callback.Run(DRIVE_FILE_ERROR_NOT_FOUND);
650 return; 648 return;
651 } 649 }
652 650
651 std::string saved_root_resource_id = root_->resource_id();
653 ClearRoot(); 652 ClearRoot();
654 653
655 // Version check. 654 // Version check.
656 int32 version = 0; 655 int32 version = 0;
657 SerializedMap::iterator iter = serialized_resources->find(kDBKeyVersion); 656 SerializedMap::iterator iter = serialized_resources->find(kDBKeyVersion);
658 if (iter == serialized_resources->end() || 657 if (iter == serialized_resources->end() ||
659 !base::StringToInt(iter->second, &version) || 658 !base::StringToInt(iter->second, &version) ||
660 version != kProtoVersion) { 659 version != kProtoVersion) {
661 callback.Run(DRIVE_FILE_ERROR_FAILED); 660 callback.Run(DRIVE_FILE_ERROR_FAILED);
662 return; 661 return;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
703 resource_map.find(entry->parent_resource_id()); 702 resource_map.find(entry->parent_resource_id());
704 if (parent_it != resource_map.end()) { 703 if (parent_it != resource_map.end()) {
705 DriveDirectory* parent = parent_it->second->AsDriveDirectory(); 704 DriveDirectory* parent = parent_it->second->AsDriveDirectory();
706 if (parent) { 705 if (parent) {
707 DVLOG(1) << "Adding " << entry->resource_id() 706 DVLOG(1) << "Adding " << entry->resource_id()
708 << " as a child of " << parent->resource_id(); 707 << " as a child of " << parent->resource_id();
709 parent->AddEntry(entry); 708 parent->AddEntry(entry);
710 } else { 709 } else {
711 NOTREACHED() << "Parent is not a directory " << parent->resource_id(); 710 NOTREACHED() << "Parent is not a directory " << parent->resource_id();
712 } 711 }
713 } else if (entry->resource_id() == kDriveRootDirectoryResourceId) { 712 } else if (entry->resource_id() == saved_root_resource_id) {
714 root_.reset(entry->AsDriveDirectory()); 713 root_.reset(entry->AsDriveDirectory());
715 DCHECK(root_.get()); 714 DCHECK(root_.get());
716 AddEntryToResourceMap(root_.get()); 715 AddEntryToResourceMap(root_.get());
717 } else { 716 } else {
718 NOTREACHED() << "Missing parent id " << entry->parent_resource_id() 717 NOTREACHED() << "Missing parent id " << entry->parent_resource_id()
719 << " for resource " << entry->resource_id(); 718 << " for resource " << entry->resource_id();
720 } 719 }
721 } 720 }
722 721
723 if (!root_.get()) { 722 if (!root_.get()) {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
765 set_last_serialized(base::Time::Now()); 764 set_last_serialized(base::Time::Now());
766 set_serialized_size(serialized_size); 765 set_serialized_size(serialized_size);
767 766
768 blocking_task_runner_->PostTask( 767 blocking_task_runner_->PostTask(
769 FROM_HERE, 768 FROM_HERE,
770 base::Bind(&ResourceMetadataDB::Save, 769 base::Bind(&ResourceMetadataDB::Save,
771 base::Unretained(resource_metadata_db_.get()), 770 base::Unretained(resource_metadata_db_.get()),
772 serialized_resources)); 771 serialized_resources));
773 } 772 }
774 773
774 const std::string DriveResourceMetadata::root_resource_id() const {
775 return root_.get() ? root_->resource_id() : std::string();
776 }
777
775 void DriveResourceMetadata::SerializeToString( 778 void DriveResourceMetadata::SerializeToString(
776 std::string* serialized_proto) const { 779 std::string* serialized_proto) const {
777 DriveRootDirectoryProto proto; 780 DriveRootDirectoryProto proto;
778 root_->ToProto(proto.mutable_drive_directory()); 781 root_->ToProto(proto.mutable_drive_directory());
779 proto.set_largest_changestamp(largest_changestamp_); 782 proto.set_largest_changestamp(largest_changestamp_);
780 proto.set_version(kProtoVersion); 783 proto.set_version(kProtoVersion);
781 784
782 const bool ok = proto.SerializeToString(serialized_proto); 785 const bool ok = proto.SerializeToString(serialized_proto);
783 DCHECK(ok); 786 DCHECK(ok);
784 } 787 }
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
865 DCHECK(result.get()); 868 DCHECK(result.get());
866 869
867 result->second.path = second_path; 870 result->second.path = second_path;
868 result->second.error = error; 871 result->second.error = error;
869 result->second.proto = entry_proto.Pass(); 872 result->second.proto = entry_proto.Pass();
870 873
871 callback.Run(result.Pass()); 874 callback.Run(result.Pass());
872 } 875 }
873 876
874 } // namespace drive 877 } // namespace drive
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698