| 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/extensions/file_browser_private_api.h" | 5 #include "chrome/browser/chromeos/extensions/file_browser_private_api.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 #include "chrome/common/pref_names.h" | 45 #include "chrome/common/pref_names.h" |
| 46 #include "content/public/browser/child_process_security_policy.h" | 46 #include "content/public/browser/child_process_security_policy.h" |
| 47 #include "content/public/browser/render_process_host.h" | 47 #include "content/public/browser/render_process_host.h" |
| 48 #include "content/public/browser/render_view_host.h" | 48 #include "content/public/browser/render_view_host.h" |
| 49 #include "googleurl/src/gurl.h" | 49 #include "googleurl/src/gurl.h" |
| 50 #include "grit/generated_resources.h" | 50 #include "grit/generated_resources.h" |
| 51 #include "grit/platform_locale_settings.h" | 51 #include "grit/platform_locale_settings.h" |
| 52 #include "net/base/escape.h" | 52 #include "net/base/escape.h" |
| 53 #include "ui/base/dialogs/selected_file_info.h" | 53 #include "ui/base/dialogs/selected_file_info.h" |
| 54 #include "ui/base/l10n/l10n_util.h" | 54 #include "ui/base/l10n/l10n_util.h" |
| 55 #include "webkit/chromeos/fileapi/cros_mount_point_provider.h" |
| 55 #include "webkit/fileapi/file_system_context.h" | 56 #include "webkit/fileapi/file_system_context.h" |
| 56 #include "webkit/fileapi/file_system_file_util.h" | 57 #include "webkit/fileapi/file_system_file_util.h" |
| 57 #include "webkit/fileapi/file_system_mount_point_provider.h" | |
| 58 #include "webkit/fileapi/file_system_operation_context.h" | 58 #include "webkit/fileapi/file_system_operation_context.h" |
| 59 #include "webkit/fileapi/file_system_types.h" | 59 #include "webkit/fileapi/file_system_types.h" |
| 60 #include "webkit/fileapi/file_system_url.h" |
| 60 #include "webkit/fileapi/file_system_util.h" | 61 #include "webkit/fileapi/file_system_util.h" |
| 61 | 62 |
| 62 using chromeos::disks::DiskMountManager; | 63 using chromeos::disks::DiskMountManager; |
| 63 using content::BrowserContext; | 64 using content::BrowserContext; |
| 64 using content::BrowserThread; | 65 using content::BrowserThread; |
| 65 using content::ChildProcessSecurityPolicy; | 66 using content::ChildProcessSecurityPolicy; |
| 66 using content::SiteInstance; | 67 using content::SiteInstance; |
| 67 using content::WebContents; | 68 using content::WebContents; |
| 68 using extensions::Extension; | 69 using extensions::Extension; |
| 69 using file_handler_util::FileTaskExecutor; | 70 using file_handler_util::FileTaskExecutor; |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 gdata::GDataCache::CACHE_TYPE_PERSISTENT), | 222 gdata::GDataCache::CACHE_TYPE_PERSISTENT), |
| 222 file_handler_util::GetReadOnlyPermissions()); | 223 file_handler_util::GetReadOnlyPermissions()); |
| 223 | 224 |
| 224 FilePath mount_point_virtual; | 225 FilePath mount_point_virtual; |
| 225 if (provider->GetVirtualPath(mount_point, &mount_point_virtual)) | 226 if (provider->GetVirtualPath(mount_point, &mount_point_virtual)) |
| 226 provider->GrantFileAccessToExtension(extension_id, mount_point_virtual); | 227 provider->GrantFileAccessToExtension(extension_id, mount_point_virtual); |
| 227 } | 228 } |
| 228 | 229 |
| 229 // Given a file url, find the virtual FilePath associated with it. | 230 // Given a file url, find the virtual FilePath associated with it. |
| 230 FilePath GetVirtualPathFromURL(const GURL& file_url) { | 231 FilePath GetVirtualPathFromURL(const GURL& file_url) { |
| 231 FilePath virtual_path; | 232 fileapi::FileSystemURL url(file_url); |
| 232 fileapi::FileSystemType type = fileapi::kFileSystemTypeUnknown; | 233 if (!chromeos::CrosMountPointProvider::CanHandleURL(url)) { |
| 233 GURL file_origin_url; | |
| 234 if (!CrackFileSystemURL(file_url, &file_origin_url, &type, &virtual_path) || | |
| 235 type != fileapi::kFileSystemTypeExternal) { | |
| 236 NOTREACHED(); | 234 NOTREACHED(); |
| 237 return FilePath(); | 235 return FilePath(); |
| 238 } | 236 } |
| 239 return virtual_path; | 237 return url.virtual_path(); |
| 240 } | 238 } |
| 241 | 239 |
| 242 // Look up apps in the registry, and collect applications that match the file | 240 // Look up apps in the registry, and collect applications that match the file |
| 243 // paths given. Returns the intersection of all available application ids in | 241 // paths given. Returns the intersection of all available application ids in |
| 244 // |available_apps| and a map of application ID to the Drive web application | 242 // |available_apps| and a map of application ID to the Drive web application |
| 245 // info collected in |app_info| so details can be collected later. The caller | 243 // info collected in |app_info| so details can be collected later. The caller |
| 246 // takes ownership of the pointers in |app_info|. | 244 // takes ownership of the pointers in |app_info|. |
| 247 void IntersectAvailableDriveTasks( | 245 void IntersectAvailableDriveTasks( |
| 248 gdata::DriveWebAppsRegistry* registry, | 246 gdata::DriveWebAppsRegistry* registry, |
| 249 const std::vector<FilePath>& file_paths, | 247 const std::vector<FilePath>& file_paths, |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 422 file_system_context_->external_provider(); | 420 file_system_context_->external_provider(); |
| 423 if (!provider) | 421 if (!provider) |
| 424 return false; | 422 return false; |
| 425 | 423 |
| 426 // Grant full access to File API from this component extension. | 424 // Grant full access to File API from this component extension. |
| 427 provider->GrantFullAccessToExtension(extension_->id()); | 425 provider->GrantFullAccessToExtension(extension_->id()); |
| 428 | 426 |
| 429 // Grant R/W file permissions to the renderer hosting component | 427 // Grant R/W file permissions to the renderer hosting component |
| 430 // extension for all paths exposed by our local file system provider. | 428 // extension for all paths exposed by our local file system provider. |
| 431 std::vector<FilePath> root_dirs = provider->GetRootDirectories(); | 429 std::vector<FilePath> root_dirs = provider->GetRootDirectories(); |
| 432 for (std::vector<FilePath>::iterator iter = root_dirs.begin(); | 430 for (size_t i = 0; i < root_dirs.size(); ++i) { |
| 433 iter != root_dirs.end(); | |
| 434 ++iter) { | |
| 435 ChildProcessSecurityPolicy::GetInstance()->GrantPermissionsForFile( | 431 ChildProcessSecurityPolicy::GetInstance()->GrantPermissionsForFile( |
| 436 child_id_, *iter, file_handler_util::GetReadWritePermissions()); | 432 child_id_, root_dirs[i], |
| 433 file_handler_util::GetReadWritePermissions()); |
| 437 } | 434 } |
| 438 return true; | 435 return true; |
| 439 } | 436 } |
| 440 | 437 |
| 441 RequestLocalFileSystemFunction* function_; | 438 RequestLocalFileSystemFunction* function_; |
| 442 scoped_refptr<fileapi::FileSystemContext> file_system_context_; | 439 scoped_refptr<fileapi::FileSystemContext> file_system_context_; |
| 443 // Renderer process id. | 440 // Renderer process id. |
| 444 int child_id_; | 441 int child_id_; |
| 445 // Extension source URL. | 442 // Extension source URL. |
| 446 scoped_refptr<const Extension> extension_; | 443 scoped_refptr<const Extension> extension_; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 496 } | 493 } |
| 497 | 494 |
| 498 void RequestLocalFileSystemFunction::RespondFailedOnUIThread( | 495 void RequestLocalFileSystemFunction::RespondFailedOnUIThread( |
| 499 base::PlatformFileError error_code) { | 496 base::PlatformFileError error_code) { |
| 500 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 497 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 501 error_ = base::StringPrintf(kFileError, static_cast<int>(error_code)); | 498 error_ = base::StringPrintf(kFileError, static_cast<int>(error_code)); |
| 502 SendResponse(false); | 499 SendResponse(false); |
| 503 } | 500 } |
| 504 | 501 |
| 505 bool FileWatchBrowserFunctionBase::GetLocalFilePath( | 502 bool FileWatchBrowserFunctionBase::GetLocalFilePath( |
| 506 scoped_refptr<fileapi::FileSystemContext> file_system_context, | |
| 507 const GURL& file_url, FilePath* local_path, FilePath* virtual_path) { | 503 const GURL& file_url, FilePath* local_path, FilePath* virtual_path) { |
| 508 GURL file_origin_url; | 504 fileapi::FileSystemURL url(file_url); |
| 509 fileapi::FileSystemType type; | 505 if (!chromeos::CrosMountPointProvider::CanHandleURL(url)) |
| 510 if (!CrackFileSystemURL(file_url, &file_origin_url, &type, | |
| 511 virtual_path)) { | |
| 512 return false; | 506 return false; |
| 513 } | 507 *local_path = url.path(); |
| 514 if (type != fileapi::kFileSystemTypeExternal) | 508 *virtual_path = url.virtual_path(); |
| 515 return false; | |
| 516 | |
| 517 FilePath root_path = file_system_context-> | |
| 518 external_provider()->GetFileSystemRootPathOnFileThread( | |
| 519 file_origin_url, | |
| 520 fileapi::kFileSystemTypeExternal, | |
| 521 *virtual_path, | |
| 522 false); | |
| 523 if (root_path == FilePath()) | |
| 524 return false; | |
| 525 | |
| 526 *local_path = root_path.Append(*virtual_path); | |
| 527 return true; | 509 return true; |
| 528 } | 510 } |
| 529 | 511 |
| 530 void FileWatchBrowserFunctionBase::RespondOnUIThread(bool success) { | 512 void FileWatchBrowserFunctionBase::RespondOnUIThread(bool success) { |
| 531 SetResult(Value::CreateBooleanValue(success)); | 513 SetResult(Value::CreateBooleanValue(success)); |
| 532 SendResponse(success); | 514 SendResponse(success); |
| 533 } | 515 } |
| 534 | 516 |
| 535 bool FileWatchBrowserFunctionBase::RunImpl() { | 517 bool FileWatchBrowserFunctionBase::RunImpl() { |
| 536 if (!render_view_host() || !render_view_host()->GetProcess()) | 518 if (!render_view_host() || !render_view_host()->GetProcess()) |
| (...skipping 19 matching lines...) Expand all Loading... |
| 556 | 538 |
| 557 return true; | 539 return true; |
| 558 } | 540 } |
| 559 | 541 |
| 560 void FileWatchBrowserFunctionBase::RunFileWatchOperationOnFileThread( | 542 void FileWatchBrowserFunctionBase::RunFileWatchOperationOnFileThread( |
| 561 scoped_refptr<fileapi::FileSystemContext> file_system_context, | 543 scoped_refptr<fileapi::FileSystemContext> file_system_context, |
| 562 scoped_refptr<FileBrowserEventRouter> event_router, | 544 scoped_refptr<FileBrowserEventRouter> event_router, |
| 563 const GURL& file_url, const std::string& extension_id) { | 545 const GURL& file_url, const std::string& extension_id) { |
| 564 FilePath local_path; | 546 FilePath local_path; |
| 565 FilePath virtual_path; | 547 FilePath virtual_path; |
| 566 if (!GetLocalFilePath( | 548 if (!GetLocalFilePath(file_url, &local_path, &virtual_path) || |
| 567 file_system_context, file_url, &local_path, &virtual_path) || | |
| 568 local_path == FilePath()) { | 549 local_path == FilePath()) { |
| 569 BrowserThread::PostTask( | 550 BrowserThread::PostTask( |
| 570 BrowserThread::UI, FROM_HERE, | 551 BrowserThread::UI, FROM_HERE, |
| 571 base::Bind( | 552 base::Bind( |
| 572 &FileWatchBrowserFunctionBase::RespondOnUIThread, | 553 &FileWatchBrowserFunctionBase::RespondOnUIThread, |
| 573 this, | 554 this, |
| 574 false)); | 555 false)); |
| 575 } | 556 } |
| 576 if (!PerformFileWatchOperation(event_router, | 557 if (!PerformFileWatchOperation(event_router, |
| 577 local_path, | 558 local_path, |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 612 // from drive. We'll know that they are drive apps because the extension id will | 593 // from drive. We'll know that they are drive apps because the extension id will |
| 613 // begin with kDriveTaskExtensionPrefix. | 594 // begin with kDriveTaskExtensionPrefix. |
| 614 bool GetFileTasksFileBrowserFunction::FindDriveAppTasks( | 595 bool GetFileTasksFileBrowserFunction::FindDriveAppTasks( |
| 615 const std::vector<GURL>& file_urls, | 596 const std::vector<GURL>& file_urls, |
| 616 ListValue* result_list) { | 597 ListValue* result_list) { |
| 617 | 598 |
| 618 // Crack all the urls into file paths. | 599 // Crack all the urls into file paths. |
| 619 std::vector<FilePath> file_paths; | 600 std::vector<FilePath> file_paths; |
| 620 for (std::vector<GURL>::const_iterator iter = file_urls.begin(); | 601 for (std::vector<GURL>::const_iterator iter = file_urls.begin(); |
| 621 iter != file_urls.end(); ++iter) { | 602 iter != file_urls.end(); ++iter) { |
| 622 FilePath raw_path; | 603 fileapi::FileSystemURL url(*iter); |
| 623 fileapi::FileSystemType type = fileapi::kFileSystemTypeUnknown; | 604 if (chromeos::CrosMountPointProvider::CanHandleURL(url)) |
| 624 if (fileapi::CrackFileSystemURL(*iter, NULL, &type, &raw_path) && | 605 file_paths.push_back(url.virtual_path()); |
| 625 type == fileapi::kFileSystemTypeExternal) { | |
| 626 file_paths.push_back(raw_path); | |
| 627 } | |
| 628 } | 606 } |
| 629 | 607 |
| 630 gdata::GDataSystemService* system_service = | 608 gdata::GDataSystemService* system_service = |
| 631 gdata::GDataSystemServiceFactory::GetForProfile(profile_); | 609 gdata::GDataSystemServiceFactory::GetForProfile(profile_); |
| 632 // |system_service| is NULL if incognito window / guest login. We return true | 610 // |system_service| is NULL if incognito window / guest login. We return true |
| 633 // in this case because there might be other extension tasks, even if we don't | 611 // in this case because there might be other extension tasks, even if we don't |
| 634 // have any to add. | 612 // have any to add. |
| 635 if (!system_service || !system_service->webapps_registry()) | 613 if (!system_service || !system_service->webapps_registry()) |
| 636 return true; | 614 return true; |
| 637 | 615 |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 828 base::Bind( | 806 base::Bind( |
| 829 &FileBrowserFunction::GetLocalPathsOnFileThread, | 807 &FileBrowserFunction::GetLocalPathsOnFileThread, |
| 830 this, | 808 this, |
| 831 file_system_context, file_urls, callback)); | 809 file_system_context, file_urls, callback)); |
| 832 } | 810 } |
| 833 | 811 |
| 834 // GetFileSystemRootPathOnFileThread can only be called from the file thread, | 812 // GetFileSystemRootPathOnFileThread can only be called from the file thread, |
| 835 // so here we are. This function takes a vector of virtual paths, converts | 813 // so here we are. This function takes a vector of virtual paths, converts |
| 836 // them to local paths and calls |callback| with the result vector, on the UI | 814 // them to local paths and calls |callback| with the result vector, on the UI |
| 837 // thread. | 815 // thread. |
| 816 // TODO(kinuko): We no longer call GetFileSystemRootPathOnFileThread and |
| 817 // we can likely remove this cross-thread helper method. |
| 838 void FileBrowserFunction::GetLocalPathsOnFileThread( | 818 void FileBrowserFunction::GetLocalPathsOnFileThread( |
| 839 scoped_refptr<fileapi::FileSystemContext> file_system_context, | 819 scoped_refptr<fileapi::FileSystemContext> file_system_context, |
| 840 const UrlList& file_urls, | 820 const UrlList& file_urls, |
| 841 GetLocalPathsCallback callback) { | 821 GetLocalPathsCallback callback) { |
| 842 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 822 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 843 std::vector<ui::SelectedFileInfo> selected_files; | 823 std::vector<ui::SelectedFileInfo> selected_files; |
| 844 | 824 |
| 845 // FilePath(virtual_path) doesn't work on win, so limit this to ChromeOS. | |
| 846 fileapi::ExternalFileSystemMountPointProvider* provider = | |
| 847 file_system_context->external_provider(); | |
| 848 if (!provider) { | |
| 849 LOG(WARNING) << "External provider is not available"; | |
| 850 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 851 base::Bind(callback, selected_files)); | |
| 852 return; | |
| 853 } | |
| 854 | |
| 855 GURL origin_url = source_url().GetOrigin(); | 825 GURL origin_url = source_url().GetOrigin(); |
| 856 size_t len = file_urls.size(); | 826 size_t len = file_urls.size(); |
| 857 selected_files.reserve(len); | 827 selected_files.reserve(len); |
| 858 for (size_t i = 0; i < len; ++i) { | 828 for (size_t i = 0; i < len; ++i) { |
| 859 FilePath local_path; | 829 FilePath local_path; |
| 860 const GURL& file_url = file_urls[i]; | 830 const GURL& file_url = file_urls[i]; |
| 861 | 831 |
| 862 // If "localPath" parameter is set, use it as the real path. | 832 // If "localPath" parameter is set, use it as the real path. |
| 863 // TODO(satorux): Eventually, we should be able to get the real path | 833 // TODO(satorux): Eventually, we should be able to get the real path |
| 864 // from GDataFileSystem instead of passing through with filesystem | 834 // from GDataFileSystem instead of passing through with filesystem |
| (...skipping 14 matching lines...) Expand all Loading... |
| 879 net::UnescapeURLComponent(parameters[i].second, | 849 net::UnescapeURLComponent(parameters[i].second, |
| 880 kUnescapeRuleForQueryParameters); | 850 kUnescapeRuleForQueryParameters); |
| 881 local_path = FilePath::FromUTF8Unsafe(unescaped_value); | 851 local_path = FilePath::FromUTF8Unsafe(unescaped_value); |
| 882 break; | 852 break; |
| 883 } | 853 } |
| 884 } | 854 } |
| 885 } | 855 } |
| 886 } | 856 } |
| 887 | 857 |
| 888 // Extract the path from |file_url|. | 858 // Extract the path from |file_url|. |
| 889 GURL file_origin_url; | 859 fileapi::FileSystemURL url(file_url); |
| 890 FilePath virtual_path; | 860 if (!chromeos::CrosMountPointProvider::CanHandleURL(url)) |
| 891 fileapi::FileSystemType type; | 861 continue; |
| 892 | 862 |
| 893 if (!CrackFileSystemURL(file_url, &file_origin_url, &type, | 863 if (!url.path().empty()) { |
| 894 &virtual_path)) { | 864 DVLOG(1) << "Selected: file path: " << url.path().value() |
| 895 continue; | |
| 896 } | |
| 897 if (type != fileapi::kFileSystemTypeExternal) { | |
| 898 NOTREACHED(); | |
| 899 continue; | |
| 900 } | |
| 901 | |
| 902 FilePath root = provider->GetFileSystemRootPathOnFileThread( | |
| 903 origin_url, | |
| 904 fileapi::kFileSystemTypeExternal, | |
| 905 FilePath(virtual_path), | |
| 906 false); | |
| 907 FilePath file_path; | |
| 908 if (!root.empty()) { | |
| 909 file_path = root.Append(virtual_path); | |
| 910 } else { | |
| 911 LOG(WARNING) << "GetLocalPathsOnFileThread failed " | |
| 912 << file_url.spec(); | |
| 913 } | |
| 914 | |
| 915 if (!file_path.empty()) { | |
| 916 DVLOG(1) << "Selected: file path: " << file_path.value() | |
| 917 << " local path: " << local_path.value(); | 865 << " local path: " << local_path.value(); |
| 918 selected_files.push_back( | 866 selected_files.push_back( |
| 919 ui::SelectedFileInfo(file_path, local_path)); | 867 ui::SelectedFileInfo(url.path(), local_path)); |
| 920 } | 868 } |
| 921 } | 869 } |
| 922 | 870 |
| 923 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 871 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 924 base::Bind(callback, selected_files)); | 872 base::Bind(callback, selected_files)); |
| 925 } | 873 } |
| 926 | 874 |
| 927 bool SelectFileFunction::RunImpl() { | 875 bool SelectFileFunction::RunImpl() { |
| 928 if (args_->GetSize() != 2) { | 876 if (args_->GetSize() != 2) { |
| 929 return false; | 877 return false; |
| (...skipping 1501 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2431 gdata::GDataSystemService* system_service = | 2379 gdata::GDataSystemService* system_service = |
| 2432 gdata::GDataSystemServiceFactory::GetForProfile(profile_); | 2380 gdata::GDataSystemServiceFactory::GetForProfile(profile_); |
| 2433 if (!system_service || !system_service->file_system()) | 2381 if (!system_service || !system_service->file_system()) |
| 2434 return false; | 2382 return false; |
| 2435 | 2383 |
| 2436 FilePath directory_path = GetVirtualPathFromURL(GURL(file_url_as_string)); | 2384 FilePath directory_path = GetVirtualPathFromURL(GURL(file_url_as_string)); |
| 2437 system_service->file_system()->RequestDirectoryRefresh(directory_path); | 2385 system_service->file_system()->RequestDirectoryRefresh(directory_path); |
| 2438 | 2386 |
| 2439 return true; | 2387 return true; |
| 2440 } | 2388 } |
| OLD | NEW |