| 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/gdata/gdata_directory_service.h" | 5 #include "chrome/browser/chromeos/gdata/gdata_directory_service.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" |
| 11 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
| 12 #include "base/sequenced_task_runner.h" | 12 #include "base/sequenced_task_runner.h" |
| 13 #include "base/tracked_objects.h" | 13 #include "base/tracked_objects.h" |
| 14 #include "chrome/browser/chromeos/gdata/gdata.pb.h" | 14 #include "chrome/browser/chromeos/gdata/drive.pb.h" |
| 15 #include "chrome/browser/chromeos/gdata/gdata_files.h" | 15 #include "chrome/browser/chromeos/gdata/gdata_files.h" |
| 16 #include "chrome/browser/chromeos/gdata/gdata_util.h" | 16 #include "chrome/browser/chromeos/gdata/gdata_util.h" |
| 17 #include "chrome/browser/chromeos/gdata/gdata_wapi_parser.h" | 17 #include "chrome/browser/chromeos/gdata/gdata_wapi_parser.h" |
| 18 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 19 | 19 |
| 20 using content::BrowserThread; | 20 using content::BrowserThread; |
| 21 | 21 |
| 22 namespace gdata { | 22 namespace gdata { |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| (...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 GDataEntry* entry = GetEntryByResourceId(resource_id); | 328 GDataEntry* entry = GetEntryByResourceId(resource_id); |
| 329 callback.Run(entry); | 329 callback.Run(entry); |
| 330 } | 330 } |
| 331 | 331 |
| 332 void GDataDirectoryService::GetEntryInfoByResourceId( | 332 void GDataDirectoryService::GetEntryInfoByResourceId( |
| 333 const std::string& resource_id, | 333 const std::string& resource_id, |
| 334 const GetEntryInfoWithFilePathCallback& callback) { | 334 const GetEntryInfoWithFilePathCallback& callback) { |
| 335 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 335 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 336 DCHECK(!callback.is_null()); | 336 DCHECK(!callback.is_null()); |
| 337 | 337 |
| 338 scoped_ptr<GDataEntryProto> entry_proto; | 338 scoped_ptr<DriveEntryProto> entry_proto; |
| 339 GDataFileError error = GDATA_FILE_ERROR_FAILED; | 339 GDataFileError error = GDATA_FILE_ERROR_FAILED; |
| 340 FilePath drive_file_path; | 340 FilePath drive_file_path; |
| 341 | 341 |
| 342 GDataEntry* entry = GetEntryByResourceId(resource_id); | 342 GDataEntry* entry = GetEntryByResourceId(resource_id); |
| 343 if (entry) { | 343 if (entry) { |
| 344 entry_proto.reset(new GDataEntryProto); | 344 entry_proto.reset(new DriveEntryProto); |
| 345 entry->ToProtoFull(entry_proto.get()); | 345 entry->ToProtoFull(entry_proto.get()); |
| 346 error = GDATA_FILE_OK; | 346 error = GDATA_FILE_OK; |
| 347 drive_file_path = entry->GetFilePath(); | 347 drive_file_path = entry->GetFilePath(); |
| 348 } else { | 348 } else { |
| 349 error = GDATA_FILE_ERROR_NOT_FOUND; | 349 error = GDATA_FILE_ERROR_NOT_FOUND; |
| 350 } | 350 } |
| 351 | 351 |
| 352 base::MessageLoopProxy::current()->PostTask( | 352 base::MessageLoopProxy::current()->PostTask( |
| 353 FROM_HERE, | 353 FROM_HERE, |
| 354 base::Bind(callback, | 354 base::Bind(callback, |
| 355 error, | 355 error, |
| 356 drive_file_path, | 356 drive_file_path, |
| 357 base::Passed(&entry_proto))); | 357 base::Passed(&entry_proto))); |
| 358 } | 358 } |
| 359 | 359 |
| 360 void GDataDirectoryService::GetEntryInfoByPath( | 360 void GDataDirectoryService::GetEntryInfoByPath( |
| 361 const FilePath& path, | 361 const FilePath& path, |
| 362 const GetEntryInfoCallback& callback) { | 362 const GetEntryInfoCallback& callback) { |
| 363 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 363 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 364 DCHECK(!callback.is_null()); | 364 DCHECK(!callback.is_null()); |
| 365 | 365 |
| 366 scoped_ptr<GDataEntryProto> entry_proto; | 366 scoped_ptr<DriveEntryProto> entry_proto; |
| 367 GDataFileError error = GDATA_FILE_ERROR_FAILED; | 367 GDataFileError error = GDATA_FILE_ERROR_FAILED; |
| 368 | 368 |
| 369 GDataEntry* entry = FindEntryByPathSync(path); | 369 GDataEntry* entry = FindEntryByPathSync(path); |
| 370 if (entry) { | 370 if (entry) { |
| 371 entry_proto.reset(new GDataEntryProto); | 371 entry_proto.reset(new DriveEntryProto); |
| 372 entry->ToProtoFull(entry_proto.get()); | 372 entry->ToProtoFull(entry_proto.get()); |
| 373 error = GDATA_FILE_OK; | 373 error = GDATA_FILE_OK; |
| 374 } else { | 374 } else { |
| 375 error = GDATA_FILE_ERROR_NOT_FOUND; | 375 error = GDATA_FILE_ERROR_NOT_FOUND; |
| 376 } | 376 } |
| 377 | 377 |
| 378 base::MessageLoopProxy::current()->PostTask( | 378 base::MessageLoopProxy::current()->PostTask( |
| 379 FROM_HERE, | 379 FROM_HERE, |
| 380 base::Bind(callback, error, base::Passed(&entry_proto))); | 380 base::Bind(callback, error, base::Passed(&entry_proto))); |
| 381 } | 381 } |
| 382 | 382 |
| 383 void GDataDirectoryService::ReadDirectoryByPath( | 383 void GDataDirectoryService::ReadDirectoryByPath( |
| 384 const FilePath& path, | 384 const FilePath& path, |
| 385 const ReadDirectoryCallback& callback) { | 385 const ReadDirectoryCallback& callback) { |
| 386 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 386 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 387 DCHECK(!callback.is_null()); | 387 DCHECK(!callback.is_null()); |
| 388 | 388 |
| 389 scoped_ptr<GDataEntryProtoVector> entries; | 389 scoped_ptr<DriveEntryProtoVector> entries; |
| 390 GDataFileError error = GDATA_FILE_ERROR_FAILED; | 390 GDataFileError error = GDATA_FILE_ERROR_FAILED; |
| 391 | 391 |
| 392 GDataEntry* entry = FindEntryByPathSync(path); | 392 GDataEntry* entry = FindEntryByPathSync(path); |
| 393 if (entry && entry->AsGDataDirectory()) { | 393 if (entry && entry->AsGDataDirectory()) { |
| 394 entries = entry->AsGDataDirectory()->ToProtoVector(); | 394 entries = entry->AsGDataDirectory()->ToProtoVector(); |
| 395 error = GDATA_FILE_OK; | 395 error = GDATA_FILE_OK; |
| 396 } else if (entry && !entry->AsGDataDirectory()) { | 396 } else if (entry && !entry->AsGDataDirectory()) { |
| 397 error = GDATA_FILE_ERROR_NOT_A_DIRECTORY; | 397 error = GDATA_FILE_ERROR_NOT_A_DIRECTORY; |
| 398 } else { | 398 } else { |
| 399 error = GDATA_FILE_ERROR_NOT_FOUND; | 399 error = GDATA_FILE_ERROR_NOT_FOUND; |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 624 | 624 |
| 625 if (!blocking_task_runner_ || !directory_service_db_.get()) { | 625 if (!blocking_task_runner_ || !directory_service_db_.get()) { |
| 626 NOTREACHED(); | 626 NOTREACHED(); |
| 627 return; | 627 return; |
| 628 } | 628 } |
| 629 | 629 |
| 630 size_t serialized_size = 0; | 630 size_t serialized_size = 0; |
| 631 SerializedMap serialized_resources; | 631 SerializedMap serialized_resources; |
| 632 for (ResourceMap::const_iterator iter = resource_map_.begin(); | 632 for (ResourceMap::const_iterator iter = resource_map_.begin(); |
| 633 iter != resource_map_.end(); ++iter) { | 633 iter != resource_map_.end(); ++iter) { |
| 634 GDataEntryProto proto; | 634 DriveEntryProto proto; |
| 635 iter->second->ToProtoFull(&proto); | 635 iter->second->ToProtoFull(&proto); |
| 636 std::string serialized_string; | 636 std::string serialized_string; |
| 637 const bool ok = proto.SerializeToString(&serialized_string); | 637 const bool ok = proto.SerializeToString(&serialized_string); |
| 638 DCHECK(ok); | 638 DCHECK(ok); |
| 639 if (ok) { | 639 if (ok) { |
| 640 serialized_resources.insert( | 640 serialized_resources.insert( |
| 641 std::make_pair(std::string(kDBKeyResourceIdPrefix) + iter->first, | 641 std::make_pair(std::string(kDBKeyResourceIdPrefix) + iter->first, |
| 642 serialized_string)); | 642 serialized_string)); |
| 643 serialized_size += serialized_string.size(); | 643 serialized_size += serialized_string.size(); |
| 644 } | 644 } |
| 645 } | 645 } |
| 646 | 646 |
| 647 serialized_resources.insert(std::make_pair(kDBKeyVersion, | 647 serialized_resources.insert(std::make_pair(kDBKeyVersion, |
| 648 base::IntToString(kProtoVersion))); | 648 base::IntToString(kProtoVersion))); |
| 649 serialized_resources.insert(std::make_pair(kDBKeyLargestChangestamp, | 649 serialized_resources.insert(std::make_pair(kDBKeyLargestChangestamp, |
| 650 base::IntToString(largest_changestamp_))); | 650 base::IntToString(largest_changestamp_))); |
| 651 set_last_serialized(base::Time::Now()); | 651 set_last_serialized(base::Time::Now()); |
| 652 set_serialized_size(serialized_size); | 652 set_serialized_size(serialized_size); |
| 653 | 653 |
| 654 blocking_task_runner_->PostTask( | 654 blocking_task_runner_->PostTask( |
| 655 FROM_HERE, | 655 FROM_HERE, |
| 656 base::Bind(&ResourceMetadataDB::Save, | 656 base::Bind(&ResourceMetadataDB::Save, |
| 657 base::Unretained(directory_service_db_.get()), | 657 base::Unretained(directory_service_db_.get()), |
| 658 serialized_resources)); | 658 serialized_resources)); |
| 659 } | 659 } |
| 660 | 660 |
| 661 void GDataDirectoryService::SerializeToString( | 661 void GDataDirectoryService::SerializeToString( |
| 662 std::string* serialized_proto) const { | 662 std::string* serialized_proto) const { |
| 663 GDataRootDirectoryProto proto; | 663 DriveRootDirectoryProto proto; |
| 664 root_->ToProto(proto.mutable_gdata_directory()); | 664 root_->ToProto(proto.mutable_gdata_directory()); |
| 665 proto.set_largest_changestamp(largest_changestamp_); | 665 proto.set_largest_changestamp(largest_changestamp_); |
| 666 proto.set_version(kProtoVersion); | 666 proto.set_version(kProtoVersion); |
| 667 | 667 |
| 668 const bool ok = proto.SerializeToString(serialized_proto); | 668 const bool ok = proto.SerializeToString(serialized_proto); |
| 669 DCHECK(ok); | 669 DCHECK(ok); |
| 670 } | 670 } |
| 671 | 671 |
| 672 bool GDataDirectoryService::ParseFromString( | 672 bool GDataDirectoryService::ParseFromString( |
| 673 const std::string& serialized_proto) { | 673 const std::string& serialized_proto) { |
| 674 GDataRootDirectoryProto proto; | 674 DriveRootDirectoryProto proto; |
| 675 if (!proto.ParseFromString(serialized_proto)) | 675 if (!proto.ParseFromString(serialized_proto)) |
| 676 return false; | 676 return false; |
| 677 | 677 |
| 678 if (proto.version() != kProtoVersion) { | 678 if (proto.version() != kProtoVersion) { |
| 679 LOG(ERROR) << "Incompatible proto detected (incompatible version): " | 679 LOG(ERROR) << "Incompatible proto detected (incompatible version): " |
| 680 << proto.version(); | 680 << proto.version(); |
| 681 return false; | 681 return false; |
| 682 } | 682 } |
| 683 | 683 |
| 684 root_->FromProto(proto.gdata_directory()); | 684 root_->FromProto(proto.gdata_directory()); |
| 685 | 685 |
| 686 origin_ = FROM_CACHE; | 686 origin_ = FROM_CACHE; |
| 687 largest_changestamp_ = proto.largest_changestamp(); | 687 largest_changestamp_ = proto.largest_changestamp(); |
| 688 | 688 |
| 689 return true; | 689 return true; |
| 690 } | 690 } |
| 691 | 691 |
| 692 scoped_ptr<GDataEntry> GDataDirectoryService::FromProtoString( | 692 scoped_ptr<GDataEntry> GDataDirectoryService::FromProtoString( |
| 693 const std::string& serialized_proto) { | 693 const std::string& serialized_proto) { |
| 694 GDataEntryProto entry_proto; | 694 DriveEntryProto entry_proto; |
| 695 if (!entry_proto.ParseFromString(serialized_proto)) | 695 if (!entry_proto.ParseFromString(serialized_proto)) |
| 696 return scoped_ptr<GDataEntry>(); | 696 return scoped_ptr<GDataEntry>(); |
| 697 | 697 |
| 698 scoped_ptr<GDataEntry> entry; | 698 scoped_ptr<GDataEntry> entry; |
| 699 if (entry_proto.file_info().is_directory()) { | 699 if (entry_proto.file_info().is_directory()) { |
| 700 entry.reset(CreateGDataDirectory()); | 700 entry.reset(CreateGDataDirectory()); |
| 701 // Call GDataEntry::FromProto instead of GDataDirectory::FromProto because | 701 // Call GDataEntry::FromProto instead of GDataDirectory::FromProto because |
| 702 // the proto does not include children. | 702 // the proto does not include children. |
| 703 entry->FromProto(entry_proto); | 703 entry->FromProto(entry_proto); |
| 704 } else { | 704 } else { |
| 705 scoped_ptr<GDataFile> file(CreateGDataFile()); | 705 scoped_ptr<GDataFile> file(CreateGDataFile()); |
| 706 // Call GDataFile::FromProto. | 706 // Call GDataFile::FromProto. |
| 707 file->FromProto(entry_proto); | 707 file->FromProto(entry_proto); |
| 708 entry.reset(file.release()); | 708 entry.reset(file.release()); |
| 709 } | 709 } |
| 710 return entry.Pass(); | 710 return entry.Pass(); |
| 711 } | 711 } |
| 712 | 712 |
| 713 void GDataDirectoryService::GetEntryInfoPairByPathsAfterGetFirst( | 713 void GDataDirectoryService::GetEntryInfoPairByPathsAfterGetFirst( |
| 714 const FilePath& first_path, | 714 const FilePath& first_path, |
| 715 const FilePath& second_path, | 715 const FilePath& second_path, |
| 716 const GetEntryInfoPairCallback& callback, | 716 const GetEntryInfoPairCallback& callback, |
| 717 GDataFileError error, | 717 GDataFileError error, |
| 718 scoped_ptr<GDataEntryProto> entry_proto) { | 718 scoped_ptr<DriveEntryProto> entry_proto) { |
| 719 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 719 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 720 DCHECK(!callback.is_null()); | 720 DCHECK(!callback.is_null()); |
| 721 | 721 |
| 722 scoped_ptr<EntryInfoPairResult> result(new EntryInfoPairResult); | 722 scoped_ptr<EntryInfoPairResult> result(new EntryInfoPairResult); |
| 723 result->first.path = first_path; | 723 result->first.path = first_path; |
| 724 result->first.error = error; | 724 result->first.error = error; |
| 725 result->first.proto = entry_proto.Pass(); | 725 result->first.proto = entry_proto.Pass(); |
| 726 | 726 |
| 727 // If the first one is not found, don't continue. | 727 // If the first one is not found, don't continue. |
| 728 if (error != GDATA_FILE_OK) { | 728 if (error != GDATA_FILE_OK) { |
| 729 callback.Run(result.Pass()); | 729 callback.Run(result.Pass()); |
| 730 return; | 730 return; |
| 731 } | 731 } |
| 732 | 732 |
| 733 // Get the second entry. | 733 // Get the second entry. |
| 734 GetEntryInfoByPath( | 734 GetEntryInfoByPath( |
| 735 second_path, | 735 second_path, |
| 736 base::Bind(&GDataDirectoryService::GetEntryInfoPairByPathsAfterGetSecond, | 736 base::Bind(&GDataDirectoryService::GetEntryInfoPairByPathsAfterGetSecond, |
| 737 weak_ptr_factory_.GetWeakPtr(), | 737 weak_ptr_factory_.GetWeakPtr(), |
| 738 second_path, | 738 second_path, |
| 739 callback, | 739 callback, |
| 740 base::Passed(&result))); | 740 base::Passed(&result))); |
| 741 } | 741 } |
| 742 | 742 |
| 743 void GDataDirectoryService::GetEntryInfoPairByPathsAfterGetSecond( | 743 void GDataDirectoryService::GetEntryInfoPairByPathsAfterGetSecond( |
| 744 const FilePath& second_path, | 744 const FilePath& second_path, |
| 745 const GetEntryInfoPairCallback& callback, | 745 const GetEntryInfoPairCallback& callback, |
| 746 scoped_ptr<EntryInfoPairResult> result, | 746 scoped_ptr<EntryInfoPairResult> result, |
| 747 GDataFileError error, | 747 GDataFileError error, |
| 748 scoped_ptr<GDataEntryProto> entry_proto) { | 748 scoped_ptr<DriveEntryProto> entry_proto) { |
| 749 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 749 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 750 DCHECK(!callback.is_null()); | 750 DCHECK(!callback.is_null()); |
| 751 DCHECK(result.get()); | 751 DCHECK(result.get()); |
| 752 | 752 |
| 753 result->second.path = second_path; | 753 result->second.path = second_path; |
| 754 result->second.error = error; | 754 result->second.error = error; |
| 755 result->second.proto = entry_proto.Pass(); | 755 result->second.proto = entry_proto.Pass(); |
| 756 | 756 |
| 757 callback.Run(result.Pass()); | 757 callback.Run(result.Pass()); |
| 758 } | 758 } |
| 759 | 759 |
| 760 } // namespace gdata | 760 } // namespace gdata |
| OLD | NEW |