private_api_file_system.h revision 010d83a9304c5a91596085d917d248abff47903a
1// Copyright 2013 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// This file provides file system related API functions.
6
7#ifndef CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_PRIVATE_API_FILE_SYSTEM_H_
8#define CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_PRIVATE_API_FILE_SYSTEM_H_
9
10#include <string>
11
12#include "base/platform_file.h"
13#include "chrome/browser/chromeos/drive/file_errors.h"
14#include "chrome/browser/chromeos/extensions/file_manager/private_api_base.h"
15
16class GURL;
17
18namespace base {
19class FilePath;
20}
21
22namespace fileapi {
23class FileSystemContext;
24}
25
26namespace file_manager {
27namespace util {
28struct EntryDefinition;
29}  // namespace util
30}  // namespace file_manager
31
32namespace extensions {
33
34// Implements the chrome.fileBrowserPrivate.requestFileSystem method.
35class FileBrowserPrivateRequestFileSystemFunction
36    : public LoggedAsyncExtensionFunction {
37 public:
38  DECLARE_EXTENSION_FUNCTION("fileBrowserPrivate.requestFileSystem",
39                             FILEBROWSERPRIVATE_REQUESTFILESYSTEM)
40
41 protected:
42  virtual ~FileBrowserPrivateRequestFileSystemFunction() {}
43
44  // AsyncExtensionFunction overrides.
45  virtual bool RunAsync() OVERRIDE;
46
47 private:
48  void RespondSuccessOnUIThread(const std::string& name,
49                                const GURL& root_url);
50  void RespondFailedOnUIThread(base::File::Error error_code);
51
52  // Called when something goes wrong. Records the error to |error_| per the
53  // error code and reports that the private API function failed.
54  void DidFail(base::File::Error error_code);
55
56  // Sets up file system access permissions to the extension identified by
57  // |child_id|.
58  bool SetupFileSystemAccessPermissions(
59      scoped_refptr<fileapi::FileSystemContext> file_system_context,
60      int child_id,
61      Profile* profile,
62      scoped_refptr<const extensions::Extension> extension);
63
64  // Called when the entry definition is computed.
65  void OnEntryDefinition(
66      const file_manager::util::EntryDefinition& entry_definition);
67};
68
69// Base class for FileBrowserPrivateAddFileWatchFunction and
70// FileBrowserPrivateRemoveFileWatchFunction. Although it's called "FileWatch",
71// the class and its sub classes are used only for watching changes in
72// directories.
73class FileWatchFunctionBase : public LoggedAsyncExtensionFunction {
74 protected:
75  virtual ~FileWatchFunctionBase() {}
76
77  // Performs a file watch operation (ex. adds or removes a file watch).
78  virtual void PerformFileWatchOperation(
79      const base::FilePath& local_path,
80      const base::FilePath& virtual_path,
81      const std::string& extension_id) = 0;
82
83  // AsyncExtensionFunction overrides.
84  virtual bool RunAsync() OVERRIDE;
85
86  // Calls SendResponse() with |success| converted to base::Value.
87  void Respond(bool success);
88};
89
90// Implements the chrome.fileBrowserPrivate.addFileWatch method.
91// Starts watching changes in directories.
92class FileBrowserPrivateAddFileWatchFunction : public FileWatchFunctionBase {
93 public:
94  DECLARE_EXTENSION_FUNCTION("fileBrowserPrivate.addFileWatch",
95                             FILEBROWSERPRIVATE_ADDFILEWATCH)
96
97 protected:
98  virtual ~FileBrowserPrivateAddFileWatchFunction() {}
99
100  // FileWatchFunctionBase override.
101  virtual void PerformFileWatchOperation(
102      const base::FilePath& local_path,
103      const base::FilePath& virtual_path,
104      const std::string& extension_id) OVERRIDE;
105};
106
107
108// Implements the chrome.fileBrowserPrivate.removeFileWatch method.
109// Stops watching changes in directories.
110class FileBrowserPrivateRemoveFileWatchFunction : public FileWatchFunctionBase {
111 public:
112  DECLARE_EXTENSION_FUNCTION("fileBrowserPrivate.removeFileWatch",
113                             FILEBROWSERPRIVATE_REMOVEFILEWATCH)
114
115 protected:
116  virtual ~FileBrowserPrivateRemoveFileWatchFunction() {}
117
118  // FileWatchFunctionBase override.
119  virtual void PerformFileWatchOperation(
120      const base::FilePath& local_path,
121      const base::FilePath& virtual_path,
122      const std::string& extension_id) OVERRIDE;
123};
124
125// Implements the chrome.fileBrowserPrivate.getSizeStats method.
126class FileBrowserPrivateGetSizeStatsFunction
127    : public LoggedAsyncExtensionFunction {
128 public:
129  DECLARE_EXTENSION_FUNCTION("fileBrowserPrivate.getSizeStats",
130                             FILEBROWSERPRIVATE_GETSIZESTATS)
131
132 protected:
133  virtual ~FileBrowserPrivateGetSizeStatsFunction() {}
134
135  // AsyncExtensionFunction overrides.
136  virtual bool RunAsync() OVERRIDE;
137
138 private:
139  void GetDriveAvailableSpaceCallback(drive::FileError error,
140                                      int64 bytes_total,
141                                      int64 bytes_used);
142
143  void GetSizeStatsCallback(const uint64* total_size,
144                            const uint64* remaining_size);
145};
146
147// Implements the chrome.fileBrowserPrivate.validatePathNameLength method.
148class FileBrowserPrivateValidatePathNameLengthFunction
149    : public LoggedAsyncExtensionFunction {
150 public:
151  DECLARE_EXTENSION_FUNCTION("fileBrowserPrivate.validatePathNameLength",
152                             FILEBROWSERPRIVATE_VALIDATEPATHNAMELENGTH)
153
154 protected:
155  virtual ~FileBrowserPrivateValidatePathNameLengthFunction() {}
156
157  void OnFilePathLimitRetrieved(size_t current_length, size_t max_length);
158
159  // AsyncExtensionFunction overrides.
160  virtual bool RunAsync() OVERRIDE;
161};
162
163// Implements the chrome.fileBrowserPrivate.formatVolume method.
164// Formats Volume given its mount path.
165class FileBrowserPrivateFormatVolumeFunction
166    : public LoggedAsyncExtensionFunction {
167 public:
168  DECLARE_EXTENSION_FUNCTION("fileBrowserPrivate.formatVolume",
169                             FILEBROWSERPRIVATE_FORMATVOLUME)
170
171 protected:
172  virtual ~FileBrowserPrivateFormatVolumeFunction() {}
173
174  // AsyncExtensionFunction overrides.
175  virtual bool RunAsync() OVERRIDE;
176};
177
178// Implements the chrome.fileBrowserPrivate.startCopy method.
179class FileBrowserPrivateStartCopyFunction
180    : public LoggedAsyncExtensionFunction {
181 public:
182  DECLARE_EXTENSION_FUNCTION("fileBrowserPrivate.startCopy",
183                             FILEBROWSERPRIVATE_STARTCOPY)
184
185 protected:
186  virtual ~FileBrowserPrivateStartCopyFunction() {}
187
188  // AsyncExtensionFunction overrides.
189  virtual bool RunAsync() OVERRIDE;
190
191 private:
192  // Part of RunAsync(). Called after Copy() is started on IO thread.
193  void RunAfterStartCopy(int operation_id);
194};
195
196// Implements the chrome.fileBrowserPrivate.cancelCopy method.
197class FileBrowserPrivateCancelCopyFunction
198    : public LoggedAsyncExtensionFunction {
199 public:
200  DECLARE_EXTENSION_FUNCTION("fileBrowserPrivate.cancelCopy",
201                             FILEBROWSERPRIVATE_CANCELCOPY)
202
203 protected:
204  virtual ~FileBrowserPrivateCancelCopyFunction() {}
205
206  // AsyncExtensionFunction overrides.
207  virtual bool RunAsync() OVERRIDE;
208};
209
210}  // namespace extensions
211
212#endif  // CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_PRIVATE_API_FILE_SYSTEM_H_
213