| 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 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 | 180 |
| 181 // DriveResourceMetadata class implementation. | 181 // DriveResourceMetadata class implementation. |
| 182 | 182 |
| 183 DriveResourceMetadata::DriveResourceMetadata() | 183 DriveResourceMetadata::DriveResourceMetadata() |
| 184 : blocking_task_runner_(NULL), | 184 : blocking_task_runner_(NULL), |
| 185 serialized_size_(0), | 185 serialized_size_(0), |
| 186 largest_changestamp_(0), | 186 largest_changestamp_(0), |
| 187 initialized_(false), | 187 initialized_(false), |
| 188 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 188 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
| 189 root_ = CreateDriveDirectory().Pass(); | 189 root_ = CreateDriveDirectory().Pass(); |
| 190 if (!google_apis::util::IsDriveV2ApiEnabled()) | 190 root_->set_title(kDriveRootDirectory); |
| 191 InitializeRootEntry(kDriveRootDirectoryResourceId); | 191 root_->SetBaseNameFromTitle(); |
| 192 } | 192 } |
| 193 | 193 |
| 194 DriveResourceMetadata::~DriveResourceMetadata() { | 194 DriveResourceMetadata::~DriveResourceMetadata() { |
| 195 ClearRoot(); | 195 ClearRoot(); |
| 196 | 196 |
| 197 // Ensure db is closed on the blocking pool. | 197 // Ensure db is closed on the blocking pool. |
| 198 if (blocking_task_runner_ && resource_metadata_db_.get()) | 198 if (blocking_task_runner_ && resource_metadata_db_.get()) |
| 199 blocking_task_runner_->DeleteSoon(FROM_HERE, | 199 blocking_task_runner_->DeleteSoon(FROM_HERE, |
| 200 resource_metadata_db_.release()); | 200 resource_metadata_db_.release()); |
| 201 } | 201 } |
| 202 | 202 |
| 203 scoped_ptr<DriveFile> DriveResourceMetadata::CreateDriveFile() { | 203 scoped_ptr<DriveFile> DriveResourceMetadata::CreateDriveFile() { |
| 204 return scoped_ptr<DriveFile>(new DriveFile(this)); | 204 return scoped_ptr<DriveFile>(new DriveFile(this)); |
| 205 } | 205 } |
| 206 | 206 |
| 207 scoped_ptr<DriveDirectory> DriveResourceMetadata::CreateDriveDirectory() { | 207 scoped_ptr<DriveDirectory> DriveResourceMetadata::CreateDriveDirectory() { |
| 208 return scoped_ptr<DriveDirectory>(new DriveDirectory(this)); | 208 return scoped_ptr<DriveDirectory>(new DriveDirectory(this)); |
| 209 } | 209 } |
| 210 | 210 |
| 211 void DriveResourceMetadata::InitializeRootEntry(const std::string& root_id) { | 211 void DriveResourceMetadata::InitializeRootEntry(const std::string& id) { |
| 212 root_ = CreateDriveDirectory().Pass(); | 212 DCHECK(root_->resource_id().empty()); |
| 213 root_->set_title(kDriveRootDirectory); | 213 root_->set_resource_id(id); |
| 214 root_->SetBaseNameFromTitle(); | |
| 215 root_->set_resource_id(root_id); | |
| 216 AddEntryToResourceMap(root_.get()); | 214 AddEntryToResourceMap(root_.get()); |
| 217 } | 215 } |
| 218 | 216 |
| 219 void DriveResourceMetadata::ClearRoot() { | 217 void DriveResourceMetadata::ClearRoot() { |
| 220 if (!root_.get()) | 218 if (!root_.get()) |
| 221 return; | 219 return; |
| 222 | 220 |
| 221 // The root is not yet initialized. |
| 222 if (root_->resource_id().empty()) |
| 223 return; |
| 224 |
| 223 // Note that children have a reference to root_, | 225 // Note that children have a reference to root_, |
| 224 // so we need to delete them here. | 226 // so we need to delete them here. |
| 225 root_->RemoveChildren(); | 227 root_->RemoveChildren(); |
| 226 RemoveEntryFromResourceMap(root_->resource_id()); | 228 RemoveEntryFromResourceMap(root_->resource_id()); |
| 227 DCHECK(resource_map_.empty()); | 229 DCHECK(resource_map_.empty()); |
| 230 // The resource_map_ should be empty here, but to make sure for non-Debug |
| 231 // build. |
| 228 resource_map_.clear(); | 232 resource_map_.clear(); |
| 229 root_.reset(); | 233 root_.reset(); |
| 230 } | 234 } |
| 231 | 235 |
| 232 void DriveResourceMetadata::AddEntryToDirectory( | 236 void DriveResourceMetadata::AddEntryToDirectory( |
| 233 const FilePath& directory_path, | 237 const FilePath& directory_path, |
| 234 scoped_ptr<google_apis::DocumentEntry> doc_entry, | 238 scoped_ptr<google_apis::DocumentEntry> doc_entry, |
| 235 const FileMoveCallback& callback) { | 239 const FileMoveCallback& callback) { |
| 236 DCHECK(!directory_path.empty()); | 240 DCHECK(!directory_path.empty()); |
| 237 DCHECK(!callback.is_null()); | 241 DCHECK(!callback.is_null()); |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 335 MoveEntryToDirectory(file_path, entry->parent()->GetFilePath(), callback); | 339 MoveEntryToDirectory(file_path, entry->parent()->GetFilePath(), callback); |
| 336 } | 340 } |
| 337 | 341 |
| 338 void DriveResourceMetadata::RemoveEntryFromParent( | 342 void DriveResourceMetadata::RemoveEntryFromParent( |
| 339 const std::string& resource_id, | 343 const std::string& resource_id, |
| 340 const FileMoveCallback& callback) { | 344 const FileMoveCallback& callback) { |
| 341 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 345 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 342 DCHECK(!callback.is_null()); | 346 DCHECK(!callback.is_null()); |
| 343 | 347 |
| 344 // Disallow deletion of root. | 348 // Disallow deletion of root. |
| 345 if (resource_id == kDriveRootDirectoryResourceId) { | 349 if (resource_id == root_->resource_id()) { |
| 346 PostFileMoveCallbackError(callback, DRIVE_FILE_ERROR_ACCESS_DENIED); | 350 PostFileMoveCallbackError(callback, DRIVE_FILE_ERROR_ACCESS_DENIED); |
| 347 return; | 351 return; |
| 348 } | 352 } |
| 349 | 353 |
| 350 DriveEntry* entry = GetEntryByResourceId(resource_id); | 354 DriveEntry* entry = GetEntryByResourceId(resource_id); |
| 351 if (!entry) { | 355 if (!entry) { |
| 352 PostFileMoveCallbackError(callback, DRIVE_FILE_ERROR_NOT_FOUND); | 356 PostFileMoveCallbackError(callback, DRIVE_FILE_ERROR_NOT_FOUND); |
| 353 return; | 357 return; |
| 354 } | 358 } |
| 355 | 359 |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 623 } | 627 } |
| 624 | 628 |
| 625 void DriveResourceMetadata::InitResourceMap( | 629 void DriveResourceMetadata::InitResourceMap( |
| 626 CreateDBParams* create_params, | 630 CreateDBParams* create_params, |
| 627 const FileOperationCallback& callback) { | 631 const FileOperationCallback& callback) { |
| 628 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 632 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 629 DCHECK(create_params); | 633 DCHECK(create_params); |
| 630 DCHECK(!resource_metadata_db_.get()); | 634 DCHECK(!resource_metadata_db_.get()); |
| 631 DCHECK(!callback.is_null()); | 635 DCHECK(!callback.is_null()); |
| 632 | 636 |
| 633 | |
| 634 SerializedMap* serialized_resources = &create_params->serialized_resources; | 637 SerializedMap* serialized_resources = &create_params->serialized_resources; |
| 635 resource_metadata_db_ = create_params->db.Pass(); | 638 resource_metadata_db_ = create_params->db.Pass(); |
| 636 if (serialized_resources->empty()) { | 639 if (serialized_resources->empty()) { |
| 637 callback.Run(DRIVE_FILE_ERROR_NOT_FOUND); | 640 callback.Run(DRIVE_FILE_ERROR_NOT_FOUND); |
| 638 return; | 641 return; |
| 639 } | 642 } |
| 640 | 643 |
| 644 // Save root directory resource ID as ClearRoot() resets |root_|. |
| 645 std::string saved_root_resource_id = root_->resource_id(); |
| 641 ClearRoot(); | 646 ClearRoot(); |
| 642 | 647 |
| 643 // Version check. | 648 // Version check. |
| 644 int32 version = 0; | 649 int32 version = 0; |
| 645 SerializedMap::iterator iter = serialized_resources->find(kDBKeyVersion); | 650 SerializedMap::iterator iter = serialized_resources->find(kDBKeyVersion); |
| 646 if (iter == serialized_resources->end() || | 651 if (iter == serialized_resources->end() || |
| 647 !base::StringToInt(iter->second, &version) || | 652 !base::StringToInt(iter->second, &version) || |
| 648 version != kProtoVersion) { | 653 version != kProtoVersion) { |
| 649 callback.Run(DRIVE_FILE_ERROR_FAILED); | 654 callback.Run(DRIVE_FILE_ERROR_FAILED); |
| 650 return; | 655 return; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 692 resource_map.find(entry->parent_resource_id()); | 697 resource_map.find(entry->parent_resource_id()); |
| 693 if (parent_it != resource_map.end()) { | 698 if (parent_it != resource_map.end()) { |
| 694 DriveDirectory* parent = parent_it->second->AsDriveDirectory(); | 699 DriveDirectory* parent = parent_it->second->AsDriveDirectory(); |
| 695 if (parent) { | 700 if (parent) { |
| 696 DVLOG(1) << "Adding " << entry->resource_id() | 701 DVLOG(1) << "Adding " << entry->resource_id() |
| 697 << " as a child of " << parent->resource_id(); | 702 << " as a child of " << parent->resource_id(); |
| 698 parent->AddEntry(entry); | 703 parent->AddEntry(entry); |
| 699 } else { | 704 } else { |
| 700 NOTREACHED() << "Parent is not a directory " << parent->resource_id(); | 705 NOTREACHED() << "Parent is not a directory " << parent->resource_id(); |
| 701 } | 706 } |
| 702 } else if (entry->resource_id() == kDriveRootDirectoryResourceId) { | 707 } else if (entry->resource_id() == saved_root_resource_id) { |
| 703 root_.reset(entry->AsDriveDirectory()); | 708 root_.reset(entry->AsDriveDirectory()); |
| 704 DCHECK(root_.get()); | 709 DCHECK(root_.get()); |
| 705 AddEntryToResourceMap(root_.get()); | 710 AddEntryToResourceMap(root_.get()); |
| 706 } else { | 711 } else { |
| 707 NOTREACHED() << "Missing parent id " << entry->parent_resource_id() | 712 NOTREACHED() << "Missing parent id " << entry->parent_resource_id() |
| 708 << " for resource " << entry->resource_id(); | 713 << " for resource " << entry->resource_id(); |
| 709 } | 714 } |
| 710 } | 715 } |
| 711 | 716 |
| 712 if (!root_.get()) { | 717 if (!root_.get()) { |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 778 if (!proto.ParseFromString(serialized_proto)) | 783 if (!proto.ParseFromString(serialized_proto)) |
| 779 return false; | 784 return false; |
| 780 | 785 |
| 781 if (proto.version() != kProtoVersion) { | 786 if (proto.version() != kProtoVersion) { |
| 782 LOG(ERROR) << "Incompatible proto detected (incompatible version): " | 787 LOG(ERROR) << "Incompatible proto detected (incompatible version): " |
| 783 << proto.version(); | 788 << proto.version(); |
| 784 return false; | 789 return false; |
| 785 } | 790 } |
| 786 | 791 |
| 787 root_->FromProto(proto.drive_directory()); | 792 root_->FromProto(proto.drive_directory()); |
| 793 // Call AddEntryToResourceMap() instead of InitializeRootEntry() as the root |
| 794 // resource is already set from proto. |
| 795 AddEntryToResourceMap(root_.get()); |
| 788 | 796 |
| 789 initialized_ = true; | 797 initialized_ = true; |
| 790 largest_changestamp_ = proto.largest_changestamp(); | 798 largest_changestamp_ = proto.largest_changestamp(); |
| 791 | 799 |
| 792 return true; | 800 return true; |
| 793 } | 801 } |
| 794 | 802 |
| 795 scoped_ptr<DriveEntry> DriveResourceMetadata::CreateDriveEntryFromProto( | 803 scoped_ptr<DriveEntry> DriveResourceMetadata::CreateDriveEntryFromProto( |
| 796 const DriveEntryProto& entry_proto) { | 804 const DriveEntryProto& entry_proto) { |
| 797 scoped_ptr<DriveEntry> entry; | 805 scoped_ptr<DriveEntry> entry; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 859 DCHECK(result.get()); | 867 DCHECK(result.get()); |
| 860 | 868 |
| 861 result->second.path = second_path; | 869 result->second.path = second_path; |
| 862 result->second.error = error; | 870 result->second.error = error; |
| 863 result->second.proto = entry_proto.Pass(); | 871 result->second.proto = entry_proto.Pass(); |
| 864 | 872 |
| 865 callback.Run(result.Pass()); | 873 callback.Run(result.Pass()); |
| 866 } | 874 } |
| 867 | 875 |
| 868 } // namespace drive | 876 } // namespace drive |
| OLD | NEW |