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 <vector> 9 10 #include "base/files/file_path.h" 11 #include "chrome/browser/extensions/chrome_extension_function.h" 12 #include "chrome/common/extensions/api/file_system.h" 13 #include "ui/shell_dialogs/select_file_dialog.h" 14 15 namespace extensions { 16 class ExtensionPrefs; 17 18 namespace file_system_api { 19 20 // Methods to get and set the path of the directory containing the last file 21 // chosen by the user in response to a chrome.fileSystem.chooseEntry() call for 22 // the given extension. 23 24 // Returns an empty path on failure. 25 base::FilePath GetLastChooseEntryDirectory(const ExtensionPrefs* prefs, 26 const std::string& extension_id); 27 28 void SetLastChooseEntryDirectory(ExtensionPrefs* prefs, 29 const std::string& extension_id, 30 const base::FilePath& path); 31 32 std::vector<base::FilePath> GetGrayListedDirectories(); 33 34 } // namespace file_system_api 35 36 class FileSystemGetDisplayPathFunction : public ChromeSyncExtensionFunction { 37 public: 38 DECLARE_EXTENSION_FUNCTION("fileSystem.getDisplayPath", 39 FILESYSTEM_GETDISPLAYPATH) 40 41 protected: ~FileSystemGetDisplayPathFunction()42 virtual ~FileSystemGetDisplayPathFunction() {} 43 virtual bool RunSync() OVERRIDE; 44 }; 45 46 class FileSystemEntryFunction : public ChromeAsyncExtensionFunction { 47 protected: 48 FileSystemEntryFunction(); 49 ~FileSystemEntryFunction()50 virtual ~FileSystemEntryFunction() {} 51 52 // This is called when writable file entries are being returned. The function 53 // will ensure the files exist, creating them if necessary, and also check 54 // that none of the files are links. If it succeeds it proceeds to 55 // RegisterFileSystemsAndSendResponse, otherwise to HandleWritableFileError. 56 void PrepareFilesForWritableApp(const std::vector<base::FilePath>& path); 57 58 // This will finish the choose file process. This is either called directly 59 // from FilesSelected, or from WritableFileChecker. It is called on the UI 60 // thread. 61 void RegisterFileSystemsAndSendResponse( 62 const std::vector<base::FilePath>& path); 63 64 // Creates a response dictionary and sets it as the response to be sent. 65 void CreateResponse(); 66 67 // Adds an entry to the response dictionary. 68 void AddEntryToResponse(const base::FilePath& path, 69 const std::string& id_override); 70 71 // called on the UI thread if there is a problem checking a writable file. 72 void HandleWritableFileError(const base::FilePath& error_path); 73 74 // Whether multiple entries have been requested. 75 bool multiple_; 76 77 // Whether a directory has been requested. 78 bool is_directory_; 79 80 // The dictionary to send as the response. 81 base::DictionaryValue* response_; 82 }; 83 84 class FileSystemGetWritableEntryFunction : public FileSystemEntryFunction { 85 public: 86 DECLARE_EXTENSION_FUNCTION("fileSystem.getWritableEntry", 87 FILESYSTEM_GETWRITABLEENTRY) 88 89 protected: ~FileSystemGetWritableEntryFunction()90 virtual ~FileSystemGetWritableEntryFunction() {} 91 virtual bool RunAsync() OVERRIDE; 92 93 private: 94 void CheckPermissionAndSendResponse(); 95 void SetIsDirectoryOnFileThread(); 96 97 // The path to the file for which a writable entry has been requested. 98 base::FilePath path_; 99 }; 100 101 class FileSystemIsWritableEntryFunction : public ChromeSyncExtensionFunction { 102 public: 103 DECLARE_EXTENSION_FUNCTION("fileSystem.isWritableEntry", 104 FILESYSTEM_ISWRITABLEENTRY) 105 106 protected: ~FileSystemIsWritableEntryFunction()107 virtual ~FileSystemIsWritableEntryFunction() {} 108 virtual bool RunSync() OVERRIDE; 109 }; 110 111 class FileSystemChooseEntryFunction : public FileSystemEntryFunction { 112 public: 113 // Allow picker UI to be skipped in testing. 114 static void SkipPickerAndAlwaysSelectPathForTest(base::FilePath* path); 115 static void SkipPickerAndAlwaysSelectPathsForTest( 116 std::vector<base::FilePath>* paths); 117 static void SkipPickerAndSelectSuggestedPathForTest(); 118 static void SkipPickerAndAlwaysCancelForTest(); 119 static void StopSkippingPickerForTest(); 120 // Allow directory access confirmation UI to be skipped in testing. 121 static void SkipDirectoryConfirmationForTest(); 122 static void AutoCancelDirectoryConfirmationForTest(); 123 static void StopSkippingDirectoryConfirmationForTest(); 124 // Call this with the directory for test file paths. On Chrome OS, accessed 125 // path needs to be explicitly registered for smooth integration with Google 126 // Drive support. 127 static void RegisterTempExternalFileSystemForTest(const std::string& name, 128 const base::FilePath& path); 129 130 DECLARE_EXTENSION_FUNCTION("fileSystem.chooseEntry", FILESYSTEM_CHOOSEENTRY) 131 132 typedef std::vector<linked_ptr<extensions::api::file_system::AcceptOption> > 133 AcceptOptions; 134 135 static void BuildFileTypeInfo( 136 ui::SelectFileDialog::FileTypeInfo* file_type_info, 137 const base::FilePath::StringType& suggested_extension, 138 const AcceptOptions* accepts, 139 const bool* acceptsAllTypes); 140 static void BuildSuggestion(const std::string* opt_name, 141 base::FilePath* suggested_name, 142 base::FilePath::StringType* suggested_extension); 143 144 protected: 145 class FilePicker; 146 ~FileSystemChooseEntryFunction()147 virtual ~FileSystemChooseEntryFunction() {} 148 virtual bool RunAsync() OVERRIDE; 149 void ShowPicker(const ui::SelectFileDialog::FileTypeInfo& file_type_info, 150 ui::SelectFileDialog::Type picker_type); 151 152 private: 153 void SetInitialPathOnFileThread(const base::FilePath& suggested_name, 154 const base::FilePath& previous_path); 155 156 // FilesSelected and FileSelectionCanceled are called by the file picker. 157 void FilesSelected(const std::vector<base::FilePath>& path); 158 void FileSelectionCanceled(); 159 160 // Check if |check_path|, the canonicalized form of the chosen directory 161 // |paths|, is or is an ancestor of a sensitive directory. If so, show a 162 // dialog to confirm that the user wants to open the directory. 163 // Calls OnDirectoryAccessConfirmed if the directory isn't sensitive or the 164 // user chooses to open it. Otherwise, calls FileSelectionCanceled. 165 void ConfirmDirectoryAccessOnFileThread( 166 const base::FilePath& check_path, 167 const std::vector<base::FilePath>& paths, 168 content::WebContents* web_contents); 169 void OnDirectoryAccessConfirmed(const std::vector<base::FilePath>& paths); 170 171 base::FilePath initial_path_; 172 }; 173 174 class FileSystemRetainEntryFunction : public ChromeAsyncExtensionFunction { 175 public: 176 DECLARE_EXTENSION_FUNCTION("fileSystem.retainEntry", FILESYSTEM_RETAINENTRY) 177 178 protected: ~FileSystemRetainEntryFunction()179 virtual ~FileSystemRetainEntryFunction() {} 180 virtual bool RunAsync() OVERRIDE; 181 182 private: 183 // Retains the file entry referenced by |entry_id| in apps::SavedFilesService. 184 // |entry_id| must refer to an entry in an isolated file system. 185 void RetainFileEntry(const std::string& entry_id); 186 187 void SetIsDirectoryOnFileThread(); 188 189 // Whether the file being retained is a directory. 190 bool is_directory_; 191 192 // The path to the file to retain. 193 base::FilePath path_; 194 }; 195 196 class FileSystemIsRestorableFunction : public ChromeSyncExtensionFunction { 197 public: 198 DECLARE_EXTENSION_FUNCTION("fileSystem.isRestorable", FILESYSTEM_ISRESTORABLE) 199 200 protected: ~FileSystemIsRestorableFunction()201 virtual ~FileSystemIsRestorableFunction() {} 202 virtual bool RunSync() OVERRIDE; 203 }; 204 205 class FileSystemRestoreEntryFunction : public FileSystemEntryFunction { 206 public: 207 DECLARE_EXTENSION_FUNCTION("fileSystem.restoreEntry", FILESYSTEM_RESTOREENTRY) 208 209 protected: ~FileSystemRestoreEntryFunction()210 virtual ~FileSystemRestoreEntryFunction() {} 211 virtual bool RunAsync() OVERRIDE; 212 }; 213 214 } // namespace extensions 215 216 #endif // CHROME_BROWSER_EXTENSIONS_API_FILE_SYSTEM_FILE_SYSTEM_API_H_ 217