Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(485)

Side by Side Diff: chrome/browser/extensions/api/file_system/file_system_api.h

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

Powered by Google App Engine
This is Rietveld 408576698