| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_FILE_SYSTEM_FILE_SYSTEM_API_H_ | |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_FILE_SYSTEM_FILE_SYSTEM_API_H_ | |
| 7 | |
| 8 #include <memory> | |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/files/file.h" | |
| 13 #include "base/files/file_path.h" | |
| 14 #include "base/macros.h" | |
| 15 #include "base/memory/weak_ptr.h" | |
| 16 #include "base/values.h" | |
| 17 #include "build/build_config.h" | |
| 18 #include "chrome/browser/extensions/chrome_extension_function.h" | |
| 19 #include "chrome/browser/extensions/chrome_extension_function_details.h" | |
| 20 #include "chrome/common/extensions/api/file_system.h" | |
| 21 #include "extensions/browser/extension_function.h" | |
| 22 #include "ui/shell_dialogs/select_file_dialog.h" | |
| 23 | |
| 24 #if defined(OS_CHROMEOS) | |
| 25 #include "chrome/browser/extensions/api/file_system/consent_provider.h" | |
| 26 #endif | |
| 27 | |
| 28 namespace content { | |
| 29 class WebContents; | |
| 30 } // namespace content | |
| 31 | |
| 32 namespace extensions { | |
| 33 class ExtensionPrefs; | |
| 34 | |
| 35 namespace file_system_api { | |
| 36 | |
| 37 // Methods to get and set the path of the directory containing the last file | |
| 38 // chosen by the user in response to a chrome.fileSystem.chooseEntry() call for | |
| 39 // the given extension. | |
| 40 | |
| 41 // Returns an empty path on failure. | |
| 42 base::FilePath GetLastChooseEntryDirectory(const ExtensionPrefs* prefs, | |
| 43 const std::string& extension_id); | |
| 44 | |
| 45 void SetLastChooseEntryDirectory(ExtensionPrefs* prefs, | |
| 46 const std::string& extension_id, | |
| 47 const base::FilePath& path); | |
| 48 | |
| 49 #if defined(OS_CHROMEOS) | |
| 50 // Dispatches an event about a mounted on unmounted volume in the system to | |
| 51 // each extension which can request it. | |
| 52 void DispatchVolumeListChangeEvent(Profile* profile); | |
| 53 #endif | |
| 54 | |
| 55 } // namespace file_system_api | |
| 56 | |
| 57 class FileSystemGetDisplayPathFunction : public UIThreadExtensionFunction { | |
| 58 public: | |
| 59 DECLARE_EXTENSION_FUNCTION("fileSystem.getDisplayPath", | |
| 60 FILESYSTEM_GETDISPLAYPATH) | |
| 61 | |
| 62 protected: | |
| 63 ~FileSystemGetDisplayPathFunction() override {} | |
| 64 ResponseAction Run() override; | |
| 65 }; | |
| 66 | |
| 67 class FileSystemEntryFunction : public ChromeAsyncExtensionFunction { | |
| 68 protected: | |
| 69 FileSystemEntryFunction(); | |
| 70 | |
| 71 ~FileSystemEntryFunction() override {} | |
| 72 | |
| 73 // This is called when writable file entries are being returned. The function | |
| 74 // will ensure the files exist, creating them if necessary, and also check | |
| 75 // that none of the files are links. If it succeeds it proceeds to | |
| 76 // RegisterFileSystemsAndSendResponse, otherwise to HandleWritableFileError. | |
| 77 void PrepareFilesForWritableApp(const std::vector<base::FilePath>& path); | |
| 78 | |
| 79 // This will finish the choose file process. This is either called directly | |
| 80 // from FilesSelected, or from WritableFileChecker. It is called on the UI | |
| 81 // thread. | |
| 82 void RegisterFileSystemsAndSendResponse( | |
| 83 const std::vector<base::FilePath>& path); | |
| 84 | |
| 85 // Creates a result dictionary. | |
| 86 std::unique_ptr<base::DictionaryValue> CreateResult(); | |
| 87 | |
| 88 // Adds an entry to the result dictionary. | |
| 89 void AddEntryToResult(const base::FilePath& path, | |
| 90 const std::string& id_override, | |
| 91 base::DictionaryValue* result); | |
| 92 | |
| 93 // called on the UI thread if there is a problem checking a writable file. | |
| 94 void HandleWritableFileError(const base::FilePath& error_path); | |
| 95 | |
| 96 // Whether multiple entries have been requested. | |
| 97 bool multiple_; | |
| 98 | |
| 99 // Whether a directory has been requested. | |
| 100 bool is_directory_; | |
| 101 }; | |
| 102 | |
| 103 class FileSystemGetWritableEntryFunction : public FileSystemEntryFunction { | |
| 104 public: | |
| 105 DECLARE_EXTENSION_FUNCTION("fileSystem.getWritableEntry", | |
| 106 FILESYSTEM_GETWRITABLEENTRY) | |
| 107 | |
| 108 protected: | |
| 109 ~FileSystemGetWritableEntryFunction() override {} | |
| 110 bool RunAsync() override; | |
| 111 | |
| 112 private: | |
| 113 void CheckPermissionAndSendResponse(); | |
| 114 void SetIsDirectoryAsync(); | |
| 115 | |
| 116 // The path to the file for which a writable entry has been requested. | |
| 117 base::FilePath path_; | |
| 118 }; | |
| 119 | |
| 120 class FileSystemIsWritableEntryFunction : public UIThreadExtensionFunction { | |
| 121 public: | |
| 122 DECLARE_EXTENSION_FUNCTION("fileSystem.isWritableEntry", | |
| 123 FILESYSTEM_ISWRITABLEENTRY) | |
| 124 | |
| 125 protected: | |
| 126 ~FileSystemIsWritableEntryFunction() override {} | |
| 127 ResponseAction Run() override; | |
| 128 }; | |
| 129 | |
| 130 class FileSystemChooseEntryFunction : public FileSystemEntryFunction { | |
| 131 public: | |
| 132 // Allow picker UI to be skipped in testing. | |
| 133 static void SkipPickerAndAlwaysSelectPathForTest(base::FilePath* path); | |
| 134 static void SkipPickerAndAlwaysSelectPathsForTest( | |
| 135 std::vector<base::FilePath>* paths); | |
| 136 static void SkipPickerAndSelectSuggestedPathForTest(); | |
| 137 static void SkipPickerAndAlwaysCancelForTest(); | |
| 138 static void StopSkippingPickerForTest(); | |
| 139 // Allow directory access confirmation UI to be skipped in testing. | |
| 140 static void SkipDirectoryConfirmationForTest(); | |
| 141 static void AutoCancelDirectoryConfirmationForTest(); | |
| 142 static void StopSkippingDirectoryConfirmationForTest(); | |
| 143 // Call this with the directory for test file paths. On Chrome OS, accessed | |
| 144 // path needs to be explicitly registered for smooth integration with Google | |
| 145 // Drive support. | |
| 146 static void RegisterTempExternalFileSystemForTest(const std::string& name, | |
| 147 const base::FilePath& path); | |
| 148 DECLARE_EXTENSION_FUNCTION("fileSystem.chooseEntry", FILESYSTEM_CHOOSEENTRY) | |
| 149 | |
| 150 typedef std::vector<api::file_system::AcceptOption> AcceptOptions; | |
| 151 | |
| 152 static void BuildFileTypeInfo( | |
| 153 ui::SelectFileDialog::FileTypeInfo* file_type_info, | |
| 154 const base::FilePath::StringType& suggested_extension, | |
| 155 const AcceptOptions* accepts, | |
| 156 const bool* acceptsAllTypes); | |
| 157 static void BuildSuggestion(const std::string* opt_name, | |
| 158 base::FilePath* suggested_name, | |
| 159 base::FilePath::StringType* suggested_extension); | |
| 160 | |
| 161 protected: | |
| 162 ~FileSystemChooseEntryFunction() override {} | |
| 163 bool RunAsync() override; | |
| 164 void ShowPicker(const ui::SelectFileDialog::FileTypeInfo& file_type_info, | |
| 165 ui::SelectFileDialog::Type picker_type); | |
| 166 | |
| 167 private: | |
| 168 void SetInitialPathAndShowPicker( | |
| 169 const base::FilePath& previous_path, | |
| 170 const base::FilePath& suggested_name, | |
| 171 const ui::SelectFileDialog::FileTypeInfo& file_type_info, | |
| 172 ui::SelectFileDialog::Type picker_type, | |
| 173 bool is_path_non_native_directory); | |
| 174 | |
| 175 // FilesSelected and FileSelectionCanceled are called by the file picker. | |
| 176 void FilesSelected(const std::vector<base::FilePath>& paths); | |
| 177 void FileSelectionCanceled(); | |
| 178 | |
| 179 // Check if the chosen directory is or is an ancestor of a sensitive | |
| 180 // directory. If so, show a dialog to confirm that the user wants to open the | |
| 181 // directory. Calls OnDirectoryAccessConfirmed if the directory isn't | |
| 182 // sensitive or the user chooses to open it. Otherwise, calls | |
| 183 // FileSelectionCanceled. | |
| 184 void ConfirmDirectoryAccessAsync(bool non_native_path, | |
| 185 const std::vector<base::FilePath>& paths, | |
| 186 content::WebContents* web_contents); | |
| 187 void OnDirectoryAccessConfirmed(const std::vector<base::FilePath>& paths); | |
| 188 | |
| 189 base::FilePath initial_path_; | |
| 190 }; | |
| 191 | |
| 192 class FileSystemRetainEntryFunction : public ChromeAsyncExtensionFunction { | |
| 193 public: | |
| 194 DECLARE_EXTENSION_FUNCTION("fileSystem.retainEntry", FILESYSTEM_RETAINENTRY) | |
| 195 | |
| 196 protected: | |
| 197 ~FileSystemRetainEntryFunction() override {} | |
| 198 bool RunAsync() override; | |
| 199 | |
| 200 private: | |
| 201 // Retains the file entry referenced by |entry_id| in apps::SavedFilesService. | |
| 202 // |entry_id| must refer to an entry in an isolated file system. |path| is a | |
| 203 // path of the entry. |file_info| is base::File::Info of the entry if it can | |
| 204 // be obtained. | |
| 205 void RetainFileEntry(const std::string& entry_id, | |
| 206 const base::FilePath& path, | |
| 207 std::unique_ptr<base::File::Info> file_info); | |
| 208 }; | |
| 209 | |
| 210 class FileSystemIsRestorableFunction : public UIThreadExtensionFunction { | |
| 211 public: | |
| 212 DECLARE_EXTENSION_FUNCTION("fileSystem.isRestorable", FILESYSTEM_ISRESTORABLE) | |
| 213 | |
| 214 protected: | |
| 215 ~FileSystemIsRestorableFunction() override {} | |
| 216 ResponseAction Run() override; | |
| 217 }; | |
| 218 | |
| 219 class FileSystemRestoreEntryFunction : public FileSystemEntryFunction { | |
| 220 public: | |
| 221 DECLARE_EXTENSION_FUNCTION("fileSystem.restoreEntry", FILESYSTEM_RESTOREENTRY) | |
| 222 | |
| 223 protected: | |
| 224 ~FileSystemRestoreEntryFunction() override {} | |
| 225 bool RunAsync() override; | |
| 226 }; | |
| 227 | |
| 228 class FileSystemObserveDirectoryFunction : public UIThreadExtensionFunction { | |
| 229 public: | |
| 230 DECLARE_EXTENSION_FUNCTION("fileSystem.observeDirectory", | |
| 231 FILESYSTEM_OBSERVEDIRECTORY) | |
| 232 | |
| 233 protected: | |
| 234 ~FileSystemObserveDirectoryFunction() override {} | |
| 235 ResponseAction Run() override; | |
| 236 }; | |
| 237 | |
| 238 class FileSystemUnobserveEntryFunction : public UIThreadExtensionFunction { | |
| 239 public: | |
| 240 DECLARE_EXTENSION_FUNCTION("fileSystem.unobserveEntry", | |
| 241 FILESYSTEM_UNOBSERVEENTRY) | |
| 242 | |
| 243 protected: | |
| 244 ~FileSystemUnobserveEntryFunction() override {} | |
| 245 ResponseAction Run() override; | |
| 246 }; | |
| 247 | |
| 248 class FileSystemGetObservedEntriesFunction : public UIThreadExtensionFunction { | |
| 249 public: | |
| 250 DECLARE_EXTENSION_FUNCTION("fileSystem.getObservedEntries", | |
| 251 FILESYSTEM_GETOBSERVEDENTRIES); | |
| 252 | |
| 253 protected: | |
| 254 ~FileSystemGetObservedEntriesFunction() override {} | |
| 255 ResponseAction Run() override; | |
| 256 }; | |
| 257 | |
| 258 #if !defined(OS_CHROMEOS) | |
| 259 // Stub for non Chrome OS operating systems. | |
| 260 class FileSystemRequestFileSystemFunction : public UIThreadExtensionFunction { | |
| 261 public: | |
| 262 DECLARE_EXTENSION_FUNCTION("fileSystem.requestFileSystem", | |
| 263 FILESYSTEM_REQUESTFILESYSTEM); | |
| 264 | |
| 265 protected: | |
| 266 ~FileSystemRequestFileSystemFunction() override {} | |
| 267 | |
| 268 // UIThreadExtensionFunction overrides. | |
| 269 ExtensionFunction::ResponseAction Run() override; | |
| 270 }; | |
| 271 | |
| 272 // Stub for non Chrome OS operating systems. | |
| 273 class FileSystemGetVolumeListFunction : public UIThreadExtensionFunction { | |
| 274 public: | |
| 275 DECLARE_EXTENSION_FUNCTION("fileSystem.getVolumeList", | |
| 276 FILESYSTEM_GETVOLUMELIST); | |
| 277 | |
| 278 protected: | |
| 279 ~FileSystemGetVolumeListFunction() override {} | |
| 280 | |
| 281 // UIThreadExtensionFunction overrides. | |
| 282 ExtensionFunction::ResponseAction Run() override; | |
| 283 }; | |
| 284 | |
| 285 #else | |
| 286 // Requests a file system for the specified volume id. | |
| 287 class FileSystemRequestFileSystemFunction : public UIThreadExtensionFunction { | |
| 288 public: | |
| 289 DECLARE_EXTENSION_FUNCTION("fileSystem.requestFileSystem", | |
| 290 FILESYSTEM_REQUESTFILESYSTEM) | |
| 291 FileSystemRequestFileSystemFunction(); | |
| 292 | |
| 293 protected: | |
| 294 ~FileSystemRequestFileSystemFunction() override; | |
| 295 | |
| 296 // UIThreadExtensionFunction overrides. | |
| 297 ExtensionFunction::ResponseAction Run() override; | |
| 298 | |
| 299 private: | |
| 300 // Called when a user grants or rejects permissions for the file system | |
| 301 // access. | |
| 302 void OnConsentReceived(const base::WeakPtr<file_manager::Volume>& volume, | |
| 303 bool writable, | |
| 304 file_system_api::ConsentProvider::Consent result); | |
| 305 | |
| 306 ChromeExtensionFunctionDetails chrome_details_; | |
| 307 }; | |
| 308 | |
| 309 // Requests a list of available volumes. | |
| 310 class FileSystemGetVolumeListFunction : public UIThreadExtensionFunction { | |
| 311 public: | |
| 312 DECLARE_EXTENSION_FUNCTION("fileSystem.getVolumeList", | |
| 313 FILESYSTEM_GETVOLUMELIST); | |
| 314 FileSystemGetVolumeListFunction(); | |
| 315 | |
| 316 protected: | |
| 317 ~FileSystemGetVolumeListFunction() override; | |
| 318 | |
| 319 // UIThreadExtensionFunction overrides. | |
| 320 ExtensionFunction::ResponseAction Run() override; | |
| 321 | |
| 322 private: | |
| 323 ChromeExtensionFunctionDetails chrome_details_; | |
| 324 }; | |
| 325 #endif | |
| 326 | |
| 327 } // namespace extensions | |
| 328 | |
| 329 #endif // CHROME_BROWSER_EXTENSIONS_API_FILE_SYSTEM_FILE_SYSTEM_API_H_ | |
| OLD | NEW |