OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_FILE_SYSTEM_FILE_SYSTEM_API_H_ | 5 #ifndef EXTENSIONS_BROWSER_API_FILE_SYSTEM_FILE_SYSTEM_API_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_API_FILE_SYSTEM_FILE_SYSTEM_API_H_ | 6 #define EXTENSIONS_BROWSER_API_FILE_SYSTEM_FILE_SYSTEM_API_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/files/file.h" | 12 #include "base/files/file.h" |
13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/weak_ptr.h" | |
16 #include "base/values.h" | 15 #include "base/values.h" |
17 #include "build/build_config.h" | 16 #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" | 17 #include "extensions/browser/extension_function.h" |
| 18 #include "extensions/common/api/file_system.h" |
22 #include "ui/shell_dialogs/select_file_dialog.h" | 19 #include "ui/shell_dialogs/select_file_dialog.h" |
23 | 20 |
24 #if defined(OS_CHROMEOS) | |
25 #include "chrome/browser/extensions/api/file_system/consent_provider.h" | |
26 #endif | |
27 | |
28 namespace extensions { | 21 namespace extensions { |
29 class ExtensionPrefs; | 22 class ExtensionPrefs; |
30 | 23 |
31 namespace file_system_api { | 24 namespace file_system_api { |
32 | 25 |
33 // Methods to get and set the path of the directory containing the last file | 26 // 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 | 27 // chosen by the user in response to a chrome.fileSystem.chooseEntry() call for |
35 // the given extension. | 28 // the given extension. |
36 | 29 |
37 // Returns an empty path on failure. | 30 // Returns an empty path on failure. |
38 base::FilePath GetLastChooseEntryDirectory(const ExtensionPrefs* prefs, | 31 base::FilePath GetLastChooseEntryDirectory(const ExtensionPrefs* prefs, |
39 const std::string& extension_id); | 32 const std::string& extension_id); |
40 | 33 |
41 void SetLastChooseEntryDirectory(ExtensionPrefs* prefs, | 34 void SetLastChooseEntryDirectory(ExtensionPrefs* prefs, |
42 const std::string& extension_id, | 35 const std::string& extension_id, |
43 const base::FilePath& path); | 36 const base::FilePath& path); |
44 | 37 |
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 | 38 } // namespace file_system_api |
52 | 39 |
53 class FileSystemGetDisplayPathFunction : public UIThreadExtensionFunction { | 40 class FileSystemGetDisplayPathFunction : public UIThreadExtensionFunction { |
54 public: | 41 public: |
55 DECLARE_EXTENSION_FUNCTION("fileSystem.getDisplayPath", | 42 DECLARE_EXTENSION_FUNCTION("fileSystem.getDisplayPath", |
56 FILESYSTEM_GETDISPLAYPATH) | 43 FILESYSTEM_GETDISPLAYPATH) |
57 | 44 |
58 protected: | 45 protected: |
59 ~FileSystemGetDisplayPathFunction() override {} | 46 ~FileSystemGetDisplayPathFunction() override {} |
60 ResponseAction Run() override; | 47 ResponseAction Run() override; |
61 }; | 48 }; |
62 | 49 |
63 class FileSystemEntryFunction : public ChromeAsyncExtensionFunction { | 50 class FileSystemEntryFunction : public UIThreadExtensionFunction { |
64 protected: | 51 protected: |
65 FileSystemEntryFunction(); | 52 FileSystemEntryFunction(); |
66 | 53 |
67 ~FileSystemEntryFunction() override {} | 54 ~FileSystemEntryFunction() override {} |
68 | 55 |
69 // This is called when writable file entries are being returned. The function | 56 // 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 | 57 // 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 | 58 // that none of the files are links. If it succeeds it proceeds to |
72 // RegisterFileSystemsAndSendResponse, otherwise to HandleWritableFileError. | 59 // RegisterFileSystemsAndSendResponse, otherwise to HandleWritableFileError. |
73 void PrepareFilesForWritableApp(const std::vector<base::FilePath>& path); | 60 void PrepareFilesForWritableApp(const std::vector<base::FilePath>& path); |
(...skipping 22 matching lines...) Expand all Loading... |
96 bool is_directory_; | 83 bool is_directory_; |
97 }; | 84 }; |
98 | 85 |
99 class FileSystemGetWritableEntryFunction : public FileSystemEntryFunction { | 86 class FileSystemGetWritableEntryFunction : public FileSystemEntryFunction { |
100 public: | 87 public: |
101 DECLARE_EXTENSION_FUNCTION("fileSystem.getWritableEntry", | 88 DECLARE_EXTENSION_FUNCTION("fileSystem.getWritableEntry", |
102 FILESYSTEM_GETWRITABLEENTRY) | 89 FILESYSTEM_GETWRITABLEENTRY) |
103 | 90 |
104 protected: | 91 protected: |
105 ~FileSystemGetWritableEntryFunction() override {} | 92 ~FileSystemGetWritableEntryFunction() override {} |
106 bool RunAsync() override; | 93 ResponseAction Run() override; |
107 | 94 |
108 private: | 95 private: |
109 void CheckPermissionAndSendResponse(); | 96 void CheckPermissionAndSendResponse(); |
110 void SetIsDirectoryAsync(); | 97 void SetIsDirectoryAsync(); |
111 | 98 |
112 // The path to the file for which a writable entry has been requested. | 99 // The path to the file for which a writable entry has been requested. |
113 base::FilePath path_; | 100 base::FilePath path_; |
114 }; | 101 }; |
115 | 102 |
116 class FileSystemIsWritableEntryFunction : public UIThreadExtensionFunction { | 103 class FileSystemIsWritableEntryFunction : public UIThreadExtensionFunction { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 const AcceptOptions* accepts, | 138 const AcceptOptions* accepts, |
152 const bool* acceptsAllTypes); | 139 const bool* acceptsAllTypes); |
153 static void BuildSuggestion(const std::string* opt_name, | 140 static void BuildSuggestion(const std::string* opt_name, |
154 base::FilePath* suggested_name, | 141 base::FilePath* suggested_name, |
155 base::FilePath::StringType* suggested_extension); | 142 base::FilePath::StringType* suggested_extension); |
156 | 143 |
157 protected: | 144 protected: |
158 class FilePicker; | 145 class FilePicker; |
159 | 146 |
160 ~FileSystemChooseEntryFunction() override {} | 147 ~FileSystemChooseEntryFunction() override {} |
161 bool RunAsync() override; | 148 ResponseAction Run() override; |
162 void ShowPicker(const ui::SelectFileDialog::FileTypeInfo& file_type_info, | 149 void ShowPicker(const ui::SelectFileDialog::FileTypeInfo& file_type_info, |
163 ui::SelectFileDialog::Type picker_type); | 150 ui::SelectFileDialog::Type picker_type); |
164 | 151 |
165 private: | 152 private: |
166 void SetInitialPathAndShowPicker( | 153 void SetInitialPathAndShowPicker( |
167 const base::FilePath& previous_path, | 154 const base::FilePath& previous_path, |
168 const base::FilePath& suggested_name, | 155 const base::FilePath& suggested_name, |
169 const ui::SelectFileDialog::FileTypeInfo& file_type_info, | 156 const ui::SelectFileDialog::FileTypeInfo& file_type_info, |
170 ui::SelectFileDialog::Type picker_type, | 157 ui::SelectFileDialog::Type picker_type, |
171 bool is_path_non_native_directory); | 158 bool is_path_non_native_directory); |
172 | 159 |
173 // FilesSelected and FileSelectionCanceled are called by the file picker. | 160 // FilesSelected and FileSelectionCanceled are called by the file picker. |
174 void FilesSelected(const std::vector<base::FilePath>& path); | 161 void FilesSelected(const std::vector<base::FilePath>& path); |
175 void FileSelectionCanceled(); | 162 void FileSelectionCanceled(); |
176 | 163 |
177 // Check if the chosen directory is or is an ancestor of a sensitive | 164 // 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 | 165 // directory. If so, calls ConfirmSensitiveDirectoryAccess. Otherwise, calls |
179 // directory. Calls OnDirectoryAccessConfirmed if the directory isn't | 166 // OnDirectoryAccessConfirmed. |
180 // sensitive or the user chooses to open it. Otherwise, calls | |
181 // FileSelectionCanceled. | |
182 void ConfirmDirectoryAccessAsync(bool non_native_path, | 167 void ConfirmDirectoryAccessAsync(bool non_native_path, |
183 const std::vector<base::FilePath>& paths, | 168 const std::vector<base::FilePath>& paths, |
184 content::WebContents* web_contents); | 169 content::WebContents* web_contents); |
| 170 |
| 171 // Shows a dialog to confirm whether the user wants to open the directory. |
| 172 // Calls OnDirectoryAccessConfirmed or FileSelectionCanceled. |
| 173 void ConfirmSensitiveDirectoryAccess(const std::vector<base::FilePath>& paths, |
| 174 content::WebContents* web_contents); |
| 175 |
185 void OnDirectoryAccessConfirmed(const std::vector<base::FilePath>& paths); | 176 void OnDirectoryAccessConfirmed(const std::vector<base::FilePath>& paths); |
186 | 177 |
187 base::FilePath initial_path_; | 178 base::FilePath initial_path_; |
188 }; | 179 }; |
189 | 180 |
190 class FileSystemRetainEntryFunction : public ChromeAsyncExtensionFunction { | 181 class FileSystemRetainEntryFunction : public UIThreadExtensionFunction { |
191 public: | 182 public: |
192 DECLARE_EXTENSION_FUNCTION("fileSystem.retainEntry", FILESYSTEM_RETAINENTRY) | 183 DECLARE_EXTENSION_FUNCTION("fileSystem.retainEntry", FILESYSTEM_RETAINENTRY) |
193 | 184 |
194 protected: | 185 protected: |
195 ~FileSystemRetainEntryFunction() override {} | 186 ~FileSystemRetainEntryFunction() override {} |
196 bool RunAsync() override; | 187 ResponseAction Run() override; |
197 | 188 |
198 private: | 189 private: |
199 // Retains the file entry referenced by |entry_id| in apps::SavedFilesService. | 190 // 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 | 191 // |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 | 192 // path of the entry. |file_info| is base::File::Info of the entry if it can |
202 // be obtained. | 193 // be obtained. |
203 void RetainFileEntry(const std::string& entry_id, | 194 void RetainFileEntry(const std::string& entry_id, |
204 const base::FilePath& path, | 195 const base::FilePath& path, |
205 std::unique_ptr<base::File::Info> file_info); | 196 std::unique_ptr<base::File::Info> file_info); |
206 }; | 197 }; |
207 | 198 |
208 class FileSystemIsRestorableFunction : public UIThreadExtensionFunction { | 199 class FileSystemIsRestorableFunction : public UIThreadExtensionFunction { |
209 public: | 200 public: |
210 DECLARE_EXTENSION_FUNCTION("fileSystem.isRestorable", FILESYSTEM_ISRESTORABLE) | 201 DECLARE_EXTENSION_FUNCTION("fileSystem.isRestorable", FILESYSTEM_ISRESTORABLE) |
211 | 202 |
212 protected: | 203 protected: |
213 ~FileSystemIsRestorableFunction() override {} | 204 ~FileSystemIsRestorableFunction() override {} |
214 ResponseAction Run() override; | 205 ResponseAction Run() override; |
215 }; | 206 }; |
216 | 207 |
217 class FileSystemRestoreEntryFunction : public FileSystemEntryFunction { | 208 class FileSystemRestoreEntryFunction : public FileSystemEntryFunction { |
218 public: | 209 public: |
219 DECLARE_EXTENSION_FUNCTION("fileSystem.restoreEntry", FILESYSTEM_RESTOREENTRY) | 210 DECLARE_EXTENSION_FUNCTION("fileSystem.restoreEntry", FILESYSTEM_RESTOREENTRY) |
220 | 211 |
221 protected: | 212 protected: |
222 ~FileSystemRestoreEntryFunction() override {} | 213 ~FileSystemRestoreEntryFunction() override {} |
223 bool RunAsync() override; | 214 ResponseAction Run() override; |
224 }; | 215 }; |
225 | 216 |
226 class FileSystemObserveDirectoryFunction : public UIThreadExtensionFunction { | 217 class FileSystemObserveDirectoryFunction : public UIThreadExtensionFunction { |
227 public: | 218 public: |
228 DECLARE_EXTENSION_FUNCTION("fileSystem.observeDirectory", | 219 DECLARE_EXTENSION_FUNCTION("fileSystem.observeDirectory", |
229 FILESYSTEM_OBSERVEDIRECTORY) | 220 FILESYSTEM_OBSERVEDIRECTORY) |
230 | 221 |
231 protected: | 222 protected: |
232 ~FileSystemObserveDirectoryFunction() override {} | 223 ~FileSystemObserveDirectoryFunction() override {} |
233 ResponseAction Run() override; | 224 ResponseAction Run() override; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 | 281 |
291 protected: | 282 protected: |
292 ~FileSystemRequestFileSystemFunction() override; | 283 ~FileSystemRequestFileSystemFunction() override; |
293 | 284 |
294 // UIThreadExtensionFunction overrides. | 285 // UIThreadExtensionFunction overrides. |
295 ExtensionFunction::ResponseAction Run() override; | 286 ExtensionFunction::ResponseAction Run() override; |
296 | 287 |
297 private: | 288 private: |
298 // Called when a user grants or rejects permissions for the file system | 289 // Called when a user grants or rejects permissions for the file system |
299 // access. | 290 // access. |
300 void OnConsentReceived(const base::WeakPtr<file_manager::Volume>& volume, | 291 void OnGotFileSystem(const std::string& id, const std::string& path); |
301 bool writable, | 292 void OnError(const std::string& error); |
302 file_system_api::ConsentProvider::Consent result); | |
303 | |
304 ChromeExtensionFunctionDetails chrome_details_; | |
305 }; | 293 }; |
306 | 294 |
307 // Requests a list of available volumes. | 295 // Requests a list of available volumes. |
308 class FileSystemGetVolumeListFunction : public UIThreadExtensionFunction { | 296 class FileSystemGetVolumeListFunction : public UIThreadExtensionFunction { |
309 public: | 297 public: |
310 DECLARE_EXTENSION_FUNCTION("fileSystem.getVolumeList", | 298 DECLARE_EXTENSION_FUNCTION("fileSystem.getVolumeList", |
311 FILESYSTEM_GETVOLUMELIST); | 299 FILESYSTEM_GETVOLUMELIST); |
312 FileSystemGetVolumeListFunction(); | 300 FileSystemGetVolumeListFunction(); |
313 | 301 |
314 protected: | 302 protected: |
315 ~FileSystemGetVolumeListFunction() override; | 303 ~FileSystemGetVolumeListFunction() override; |
316 | 304 |
317 // UIThreadExtensionFunction overrides. | 305 // UIThreadExtensionFunction overrides. |
318 ExtensionFunction::ResponseAction Run() override; | 306 ExtensionFunction::ResponseAction Run() override; |
319 | 307 |
320 private: | 308 private: |
321 ChromeExtensionFunctionDetails chrome_details_; | 309 void OnGotVolumeList(const std::vector<api::file_system::Volume>& volumes); |
| 310 void OnError(const std::string& error); |
322 }; | 311 }; |
323 #endif | 312 #endif |
324 | 313 |
325 } // namespace extensions | 314 } // namespace extensions |
326 | 315 |
327 #endif // CHROME_BROWSER_EXTENSIONS_API_FILE_SYSTEM_FILE_SYSTEM_API_H_ | 316 #endif // EXTENSIONS_BROWSER_API_FILE_SYSTEM_FILE_SYSTEM_API_H_ |
OLD | NEW |