| 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_context.h" | 12 #include "webkit/fileapi/file_system_context.h" |
| 12 #include "webkit/fileapi/file_system_operation_interface.h" | 13 #include "webkit/fileapi/file_system_operation_interface.h" |
| 13 #include "webkit/glue/webkit_glue.h" | 14 #include "webkit/glue/webkit_glue.h" |
| 14 #include "webkit/tools/test_shell/simple_resource_loader_bridge.h" | 15 #include "webkit/tools/test_shell/simple_resource_loader_bridge.h" |
| 15 | 16 |
| 17 using fileapi::FileSystemCallbackDispatcher; |
| 16 using fileapi::FileSystemContext; | 18 using fileapi::FileSystemContext; |
| 17 using fileapi::FileSystemOperationInterface; | 19 using fileapi::FileSystemOperationInterface; |
| 18 using fileapi::WebFileWriterBase; | 20 using fileapi::WebFileWriterBase; |
| 19 using WebKit::WebFileWriterClient; | 21 using WebKit::WebFileWriterClient; |
| 20 using WebKit::WebString; | 22 using WebKit::WebString; |
| 21 using WebKit::WebURL; | 23 using WebKit::WebURL; |
| 22 | 24 |
| 23 net::URLRequestContext* SimpleFileWriter::request_context_ = NULL; | 25 net::URLRequestContext* SimpleFileWriter::request_context_ = NULL; |
| 24 | 26 |
| 25 // 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, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 44 | 46 |
| 45 void Truncate(const GURL& path, int64 offset) { | 47 void Truncate(const GURL& path, int64 offset) { |
| 46 if (!io_thread_->BelongsToCurrentThread()) { | 48 if (!io_thread_->BelongsToCurrentThread()) { |
| 47 io_thread_->PostTask( | 49 io_thread_->PostTask( |
| 48 FROM_HERE, | 50 FROM_HERE, |
| 49 base::Bind(&IOThreadProxy::Truncate, this, path, offset)); | 51 base::Bind(&IOThreadProxy::Truncate, this, path, offset)); |
| 50 return; | 52 return; |
| 51 } | 53 } |
| 52 DCHECK(!operation_); | 54 DCHECK(!operation_); |
| 53 operation_ = GetNewOperation(path); | 55 operation_ = GetNewOperation(path); |
| 54 operation_->Truncate(path, offset, | 56 operation_->Truncate(path, offset); |
| 55 base::Bind(&IOThreadProxy::DidFinish, this)); | |
| 56 } | 57 } |
| 57 | 58 |
| 58 void Write(const GURL& path, const GURL& blob_url, int64 offset) { | 59 void Write(const GURL& path, const GURL& blob_url, int64 offset) { |
| 59 if (!io_thread_->BelongsToCurrentThread()) { | 60 if (!io_thread_->BelongsToCurrentThread()) { |
| 60 io_thread_->PostTask( | 61 io_thread_->PostTask( |
| 61 FROM_HERE, | 62 FROM_HERE, |
| 62 base::Bind(&IOThreadProxy::Write, this, path, blob_url, offset)); | 63 base::Bind(&IOThreadProxy::Write, this, path, blob_url, offset)); |
| 63 return; | 64 return; |
| 64 } | 65 } |
| 65 DCHECK(request_context_); | 66 DCHECK(request_context_); |
| 66 DCHECK(!operation_); | 67 DCHECK(!operation_); |
| 67 operation_ = GetNewOperation(path); | 68 operation_ = GetNewOperation(path); |
| 68 operation_->Write(request_context_, path, blob_url, offset, | 69 operation_->Write(request_context_, path, blob_url, offset); |
| 69 base::Bind(&IOThreadProxy::DidWrite, this)); | |
| 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 } |
| 79 if (!operation_) { | 79 if (!operation_) { |
| 80 DidFailOnMainThread(base::PLATFORM_FILE_ERROR_INVALID_OPERATION); | 80 DidFail(base::PLATFORM_FILE_ERROR_INVALID_OPERATION); |
| 81 return; | 81 return; |
| 82 } | 82 } |
| 83 operation_->Cancel(base::Bind(&IOThreadProxy::DidFinish, this)); | 83 operation_->Cancel(CallbackDispatcher::Create(this)); |
| 84 } | 84 } |
| 85 | 85 |
| 86 private: | 86 private: |
| 87 // Inner class to receive callbacks from FileSystemOperation. |
| 88 class CallbackDispatcher : public FileSystemCallbackDispatcher { |
| 89 public: |
| 90 // An instance of this class must be created by Create() |
| 91 // (so that we do not leak ownerships). |
| 92 static scoped_ptr<FileSystemCallbackDispatcher> Create( |
| 93 IOThreadProxy* proxy) { |
| 94 return scoped_ptr<FileSystemCallbackDispatcher>( |
| 95 new CallbackDispatcher(proxy)); |
| 96 } |
| 97 |
| 98 ~CallbackDispatcher() { |
| 99 proxy_->ClearOperation(); |
| 100 } |
| 101 |
| 102 virtual void DidSucceed() { |
| 103 proxy_->DidSucceed(); |
| 104 } |
| 105 |
| 106 virtual void DidFail(base::PlatformFileError error_code) { |
| 107 proxy_->DidFail(error_code); |
| 108 } |
| 109 |
| 110 virtual void DidWrite(int64 bytes, bool complete) { |
| 111 proxy_->DidWrite(bytes, complete); |
| 112 } |
| 113 |
| 114 virtual void DidReadMetadata( |
| 115 const base::PlatformFileInfo&, |
| 116 const FilePath&) { |
| 117 NOTREACHED(); |
| 118 } |
| 119 |
| 120 virtual void DidReadDirectory( |
| 121 const std::vector<base::FileUtilProxy::Entry>& entries, |
| 122 bool has_more) { |
| 123 NOTREACHED(); |
| 124 } |
| 125 |
| 126 virtual void DidOpenFileSystem( |
| 127 const std::string& name, |
| 128 const GURL& root) { |
| 129 NOTREACHED(); |
| 130 } |
| 131 |
| 132 private: |
| 133 explicit CallbackDispatcher(IOThreadProxy* proxy) : proxy_(proxy) {} |
| 134 scoped_refptr<IOThreadProxy> proxy_; |
| 135 }; |
| 136 |
| 87 FileSystemOperationInterface* GetNewOperation(const GURL& path) { | 137 FileSystemOperationInterface* GetNewOperation(const GURL& path) { |
| 88 return file_system_context_->CreateFileSystemOperation(path, io_thread_); | 138 // The FileSystemOperation takes ownership of the CallbackDispatcher. |
| 139 return file_system_context_->CreateFileSystemOperation( |
| 140 path, CallbackDispatcher::Create(this), io_thread_); |
| 89 } | 141 } |
| 90 | 142 |
| 91 void DidSucceedOnMainThread() { | 143 void DidSucceed() { |
| 92 if (!main_thread_->BelongsToCurrentThread()) { | 144 if (!main_thread_->BelongsToCurrentThread()) { |
| 93 main_thread_->PostTask( | 145 main_thread_->PostTask( |
| 94 FROM_HERE, | 146 FROM_HERE, |
| 95 base::Bind(&IOThreadProxy::DidSucceedOnMainThread, this)); | 147 base::Bind(&IOThreadProxy::DidSucceed, this)); |
| 96 return; | 148 return; |
| 97 } | 149 } |
| 98 if (simple_writer_) | 150 if (simple_writer_) |
| 99 simple_writer_->DidSucceed(); | 151 simple_writer_->DidSucceed(); |
| 100 } | 152 } |
| 101 | 153 |
| 102 void DidFailOnMainThread(base::PlatformFileError error_code) { | 154 void DidFail(base::PlatformFileError error_code) { |
| 103 if (!main_thread_->BelongsToCurrentThread()) { | 155 if (!main_thread_->BelongsToCurrentThread()) { |
| 104 main_thread_->PostTask( | 156 main_thread_->PostTask( |
| 105 FROM_HERE, | 157 FROM_HERE, |
| 106 base::Bind(&IOThreadProxy::DidFailOnMainThread, this, error_code)); | 158 base::Bind(&IOThreadProxy::DidFail, this, error_code)); |
| 107 return; | 159 return; |
| 108 } | 160 } |
| 109 if (simple_writer_) | 161 if (simple_writer_) |
| 110 simple_writer_->DidFail(error_code); | 162 simple_writer_->DidFail(error_code); |
| 111 } | 163 } |
| 112 | 164 |
| 113 void DidWriteOnMainThread(int64 bytes, bool complete) { | 165 void DidWrite(int64 bytes, bool complete) { |
| 114 if (!main_thread_->BelongsToCurrentThread()) { | 166 if (!main_thread_->BelongsToCurrentThread()) { |
| 115 main_thread_->PostTask( | 167 main_thread_->PostTask( |
| 116 FROM_HERE, | 168 FROM_HERE, |
| 117 base::Bind(&IOThreadProxy::DidWriteOnMainThread, | 169 base::Bind(&IOThreadProxy::DidWrite, this, bytes, complete)); |
| 118 this, bytes, complete)); | |
| 119 return; | 170 return; |
| 120 } | 171 } |
| 121 if (simple_writer_) | 172 if (simple_writer_) |
| 122 simple_writer_->DidWrite(bytes, complete); | 173 simple_writer_->DidWrite(bytes, complete); |
| 123 } | 174 } |
| 124 | 175 |
| 125 void ClearOperation() { | 176 void ClearOperation() { |
| 126 DCHECK(io_thread_->BelongsToCurrentThread()); | 177 DCHECK(io_thread_->BelongsToCurrentThread()); |
| 127 operation_ = NULL; | 178 operation_ = NULL; |
| 128 } | 179 } |
| 129 | 180 |
| 130 void DidFinish(base::PlatformFileError result) { | |
| 131 if (result == base::PLATFORM_FILE_OK) | |
| 132 DidSucceedOnMainThread(); | |
| 133 else | |
| 134 DidFailOnMainThread(result); | |
| 135 ClearOperation(); | |
| 136 } | |
| 137 | |
| 138 void DidWrite(base::PlatformFileError result, int64 bytes, bool complete) { | |
| 139 if (result == base::PLATFORM_FILE_OK) { | |
| 140 DidWriteOnMainThread(bytes, complete); | |
| 141 if (complete) | |
| 142 ClearOperation(); | |
| 143 } else { | |
| 144 DidFailOnMainThread(result); | |
| 145 ClearOperation(); | |
| 146 } | |
| 147 } | |
| 148 | |
| 149 scoped_refptr<base::MessageLoopProxy> io_thread_; | 181 scoped_refptr<base::MessageLoopProxy> io_thread_; |
| 150 scoped_refptr<base::MessageLoopProxy> main_thread_; | 182 scoped_refptr<base::MessageLoopProxy> main_thread_; |
| 151 | 183 |
| 152 // Only used on the main thread. | 184 // Only used on the main thread. |
| 153 base::WeakPtr<SimpleFileWriter> simple_writer_; | 185 base::WeakPtr<SimpleFileWriter> simple_writer_; |
| 154 | 186 |
| 155 // Only used on the io thread. | 187 // Only used on the io thread. |
| 156 FileSystemOperationInterface* operation_; | 188 FileSystemOperationInterface* operation_; |
| 157 | 189 |
| 158 scoped_refptr<FileSystemContext> file_system_context_; | 190 scoped_refptr<FileSystemContext> file_system_context_; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 175 } | 207 } |
| 176 | 208 |
| 177 void SimpleFileWriter::DoWrite( | 209 void SimpleFileWriter::DoWrite( |
| 178 const GURL& path, const GURL& blob_url, int64 offset) { | 210 const GURL& path, const GURL& blob_url, int64 offset) { |
| 179 io_thread_proxy_->Write(path, blob_url, offset); | 211 io_thread_proxy_->Write(path, blob_url, offset); |
| 180 } | 212 } |
| 181 | 213 |
| 182 void SimpleFileWriter::DoCancel() { | 214 void SimpleFileWriter::DoCancel() { |
| 183 io_thread_proxy_->Cancel(); | 215 io_thread_proxy_->Cancel(); |
| 184 } | 216 } |
| OLD | NEW |