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