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_
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"
15 namespace extensions {
18 namespace file_system_api {
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.
24 // Returns an empty path on failure.
25 base::FilePath GetLastChooseEntryDirectory(const ExtensionPrefs* prefs,
26 const std::string& extension_id);
28 void SetLastChooseEntryDirectory(ExtensionPrefs* prefs,
29 const std::string& extension_id,
30 const base::FilePath& path);
32 std::vector<base::FilePath> GetGrayListedDirectories();
34 } // namespace file_system_api
36 class FileSystemGetDisplayPathFunction : public ChromeSyncExtensionFunction {
38 DECLARE_EXTENSION_FUNCTION("fileSystem.getDisplayPath",
39 FILESYSTEM_GETDISPLAYPATH)
42 virtual ~FileSystemGetDisplayPathFunction() {}
43 virtual bool RunSync() OVERRIDE;
46 class FileSystemEntryFunction : public ChromeAsyncExtensionFunction {
48 FileSystemEntryFunction();
50 virtual ~FileSystemEntryFunction() {}
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 CheckWritableFiles(const std::vector<base::FilePath>& path);
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
61 void RegisterFileSystemsAndSendResponse(
62 const std::vector<base::FilePath>& path);
64 // Creates a response dictionary and sets it as the response to be sent.
65 void CreateResponse();
67 // Adds an entry to the response dictionary.
68 void AddEntryToResponse(const base::FilePath& path,
69 const std::string& id_override);
71 // called on the UI thread if there is a problem checking a writable file.
72 void HandleWritableFileError(const base::FilePath& error_path);
74 // Whether multiple entries have been requested.
77 // Whether a directory has been requested.
80 // The dictionary to send as the response.
81 base::DictionaryValue* response_;
84 class FileSystemGetWritableEntryFunction : public FileSystemEntryFunction {
86 DECLARE_EXTENSION_FUNCTION("fileSystem.getWritableEntry",
87 FILESYSTEM_GETWRITABLEENTRY)
90 virtual ~FileSystemGetWritableEntryFunction() {}
91 virtual bool RunAsync() OVERRIDE;
94 void CheckPermissionAndSendResponse();
95 void SetIsDirectoryOnFileThread();
97 // The path to the file for which a writable entry has been requested.
101 class FileSystemIsWritableEntryFunction : public ChromeSyncExtensionFunction {
103 DECLARE_EXTENSION_FUNCTION("fileSystem.isWritableEntry",
104 FILESYSTEM_ISWRITABLEENTRY)
107 virtual ~FileSystemIsWritableEntryFunction() {}
108 virtual bool RunSync() OVERRIDE;
111 class FileSystemChooseEntryFunction : public FileSystemEntryFunction {
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
127 static void RegisterTempExternalFileSystemForTest(const std::string& name,
128 const base::FilePath& path);
130 DECLARE_EXTENSION_FUNCTION("fileSystem.chooseEntry", FILESYSTEM_CHOOSEENTRY)
132 typedef std::vector<linked_ptr<extensions::api::file_system::AcceptOption> >
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);
147 virtual ~FileSystemChooseEntryFunction() {}
148 virtual bool RunAsync() OVERRIDE;
149 void ShowPicker(const ui::SelectFileDialog::FileTypeInfo& file_type_info,
150 ui::SelectFileDialog::Type picker_type);
153 void SetInitialPathOnFileThread(const base::FilePath& suggested_name,
154 const base::FilePath& previous_path);
156 // FilesSelected and FileSelectionCanceled are called by the file picker.
157 void FilesSelected(const std::vector<base::FilePath>& path);
158 void FileSelectionCanceled();
160 // Check if the chosen directory is or is an ancestor of a sensitive
161 // directory. If so, show a dialog to confirm that the user wants to open the
162 // directory. Calls OnDirectoryAccessConfirmed if the directory isn't
163 // sensitive or the user chooses to open it. Otherwise, calls
164 // FileSelectionCanceled.
165 void ConfirmDirectoryAccessOnFileThread(
166 const std::vector<base::FilePath>& paths,
167 content::WebContents* web_contents);
168 void OnDirectoryAccessConfirmed(const std::vector<base::FilePath>& paths);
170 base::FilePath initial_path_;
173 class FileSystemRetainEntryFunction : public ChromeAsyncExtensionFunction {
175 DECLARE_EXTENSION_FUNCTION("fileSystem.retainEntry", FILESYSTEM_RETAINENTRY)
178 virtual ~FileSystemRetainEntryFunction() {}
179 virtual bool RunAsync() OVERRIDE;
182 // Retains the file entry referenced by |entry_id| in apps::SavedFilesService.
183 // |entry_id| must refer to an entry in an isolated file system.
184 void RetainFileEntry(const std::string& entry_id);
186 void SetIsDirectoryOnFileThread();
188 // Whether the file being retained is a directory.
191 // The path to the file to retain.
192 base::FilePath path_;
195 class FileSystemIsRestorableFunction : public ChromeSyncExtensionFunction {
197 DECLARE_EXTENSION_FUNCTION("fileSystem.isRestorable", FILESYSTEM_ISRESTORABLE)
200 virtual ~FileSystemIsRestorableFunction() {}
201 virtual bool RunSync() OVERRIDE;
204 class FileSystemRestoreEntryFunction : public FileSystemEntryFunction {
206 DECLARE_EXTENSION_FUNCTION("fileSystem.restoreEntry", FILESYSTEM_RESTOREENTRY)
209 virtual ~FileSystemRestoreEntryFunction() {}
210 virtual bool RunAsync() OVERRIDE;
213 } // namespace extensions
215 #endif // CHROME_BROWSER_EXTENSIONS_API_FILE_SYSTEM_FILE_SYSTEM_API_H_