[email protected] | 60f60f8 | 2012-01-11 10:26:10 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 2687ad7d | 2010-10-14 00:44:08 | [diff] [blame] | 2 | // 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] | 2de5530 | 2011-10-10 22:09:58 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | 4aefec14 | 2012-04-23 21:23:04 | [diff] [blame] | 8 | #include "base/location.h" |
[email protected] | 2687ad7d | 2010-10-14 00:44:08 | [diff] [blame] | 9 | #include "base/logging.h" |
| 10 | #include "base/message_loop_proxy.h" |
| 11 | #include "net/url_request/url_request_context.h" |
[email protected] | 073a04f | 2011-03-24 00:49:40 | [diff] [blame] | 12 | #include "webkit/fileapi/file_system_context.h" |
[email protected] | 3eb080d | 2012-01-16 04:20:06 | [diff] [blame] | 13 | #include "webkit/fileapi/file_system_operation_interface.h" |
[email protected] | 2687ad7d | 2010-10-14 00:44:08 | [diff] [blame] | 14 | #include "webkit/glue/webkit_glue.h" |
| 15 | #include "webkit/tools/test_shell/simple_resource_loader_bridge.h" |
| 16 | |
[email protected] | 073a04f | 2011-03-24 00:49:40 | [diff] [blame] | 17 | using fileapi::FileSystemContext; |
[email protected] | 3eb080d | 2012-01-16 04:20:06 | [diff] [blame] | 18 | using fileapi::FileSystemOperationInterface; |
[email protected] | 2687ad7d | 2010-10-14 00:44:08 | [diff] [blame] | 19 | using fileapi::WebFileWriterBase; |
| 20 | using WebKit::WebFileWriterClient; |
| 21 | using WebKit::WebString; |
| 22 | using WebKit::WebURL; |
| 23 | |
[email protected] | aeb53f0 | 2011-01-15 00:21:34 | [diff] [blame] | 24 | net::URLRequestContext* SimpleFileWriter::request_context_ = NULL; |
[email protected] | 2687ad7d | 2010-10-14 00:44:08 | [diff] [blame] | 25 | |
| 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. |
| 29 | class SimpleFileWriter::IOThreadProxy |
| 30 | : public base::RefCountedThreadSafe<SimpleFileWriter::IOThreadProxy> { |
| 31 | public: |
[email protected] | e7e3803 | 2011-07-26 17:25:25 | [diff] [blame] | 32 | IOThreadProxy(const base::WeakPtr<SimpleFileWriter>& simple_writer, |
| 33 | FileSystemContext* file_system_context) |
[email protected] | 103c8d4 | 2010-11-30 18:58:13 | [diff] [blame] | 34 | : simple_writer_(simple_writer), |
[email protected] | 073a04f | 2011-03-24 00:49:40 | [diff] [blame] | 35 | operation_(NULL), |
| 36 | file_system_context_(file_system_context) { |
[email protected] | 2687ad7d | 2010-10-14 00:44:08 | [diff] [blame] | 37 | // The IO thread needs to be running for this class to work. |
| 38 | SimpleResourceLoaderBridge::EnsureIOThread(); |
| 39 | io_thread_ = SimpleResourceLoaderBridge::GetIoThread(); |
[email protected] | edd685f | 2011-08-15 20:33:46 | [diff] [blame] | 40 | main_thread_ = base::MessageLoopProxy::current(); |
[email protected] | 2687ad7d | 2010-10-14 00:44:08 | [diff] [blame] | 41 | } |
| 42 | |
[email protected] | 41e88943 | 2011-04-13 19:53:30 | [diff] [blame] | 43 | void Truncate(const GURL& path, int64 offset) { |
[email protected] | 2687ad7d | 2010-10-14 00:44:08 | [diff] [blame] | 44 | if (!io_thread_->BelongsToCurrentThread()) { |
[email protected] | 2de5530 | 2011-10-10 22:09:58 | [diff] [blame] | 45 | io_thread_->PostTask( |
| 46 | FROM_HERE, |
| 47 | base::Bind(&IOThreadProxy::Truncate, this, path, offset)); |
[email protected] | 2687ad7d | 2010-10-14 00:44:08 | [diff] [blame] | 48 | return; |
| 49 | } |
[email protected] | 103c8d4 | 2010-11-30 18:58:13 | [diff] [blame] | 50 | DCHECK(!operation_); |
[email protected] | 3eb080d | 2012-01-16 04:20:06 | [diff] [blame] | 51 | operation_ = GetNewOperation(path); |
[email protected] | 81f8a3d | 2012-02-14 08:40:37 | [diff] [blame] | 52 | operation_->Truncate(path, offset, |
| 53 | base::Bind(&IOThreadProxy::DidFinish, this)); |
[email protected] | 2687ad7d | 2010-10-14 00:44:08 | [diff] [blame] | 54 | } |
| 55 | |
[email protected] | 41e88943 | 2011-04-13 19:53:30 | [diff] [blame] | 56 | void Write(const GURL& path, const GURL& blob_url, int64 offset) { |
[email protected] | 2687ad7d | 2010-10-14 00:44:08 | [diff] [blame] | 57 | if (!io_thread_->BelongsToCurrentThread()) { |
[email protected] | 2de5530 | 2011-10-10 22:09:58 | [diff] [blame] | 58 | io_thread_->PostTask( |
| 59 | FROM_HERE, |
| 60 | base::Bind(&IOThreadProxy::Write, this, path, blob_url, offset)); |
[email protected] | 2687ad7d | 2010-10-14 00:44:08 | [diff] [blame] | 61 | return; |
| 62 | } |
| 63 | DCHECK(request_context_); |
[email protected] | 103c8d4 | 2010-11-30 18:58:13 | [diff] [blame] | 64 | DCHECK(!operation_); |
[email protected] | 3eb080d | 2012-01-16 04:20:06 | [diff] [blame] | 65 | operation_ = GetNewOperation(path); |
[email protected] | 81f8a3d | 2012-02-14 08:40:37 | [diff] [blame] | 66 | operation_->Write(request_context_, path, blob_url, offset, |
| 67 | base::Bind(&IOThreadProxy::DidWrite, this)); |
[email protected] | 2687ad7d | 2010-10-14 00:44:08 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void Cancel() { |
| 71 | if (!io_thread_->BelongsToCurrentThread()) { |
[email protected] | 2de5530 | 2011-10-10 22:09:58 | [diff] [blame] | 72 | io_thread_->PostTask( |
| 73 | FROM_HERE, |
| 74 | base::Bind(&IOThreadProxy::Cancel, this)); |
[email protected] | 2687ad7d | 2010-10-14 00:44:08 | [diff] [blame] | 75 | return; |
| 76 | } |
[email protected] | 103c8d4 | 2010-11-30 18:58:13 | [diff] [blame] | 77 | if (!operation_) { |
[email protected] | 81f8a3d | 2012-02-14 08:40:37 | [diff] [blame] | 78 | DidFailOnMainThread(base::PLATFORM_FILE_ERROR_INVALID_OPERATION); |
[email protected] | 2687ad7d | 2010-10-14 00:44:08 | [diff] [blame] | 79 | return; |
| 80 | } |
[email protected] | 81f8a3d | 2012-02-14 08:40:37 | [diff] [blame] | 81 | operation_->Cancel(base::Bind(&IOThreadProxy::DidFinish, this)); |
[email protected] | 2687ad7d | 2010-10-14 00:44:08 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | private: |
[email protected] | e0ffc6f | 2012-04-28 07:04:36 | [diff] [blame^] | 85 | friend class base::RefCountedThreadSafe<IOThreadProxy>; |
| 86 | virtual ~IOThreadProxy() {} |
| 87 | |
[email protected] | 3eb080d | 2012-01-16 04:20:06 | [diff] [blame] | 88 | FileSystemOperationInterface* GetNewOperation(const GURL& path) { |
[email protected] | 81f8a3d | 2012-02-14 08:40:37 | [diff] [blame] | 89 | return file_system_context_->CreateFileSystemOperation(path, io_thread_); |
[email protected] | 2687ad7d | 2010-10-14 00:44:08 | [diff] [blame] | 90 | } |
| 91 | |
[email protected] | 81f8a3d | 2012-02-14 08:40:37 | [diff] [blame] | 92 | void DidSucceedOnMainThread() { |
[email protected] | 2687ad7d | 2010-10-14 00:44:08 | [diff] [blame] | 93 | if (!main_thread_->BelongsToCurrentThread()) { |
[email protected] | 2de5530 | 2011-10-10 22:09:58 | [diff] [blame] | 94 | main_thread_->PostTask( |
| 95 | FROM_HERE, |
[email protected] | 81f8a3d | 2012-02-14 08:40:37 | [diff] [blame] | 96 | base::Bind(&IOThreadProxy::DidSucceedOnMainThread, this)); |
[email protected] | 2687ad7d | 2010-10-14 00:44:08 | [diff] [blame] | 97 | return; |
| 98 | } |
| 99 | if (simple_writer_) |
| 100 | simple_writer_->DidSucceed(); |
| 101 | } |
| 102 | |
[email protected] | 81f8a3d | 2012-02-14 08:40:37 | [diff] [blame] | 103 | void DidFailOnMainThread(base::PlatformFileError error_code) { |
[email protected] | 2687ad7d | 2010-10-14 00:44:08 | [diff] [blame] | 104 | if (!main_thread_->BelongsToCurrentThread()) { |
[email protected] | 2de5530 | 2011-10-10 22:09:58 | [diff] [blame] | 105 | main_thread_->PostTask( |
| 106 | FROM_HERE, |
[email protected] | 81f8a3d | 2012-02-14 08:40:37 | [diff] [blame] | 107 | base::Bind(&IOThreadProxy::DidFailOnMainThread, this, error_code)); |
[email protected] | 2687ad7d | 2010-10-14 00:44:08 | [diff] [blame] | 108 | return; |
| 109 | } |
| 110 | if (simple_writer_) |
| 111 | simple_writer_->DidFail(error_code); |
| 112 | } |
| 113 | |
[email protected] | 81f8a3d | 2012-02-14 08:40:37 | [diff] [blame] | 114 | void DidWriteOnMainThread(int64 bytes, bool complete) { |
[email protected] | 2687ad7d | 2010-10-14 00:44:08 | [diff] [blame] | 115 | if (!main_thread_->BelongsToCurrentThread()) { |
[email protected] | 2de5530 | 2011-10-10 22:09:58 | [diff] [blame] | 116 | main_thread_->PostTask( |
| 117 | FROM_HERE, |
[email protected] | 81f8a3d | 2012-02-14 08:40:37 | [diff] [blame] | 118 | base::Bind(&IOThreadProxy::DidWriteOnMainThread, |
| 119 | this, bytes, complete)); |
[email protected] | 2687ad7d | 2010-10-14 00:44:08 | [diff] [blame] | 120 | return; |
| 121 | } |
| 122 | if (simple_writer_) |
| 123 | simple_writer_->DidWrite(bytes, complete); |
| 124 | } |
| 125 | |
[email protected] | 103c8d4 | 2010-11-30 18:58:13 | [diff] [blame] | 126 | void ClearOperation() { |
| 127 | DCHECK(io_thread_->BelongsToCurrentThread()); |
| 128 | operation_ = NULL; |
| 129 | } |
| 130 | |
[email protected] | 81f8a3d | 2012-02-14 08:40:37 | [diff] [blame] | 131 | 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] | 2687ad7d | 2010-10-14 00:44:08 | [diff] [blame] | 150 | 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] | 3eb080d | 2012-01-16 04:20:06 | [diff] [blame] | 157 | FileSystemOperationInterface* operation_; |
[email protected] | 073a04f | 2011-03-24 00:49:40 | [diff] [blame] | 158 | |
| 159 | scoped_refptr<FileSystemContext> file_system_context_; |
[email protected] | 2687ad7d | 2010-10-14 00:44:08 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | |
| 163 | SimpleFileWriter::SimpleFileWriter( |
[email protected] | 41e88943 | 2011-04-13 19:53:30 | [diff] [blame] | 164 | const GURL& path, |
[email protected] | 073a04f | 2011-03-24 00:49:40 | [diff] [blame] | 165 | WebFileWriterClient* client, |
| 166 | FileSystemContext* file_system_context) |
[email protected] | 2687ad7d | 2010-10-14 00:44:08 | [diff] [blame] | 167 | : WebFileWriterBase(path, client), |
[email protected] | 073a04f | 2011-03-24 00:49:40 | [diff] [blame] | 168 | io_thread_proxy_(new IOThreadProxy(AsWeakPtr(), file_system_context)) { |
[email protected] | 2687ad7d | 2010-10-14 00:44:08 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | SimpleFileWriter::~SimpleFileWriter() { |
| 172 | } |
| 173 | |
[email protected] | 41e88943 | 2011-04-13 19:53:30 | [diff] [blame] | 174 | void SimpleFileWriter::DoTruncate(const GURL& path, int64 offset) { |
[email protected] | 2687ad7d | 2010-10-14 00:44:08 | [diff] [blame] | 175 | io_thread_proxy_->Truncate(path, offset); |
| 176 | } |
| 177 | |
| 178 | void SimpleFileWriter::DoWrite( |
[email protected] | 41e88943 | 2011-04-13 19:53:30 | [diff] [blame] | 179 | const GURL& path, const GURL& blob_url, int64 offset) { |
[email protected] | 2687ad7d | 2010-10-14 00:44:08 | [diff] [blame] | 180 | io_thread_proxy_->Write(path, blob_url, offset); |
| 181 | } |
| 182 | |
| 183 | void SimpleFileWriter::DoCancel() { |
| 184 | io_thread_proxy_->Cancel(); |
| 185 | } |