| 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_file_system_proxy.h" | 5 #include "chrome/browser/chromeos/gdata/gdata_file_system_proxy.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "webkit/fileapi/file_system_types.h" | 21 #include "webkit/fileapi/file_system_types.h" |
| 22 #include "webkit/fileapi/file_system_url.h" | 22 #include "webkit/fileapi/file_system_url.h" |
| 23 #include "webkit/fileapi/file_system_util.h" | 23 #include "webkit/fileapi/file_system_util.h" |
| 24 | 24 |
| 25 using base::MessageLoopProxy; | 25 using base::MessageLoopProxy; |
| 26 using content::BrowserThread; | 26 using content::BrowserThread; |
| 27 using fileapi::FileSystemURL; | 27 using fileapi::FileSystemURL; |
| 28 using fileapi::FileSystemOperationInterface; | 28 using fileapi::FileSystemOperationInterface; |
| 29 using webkit_blob::ShareableFileReference; | 29 using webkit_blob::ShareableFileReference; |
| 30 | 30 |
| 31 namespace gdata { |
| 32 |
| 31 namespace { | 33 namespace { |
| 32 | 34 |
| 33 const char kGDataRootDirectory[] = "drive"; | 35 const char kGDataRootDirectory[] = "drive"; |
| 34 const char kFeedField[] = "feed"; | 36 const char kFeedField[] = "feed"; |
| 35 | 37 |
| 36 // Helper function that creates platform file on blocking IO thread pool. | 38 // Helper function that creates platform file on blocking IO thread pool. |
| 37 void OpenPlatformFileOnIOPool(const FilePath& local_path, | 39 void OpenPlatformFileOnIOPool(const FilePath& local_path, |
| 38 int file_flags, | 40 int file_flags, |
| 39 base::PlatformFile* platform_file, | 41 base::PlatformFile* platform_file, |
| 40 base::PlatformFileError* open_error) { | 42 base::PlatformFileError* open_error) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 54 base::PlatformFileError* open_error) { | 56 base::PlatformFileError* open_error) { |
| 55 callback.Run(*open_error, *platform_file, peer_handle); | 57 callback.Run(*open_error, *platform_file, peer_handle); |
| 56 } | 58 } |
| 57 | 59 |
| 58 // Helper function to run OpenFileCallback from | 60 // Helper function to run OpenFileCallback from |
| 59 // GDataFileSystemProxy::OpenFile(). | 61 // GDataFileSystemProxy::OpenFile(). |
| 60 void OnGetFileByPathForOpen( | 62 void OnGetFileByPathForOpen( |
| 61 const FileSystemOperationInterface::OpenFileCallback& callback, | 63 const FileSystemOperationInterface::OpenFileCallback& callback, |
| 62 int file_flags, | 64 int file_flags, |
| 63 base::ProcessHandle peer_handle, | 65 base::ProcessHandle peer_handle, |
| 64 gdata::GDataFileError gdata_error, | 66 GDataFileError gdata_error, |
| 65 const FilePath& local_path, | 67 const FilePath& local_path, |
| 66 const std::string& unused_mime_type, | 68 const std::string& unused_mime_type, |
| 67 gdata::GDataFileType file_type) { | 69 GDataFileType file_type) { |
| 68 base::PlatformFileError error = | 70 base::PlatformFileError error = |
| 69 gdata::util::GDataFileErrorToPlatformError(gdata_error); | 71 util::GDataFileErrorToPlatformError(gdata_error); |
| 70 if (error != base::PLATFORM_FILE_OK) { | 72 if (error != base::PLATFORM_FILE_OK) { |
| 71 callback.Run(error, base::kInvalidPlatformFileValue, peer_handle); | 73 callback.Run(error, base::kInvalidPlatformFileValue, peer_handle); |
| 72 return; | 74 return; |
| 73 } | 75 } |
| 74 | 76 |
| 75 base::PlatformFile* platform_file = new base::PlatformFile( | 77 base::PlatformFile* platform_file = new base::PlatformFile( |
| 76 base::kInvalidPlatformFileValue); | 78 base::kInvalidPlatformFileValue); |
| 77 base::PlatformFileError* open_error = | 79 base::PlatformFileError* open_error = |
| 78 new base::PlatformFileError(base::PLATFORM_FILE_ERROR_FAILED); | 80 new base::PlatformFileError(base::PLATFORM_FILE_ERROR_FAILED); |
| 79 BrowserThread::GetBlockingPool()->PostTaskAndReply(FROM_HERE, | 81 BrowserThread::GetBlockingPool()->PostTaskAndReply(FROM_HERE, |
| 80 base::Bind(&OpenPlatformFileOnIOPool, | 82 base::Bind(&OpenPlatformFileOnIOPool, |
| 81 local_path, | 83 local_path, |
| 82 file_flags, | 84 file_flags, |
| 83 platform_file, | 85 platform_file, |
| 84 open_error), | 86 open_error), |
| 85 base::Bind(&OnPlatformFileOpened, | 87 base::Bind(&OnPlatformFileOpened, |
| 86 callback, | 88 callback, |
| 87 peer_handle, | 89 peer_handle, |
| 88 base::Owned(platform_file), | 90 base::Owned(platform_file), |
| 89 base::Owned(open_error))); | 91 base::Owned(open_error))); |
| 90 | 92 |
| 91 } | 93 } |
| 92 | 94 |
| 93 // Helper function to run SnapshotFileCallback from | 95 // Helper function to run SnapshotFileCallback from |
| 94 // GDataFileSystemProxy::CreateSnapshotFile(). | 96 // GDataFileSystemProxy::CreateSnapshotFile(). |
| 95 void CallSnapshotFileCallback( | 97 void CallSnapshotFileCallback( |
| 96 const FileSystemOperationInterface::SnapshotFileCallback& callback, | 98 const FileSystemOperationInterface::SnapshotFileCallback& callback, |
| 97 const base::PlatformFileInfo& file_info, | 99 const base::PlatformFileInfo& file_info, |
| 98 gdata::GDataFileError gdata_error, | 100 GDataFileError gdata_error, |
| 99 const FilePath& local_path, | 101 const FilePath& local_path, |
| 100 const std::string& unused_mime_type, | 102 const std::string& unused_mime_type, |
| 101 gdata::GDataFileType file_type) { | 103 GDataFileType file_type) { |
| 102 scoped_refptr<ShareableFileReference> file_ref; | 104 scoped_refptr<ShareableFileReference> file_ref; |
| 103 base::PlatformFileError error = | 105 base::PlatformFileError error = |
| 104 gdata::util::GDataFileErrorToPlatformError(gdata_error); | 106 util::GDataFileErrorToPlatformError(gdata_error); |
| 105 | 107 |
| 106 // If the file is a hosted document, a temporary JSON file is created to | 108 // If the file is a hosted document, a temporary JSON file is created to |
| 107 // represent the document. The JSON file is not cached and its lifetime | 109 // represent the document. The JSON file is not cached and its lifetime |
| 108 // is managed by ShareableFileReference. | 110 // is managed by ShareableFileReference. |
| 109 if (error == base::PLATFORM_FILE_OK && file_type == gdata::HOSTED_DOCUMENT) { | 111 if (error == base::PLATFORM_FILE_OK && file_type == HOSTED_DOCUMENT) { |
| 110 file_ref = ShareableFileReference::GetOrCreate( | 112 file_ref = ShareableFileReference::GetOrCreate( |
| 111 local_path, ShareableFileReference::DELETE_ON_FINAL_RELEASE, | 113 local_path, ShareableFileReference::DELETE_ON_FINAL_RELEASE, |
| 112 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); | 114 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); |
| 113 } | 115 } |
| 114 | 116 |
| 115 // When reading file, last modified time specified in file info will be | 117 // When reading file, last modified time specified in file info will be |
| 116 // compared to the last modified time of the local version of the drive file. | 118 // compared to the last modified time of the local version of the drive file. |
| 117 // Since those two values don't generally match (last modification time on the | 119 // Since those two values don't generally match (last modification time on the |
| 118 // drive server vs. last modification time of the local, downloaded file), so | 120 // drive server vs. last modification time of the local, downloaded file), so |
| 119 // we have to opt out from this check. We do this by unsetting last_modified | 121 // we have to opt out from this check. We do this by unsetting last_modified |
| 120 // value in the file info passed to the CreateSnapshot caller. | 122 // value in the file info passed to the CreateSnapshot caller. |
| 121 base::PlatformFileInfo final_file_info(file_info); | 123 base::PlatformFileInfo final_file_info(file_info); |
| 122 final_file_info.last_modified = base::Time(); | 124 final_file_info.last_modified = base::Time(); |
| 123 | 125 |
| 124 callback.Run(error, final_file_info, local_path, file_ref); | 126 callback.Run(error, final_file_info, local_path, file_ref); |
| 125 } | 127 } |
| 126 | 128 |
| 127 void OnClose(const FilePath& local_path, gdata::GDataFileError error_code) { | 129 void OnClose(const FilePath& local_path, GDataFileError error_code) { |
| 128 DVLOG(1) << "Closed: " << local_path.AsUTF8Unsafe() << ": " << error_code; | 130 DVLOG(1) << "Closed: " << local_path.AsUTF8Unsafe() << ": " << error_code; |
| 129 } | 131 } |
| 130 | 132 |
| 131 void DoTruncateOnFileThread( | 133 void DoTruncateOnFileThread( |
| 132 const FilePath& local_cache_path, | 134 const FilePath& local_cache_path, |
| 133 int64 length, | 135 int64 length, |
| 134 base::PlatformFileError* result) { | 136 base::PlatformFileError* result) { |
| 135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 136 | 138 |
| 137 base::PlatformFile file = base::CreatePlatformFile( | 139 base::PlatformFile file = base::CreatePlatformFile( |
| 138 local_cache_path, | 140 local_cache_path, |
| 139 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, | 141 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, |
| 140 NULL, | 142 NULL, |
| 141 result); | 143 result); |
| 142 if (*result == base::PLATFORM_FILE_OK) { | 144 if (*result == base::PLATFORM_FILE_OK) { |
| 143 DCHECK_NE(base::kInvalidPlatformFileValue, file); | 145 DCHECK_NE(base::kInvalidPlatformFileValue, file); |
| 144 if (!base::TruncatePlatformFile(file, length)) | 146 if (!base::TruncatePlatformFile(file, length)) |
| 145 *result = base::PLATFORM_FILE_ERROR_FAILED; | 147 *result = base::PLATFORM_FILE_ERROR_FAILED; |
| 146 base::ClosePlatformFile(file); | 148 base::ClosePlatformFile(file); |
| 147 } | 149 } |
| 148 } | 150 } |
| 149 | 151 |
| 150 void DidCloseFileForTruncate( | 152 void DidCloseFileForTruncate( |
| 151 const FileSystemOperationInterface::StatusCallback& callback, | 153 const FileSystemOperationInterface::StatusCallback& callback, |
| 152 base::PlatformFileError truncate_result, | 154 base::PlatformFileError truncate_result, |
| 153 gdata::GDataFileError close_result) { | 155 GDataFileError close_result) { |
| 154 // Reports the first error. | 156 // Reports the first error. |
| 155 callback.Run(truncate_result == base::PLATFORM_FILE_OK ? | 157 callback.Run(truncate_result == base::PLATFORM_FILE_OK ? |
| 156 gdata::util::GDataFileErrorToPlatformError(close_result) : | 158 util::GDataFileErrorToPlatformError(close_result) : |
| 157 truncate_result); | 159 truncate_result); |
| 158 } | 160 } |
| 159 | 161 |
| 160 } // namespace | 162 } // namespace |
| 161 | 163 |
| 162 namespace gdata { | |
| 163 | |
| 164 base::FileUtilProxy::Entry GDataEntryProtoToFileUtilProxyEntry( | 164 base::FileUtilProxy::Entry GDataEntryProtoToFileUtilProxyEntry( |
| 165 const GDataEntryProto& proto) { | 165 const GDataEntryProto& proto) { |
| 166 base::PlatformFileInfo file_info; | 166 base::PlatformFileInfo file_info; |
| 167 GDataEntry::ConvertProtoToPlatformFileInfo(proto.file_info(), &file_info); | 167 GDataEntry::ConvertProtoToPlatformFileInfo(proto.file_info(), &file_info); |
| 168 | 168 |
| 169 base::FileUtilProxy::Entry entry; | 169 base::FileUtilProxy::Entry entry; |
| 170 entry.name = proto.base_name(); | 170 entry.name = proto.base_name(); |
| 171 entry.is_directory = file_info.is_directory; | 171 entry.is_directory = file_info.is_directory; |
| 172 entry.size = file_info.size; | 172 entry.size = file_info.size; |
| 173 entry.last_modified_time = file_info.last_modified; | 173 entry.last_modified_time = file_info.last_modified; |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 | 354 |
| 355 void GDataFileSystemProxy::OnOpenFileForWriting( | 355 void GDataFileSystemProxy::OnOpenFileForWriting( |
| 356 int file_flags, | 356 int file_flags, |
| 357 base::ProcessHandle peer_handle, | 357 base::ProcessHandle peer_handle, |
| 358 const FileSystemOperationInterface::OpenFileCallback& callback, | 358 const FileSystemOperationInterface::OpenFileCallback& callback, |
| 359 GDataFileError gdata_error, | 359 GDataFileError gdata_error, |
| 360 const FilePath& local_cache_path) { | 360 const FilePath& local_cache_path) { |
| 361 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 361 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 362 | 362 |
| 363 base::PlatformFileError error = | 363 base::PlatformFileError error = |
| 364 gdata::util::GDataFileErrorToPlatformError(gdata_error); | 364 util::GDataFileErrorToPlatformError(gdata_error); |
| 365 | 365 |
| 366 if (error != base::PLATFORM_FILE_OK) { | 366 if (error != base::PLATFORM_FILE_OK) { |
| 367 callback.Run(error, base::kInvalidPlatformFileValue, peer_handle); | 367 callback.Run(error, base::kInvalidPlatformFileValue, peer_handle); |
| 368 return; | 368 return; |
| 369 } | 369 } |
| 370 | 370 |
| 371 // Cache file prepared for modification is available. Truncate it. | 371 // Cache file prepared for modification is available. Truncate it. |
| 372 // File operation must be done on FILE thread, so relay the operation. | 372 // File operation must be done on FILE thread, so relay the operation. |
| 373 base::PlatformFileError* result = | 373 base::PlatformFileError* result = |
| 374 new base::PlatformFileError(base::PLATFORM_FILE_ERROR_FAILED); | 374 new base::PlatformFileError(base::PLATFORM_FILE_ERROR_FAILED); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 389 } | 389 } |
| 390 | 390 |
| 391 void GDataFileSystemProxy::OnCreateFileForOpen( | 391 void GDataFileSystemProxy::OnCreateFileForOpen( |
| 392 const FilePath& file_path, | 392 const FilePath& file_path, |
| 393 int file_flags, | 393 int file_flags, |
| 394 base::ProcessHandle peer_handle, | 394 base::ProcessHandle peer_handle, |
| 395 const FileSystemOperationInterface::OpenFileCallback& callback, | 395 const FileSystemOperationInterface::OpenFileCallback& callback, |
| 396 GDataFileError gdata_error) { | 396 GDataFileError gdata_error) { |
| 397 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 397 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 398 base::PlatformFileError create_result = | 398 base::PlatformFileError create_result = |
| 399 gdata::util::GDataFileErrorToPlatformError(gdata_error); | 399 util::GDataFileErrorToPlatformError(gdata_error); |
| 400 | 400 |
| 401 if ((create_result == base::PLATFORM_FILE_OK) || | 401 if ((create_result == base::PLATFORM_FILE_OK) || |
| 402 ((create_result == base::PLATFORM_FILE_ERROR_EXISTS) && | 402 ((create_result == base::PLATFORM_FILE_ERROR_EXISTS) && |
| 403 (file_flags & base::PLATFORM_FILE_CREATE_ALWAYS))) { | 403 (file_flags & base::PLATFORM_FILE_CREATE_ALWAYS))) { |
| 404 // If we are trying to always create an existing file, then | 404 // If we are trying to always create an existing file, then |
| 405 // if it really exists open it as truncated. | 405 // if it really exists open it as truncated. |
| 406 file_flags &= ~base::PLATFORM_FILE_CREATE; | 406 file_flags &= ~base::PLATFORM_FILE_CREATE; |
| 407 file_flags &= ~base::PLATFORM_FILE_CREATE_ALWAYS; | 407 file_flags &= ~base::PLATFORM_FILE_CREATE_ALWAYS; |
| 408 file_flags |= base::PLATFORM_FILE_OPEN_TRUNCATED; | 408 file_flags |= base::PLATFORM_FILE_OPEN_TRUNCATED; |
| 409 } else { | 409 } else { |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 638 // what platform you're on. | 638 // what platform you're on. |
| 639 if (!url.is_valid() || url.type() != fileapi::kFileSystemTypeExternal) { | 639 if (!url.is_valid() || url.type() != fileapi::kFileSystemTypeExternal) { |
| 640 return false; | 640 return false; |
| 641 } | 641 } |
| 642 *file_path = url.path(); | 642 *file_path = url.path(); |
| 643 return true; | 643 return true; |
| 644 } | 644 } |
| 645 | 645 |
| 646 void GDataFileSystemProxy::OnStatusCallback( | 646 void GDataFileSystemProxy::OnStatusCallback( |
| 647 const fileapi::FileSystemOperationInterface::StatusCallback& callback, | 647 const fileapi::FileSystemOperationInterface::StatusCallback& callback, |
| 648 gdata::GDataFileError error) { | 648 GDataFileError error) { |
| 649 callback.Run(util::GDataFileErrorToPlatformError(error)); | 649 callback.Run(util::GDataFileErrorToPlatformError(error)); |
| 650 } | 650 } |
| 651 | 651 |
| 652 void GDataFileSystemProxy::OnGetMetadata( | 652 void GDataFileSystemProxy::OnGetMetadata( |
| 653 const FilePath& file_path, | 653 const FilePath& file_path, |
| 654 const FileSystemOperationInterface::GetMetadataCallback& callback, | 654 const FileSystemOperationInterface::GetMetadataCallback& callback, |
| 655 GDataFileError error, | 655 GDataFileError error, |
| 656 scoped_ptr<gdata::GDataEntryProto> entry_proto) { | 656 scoped_ptr<GDataEntryProto> entry_proto) { |
| 657 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 657 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 658 | 658 |
| 659 if (error != GDATA_FILE_OK) { | 659 if (error != GDATA_FILE_OK) { |
| 660 callback.Run(util::GDataFileErrorToPlatformError(error), | 660 callback.Run(util::GDataFileErrorToPlatformError(error), |
| 661 base::PlatformFileInfo(), | 661 base::PlatformFileInfo(), |
| 662 FilePath()); | 662 FilePath()); |
| 663 return; | 663 return; |
| 664 } | 664 } |
| 665 DCHECK(entry_proto.get()); | 665 DCHECK(entry_proto.get()); |
| 666 | 666 |
| 667 base::PlatformFileInfo file_info; | 667 base::PlatformFileInfo file_info; |
| 668 GDataEntry::ConvertProtoToPlatformFileInfo( | 668 GDataEntry::ConvertProtoToPlatformFileInfo( |
| 669 entry_proto->file_info(), | 669 entry_proto->file_info(), |
| 670 &file_info); | 670 &file_info); |
| 671 | 671 |
| 672 callback.Run(base::PLATFORM_FILE_OK, file_info, file_path); | 672 callback.Run(base::PLATFORM_FILE_OK, file_info, file_path); |
| 673 } | 673 } |
| 674 | 674 |
| 675 void GDataFileSystemProxy::OnReadDirectory( | 675 void GDataFileSystemProxy::OnReadDirectory( |
| 676 const FileSystemOperationInterface::ReadDirectoryCallback& | 676 const FileSystemOperationInterface::ReadDirectoryCallback& |
| 677 callback, | 677 callback, |
| 678 GDataFileError error, | 678 GDataFileError error, |
| 679 bool hide_hosted_documents, | 679 bool hide_hosted_documents, |
| 680 scoped_ptr<gdata::GDataEntryProtoVector> proto_entries) { | 680 scoped_ptr<GDataEntryProtoVector> proto_entries) { |
| 681 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 681 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 682 | 682 |
| 683 if (error != GDATA_FILE_OK) { | 683 if (error != GDATA_FILE_OK) { |
| 684 callback.Run(util::GDataFileErrorToPlatformError(error), | 684 callback.Run(util::GDataFileErrorToPlatformError(error), |
| 685 std::vector<base::FileUtilProxy::Entry>(), | 685 std::vector<base::FileUtilProxy::Entry>(), |
| 686 false); | 686 false); |
| 687 return; | 687 return; |
| 688 } | 688 } |
| 689 DCHECK(proto_entries.get()); | 689 DCHECK(proto_entries.get()); |
| 690 | 690 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 729 | 729 |
| 730 void GDataFileSystemProxy::CloseWritableSnapshotFile( | 730 void GDataFileSystemProxy::CloseWritableSnapshotFile( |
| 731 const FilePath& virtual_path, | 731 const FilePath& virtual_path, |
| 732 const FilePath& local_path) { | 732 const FilePath& local_path) { |
| 733 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 733 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 734 | 734 |
| 735 file_system_->CloseFile(virtual_path, base::Bind(&OnClose, virtual_path)); | 735 file_system_->CloseFile(virtual_path, base::Bind(&OnClose, virtual_path)); |
| 736 } | 736 } |
| 737 | 737 |
| 738 } // namespace gdata | 738 } // namespace gdata |
| OLD | NEW |