private_api_file_system.h revision a36e5920737c6adbddd3e43b760e5de8431db6e0
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 {
27
28// Implements the chrome.fileBrowserPrivate.requestFileSystem method.
29class RequestFileSystemFunction : public LoggedAsyncExtensionFunction {
30 public:
31  DECLARE_EXTENSION_FUNCTION("fileBrowserPrivate.requestFileSystem",
32                             FILEBROWSERPRIVATE_REQUESTFILESYSTEM)
33
34  RequestFileSystemFunction();
35
36 protected:
37  virtual ~RequestFileSystemFunction();
38
39  // AsyncExtensionFunction overrides.
40  virtual bool RunImpl() OVERRIDE;
41
42 private:
43  void RespondSuccessOnUIThread(const std::string& name,
44                                const GURL& root_path);
45  void RespondFailedOnUIThread(base::PlatformFileError error_code);
46
47  // Called when FileSystemContext::OpenFileSystem() is done.
48  void DidOpenFileSystem(
49      scoped_refptr<fileapi::FileSystemContext> file_system_context,
50      base::PlatformFileError result,
51      const std::string& name,
52      const GURL& root_path);
53
54  // Called when something goes wrong. Records the error to |error_| per the
55  // error code and reports that the private API function failed.
56  void DidFail(base::PlatformFileError error_code);
57
58  // Sets up file system access permissions to the extension identified by
59  // |child_id|.
60  bool SetupFileSystemAccessPermissions(
61      scoped_refptr<fileapi::FileSystemContext> file_system_context,
62      int child_id,
63      scoped_refptr<const extensions::Extension> extension);
64};
65
66// Base class for AddFileWatchFunction and RemoveFileWatchFunction. Although
67// it's called "FileWatch", the class and its sub classes are used only for
68// watching changes in directories.
69class FileWatchFunctionBase : public LoggedAsyncExtensionFunction {
70 public:
71  FileWatchFunctionBase();
72
73 protected:
74  virtual ~FileWatchFunctionBase();
75
76  // Performs a file watch operation (ex. adds or removes a file watch).
77  virtual void PerformFileWatchOperation(
78      const base::FilePath& local_path,
79      const base::FilePath& virtual_path,
80      const std::string& extension_id) = 0;
81
82  // AsyncExtensionFunction overrides.
83  virtual bool RunImpl() OVERRIDE;
84
85  // Calls SendResponse() with |success| converted to base::Value.
86  void Respond(bool success);
87};
88
89// Implements the chrome.fileBrowserPrivate.addFileWatch method.
90// Starts watching changes in directories.
91class AddFileWatchFunction : public FileWatchFunctionBase {
92 public:
93  DECLARE_EXTENSION_FUNCTION("fileBrowserPrivate.addFileWatch",
94                             FILEBROWSERPRIVATE_ADDFILEWATCH)
95
96  AddFileWatchFunction();
97
98 protected:
99  virtual ~AddFileWatchFunction();
100
101  // FileWatchFunctionBase override.
102  virtual void PerformFileWatchOperation(
103      const base::FilePath& local_path,
104      const base::FilePath& virtual_path,
105      const std::string& extension_id) OVERRIDE;
106};
107
108
109// Implements the chrome.fileBrowserPrivate.removeFileWatch method.
110// Stops watching changes in directories.
111class RemoveFileWatchFunction : public FileWatchFunctionBase {
112 public:
113  DECLARE_EXTENSION_FUNCTION("fileBrowserPrivate.removeFileWatch",
114                             FILEBROWSERPRIVATE_REMOVEFILEWATCH)
115
116  RemoveFileWatchFunction();
117
118 protected:
119  virtual ~RemoveFileWatchFunction();
120
121  // FileWatchFunctionBase override.
122  virtual void PerformFileWatchOperation(
123      const base::FilePath& local_path,
124      const base::FilePath& virtual_path,
125      const std::string& extension_id) OVERRIDE;
126};
127
128// Implements the chrome.fileBrowserPrivate.setLastModified method.
129// Sets last modified date in seconds of local file
130class SetLastModifiedFunction : public LoggedAsyncExtensionFunction {
131 public:
132  DECLARE_EXTENSION_FUNCTION("fileBrowserPrivate.setLastModified",
133                             FILEBROWSERPRIVATE_SETLASTMODIFIED)
134
135  SetLastModifiedFunction();
136
137 protected:
138  virtual ~SetLastModifiedFunction();
139
140  // AsyncExtensionFunction overrides.
141  virtual bool RunImpl() OVERRIDE;
142};
143
144// Implements the chrome.fileBrowserPrivate.getSizeStats method.
145class GetSizeStatsFunction : public LoggedAsyncExtensionFunction {
146 public:
147  DECLARE_EXTENSION_FUNCTION("fileBrowserPrivate.getSizeStats",
148                             FILEBROWSERPRIVATE_GETSIZESTATS)
149
150  GetSizeStatsFunction();
151
152 protected:
153  virtual ~GetSizeStatsFunction();
154
155  // AsyncExtensionFunction overrides.
156  virtual bool RunImpl() OVERRIDE;
157
158 private:
159  void GetDriveAvailableSpaceCallback(drive::FileError error,
160                                      int64 bytes_total,
161                                      int64 bytes_used);
162
163  void GetSizeStatsCallback(const uint64* total_size,
164                            const uint64* remaining_size);
165};
166
167// Implements the chrome.fileBrowserPrivate.getVolumeMetadata method.
168// Retrieves devices meta-data. Expects volume's device path as an argument.
169class GetVolumeMetadataFunction : public LoggedAsyncExtensionFunction {
170 public:
171  DECLARE_EXTENSION_FUNCTION("fileBrowserPrivate.getVolumeMetadata",
172                             FILEBROWSERPRIVATE_GETVOLUMEMETADATA)
173
174  GetVolumeMetadataFunction();
175
176 protected:
177  virtual ~GetVolumeMetadataFunction();
178
179  // AsyncExtensionFunction overrides.
180  virtual bool RunImpl() OVERRIDE;
181};
182
183// Implements the chrome.fileBrowserPrivate.validatePathNameLength method.
184class ValidatePathNameLengthFunction : public LoggedAsyncExtensionFunction {
185 public:
186  DECLARE_EXTENSION_FUNCTION("fileBrowserPrivate.validatePathNameLength",
187                             FILEBROWSERPRIVATE_VALIDATEPATHNAMELENGTH)
188
189  ValidatePathNameLengthFunction();
190
191 protected:
192  virtual ~ValidatePathNameLengthFunction();
193
194  void OnFilePathLimitRetrieved(size_t current_length, size_t max_length);
195
196  // AsyncExtensionFunction overrides.
197  virtual bool RunImpl() OVERRIDE;
198};
199
200// Implements the chrome.fileBrowserPrivate.formatDevice method.
201// Formats Device given its mount path.
202class FormatDeviceFunction : public LoggedAsyncExtensionFunction {
203 public:
204  DECLARE_EXTENSION_FUNCTION("fileBrowserPrivate.formatDevice",
205                             FILEBROWSERPRIVATE_FORMATDEVICE)
206
207  FormatDeviceFunction();
208
209 protected:
210  virtual ~FormatDeviceFunction();
211
212  // AsyncExtensionFunction overrides.
213  virtual bool RunImpl() OVERRIDE;
214};
215
216}  // namespace file_manager
217
218#endif  // CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_PRIVATE_API_FILE_SYSTEM_H_
219