1// Copyright (c) 2011 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_EXTENSION_FILE_BROWSER_PRIVATE_API_H_
6#define CHROME_BROWSER_EXTENSIONS_EXTENSION_FILE_BROWSER_PRIVATE_API_H_
7#pragma once
8
9#include <map>
10#include <string>
11#include <vector>
12
13#include "base/platform_file.h"
14#include "chrome/browser/extensions/extension_function.h"
15#include "chrome/browser/ui/shell_dialogs.h"
16#include "googleurl/src/url_util.h"
17#include "webkit/fileapi/file_system_callback_dispatcher.h"
18
19class GURL;
20class HtmlDialogView;
21
22// Implements the chrome.fileBrowserPrivate.requestLocalFileSystem method.
23class RequestLocalFileSystemFunction : public AsyncExtensionFunction {
24 protected:
25  friend class LocalFileSystemCallbackDispatcher;
26  // AsyncExtensionFunction overrides.
27  virtual bool RunImpl() OVERRIDE;
28  void RespondSuccessOnUIThread(const std::string& name,
29                                const GURL& root_path);
30  void RespondFailedOnUIThread(base::PlatformFileError error_code);
31  void RequestOnFileThread(const GURL& source_url, int child_id);
32  DECLARE_EXTENSION_FUNCTION_NAME("fileBrowserPrivate.requestLocalFileSystem");
33};
34
35// Implements the chrome.fileBrowserPrivate.getFileTasks method.
36class GetFileTasksFileBrowserFunction : public AsyncExtensionFunction {
37 protected:
38  // AsyncExtensionFunction overrides.
39  virtual bool RunImpl() OVERRIDE;
40
41 private:
42  DECLARE_EXTENSION_FUNCTION_NAME("fileBrowserPrivate.getFileTasks");
43};
44
45
46// Implements the chrome.fileBrowserPrivate.executeTask method.
47class ExecuteTasksFileBrowserFunction : public AsyncExtensionFunction {
48 protected:
49  // AsyncExtensionFunction overrides.
50  virtual bool RunImpl() OVERRIDE;
51
52 private:
53  struct FileDefinition {
54    GURL target_file_url;
55    FilePath virtual_path;
56    bool is_directory;
57  };
58  typedef std::vector<FileDefinition> FileDefinitionList;
59  friend class ExecuteTasksFileSystemCallbackDispatcher;
60  // Initates execution of context menu tasks identified with |task_id| for
61  // each element of |files_list|.
62  bool InitiateFileTaskExecution(const std::string& task_id,
63                                 ListValue* files_list);
64  void RequestFileEntryOnFileThread(const GURL& source_url,
65                                    const std::string& task_id,
66                                    const std::vector<GURL>& file_urls);
67  void RespondFailedOnUIThread(base::PlatformFileError error_code);
68  void ExecuteFileActionsOnUIThread(const std::string& task_id,
69                                    const std::string& file_system_name,
70                                    const GURL& file_system_root,
71                                    const FileDefinitionList& file_list);
72  void ExecuteFailedOnUIThread();
73  DECLARE_EXTENSION_FUNCTION_NAME("fileBrowserPrivate.executeTask");
74};
75
76// Parent class for the chromium extension APIs for the file dialog.
77class FileDialogFunction
78    : public AsyncExtensionFunction {
79 public:
80  FileDialogFunction();
81
82  // Register/unregister callbacks.
83  // When file selection events occur in a tab, we'll call back the
84  // appropriate listener with the right params.
85  class Callback {
86   public:
87    Callback(SelectFileDialog::Listener* listener,
88             HtmlDialogView* dialog,
89             void* params)
90        : listener_(listener),
91          dialog_(dialog),
92          params_(params) {
93    }
94    SelectFileDialog::Listener* listener() const { return listener_; }
95    HtmlDialogView* dialog() const { return dialog_; }
96    void* params() const { return params_; }
97    bool IsNull() const { return listener_ == NULL; }
98
99    static void Add(int32 tab_id,
100                    SelectFileDialog::Listener* listener,
101                    HtmlDialogView* dialog,
102                    void* params);
103    static void Remove(int32 tab_id);
104    static const Callback& Find(int32 tab_id);
105    static const Callback& null() { return null_; }
106
107   private:
108
109    SelectFileDialog::Listener* listener_;
110    HtmlDialogView* dialog_;
111    void* params_;
112
113    // statics.
114    typedef std::map<int32, Callback> Map;
115    static Map map_;
116    static Callback null_;
117  };
118
119 protected:
120  typedef std::vector<GURL> UrlList;
121  typedef std::vector<FilePath> FilePathList;
122
123  virtual ~FileDialogFunction();
124
125  // Convert virtual paths to local paths on the file thread.
126  void GetLocalPathsOnFileThread(const UrlList& file_urls,
127                                 const std::string& internal_task_id);
128
129  // Callback with converted local paths.
130  virtual void GetLocalPathsResponseOnUIThread(const FilePathList& files,
131      const std::string& internal_task_id) {}
132
133  // Get the callback for the hosting tab.
134  const Callback& GetCallback() const;
135
136  // Closes the dialog window containing the file dialog HtmlDialogView.
137  void CloseDialog(HtmlDialogView* dialog);
138
139 private:
140  // Figure out the tab_id of the hosting tab.
141  int32 GetTabId() const;
142};
143
144// Select a single file.  Closes the dialog window.
145class SelectFileFunction
146    : public FileDialogFunction {
147 public:
148  SelectFileFunction() {}
149
150 protected:
151  virtual ~SelectFileFunction() {}
152
153  // AsyncExtensionFunction overrides.
154  virtual bool RunImpl() OVERRIDE;
155
156  // FileDialogFunction overrides.
157  virtual void GetLocalPathsResponseOnUIThread(
158      const FilePathList& files, const std::string& internal_task_id) OVERRIDE;
159
160 private:
161  DECLARE_EXTENSION_FUNCTION_NAME("fileBrowserPrivate.selectFile");
162};
163
164// View multiple selected files.  Window stays open.
165class ViewFilesFunction
166    : public FileDialogFunction {
167 public:
168  ViewFilesFunction();
169
170 protected:
171  virtual ~ViewFilesFunction();
172
173  // AsyncExtensionFunction overrides.
174  virtual bool RunImpl() OVERRIDE;
175
176  // FileDialogFunction overrides.
177  virtual void GetLocalPathsResponseOnUIThread(
178      const FilePathList& files, const std::string& internal_task_id) OVERRIDE;
179
180 private:
181  DECLARE_EXTENSION_FUNCTION_NAME("fileBrowserPrivate.viewFiles");
182};
183
184// Select multiple files.  Closes the dialog window.
185class SelectFilesFunction
186    : public FileDialogFunction {
187 public:
188  SelectFilesFunction();
189
190 protected:
191  virtual ~SelectFilesFunction();
192
193  // AsyncExtensionFunction overrides.
194  virtual bool RunImpl() OVERRIDE;
195
196  // FileDialogFunction overrides.
197  virtual void GetLocalPathsResponseOnUIThread(
198      const FilePathList& files, const std::string& internal_task_id) OVERRIDE;
199
200 private:
201  DECLARE_EXTENSION_FUNCTION_NAME("fileBrowserPrivate.selectFiles");
202};
203
204// Cancel file selection Dialog.  Closes the dialog window.
205class CancelFileDialogFunction
206    : public FileDialogFunction {
207 public:
208  CancelFileDialogFunction() {}
209
210 protected:
211  virtual ~CancelFileDialogFunction() {}
212
213  // AsyncExtensionFunction overrides.
214  virtual bool RunImpl() OVERRIDE;
215
216 private:
217  DECLARE_EXTENSION_FUNCTION_NAME("fileBrowserPrivate.cancelDialog");
218};
219
220// File Dialog Strings.
221class FileDialogStringsFunction : public SyncExtensionFunction {
222 public:
223  FileDialogStringsFunction() {}
224
225 protected:
226  virtual ~FileDialogStringsFunction() {}
227
228  // SyncExtensionFunction overrides.
229  virtual bool RunImpl() OVERRIDE;
230
231 private:
232  DECLARE_EXTENSION_FUNCTION_NAME("fileBrowserPrivate.getStrings");
233};
234
235#endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_FILE_BROWSER_PRIVATE_API_H_
236