| 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/file_manager/fileapi_util.h" | 5 #include "chrome/browser/chromeos/file_manager/fileapi_util.h" |
| 6 | 6 |
| 7 #include "base/files/file.h" | 7 #include "base/files/file.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "chrome/browser/chromeos/drive/file_system_util.h" | 9 #include "chrome/browser/chromeos/drive/file_system_util.h" |
| 10 #include "chrome/browser/chromeos/file_manager/app_id.h" | 10 #include "chrome/browser/chromeos/file_manager/app_id.h" |
| 11 #include "chrome/browser/extensions/extension_util.h" | 11 #include "chrome/browser/extensions/extension_util.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 14 #include "content/public/browser/render_view_host.h" | 14 #include "content/public/browser/render_view_host.h" |
| 15 #include "content/public/browser/site_instance.h" | 15 #include "content/public/browser/site_instance.h" |
| 16 #include "content/public/browser/storage_partition.h" | 16 #include "content/public/browser/storage_partition.h" |
| 17 #include "extensions/common/extension.h" | 17 #include "extensions/common/extension.h" |
| 18 #include "google_apis/drive/task_util.h" | 18 #include "google_apis/drive/task_util.h" |
| 19 #include "net/base/escape.h" | 19 #include "net/base/escape.h" |
| 20 #include "url/gurl.h" | 20 #include "url/gurl.h" |
| 21 #include "webkit/browser/fileapi/file_system_context.h" | 21 #include "storage/browser/fileapi/file_system_context.h" |
| 22 #include "webkit/browser/fileapi/open_file_system_mode.h" | 22 #include "storage/browser/fileapi/open_file_system_mode.h" |
| 23 #include "webkit/common/fileapi/file_system_util.h" | 23 #include "storage/common/fileapi/file_system_util.h" |
| 24 | 24 |
| 25 using content::BrowserThread; | 25 using content::BrowserThread; |
| 26 | 26 |
| 27 namespace file_manager { | 27 namespace file_manager { |
| 28 namespace util { | 28 namespace util { |
| 29 | 29 |
| 30 namespace { | 30 namespace { |
| 31 | 31 |
| 32 GURL ConvertRelativeFilePathToFileSystemUrl(const base::FilePath& relative_path, | 32 GURL ConvertRelativeFilePathToFileSystemUrl(const base::FilePath& relative_path, |
| 33 const std::string& extension_id) { | 33 const std::string& extension_id) { |
| 34 GURL base_url = fileapi::GetFileSystemRootURI( | 34 GURL base_url = storage::GetFileSystemRootURI( |
| 35 extensions::Extension::GetBaseURLFromExtensionId(extension_id), | 35 extensions::Extension::GetBaseURLFromExtensionId(extension_id), |
| 36 fileapi::kFileSystemTypeExternal); | 36 storage::kFileSystemTypeExternal); |
| 37 return GURL(base_url.spec() + | 37 return GURL(base_url.spec() + |
| 38 net::EscapeUrlEncodedData(relative_path.AsUTF8Unsafe(), | 38 net::EscapeUrlEncodedData(relative_path.AsUTF8Unsafe(), |
| 39 false)); // Space to %20 instead of +. | 39 false)); // Space to %20 instead of +. |
| 40 } | 40 } |
| 41 | 41 |
| 42 // Creates an ErrorDefinition with an error set to |error|. | 42 // Creates an ErrorDefinition with an error set to |error|. |
| 43 EntryDefinition CreateEntryDefinitionWithError(base::File::Error error) { | 43 EntryDefinition CreateEntryDefinitionWithError(base::File::Error error) { |
| 44 EntryDefinition result; | 44 EntryDefinition result; |
| 45 result.error = error; | 45 result.error = error; |
| 46 return result; | 46 return result; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 67 // the virtual path to an URL, and calls OnResolvedURL(). In case of error | 67 // the virtual path to an URL, and calls OnResolvedURL(). In case of error |
| 68 // calls OnIteratorConverted with an error entry definition. | 68 // calls OnIteratorConverted with an error entry definition. |
| 69 void ConvertNextIterator(scoped_ptr<FileDefinitionListConverter> self_deleter, | 69 void ConvertNextIterator(scoped_ptr<FileDefinitionListConverter> self_deleter, |
| 70 FileDefinitionList::const_iterator iterator); | 70 FileDefinitionList::const_iterator iterator); |
| 71 | 71 |
| 72 // Creates an entry definition from the URL as well as the file definition. | 72 // Creates an entry definition from the URL as well as the file definition. |
| 73 // Then, calls OnIteratorConverted with the created entry definition. | 73 // Then, calls OnIteratorConverted with the created entry definition. |
| 74 void OnResolvedURL(scoped_ptr<FileDefinitionListConverter> self_deleter, | 74 void OnResolvedURL(scoped_ptr<FileDefinitionListConverter> self_deleter, |
| 75 FileDefinitionList::const_iterator iterator, | 75 FileDefinitionList::const_iterator iterator, |
| 76 base::File::Error error, | 76 base::File::Error error, |
| 77 const fileapi::FileSystemInfo& info, | 77 const storage::FileSystemInfo& info, |
| 78 const base::FilePath& file_path, | 78 const base::FilePath& file_path, |
| 79 fileapi::FileSystemContext::ResolvedEntryType type); | 79 storage::FileSystemContext::ResolvedEntryType type); |
| 80 | 80 |
| 81 // Called when the iterator is converted. Adds the |entry_definition| to | 81 // Called when the iterator is converted. Adds the |entry_definition| to |
| 82 // |results_| and calls ConvertNextIterator() for the next element. | 82 // |results_| and calls ConvertNextIterator() for the next element. |
| 83 void OnIteratorConverted(scoped_ptr<FileDefinitionListConverter> self_deleter, | 83 void OnIteratorConverted(scoped_ptr<FileDefinitionListConverter> self_deleter, |
| 84 FileDefinitionList::const_iterator iterator, | 84 FileDefinitionList::const_iterator iterator, |
| 85 const EntryDefinition& entry_definition); | 85 const EntryDefinition& entry_definition); |
| 86 | 86 |
| 87 scoped_refptr<fileapi::FileSystemContext> file_system_context_; | 87 scoped_refptr<storage::FileSystemContext> file_system_context_; |
| 88 const std::string extension_id_; | 88 const std::string extension_id_; |
| 89 const FileDefinitionList file_definition_list_; | 89 const FileDefinitionList file_definition_list_; |
| 90 const EntryDefinitionListCallback callback_; | 90 const EntryDefinitionListCallback callback_; |
| 91 scoped_ptr<EntryDefinitionList> result_; | 91 scoped_ptr<EntryDefinitionList> result_; |
| 92 }; | 92 }; |
| 93 | 93 |
| 94 FileDefinitionListConverter::FileDefinitionListConverter( | 94 FileDefinitionListConverter::FileDefinitionListConverter( |
| 95 Profile* profile, | 95 Profile* profile, |
| 96 const std::string& extension_id, | 96 const std::string& extension_id, |
| 97 const FileDefinitionList& file_definition_list, | 97 const FileDefinitionList& file_definition_list, |
| (...skipping 30 matching lines...) Expand all Loading... |
| 128 } | 128 } |
| 129 | 129 |
| 130 if (!file_system_context_.get()) { | 130 if (!file_system_context_.get()) { |
| 131 OnIteratorConverted(self_deleter.Pass(), | 131 OnIteratorConverted(self_deleter.Pass(), |
| 132 iterator, | 132 iterator, |
| 133 CreateEntryDefinitionWithError( | 133 CreateEntryDefinitionWithError( |
| 134 base::File::FILE_ERROR_INVALID_OPERATION)); | 134 base::File::FILE_ERROR_INVALID_OPERATION)); |
| 135 return; | 135 return; |
| 136 } | 136 } |
| 137 | 137 |
| 138 fileapi::FileSystemURL url = file_system_context_->CreateCrackedFileSystemURL( | 138 storage::FileSystemURL url = file_system_context_->CreateCrackedFileSystemURL( |
| 139 extensions::Extension::GetBaseURLFromExtensionId(extension_id_), | 139 extensions::Extension::GetBaseURLFromExtensionId(extension_id_), |
| 140 fileapi::kFileSystemTypeExternal, | 140 storage::kFileSystemTypeExternal, |
| 141 iterator->virtual_path); | 141 iterator->virtual_path); |
| 142 DCHECK(url.is_valid()); | 142 DCHECK(url.is_valid()); |
| 143 | 143 |
| 144 // The converter object will be deleted if the callback is not called because | 144 // The converter object will be deleted if the callback is not called because |
| 145 // of shutdown during ResolveURL(). | 145 // of shutdown during ResolveURL(). |
| 146 file_system_context_->ResolveURL( | 146 file_system_context_->ResolveURL( |
| 147 url, | 147 url, |
| 148 base::Bind(&FileDefinitionListConverter::OnResolvedURL, | 148 base::Bind(&FileDefinitionListConverter::OnResolvedURL, |
| 149 base::Unretained(this), | 149 base::Unretained(this), |
| 150 base::Passed(&self_deleter), | 150 base::Passed(&self_deleter), |
| 151 iterator)); | 151 iterator)); |
| 152 } | 152 } |
| 153 | 153 |
| 154 void FileDefinitionListConverter::OnResolvedURL( | 154 void FileDefinitionListConverter::OnResolvedURL( |
| 155 scoped_ptr<FileDefinitionListConverter> self_deleter, | 155 scoped_ptr<FileDefinitionListConverter> self_deleter, |
| 156 FileDefinitionList::const_iterator iterator, | 156 FileDefinitionList::const_iterator iterator, |
| 157 base::File::Error error, | 157 base::File::Error error, |
| 158 const fileapi::FileSystemInfo& info, | 158 const storage::FileSystemInfo& info, |
| 159 const base::FilePath& file_path, | 159 const base::FilePath& file_path, |
| 160 fileapi::FileSystemContext::ResolvedEntryType type) { | 160 storage::FileSystemContext::ResolvedEntryType type) { |
| 161 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 161 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 162 | 162 |
| 163 if (error != base::File::FILE_OK) { | 163 if (error != base::File::FILE_OK) { |
| 164 OnIteratorConverted(self_deleter.Pass(), | 164 OnIteratorConverted(self_deleter.Pass(), |
| 165 iterator, | 165 iterator, |
| 166 CreateEntryDefinitionWithError(error)); | 166 CreateEntryDefinitionWithError(error)); |
| 167 return; | 167 return; |
| 168 } | 168 } |
| 169 | 169 |
| 170 EntryDefinition entry_definition; | 170 EntryDefinition entry_definition; |
| 171 entry_definition.file_system_root_url = info.root_url.spec(); | 171 entry_definition.file_system_root_url = info.root_url.spec(); |
| 172 entry_definition.file_system_name = info.name; | 172 entry_definition.file_system_name = info.name; |
| 173 switch (type) { | 173 switch (type) { |
| 174 case fileapi::FileSystemContext::RESOLVED_ENTRY_FILE: | 174 case storage::FileSystemContext::RESOLVED_ENTRY_FILE: |
| 175 entry_definition.is_directory = false; | 175 entry_definition.is_directory = false; |
| 176 break; | 176 break; |
| 177 case fileapi::FileSystemContext::RESOLVED_ENTRY_DIRECTORY: | 177 case storage::FileSystemContext::RESOLVED_ENTRY_DIRECTORY: |
| 178 entry_definition.is_directory = true; | 178 entry_definition.is_directory = true; |
| 179 break; | 179 break; |
| 180 case fileapi::FileSystemContext::RESOLVED_ENTRY_NOT_FOUND: | 180 case storage::FileSystemContext::RESOLVED_ENTRY_NOT_FOUND: |
| 181 entry_definition.is_directory = iterator->is_directory; | 181 entry_definition.is_directory = iterator->is_directory; |
| 182 break; | 182 break; |
| 183 } | 183 } |
| 184 entry_definition.error = base::File::FILE_OK; | 184 entry_definition.error = base::File::FILE_OK; |
| 185 | 185 |
| 186 // Construct a target Entry.fullPath value from the virtual path and the | 186 // Construct a target Entry.fullPath value from the virtual path and the |
| 187 // root URL. Eg. Downloads/A/b.txt -> A/b.txt. | 187 // root URL. Eg. Downloads/A/b.txt -> A/b.txt. |
| 188 const base::FilePath root_virtual_path = | 188 const base::FilePath root_virtual_path = |
| 189 file_system_context_->CrackURL(info.root_url).virtual_path(); | 189 file_system_context_->CrackURL(info.root_url).virtual_path(); |
| 190 DCHECK(root_virtual_path == iterator->virtual_path || | 190 DCHECK(root_virtual_path == iterator->virtual_path || |
| (...skipping 17 matching lines...) Expand all Loading... |
| 208 // the redundant container. | 208 // the redundant container. |
| 209 void OnConvertFileDefinitionDone( | 209 void OnConvertFileDefinitionDone( |
| 210 const EntryDefinitionCallback& callback, | 210 const EntryDefinitionCallback& callback, |
| 211 scoped_ptr<EntryDefinitionList> entry_definition_list) { | 211 scoped_ptr<EntryDefinitionList> entry_definition_list) { |
| 212 DCHECK_EQ(1u, entry_definition_list->size()); | 212 DCHECK_EQ(1u, entry_definition_list->size()); |
| 213 callback.Run(entry_definition_list->at(0)); | 213 callback.Run(entry_definition_list->at(0)); |
| 214 } | 214 } |
| 215 | 215 |
| 216 // Used to implement CheckIfDirectoryExists(). | 216 // Used to implement CheckIfDirectoryExists(). |
| 217 void CheckIfDirectoryExistsOnIOThread( | 217 void CheckIfDirectoryExistsOnIOThread( |
| 218 scoped_refptr<fileapi::FileSystemContext> file_system_context, | 218 scoped_refptr<storage::FileSystemContext> file_system_context, |
| 219 const GURL& url, | 219 const GURL& url, |
| 220 const fileapi::FileSystemOperationRunner::StatusCallback& callback) { | 220 const storage::FileSystemOperationRunner::StatusCallback& callback) { |
| 221 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 221 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 222 | 222 |
| 223 fileapi::FileSystemURL file_system_url = file_system_context->CrackURL(url); | 223 storage::FileSystemURL file_system_url = file_system_context->CrackURL(url); |
| 224 file_system_context->operation_runner()->DirectoryExists( | 224 file_system_context->operation_runner()->DirectoryExists( |
| 225 file_system_url, callback); | 225 file_system_url, callback); |
| 226 } | 226 } |
| 227 | 227 |
| 228 } // namespace | 228 } // namespace |
| 229 | 229 |
| 230 EntryDefinition::EntryDefinition() { | 230 EntryDefinition::EntryDefinition() { |
| 231 } | 231 } |
| 232 | 232 |
| 233 EntryDefinition::~EntryDefinition() { | 233 EntryDefinition::~EntryDefinition() { |
| 234 } | 234 } |
| 235 | 235 |
| 236 fileapi::FileSystemContext* GetFileSystemContextForExtensionId( | 236 storage::FileSystemContext* GetFileSystemContextForExtensionId( |
| 237 Profile* profile, | 237 Profile* profile, |
| 238 const std::string& extension_id) { | 238 const std::string& extension_id) { |
| 239 GURL site = extensions::util::GetSiteForExtensionId(extension_id, profile); | 239 GURL site = extensions::util::GetSiteForExtensionId(extension_id, profile); |
| 240 return content::BrowserContext::GetStoragePartitionForSite(profile, site)-> | 240 return content::BrowserContext::GetStoragePartitionForSite(profile, site)-> |
| 241 GetFileSystemContext(); | 241 GetFileSystemContext(); |
| 242 } | 242 } |
| 243 | 243 |
| 244 fileapi::FileSystemContext* GetFileSystemContextForRenderViewHost( | 244 storage::FileSystemContext* GetFileSystemContextForRenderViewHost( |
| 245 Profile* profile, | 245 Profile* profile, |
| 246 content::RenderViewHost* render_view_host) { | 246 content::RenderViewHost* render_view_host) { |
| 247 content::SiteInstance* site_instance = render_view_host->GetSiteInstance(); | 247 content::SiteInstance* site_instance = render_view_host->GetSiteInstance(); |
| 248 return content::BrowserContext::GetStoragePartition(profile, site_instance)-> | 248 return content::BrowserContext::GetStoragePartition(profile, site_instance)-> |
| 249 GetFileSystemContext(); | 249 GetFileSystemContext(); |
| 250 } | 250 } |
| 251 | 251 |
| 252 base::FilePath ConvertDrivePathToRelativeFileSystemPath( | 252 base::FilePath ConvertDrivePathToRelativeFileSystemPath( |
| 253 Profile* profile, | 253 Profile* profile, |
| 254 const std::string& extension_id, | 254 const std::string& extension_id, |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 | 294 |
| 295 bool ConvertAbsoluteFilePathToRelativeFileSystemPath( | 295 bool ConvertAbsoluteFilePathToRelativeFileSystemPath( |
| 296 Profile* profile, | 296 Profile* profile, |
| 297 const std::string& extension_id, | 297 const std::string& extension_id, |
| 298 const base::FilePath& absolute_path, | 298 const base::FilePath& absolute_path, |
| 299 base::FilePath* virtual_path) { | 299 base::FilePath* virtual_path) { |
| 300 // File browser APIs are meant to be used only from extension context, so the | 300 // File browser APIs are meant to be used only from extension context, so the |
| 301 // extension's site is the one in whose file system context the virtual path | 301 // extension's site is the one in whose file system context the virtual path |
| 302 // should be found. | 302 // should be found. |
| 303 GURL site = extensions::util::GetSiteForExtensionId(extension_id, profile); | 303 GURL site = extensions::util::GetSiteForExtensionId(extension_id, profile); |
| 304 fileapi::ExternalFileSystemBackend* backend = | 304 storage::ExternalFileSystemBackend* backend = |
| 305 content::BrowserContext::GetStoragePartitionForSite(profile, site)-> | 305 content::BrowserContext::GetStoragePartitionForSite(profile, site) |
| 306 GetFileSystemContext()->external_backend(); | 306 ->GetFileSystemContext() |
| 307 ->external_backend(); |
| 307 if (!backend) | 308 if (!backend) |
| 308 return false; | 309 return false; |
| 309 | 310 |
| 310 // Find if this file path is managed by the external backend. | 311 // Find if this file path is managed by the external backend. |
| 311 if (!backend->GetVirtualPath(absolute_path, virtual_path)) | 312 if (!backend->GetVirtualPath(absolute_path, virtual_path)) |
| 312 return false; | 313 return false; |
| 313 | 314 |
| 314 return true; | 315 return true; |
| 315 } | 316 } |
| 316 | 317 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 336 FileDefinitionList file_definition_list; | 337 FileDefinitionList file_definition_list; |
| 337 file_definition_list.push_back(file_definition); | 338 file_definition_list.push_back(file_definition); |
| 338 ConvertFileDefinitionListToEntryDefinitionList( | 339 ConvertFileDefinitionListToEntryDefinitionList( |
| 339 profile, | 340 profile, |
| 340 extension_id, | 341 extension_id, |
| 341 file_definition_list, | 342 file_definition_list, |
| 342 base::Bind(&OnConvertFileDefinitionDone, callback)); | 343 base::Bind(&OnConvertFileDefinitionDone, callback)); |
| 343 } | 344 } |
| 344 | 345 |
| 345 void CheckIfDirectoryExists( | 346 void CheckIfDirectoryExists( |
| 346 scoped_refptr<fileapi::FileSystemContext> file_system_context, | 347 scoped_refptr<storage::FileSystemContext> file_system_context, |
| 347 const GURL& url, | 348 const GURL& url, |
| 348 const fileapi::FileSystemOperationRunner::StatusCallback& callback) { | 349 const storage::FileSystemOperationRunner::StatusCallback& callback) { |
| 349 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 350 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 350 | 351 |
| 351 // Check the existence of directory using file system API implementation on | 352 // Check the existence of directory using file system API implementation on |
| 352 // behalf of the file manager app. We need to grant access beforehand. | 353 // behalf of the file manager app. We need to grant access beforehand. |
| 353 fileapi::ExternalFileSystemBackend* backend = | 354 storage::ExternalFileSystemBackend* backend = |
| 354 file_system_context->external_backend(); | 355 file_system_context->external_backend(); |
| 355 DCHECK(backend); | 356 DCHECK(backend); |
| 356 backend->GrantFullAccessToExtension(kFileManagerAppId); | 357 backend->GrantFullAccessToExtension(kFileManagerAppId); |
| 357 | 358 |
| 358 BrowserThread::PostTask( | 359 BrowserThread::PostTask( |
| 359 BrowserThread::IO, FROM_HERE, | 360 BrowserThread::IO, FROM_HERE, |
| 360 base::Bind(&CheckIfDirectoryExistsOnIOThread, | 361 base::Bind(&CheckIfDirectoryExistsOnIOThread, |
| 361 file_system_context, | 362 file_system_context, |
| 362 url, | 363 url, |
| 363 google_apis::CreateRelayCallback(callback))); | 364 google_apis::CreateRelayCallback(callback))); |
| 364 } | 365 } |
| 365 | 366 |
| 366 } // namespace util | 367 } // namespace util |
| 367 } // namespace file_manager | 368 } // namespace file_manager |
| OLD | NEW |