provided_file_system.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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/files/file.h"
8#include "chrome/browser/chromeos/file_system_provider/operations/close_file.h"
9#include "chrome/browser/chromeos/file_system_provider/operations/get_metadata.h"
10#include "chrome/browser/chromeos/file_system_provider/operations/open_file.h"
11#include "chrome/browser/chromeos/file_system_provider/operations/read_directory.h"
12#include "chrome/browser/chromeos/file_system_provider/operations/read_file.h"
13#include "chrome/browser/chromeos/file_system_provider/operations/unmount.h"
14#include "chrome/browser/chromeos/file_system_provider/request_manager.h"
15#include "chrome/common/extensions/api/file_system_provider.h"
16#include "extensions/browser/event_router.h"
17
18namespace net {
19class IOBuffer;
20}  // namespace net
21
22namespace chromeos {
23namespace file_system_provider {
24
25ProvidedFileSystem::ProvidedFileSystem(
26    extensions::EventRouter* event_router,
27    const ProvidedFileSystemInfo& file_system_info)
28    : event_router_(event_router),
29      file_system_info_(file_system_info),
30      weak_ptr_factory_(this) {
31}
32
33ProvidedFileSystem::~ProvidedFileSystem() {}
34
35void ProvidedFileSystem::RequestUnmount(
36    const fileapi::AsyncFileUtil::StatusCallback& callback) {
37  if (!request_manager_.CreateRequest(
38          RequestManager::REQUEST_UNMOUNT,
39          scoped_ptr<RequestManager::HandlerInterface>(new operations::Unmount(
40              event_router_, file_system_info_, callback)))) {
41    callback.Run(base::File::FILE_ERROR_SECURITY);
42  }
43}
44
45void ProvidedFileSystem::GetMetadata(
46    const base::FilePath& entry_path,
47    const fileapi::AsyncFileUtil::GetFileInfoCallback& callback) {
48  if (!request_manager_.CreateRequest(
49          RequestManager::GET_METADATA,
50          scoped_ptr<RequestManager::HandlerInterface>(
51              new operations::GetMetadata(
52                  event_router_, file_system_info_, entry_path, callback)))) {
53    callback.Run(base::File::FILE_ERROR_SECURITY, base::File::Info());
54  }
55}
56
57void ProvidedFileSystem::ReadDirectory(
58    const base::FilePath& directory_path,
59    const fileapi::AsyncFileUtil::ReadDirectoryCallback& callback) {
60  if (!request_manager_.CreateRequest(
61          RequestManager::READ_DIRECTORY,
62          scoped_ptr<
63              RequestManager::HandlerInterface>(new operations::ReadDirectory(
64              event_router_, file_system_info_, directory_path, callback)))) {
65    callback.Run(base::File::FILE_ERROR_SECURITY,
66                 fileapi::AsyncFileUtil::EntryList(),
67                 false /* has_more */);
68  }
69}
70
71void ProvidedFileSystem::ReadFile(int file_handle,
72                                  net::IOBuffer* buffer,
73                                  int64 offset,
74                                  int length,
75                                  const ReadChunkReceivedCallback& callback) {
76  if (!request_manager_.CreateRequest(
77          RequestManager::READ_FILE,
78          make_scoped_ptr<RequestManager::HandlerInterface>(
79              new operations::ReadFile(event_router_,
80                                       file_system_info_,
81                                       file_handle,
82                                       buffer,
83                                       offset,
84                                       length,
85                                       callback)))) {
86    callback.Run(0 /* chunk_length */,
87                 false /* has_more */,
88                 base::File::FILE_ERROR_SECURITY);
89  }
90}
91
92void ProvidedFileSystem::OpenFile(const base::FilePath& file_path,
93                                  OpenFileMode mode,
94                                  bool create,
95                                  const OpenFileCallback& callback) {
96  // Writing is not supported. Note, that this includes a situation, when a file
97  // exists, but |create| is set to true.
98  if (mode == OPEN_FILE_MODE_WRITE || create) {
99    callback.Run(0 /* file_handle */, base::File::FILE_ERROR_SECURITY);
100    return;
101  }
102
103  if (!request_manager_.CreateRequest(
104          RequestManager::OPEN_FILE,
105          scoped_ptr<RequestManager::HandlerInterface>(
106              new operations::OpenFile(event_router_,
107                                       file_system_info_,
108                                       file_path,
109                                       mode,
110                                       create,
111                                       callback)))) {
112    callback.Run(0 /* file_handle */, base::File::FILE_ERROR_SECURITY);
113  }
114}
115
116void ProvidedFileSystem::CloseFile(
117    int file_handle,
118    const fileapi::AsyncFileUtil::StatusCallback& callback) {
119  if (!request_manager_.CreateRequest(
120          RequestManager::CLOSE_FILE,
121          scoped_ptr<RequestManager::HandlerInterface>(
122              new operations::CloseFile(
123                  event_router_, file_system_info_, file_handle, callback)))) {
124    callback.Run(base::File::FILE_ERROR_SECURITY);
125  }
126}
127
128const ProvidedFileSystemInfo& ProvidedFileSystem::GetFileSystemInfo() const {
129  return file_system_info_;
130}
131
132RequestManager* ProvidedFileSystem::GetRequestManager() {
133  return &request_manager_;
134}
135
136base::WeakPtr<ProvidedFileSystemInterface> ProvidedFileSystem::GetWeakPtr() {
137  return weak_ptr_factory_.GetWeakPtr();
138}
139
140}  // namespace file_system_provider
141}  // namespace chromeos
142