summaryrefslogtreecommitdiffstats
path: root/chrome/browser/file_system
diff options
context:
space:
mode:
authorkkanetkar@chromium.org <kkanetkar@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-09-02 03:43:36 +0000
committerkkanetkar@chromium.org <kkanetkar@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-09-02 03:43:36 +0000
commit61da1dbbe85912d3b21310b5e934ddcc273e5cb1 (patch)
tree4a8e50420f8583b860db847d2f3ea79e907bfdaa /chrome/browser/file_system
parent6470a9eb1c4fdcfb45b504e9b6ce57941ff956de (diff)
downloadchromium_src-61da1dbbe85912d3b21310b5e934ddcc273e5cb1.zip
chromium_src-61da1dbbe85912d3b21310b5e934ddcc273e5cb1.tar.gz
chromium_src-61da1dbbe85912d3b21310b5e934ddcc273e5cb1.tar.bz2
Changes for browser-side implementation for file api.
BUG=32277 TEST=Separate CL for unit test. Review URL: http://codereview.chromium.org/3212002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@58312 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/file_system')
-rw-r--r--chrome/browser/file_system/file_system_backend.cc188
-rw-r--r--chrome/browser/file_system/file_system_backend.h83
-rw-r--r--chrome/browser/file_system/file_system_backend_client.h32
3 files changed, 303 insertions, 0 deletions
diff --git a/chrome/browser/file_system/file_system_backend.cc b/chrome/browser/file_system/file_system_backend.cc
new file mode 100644
index 0000000..36b1005
--- /dev/null
+++ b/chrome/browser/file_system/file_system_backend.cc
@@ -0,0 +1,188 @@
+// 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 file_util::FileInfo& 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 file_util::FileInfo& 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 file_util::FileInfo& 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_);
+}
diff --git a/chrome/browser/file_system/file_system_backend.h b/chrome/browser/file_system/file_system_backend.h
new file mode 100644
index 0000000..9b33afb
--- /dev/null
+++ b/chrome/browser/file_system/file_system_backend.h
@@ -0,0 +1,83 @@
+// 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.
+
+#ifndef CHROME_BROWSER_FILE_SYSTEM_FILE_SYSTEM_BACKEND_H_
+#define CHROME_BROWSER_FILE_SYSTEM_FILE_SYSTEM_BACKEND_H_
+
+#include <vector>
+
+#include "base/file_path.h"
+#include "base/file_util_proxy.h"
+#include "base/scoped_callback_factory.h"
+#include "chrome/browser/chrome_thread.h"
+
+class FileSystemBackendClient;
+
+class FileSystemBackend {
+ public:
+ FileSystemBackend();
+
+ void set_client(FileSystemBackendClient* client);
+
+ void CreateFile(const FilePath& path,
+ bool exclusive,
+ int request_id);
+
+ void CreateDirectory(const FilePath& path,
+ bool exclusive,
+ int request_id);
+
+ void Copy(const FilePath& src_path,
+ const FilePath& dest_path,
+ int request_id);
+
+ void Move(const FilePath& src_path,
+ const FilePath& dest_path,
+ int request_id);
+
+ void DirectoryExists(const FilePath& path, int request_id);
+
+ void FileExists(const FilePath& path, int request_id);
+
+ void GetMetadata(const FilePath& path, int request_id);
+
+ void ReadDirectory(const FilePath& path, int request_id);
+
+ void Remove(const FilePath& path, int request_id);
+
+ private:
+ // Callbacks for above methods.
+ void DidCreateFileExclusive(
+ base::PlatformFileError rv, base::PassPlatformFile file, bool created);
+
+ // Returns success even if the file already existed.
+ void DidCreateFileNonExclusive(
+ base::PlatformFileError rv, base::PassPlatformFile file, bool created);
+
+ // Generic callback that translates platform errors to WebKit error codes.
+ void DidFinishFileOperation(base::PlatformFileError rv);
+
+ void DidDirectoryExists(base::PlatformFileError rv,
+ const file_util::FileInfo& file_info);
+
+ void DidFileExists(base::PlatformFileError rv,
+ const file_util::FileInfo& file_info);
+
+ void DidGetMetadata(base::PlatformFileError rv,
+ const file_util::FileInfo& file_info);
+
+ void DidReadDirectory(
+ base::PlatformFileError rv,
+ const std::vector<base::file_util_proxy::Entry>& entries);
+
+ // Not owned.
+ FileSystemBackendClient* client_;
+ // Client's request id.
+ int request_id_;
+ base::ScopedCallbackFactory<FileSystemBackend> callback_factory_;
+
+ DISALLOW_COPY_AND_ASSIGN(FileSystemBackend);
+};
+
+#endif // CHROME_BROWSER_FILE_SYSTEM_FILE_SYSTEM_BACKEND_H_
diff --git a/chrome/browser/file_system/file_system_backend_client.h b/chrome/browser/file_system/file_system_backend_client.h
new file mode 100644
index 0000000..6da0bc2
--- /dev/null
+++ b/chrome/browser/file_system/file_system_backend_client.h
@@ -0,0 +1,32 @@
+// 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.
+
+#ifndef CHROME_BROWSER_FILE_SYSTEM_FILE_SYSTEM_BACKEND_CLIENT_H_
+#define CHROME_BROWSER_FILE_SYSTEM_FILE_SYSTEM_BACKEND_CLIENT_H_
+
+#include <vector>
+
+#include "base/file_util_proxy.h"
+#include "third_party/WebKit/WebKit/chromium/public/WebFileError.h"
+
+// Interface for client of FileSystemBackend.
+class FileSystemBackendClient {
+ public:
+ virtual ~FileSystemBackendClient() {}
+
+ virtual void DidFail(WebKit::WebFileError status, int request_id) = 0;
+
+ virtual void DidSucceed(int request_id) = 0;
+
+ // Info about the file entry such as modification date and size.
+ virtual void DidReadMetadata(const file_util::FileInfo& info,
+ int request_id) = 0;
+
+ virtual void DidReadDirectory(
+ const std::vector<base::file_util_proxy::Entry>& entries,
+ bool has_more,
+ int request_id) = 0;
+};
+
+#endif // CHROME_BROWSER_FILE_SYSTEM_FILE_SYSTEM_BACKEND_CLIENT_H_