| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/message_loop_proxy.h" | 8 #include "base/message_loop_proxy.h" |
| 9 #include "net/url_request/url_request_context.h" | 9 #include "net/url_request/url_request_context.h" |
| 10 #include "webkit/fileapi/file_system_callback_dispatcher.h" | 10 #include "webkit/fileapi/file_system_callback_dispatcher.h" |
| 11 #include "webkit/fileapi/file_system_context.h" |
| 12 #include "webkit/fileapi/file_system_file_util.h" |
| 11 #include "webkit/fileapi/file_system_operation.h" | 13 #include "webkit/fileapi/file_system_operation.h" |
| 12 #include "webkit/glue/webkit_glue.h" | 14 #include "webkit/glue/webkit_glue.h" |
| 13 #include "webkit/tools/test_shell/simple_resource_loader_bridge.h" | 15 #include "webkit/tools/test_shell/simple_resource_loader_bridge.h" |
| 14 | 16 |
| 17 using fileapi::FileSystemCallbackDispatcher; |
| 18 using fileapi::FileSystemContext; |
| 19 using fileapi::FileSystemFileUtil; |
| 15 using fileapi::FileSystemOperation; | 20 using fileapi::FileSystemOperation; |
| 16 using fileapi::FileSystemCallbackDispatcher; | |
| 17 using fileapi::WebFileWriterBase; | 21 using fileapi::WebFileWriterBase; |
| 18 using WebKit::WebFileWriterClient; | 22 using WebKit::WebFileWriterClient; |
| 19 using WebKit::WebString; | 23 using WebKit::WebString; |
| 20 using WebKit::WebURL; | 24 using WebKit::WebURL; |
| 21 | 25 |
| 22 net::URLRequestContext* SimpleFileWriter::request_context_ = NULL; | 26 net::URLRequestContext* SimpleFileWriter::request_context_ = NULL; |
| 23 | 27 |
| 24 // Helper class to proxy to write and truncate calls to the IO thread, | 28 // Helper class to proxy to write and truncate calls to the IO thread, |
| 25 // and to proxy the results back to the main thead. There is a one-to-one | 29 // and to proxy the results back to the main thead. There is a one-to-one |
| 26 // relationship between SimpleFileWriters and IOThreadBackends. | 30 // relationship between SimpleFileWriters and IOThreadBackends. |
| 27 class SimpleFileWriter::IOThreadProxy | 31 class SimpleFileWriter::IOThreadProxy |
| 28 : public base::RefCountedThreadSafe<SimpleFileWriter::IOThreadProxy> { | 32 : public base::RefCountedThreadSafe<SimpleFileWriter::IOThreadProxy> { |
| 29 public: | 33 public: |
| 30 explicit IOThreadProxy(const base::WeakPtr<SimpleFileWriter>& simple_writer) | 34 explicit IOThreadProxy(const base::WeakPtr<SimpleFileWriter>& simple_writer, |
| 35 FileSystemContext* file_system_context) |
| 31 : simple_writer_(simple_writer), | 36 : simple_writer_(simple_writer), |
| 32 operation_(NULL) { | 37 operation_(NULL), |
| 38 file_system_context_(file_system_context) { |
| 33 // The IO thread needs to be running for this class to work. | 39 // The IO thread needs to be running for this class to work. |
| 34 SimpleResourceLoaderBridge::EnsureIOThread(); | 40 SimpleResourceLoaderBridge::EnsureIOThread(); |
| 35 io_thread_ = SimpleResourceLoaderBridge::GetIoThread(); | 41 io_thread_ = SimpleResourceLoaderBridge::GetIoThread(); |
| 36 main_thread_ = base::MessageLoopProxy::CreateForCurrentThread(); | 42 main_thread_ = base::MessageLoopProxy::CreateForCurrentThread(); |
| 37 } | 43 } |
| 38 | 44 |
| 39 virtual ~IOThreadProxy() { | 45 virtual ~IOThreadProxy() { |
| 40 } | 46 } |
| 41 | 47 |
| 42 void Truncate(const FilePath& path, int64 offset) { | 48 void Truncate(const FilePath& path, int64 offset) { |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 const FilePath& root_path) { | 118 const FilePath& root_path) { |
| 113 NOTREACHED(); | 119 NOTREACHED(); |
| 114 } | 120 } |
| 115 | 121 |
| 116 scoped_refptr<IOThreadProxy> proxy_; | 122 scoped_refptr<IOThreadProxy> proxy_; |
| 117 }; | 123 }; |
| 118 | 124 |
| 119 FileSystemOperation* GetNewOperation() { | 125 FileSystemOperation* GetNewOperation() { |
| 120 // The FileSystemOperation takes ownership of the CallbackDispatcher. | 126 // The FileSystemOperation takes ownership of the CallbackDispatcher. |
| 121 return new FileSystemOperation(new CallbackDispatcher(this), | 127 return new FileSystemOperation(new CallbackDispatcher(this), |
| 122 io_thread_, NULL); | 128 io_thread_, file_system_context_.get(), |
| 129 NULL); |
| 123 } | 130 } |
| 124 | 131 |
| 125 void DidSucceed() { | 132 void DidSucceed() { |
| 126 if (!main_thread_->BelongsToCurrentThread()) { | 133 if (!main_thread_->BelongsToCurrentThread()) { |
| 127 main_thread_->PostTask(FROM_HERE, NewRunnableMethod( | 134 main_thread_->PostTask(FROM_HERE, NewRunnableMethod( |
| 128 this, &IOThreadProxy::DidSucceed)); | 135 this, &IOThreadProxy::DidSucceed)); |
| 129 return; | 136 return; |
| 130 } | 137 } |
| 131 if (simple_writer_) | 138 if (simple_writer_) |
| 132 simple_writer_->DidSucceed(); | 139 simple_writer_->DidSucceed(); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 158 } | 165 } |
| 159 | 166 |
| 160 scoped_refptr<base::MessageLoopProxy> io_thread_; | 167 scoped_refptr<base::MessageLoopProxy> io_thread_; |
| 161 scoped_refptr<base::MessageLoopProxy> main_thread_; | 168 scoped_refptr<base::MessageLoopProxy> main_thread_; |
| 162 | 169 |
| 163 // Only used on the main thread. | 170 // Only used on the main thread. |
| 164 base::WeakPtr<SimpleFileWriter> simple_writer_; | 171 base::WeakPtr<SimpleFileWriter> simple_writer_; |
| 165 | 172 |
| 166 // Only used on the io thread. | 173 // Only used on the io thread. |
| 167 FileSystemOperation* operation_; | 174 FileSystemOperation* operation_; |
| 175 |
| 176 scoped_refptr<FileSystemContext> file_system_context_; |
| 168 }; | 177 }; |
| 169 | 178 |
| 170 | 179 |
| 171 SimpleFileWriter::SimpleFileWriter( | 180 SimpleFileWriter::SimpleFileWriter( |
| 172 const WebString& path, WebFileWriterClient* client) | 181 const WebString& path, |
| 182 WebFileWriterClient* client, |
| 183 FileSystemContext* file_system_context) |
| 173 : WebFileWriterBase(path, client), | 184 : WebFileWriterBase(path, client), |
| 174 io_thread_proxy_(new IOThreadProxy(AsWeakPtr())) { | 185 io_thread_proxy_(new IOThreadProxy(AsWeakPtr(), file_system_context)) { |
| 175 } | 186 } |
| 176 | 187 |
| 177 SimpleFileWriter::~SimpleFileWriter() { | 188 SimpleFileWriter::~SimpleFileWriter() { |
| 178 } | 189 } |
| 179 | 190 |
| 180 void SimpleFileWriter::DoTruncate(const FilePath& path, int64 offset) { | 191 void SimpleFileWriter::DoTruncate(const FilePath& path, int64 offset) { |
| 181 io_thread_proxy_->Truncate(path, offset); | 192 io_thread_proxy_->Truncate(path, offset); |
| 182 } | 193 } |
| 183 | 194 |
| 184 void SimpleFileWriter::DoWrite( | 195 void SimpleFileWriter::DoWrite( |
| 185 const FilePath& path, const GURL& blob_url, int64 offset) { | 196 const FilePath& path, const GURL& blob_url, int64 offset) { |
| 186 io_thread_proxy_->Write(path, blob_url, offset); | 197 io_thread_proxy_->Write(path, blob_url, offset); |
| 187 } | 198 } |
| 188 | 199 |
| 189 void SimpleFileWriter::DoCancel() { | 200 void SimpleFileWriter::DoCancel() { |
| 190 io_thread_proxy_->Cancel(); | 201 io_thread_proxy_->Cancel(); |
| 191 } | 202 } |
| OLD | NEW |