• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string>
9 #include <vector>
10 
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"
15 
16 namespace extensions {
17 class ExtensionPrefs;
18 
19 namespace file_system_api {
20 
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.
24 
25 // Returns an empty path on failure.
26 base::FilePath GetLastChooseEntryDirectory(const ExtensionPrefs* prefs,
27                                            const std::string& extension_id);
28 
29 void SetLastChooseEntryDirectory(ExtensionPrefs* prefs,
30                                  const std::string& extension_id,
31                                  const base::FilePath& path);
32 
33 std::vector<base::FilePath> GetGrayListedDirectories();
34 
35 }  // namespace file_system_api
36 
37 class FileSystemGetDisplayPathFunction : public ChromeSyncExtensionFunction {
38  public:
39   DECLARE_EXTENSION_FUNCTION("fileSystem.getDisplayPath",
40                              FILESYSTEM_GETDISPLAYPATH)
41 
42  protected:
~FileSystemGetDisplayPathFunction()43   virtual ~FileSystemGetDisplayPathFunction() {}
44   virtual bool RunSync() OVERRIDE;
45 };
46 
47 class FileSystemEntryFunction : public ChromeAsyncExtensionFunction {
48  protected:
49   FileSystemEntryFunction();
50 
~FileSystemEntryFunction()51   virtual ~FileSystemEntryFunction() {}
52 
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);
58 
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
61   // thread.
62   void RegisterFileSystemsAndSendResponse(
63       const std::vector<base::FilePath>& path);
64 
65   // Creates a response dictionary and sets it as the response to be sent.
66   void CreateResponse();
67 
68   // Adds an entry to the response dictionary.
69   void AddEntryToResponse(const base::FilePath& path,
70                           const std::string& id_override);
71 
72   // called on the UI thread if there is a problem checking a writable file.
73   void HandleWritableFileError(const base::FilePath& error_path);
74 
75   // Whether multiple entries have been requested.
76   bool multiple_;
77 
78   // Whether a directory has been requested.
79   bool is_directory_;
80 
81   // The dictionary to send as the response.
82   base::DictionaryValue* response_;
83 };
84 
85 class FileSystemGetWritableEntryFunction : public FileSystemEntryFunction {
86  public:
87   DECLARE_EXTENSION_FUNCTION("fileSystem.getWritableEntry",
88                              FILESYSTEM_GETWRITABLEENTRY)
89 
90  protected:
~FileSystemGetWritableEntryFunction()91   virtual ~FileSystemGetWritableEntryFunction() {}
92   virtual bool RunAsync() OVERRIDE;
93 
94  private:
95   void CheckPermissionAndSendResponse();
96   void SetIsDirectoryOnFileThread();
97 
98   // The path to the file for which a writable entry has been requested.
99   base::FilePath path_;
100 };
101 
102 class FileSystemIsWritableEntryFunction : public ChromeSyncExtensionFunction {
103  public:
104   DECLARE_EXTENSION_FUNCTION("fileSystem.isWritableEntry",
105                              FILESYSTEM_ISWRITABLEENTRY)
106 
107  protected:
~FileSystemIsWritableEntryFunction()108   virtual ~FileSystemIsWritableEntryFunction() {}
109   virtual bool RunSync() OVERRIDE;
110 };
111 
112 class FileSystemChooseEntryFunction : public FileSystemEntryFunction {
113  public:
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
127   // Drive support.
128   static void RegisterTempExternalFileSystemForTest(const std::string& name,
129                                                     const base::FilePath& path);
130 
131   DECLARE_EXTENSION_FUNCTION("fileSystem.chooseEntry", FILESYSTEM_CHOOSEENTRY)
132 
133   typedef std::vector<linked_ptr<extensions::api::file_system::AcceptOption> >
134       AcceptOptions;
135 
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);
144 
145  protected:
146   class FilePicker;
147 
~FileSystemChooseEntryFunction()148   virtual ~FileSystemChooseEntryFunction() {}
149   virtual bool RunAsync() OVERRIDE;
150   void ShowPicker(const ui::SelectFileDialog::FileTypeInfo& file_type_info,
151                   ui::SelectFileDialog::Type picker_type);
152 
153  private:
154   void SetInitialPathOnFileThread(const base::FilePath& suggested_name,
155                                   const base::FilePath& previous_path);
156 
157   // FilesSelected and FileSelectionCanceled are called by the file picker.
158   void FilesSelected(const std::vector<base::FilePath>& path);
159   void FileSelectionCanceled();
160 
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);
171 
172   base::FilePath initial_path_;
173 };
174 
175 class FileSystemRetainEntryFunction : public ChromeAsyncExtensionFunction {
176  public:
177   DECLARE_EXTENSION_FUNCTION("fileSystem.retainEntry", FILESYSTEM_RETAINENTRY)
178 
179  protected:
~FileSystemRetainEntryFunction()180   virtual ~FileSystemRetainEntryFunction() {}
181   virtual bool RunAsync() OVERRIDE;
182 
183  private:
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);
187 
188   void SetIsDirectoryOnFileThread();
189 
190   // Whether the file being retained is a directory.
191   bool is_directory_;
192 
193   // The path to the file to retain.
194   base::FilePath path_;
195 };
196 
197 class FileSystemIsRestorableFunction : public ChromeSyncExtensionFunction {
198  public:
199   DECLARE_EXTENSION_FUNCTION("fileSystem.isRestorable", FILESYSTEM_ISRESTORABLE)
200 
201  protected:
~FileSystemIsRestorableFunction()202   virtual ~FileSystemIsRestorableFunction() {}
203   virtual bool RunSync() OVERRIDE;
204 };
205 
206 class FileSystemRestoreEntryFunction : public FileSystemEntryFunction {
207  public:
208   DECLARE_EXTENSION_FUNCTION("fileSystem.restoreEntry", FILESYSTEM_RESTOREENTRY)
209 
210  protected:
~FileSystemRestoreEntryFunction()211   virtual ~FileSystemRestoreEntryFunction() {}
212   virtual bool RunAsync() OVERRIDE;
213 };
214 
215 class FileSystemObserveDirectoryFunction : public ChromeSyncExtensionFunction {
216  public:
217   DECLARE_EXTENSION_FUNCTION("fileSystem.observeDirectory",
218                              FILESYSTEM_OBSERVEDIRECTORY)
219 
220  protected:
~FileSystemObserveDirectoryFunction()221   virtual ~FileSystemObserveDirectoryFunction() {}
222   virtual bool RunSync() OVERRIDE;
223 };
224 
225 class FileSystemUnobserveEntryFunction : public ChromeSyncExtensionFunction {
226  public:
227   DECLARE_EXTENSION_FUNCTION("fileSystem.unobserveEntry",
228                              FILESYSTEM_UNOBSERVEENTRY)
229 
230  protected:
~FileSystemUnobserveEntryFunction()231   virtual ~FileSystemUnobserveEntryFunction() {}
232   virtual bool RunSync() OVERRIDE;
233 };
234 
235 class FileSystemGetObservedEntriesFunction
236     : public ChromeSyncExtensionFunction {
237  public:
238   DECLARE_EXTENSION_FUNCTION("fileSystem.getObservedEntries",
239                              FILESYSTEM_GETOBSERVEDENTRIES);
240 
241  protected:
~FileSystemGetObservedEntriesFunction()242   virtual ~FileSystemGetObservedEntriesFunction() {}
243   virtual bool RunSync() OVERRIDE;
244 };
245 
246 }  // namespace extensions
247 
248 #endif  // CHROME_BROWSER_EXTENSIONS_API_FILE_SYSTEM_FILE_SYSTEM_API_H_
249