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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_FILE_SYSTEM_FILE_SYSTEM_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_FILE_SYSTEM_FILE_SYSTEM_API_H_
11 #include "base/files/file_path.h"
12 #include "chrome/browser/extensions/chrome_extension_function.h"
13 #include "chrome/common/extensions/api/file_system.h"
14 #include "ui/shell_dialogs/select_file_dialog.h"
16 namespace extensions {
19 namespace file_system_api {
21 // Methods to get and set the path of the directory containing the last file
22 // chosen by the user in response to a chrome.fileSystem.chooseEntry() call for
23 // the given extension.
25 // Returns an empty path on failure.
26 base::FilePath GetLastChooseEntryDirectory(const ExtensionPrefs* prefs,
27 const std::string& extension_id);
29 void SetLastChooseEntryDirectory(ExtensionPrefs* prefs,
30 const std::string& extension_id,
31 const base::FilePath& path);
33 std::vector<base::FilePath> GetGrayListedDirectories();
35 } // namespace file_system_api
37 class FileSystemGetDisplayPathFunction : public ChromeSyncExtensionFunction {
39 DECLARE_EXTENSION_FUNCTION("fileSystem.getDisplayPath",
40 FILESYSTEM_GETDISPLAYPATH)
43 virtual ~FileSystemGetDisplayPathFunction() {}
44 virtual bool RunSync() OVERRIDE;
47 class FileSystemEntryFunction : public ChromeAsyncExtensionFunction {
49 FileSystemEntryFunction();
51 virtual ~FileSystemEntryFunction() {}
53 // This is called when writable file entries are being returned. The function
54 // will ensure the files exist, creating them if necessary, and also check
55 // that none of the files are links. If it succeeds it proceeds to
56 // RegisterFileSystemsAndSendResponse, otherwise to HandleWritableFileError.
57 void PrepareFilesForWritableApp(const std::vector<base::FilePath>& path);
59 // This will finish the choose file process. This is either called directly
60 // from FilesSelected, or from WritableFileChecker. It is called on the UI
62 void RegisterFileSystemsAndSendResponse(
63 const std::vector<base::FilePath>& path);
65 // Creates a response dictionary and sets it as the response to be sent.
66 void CreateResponse();
68 // Adds an entry to the response dictionary.
69 void AddEntryToResponse(const base::FilePath& path,
70 const std::string& id_override);
72 // called on the UI thread if there is a problem checking a writable file.
73 void HandleWritableFileError(const base::FilePath& error_path);
75 // Whether multiple entries have been requested.
78 // Whether a directory has been requested.
81 // The dictionary to send as the response.
82 base::DictionaryValue* response_;
85 class FileSystemGetWritableEntryFunction : public FileSystemEntryFunction {
87 DECLARE_EXTENSION_FUNCTION("fileSystem.getWritableEntry",
88 FILESYSTEM_GETWRITABLEENTRY)
91 virtual ~FileSystemGetWritableEntryFunction() {}
92 virtual bool RunAsync() OVERRIDE;
95 void CheckPermissionAndSendResponse();
96 void SetIsDirectoryOnFileThread();
98 // The path to the file for which a writable entry has been requested.
102 class FileSystemIsWritableEntryFunction : public ChromeSyncExtensionFunction {
104 DECLARE_EXTENSION_FUNCTION("fileSystem.isWritableEntry",
105 FILESYSTEM_ISWRITABLEENTRY)
108 virtual ~FileSystemIsWritableEntryFunction() {}
109 virtual bool RunSync() OVERRIDE;
112 class FileSystemChooseEntryFunction : public FileSystemEntryFunction {
114 // Allow picker UI to be skipped in testing.
115 static void SkipPickerAndAlwaysSelectPathForTest(base::FilePath* path);
116 static void SkipPickerAndAlwaysSelectPathsForTest(
117 std::vector<base::FilePath>* paths);
118 static void SkipPickerAndSelectSuggestedPathForTest();
119 static void SkipPickerAndAlwaysCancelForTest();
120 static void StopSkippingPickerForTest();
121 // Allow directory access confirmation UI to be skipped in testing.
122 static void SkipDirectoryConfirmationForTest();
123 static void AutoCancelDirectoryConfirmationForTest();
124 static void StopSkippingDirectoryConfirmationForTest();
125 // Call this with the directory for test file paths. On Chrome OS, accessed
126 // path needs to be explicitly registered for smooth integration with Google
128 static void RegisterTempExternalFileSystemForTest(const std::string& name,
129 const base::FilePath& path);
131 DECLARE_EXTENSION_FUNCTION("fileSystem.chooseEntry", FILESYSTEM_CHOOSEENTRY)
133 typedef std::vector<linked_ptr<extensions::api::file_system::AcceptOption> >
136 static void BuildFileTypeInfo(
137 ui::SelectFileDialog::FileTypeInfo* file_type_info,
138 const base::FilePath::StringType& suggested_extension,
139 const AcceptOptions* accepts,
140 const bool* acceptsAllTypes);
141 static void BuildSuggestion(const std::string* opt_name,
142 base::FilePath* suggested_name,
143 base::FilePath::StringType* suggested_extension);
148 virtual ~FileSystemChooseEntryFunction() {}
149 virtual bool RunAsync() OVERRIDE;
150 void ShowPicker(const ui::SelectFileDialog::FileTypeInfo& file_type_info,
151 ui::SelectFileDialog::Type picker_type);
154 void SetInitialPathOnFileThread(const base::FilePath& suggested_name,
155 const base::FilePath& previous_path);
157 // FilesSelected and FileSelectionCanceled are called by the file picker.
158 void FilesSelected(const std::vector<base::FilePath>& path);
159 void FileSelectionCanceled();
161 // Check if |check_path|, the canonicalized form of the chosen directory
162 // |paths|, is or is an ancestor of a sensitive directory. If so, show a
163 // dialog to confirm that the user wants to open the directory.
164 // Calls OnDirectoryAccessConfirmed if the directory isn't sensitive or the
165 // user chooses to open it. Otherwise, calls FileSelectionCanceled.
166 void ConfirmDirectoryAccessOnFileThread(
167 const base::FilePath& check_path,
168 const std::vector<base::FilePath>& paths,
169 content::WebContents* web_contents);
170 void OnDirectoryAccessConfirmed(const std::vector<base::FilePath>& paths);
172 base::FilePath initial_path_;
175 class FileSystemRetainEntryFunction : public ChromeAsyncExtensionFunction {
177 DECLARE_EXTENSION_FUNCTION("fileSystem.retainEntry", FILESYSTEM_RETAINENTRY)
180 virtual ~FileSystemRetainEntryFunction() {}
181 virtual bool RunAsync() OVERRIDE;
184 // Retains the file entry referenced by |entry_id| in apps::SavedFilesService.
185 // |entry_id| must refer to an entry in an isolated file system.
186 void RetainFileEntry(const std::string& entry_id);
188 void SetIsDirectoryOnFileThread();
190 // Whether the file being retained is a directory.
193 // The path to the file to retain.
194 base::FilePath path_;
197 class FileSystemIsRestorableFunction : public ChromeSyncExtensionFunction {
199 DECLARE_EXTENSION_FUNCTION("fileSystem.isRestorable", FILESYSTEM_ISRESTORABLE)
202 virtual ~FileSystemIsRestorableFunction() {}
203 virtual bool RunSync() OVERRIDE;
206 class FileSystemRestoreEntryFunction : public FileSystemEntryFunction {
208 DECLARE_EXTENSION_FUNCTION("fileSystem.restoreEntry", FILESYSTEM_RESTOREENTRY)
211 virtual ~FileSystemRestoreEntryFunction() {}
212 virtual bool RunAsync() OVERRIDE;
215 class FileSystemObserveDirectoryFunction : public ChromeSyncExtensionFunction {
217 DECLARE_EXTENSION_FUNCTION("fileSystem.observeDirectory",
218 FILESYSTEM_OBSERVEDIRECTORY)
221 virtual ~FileSystemObserveDirectoryFunction() {}
222 virtual bool RunSync() OVERRIDE;
225 class FileSystemUnobserveEntryFunction : public ChromeSyncExtensionFunction {
227 DECLARE_EXTENSION_FUNCTION("fileSystem.unobserveEntry",
228 FILESYSTEM_UNOBSERVEENTRY)
231 virtual ~FileSystemUnobserveEntryFunction() {}
232 virtual bool RunSync() OVERRIDE;
235 class FileSystemGetObservedEntriesFunction
236 : public ChromeSyncExtensionFunction {
238 DECLARE_EXTENSION_FUNCTION("fileSystem.getObservedEntries",
239 FILESYSTEM_GETOBSERVEDENTRIES);
242 virtual ~FileSystemGetObservedEntriesFunction() {}
243 virtual bool RunSync() OVERRIDE;
246 } // namespace extensions
248 #endif // CHROME_BROWSER_EXTENSIONS_API_FILE_SYSTEM_FILE_SYSTEM_API_H_