summaryrefslogtreecommitdiffstats
path: root/chrome/browser/file_system/file_system_backend.cc
blob: 12847ed1a8f089cd01e20f11d56e0912de2633dc (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright (c) 2010 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.

#include "base/file_util_proxy.h"
#include "base/platform_file.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/file_system/file_system_backend.h"
#include "chrome/browser/file_system/file_system_backend_client.h"
#include "third_party/WebKit/WebKit/chromium/public/WebFileError.h"

namespace {
// Utility method for error conversions.
WebKit::WebFileError PlatformToWebkitError(base::PlatformFileError rv) {
  switch (rv) {
    case base::PLATFORM_FILE_ERROR_NOT_FOUND:
        return WebKit::WebFileErrorNotFound;
    case base::PLATFORM_FILE_ERROR_INVALID_OPERATION:
    case base::PLATFORM_FILE_ERROR_EXISTS:
    case base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY:
         return WebKit::WebFileErrorInvalidModification;
    case base::PLATFORM_FILE_ERROR_ACCESS_DENIED:
        return WebKit::WebFileErrorInvalidModification;
    default:
        return WebKit::WebFileErrorNoModificationAllowed;
  }
}
}  // namespace

FileSystemBackend::FileSystemBackend()
    : callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {}

void FileSystemBackend::set_client(FileSystemBackendClient* client) {
  client_ = client;
}

void FileSystemBackend::CreateFile(const FilePath& path,
                                   bool exclusive,
                                   int request_id) {
  request_id_ = request_id;
  base::FileUtilProxy::CreateOrOpen(
    ChromeThread::GetMessageLoopProxyForThread(ChromeThread::FILE),
    path, base::PLATFORM_FILE_CREATE,
    callback_factory_.NewCallback(
        exclusive ? &FileSystemBackend::DidCreateFileExclusive
                  : &FileSystemBackend::DidCreateFileNonExclusive));
}

void FileSystemBackend::CreateDirectory(const FilePath& path,
                                        bool exclusive,
                                        int request_id) {
  request_id_ = request_id;
  base::FileUtilProxy::CreateDirectory(
      ChromeThread::GetMessageLoopProxyForThread(ChromeThread::FILE),
      path, exclusive, callback_factory_.NewCallback(
          &FileSystemBackend::DidFinishFileOperation));
}

void FileSystemBackend::Copy(const FilePath& src_path,
                             const FilePath& dest_path,
                             int request_id) {
  request_id_ = request_id;
  base::FileUtilProxy::Copy(
      ChromeThread::GetMessageLoopProxyForThread(ChromeThread::FILE),
      src_path, dest_path, callback_factory_.NewCallback(
          &FileSystemBackend::DidFinishFileOperation));
}

void FileSystemBackend::Move(const FilePath& src_path,
                             const FilePath& dest_path,
                             int request_id) {
  request_id_ = request_id;
  base::FileUtilProxy::Move(
      ChromeThread::GetMessageLoopProxyForThread(ChromeThread::FILE),
      src_path, dest_path, callback_factory_.NewCallback(
          &FileSystemBackend::DidFinishFileOperation));
}

void FileSystemBackend::DirectoryExists(const FilePath& path, int request_id) {
  request_id_ = request_id;
  base::FileUtilProxy::GetFileInfo(
      ChromeThread::GetMessageLoopProxyForThread(ChromeThread::FILE),
      path, callback_factory_.NewCallback(
              &FileSystemBackend::DidDirectoryExists));
}

void FileSystemBackend::FileExists(const FilePath& path, int request_id) {
  request_id_ = request_id;
  base::FileUtilProxy::GetFileInfo(
      ChromeThread::GetMessageLoopProxyForThread(ChromeThread::FILE),
      path, callback_factory_.NewCallback(&FileSystemBackend::DidFileExists));
}

void FileSystemBackend::GetMetadata(const FilePath& path, int request_id) {
  request_id_ = request_id;
  base::FileUtilProxy::GetFileInfo(
      ChromeThread::GetMessageLoopProxyForThread(ChromeThread::FILE),
      path, callback_factory_.NewCallback(&FileSystemBackend::DidGetMetadata));
}

void FileSystemBackend::ReadDirectory(
    const FilePath& path, int request_id) {
  request_id_ = request_id;
  base::FileUtilProxy::ReadDirectory(
      ChromeThread::GetMessageLoopProxyForThread(ChromeThread::FILE),
      path, callback_factory_.NewCallback(
          &FileSystemBackend::DidReadDirectory));
}

void FileSystemBackend::Remove(const FilePath& path, int request_id) {
  request_id_ = request_id;
  base::FileUtilProxy::Delete(
      ChromeThread::GetMessageLoopProxyForThread(ChromeThread::FILE),
      path, callback_factory_.NewCallback(
          &FileSystemBackend::DidFinishFileOperation));
}

void FileSystemBackend::DidCreateFileExclusive(base::PlatformFileError rv,
                                               base::PassPlatformFile file,
                                               bool created) {
  DidFinishFileOperation(rv);
}

void FileSystemBackend::DidCreateFileNonExclusive(base::PlatformFileError rv,
                                                  base::PassPlatformFile file,
                                                  bool created) {
  // Supress the already exists error and report success.
  if (rv == base::PLATFORM_FILE_OK ||
      rv == base::PLATFORM_FILE_ERROR_EXISTS)
    client_->DidSucceed(rv);
  else
    client_->DidFail(PlatformToWebkitError(rv), request_id_);
}

void FileSystemBackend::DidFinishFileOperation(base::PlatformFileError rv) {
  DCHECK(client_);
  if (rv == base::PLATFORM_FILE_OK)
    client_->DidSucceed(request_id_);
  else
    client_->DidFail(PlatformToWebkitError(rv), request_id_);
}

void FileSystemBackend::DidDirectoryExists(
    base::PlatformFileError rv, const base::PlatformFileInfo& file_info) {
  DCHECK(client_);
  if (rv == base::PLATFORM_FILE_OK) {
    if (file_info.is_directory)
      client_->DidSucceed(request_id_);
    else
      client_->DidFail(WebKit::WebFileErrorInvalidState, request_id_);
  } else {
    // Something else went wrong.
    client_->DidFail(PlatformToWebkitError(rv), request_id_);
  }
}

void FileSystemBackend::DidFileExists(
    base::PlatformFileError rv,
    const base::PlatformFileInfo& file_info) {
  DCHECK(client_);
  if (rv == base::PLATFORM_FILE_OK) {
    if (file_info.is_directory)
      client_->DidFail(WebKit::WebFileErrorInvalidState, request_id_);
    else
      client_->DidSucceed(request_id_);
  } else {
    // Something else went wrong.
    client_->DidFail(PlatformToWebkitError(rv), request_id_);
  }
}

void FileSystemBackend::DidGetMetadata(
    base::PlatformFileError rv,
    const base::PlatformFileInfo& file_info) {
  DCHECK(client_);
  if (rv == base::PLATFORM_FILE_OK)
    client_->DidReadMetadata(file_info, request_id_);
  else
    client_->DidFail(PlatformToWebkitError(rv), request_id_);
}

void FileSystemBackend::DidReadDirectory(
    base::PlatformFileError rv,
    const std::vector<base::file_util_proxy::Entry>& entries) {
  DCHECK(client_);
  if (rv == base::PLATFORM_FILE_OK)
    client_->DidReadDirectory(entries, false /* has_more */ , request_id_);
  else
    client_->DidFail(PlatformToWebkitError(rv), request_id_);
}