| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/file_manager/filesystem_api_util.h" | 5 #include "chrome/browser/chromeos/file_manager/filesystem_api_util.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/files/file.h" | 8 #include "base/files/file.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "chrome/browser/chromeos/drive/file_errors.h" | 11 #include "chrome/browser/chromeos/drive/file_errors.h" |
| 12 #include "chrome/browser/chromeos/drive/file_system_interface.h" | 12 #include "chrome/browser/chromeos/drive/file_system_interface.h" |
| 13 #include "chrome/browser/chromeos/drive/file_system_util.h" | 13 #include "chrome/browser/chromeos/drive/file_system_util.h" |
| 14 #include "chrome/browser/chromeos/file_manager/app_id.h" | 14 #include "chrome/browser/chromeos/file_manager/app_id.h" |
| 15 #include "chrome/browser/chromeos/file_manager/fileapi_util.h" | 15 #include "chrome/browser/chromeos/file_manager/fileapi_util.h" |
| 16 #include "chrome/browser/chromeos/file_system_provider/mount_path_util.h" | 16 #include "chrome/browser/chromeos/file_system_provider/mount_path_util.h" |
| 17 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_inte
rface.h" | 17 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_inte
rface.h" |
| 18 #include "chrome/browser/extensions/extension_util.h" | 18 #include "chrome/browser/extensions/extension_util.h" |
| 19 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
| 20 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
| 21 #include "content/public/browser/storage_partition.h" | 21 #include "content/public/browser/storage_partition.h" |
| 22 #include "google_apis/drive/task_util.h" | 22 #include "google_apis/drive/task_util.h" |
| 23 #include "webkit/browser/fileapi/file_system_context.h" | 23 #include "storage/browser/fileapi/file_system_context.h" |
| 24 | 24 |
| 25 namespace file_manager { | 25 namespace file_manager { |
| 26 namespace util { | 26 namespace util { |
| 27 | 27 |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 // Helper function used to implement GetNonNativeLocalPathMimeType. It extracts | 30 // Helper function used to implement GetNonNativeLocalPathMimeType. It extracts |
| 31 // the mime type from the passed Drive resource entry. | 31 // the mime type from the passed Drive resource entry. |
| 32 void GetMimeTypeAfterGetResourceEntryForDrive( | 32 void GetMimeTypeAfterGetResourceEntryForDrive( |
| 33 const base::Callback<void(bool, const std::string&)>& callback, | 33 const base::Callback<void(bool, const std::string&)>& callback, |
| (...skipping 27 matching lines...) Expand all Loading... |
| 61 // File::Error, by regarding FILE_OK as the only successful value. | 61 // File::Error, by regarding FILE_OK as the only successful value. |
| 62 void BoolCallbackAsFileErrorCallback( | 62 void BoolCallbackAsFileErrorCallback( |
| 63 const base::Callback<void(bool)>& callback, | 63 const base::Callback<void(bool)>& callback, |
| 64 base::File::Error error) { | 64 base::File::Error error) { |
| 65 return callback.Run(error == base::File::FILE_OK); | 65 return callback.Run(error == base::File::FILE_OK); |
| 66 } | 66 } |
| 67 | 67 |
| 68 // Part of PrepareFileOnIOThread. It tries to create a new file if the given | 68 // Part of PrepareFileOnIOThread. It tries to create a new file if the given |
| 69 // |url| is not already inhabited. | 69 // |url| is not already inhabited. |
| 70 void PrepareFileAfterCheckExistOnIOThread( | 70 void PrepareFileAfterCheckExistOnIOThread( |
| 71 scoped_refptr<fileapi::FileSystemContext> file_system_context, | 71 scoped_refptr<storage::FileSystemContext> file_system_context, |
| 72 const fileapi::FileSystemURL& url, | 72 const storage::FileSystemURL& url, |
| 73 const fileapi::FileSystemOperation::StatusCallback& callback, | 73 const storage::FileSystemOperation::StatusCallback& callback, |
| 74 base::File::Error error) { | 74 base::File::Error error) { |
| 75 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 75 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 76 | 76 |
| 77 if (error != base::File::FILE_ERROR_NOT_FOUND) { | 77 if (error != base::File::FILE_ERROR_NOT_FOUND) { |
| 78 callback.Run(error); | 78 callback.Run(error); |
| 79 return; | 79 return; |
| 80 } | 80 } |
| 81 | 81 |
| 82 // Call with the second argument |exclusive| set to false, meaning that it | 82 // Call with the second argument |exclusive| set to false, meaning that it |
| 83 // is not an error even if the file already exists (it can happen if the file | 83 // is not an error even if the file already exists (it can happen if the file |
| 84 // is created after the previous FileExists call and before this CreateFile.) | 84 // is created after the previous FileExists call and before this CreateFile.) |
| 85 // | 85 // |
| 86 // Note that the preceding call to FileExists is necessary for handling | 86 // Note that the preceding call to FileExists is necessary for handling |
| 87 // read only filesystems that blindly rejects handling CreateFile(). | 87 // read only filesystems that blindly rejects handling CreateFile(). |
| 88 file_system_context->operation_runner()->CreateFile(url, false, callback); | 88 file_system_context->operation_runner()->CreateFile(url, false, callback); |
| 89 } | 89 } |
| 90 | 90 |
| 91 // Checks whether a file exists at the given |url|, and try creating it if it | 91 // Checks whether a file exists at the given |url|, and try creating it if it |
| 92 // is not already there. | 92 // is not already there. |
| 93 void PrepareFileOnIOThread( | 93 void PrepareFileOnIOThread( |
| 94 scoped_refptr<fileapi::FileSystemContext> file_system_context, | 94 scoped_refptr<storage::FileSystemContext> file_system_context, |
| 95 const fileapi::FileSystemURL& url, | 95 const storage::FileSystemURL& url, |
| 96 const base::Callback<void(bool)>& callback) { | 96 const base::Callback<void(bool)>& callback) { |
| 97 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 97 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 98 | 98 |
| 99 file_system_context->operation_runner()->FileExists( | 99 file_system_context->operation_runner()->FileExists( |
| 100 url, | 100 url, |
| 101 base::Bind(&PrepareFileAfterCheckExistOnIOThread, | 101 base::Bind(&PrepareFileAfterCheckExistOnIOThread, |
| 102 file_system_context, | 102 file_system_context, |
| 103 url, | 103 url, |
| 104 base::Bind(&BoolCallbackAsFileErrorCallback, callback))); | 104 base::Bind(&BoolCallbackAsFileErrorCallback, callback))); |
| 105 } | 105 } |
| 106 | 106 |
| 107 } // namespace | 107 } // namespace |
| 108 | 108 |
| 109 bool IsUnderNonNativeLocalPath(Profile* profile, | 109 bool IsUnderNonNativeLocalPath(Profile* profile, |
| 110 const base::FilePath& path) { | 110 const base::FilePath& path) { |
| 111 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 111 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 112 | 112 |
| 113 GURL url; | 113 GURL url; |
| 114 if (!util::ConvertAbsoluteFilePathToFileSystemUrl( | 114 if (!util::ConvertAbsoluteFilePathToFileSystemUrl( |
| 115 profile, path, kFileManagerAppId, &url)) { | 115 profile, path, kFileManagerAppId, &url)) { |
| 116 return false; | 116 return false; |
| 117 } | 117 } |
| 118 | 118 |
| 119 fileapi::FileSystemURL filesystem_url = | 119 storage::FileSystemURL filesystem_url = |
| 120 GetFileSystemContextForExtensionId(profile, | 120 GetFileSystemContextForExtensionId(profile, kFileManagerAppId) |
| 121 kFileManagerAppId)->CrackURL(url); | 121 ->CrackURL(url); |
| 122 if (!filesystem_url.is_valid()) | 122 if (!filesystem_url.is_valid()) |
| 123 return false; | 123 return false; |
| 124 | 124 |
| 125 switch (filesystem_url.type()) { | 125 switch (filesystem_url.type()) { |
| 126 case fileapi::kFileSystemTypeNativeLocal: | 126 case storage::kFileSystemTypeNativeLocal: |
| 127 case fileapi::kFileSystemTypeRestrictedNativeLocal: | 127 case storage::kFileSystemTypeRestrictedNativeLocal: |
| 128 return false; | 128 return false; |
| 129 default: | 129 default: |
| 130 // The path indeed corresponds to a mount point not associated with a | 130 // The path indeed corresponds to a mount point not associated with a |
| 131 // native local path. | 131 // native local path. |
| 132 return true; | 132 return true; |
| 133 } | 133 } |
| 134 } | 134 } |
| 135 | 135 |
| 136 void GetNonNativeLocalPathMimeType( | 136 void GetNonNativeLocalPathMimeType( |
| 137 Profile* profile, | 137 Profile* profile, |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 if (!util::ConvertAbsoluteFilePathToFileSystemUrl( | 217 if (!util::ConvertAbsoluteFilePathToFileSystemUrl( |
| 218 profile, path, kFileManagerAppId, &url)) { | 218 profile, path, kFileManagerAppId, &url)) { |
| 219 // Posting to the current thread, so that we always call back asynchronously | 219 // Posting to the current thread, so that we always call back asynchronously |
| 220 // independent from whether or not the operation succeeds. | 220 // independent from whether or not the operation succeeds. |
| 221 content::BrowserThread::PostTask(content::BrowserThread::UI, | 221 content::BrowserThread::PostTask(content::BrowserThread::UI, |
| 222 FROM_HERE, | 222 FROM_HERE, |
| 223 base::Bind(callback, false)); | 223 base::Bind(callback, false)); |
| 224 return; | 224 return; |
| 225 } | 225 } |
| 226 | 226 |
| 227 fileapi::FileSystemContext* const context = | 227 storage::FileSystemContext* const context = |
| 228 GetFileSystemContextForExtensionId(profile, kFileManagerAppId); | 228 GetFileSystemContextForExtensionId(profile, kFileManagerAppId); |
| 229 DCHECK(context); | 229 DCHECK(context); |
| 230 | 230 |
| 231 // Check the existence of a file using file system API implementation on | 231 // Check the existence of a file using file system API implementation on |
| 232 // behalf of the file manager app. We need to grant access beforehand. | 232 // behalf of the file manager app. We need to grant access beforehand. |
| 233 context->external_backend()->GrantFullAccessToExtension(kFileManagerAppId); | 233 context->external_backend()->GrantFullAccessToExtension(kFileManagerAppId); |
| 234 | 234 |
| 235 content::BrowserThread::PostTask( | 235 content::BrowserThread::PostTask( |
| 236 content::BrowserThread::IO, | 236 content::BrowserThread::IO, |
| 237 FROM_HERE, | 237 FROM_HERE, |
| 238 base::Bind(&PrepareFileOnIOThread, | 238 base::Bind(&PrepareFileOnIOThread, |
| 239 make_scoped_refptr(context), | 239 make_scoped_refptr(context), |
| 240 context->CrackURL(url), | 240 context->CrackURL(url), |
| 241 google_apis::CreateRelayCallback(callback))); | 241 google_apis::CreateRelayCallback(callback))); |
| 242 } | 242 } |
| 243 | 243 |
| 244 } // namespace util | 244 } // namespace util |
| 245 } // namespace file_manager | 245 } // namespace file_manager |
| OLD | NEW |