Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / file_system / file_system_api.h
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 <vector>
9
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"
14
15 namespace extensions {
16 class ExtensionPrefs;
17
18 namespace file_system_api {
19
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.
23
24 // Returns an empty path on failure.
25 base::FilePath GetLastChooseEntryDirectory(const ExtensionPrefs* prefs,
26                                            const std::string& extension_id);
27
28 void SetLastChooseEntryDirectory(ExtensionPrefs* prefs,
29                                  const std::string& extension_id,
30                                  const base::FilePath& path);
31
32 std::vector<base::FilePath> GetGrayListedDirectories();
33
34 }  // namespace file_system_api
35
36 class FileSystemGetDisplayPathFunction : public ChromeSyncExtensionFunction {
37  public:
38   DECLARE_EXTENSION_FUNCTION("fileSystem.getDisplayPath",
39                              FILESYSTEM_GETDISPLAYPATH)
40
41  protected:
42   virtual ~FileSystemGetDisplayPathFunction() {}
43   virtual bool RunSync() OVERRIDE;
44 };
45
46 class FileSystemEntryFunction : public ChromeAsyncExtensionFunction {
47  protected:
48   FileSystemEntryFunction();
49
50   virtual ~FileSystemEntryFunction() {}
51
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);
57
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
60   // thread.
61   void RegisterFileSystemsAndSendResponse(
62       const std::vector<base::FilePath>& path);
63
64   // Creates a response dictionary and sets it as the response to be sent.
65   void CreateResponse();
66
67   // Adds an entry to the response dictionary.
68   void AddEntryToResponse(const base::FilePath& path,
69                           const std::string& id_override);
70
71   // called on the UI thread if there is a problem checking a writable file.
72   void HandleWritableFileError(const base::FilePath& error_path);
73
74   // Whether multiple entries have been requested.
75   bool multiple_;
76
77   // Whether a directory has been requested.
78   bool is_directory_;
79
80   // The dictionary to send as the response.
81   base::DictionaryValue* response_;
82 };
83
84 class FileSystemGetWritableEntryFunction : public FileSystemEntryFunction {
85  public:
86   DECLARE_EXTENSION_FUNCTION("fileSystem.getWritableEntry",
87                              FILESYSTEM_GETWRITABLEENTRY)
88
89  protected:
90   virtual ~FileSystemGetWritableEntryFunction() {}
91   virtual bool RunAsync() OVERRIDE;
92
93  private:
94   void CheckPermissionAndSendResponse();
95   void SetIsDirectoryOnFileThread();
96
97   // The path to the file for which a writable entry has been requested.
98   base::FilePath path_;
99 };
100
101 class FileSystemIsWritableEntryFunction : public ChromeSyncExtensionFunction {
102  public:
103   DECLARE_EXTENSION_FUNCTION("fileSystem.isWritableEntry",
104                              FILESYSTEM_ISWRITABLEENTRY)
105
106  protected:
107   virtual ~FileSystemIsWritableEntryFunction() {}
108   virtual bool RunSync() OVERRIDE;
109 };
110
111 class FileSystemChooseEntryFunction : public FileSystemEntryFunction {
112  public:
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
126   // Drive support.
127   static void RegisterTempExternalFileSystemForTest(const std::string& name,
128                                                     const base::FilePath& path);
129
130   DECLARE_EXTENSION_FUNCTION("fileSystem.chooseEntry", FILESYSTEM_CHOOSEENTRY)
131
132   typedef std::vector<linked_ptr<extensions::api::file_system::AcceptOption> >
133       AcceptOptions;
134
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);
143
144  protected:
145   class FilePicker;
146
147   virtual ~FileSystemChooseEntryFunction() {}
148   virtual bool RunAsync() OVERRIDE;
149   void ShowPicker(const ui::SelectFileDialog::FileTypeInfo& file_type_info,
150                   ui::SelectFileDialog::Type picker_type);
151
152  private:
153   void SetInitialPathOnFileThread(const base::FilePath& suggested_name,
154                                   const base::FilePath& previous_path);
155
156   // FilesSelected and FileSelectionCanceled are called by the file picker.
157   void FilesSelected(const std::vector<base::FilePath>& path);
158   void FileSelectionCanceled();
159
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);
169
170   base::FilePath initial_path_;
171 };
172
173 class FileSystemRetainEntryFunction : public ChromeAsyncExtensionFunction {
174  public:
175   DECLARE_EXTENSION_FUNCTION("fileSystem.retainEntry", FILESYSTEM_RETAINENTRY)
176
177  protected:
178   virtual ~FileSystemRetainEntryFunction() {}
179   virtual bool RunAsync() OVERRIDE;
180
181  private:
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);
185
186   void SetIsDirectoryOnFileThread();
187
188   // Whether the file being retained is a directory.
189   bool is_directory_;
190
191   // The path to the file to retain.
192   base::FilePath path_;
193 };
194
195 class FileSystemIsRestorableFunction : public ChromeSyncExtensionFunction {
196  public:
197   DECLARE_EXTENSION_FUNCTION("fileSystem.isRestorable", FILESYSTEM_ISRESTORABLE)
198
199  protected:
200   virtual ~FileSystemIsRestorableFunction() {}
201   virtual bool RunSync() OVERRIDE;
202 };
203
204 class FileSystemRestoreEntryFunction : public FileSystemEntryFunction {
205  public:
206   DECLARE_EXTENSION_FUNCTION("fileSystem.restoreEntry", FILESYSTEM_RESTOREENTRY)
207
208  protected:
209   virtual ~FileSystemRestoreEntryFunction() {}
210   virtual bool RunAsync() OVERRIDE;
211 };
212
213 }  // namespace extensions
214
215 #endif  // CHROME_BROWSER_EXTENSIONS_API_FILE_SYSTEM_FILE_SYSTEM_API_H_