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/extensions/api/file_system/file_system_api.h" | 5 #include "chrome/browser/extensions/api/file_system/file_system_api.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "apps/saved_files_service.h" | 9 #include "apps/saved_files_service.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/path_service.h" | 14 #include "base/path_service.h" |
15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
17 #include "base/strings/sys_string_conversions.h" | 17 #include "base/strings/sys_string_conversions.h" |
18 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
19 #include "base/value_conversions.h" | 19 #include "base/value_conversions.h" |
20 #include "base/values.h" | 20 #include "base/values.h" |
21 #include "chrome/browser/extensions/api/file_handlers/app_file_handler_util.h" | 21 #include "chrome/browser/extensions/api/file_handlers/app_file_handler_util.h" |
22 #include "chrome/browser/extensions/extension_service.h" | 22 #include "chrome/browser/extensions/extension_service.h" |
| 23 #include "chrome/browser/extensions/extension_util.h" |
23 #include "chrome/browser/extensions/path_util.h" | 24 #include "chrome/browser/extensions/path_util.h" |
24 #include "chrome/browser/platform_util.h" | 25 #include "chrome/browser/platform_util.h" |
25 #include "chrome/browser/profiles/profile.h" | 26 #include "chrome/browser/profiles/profile.h" |
26 #include "chrome/browser/ui/apps/directory_access_confirmation_dialog.h" | 27 #include "chrome/browser/ui/apps/directory_access_confirmation_dialog.h" |
27 #include "chrome/browser/ui/chrome_select_file_policy.h" | 28 #include "chrome/browser/ui/chrome_select_file_policy.h" |
28 #include "chrome/common/chrome_paths.h" | 29 #include "chrome/common/chrome_paths.h" |
29 #include "chrome/common/extensions/api/file_system.h" | 30 #include "chrome/common/extensions/api/file_system.h" |
30 #include "chrome/grit/generated_resources.h" | 31 #include "chrome/grit/generated_resources.h" |
31 #include "content/public/browser/browser_thread.h" | 32 #include "content/public/browser/browser_thread.h" |
32 #include "content/public/browser/child_process_security_policy.h" | 33 #include "content/public/browser/child_process_security_policy.h" |
33 #include "content/public/browser/render_process_host.h" | 34 #include "content/public/browser/render_process_host.h" |
34 #include "content/public/browser/render_view_host.h" | 35 #include "content/public/browser/render_view_host.h" |
| 36 #include "content/public/browser/storage_partition.h" |
35 #include "content/public/browser/web_contents.h" | 37 #include "content/public/browser/web_contents.h" |
36 #include "extensions/browser/app_window/app_window.h" | 38 #include "extensions/browser/app_window/app_window.h" |
37 #include "extensions/browser/app_window/app_window_registry.h" | 39 #include "extensions/browser/app_window/app_window_registry.h" |
38 #include "extensions/browser/extension_prefs.h" | 40 #include "extensions/browser/extension_prefs.h" |
39 #include "extensions/browser/extension_system.h" | 41 #include "extensions/browser/extension_system.h" |
40 #include "extensions/browser/granted_file_entry.h" | 42 #include "extensions/browser/granted_file_entry.h" |
41 #include "extensions/common/permissions/api_permission.h" | 43 #include "extensions/common/permissions/api_permission.h" |
42 #include "extensions/common/permissions/permissions_data.h" | 44 #include "extensions/common/permissions/permissions_data.h" |
43 #include "net/base/mime_util.h" | 45 #include "net/base/mime_util.h" |
44 #include "storage/browser/fileapi/external_mount_points.h" | 46 #include "storage/browser/fileapi/external_mount_points.h" |
| 47 #include "storage/browser/fileapi/file_system_operation_runner.h" |
45 #include "storage/browser/fileapi/isolated_context.h" | 48 #include "storage/browser/fileapi/isolated_context.h" |
46 #include "storage/common/fileapi/file_system_types.h" | 49 #include "storage/common/fileapi/file_system_types.h" |
47 #include "storage/common/fileapi/file_system_util.h" | 50 #include "storage/common/fileapi/file_system_util.h" |
48 #include "ui/base/l10n/l10n_util.h" | 51 #include "ui/base/l10n/l10n_util.h" |
49 #include "ui/shell_dialogs/select_file_dialog.h" | 52 #include "ui/shell_dialogs/select_file_dialog.h" |
50 #include "ui/shell_dialogs/selected_file_info.h" | 53 #include "ui/shell_dialogs/selected_file_info.h" |
51 | 54 |
52 #if defined(OS_MACOSX) | 55 #if defined(OS_MACOSX) |
53 #include <CoreFoundation/CoreFoundation.h> | 56 #include <CoreFoundation/CoreFoundation.h> |
54 #include "base/mac/foundation_util.h" | 57 #include "base/mac/foundation_util.h" |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 | 158 |
156 const int kGraylistedPaths[] = { | 159 const int kGraylistedPaths[] = { |
157 base::DIR_HOME, | 160 base::DIR_HOME, |
158 #if defined(OS_WIN) | 161 #if defined(OS_WIN) |
159 base::DIR_PROGRAM_FILES, | 162 base::DIR_PROGRAM_FILES, |
160 base::DIR_PROGRAM_FILESX86, | 163 base::DIR_PROGRAM_FILESX86, |
161 base::DIR_WINDOWS, | 164 base::DIR_WINDOWS, |
162 #endif | 165 #endif |
163 }; | 166 }; |
164 | 167 |
| 168 typedef base::Callback<void(scoped_ptr<base::File::Info>)> FileInfoOptCallback; |
| 169 |
| 170 // Passes optional file info to the UI thread depending on |result| and |info|. |
| 171 void PassFileInfoToUIThread(const FileInfoOptCallback& callback, |
| 172 base::File::Error result, |
| 173 const base::File::Info& info) { |
| 174 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 175 scoped_ptr<base::File::Info> file_info( |
| 176 result == base::File::FILE_OK ? new base::File::Info(info) : NULL); |
| 177 content::BrowserThread::PostTask( |
| 178 content::BrowserThread::UI, |
| 179 FROM_HERE, |
| 180 base::Bind(callback, base::Passed(&file_info))); |
| 181 } |
| 182 |
165 } // namespace | 183 } // namespace |
166 | 184 |
167 namespace extensions { | 185 namespace extensions { |
168 | 186 |
169 namespace file_system_api { | 187 namespace file_system_api { |
170 | 188 |
171 base::FilePath GetLastChooseEntryDirectory(const ExtensionPrefs* prefs, | 189 base::FilePath GetLastChooseEntryDirectory(const ExtensionPrefs* prefs, |
172 const std::string& extension_id) { | 190 const std::string& extension_id) { |
173 base::FilePath path; | 191 base::FilePath path; |
174 std::string string_path; | 192 std::string string_path; |
(...skipping 676 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
851 } | 869 } |
852 | 870 |
853 bool FileSystemRetainEntryFunction::RunAsync() { | 871 bool FileSystemRetainEntryFunction::RunAsync() { |
854 std::string entry_id; | 872 std::string entry_id; |
855 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &entry_id)); | 873 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &entry_id)); |
856 SavedFilesService* saved_files_service = SavedFilesService::Get(GetProfile()); | 874 SavedFilesService* saved_files_service = SavedFilesService::Get(GetProfile()); |
857 // Add the file to the retain list if it is not already on there. | 875 // Add the file to the retain list if it is not already on there. |
858 if (!saved_files_service->IsRegistered(extension_->id(), entry_id)) { | 876 if (!saved_files_service->IsRegistered(extension_->id(), entry_id)) { |
859 std::string filesystem_name; | 877 std::string filesystem_name; |
860 std::string filesystem_path; | 878 std::string filesystem_path; |
| 879 base::FilePath path; |
861 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &filesystem_name)); | 880 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &filesystem_name)); |
862 EXTENSION_FUNCTION_VALIDATE(args_->GetString(2, &filesystem_path)); | 881 EXTENSION_FUNCTION_VALIDATE(args_->GetString(2, &filesystem_path)); |
863 if (!app_file_handler_util::ValidateFileEntryAndGetPath(filesystem_name, | 882 if (!app_file_handler_util::ValidateFileEntryAndGetPath(filesystem_name, |
864 filesystem_path, | 883 filesystem_path, |
865 render_view_host_, | 884 render_view_host_, |
866 &path_, | 885 &path, |
867 &error_)) { | 886 &error_)) { |
868 return false; | 887 return false; |
869 } | 888 } |
870 | 889 |
871 content::BrowserThread::PostTaskAndReply( | 890 std::string filesystem_id; |
872 content::BrowserThread::FILE, | 891 if (!storage::CrackIsolatedFileSystemName(filesystem_name, &filesystem_id)) |
| 892 return false; |
| 893 |
| 894 const GURL site = |
| 895 extensions::util::GetSiteForExtensionId(extension_id(), GetProfile()); |
| 896 storage::FileSystemContext* const context = |
| 897 content::BrowserContext::GetStoragePartitionForSite(GetProfile(), site) |
| 898 ->GetFileSystemContext(); |
| 899 const storage::FileSystemURL url = context->CreateCrackedFileSystemURL( |
| 900 site, |
| 901 storage::kFileSystemTypeIsolated, |
| 902 IsolatedContext::GetInstance() |
| 903 ->CreateVirtualRootPath(filesystem_id) |
| 904 .Append(base::FilePath::FromUTF8Unsafe(filesystem_path))); |
| 905 |
| 906 content::BrowserThread::PostTask( |
| 907 content::BrowserThread::IO, |
873 FROM_HERE, | 908 FROM_HERE, |
874 base::Bind(&FileSystemRetainEntryFunction::SetIsDirectoryOnFileThread, | 909 base::Bind( |
875 this), | 910 base::IgnoreResult( |
876 base::Bind(&FileSystemRetainEntryFunction::RetainFileEntry, | 911 &storage::FileSystemOperationRunner::GetMetadata), |
877 this, | 912 context->operation_runner()->AsWeakPtr(), |
878 entry_id)); | 913 url, |
| 914 base::Bind( |
| 915 &PassFileInfoToUIThread, |
| 916 base::Bind(&FileSystemRetainEntryFunction::RetainFileEntry, |
| 917 this, |
| 918 entry_id, |
| 919 path)))); |
879 return true; | 920 return true; |
880 } | 921 } |
881 | 922 |
882 saved_files_service->EnqueueFileEntry(extension_->id(), entry_id); | 923 saved_files_service->EnqueueFileEntry(extension_->id(), entry_id); |
883 SendResponse(true); | 924 SendResponse(true); |
884 return true; | 925 return true; |
885 } | 926 } |
886 | 927 |
887 void FileSystemRetainEntryFunction::RetainFileEntry( | 928 void FileSystemRetainEntryFunction::RetainFileEntry( |
888 const std::string& entry_id) { | 929 const std::string& entry_id, |
| 930 const base::FilePath& path, |
| 931 scoped_ptr<base::File::Info> file_info) { |
| 932 if (!file_info) { |
| 933 SendResponse(false); |
| 934 return; |
| 935 } |
| 936 |
889 SavedFilesService* saved_files_service = SavedFilesService::Get(GetProfile()); | 937 SavedFilesService* saved_files_service = SavedFilesService::Get(GetProfile()); |
890 saved_files_service->RegisterFileEntry( | 938 saved_files_service->RegisterFileEntry( |
891 extension_->id(), entry_id, path_, is_directory_); | 939 extension_->id(), entry_id, path, file_info->is_directory); |
892 saved_files_service->EnqueueFileEntry(extension_->id(), entry_id); | 940 saved_files_service->EnqueueFileEntry(extension_->id(), entry_id); |
893 SendResponse(true); | 941 SendResponse(true); |
894 } | 942 } |
895 | 943 |
896 void FileSystemRetainEntryFunction::SetIsDirectoryOnFileThread() { | |
897 is_directory_ = base::DirectoryExists(path_); | |
898 } | |
899 | |
900 bool FileSystemIsRestorableFunction::RunSync() { | 944 bool FileSystemIsRestorableFunction::RunSync() { |
901 std::string entry_id; | 945 std::string entry_id; |
902 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &entry_id)); | 946 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &entry_id)); |
903 SetResult(new base::FundamentalValue(SavedFilesService::Get( | 947 SetResult(new base::FundamentalValue(SavedFilesService::Get( |
904 GetProfile())->IsRegistered(extension_->id(), entry_id))); | 948 GetProfile())->IsRegistered(extension_->id(), entry_id))); |
905 return true; | 949 return true; |
906 } | 950 } |
907 | 951 |
908 bool FileSystemRestoreEntryFunction::RunAsync() { | 952 bool FileSystemRestoreEntryFunction::RunAsync() { |
909 std::string entry_id; | 953 std::string entry_id; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
944 return false; | 988 return false; |
945 } | 989 } |
946 | 990 |
947 bool FileSystemGetObservedEntriesFunction::RunSync() { | 991 bool FileSystemGetObservedEntriesFunction::RunSync() { |
948 NOTIMPLEMENTED(); | 992 NOTIMPLEMENTED(); |
949 error_ = kUnknownIdError; | 993 error_ = kUnknownIdError; |
950 return false; | 994 return false; |
951 } | 995 } |
952 | 996 |
953 } // namespace extensions | 997 } // namespace extensions |
OLD | NEW |