blob: 6689bf1b4916f1a789020002f51007ef2057ff0d [file] [log] [blame]
[email protected]60f60f82012-01-11 10:26:101// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]2687ad7d2010-10-14 00:44:082// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "webkit/tools/test_shell/simple_file_writer.h"
6
[email protected]2de55302011-10-10 22:09:587#include "base/bind.h"
[email protected]4aefec142012-04-23 21:23:048#include "base/location.h"
[email protected]2687ad7d2010-10-14 00:44:089#include "base/logging.h"
10#include "base/message_loop_proxy.h"
11#include "net/url_request/url_request_context.h"
[email protected]073a04f2011-03-24 00:49:4012#include "webkit/fileapi/file_system_context.h"
[email protected]3eb080d2012-01-16 04:20:0613#include "webkit/fileapi/file_system_operation_interface.h"
[email protected]2687ad7d2010-10-14 00:44:0814#include "webkit/glue/webkit_glue.h"
15#include "webkit/tools/test_shell/simple_resource_loader_bridge.h"
16
[email protected]073a04f2011-03-24 00:49:4017using fileapi::FileSystemContext;
[email protected]3eb080d2012-01-16 04:20:0618using fileapi::FileSystemOperationInterface;
[email protected]2687ad7d2010-10-14 00:44:0819using fileapi::WebFileWriterBase;
20using WebKit::WebFileWriterClient;
21using WebKit::WebString;
22using WebKit::WebURL;
23
[email protected]aeb53f02011-01-15 00:21:3424net::URLRequestContext* SimpleFileWriter::request_context_ = NULL;
[email protected]2687ad7d2010-10-14 00:44:0825
26// Helper class to proxy to write and truncate calls to the IO thread,
27// and to proxy the results back to the main thead. There is a one-to-one
28// relationship between SimpleFileWriters and IOThreadBackends.
29class SimpleFileWriter::IOThreadProxy
30 : public base::RefCountedThreadSafe<SimpleFileWriter::IOThreadProxy> {
31 public:
[email protected]e7e38032011-07-26 17:25:2532 IOThreadProxy(const base::WeakPtr<SimpleFileWriter>& simple_writer,
33 FileSystemContext* file_system_context)
[email protected]103c8d42010-11-30 18:58:1334 : simple_writer_(simple_writer),
[email protected]073a04f2011-03-24 00:49:4035 operation_(NULL),
36 file_system_context_(file_system_context) {
[email protected]2687ad7d2010-10-14 00:44:0837 // The IO thread needs to be running for this class to work.
38 SimpleResourceLoaderBridge::EnsureIOThread();
39 io_thread_ = SimpleResourceLoaderBridge::GetIoThread();
[email protected]edd685f2011-08-15 20:33:4640 main_thread_ = base::MessageLoopProxy::current();
[email protected]2687ad7d2010-10-14 00:44:0841 }
42
[email protected]41e889432011-04-13 19:53:3043 void Truncate(const GURL& path, int64 offset) {
[email protected]2687ad7d2010-10-14 00:44:0844 if (!io_thread_->BelongsToCurrentThread()) {
[email protected]2de55302011-10-10 22:09:5845 io_thread_->PostTask(
46 FROM_HERE,
47 base::Bind(&IOThreadProxy::Truncate, this, path, offset));
[email protected]2687ad7d2010-10-14 00:44:0848 return;
49 }
[email protected]103c8d42010-11-30 18:58:1350 DCHECK(!operation_);
[email protected]3eb080d2012-01-16 04:20:0651 operation_ = GetNewOperation(path);
[email protected]81f8a3d2012-02-14 08:40:3752 operation_->Truncate(path, offset,
53 base::Bind(&IOThreadProxy::DidFinish, this));
[email protected]2687ad7d2010-10-14 00:44:0854 }
55
[email protected]41e889432011-04-13 19:53:3056 void Write(const GURL& path, const GURL& blob_url, int64 offset) {
[email protected]2687ad7d2010-10-14 00:44:0857 if (!io_thread_->BelongsToCurrentThread()) {
[email protected]2de55302011-10-10 22:09:5858 io_thread_->PostTask(
59 FROM_HERE,
60 base::Bind(&IOThreadProxy::Write, this, path, blob_url, offset));
[email protected]2687ad7d2010-10-14 00:44:0861 return;
62 }
63 DCHECK(request_context_);
[email protected]103c8d42010-11-30 18:58:1364 DCHECK(!operation_);
[email protected]3eb080d2012-01-16 04:20:0665 operation_ = GetNewOperation(path);
[email protected]81f8a3d2012-02-14 08:40:3766 operation_->Write(request_context_, path, blob_url, offset,
67 base::Bind(&IOThreadProxy::DidWrite, this));
[email protected]2687ad7d2010-10-14 00:44:0868 }
69
70 void Cancel() {
71 if (!io_thread_->BelongsToCurrentThread()) {
[email protected]2de55302011-10-10 22:09:5872 io_thread_->PostTask(
73 FROM_HERE,
74 base::Bind(&IOThreadProxy::Cancel, this));
[email protected]2687ad7d2010-10-14 00:44:0875 return;
76 }
[email protected]103c8d42010-11-30 18:58:1377 if (!operation_) {
[email protected]81f8a3d2012-02-14 08:40:3778 DidFailOnMainThread(base::PLATFORM_FILE_ERROR_INVALID_OPERATION);
[email protected]2687ad7d2010-10-14 00:44:0879 return;
80 }
[email protected]81f8a3d2012-02-14 08:40:3781 operation_->Cancel(base::Bind(&IOThreadProxy::DidFinish, this));
[email protected]2687ad7d2010-10-14 00:44:0882 }
83
84 private:
[email protected]e0ffc6f2012-04-28 07:04:3685 friend class base::RefCountedThreadSafe<IOThreadProxy>;
86 virtual ~IOThreadProxy() {}
87
[email protected]3eb080d2012-01-16 04:20:0688 FileSystemOperationInterface* GetNewOperation(const GURL& path) {
[email protected]81f8a3d2012-02-14 08:40:3789 return file_system_context_->CreateFileSystemOperation(path, io_thread_);
[email protected]2687ad7d2010-10-14 00:44:0890 }
91
[email protected]81f8a3d2012-02-14 08:40:3792 void DidSucceedOnMainThread() {
[email protected]2687ad7d2010-10-14 00:44:0893 if (!main_thread_->BelongsToCurrentThread()) {
[email protected]2de55302011-10-10 22:09:5894 main_thread_->PostTask(
95 FROM_HERE,
[email protected]81f8a3d2012-02-14 08:40:3796 base::Bind(&IOThreadProxy::DidSucceedOnMainThread, this));
[email protected]2687ad7d2010-10-14 00:44:0897 return;
98 }
99 if (simple_writer_)
100 simple_writer_->DidSucceed();
101 }
102
[email protected]81f8a3d2012-02-14 08:40:37103 void DidFailOnMainThread(base::PlatformFileError error_code) {
[email protected]2687ad7d2010-10-14 00:44:08104 if (!main_thread_->BelongsToCurrentThread()) {
[email protected]2de55302011-10-10 22:09:58105 main_thread_->PostTask(
106 FROM_HERE,
[email protected]81f8a3d2012-02-14 08:40:37107 base::Bind(&IOThreadProxy::DidFailOnMainThread, this, error_code));
[email protected]2687ad7d2010-10-14 00:44:08108 return;
109 }
110 if (simple_writer_)
111 simple_writer_->DidFail(error_code);
112 }
113
[email protected]81f8a3d2012-02-14 08:40:37114 void DidWriteOnMainThread(int64 bytes, bool complete) {
[email protected]2687ad7d2010-10-14 00:44:08115 if (!main_thread_->BelongsToCurrentThread()) {
[email protected]2de55302011-10-10 22:09:58116 main_thread_->PostTask(
117 FROM_HERE,
[email protected]81f8a3d2012-02-14 08:40:37118 base::Bind(&IOThreadProxy::DidWriteOnMainThread,
119 this, bytes, complete));
[email protected]2687ad7d2010-10-14 00:44:08120 return;
121 }
122 if (simple_writer_)
123 simple_writer_->DidWrite(bytes, complete);
124 }
125
[email protected]103c8d42010-11-30 18:58:13126 void ClearOperation() {
127 DCHECK(io_thread_->BelongsToCurrentThread());
128 operation_ = NULL;
129 }
130
[email protected]81f8a3d2012-02-14 08:40:37131 void DidFinish(base::PlatformFileError result) {
132 if (result == base::PLATFORM_FILE_OK)
133 DidSucceedOnMainThread();
134 else
135 DidFailOnMainThread(result);
136 ClearOperation();
137 }
138
139 void DidWrite(base::PlatformFileError result, int64 bytes, bool complete) {
140 if (result == base::PLATFORM_FILE_OK) {
141 DidWriteOnMainThread(bytes, complete);
142 if (complete)
143 ClearOperation();
144 } else {
145 DidFailOnMainThread(result);
146 ClearOperation();
147 }
148 }
149
[email protected]2687ad7d2010-10-14 00:44:08150 scoped_refptr<base::MessageLoopProxy> io_thread_;
151 scoped_refptr<base::MessageLoopProxy> main_thread_;
152
153 // Only used on the main thread.
154 base::WeakPtr<SimpleFileWriter> simple_writer_;
155
156 // Only used on the io thread.
[email protected]3eb080d2012-01-16 04:20:06157 FileSystemOperationInterface* operation_;
[email protected]073a04f2011-03-24 00:49:40158
159 scoped_refptr<FileSystemContext> file_system_context_;
[email protected]2687ad7d2010-10-14 00:44:08160};
161
162
163SimpleFileWriter::SimpleFileWriter(
[email protected]41e889432011-04-13 19:53:30164 const GURL& path,
[email protected]073a04f2011-03-24 00:49:40165 WebFileWriterClient* client,
166 FileSystemContext* file_system_context)
[email protected]2687ad7d2010-10-14 00:44:08167 : WebFileWriterBase(path, client),
[email protected]073a04f2011-03-24 00:49:40168 io_thread_proxy_(new IOThreadProxy(AsWeakPtr(), file_system_context)) {
[email protected]2687ad7d2010-10-14 00:44:08169}
170
171SimpleFileWriter::~SimpleFileWriter() {
172}
173
[email protected]41e889432011-04-13 19:53:30174void SimpleFileWriter::DoTruncate(const GURL& path, int64 offset) {
[email protected]2687ad7d2010-10-14 00:44:08175 io_thread_proxy_->Truncate(path, offset);
176}
177
178void SimpleFileWriter::DoWrite(
[email protected]41e889432011-04-13 19:53:30179 const GURL& path, const GURL& blob_url, int64 offset) {
[email protected]2687ad7d2010-10-14 00:44:08180 io_thread_proxy_->Write(path, blob_url, offset);
181}
182
183void SimpleFileWriter::DoCancel() {
184 io_thread_proxy_->Cancel();
185}