Commit b90c93ff authored by dumi@chromium.org's avatar dumi@chromium.org

Moving file_system_proxy to base/ and changing it to work with...

Moving file_system_proxy to base/ and changing it to work with MessageLoopProxies instead of ChromeThreads.

BUG=none
TEST=none

Review URL: http://codereview.chromium.org/3131026

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@56923 0039d316-1c4b-4281-b951-d872f2087c98
parent 826cb81e
...@@ -81,6 +81,8 @@ ...@@ -81,6 +81,8 @@
'file_util_mac.mm', 'file_util_mac.mm',
'file_util_posix.cc', 'file_util_posix.cc',
'file_util_win.cc', 'file_util_win.cc',
'file_util_proxy.cc',
'file_util_proxy.h',
'file_version_info.h', 'file_version_info.h',
'file_version_info_mac.h', 'file_version_info_mac.h',
'file_version_info_mac.mm', 'file_version_info_mac.mm',
......
...@@ -2,20 +2,58 @@ ...@@ -2,20 +2,58 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "chrome/browser/file_system_proxy.h" #include "base/file_util_proxy.h"
#include "base/file_util.h" #include "base/file_util.h"
#include "chrome/browser/chrome_thread_relay.h" #include "base/message_loop_proxy.h"
namespace { namespace {
class RelayCreateOrOpen : public ChromeThreadRelay { class MessageLoopRelay
: public base::RefCountedThreadSafe<MessageLoopRelay> {
public:
MessageLoopRelay()
: origin_message_loop_proxy_(
base::MessageLoopProxy::CreateForCurrentThread()) {
}
void Start(scoped_refptr<base::MessageLoopProxy> message_loop_proxy,
const tracked_objects::Location& from_here) {
message_loop_proxy->PostTask(
from_here,
NewRunnableMethod(this, &MessageLoopRelay::ProcessOnTargetThread));
}
protected:
friend class base::RefCountedThreadSafe<MessageLoopRelay>;
virtual ~MessageLoopRelay() {}
// Called to perform work on the FILE thread.
virtual void RunWork() = 0;
// Called to notify the callback on the origin thread.
virtual void RunCallback() = 0;
private:
void ProcessOnTargetThread() {
RunWork();
origin_message_loop_proxy_->PostTask(
FROM_HERE,
NewRunnableMethod(this, &MessageLoopRelay::RunCallback));
}
scoped_refptr<base::MessageLoopProxy> origin_message_loop_proxy_;
};
class RelayCreateOrOpen : public MessageLoopRelay {
public: public:
RelayCreateOrOpen( RelayCreateOrOpen(
scoped_refptr<base::MessageLoopProxy> message_loop_proxy,
const FilePath& file_path, const FilePath& file_path,
int file_flags, int file_flags,
FileSystemProxy::CreateOrOpenCallback* callback) base::FileUtilProxy::CreateOrOpenCallback* callback)
: file_path_(file_path), : message_loop_proxy_(message_loop_proxy),
file_path_(file_path),
file_flags_(file_flags), file_flags_(file_flags),
callback_(callback), callback_(callback),
file_handle_(base::kInvalidPlatformFileValue), file_handle_(base::kInvalidPlatformFileValue),
...@@ -26,7 +64,7 @@ class RelayCreateOrOpen : public ChromeThreadRelay { ...@@ -26,7 +64,7 @@ class RelayCreateOrOpen : public ChromeThreadRelay {
protected: protected:
virtual ~RelayCreateOrOpen() { virtual ~RelayCreateOrOpen() {
if (file_handle_ != base::kInvalidPlatformFileValue) if (file_handle_ != base::kInvalidPlatformFileValue)
FileSystemProxy::Close(file_handle_, NULL); base::FileUtilProxy::Close(message_loop_proxy_, file_handle_, NULL);
} }
virtual void RunWork() { virtual void RunWork() {
...@@ -39,18 +77,21 @@ class RelayCreateOrOpen : public ChromeThreadRelay { ...@@ -39,18 +77,21 @@ class RelayCreateOrOpen : public ChromeThreadRelay {
} }
private: private:
scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
FilePath file_path_; FilePath file_path_;
int file_flags_; int file_flags_;
FileSystemProxy::CreateOrOpenCallback* callback_; base::FileUtilProxy::CreateOrOpenCallback* callback_;
base::PlatformFile file_handle_; base::PlatformFile file_handle_;
bool created_; bool created_;
}; };
class RelayCreateTemporary : public ChromeThreadRelay { class RelayCreateTemporary : public MessageLoopRelay {
public: public:
explicit RelayCreateTemporary( RelayCreateTemporary(
FileSystemProxy::CreateTemporaryCallback* callback) scoped_refptr<base::MessageLoopProxy> message_loop_proxy,
: callback_(callback), base::FileUtilProxy::CreateTemporaryCallback* callback)
: message_loop_proxy_(message_loop_proxy),
callback_(callback),
file_handle_(base::kInvalidPlatformFileValue) { file_handle_(base::kInvalidPlatformFileValue) {
DCHECK(callback); DCHECK(callback);
} }
...@@ -58,7 +99,7 @@ class RelayCreateTemporary : public ChromeThreadRelay { ...@@ -58,7 +99,7 @@ class RelayCreateTemporary : public ChromeThreadRelay {
protected: protected:
virtual ~RelayCreateTemporary() { virtual ~RelayCreateTemporary() {
if (file_handle_ != base::kInvalidPlatformFileValue) if (file_handle_ != base::kInvalidPlatformFileValue)
FileSystemProxy::Close(file_handle_, NULL); base::FileUtilProxy::Close(message_loop_proxy_, file_handle_, NULL);
} }
virtual void RunWork() { virtual void RunWork() {
...@@ -82,14 +123,16 @@ class RelayCreateTemporary : public ChromeThreadRelay { ...@@ -82,14 +123,16 @@ class RelayCreateTemporary : public ChromeThreadRelay {
} }
private: private:
FileSystemProxy::CreateTemporaryCallback* callback_; scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
base::FileUtilProxy::CreateTemporaryCallback* callback_;
base::PlatformFile file_handle_; base::PlatformFile file_handle_;
FilePath file_path_; FilePath file_path_;
}; };
class RelayWithStatusCallback : public ChromeThreadRelay { class RelayWithStatusCallback : public MessageLoopRelay {
public: public:
explicit RelayWithStatusCallback(FileSystemProxy::StatusCallback* callback) explicit RelayWithStatusCallback(
base::FileUtilProxy::StatusCallback* callback)
: callback_(callback), : callback_(callback),
succeeded_(false) { succeeded_(false) {
// It is OK for callback to be NULL. // It is OK for callback to be NULL.
...@@ -107,14 +150,14 @@ class RelayWithStatusCallback : public ChromeThreadRelay { ...@@ -107,14 +150,14 @@ class RelayWithStatusCallback : public ChromeThreadRelay {
void SetStatus(bool succeeded) { succeeded_ = succeeded; } void SetStatus(bool succeeded) { succeeded_ = succeeded; }
private: private:
FileSystemProxy::StatusCallback* callback_; base::FileUtilProxy::StatusCallback* callback_;
bool succeeded_; bool succeeded_;
}; };
class RelayClose : public RelayWithStatusCallback { class RelayClose : public RelayWithStatusCallback {
public: public:
RelayClose(base::PlatformFile file_handle, RelayClose(base::PlatformFile file_handle,
FileSystemProxy::StatusCallback* callback) base::FileUtilProxy::StatusCallback* callback)
: RelayWithStatusCallback(callback), : RelayWithStatusCallback(callback),
file_handle_(file_handle) { file_handle_(file_handle) {
} }
...@@ -132,7 +175,7 @@ class RelayDelete : public RelayWithStatusCallback { ...@@ -132,7 +175,7 @@ class RelayDelete : public RelayWithStatusCallback {
public: public:
RelayDelete(const FilePath& file_path, RelayDelete(const FilePath& file_path,
bool recursive, bool recursive,
FileSystemProxy::StatusCallback* callback) base::FileUtilProxy::StatusCallback* callback)
: RelayWithStatusCallback(callback), : RelayWithStatusCallback(callback),
file_path_(file_path), file_path_(file_path),
recursive_(recursive) { recursive_(recursive) {
...@@ -149,32 +192,54 @@ class RelayDelete : public RelayWithStatusCallback { ...@@ -149,32 +192,54 @@ class RelayDelete : public RelayWithStatusCallback {
}; };
void Start(const tracked_objects::Location& from_here, void Start(const tracked_objects::Location& from_here,
scoped_refptr<ChromeThreadRelay> relay) { scoped_refptr<base::MessageLoopProxy> message_loop_proxy,
relay->Start(ChromeThread::FILE, from_here); scoped_refptr<MessageLoopRelay> relay) {
relay->Start(message_loop_proxy, from_here);
} }
} // namespace } // namespace
void FileSystemProxy::CreateOrOpen(const FilePath& file_path, int file_flags, namespace base {
CreateOrOpenCallback* callback) {
Start(FROM_HERE, new RelayCreateOrOpen(file_path, file_flags, callback)); // static
void FileUtilProxy::CreateOrOpen(
scoped_refptr<MessageLoopProxy> message_loop_proxy,
const FilePath& file_path, int file_flags,
CreateOrOpenCallback* callback) {
Start(FROM_HERE, message_loop_proxy, new RelayCreateOrOpen(
message_loop_proxy, file_path, file_flags, callback));
} }
void FileSystemProxy::CreateTemporary(CreateTemporaryCallback* callback) { // static
Start(FROM_HERE, new RelayCreateTemporary(callback)); void FileUtilProxy::CreateTemporary(
scoped_refptr<MessageLoopProxy> message_loop_proxy,
CreateTemporaryCallback* callback) {
Start(FROM_HERE, message_loop_proxy,
new RelayCreateTemporary(message_loop_proxy, callback));
} }
void FileSystemProxy::Close(base::PlatformFile file_handle, // static
StatusCallback* callback) { void FileUtilProxy::Close(scoped_refptr<MessageLoopProxy> message_loop_proxy,
Start(FROM_HERE, new RelayClose(file_handle, callback)); base::PlatformFile file_handle,
StatusCallback* callback) {
Start(FROM_HERE, message_loop_proxy, new RelayClose(file_handle, callback));
} }
void FileSystemProxy::Delete(const FilePath& file_path, // static
StatusCallback* callback) { void FileUtilProxy::Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy,
Start(FROM_HERE, new RelayDelete(file_path, false, callback)); const FilePath& file_path,
StatusCallback* callback) {
Start(FROM_HERE, message_loop_proxy,
new RelayDelete(file_path, false, callback));
} }
void FileSystemProxy::RecursiveDelete(const FilePath& file_path, // static
StatusCallback* callback) { void FileUtilProxy::RecursiveDelete(
Start(FROM_HERE, new RelayDelete(file_path, true, callback)); scoped_refptr<MessageLoopProxy> message_loop_proxy,
const FilePath& file_path,
StatusCallback* callback) {
Start(FROM_HERE, message_loop_proxy,
new RelayDelete(file_path, true, callback));
} }
} // namespace base
...@@ -2,14 +2,20 @@ ...@@ -2,14 +2,20 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef CHROME_BROWSER_FILE_SYSTEM_PROXY_H_ #ifndef BASE_FILE_SYSTEM_PROXY_H_
#define CHROME_BROWSER_FILE_SYSTEM_PROXY_H_ #define BASE_FILE_SYSTEM_PROXY_H_
#include "base/callback.h" #include "base/callback.h"
#include "base/platform_file.h" #include "base/platform_file.h"
#include "base/ref_counted.h"
#include "base/tracked_objects.h"
namespace base {
class MessageLoopProxy;
// This class provides asynchronous access to common file routines. // This class provides asynchronous access to common file routines.
class FileSystemProxy { class FileUtilProxy {
public: public:
// This callback is used by methods that report success with a bool. It is // This callback is used by methods that report success with a bool. It is
// valid to pass NULL as the callback parameter to any function that takes a // valid to pass NULL as the callback parameter to any function that takes a
...@@ -20,7 +26,8 @@ class FileSystemProxy { ...@@ -20,7 +26,8 @@ class FileSystemProxy {
// for the callback. // for the callback.
typedef Callback2<base::PassPlatformFile, bool /* created */>::Type typedef Callback2<base::PassPlatformFile, bool /* created */>::Type
CreateOrOpenCallback; CreateOrOpenCallback;
static void CreateOrOpen(const FilePath& file_path, static void CreateOrOpen(scoped_refptr<MessageLoopProxy> message_loop_proxy,
const FilePath& file_path,
int file_flags, int file_flags,
CreateOrOpenCallback* callback); CreateOrOpenCallback* callback);
...@@ -28,21 +35,30 @@ class FileSystemProxy { ...@@ -28,21 +35,30 @@ class FileSystemProxy {
// are returned. It is invalid to pass NULL for the callback. // are returned. It is invalid to pass NULL for the callback.
typedef Callback2<base::PassPlatformFile, FilePath>::Type typedef Callback2<base::PassPlatformFile, FilePath>::Type
CreateTemporaryCallback; CreateTemporaryCallback;
static void CreateTemporary(CreateTemporaryCallback* callback); static void CreateTemporary(
scoped_refptr<MessageLoopProxy> message_loop_proxy,
CreateTemporaryCallback* callback);
// Close the given file handle. // Close the given file handle.
static void Close(base::PlatformFile, StatusCallback* callback); static void Close(scoped_refptr<MessageLoopProxy> message_loop_proxy,
base::PlatformFile,
StatusCallback* callback);
// Deletes a file or empty directory. // Deletes a file or empty directory.
static void Delete(const FilePath& file_path, static void Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy,
const FilePath& file_path,
StatusCallback* callback); StatusCallback* callback);
// Deletes a directory and all of its contents. // Deletes a directory and all of its contents.
static void RecursiveDelete(const FilePath& file_path, static void RecursiveDelete(
StatusCallback* callback); scoped_refptr<MessageLoopProxy> message_loop_proxy,
const FilePath& file_path,
StatusCallback* callback);
private: private:
DISALLOW_IMPLICIT_CONSTRUCTORS(FileSystemProxy); DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy);
}; };
#endif // CHROME_BROWSER_FILE_SYSTEM_PROXY_H_ } // namespace base
#endif // BASE_FILE_SYSTEM_PROXY_H_
// 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 "chrome/browser/chrome_thread_relay.h"
ChromeThreadRelay::ChromeThreadRelay() {
if (!ChromeThread::GetCurrentThreadIdentifier(&origin_thread_id_))
NOTREACHED() << "Must be created on a valid ChromeThread";
}
void ChromeThreadRelay::Start(ChromeThread::ID target_thread_id,
const tracked_objects::Location& from_here) {
ChromeThread::PostTask(
target_thread_id,
from_here,
NewRunnableMethod(this, &ChromeThreadRelay::ProcessOnTargetThread));
}
void ChromeThreadRelay::ProcessOnTargetThread() {
RunWork();
ChromeThread::PostTask(
origin_thread_id_,
FROM_HERE,
NewRunnableMethod(this, &ChromeThreadRelay::RunCallback));
}
// 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.
// ChromeThreadRelay provides a convenient way to bounce work to a specific
// ChromeThread and then return results back to the originating thread.
//
// EXAMPLE:
//
// class MyRelay : public ChromeThreadRelay {
// public:
// MyRelay(const Params& params) : params_(params) {
// }
// protected:
// virtual void RunWork() {
// results_ = DoWork(params_);
// }
// virtual void RunCallback() {
// ... use results_ on the originating thread ...
// }
// private:
// Params params_;
// Results_ results_;
// };
#ifndef CHROME_BROWSER_CHROME_THREAD_RELAY_H_
#define CHROME_BROWSER_CHROME_THREAD_RELAY_H_
#include "base/ref_counted.h"
#include "chrome/browser/chrome_thread.h"
class ChromeThreadRelay
: public base::RefCountedThreadSafe<ChromeThreadRelay> {
public:
ChromeThreadRelay();
void Start(ChromeThread::ID target_thread_id,
const tracked_objects::Location& from_here);
protected:
friend class base::RefCountedThreadSafe<ChromeThreadRelay>;
virtual ~ChromeThreadRelay() {}
// Called to perform work on the FILE thread.
virtual void RunWork() = 0;
// Called to notify the callback on the origin thread.
virtual void RunCallback() = 0;
private:
void ProcessOnTargetThread();
ChromeThread::ID origin_thread_id_;
};
#endif // CHROME_BROWSER_CHROME_THREAD_RELAY_H_
...@@ -5,10 +5,10 @@ ...@@ -5,10 +5,10 @@
#include "chrome/browser/renderer_host/redirect_to_file_resource_handler.h" #include "chrome/browser/renderer_host/redirect_to_file_resource_handler.h"
#include "base/file_util.h" #include "base/file_util.h"
#include "base/file_util_proxy.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/platform_file.h" #include "base/platform_file.h"
#include "base/task.h" #include "base/task.h"
#include "chrome/browser/file_system_proxy.h"
#include "chrome/browser/renderer_host/resource_dispatcher_host.h" #include "chrome/browser/renderer_host/resource_dispatcher_host.h"
#include "chrome/common/resource_response.h" #include "chrome/common/resource_response.h"
#include "net/base/file_stream.h" #include "net/base/file_stream.h"
...@@ -70,7 +70,8 @@ bool RedirectToFileResourceHandler::OnWillStart(int request_id, ...@@ -70,7 +70,8 @@ bool RedirectToFileResourceHandler::OnWillStart(int request_id,
// TODO(darin): This is sub-optimal. We should not delay starting the // TODO(darin): This is sub-optimal. We should not delay starting the
// network request like this. // network request like this.
*defer = true; *defer = true;
FileSystemProxy::CreateTemporary( base::FileUtilProxy::CreateTemporary(
ChromeThread::GetMessageLoopProxyForThread(ChromeThread::FILE),
callback_factory_.NewCallback( callback_factory_.NewCallback(
&RedirectToFileResourceHandler::DidCreateTemporaryFile)); &RedirectToFileResourceHandler::DidCreateTemporaryFile));
return true; return true;
...@@ -142,7 +143,9 @@ void RedirectToFileResourceHandler::OnRequestClosed() { ...@@ -142,7 +143,9 @@ void RedirectToFileResourceHandler::OnRequestClosed() {
file_stream_->Close(); file_stream_->Close();
file_stream_.reset(); file_stream_.reset();
FileSystemProxy::Delete(file_path_, NULL); base::FileUtilProxy::Delete(
ChromeThread::GetMessageLoopProxyForThread(ChromeThread::FILE),
file_path_, NULL);
} }
RedirectToFileResourceHandler::~RedirectToFileResourceHandler() { RedirectToFileResourceHandler::~RedirectToFileResourceHandler() {
......
...@@ -353,8 +353,6 @@ ...@@ -353,8 +353,6 @@
'browser/chrome_plugin_host.h', 'browser/chrome_plugin_host.h',
'browser/chrome_thread.cc', 'browser/chrome_thread.cc',
'browser/chrome_thread.h', 'browser/chrome_thread.h',
'browser/chrome_thread_relay.cc',
'browser/chrome_thread_relay.h',
'browser/chromeos/notifications/balloon_collection_impl.cc', 'browser/chromeos/notifications/balloon_collection_impl.cc',
'browser/chromeos/notifications/balloon_collection_impl.h', 'browser/chromeos/notifications/balloon_collection_impl.h',
'browser/chromeos/notifications/balloon_view.cc', 'browser/chromeos/notifications/balloon_view.cc',
...@@ -1457,8 +1455,6 @@ ...@@ -1457,8 +1455,6 @@
'browser/file_path_watcher_inotify.cc', 'browser/file_path_watcher_inotify.cc',
'browser/file_path_watcher_mac.cc', 'browser/file_path_watcher_mac.cc',
'browser/file_path_watcher_win.cc', 'browser/file_path_watcher_win.cc',
'browser/file_system_proxy.cc',
'browser/file_system_proxy.h',
'browser/find_bar.h', 'browser/find_bar.h',
'browser/find_bar_controller.cc', 'browser/find_bar_controller.cc',
'browser/find_bar_controller.h', 'browser/find_bar_controller.h',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment