Chromium Code Reviews| 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 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 191 | 191 |
| 192 // DriveResourceMetadata class implementation. | 192 // DriveResourceMetadata class implementation. |
| 193 | 193 |
| 194 DriveResourceMetadata::DriveResourceMetadata() | 194 DriveResourceMetadata::DriveResourceMetadata() |
| 195 : blocking_task_runner_(NULL), | 195 : blocking_task_runner_(NULL), |
| 196 serialized_size_(0), | 196 serialized_size_(0), |
| 197 largest_changestamp_(0), | 197 largest_changestamp_(0), |
| 198 origin_(UNINITIALIZED), | 198 origin_(UNINITIALIZED), |
| 199 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 199 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
| 200 root_ = CreateDriveDirectory().Pass(); | 200 root_ = CreateDriveDirectory().Pass(); |
| 201 if (!google_apis::util::IsDriveV2ApiEnabled()) | 201 root_->set_title(kDriveRootDirectory); |
| 202 InitializeRootEntry(kDriveRootDirectoryResourceId); | 202 root_->SetBaseNameFromTitle(); |
| 203 } | 203 } |
| 204 | 204 |
| 205 DriveResourceMetadata::~DriveResourceMetadata() { | 205 DriveResourceMetadata::~DriveResourceMetadata() { |
| 206 ClearRoot(); | 206 ClearRoot(); |
| 207 | 207 |
| 208 // Ensure db is closed on the blocking pool. | 208 // Ensure db is closed on the blocking pool. |
| 209 if (blocking_task_runner_ && resource_metadata_db_.get()) | 209 if (blocking_task_runner_ && resource_metadata_db_.get()) |
| 210 blocking_task_runner_->DeleteSoon(FROM_HERE, | 210 blocking_task_runner_->DeleteSoon(FROM_HERE, |
| 211 resource_metadata_db_.release()); | 211 resource_metadata_db_.release()); |
| 212 } | 212 } |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 225 } | 225 } |
| 226 | 226 |
| 227 scoped_ptr<DriveFile> DriveResourceMetadata::CreateDriveFile() { | 227 scoped_ptr<DriveFile> DriveResourceMetadata::CreateDriveFile() { |
| 228 return scoped_ptr<DriveFile>(new DriveFile(this)); | 228 return scoped_ptr<DriveFile>(new DriveFile(this)); |
| 229 } | 229 } |
| 230 | 230 |
| 231 scoped_ptr<DriveDirectory> DriveResourceMetadata::CreateDriveDirectory() { | 231 scoped_ptr<DriveDirectory> DriveResourceMetadata::CreateDriveDirectory() { |
| 232 return scoped_ptr<DriveDirectory>(new DriveDirectory(this)); | 232 return scoped_ptr<DriveDirectory>(new DriveDirectory(this)); |
| 233 } | 233 } |
| 234 | 234 |
| 235 void DriveResourceMetadata::InitializeRootEntry(const std::string& root_id) { | 235 void DriveResourceMetadata::SetRootResourceId(const std::string& id) { |
| 236 root_ = CreateDriveDirectory().Pass(); | 236 DCHECK(root_->resource_id().empty()); |
| 237 root_->set_title(kDriveRootDirectory); | 237 root_->set_resource_id(id); |
| 238 root_->SetBaseNameFromTitle(); | |
| 239 root_->set_resource_id(root_id); | |
| 240 AddEntryToResourceMap(root_.get()); | 238 AddEntryToResourceMap(root_.get()); |
| 241 } | 239 } |
| 242 | 240 |
| 243 void DriveResourceMetadata::ClearRoot() { | 241 void DriveResourceMetadata::ClearRoot() { |
| 244 if (!root_.get()) | 242 if (!root_.get()) |
| 245 return; | 243 return; |
| 246 | 244 |
| 247 // Note that children have a reference to root_, | 245 // If root resource ID is not set, it means no children. |
| 248 // so we need to delete them here. | 246 if (!root_->resource_id().empty()) { |
|
satorux1
2012/10/26 06:14:36
Do we need to check? Can we just always run the sa
kochi
2012/10/26 07:35:42
No. RemoveEntryFromResourceMap DCHECK()'s empty re
satorux1
2012/10/26 07:52:44
I think we can change ClearRoot() to do something
| |
| 249 root_->RemoveChildren(); | 247 // Note that children have a reference to root_, |
| 250 RemoveEntryFromResourceMap(root_->resource_id()); | 248 // so we need to delete them here. |
| 251 DCHECK(resource_map_.empty()); | 249 root_->RemoveChildren(); |
| 252 resource_map_.clear(); | 250 RemoveEntryFromResourceMap(root_->resource_id()); |
| 251 DCHECK(resource_map_.empty()); | |
| 252 resource_map_.clear(); | |
| 253 } | |
| 253 root_.reset(); | 254 root_.reset(); |
| 254 } | 255 } |
| 255 | 256 |
| 256 void DriveResourceMetadata::AddEntryToDirectory( | 257 void DriveResourceMetadata::AddEntryToDirectory( |
| 257 const FilePath& directory_path, | 258 const FilePath& directory_path, |
| 258 scoped_ptr<google_apis::DocumentEntry> doc_entry, | 259 scoped_ptr<google_apis::DocumentEntry> doc_entry, |
| 259 const FileMoveCallback& callback) { | 260 const FileMoveCallback& callback) { |
| 260 DCHECK(!directory_path.empty()); | 261 DCHECK(!directory_path.empty()); |
| 261 DCHECK(!callback.is_null()); | 262 DCHECK(!callback.is_null()); |
| 262 | 263 |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 358 MoveEntryToDirectory(file_path, entry->parent()->GetFilePath(), callback); | 359 MoveEntryToDirectory(file_path, entry->parent()->GetFilePath(), callback); |
| 359 } | 360 } |
| 360 | 361 |
| 361 void DriveResourceMetadata::RemoveEntryFromParent( | 362 void DriveResourceMetadata::RemoveEntryFromParent( |
| 362 const std::string& resource_id, | 363 const std::string& resource_id, |
| 363 const FileMoveCallback& callback) { | 364 const FileMoveCallback& callback) { |
| 364 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 365 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 365 DCHECK(!callback.is_null()); | 366 DCHECK(!callback.is_null()); |
| 366 | 367 |
| 367 // Disallow deletion of root. | 368 // Disallow deletion of root. |
| 368 if (resource_id == kDriveRootDirectoryResourceId) { | 369 if (resource_id == root_->resource_id()) { |
| 369 PostFileMoveCallbackError(callback, DRIVE_FILE_ERROR_ACCESS_DENIED); | 370 PostFileMoveCallbackError(callback, DRIVE_FILE_ERROR_ACCESS_DENIED); |
| 370 return; | 371 return; |
| 371 } | 372 } |
| 372 | 373 |
| 373 DriveEntry* entry = GetEntryByResourceId(resource_id); | 374 DriveEntry* entry = GetEntryByResourceId(resource_id); |
| 374 if (!entry) { | 375 if (!entry) { |
| 375 PostFileMoveCallbackError(callback, DRIVE_FILE_ERROR_NOT_FOUND); | 376 PostFileMoveCallbackError(callback, DRIVE_FILE_ERROR_NOT_FOUND); |
| 376 return; | 377 return; |
| 377 } | 378 } |
| 378 | 379 |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 645 } | 646 } |
| 646 | 647 |
| 647 void DriveResourceMetadata::InitResourceMap( | 648 void DriveResourceMetadata::InitResourceMap( |
| 648 CreateDBParams* create_params, | 649 CreateDBParams* create_params, |
| 649 const FileOperationCallback& callback) { | 650 const FileOperationCallback& callback) { |
| 650 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 651 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 651 DCHECK(create_params); | 652 DCHECK(create_params); |
| 652 DCHECK(!resource_metadata_db_.get()); | 653 DCHECK(!resource_metadata_db_.get()); |
| 653 DCHECK(!callback.is_null()); | 654 DCHECK(!callback.is_null()); |
| 654 | 655 |
| 655 | |
| 656 SerializedMap* serialized_resources = &create_params->serialized_resources; | 656 SerializedMap* serialized_resources = &create_params->serialized_resources; |
| 657 resource_metadata_db_ = create_params->db.Pass(); | 657 resource_metadata_db_ = create_params->db.Pass(); |
| 658 if (serialized_resources->empty()) { | 658 if (serialized_resources->empty()) { |
| 659 origin_ = INITIALIZING; | 659 origin_ = INITIALIZING; |
| 660 callback.Run(DRIVE_FILE_ERROR_NOT_FOUND); | 660 callback.Run(DRIVE_FILE_ERROR_NOT_FOUND); |
| 661 return; | 661 return; |
| 662 } | 662 } |
| 663 | 663 |
| 664 // Save root directory resource ID as ClearRoot() resets |root_|. | |
| 665 std::string saved_root_resource_id = root_->resource_id(); | |
| 664 ClearRoot(); | 666 ClearRoot(); |
| 665 | 667 |
| 666 // Version check. | 668 // Version check. |
| 667 int32 version = 0; | 669 int32 version = 0; |
| 668 SerializedMap::iterator iter = serialized_resources->find(kDBKeyVersion); | 670 SerializedMap::iterator iter = serialized_resources->find(kDBKeyVersion); |
| 669 if (iter == serialized_resources->end() || | 671 if (iter == serialized_resources->end() || |
| 670 !base::StringToInt(iter->second, &version) || | 672 !base::StringToInt(iter->second, &version) || |
| 671 version != kProtoVersion) { | 673 version != kProtoVersion) { |
| 672 callback.Run(DRIVE_FILE_ERROR_FAILED); | 674 callback.Run(DRIVE_FILE_ERROR_FAILED); |
| 673 return; | 675 return; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 715 resource_map.find(entry->parent_resource_id()); | 717 resource_map.find(entry->parent_resource_id()); |
| 716 if (parent_it != resource_map.end()) { | 718 if (parent_it != resource_map.end()) { |
| 717 DriveDirectory* parent = parent_it->second->AsDriveDirectory(); | 719 DriveDirectory* parent = parent_it->second->AsDriveDirectory(); |
| 718 if (parent) { | 720 if (parent) { |
| 719 DVLOG(1) << "Adding " << entry->resource_id() | 721 DVLOG(1) << "Adding " << entry->resource_id() |
| 720 << " as a child of " << parent->resource_id(); | 722 << " as a child of " << parent->resource_id(); |
| 721 parent->AddEntry(entry); | 723 parent->AddEntry(entry); |
| 722 } else { | 724 } else { |
| 723 NOTREACHED() << "Parent is not a directory " << parent->resource_id(); | 725 NOTREACHED() << "Parent is not a directory " << parent->resource_id(); |
| 724 } | 726 } |
| 725 } else if (entry->resource_id() == kDriveRootDirectoryResourceId) { | 727 } else if (entry->resource_id() == saved_root_resource_id) { |
| 726 root_.reset(entry->AsDriveDirectory()); | 728 root_.reset(entry->AsDriveDirectory()); |
| 727 DCHECK(root_.get()); | 729 DCHECK(root_.get()); |
| 728 AddEntryToResourceMap(root_.get()); | 730 AddEntryToResourceMap(root_.get()); |
| 729 } else { | 731 } else { |
| 730 NOTREACHED() << "Missing parent id " << entry->parent_resource_id() | 732 NOTREACHED() << "Missing parent id " << entry->parent_resource_id() |
| 731 << " for resource " << entry->resource_id(); | 733 << " for resource " << entry->resource_id(); |
| 732 } | 734 } |
| 733 } | 735 } |
| 734 | 736 |
| 735 if (!root_.get()) { | 737 if (!root_.get()) { |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 801 if (!proto.ParseFromString(serialized_proto)) | 803 if (!proto.ParseFromString(serialized_proto)) |
| 802 return false; | 804 return false; |
| 803 | 805 |
| 804 if (proto.version() != kProtoVersion) { | 806 if (proto.version() != kProtoVersion) { |
| 805 LOG(ERROR) << "Incompatible proto detected (incompatible version): " | 807 LOG(ERROR) << "Incompatible proto detected (incompatible version): " |
| 806 << proto.version(); | 808 << proto.version(); |
| 807 return false; | 809 return false; |
| 808 } | 810 } |
| 809 | 811 |
| 810 root_->FromProto(proto.drive_directory()); | 812 root_->FromProto(proto.drive_directory()); |
| 813 // Root resource ID is already set from proto. | |
| 814 AddEntryToResourceMap(root_.get()); | |
| 811 | 815 |
| 812 origin_ = FROM_CACHE; | 816 origin_ = FROM_CACHE; |
| 813 largest_changestamp_ = proto.largest_changestamp(); | 817 largest_changestamp_ = proto.largest_changestamp(); |
| 814 | 818 |
| 815 return true; | 819 return true; |
| 816 } | 820 } |
| 817 | 821 |
| 818 scoped_ptr<DriveEntry> DriveResourceMetadata::CreateDriveEntryFromProto( | 822 scoped_ptr<DriveEntry> DriveResourceMetadata::CreateDriveEntryFromProto( |
| 819 const DriveEntryProto& entry_proto) { | 823 const DriveEntryProto& entry_proto) { |
| 820 scoped_ptr<DriveEntry> entry; | 824 scoped_ptr<DriveEntry> entry; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 882 DCHECK(result.get()); | 886 DCHECK(result.get()); |
| 883 | 887 |
| 884 result->second.path = second_path; | 888 result->second.path = second_path; |
| 885 result->second.error = error; | 889 result->second.error = error; |
| 886 result->second.proto = entry_proto.Pass(); | 890 result->second.proto = entry_proto.Pass(); |
| 887 | 891 |
| 888 callback.Run(result.Pass()); | 892 callback.Run(result.Pass()); |
| 889 } | 893 } |
| 890 | 894 |
| 891 } // namespace drive | 895 } // namespace drive |
| OLD | NEW |