summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_file_browser_private_api.h
blob: 45391cf4eb4208bb4a3804429bc3dbdc5d0c9f7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_FILE_BROWSER_PRIVATE_API_H_
#define CHROME_BROWSER_EXTENSIONS_EXTENSION_FILE_BROWSER_PRIVATE_API_H_
#pragma once

#include <map>
#include <string>
#include <vector>

#include "base/platform_file.h"
#include "chrome/browser/extensions/extension_function.h"
#include "chrome/browser/ui/shell_dialogs.h"
#include "webkit/fileapi/file_system_callback_dispatcher.h"

class GURL;

// Implements the Chrome Extension local File API.
class RequestLocalFileSystemFunction
    : public AsyncExtensionFunction {
 public:
  RequestLocalFileSystemFunction();

 protected:
  virtual ~RequestLocalFileSystemFunction();

  // AsyncExtensionFunction overrides.
  virtual bool RunImpl() OVERRIDE;

 private:
  friend class LocalFileSystemCallbackDispatcher;
  void RespondSuccessOnUIThread(const std::string& name,
                                const GURL& root);
  void RespondFailedOnUIThread(base::PlatformFileError error_code);

  DECLARE_EXTENSION_FUNCTION_NAME("fileBrowserPrivate.requestLocalFileSystem");
};

// Parent class for the chromium extension APIs for the file dialog.
class FileDialogFunction
    : public AsyncExtensionFunction {
 public:
  typedef std::vector<std::string> VirtualPathVec;
  typedef std::vector<FilePath> FilePathVec;

  FileDialogFunction();

  // Methods to register/unregister <tab_id, listener> tuples.
  // When file selection events occur in a tab, we'll call back the
  // appropriate listener.
  static void AddListener(int32 tab_id, SelectFileDialog::Listener* l);
  static void RemoveListener(int32 tab_id);

 protected:
  virtual ~FileDialogFunction();

  // Convert virtual paths to local paths on the file thread.
  void GetLocalPathsOnFileThread();

  // Callback with converted local paths.
  virtual void GetLocalPathsResponseOnUIThread() {}

  // Get the listener for the hosting tab.
  SelectFileDialog::Listener* GetListener() const;

  VirtualPathVec virtual_paths_;
  FilePathVec selected_files_;

 private:
  // Figure out the tab_id of the hosting tab.
  int32 GetTabId() const;

  typedef std::map<int32, SelectFileDialog::Listener*> ListenerMap;
  static ListenerMap listener_map_;
};

// Select a single file.
class SelectFileFunction
    : public FileDialogFunction {
 public:
  SelectFileFunction() {}

 protected:
  virtual ~SelectFileFunction() {}

  // AsyncExtensionFunction overrides.
  virtual bool RunImpl() OVERRIDE;

  // FileDialogFunction overrides.
  virtual void GetLocalPathsResponseOnUIThread() OVERRIDE;

 private:
  DECLARE_EXTENSION_FUNCTION_NAME("fileBrowserPrivate.selectFile");
};

// Select multiple files.
class SelectFilesFunction
    : public FileDialogFunction {
 public:
  SelectFilesFunction();

 protected:
  virtual ~SelectFilesFunction();

  // AsyncExtensionFunction overrides.
  virtual bool RunImpl() OVERRIDE;

  // FileDialogFunction overrides.
  virtual void GetLocalPathsResponseOnUIThread() OVERRIDE;

 private:
  DECLARE_EXTENSION_FUNCTION_NAME("fileBrowserPrivate.selectFiles");
};

// Cancel file selection Dialog.
class CancelFileDialogFunction
    : public FileDialogFunction {
 public:
  CancelFileDialogFunction() {}

 protected:
  virtual ~CancelFileDialogFunction() {}

  // AsyncExtensionFunction overrides.
  virtual bool RunImpl() OVERRIDE;

 private:
  DECLARE_EXTENSION_FUNCTION_NAME("fileBrowserPrivate.cancelDialog");
};

// File Dialog Strings.
class FileDialogStringsFunction
    : public FileDialogFunction {
 public:
  FileDialogStringsFunction() {}

 protected:
  virtual ~FileDialogStringsFunction() {}

  // AsyncExtensionFunction overrides.
  virtual bool RunImpl() OVERRIDE;

 private:
  DECLARE_EXTENSION_FUNCTION_NAME("fileBrowserPrivate.getStrings");
};

#endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_FILE_BROWSER_PRIVATE_API_H_