Chromium Code Reviews| 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(), |
|
kinuko
2011/03/22 00:16:24
Yeah, I was thinking we should have done this.
| |
| 129 NULL); | |
| 130 | |
| 123 } | 131 } |
| 124 | 132 |
| 125 void DidSucceed() { | 133 void DidSucceed() { |
| 126 if (!main_thread_->BelongsToCurrentThread()) { | 134 if (!main_thread_->BelongsToCurrentThread()) { |
| 127 main_thread_->PostTask(FROM_HERE, NewRunnableMethod( | 135 main_thread_->PostTask(FROM_HERE, NewRunnableMethod( |
| 128 this, &IOThreadProxy::DidSucceed)); | 136 this, &IOThreadProxy::DidSucceed)); |
| 129 return; | 137 return; |
| 130 } | 138 } |
| 131 if (simple_writer_) | 139 if (simple_writer_) |
| 132 simple_writer_->DidSucceed(); | 140 simple_writer_->DidSucceed(); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 158 } | 166 } |
| 159 | 167 |
| 160 scoped_refptr<base::MessageLoopProxy> io_thread_; | 168 scoped_refptr<base::MessageLoopProxy> io_thread_; |
| 161 scoped_refptr<base::MessageLoopProxy> main_thread_; | 169 scoped_refptr<base::MessageLoopProxy> main_thread_; |
| 162 | 170 |
| 163 // Only used on the main thread. | 171 // Only used on the main thread. |
| 164 base::WeakPtr<SimpleFileWriter> simple_writer_; | 172 base::WeakPtr<SimpleFileWriter> simple_writer_; |
| 165 | 173 |
| 166 // Only used on the io thread. | 174 // Only used on the io thread. |
| 167 FileSystemOperation* operation_; | 175 FileSystemOperation* operation_; |
| 176 | |
| 177 scoped_refptr<FileSystemContext> file_system_context_; | |
| 168 }; | 178 }; |
| 169 | 179 |
| 170 | 180 |
| 171 SimpleFileWriter::SimpleFileWriter( | 181 SimpleFileWriter::SimpleFileWriter( |
| 172 const WebString& path, WebFileWriterClient* client) | 182 const WebString& path, |
| 183 WebFileWriterClient* client, | |
| 184 FileSystemContext* file_system_context) | |
| 173 : WebFileWriterBase(path, client), | 185 : WebFileWriterBase(path, client), |
| 174 io_thread_proxy_(new IOThreadProxy(AsWeakPtr())) { | 186 io_thread_proxy_(new IOThreadProxy(AsWeakPtr(), file_system_context)) { |
| 175 } | 187 } |
| 176 | 188 |
| 177 SimpleFileWriter::~SimpleFileWriter() { | 189 SimpleFileWriter::~SimpleFileWriter() { |
| 178 } | 190 } |
| 179 | 191 |
| 180 void SimpleFileWriter::DoTruncate(const FilePath& path, int64 offset) { | 192 void SimpleFileWriter::DoTruncate(const FilePath& path, int64 offset) { |
| 181 io_thread_proxy_->Truncate(path, offset); | 193 io_thread_proxy_->Truncate(path, offset); |
| 182 } | 194 } |
| 183 | 195 |
| 184 void SimpleFileWriter::DoWrite( | 196 void SimpleFileWriter::DoWrite( |
| 185 const FilePath& path, const GURL& blob_url, int64 offset) { | 197 const FilePath& path, const GURL& blob_url, int64 offset) { |
| 186 io_thread_proxy_->Write(path, blob_url, offset); | 198 io_thread_proxy_->Write(path, blob_url, offset); |
| 187 } | 199 } |
| 188 | 200 |
| 189 void SimpleFileWriter::DoCancel() { | 201 void SimpleFileWriter::DoCancel() { |
| 190 io_thread_proxy_->Cancel(); | 202 io_thread_proxy_->Cancel(); |
| 191 } | 203 } |
| OLD | NEW |