provided_file_system.cc revision 116680a4aac90f2aa7413d9095a592090648e557
1// Copyright 2014 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#include "chrome/browser/chromeos/file_system_provider/provided_file_system.h"
6
7#include "base/debug/trace_event.h"
8#include "base/files/file.h"
9#include "chrome/browser/chromeos/file_system_provider/notification_manager.h"
10#include "chrome/browser/chromeos/file_system_provider/operations/close_file.h"
11#include "chrome/browser/chromeos/file_system_provider/operations/create_directory.h"
12#include "chrome/browser/chromeos/file_system_provider/operations/delete_entry.h"
13#include "chrome/browser/chromeos/file_system_provider/operations/get_metadata.h"
14#include "chrome/browser/chromeos/file_system_provider/operations/open_file.h"
15#include "chrome/browser/chromeos/file_system_provider/operations/read_directory.h"
16#include "chrome/browser/chromeos/file_system_provider/operations/read_file.h"
17#include "chrome/browser/chromeos/file_system_provider/operations/unmount.h"
18#include "chrome/browser/chromeos/file_system_provider/request_manager.h"
19#include "chrome/browser/profiles/profile.h"
20#include "chrome/common/extensions/api/file_system_provider.h"
21#include "extensions/browser/event_router.h"
22
23namespace net {
24class IOBuffer;
25}  // namespace net
26
27namespace chromeos {
28namespace file_system_provider {
29
30ProvidedFileSystem::ProvidedFileSystem(
31    Profile* profile,
32    const ProvidedFileSystemInfo& file_system_info)
33    : profile_(profile),
34      event_router_(extensions::EventRouter::Get(profile)),  // May be NULL.
35      file_system_info_(file_system_info),
36      notification_manager_(
37          new NotificationManager(profile_, file_system_info_)),
38      request_manager_(notification_manager_.get()),
39      weak_ptr_factory_(this) {
40}
41
42ProvidedFileSystem::~ProvidedFileSystem() {}
43
44void ProvidedFileSystem::RequestUnmount(
45    const fileapi::AsyncFileUtil::StatusCallback& callback) {
46  if (!request_manager_.CreateRequest(
47          REQUEST_UNMOUNT,
48          scoped_ptr<RequestManager::HandlerInterface>(new operations::Unmount(
49              event_router_, file_system_info_, callback)))) {
50    callback.Run(base::File::FILE_ERROR_SECURITY);
51  }
52}
53
54void ProvidedFileSystem::GetMetadata(const base::FilePath& entry_path,
55                                     const GetMetadataCallback& callback) {
56  if (!request_manager_.CreateRequest(
57          GET_METADATA,
58          scoped_ptr<RequestManager::HandlerInterface>(
59              new operations::GetMetadata(
60                  event_router_, file_system_info_, entry_path, callback)))) {
61    callback.Run(EntryMetadata(), base::File::FILE_ERROR_SECURITY);
62  }
63}
64
65void ProvidedFileSystem::ReadDirectory(
66    const base::FilePath& directory_path,
67    const fileapi::AsyncFileUtil::ReadDirectoryCallback& callback) {
68  if (!request_manager_.CreateRequest(
69          READ_DIRECTORY,
70          scoped_ptr<
71              RequestManager::HandlerInterface>(new operations::ReadDirectory(
72              event_router_, file_system_info_, directory_path, callback)))) {
73    callback.Run(base::File::FILE_ERROR_SECURITY,
74                 fileapi::AsyncFileUtil::EntryList(),
75                 false /* has_more */);
76  }
77}
78
79void ProvidedFileSystem::ReadFile(int file_handle,
80                                  net::IOBuffer* buffer,
81                                  int64 offset,
82                                  int length,
83                                  const ReadChunkReceivedCallback& callback) {
84  TRACE_EVENT1(
85      "file_system_provider", "ProvidedFileSystem::ReadFile", "length", length);
86  if (!request_manager_.CreateRequest(
87          READ_FILE,
88          make_scoped_ptr<RequestManager::HandlerInterface>(
89              new operations::ReadFile(event_router_,
90                                       file_system_info_,
91                                       file_handle,
92                                       buffer,
93                                       offset,
94                                       length,
95                                       callback)))) {
96    callback.Run(0 /* chunk_length */,
97                 false /* has_more */,
98                 base::File::FILE_ERROR_SECURITY);
99  }
100}
101
102void ProvidedFileSystem::OpenFile(const base::FilePath& file_path,
103                                  OpenFileMode mode,
104                                  const OpenFileCallback& callback) {
105  // Writing is not supported.
106  if (mode == OPEN_FILE_MODE_WRITE) {
107    callback.Run(0 /* file_handle */, base::File::FILE_ERROR_SECURITY);
108    return;
109  }
110
111  if (!request_manager_.CreateRequest(
112          OPEN_FILE,
113          scoped_ptr<RequestManager::HandlerInterface>(
114              new operations::OpenFile(event_router_,
115                                       file_system_info_,
116                                       file_path,
117                                       mode,
118                                       callback)))) {
119    callback.Run(0 /* file_handle */, base::File::FILE_ERROR_SECURITY);
120  }
121}
122
123void ProvidedFileSystem::CloseFile(
124    int file_handle,
125    const fileapi::AsyncFileUtil::StatusCallback& callback) {
126  if (!request_manager_.CreateRequest(
127          CLOSE_FILE,
128          scoped_ptr<RequestManager::HandlerInterface>(
129              new operations::CloseFile(
130                  event_router_, file_system_info_, file_handle, callback)))) {
131    callback.Run(base::File::FILE_ERROR_SECURITY);
132  }
133}
134
135void ProvidedFileSystem::CreateDirectory(
136    const base::FilePath& directory_path,
137    bool exclusive,
138    bool recursive,
139    const fileapi::AsyncFileUtil::StatusCallback& callback) {
140  if (!request_manager_.CreateRequest(
141          CREATE_DIRECTORY,
142          scoped_ptr<RequestManager::HandlerInterface>(
143              new operations::CreateDirectory(event_router_,
144                                              file_system_info_,
145                                              directory_path,
146                                              exclusive,
147                                              recursive,
148                                              callback)))) {
149    callback.Run(base::File::FILE_ERROR_SECURITY);
150  }
151}
152
153void ProvidedFileSystem::DeleteEntry(
154    const base::FilePath& entry_path,
155    bool recursive,
156    const fileapi::AsyncFileUtil::StatusCallback& callback) {
157  if (!request_manager_.CreateRequest(
158          DELETE_ENTRY,
159          scoped_ptr<RequestManager::HandlerInterface>(
160              new operations::DeleteEntry(event_router_,
161                                          file_system_info_,
162                                          entry_path,
163                                          recursive,
164                                          callback)))) {
165    callback.Run(base::File::FILE_ERROR_SECURITY);
166  }
167}
168
169const ProvidedFileSystemInfo& ProvidedFileSystem::GetFileSystemInfo() const {
170  return file_system_info_;
171}
172
173RequestManager* ProvidedFileSystem::GetRequestManager() {
174  return &request_manager_;
175}
176
177base::WeakPtr<ProvidedFileSystemInterface> ProvidedFileSystem::GetWeakPtr() {
178  return weak_ptr_factory_.GetWeakPtr();
179}
180
181}  // namespace file_system_provider
182}  // namespace chromeos
183