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

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: rebase. Created 8 years, 1 month 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 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 188
189 // DriveResourceMetadata class implementation. 189 // DriveResourceMetadata class implementation.
190 190
191 DriveResourceMetadata::DriveResourceMetadata() 191 DriveResourceMetadata::DriveResourceMetadata()
192 : blocking_task_runner_(NULL), 192 : blocking_task_runner_(NULL),
193 serialized_size_(0), 193 serialized_size_(0),
194 largest_changestamp_(0), 194 largest_changestamp_(0),
195 origin_(UNINITIALIZED), 195 origin_(UNINITIALIZED),
196 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { 196 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {
197 root_ = CreateDriveDirectory().Pass(); 197 root_ = CreateDriveDirectory().Pass();
198 if (!google_apis::util::IsDriveV2ApiEnabled()) 198 root_->set_title(kDriveRootDirectory);
199 InitializeRootEntry(kDriveRootDirectoryResourceId); 199 root_->SetBaseNameFromTitle();
200 } 200 }
201 201
202 DriveResourceMetadata::~DriveResourceMetadata() { 202 DriveResourceMetadata::~DriveResourceMetadata() {
203 ClearRoot(); 203 ClearRoot();
204 204
205 // Ensure db is closed on the blocking pool. 205 // Ensure db is closed on the blocking pool.
206 if (blocking_task_runner_ && resource_metadata_db_.get()) 206 if (blocking_task_runner_ && resource_metadata_db_.get())
207 blocking_task_runner_->DeleteSoon(FROM_HERE, 207 blocking_task_runner_->DeleteSoon(FROM_HERE,
208 resource_metadata_db_.release()); 208 resource_metadata_db_.release());
209 } 209 }
(...skipping 12 matching lines...) Expand all
222 } 222 }
223 223
224 scoped_ptr<DriveFile> DriveResourceMetadata::CreateDriveFile() { 224 scoped_ptr<DriveFile> DriveResourceMetadata::CreateDriveFile() {
225 return scoped_ptr<DriveFile>(new DriveFile(this)); 225 return scoped_ptr<DriveFile>(new DriveFile(this));
226 } 226 }
227 227
228 scoped_ptr<DriveDirectory> DriveResourceMetadata::CreateDriveDirectory() { 228 scoped_ptr<DriveDirectory> DriveResourceMetadata::CreateDriveDirectory() {
229 return scoped_ptr<DriveDirectory>(new DriveDirectory(this)); 229 return scoped_ptr<DriveDirectory>(new DriveDirectory(this));
230 } 230 }
231 231
232 void DriveResourceMetadata::InitializeRootEntry(const std::string& root_id) { 232 void DriveResourceMetadata::InitializeRootEntry(const std::string& id) {
233 root_ = CreateDriveDirectory().Pass(); 233 DCHECK(root_->resource_id().empty());
234 root_->set_title(kDriveRootDirectory); 234 root_->set_resource_id(id);
235 root_->SetBaseNameFromTitle();
236 root_->set_resource_id(root_id);
237 AddEntryToResourceMap(root_.get()); 235 AddEntryToResourceMap(root_.get());
238 } 236 }
239 237
240 void DriveResourceMetadata::ClearRoot() { 238 void DriveResourceMetadata::ClearRoot() {
241 if (!root_.get()) 239 if (!root_.get())
242 return; 240 return;
243 241
242 // The root is not yet initialized.
243 if (root_->resource_id().empty())
244 return;
245
244 // Note that children have a reference to root_, 246 // Note that children have a reference to root_,
245 // so we need to delete them here. 247 // so we need to delete them here.
246 root_->RemoveChildren(); 248 root_->RemoveChildren();
247 RemoveEntryFromResourceMap(root_->resource_id()); 249 RemoveEntryFromResourceMap(root_->resource_id());
248 DCHECK(resource_map_.empty()); 250 DCHECK(resource_map_.empty());
251 // The resource_map_ should be empty here, but to make sure for non-Debug
252 // build.
249 resource_map_.clear(); 253 resource_map_.clear();
250 root_.reset(); 254 root_.reset();
251 } 255 }
252 256
253 void DriveResourceMetadata::AddEntryToDirectory( 257 void DriveResourceMetadata::AddEntryToDirectory(
254 const FilePath& directory_path, 258 const FilePath& directory_path,
255 scoped_ptr<google_apis::DocumentEntry> doc_entry, 259 scoped_ptr<google_apis::DocumentEntry> doc_entry,
256 const FileMoveCallback& callback) { 260 const FileMoveCallback& callback) {
257 DCHECK(!directory_path.empty()); 261 DCHECK(!directory_path.empty());
258 DCHECK(!callback.is_null()); 262 DCHECK(!callback.is_null());
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
355 MoveEntryToDirectory(file_path, entry->parent()->GetFilePath(), callback); 359 MoveEntryToDirectory(file_path, entry->parent()->GetFilePath(), callback);
356 } 360 }
357 361
358 void DriveResourceMetadata::RemoveEntryFromParent( 362 void DriveResourceMetadata::RemoveEntryFromParent(
359 const std::string& resource_id, 363 const std::string& resource_id,
360 const FileMoveCallback& callback) { 364 const FileMoveCallback& callback) {
361 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 365 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
362 DCHECK(!callback.is_null()); 366 DCHECK(!callback.is_null());
363 367
364 // Disallow deletion of root. 368 // Disallow deletion of root.
365 if (resource_id == kDriveRootDirectoryResourceId) { 369 if (resource_id == root_->resource_id()) {
366 PostFileMoveCallbackError(callback, DRIVE_FILE_ERROR_ACCESS_DENIED); 370 PostFileMoveCallbackError(callback, DRIVE_FILE_ERROR_ACCESS_DENIED);
367 return; 371 return;
368 } 372 }
369 373
370 DriveEntry* entry = GetEntryByResourceId(resource_id); 374 DriveEntry* entry = GetEntryByResourceId(resource_id);
371 if (!entry) { 375 if (!entry) {
372 PostFileMoveCallbackError(callback, DRIVE_FILE_ERROR_NOT_FOUND); 376 PostFileMoveCallbackError(callback, DRIVE_FILE_ERROR_NOT_FOUND);
373 return; 377 return;
374 } 378 }
375 379
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after
642 } 646 }
643 647
644 void DriveResourceMetadata::InitResourceMap( 648 void DriveResourceMetadata::InitResourceMap(
645 CreateDBParams* create_params, 649 CreateDBParams* create_params,
646 const FileOperationCallback& callback) { 650 const FileOperationCallback& callback) {
647 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 651 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
648 DCHECK(create_params); 652 DCHECK(create_params);
649 DCHECK(!resource_metadata_db_.get()); 653 DCHECK(!resource_metadata_db_.get());
650 DCHECK(!callback.is_null()); 654 DCHECK(!callback.is_null());
651 655
652
653 SerializedMap* serialized_resources = &create_params->serialized_resources; 656 SerializedMap* serialized_resources = &create_params->serialized_resources;
654 resource_metadata_db_ = create_params->db.Pass(); 657 resource_metadata_db_ = create_params->db.Pass();
655 if (serialized_resources->empty()) { 658 if (serialized_resources->empty()) {
656 callback.Run(DRIVE_FILE_ERROR_NOT_FOUND); 659 callback.Run(DRIVE_FILE_ERROR_NOT_FOUND);
657 return; 660 return;
658 } 661 }
659 662
663 // Save root directory resource ID as ClearRoot() resets |root_|.
664 std::string saved_root_resource_id = root_->resource_id();
660 ClearRoot(); 665 ClearRoot();
661 666
662 // Version check. 667 // Version check.
663 int32 version = 0; 668 int32 version = 0;
664 SerializedMap::iterator iter = serialized_resources->find(kDBKeyVersion); 669 SerializedMap::iterator iter = serialized_resources->find(kDBKeyVersion);
665 if (iter == serialized_resources->end() || 670 if (iter == serialized_resources->end() ||
666 !base::StringToInt(iter->second, &version) || 671 !base::StringToInt(iter->second, &version) ||
667 version != kProtoVersion) { 672 version != kProtoVersion) {
668 callback.Run(DRIVE_FILE_ERROR_FAILED); 673 callback.Run(DRIVE_FILE_ERROR_FAILED);
669 return; 674 return;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
711 resource_map.find(entry->parent_resource_id()); 716 resource_map.find(entry->parent_resource_id());
712 if (parent_it != resource_map.end()) { 717 if (parent_it != resource_map.end()) {
713 DriveDirectory* parent = parent_it->second->AsDriveDirectory(); 718 DriveDirectory* parent = parent_it->second->AsDriveDirectory();
714 if (parent) { 719 if (parent) {
715 DVLOG(1) << "Adding " << entry->resource_id() 720 DVLOG(1) << "Adding " << entry->resource_id()
716 << " as a child of " << parent->resource_id(); 721 << " as a child of " << parent->resource_id();
717 parent->AddEntry(entry); 722 parent->AddEntry(entry);
718 } else { 723 } else {
719 NOTREACHED() << "Parent is not a directory " << parent->resource_id(); 724 NOTREACHED() << "Parent is not a directory " << parent->resource_id();
720 } 725 }
721 } else if (entry->resource_id() == kDriveRootDirectoryResourceId) { 726 } else if (entry->resource_id() == saved_root_resource_id) {
722 root_.reset(entry->AsDriveDirectory()); 727 root_.reset(entry->AsDriveDirectory());
723 DCHECK(root_.get()); 728 DCHECK(root_.get());
724 AddEntryToResourceMap(root_.get()); 729 AddEntryToResourceMap(root_.get());
725 } else { 730 } else {
726 NOTREACHED() << "Missing parent id " << entry->parent_resource_id() 731 NOTREACHED() << "Missing parent id " << entry->parent_resource_id()
727 << " for resource " << entry->resource_id(); 732 << " for resource " << entry->resource_id();
728 } 733 }
729 } 734 }
730 735
731 if (!root_.get()) { 736 if (!root_.get()) {
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
797 if (!proto.ParseFromString(serialized_proto)) 802 if (!proto.ParseFromString(serialized_proto))
798 return false; 803 return false;
799 804
800 if (proto.version() != kProtoVersion) { 805 if (proto.version() != kProtoVersion) {
801 LOG(ERROR) << "Incompatible proto detected (incompatible version): " 806 LOG(ERROR) << "Incompatible proto detected (incompatible version): "
802 << proto.version(); 807 << proto.version();
803 return false; 808 return false;
804 } 809 }
805 810
806 root_->FromProto(proto.drive_directory()); 811 root_->FromProto(proto.drive_directory());
812 // Root resource ID is already set from proto.
satorux1 2012/10/29 05:28:35 Maybe: // Call AddEntryToResourceMap() instead of
kochi 2012/10/29 05:52:39 Thanks for clarification. Applied.
813 AddEntryToResourceMap(root_.get());
807 814
808 origin_ = INITIALIZED; 815 origin_ = INITIALIZED;
809 largest_changestamp_ = proto.largest_changestamp(); 816 largest_changestamp_ = proto.largest_changestamp();
810 817
811 return true; 818 return true;
812 } 819 }
813 820
814 scoped_ptr<DriveEntry> DriveResourceMetadata::CreateDriveEntryFromProto( 821 scoped_ptr<DriveEntry> DriveResourceMetadata::CreateDriveEntryFromProto(
815 const DriveEntryProto& entry_proto) { 822 const DriveEntryProto& entry_proto) {
816 scoped_ptr<DriveEntry> entry; 823 scoped_ptr<DriveEntry> entry;
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
878 DCHECK(result.get()); 885 DCHECK(result.get());
879 886
880 result->second.path = second_path; 887 result->second.path = second_path;
881 result->second.error = error; 888 result->second.error = error;
882 result->second.proto = entry_proto.Pass(); 889 result->second.proto = entry_proto.Pass();
883 890
884 callback.Run(result.Pass()); 891 callback.Run(result.Pass());
885 } 892 }
886 893
887 } // namespace drive 894 } // namespace drive
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698