OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "webkit/tools/test_shell/simple_file_writer.h" | 5 #include "webkit/tools/test_shell/simple_file_writer.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop_proxy.h" | 9 #include "base/message_loop_proxy.h" |
10 #include "net/url_request/url_request_context.h" | 10 #include "net/url_request/url_request_context.h" |
11 #include "webkit/fileapi/file_system_callback_dispatcher.h" | 11 #include "webkit/fileapi/file_system_callback_dispatcher.h" |
12 #include "webkit/fileapi/file_system_context.h" | 12 #include "webkit/fileapi/file_system_context.h" |
13 #include "webkit/fileapi/file_system_operation.h" | 13 #include "webkit/fileapi/file_system_operation_interface.h" |
14 #include "webkit/glue/webkit_glue.h" | 14 #include "webkit/glue/webkit_glue.h" |
15 #include "webkit/tools/test_shell/simple_resource_loader_bridge.h" | 15 #include "webkit/tools/test_shell/simple_resource_loader_bridge.h" |
16 | 16 |
17 using fileapi::FileSystemCallbackDispatcher; | 17 using fileapi::FileSystemCallbackDispatcher; |
18 using fileapi::FileSystemContext; | 18 using fileapi::FileSystemContext; |
19 using fileapi::FileSystemOperation; | 19 using fileapi::FileSystemOperationInterface; |
20 using fileapi::WebFileWriterBase; | 20 using fileapi::WebFileWriterBase; |
21 using WebKit::WebFileWriterClient; | 21 using WebKit::WebFileWriterClient; |
22 using WebKit::WebString; | 22 using WebKit::WebString; |
23 using WebKit::WebURL; | 23 using WebKit::WebURL; |
24 | 24 |
25 net::URLRequestContext* SimpleFileWriter::request_context_ = NULL; | 25 net::URLRequestContext* SimpleFileWriter::request_context_ = NULL; |
26 | 26 |
27 // Helper class to proxy to write and truncate calls to the IO thread, | 27 // Helper class to proxy to write and truncate calls to the IO thread, |
28 // and to proxy the results back to the main thead. There is a one-to-one | 28 // and to proxy the results back to the main thead. There is a one-to-one |
29 // relationship between SimpleFileWriters and IOThreadBackends. | 29 // relationship between SimpleFileWriters and IOThreadBackends. |
(...skipping 15 matching lines...) Expand all Loading... |
45 } | 45 } |
46 | 46 |
47 void Truncate(const GURL& path, int64 offset) { | 47 void Truncate(const GURL& path, int64 offset) { |
48 if (!io_thread_->BelongsToCurrentThread()) { | 48 if (!io_thread_->BelongsToCurrentThread()) { |
49 io_thread_->PostTask( | 49 io_thread_->PostTask( |
50 FROM_HERE, | 50 FROM_HERE, |
51 base::Bind(&IOThreadProxy::Truncate, this, path, offset)); | 51 base::Bind(&IOThreadProxy::Truncate, this, path, offset)); |
52 return; | 52 return; |
53 } | 53 } |
54 DCHECK(!operation_); | 54 DCHECK(!operation_); |
55 operation_ = GetNewOperation(); | 55 operation_ = GetNewOperation(path); |
56 operation_->Truncate(path, offset); | 56 operation_->Truncate(path, offset); |
57 } | 57 } |
58 | 58 |
59 void Write(const GURL& path, const GURL& blob_url, int64 offset) { | 59 void Write(const GURL& path, const GURL& blob_url, int64 offset) { |
60 if (!io_thread_->BelongsToCurrentThread()) { | 60 if (!io_thread_->BelongsToCurrentThread()) { |
61 io_thread_->PostTask( | 61 io_thread_->PostTask( |
62 FROM_HERE, | 62 FROM_HERE, |
63 base::Bind(&IOThreadProxy::Write, this, path, blob_url, offset)); | 63 base::Bind(&IOThreadProxy::Write, this, path, blob_url, offset)); |
64 return; | 64 return; |
65 } | 65 } |
66 DCHECK(request_context_); | 66 DCHECK(request_context_); |
67 DCHECK(!operation_); | 67 DCHECK(!operation_); |
68 operation_ = GetNewOperation(); | 68 operation_ = GetNewOperation(path); |
69 operation_->Write(request_context_, path, blob_url, offset); | 69 operation_->Write(request_context_, path, blob_url, offset); |
70 } | 70 } |
71 | 71 |
72 void Cancel() { | 72 void Cancel() { |
73 if (!io_thread_->BelongsToCurrentThread()) { | 73 if (!io_thread_->BelongsToCurrentThread()) { |
74 io_thread_->PostTask( | 74 io_thread_->PostTask( |
75 FROM_HERE, | 75 FROM_HERE, |
76 base::Bind(&IOThreadProxy::Cancel, this)); | 76 base::Bind(&IOThreadProxy::Cancel, this)); |
77 return; | 77 return; |
78 } | 78 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 const std::string& name, | 127 const std::string& name, |
128 const GURL& root) { | 128 const GURL& root) { |
129 NOTREACHED(); | 129 NOTREACHED(); |
130 } | 130 } |
131 | 131 |
132 private: | 132 private: |
133 explicit CallbackDispatcher(IOThreadProxy* proxy) : proxy_(proxy) {} | 133 explicit CallbackDispatcher(IOThreadProxy* proxy) : proxy_(proxy) {} |
134 scoped_refptr<IOThreadProxy> proxy_; | 134 scoped_refptr<IOThreadProxy> proxy_; |
135 }; | 135 }; |
136 | 136 |
137 FileSystemOperation* GetNewOperation() { | 137 FileSystemOperationInterface* GetNewOperation(const GURL& path) { |
138 // The FileSystemOperation takes ownership of the CallbackDispatcher. | 138 // The FileSystemOperation takes ownership of the CallbackDispatcher. |
139 return new FileSystemOperation(CallbackDispatcher::Create(this), | 139 return file_system_context_->CreateFileSystemOperation( |
140 io_thread_, file_system_context_.get()); | 140 path, CallbackDispatcher::Create(this), io_thread_); |
141 } | 141 } |
142 | 142 |
143 void DidSucceed() { | 143 void DidSucceed() { |
144 if (!main_thread_->BelongsToCurrentThread()) { | 144 if (!main_thread_->BelongsToCurrentThread()) { |
145 main_thread_->PostTask( | 145 main_thread_->PostTask( |
146 FROM_HERE, | 146 FROM_HERE, |
147 base::Bind(&IOThreadProxy::DidSucceed, this)); | 147 base::Bind(&IOThreadProxy::DidSucceed, this)); |
148 return; | 148 return; |
149 } | 149 } |
150 if (simple_writer_) | 150 if (simple_writer_) |
(...skipping 27 matching lines...) Expand all Loading... |
178 operation_ = NULL; | 178 operation_ = NULL; |
179 } | 179 } |
180 | 180 |
181 scoped_refptr<base::MessageLoopProxy> io_thread_; | 181 scoped_refptr<base::MessageLoopProxy> io_thread_; |
182 scoped_refptr<base::MessageLoopProxy> main_thread_; | 182 scoped_refptr<base::MessageLoopProxy> main_thread_; |
183 | 183 |
184 // Only used on the main thread. | 184 // Only used on the main thread. |
185 base::WeakPtr<SimpleFileWriter> simple_writer_; | 185 base::WeakPtr<SimpleFileWriter> simple_writer_; |
186 | 186 |
187 // Only used on the io thread. | 187 // Only used on the io thread. |
188 FileSystemOperation* operation_; | 188 FileSystemOperationInterface* operation_; |
189 | 189 |
190 scoped_refptr<FileSystemContext> file_system_context_; | 190 scoped_refptr<FileSystemContext> file_system_context_; |
191 }; | 191 }; |
192 | 192 |
193 | 193 |
194 SimpleFileWriter::SimpleFileWriter( | 194 SimpleFileWriter::SimpleFileWriter( |
195 const GURL& path, | 195 const GURL& path, |
196 WebFileWriterClient* client, | 196 WebFileWriterClient* client, |
197 FileSystemContext* file_system_context) | 197 FileSystemContext* file_system_context) |
198 : WebFileWriterBase(path, client), | 198 : WebFileWriterBase(path, client), |
199 io_thread_proxy_(new IOThreadProxy(AsWeakPtr(), file_system_context)) { | 199 io_thread_proxy_(new IOThreadProxy(AsWeakPtr(), file_system_context)) { |
200 } | 200 } |
201 | 201 |
202 SimpleFileWriter::~SimpleFileWriter() { | 202 SimpleFileWriter::~SimpleFileWriter() { |
203 } | 203 } |
204 | 204 |
205 void SimpleFileWriter::DoTruncate(const GURL& path, int64 offset) { | 205 void SimpleFileWriter::DoTruncate(const GURL& path, int64 offset) { |
206 io_thread_proxy_->Truncate(path, offset); | 206 io_thread_proxy_->Truncate(path, offset); |
207 } | 207 } |
208 | 208 |
209 void SimpleFileWriter::DoWrite( | 209 void SimpleFileWriter::DoWrite( |
210 const GURL& path, const GURL& blob_url, int64 offset) { | 210 const GURL& path, const GURL& blob_url, int64 offset) { |
211 io_thread_proxy_->Write(path, blob_url, offset); | 211 io_thread_proxy_->Write(path, blob_url, offset); |
212 } | 212 } |
213 | 213 |
214 void SimpleFileWriter::DoCancel() { | 214 void SimpleFileWriter::DoCancel() { |
215 io_thread_proxy_->Cancel(); | 215 io_thread_proxy_->Cancel(); |
216 } | 216 } |
OLD | NEW |