2012-02-17 15:51:20 +01:00
|
|
|
// Copyright (c) 2012 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.
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
#include "libcef/browser_file_writer.h"
|
|
|
|
#include "libcef/cef_thread.h"
|
2010-10-15 20:37:25 +02:00
|
|
|
|
2011-10-12 21:09:15 +02:00
|
|
|
#include "base/bind.h"
|
2010-10-15 20:37:25 +02:00
|
|
|
#include "base/logging.h"
|
|
|
|
#include "base/message_loop_proxy.h"
|
|
|
|
#include "net/url_request/url_request_context.h"
|
2011-04-05 18:17:33 +02:00
|
|
|
#include "webkit/fileapi/file_system_context.h"
|
2012-02-17 15:51:20 +01:00
|
|
|
#include "webkit/fileapi/file_system_operation_interface.h"
|
2010-10-15 20:37:25 +02:00
|
|
|
#include "webkit/glue/webkit_glue.h"
|
|
|
|
|
2011-04-05 18:17:33 +02:00
|
|
|
using fileapi::FileSystemContext;
|
2012-02-17 15:51:20 +01:00
|
|
|
using fileapi::FileSystemOperationInterface;
|
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-09-10 21:40:03 +02:00
|
|
|
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);
|
2011-09-10 21:40:03 +02:00
|
|
|
main_thread_ = base::MessageLoopProxy::current();
|
2010-10-15 20:37:25 +02:00
|
|
|
}
|
|
|
|
|
2011-05-16 18:56:12 +02:00
|
|
|
void Truncate(const GURL& path, int64 offset) {
|
2010-10-15 20:37:25 +02:00
|
|
|
if (!io_thread_->BelongsToCurrentThread()) {
|
2011-10-12 21:09:15 +02:00
|
|
|
io_thread_->PostTask(
|
|
|
|
FROM_HERE,
|
|
|
|
base::Bind(&IOThreadProxy::Truncate, this, path, offset));
|
2010-10-15 20:37:25 +02:00
|
|
|
return;
|
|
|
|
}
|
2010-12-16 22:54:42 +01:00
|
|
|
DCHECK(!operation_);
|
2012-02-17 15:51:20 +01:00
|
|
|
operation_ = GetNewOperation(path);
|
2012-02-18 01:19:52 +01:00
|
|
|
operation_->Truncate(path, offset,
|
|
|
|
base::Bind(&IOThreadProxy::DidFinish, this));
|
2010-10-15 20:37:25 +02:00
|
|
|
}
|
|
|
|
|
2011-05-16 18:56:12 +02:00
|
|
|
void Write(const GURL& path, const GURL& blob_url, int64 offset) {
|
2010-10-15 20:37:25 +02:00
|
|
|
if (!io_thread_->BelongsToCurrentThread()) {
|
2011-10-12 21:09:15 +02:00
|
|
|
io_thread_->PostTask(
|
|
|
|
FROM_HERE,
|
|
|
|
base::Bind(&IOThreadProxy::Write, this, path, blob_url, offset));
|
2010-10-15 20:37:25 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
DCHECK(request_context_);
|
2010-12-16 22:54:42 +01:00
|
|
|
DCHECK(!operation_);
|
2012-02-17 15:51:20 +01:00
|
|
|
operation_ = GetNewOperation(path);
|
2012-02-18 01:19:52 +01:00
|
|
|
operation_->Write(request_context_, path, blob_url, offset,
|
|
|
|
base::Bind(&IOThreadProxy::DidWrite, this));
|
2010-10-15 20:37:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Cancel() {
|
|
|
|
if (!io_thread_->BelongsToCurrentThread()) {
|
2011-10-12 21:09:15 +02:00
|
|
|
io_thread_->PostTask(
|
|
|
|
FROM_HERE,
|
|
|
|
base::Bind(&IOThreadProxy::Cancel, this));
|
2010-10-15 20:37:25 +02:00
|
|
|
return;
|
|
|
|
}
|
2010-12-16 22:54:42 +01:00
|
|
|
if (!operation_) {
|
2012-02-18 01:19:52 +01:00
|
|
|
DidFailOnMainThread(base::PLATFORM_FILE_ERROR_INVALID_OPERATION);
|
2010-10-15 20:37:25 +02:00
|
|
|
return;
|
|
|
|
}
|
2012-02-18 01:19:52 +01:00
|
|
|
operation_->Cancel(base::Bind(&IOThreadProxy::DidFinish, this));
|
2010-10-15 20:37:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2012-05-23 21:01:04 +02:00
|
|
|
friend class base::RefCountedThreadSafe<IOThreadProxy>;
|
|
|
|
virtual ~IOThreadProxy() {}
|
|
|
|
|
2012-02-17 15:51:20 +01:00
|
|
|
FileSystemOperationInterface* GetNewOperation(const GURL& path) {
|
2012-05-23 21:01:04 +02:00
|
|
|
return file_system_context_->CreateFileSystemOperation(path);
|
2010-10-15 20:37:25 +02:00
|
|
|
}
|
|
|
|
|
2012-02-18 01:19:52 +01:00
|
|
|
void DidSucceedOnMainThread() {
|
2010-10-15 20:37:25 +02:00
|
|
|
if (!main_thread_->BelongsToCurrentThread()) {
|
2011-10-12 21:09:15 +02:00
|
|
|
main_thread_->PostTask(
|
|
|
|
FROM_HERE,
|
2012-02-18 01:19:52 +01:00
|
|
|
base::Bind(&IOThreadProxy::DidSucceedOnMainThread, this));
|
2010-10-15 20:37:25 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (simple_writer_)
|
|
|
|
simple_writer_->DidSucceed();
|
|
|
|
}
|
|
|
|
|
2012-02-18 01:19:52 +01:00
|
|
|
void DidFailOnMainThread(base::PlatformFileError error_code) {
|
2010-10-15 20:37:25 +02:00
|
|
|
if (!main_thread_->BelongsToCurrentThread()) {
|
2011-10-12 21:09:15 +02:00
|
|
|
main_thread_->PostTask(
|
|
|
|
FROM_HERE,
|
2012-02-18 01:19:52 +01:00
|
|
|
base::Bind(&IOThreadProxy::DidFailOnMainThread, this, error_code));
|
2010-10-15 20:37:25 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (simple_writer_)
|
|
|
|
simple_writer_->DidFail(error_code);
|
|
|
|
}
|
|
|
|
|
2012-02-18 01:19:52 +01:00
|
|
|
void DidWriteOnMainThread(int64 bytes, bool complete) {
|
2010-10-15 20:37:25 +02:00
|
|
|
if (!main_thread_->BelongsToCurrentThread()) {
|
2011-10-12 21:09:15 +02:00
|
|
|
main_thread_->PostTask(
|
|
|
|
FROM_HERE,
|
2012-02-18 01:19:52 +01:00
|
|
|
base::Bind(&IOThreadProxy::DidWriteOnMainThread,
|
|
|
|
this, bytes, complete));
|
2010-10-15 20:37:25 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-02-18 01:19:52 +01:00
|
|
|
void DidFinish(base::PlatformFileError result) {
|
|
|
|
if (result == base::PLATFORM_FILE_OK)
|
|
|
|
DidSucceedOnMainThread();
|
|
|
|
else
|
|
|
|
DidFailOnMainThread(result);
|
|
|
|
ClearOperation();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DidWrite(base::PlatformFileError result, int64 bytes, bool complete) {
|
|
|
|
if (result == base::PLATFORM_FILE_OK) {
|
|
|
|
DidWriteOnMainThread(bytes, complete);
|
|
|
|
if (complete)
|
|
|
|
ClearOperation();
|
|
|
|
} else {
|
|
|
|
DidFailOnMainThread(result);
|
|
|
|
ClearOperation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2012-02-17 15:51:20 +01:00
|
|
|
FileSystemOperationInterface* 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-05-16 18:56:12 +02:00
|
|
|
const GURL& path,
|
2011-04-05 18:17:33 +02:00
|
|
|
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() {
|
|
|
|
}
|
|
|
|
|
2011-05-16 18:56:12 +02:00
|
|
|
void BrowserFileWriter::DoTruncate(const GURL& path, int64 offset) {
|
2010-10-15 20:37:25 +02:00
|
|
|
io_thread_proxy_->Truncate(path, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserFileWriter::DoWrite(
|
2011-05-16 18:56:12 +02:00
|
|
|
const GURL& path, const GURL& blob_url, int64 offset) {
|
2010-10-15 20:37:25 +02:00
|
|
|
io_thread_proxy_->Write(path, blob_url, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserFileWriter::DoCancel() {
|
|
|
|
io_thread_proxy_->Cancel();
|
|
|
|
}
|