| 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/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 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 | 189 |
| 190 // DriveResourceMetadata class implementation. | 190 // DriveResourceMetadata class implementation. |
| 191 | 191 |
| 192 DriveResourceMetadata::DriveResourceMetadata() | 192 DriveResourceMetadata::DriveResourceMetadata() |
| 193 : blocking_task_runner_(NULL), | 193 : blocking_task_runner_(NULL), |
| 194 serialized_size_(0), | 194 serialized_size_(0), |
| 195 largest_changestamp_(0), | 195 largest_changestamp_(0), |
| 196 origin_(UNINITIALIZED), | 196 origin_(UNINITIALIZED), |
| 197 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 197 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
| 198 root_ = CreateDriveDirectory().Pass(); | 198 root_ = CreateDriveDirectory().Pass(); |
| 199 root_->set_title(kDriveRootDirectory); | 199 if (!google_apis::util::IsDriveV2ApiEnabled()) |
| 200 root_->SetBaseNameFromTitle(); | 200 InitializeRootEntry(kDriveRootDirectoryResourceId); |
| 201 } | 201 } |
| 202 | 202 |
| 203 DriveResourceMetadata::~DriveResourceMetadata() { | 203 DriveResourceMetadata::~DriveResourceMetadata() { |
| 204 ClearRoot(); | 204 ClearRoot(); |
| 205 | 205 |
| 206 // Ensure db is closed on the blocking pool. | 206 // Ensure db is closed on the blocking pool. |
| 207 if (blocking_task_runner_ && resource_metadata_db_.get()) | 207 if (blocking_task_runner_ && resource_metadata_db_.get()) |
| 208 blocking_task_runner_->DeleteSoon(FROM_HERE, | 208 blocking_task_runner_->DeleteSoon(FROM_HERE, |
| 209 resource_metadata_db_.release()); | 209 resource_metadata_db_.release()); |
| 210 } | 210 } |
| 211 | 211 |
| 212 scoped_ptr<DriveFile> DriveResourceMetadata::CreateDriveFile() { | 212 scoped_ptr<DriveFile> DriveResourceMetadata::CreateDriveFile() { |
| 213 return scoped_ptr<DriveFile>(new DriveFile(this)); | 213 return scoped_ptr<DriveFile>(new DriveFile(this)); |
| 214 } | 214 } |
| 215 | 215 |
| 216 scoped_ptr<DriveDirectory> DriveResourceMetadata::CreateDriveDirectory() { | 216 scoped_ptr<DriveDirectory> DriveResourceMetadata::CreateDriveDirectory() { |
| 217 return scoped_ptr<DriveDirectory>(new DriveDirectory(this)); | 217 return scoped_ptr<DriveDirectory>(new DriveDirectory(this)); |
| 218 } | 218 } |
| 219 | 219 |
| 220 void DriveResourceMetadata::InitializeRootEntry(const std::string& id) { | 220 void DriveResourceMetadata::InitializeRootEntry(const std::string& root_id) { |
| 221 DCHECK(root_->resource_id().empty()); | 221 root_ = CreateDriveDirectory().Pass(); |
| 222 root_->set_resource_id(id); | 222 root_->set_title(kDriveRootDirectory); |
| 223 root_->SetBaseNameFromTitle(); |
| 224 root_->set_resource_id(root_id); |
| 223 AddEntryToResourceMap(root_.get()); | 225 AddEntryToResourceMap(root_.get()); |
| 224 } | 226 } |
| 225 | 227 |
| 226 void DriveResourceMetadata::ClearRoot() { | 228 void DriveResourceMetadata::ClearRoot() { |
| 227 if (!root_.get()) | 229 if (!root_.get()) |
| 228 return; | 230 return; |
| 229 | 231 |
| 230 // The root is not yet initialized. | |
| 231 if (root_->resource_id().empty()) | |
| 232 return; | |
| 233 | |
| 234 // Note that children have a reference to root_, | 232 // Note that children have a reference to root_, |
| 235 // so we need to delete them here. | 233 // so we need to delete them here. |
| 236 root_->RemoveChildren(); | 234 root_->RemoveChildren(); |
| 237 RemoveEntryFromResourceMap(root_->resource_id()); | 235 RemoveEntryFromResourceMap(root_->resource_id()); |
| 238 DCHECK(resource_map_.empty()); | 236 DCHECK(resource_map_.empty()); |
| 239 // The resource_map_ should be empty here, but to make sure for non-Debug | |
| 240 // build. | |
| 241 resource_map_.clear(); | 237 resource_map_.clear(); |
| 242 root_.reset(); | 238 root_.reset(); |
| 243 } | 239 } |
| 244 | 240 |
| 245 void DriveResourceMetadata::AddEntryToDirectory( | 241 void DriveResourceMetadata::AddEntryToDirectory( |
| 246 const FilePath& directory_path, | 242 const FilePath& directory_path, |
| 247 scoped_ptr<google_apis::DocumentEntry> doc_entry, | 243 scoped_ptr<google_apis::DocumentEntry> doc_entry, |
| 248 const FileMoveCallback& callback) { | 244 const FileMoveCallback& callback) { |
| 249 DCHECK(!directory_path.empty()); | 245 DCHECK(!directory_path.empty()); |
| 250 DCHECK(!callback.is_null()); | 246 DCHECK(!callback.is_null()); |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 MoveEntryToDirectory(file_path, entry->parent()->GetFilePath(), callback); | 344 MoveEntryToDirectory(file_path, entry->parent()->GetFilePath(), callback); |
| 349 } | 345 } |
| 350 | 346 |
| 351 void DriveResourceMetadata::RemoveEntryFromParent( | 347 void DriveResourceMetadata::RemoveEntryFromParent( |
| 352 const std::string& resource_id, | 348 const std::string& resource_id, |
| 353 const FileMoveCallback& callback) { | 349 const FileMoveCallback& callback) { |
| 354 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 350 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 355 DCHECK(!callback.is_null()); | 351 DCHECK(!callback.is_null()); |
| 356 | 352 |
| 357 // Disallow deletion of root. | 353 // Disallow deletion of root. |
| 358 if (resource_id == root_->resource_id()) { | 354 if (resource_id == kDriveRootDirectoryResourceId) { |
| 359 PostFileMoveCallbackError(callback, DRIVE_FILE_ERROR_ACCESS_DENIED); | 355 PostFileMoveCallbackError(callback, DRIVE_FILE_ERROR_ACCESS_DENIED); |
| 360 return; | 356 return; |
| 361 } | 357 } |
| 362 | 358 |
| 363 DriveEntry* entry = GetEntryByResourceId(resource_id); | 359 DriveEntry* entry = GetEntryByResourceId(resource_id); |
| 364 if (!entry) { | 360 if (!entry) { |
| 365 PostFileMoveCallbackError(callback, DRIVE_FILE_ERROR_NOT_FOUND); | 361 PostFileMoveCallbackError(callback, DRIVE_FILE_ERROR_NOT_FOUND); |
| 366 return; | 362 return; |
| 367 } | 363 } |
| 368 | 364 |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 636 } | 632 } |
| 637 | 633 |
| 638 void DriveResourceMetadata::InitResourceMap( | 634 void DriveResourceMetadata::InitResourceMap( |
| 639 CreateDBParams* create_params, | 635 CreateDBParams* create_params, |
| 640 const FileOperationCallback& callback) { | 636 const FileOperationCallback& callback) { |
| 641 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 637 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 642 DCHECK(create_params); | 638 DCHECK(create_params); |
| 643 DCHECK(!resource_metadata_db_.get()); | 639 DCHECK(!resource_metadata_db_.get()); |
| 644 DCHECK(!callback.is_null()); | 640 DCHECK(!callback.is_null()); |
| 645 | 641 |
| 642 |
| 646 SerializedMap* serialized_resources = &create_params->serialized_resources; | 643 SerializedMap* serialized_resources = &create_params->serialized_resources; |
| 647 resource_metadata_db_ = create_params->db.Pass(); | 644 resource_metadata_db_ = create_params->db.Pass(); |
| 648 if (serialized_resources->empty()) { | 645 if (serialized_resources->empty()) { |
| 649 callback.Run(DRIVE_FILE_ERROR_NOT_FOUND); | 646 callback.Run(DRIVE_FILE_ERROR_NOT_FOUND); |
| 650 return; | 647 return; |
| 651 } | 648 } |
| 652 | 649 |
| 653 // Save root directory resource ID as ClearRoot() resets |root_|. | |
| 654 std::string saved_root_resource_id = root_->resource_id(); | |
| 655 ClearRoot(); | 650 ClearRoot(); |
| 656 | 651 |
| 657 // Version check. | 652 // Version check. |
| 658 int32 version = 0; | 653 int32 version = 0; |
| 659 SerializedMap::iterator iter = serialized_resources->find(kDBKeyVersion); | 654 SerializedMap::iterator iter = serialized_resources->find(kDBKeyVersion); |
| 660 if (iter == serialized_resources->end() || | 655 if (iter == serialized_resources->end() || |
| 661 !base::StringToInt(iter->second, &version) || | 656 !base::StringToInt(iter->second, &version) || |
| 662 version != kProtoVersion) { | 657 version != kProtoVersion) { |
| 663 callback.Run(DRIVE_FILE_ERROR_FAILED); | 658 callback.Run(DRIVE_FILE_ERROR_FAILED); |
| 664 return; | 659 return; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 706 resource_map.find(entry->parent_resource_id()); | 701 resource_map.find(entry->parent_resource_id()); |
| 707 if (parent_it != resource_map.end()) { | 702 if (parent_it != resource_map.end()) { |
| 708 DriveDirectory* parent = parent_it->second->AsDriveDirectory(); | 703 DriveDirectory* parent = parent_it->second->AsDriveDirectory(); |
| 709 if (parent) { | 704 if (parent) { |
| 710 DVLOG(1) << "Adding " << entry->resource_id() | 705 DVLOG(1) << "Adding " << entry->resource_id() |
| 711 << " as a child of " << parent->resource_id(); | 706 << " as a child of " << parent->resource_id(); |
| 712 parent->AddEntry(entry); | 707 parent->AddEntry(entry); |
| 713 } else { | 708 } else { |
| 714 NOTREACHED() << "Parent is not a directory " << parent->resource_id(); | 709 NOTREACHED() << "Parent is not a directory " << parent->resource_id(); |
| 715 } | 710 } |
| 716 } else if (entry->resource_id() == saved_root_resource_id) { | 711 } else if (entry->resource_id() == kDriveRootDirectoryResourceId) { |
| 717 root_.reset(entry->AsDriveDirectory()); | 712 root_.reset(entry->AsDriveDirectory()); |
| 718 DCHECK(root_.get()); | 713 DCHECK(root_.get()); |
| 719 AddEntryToResourceMap(root_.get()); | 714 AddEntryToResourceMap(root_.get()); |
| 720 } else { | 715 } else { |
| 721 NOTREACHED() << "Missing parent id " << entry->parent_resource_id() | 716 NOTREACHED() << "Missing parent id " << entry->parent_resource_id() |
| 722 << " for resource " << entry->resource_id(); | 717 << " for resource " << entry->resource_id(); |
| 723 } | 718 } |
| 724 } | 719 } |
| 725 | 720 |
| 726 if (!root_.get()) { | 721 if (!root_.get()) { |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 792 if (!proto.ParseFromString(serialized_proto)) | 787 if (!proto.ParseFromString(serialized_proto)) |
| 793 return false; | 788 return false; |
| 794 | 789 |
| 795 if (proto.version() != kProtoVersion) { | 790 if (proto.version() != kProtoVersion) { |
| 796 LOG(ERROR) << "Incompatible proto detected (incompatible version): " | 791 LOG(ERROR) << "Incompatible proto detected (incompatible version): " |
| 797 << proto.version(); | 792 << proto.version(); |
| 798 return false; | 793 return false; |
| 799 } | 794 } |
| 800 | 795 |
| 801 root_->FromProto(proto.drive_directory()); | 796 root_->FromProto(proto.drive_directory()); |
| 802 // Call AddEntryToResourceMap() instead of InitializeRootEntry() as the root | |
| 803 // resource is already set from proto. | |
| 804 AddEntryToResourceMap(root_.get()); | |
| 805 | 797 |
| 806 origin_ = INITIALIZED; | 798 origin_ = INITIALIZED; |
| 807 largest_changestamp_ = proto.largest_changestamp(); | 799 largest_changestamp_ = proto.largest_changestamp(); |
| 808 | 800 |
| 809 return true; | 801 return true; |
| 810 } | 802 } |
| 811 | 803 |
| 812 scoped_ptr<DriveEntry> DriveResourceMetadata::CreateDriveEntryFromProto( | 804 scoped_ptr<DriveEntry> DriveResourceMetadata::CreateDriveEntryFromProto( |
| 813 const DriveEntryProto& entry_proto) { | 805 const DriveEntryProto& entry_proto) { |
| 814 scoped_ptr<DriveEntry> entry; | 806 scoped_ptr<DriveEntry> entry; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 876 DCHECK(result.get()); | 868 DCHECK(result.get()); |
| 877 | 869 |
| 878 result->second.path = second_path; | 870 result->second.path = second_path; |
| 879 result->second.error = error; | 871 result->second.error = error; |
| 880 result->second.proto = entry_proto.Pass(); | 872 result->second.proto = entry_proto.Pass(); |
| 881 | 873 |
| 882 callback.Run(result.Pass()); | 874 callback.Run(result.Pass()); |
| 883 } | 875 } |
| 884 | 876 |
| 885 } // namespace drive | 877 } // namespace drive |
| OLD | NEW |