| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/fileapi_worker.h" | 5 #include "chrome/browser/chromeos/drive/fileapi_worker.h" |
| 6 | 6 |
| 7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/task_runner_util.h" | 9 #include "base/task_runner_util.h" |
| 10 #include "base/threading/sequenced_worker_pool.h" | 10 #include "base/threading/sequenced_worker_pool.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 return OPEN_FILE; | 37 return OPEN_FILE; |
| 38 | 38 |
| 39 if (file_flag & base::PLATFORM_FILE_CREATE) | 39 if (file_flag & base::PLATFORM_FILE_CREATE) |
| 40 return CREATE_FILE; | 40 return CREATE_FILE; |
| 41 | 41 |
| 42 DCHECK(file_flag & (base::PLATFORM_FILE_OPEN_ALWAYS | | 42 DCHECK(file_flag & (base::PLATFORM_FILE_OPEN_ALWAYS | |
| 43 base::PLATFORM_FILE_CREATE_ALWAYS)); | 43 base::PLATFORM_FILE_CREATE_ALWAYS)); |
| 44 return OPEN_OR_CREATE_FILE; | 44 return OPEN_OR_CREATE_FILE; |
| 45 } | 45 } |
| 46 | 46 |
| 47 // Runs |callback| with the PlatformFileError converted from |error|. | 47 // Runs |callback| with the File::Error converted from |error|. |
| 48 void RunStatusCallbackByFileError(const StatusCallback& callback, | 48 void RunStatusCallbackByFileError(const StatusCallback& callback, |
| 49 FileError error) { | 49 FileError error) { |
| 50 callback.Run(FileErrorToPlatformError(error)); | 50 callback.Run(FileErrorToBaseFileError(error)); |
| 51 } | 51 } |
| 52 | 52 |
| 53 // Runs |callback| with arguments converted from |error| and |entry|. | 53 // Runs |callback| with arguments converted from |error| and |entry|. |
| 54 void RunGetFileInfoCallback(const GetFileInfoCallback& callback, | 54 void RunGetFileInfoCallback(const GetFileInfoCallback& callback, |
| 55 FileError error, | 55 FileError error, |
| 56 scoped_ptr<ResourceEntry> entry) { | 56 scoped_ptr<ResourceEntry> entry) { |
| 57 if (error != FILE_ERROR_OK) { | 57 if (error != FILE_ERROR_OK) { |
| 58 callback.Run(FileErrorToPlatformError(error), base::PlatformFileInfo()); | 58 callback.Run(FileErrorToBaseFileError(error), base::File::Info()); |
| 59 return; | 59 return; |
| 60 } | 60 } |
| 61 | 61 |
| 62 DCHECK(entry); | 62 DCHECK(entry); |
| 63 base::PlatformFileInfo file_info; | 63 base::File::Info file_info; |
| 64 ConvertResourceEntryToPlatformFileInfo(*entry, &file_info); | 64 ConvertResourceEntryToFileInfo(*entry, &file_info); |
| 65 callback.Run(base::PLATFORM_FILE_OK, file_info); | 65 callback.Run(base::File::FILE_OK, file_info); |
| 66 } | 66 } |
| 67 | 67 |
| 68 // Runs |callback| with arguments converted from |error| and |resource_entries|. | 68 // Runs |callback| with arguments converted from |error| and |resource_entries|. |
| 69 void RunReadDirectoryCallback( | 69 void RunReadDirectoryCallback( |
| 70 const ReadDirectoryCallback& callback, | 70 const ReadDirectoryCallback& callback, |
| 71 FileError error, | 71 FileError error, |
| 72 scoped_ptr<ResourceEntryVector> resource_entries) { | 72 scoped_ptr<ResourceEntryVector> resource_entries) { |
| 73 if (error != FILE_ERROR_OK) { | 73 if (error != FILE_ERROR_OK) { |
| 74 callback.Run(FileErrorToPlatformError(error), | 74 callback.Run(FileErrorToBaseFileError(error), |
| 75 std::vector<fileapi::DirectoryEntry>(), false); | 75 std::vector<fileapi::DirectoryEntry>(), false); |
| 76 return; | 76 return; |
| 77 } | 77 } |
| 78 | 78 |
| 79 DCHECK(resource_entries); | 79 DCHECK(resource_entries); |
| 80 | 80 |
| 81 std::vector<fileapi::DirectoryEntry> entries; | 81 std::vector<fileapi::DirectoryEntry> entries; |
| 82 // Convert drive files to File API's directory entry. | 82 // Convert drive files to File API's directory entry. |
| 83 entries.reserve(resource_entries->size()); | 83 entries.reserve(resource_entries->size()); |
| 84 for (size_t i = 0; i < resource_entries->size(); ++i) { | 84 for (size_t i = 0; i < resource_entries->size(); ++i) { |
| 85 const ResourceEntry& resource_entry = (*resource_entries)[i]; | 85 const ResourceEntry& resource_entry = (*resource_entries)[i]; |
| 86 fileapi::DirectoryEntry entry; | 86 fileapi::DirectoryEntry entry; |
| 87 entry.name = resource_entry.base_name(); | 87 entry.name = resource_entry.base_name(); |
| 88 | 88 |
| 89 const PlatformFileInfoProto& file_info = resource_entry.file_info(); | 89 const PlatformFileInfoProto& file_info = resource_entry.file_info(); |
| 90 entry.is_directory = file_info.is_directory(); | 90 entry.is_directory = file_info.is_directory(); |
| 91 entry.size = file_info.size(); | 91 entry.size = file_info.size(); |
| 92 entry.last_modified_time = | 92 entry.last_modified_time = |
| 93 base::Time::FromInternalValue(file_info.last_modified()); | 93 base::Time::FromInternalValue(file_info.last_modified()); |
| 94 entries.push_back(entry); | 94 entries.push_back(entry); |
| 95 } | 95 } |
| 96 | 96 |
| 97 callback.Run(base::PLATFORM_FILE_OK, entries, false); | 97 callback.Run(base::File::FILE_OK, entries, false); |
| 98 } | 98 } |
| 99 | 99 |
| 100 // Runs |callback| with arguments based on |error|, |local_path| and |entry|. | 100 // Runs |callback| with arguments based on |error|, |local_path| and |entry|. |
| 101 void RunCreateSnapshotFileCallback(const CreateSnapshotFileCallback& callback, | 101 void RunCreateSnapshotFileCallback(const CreateSnapshotFileCallback& callback, |
| 102 FileError error, | 102 FileError error, |
| 103 const base::FilePath& local_path, | 103 const base::FilePath& local_path, |
| 104 scoped_ptr<ResourceEntry> entry) { | 104 scoped_ptr<ResourceEntry> entry) { |
| 105 if (error != FILE_ERROR_OK) { | 105 if (error != FILE_ERROR_OK) { |
| 106 callback.Run( | 106 callback.Run( |
| 107 FileErrorToPlatformError(error), | 107 FileErrorToBaseFileError(error), |
| 108 base::PlatformFileInfo(), base::FilePath(), | 108 base::File::Info(), base::FilePath(), |
| 109 webkit_blob::ScopedFile::ScopeOutPolicy()); | 109 webkit_blob::ScopedFile::ScopeOutPolicy()); |
| 110 return; | 110 return; |
| 111 } | 111 } |
| 112 | 112 |
| 113 DCHECK(entry); | 113 DCHECK(entry); |
| 114 | 114 |
| 115 // When reading file, last modified time specified in file info will be | 115 // 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. | 116 // 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 | 117 // 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 | 118 // 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 | 119 // 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. | 120 // value in the file info passed to the CreateSnapshot caller. |
| 121 base::PlatformFileInfo file_info; | 121 base::File::Info file_info; |
| 122 ConvertResourceEntryToPlatformFileInfo(*entry, &file_info); | 122 ConvertResourceEntryToFileInfo(*entry, &file_info); |
| 123 file_info.last_modified = base::Time(); | 123 file_info.last_modified = base::Time(); |
| 124 | 124 |
| 125 // If the file is a hosted document, a temporary JSON file is created to | 125 // If the file is a hosted document, a temporary JSON file is created to |
| 126 // represent the document. The JSON file is not cached and its lifetime | 126 // represent the document. The JSON file is not cached and its lifetime |
| 127 // is managed by ShareableFileReference. | 127 // is managed by ShareableFileReference. |
| 128 webkit_blob::ScopedFile::ScopeOutPolicy scope_out_policy = | 128 webkit_blob::ScopedFile::ScopeOutPolicy scope_out_policy = |
| 129 entry->file_specific_info().is_hosted_document() ? | 129 entry->file_specific_info().is_hosted_document() ? |
| 130 webkit_blob::ScopedFile::DELETE_ON_SCOPE_OUT : | 130 webkit_blob::ScopedFile::DELETE_ON_SCOPE_OUT : |
| 131 webkit_blob::ScopedFile::DONT_DELETE_ON_SCOPE_OUT; | 131 webkit_blob::ScopedFile::DONT_DELETE_ON_SCOPE_OUT; |
| 132 | 132 |
| 133 callback.Run(base::PLATFORM_FILE_OK, file_info, local_path, scope_out_policy); | 133 callback.Run(base::File::FILE_OK, file_info, local_path, scope_out_policy); |
| 134 } | 134 } |
| 135 | 135 |
| 136 // Runs |callback| with arguments converted from |error| and |local_path|. | 136 // Runs |callback| with arguments converted from |error| and |local_path|. |
| 137 void RunCreateWritableSnapshotFileCallback( | 137 void RunCreateWritableSnapshotFileCallback( |
| 138 const CreateWritableSnapshotFileCallback& callback, | 138 const CreateWritableSnapshotFileCallback& callback, |
| 139 FileError error, | 139 FileError error, |
| 140 const base::FilePath& local_path, | 140 const base::FilePath& local_path, |
| 141 const base::Closure& close_callback) { | 141 const base::Closure& close_callback) { |
| 142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 143 callback.Run(FileErrorToPlatformError(error), local_path, close_callback); | 143 callback.Run(FileErrorToBaseFileError(error), local_path, close_callback); |
| 144 } | 144 } |
| 145 | 145 |
| 146 // Runs |callback| with |error| and |platform_file|. | 146 // Runs |callback| with |error| and |platform_file|. |
| 147 void RunOpenFileCallback(const OpenFileCallback& callback, | 147 void RunOpenFileCallback(const OpenFileCallback& callback, |
| 148 const base::Closure& close_callback, | 148 const base::Closure& close_callback, |
| 149 base::PlatformFileError* error, | 149 base::File::Error* error, |
| 150 base::PlatformFile platform_file) { | 150 base::PlatformFile platform_file) { |
| 151 callback.Run(*error, platform_file, close_callback); | 151 callback.Run(*error, platform_file, close_callback); |
| 152 } | 152 } |
| 153 | 153 |
| 154 // Part of OpenFile(). Called after FileSystem::OpenFile(). | 154 // Part of OpenFile(). Called after FileSystem::OpenFile(). |
| 155 void OpenFileAfterFileSystemOpenFile(int file_flags, | 155 void OpenFileAfterFileSystemOpenFile(int file_flags, |
| 156 const OpenFileCallback& callback, | 156 const OpenFileCallback& callback, |
| 157 FileError error, | 157 FileError error, |
| 158 const base::FilePath& local_path, | 158 const base::FilePath& local_path, |
| 159 const base::Closure& close_callback) { | 159 const base::Closure& close_callback) { |
| 160 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 160 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 161 | 161 |
| 162 if (error != FILE_ERROR_OK) { | 162 if (error != FILE_ERROR_OK) { |
| 163 callback.Run(FileErrorToPlatformError(error), | 163 callback.Run(FileErrorToBaseFileError(error), |
| 164 base::kInvalidPlatformFileValue, | 164 base::kInvalidPlatformFileValue, |
| 165 base::Closure()); | 165 base::Closure()); |
| 166 return; | 166 return; |
| 167 } | 167 } |
| 168 | 168 |
| 169 // Here, the file should be at |local_path|, but there may be timing issue. | 169 // Here, the file should be at |local_path|, but there may be timing issue. |
| 170 // Because the file is managed by Drive file system, so, in order to avoid | 170 // Because the file is managed by Drive file system, so, in order to avoid |
| 171 // unexpected file creation, CREATE, OPEN_ALWAYS and CREATE_ALWAYS are | 171 // unexpected file creation, CREATE, OPEN_ALWAYS and CREATE_ALWAYS are |
| 172 // translated into OPEN or OPEN_TRUNCATED, here. Keep OPEN and OPEN_TRUNCATED | 172 // translated into OPEN or OPEN_TRUNCATED, here. Keep OPEN and OPEN_TRUNCATED |
| 173 // as is. | 173 // as is. |
| 174 if (file_flags & (base::PLATFORM_FILE_CREATE | | 174 if (file_flags & (base::PLATFORM_FILE_CREATE | |
| 175 base::PLATFORM_FILE_OPEN_ALWAYS)) { | 175 base::PLATFORM_FILE_OPEN_ALWAYS)) { |
| 176 file_flags &= ~(base::PLATFORM_FILE_CREATE | | 176 file_flags &= ~(base::PLATFORM_FILE_CREATE | |
| 177 base::PLATFORM_FILE_OPEN_ALWAYS); | 177 base::PLATFORM_FILE_OPEN_ALWAYS); |
| 178 file_flags |= base::PLATFORM_FILE_OPEN; | 178 file_flags |= base::PLATFORM_FILE_OPEN; |
| 179 } else if (file_flags & base::PLATFORM_FILE_CREATE_ALWAYS) { | 179 } else if (file_flags & base::PLATFORM_FILE_CREATE_ALWAYS) { |
| 180 file_flags &= ~base::PLATFORM_FILE_CREATE_ALWAYS; | 180 file_flags &= ~base::PLATFORM_FILE_CREATE_ALWAYS; |
| 181 file_flags |= base::PLATFORM_FILE_OPEN_TRUNCATED; | 181 file_flags |= base::PLATFORM_FILE_OPEN_TRUNCATED; |
| 182 } | 182 } |
| 183 | 183 |
| 184 // Cache file prepared for modification is available. Open it locally. | 184 // Cache file prepared for modification is available. Open it locally. |
| 185 base::PlatformFileError* result = | 185 // TODO(rvargas): Convert this to base::File. |
| 186 new base::PlatformFileError(base::PLATFORM_FILE_ERROR_FAILED); | 186 base::File::Error* result = |
| 187 new base::File::Error(base::File::FILE_ERROR_FAILED); |
| 187 bool posted = base::PostTaskAndReplyWithResult( | 188 bool posted = base::PostTaskAndReplyWithResult( |
| 188 BrowserThread::GetBlockingPool(), FROM_HERE, | 189 BrowserThread::GetBlockingPool(), FROM_HERE, |
| 189 base::Bind(&base::CreatePlatformFile, | 190 base::Bind(&base::CreatePlatformFile, |
| 190 local_path, file_flags, static_cast<bool*>(NULL), result), | 191 local_path, file_flags, static_cast<bool*>(NULL), |
| 192 reinterpret_cast<base::PlatformFileError*>(result)), |
| 191 base::Bind(&RunOpenFileCallback, | 193 base::Bind(&RunOpenFileCallback, |
| 192 callback, close_callback, base::Owned(result))); | 194 callback, close_callback, base::Owned(result))); |
| 193 DCHECK(posted); | 195 DCHECK(posted); |
| 194 } | 196 } |
| 195 | 197 |
| 196 } // namespace | 198 } // namespace |
| 197 | 199 |
| 198 void RunFileSystemCallback( | 200 void RunFileSystemCallback( |
| 199 const FileSystemGetter& file_system_getter, | 201 const FileSystemGetter& file_system_getter, |
| 200 const base::Callback<void(FileSystemInterface*)>& callback, | 202 const base::Callback<void(FileSystemInterface*)>& callback, |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 base::PLATFORM_FILE_OPEN_ALWAYS | | 332 base::PLATFORM_FILE_OPEN_ALWAYS | |
| 331 base::PLATFORM_FILE_CREATE_ALWAYS | | 333 base::PLATFORM_FILE_CREATE_ALWAYS | |
| 332 base::PLATFORM_FILE_OPEN_TRUNCATED | | 334 base::PLATFORM_FILE_OPEN_TRUNCATED | |
| 333 base::PLATFORM_FILE_READ | | 335 base::PLATFORM_FILE_READ | |
| 334 base::PLATFORM_FILE_WRITE | | 336 base::PLATFORM_FILE_WRITE | |
| 335 base::PLATFORM_FILE_WRITE_ATTRIBUTES | | 337 base::PLATFORM_FILE_WRITE_ATTRIBUTES | |
| 336 base::PLATFORM_FILE_APPEND)) { | 338 base::PLATFORM_FILE_APPEND)) { |
| 337 base::MessageLoopProxy::current()->PostTask( | 339 base::MessageLoopProxy::current()->PostTask( |
| 338 FROM_HERE, | 340 FROM_HERE, |
| 339 base::Bind(callback, | 341 base::Bind(callback, |
| 340 base::PLATFORM_FILE_ERROR_FAILED, | 342 base::File::FILE_ERROR_FAILED, |
| 341 base::kInvalidPlatformFileValue, | 343 base::kInvalidPlatformFileValue, |
| 342 base::Closure())); | 344 base::Closure())); |
| 343 return; | 345 return; |
| 344 } | 346 } |
| 345 | 347 |
| 346 file_system->OpenFile( | 348 file_system->OpenFile( |
| 347 file_path, GetOpenMode(file_flags), | 349 file_path, GetOpenMode(file_flags), |
| 348 std::string(), // no mime type; guess from file_path | 350 std::string(), // no mime type; guess from file_path |
| 349 base::Bind(&OpenFileAfterFileSystemOpenFile, file_flags, callback)); | 351 base::Bind(&OpenFileAfterFileSystemOpenFile, file_flags, callback)); |
| 350 } | 352 } |
| 351 | 353 |
| 352 void TouchFile(const base::FilePath& file_path, | 354 void TouchFile(const base::FilePath& file_path, |
| 353 const base::Time& last_access_time, | 355 const base::Time& last_access_time, |
| 354 const base::Time& last_modified_time, | 356 const base::Time& last_modified_time, |
| 355 const StatusCallback& callback, | 357 const StatusCallback& callback, |
| 356 FileSystemInterface* file_system) { | 358 FileSystemInterface* file_system) { |
| 357 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 359 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 358 file_system->TouchFile(file_path, last_access_time, last_modified_time, | 360 file_system->TouchFile(file_path, last_access_time, last_modified_time, |
| 359 base::Bind(&RunStatusCallbackByFileError, callback)); | 361 base::Bind(&RunStatusCallbackByFileError, callback)); |
| 360 | 362 |
| 361 } | 363 } |
| 362 | 364 |
| 363 } // namespace fileapi_internal | 365 } // namespace fileapi_internal |
| 364 } // namespace drive | 366 } // namespace drive |
| OLD | NEW |