2011-04-05 18:17:33 +02:00
|
|
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
|
2010-10-15 20:37:25 +02:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "browser_file_writer.h"
|
|
|
|
#include "cef_thread.h"
|
|
|
|
|
|
|
|
#include "base/logging.h"
|
|
|
|
#include "base/message_loop_proxy.h"
|
|
|
|
#include "net/url_request/url_request_context.h"
|
|
|
|
#include "webkit/fileapi/file_system_callback_dispatcher.h"
|
2011-04-05 18:17:33 +02:00
|
|
|
#include "webkit/fileapi/file_system_context.h"
|
|
|
|
#include "webkit/fileapi/file_system_file_util.h"
|
2010-10-15 20:37:25 +02:00
|
|
|
#include "webkit/fileapi/file_system_operation.h"
|
|
|
|
#include "webkit/glue/webkit_glue.h"
|
|
|
|
|
|
|
|
using fileapi::FileSystemCallbackDispatcher;
|
2011-04-05 18:17:33 +02:00
|
|
|
using fileapi::FileSystemContext;
|
|
|
|
using fileapi::FileSystemFileUtil;
|
|
|
|
using fileapi::FileSystemOperation;
|
2010-10-15 20:37:25 +02:00
|
|
|
using fileapi::WebFileWriterBase;
|
|
|
|
using WebKit::WebFileWriterClient;
|
|
|
|
using WebKit::WebString;
|
|
|
|
using WebKit::WebURL;
|
|
|
|
|
2011-02-15 19:07:24 +01:00
|
|
|
net::URLRequestContext* BrowserFileWriter::request_context_ = NULL;
|
2010-10-15 20:37:25 +02:00
|
|
|
|
|
|
|
// Helper class to proxy the write and truncate calls to the IO thread,
|
|
|
|
// and to proxy the results back to the main thead. There is a one-to-one
|
|
|
|
// relationship between BrowserFileWriters and IOThreadBackends.
|
|
|
|
class BrowserFileWriter::IOThreadProxy
|
|
|
|
: public base::RefCountedThreadSafe<BrowserFileWriter::IOThreadProxy> {
|
|
|
|
public:
|
2011-04-05 18:17:33 +02:00
|
|
|
explicit IOThreadProxy(const base::WeakPtr<BrowserFileWriter>& simple_writer,
|
|
|
|
FileSystemContext* file_system_context)
|
2010-12-16 22:54:42 +01:00
|
|
|
: simple_writer_(simple_writer),
|
2011-04-05 18:17:33 +02:00
|
|
|
operation_(NULL),
|
|
|
|
file_system_context_(file_system_context) {
|
2010-10-15 20:37:25 +02:00
|
|
|
io_thread_ = CefThread::GetMessageLoopProxyForThread(CefThread::IO);
|
|
|
|
main_thread_ = base::MessageLoopProxy::CreateForCurrentThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~IOThreadProxy() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void Truncate(const FilePath& path, int64 offset) {
|
|
|
|
if (!io_thread_->BelongsToCurrentThread()) {
|
|
|
|
io_thread_->PostTask(FROM_HERE, NewRunnableMethod(
|
|
|
|
this, &IOThreadProxy::Truncate, path, offset));
|
|
|
|
return;
|
|
|
|
}
|
2010-12-16 22:54:42 +01:00
|
|
|
DCHECK(!operation_);
|
|
|
|
operation_ = GetNewOperation();
|
2010-10-15 20:37:25 +02:00
|
|
|
operation_->Truncate(path, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Write(const FilePath& path, const GURL& blob_url, int64 offset) {
|
|
|
|
if (!io_thread_->BelongsToCurrentThread()) {
|
|
|
|
io_thread_->PostTask(FROM_HERE, NewRunnableMethod(
|
|
|
|
this, &IOThreadProxy::Write, path, blob_url, offset));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
DCHECK(request_context_);
|
2010-12-16 22:54:42 +01:00
|
|
|
DCHECK(!operation_);
|
|
|
|
operation_ = GetNewOperation();
|
2010-10-15 20:37:25 +02:00
|
|
|
operation_->Write(request_context_, path, blob_url, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Cancel() {
|
|
|
|
if (!io_thread_->BelongsToCurrentThread()) {
|
|
|
|
io_thread_->PostTask(FROM_HERE, NewRunnableMethod(
|
|
|
|
this, &IOThreadProxy::Cancel));
|
|
|
|
return;
|
|
|
|
}
|
2010-12-16 22:54:42 +01:00
|
|
|
if (!operation_) {
|
2010-10-15 20:37:25 +02:00
|
|
|
DidFail(base::PLATFORM_FILE_ERROR_INVALID_OPERATION);
|
|
|
|
return;
|
|
|
|
}
|
2010-12-16 22:54:42 +01:00
|
|
|
operation_->Cancel(GetNewOperation());
|
2010-10-15 20:37:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Inner class to receive callbacks from FileSystemOperation.
|
|
|
|
class CallbackDispatcher : public FileSystemCallbackDispatcher {
|
|
|
|
public:
|
|
|
|
explicit CallbackDispatcher(IOThreadProxy* proxy) : proxy_(proxy) {
|
|
|
|
}
|
|
|
|
|
2010-12-16 22:54:42 +01:00
|
|
|
~CallbackDispatcher() {
|
|
|
|
proxy_->ClearOperation();
|
|
|
|
}
|
|
|
|
|
2010-10-15 20:37:25 +02:00
|
|
|
virtual void DidSucceed() {
|
|
|
|
proxy_->DidSucceed();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void DidFail(base::PlatformFileError error_code) {
|
|
|
|
proxy_->DidFail(error_code);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void DidWrite(int64 bytes, bool complete) {
|
|
|
|
proxy_->DidWrite(bytes, complete);
|
|
|
|
}
|
|
|
|
|
2011-04-05 18:17:33 +02:00
|
|
|
virtual void DidReadMetadata(
|
|
|
|
const base::PlatformFileInfo&,
|
|
|
|
const FilePath&) {
|
2010-10-15 20:37:25 +02:00
|
|
|
NOTREACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void DidReadDirectory(
|
2010-11-16 17:01:14 +01:00
|
|
|
const std::vector<base::FileUtilProxy::Entry>& entries,
|
2010-10-15 20:37:25 +02:00
|
|
|
bool has_more) {
|
|
|
|
NOTREACHED();
|
|
|
|
}
|
|
|
|
|
2011-04-05 18:17:33 +02:00
|
|
|
virtual void DidOpenFileSystem(
|
|
|
|
const std::string& name,
|
|
|
|
const FilePath& root_path) {
|
2010-10-15 20:37:25 +02:00
|
|
|
NOTREACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
scoped_refptr<IOThreadProxy> proxy_;
|
|
|
|
};
|
|
|
|
|
|
|
|
FileSystemOperation* GetNewOperation() {
|
|
|
|
// The FileSystemOperation takes ownership of the CallbackDispatcher.
|
2011-02-15 19:07:24 +01:00
|
|
|
return new FileSystemOperation(new CallbackDispatcher(this),
|
2011-04-05 18:17:33 +02:00
|
|
|
io_thread_, file_system_context_.get(),
|
|
|
|
NULL);
|
2010-10-15 20:37:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DidSucceed() {
|
|
|
|
if (!main_thread_->BelongsToCurrentThread()) {
|
|
|
|
main_thread_->PostTask(FROM_HERE, NewRunnableMethod(
|
|
|
|
this, &IOThreadProxy::DidSucceed));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (simple_writer_)
|
|
|
|
simple_writer_->DidSucceed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DidFail(base::PlatformFileError error_code) {
|
|
|
|
if (!main_thread_->BelongsToCurrentThread()) {
|
|
|
|
main_thread_->PostTask(FROM_HERE, NewRunnableMethod(
|
|
|
|
this, &IOThreadProxy::DidFail, error_code));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (simple_writer_)
|
|
|
|
simple_writer_->DidFail(error_code);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DidWrite(int64 bytes, bool complete) {
|
|
|
|
if (!main_thread_->BelongsToCurrentThread()) {
|
|
|
|
main_thread_->PostTask(FROM_HERE, NewRunnableMethod(
|
|
|
|
this, &IOThreadProxy::DidWrite, bytes, complete));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (simple_writer_)
|
|
|
|
simple_writer_->DidWrite(bytes, complete);
|
|
|
|
}
|
|
|
|
|
2010-12-16 22:54:42 +01:00
|
|
|
void ClearOperation() {
|
|
|
|
DCHECK(io_thread_->BelongsToCurrentThread());
|
|
|
|
operation_ = NULL;
|
|
|
|
}
|
|
|
|
|
2010-10-15 20:37:25 +02:00
|
|
|
scoped_refptr<base::MessageLoopProxy> io_thread_;
|
|
|
|
scoped_refptr<base::MessageLoopProxy> main_thread_;
|
|
|
|
|
|
|
|
// Only used on the main thread.
|
|
|
|
base::WeakPtr<BrowserFileWriter> simple_writer_;
|
|
|
|
|
|
|
|
// Only used on the io thread.
|
2010-12-16 22:54:42 +01:00
|
|
|
FileSystemOperation* operation_;
|
2011-04-05 18:17:33 +02:00
|
|
|
|
|
|
|
scoped_refptr<FileSystemContext> file_system_context_;
|
2010-10-15 20:37:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
BrowserFileWriter::BrowserFileWriter(
|
2011-04-05 18:17:33 +02:00
|
|
|
const WebString& path,
|
|
|
|
WebFileWriterClient* client,
|
|
|
|
FileSystemContext* file_system_context)
|
2010-10-15 20:37:25 +02:00
|
|
|
: WebFileWriterBase(path, client),
|
2011-04-05 18:17:33 +02:00
|
|
|
io_thread_proxy_(new IOThreadProxy(AsWeakPtr(), file_system_context)) {
|
2010-10-15 20:37:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BrowserFileWriter::~BrowserFileWriter() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserFileWriter::DoTruncate(const FilePath& path, int64 offset) {
|
|
|
|
io_thread_proxy_->Truncate(path, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserFileWriter::DoWrite(
|
|
|
|
const FilePath& path, const GURL& blob_url, int64 offset) {
|
|
|
|
io_thread_proxy_->Write(path, blob_url, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserFileWriter::DoCancel() {
|
|
|
|
io_thread_proxy_->Cancel();
|
|
|
|
}
|