Chromium Code Reviews| 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" | |
| 12 #include "webkit/fileapi/file_system_context.h" | 11 #include "webkit/fileapi/file_system_context.h" |
| 13 #include "webkit/fileapi/file_system_operation_interface.h" | 12 #include "webkit/fileapi/file_system_operation_interface.h" |
| 14 #include "webkit/glue/webkit_glue.h" | 13 #include "webkit/glue/webkit_glue.h" |
| 15 #include "webkit/tools/test_shell/simple_resource_loader_bridge.h" | 14 #include "webkit/tools/test_shell/simple_resource_loader_bridge.h" |
| 16 | 15 |
| 17 using fileapi::FileSystemCallbackDispatcher; | |
| 18 using fileapi::FileSystemContext; | 16 using fileapi::FileSystemContext; |
| 19 using fileapi::FileSystemOperationInterface; | 17 using fileapi::FileSystemOperationInterface; |
| 20 using fileapi::WebFileWriterBase; | 18 using fileapi::WebFileWriterBase; |
| 21 using WebKit::WebFileWriterClient; | 19 using WebKit::WebFileWriterClient; |
| 22 using WebKit::WebString; | 20 using WebKit::WebString; |
| 23 using WebKit::WebURL; | 21 using WebKit::WebURL; |
| 24 | 22 |
| 25 net::URLRequestContext* SimpleFileWriter::request_context_ = NULL; | 23 net::URLRequestContext* SimpleFileWriter::request_context_ = NULL; |
| 26 | 24 |
| 27 // Helper class to proxy to write and truncate calls to the IO thread, | 25 // Helper class to proxy to write and truncate calls to the IO thread, |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 46 | 44 |
| 47 void Truncate(const GURL& path, int64 offset) { | 45 void Truncate(const GURL& path, int64 offset) { |
| 48 if (!io_thread_->BelongsToCurrentThread()) { | 46 if (!io_thread_->BelongsToCurrentThread()) { |
| 49 io_thread_->PostTask( | 47 io_thread_->PostTask( |
| 50 FROM_HERE, | 48 FROM_HERE, |
| 51 base::Bind(&IOThreadProxy::Truncate, this, path, offset)); | 49 base::Bind(&IOThreadProxy::Truncate, this, path, offset)); |
| 52 return; | 50 return; |
| 53 } | 51 } |
| 54 DCHECK(!operation_); | 52 DCHECK(!operation_); |
| 55 operation_ = GetNewOperation(path); | 53 operation_ = GetNewOperation(path); |
| 56 operation_->Truncate(path, offset); | 54 operation_->Truncate(path, offset, |
| 55 base::Bind(&IOThreadProxy::DidFinish, this)); | |
| 57 } | 56 } |
| 58 | 57 |
| 59 void Write(const GURL& path, const GURL& blob_url, int64 offset) { | 58 void Write(const GURL& path, const GURL& blob_url, int64 offset) { |
| 60 if (!io_thread_->BelongsToCurrentThread()) { | 59 if (!io_thread_->BelongsToCurrentThread()) { |
| 61 io_thread_->PostTask( | 60 io_thread_->PostTask( |
| 62 FROM_HERE, | 61 FROM_HERE, |
| 63 base::Bind(&IOThreadProxy::Write, this, path, blob_url, offset)); | 62 base::Bind(&IOThreadProxy::Write, this, path, blob_url, offset)); |
| 64 return; | 63 return; |
| 65 } | 64 } |
| 66 DCHECK(request_context_); | 65 DCHECK(request_context_); |
| 67 DCHECK(!operation_); | 66 DCHECK(!operation_); |
| 68 operation_ = GetNewOperation(path); | 67 operation_ = GetNewOperation(path); |
| 69 operation_->Write(request_context_, path, blob_url, offset); | 68 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 DidFail(base::PLATFORM_FILE_ERROR_INVALID_OPERATION); | 80 DidFail(base::PLATFORM_FILE_ERROR_INVALID_OPERATION); |
| 81 return; | 81 return; |
| 82 } | 82 } |
| 83 operation_->Cancel(CallbackDispatcher::Create(this)); | 83 operation_->Cancel(base::Bind(&IOThreadProxy::DidFinish, 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 | |
| 137 FileSystemOperationInterface* GetNewOperation(const GURL& path) { | 87 FileSystemOperationInterface* GetNewOperation(const GURL& path) { |
| 138 // The FileSystemOperation takes ownership of the CallbackDispatcher. | 88 return file_system_context_->CreateFileSystemOperation(path, io_thread_); |
| 139 return file_system_context_->CreateFileSystemOperation( | |
| 140 path, CallbackDispatcher::Create(this), io_thread_); | |
| 141 } | 89 } |
| 142 | 90 |
| 143 void DidSucceed() { | 91 void DidSucceed() { |
| 144 if (!main_thread_->BelongsToCurrentThread()) { | 92 if (!main_thread_->BelongsToCurrentThread()) { |
| 145 main_thread_->PostTask( | 93 main_thread_->PostTask( |
| 146 FROM_HERE, | 94 FROM_HERE, |
| 147 base::Bind(&IOThreadProxy::DidSucceed, this)); | 95 base::Bind(&IOThreadProxy::DidSucceed, this)); |
| 148 return; | 96 return; |
| 149 } | 97 } |
| 150 if (simple_writer_) | 98 if (simple_writer_) |
| 151 simple_writer_->DidSucceed(); | 99 simple_writer_->DidSucceed(); |
| 152 } | 100 } |
| 153 | 101 |
| 154 void DidFail(base::PlatformFileError error_code) { | 102 void DidFail(base::PlatformFileError error_code) { |
| 155 if (!main_thread_->BelongsToCurrentThread()) { | 103 if (!main_thread_->BelongsToCurrentThread()) { |
| 156 main_thread_->PostTask( | 104 main_thread_->PostTask( |
| 157 FROM_HERE, | 105 FROM_HERE, |
| 158 base::Bind(&IOThreadProxy::DidFail, this, error_code)); | 106 base::Bind(&IOThreadProxy::DidFail, this, error_code)); |
| 159 return; | 107 return; |
| 160 } | 108 } |
| 161 if (simple_writer_) | 109 if (simple_writer_) |
| 162 simple_writer_->DidFail(error_code); | 110 simple_writer_->DidFail(error_code); |
| 163 } | 111 } |
| 164 | 112 |
| 165 void DidWrite(int64 bytes, bool complete) { | 113 void DidWriteMain(int64 bytes, bool complete) { |
|
kinuko
2012/02/10 05:34:09
If this is only called from DidWrite maybe we coul
kinaba
2012/02/10 08:22:58
I'd rather keep it in the current form. DidWrite (
kinuko
2012/02/10 08:49:14
Ah ok. Could we rename DidWriteMain to DidWriteOn
kinaba
2012/02/10 09:15:28
Roger.
| |
| 166 if (!main_thread_->BelongsToCurrentThread()) { | 114 if (!main_thread_->BelongsToCurrentThread()) { |
| 167 main_thread_->PostTask( | 115 main_thread_->PostTask( |
| 168 FROM_HERE, | 116 FROM_HERE, |
| 169 base::Bind(&IOThreadProxy::DidWrite, this, bytes, complete)); | 117 base::Bind(&IOThreadProxy::DidWriteMain, this, bytes, complete)); |
| 170 return; | 118 return; |
| 171 } | 119 } |
| 172 if (simple_writer_) | 120 if (simple_writer_) |
| 173 simple_writer_->DidWrite(bytes, complete); | 121 simple_writer_->DidWrite(bytes, complete); |
| 174 } | 122 } |
| 175 | 123 |
| 176 void ClearOperation() { | 124 void ClearOperation() { |
| 177 DCHECK(io_thread_->BelongsToCurrentThread()); | 125 DCHECK(io_thread_->BelongsToCurrentThread()); |
| 178 operation_ = NULL; | 126 operation_ = NULL; |
| 179 } | 127 } |
| 180 | 128 |
| 129 void DidFinish(base::PlatformFileError result) { | |
| 130 if (result == base::PLATFORM_FILE_OK) | |
| 131 DidSucceed(); | |
| 132 else | |
| 133 DidFail(result); | |
| 134 ClearOperation(); | |
| 135 } | |
| 136 | |
| 137 void DidWrite(base::PlatformFileError result, int64 bytes, bool complete) { | |
| 138 if (result == base::PLATFORM_FILE_OK) { | |
| 139 DidWriteMain(bytes, complete); | |
| 140 if (complete) | |
| 141 ClearOperation(); | |
| 142 } else { | |
| 143 DidFail(result); | |
| 144 ClearOperation(); | |
| 145 } | |
| 146 } | |
| 147 | |
| 181 scoped_refptr<base::MessageLoopProxy> io_thread_; | 148 scoped_refptr<base::MessageLoopProxy> io_thread_; |
| 182 scoped_refptr<base::MessageLoopProxy> main_thread_; | 149 scoped_refptr<base::MessageLoopProxy> main_thread_; |
| 183 | 150 |
| 184 // Only used on the main thread. | 151 // Only used on the main thread. |
| 185 base::WeakPtr<SimpleFileWriter> simple_writer_; | 152 base::WeakPtr<SimpleFileWriter> simple_writer_; |
| 186 | 153 |
| 187 // Only used on the io thread. | 154 // Only used on the io thread. |
| 188 FileSystemOperationInterface* operation_; | 155 FileSystemOperationInterface* operation_; |
| 189 | 156 |
| 190 scoped_refptr<FileSystemContext> file_system_context_; | 157 scoped_refptr<FileSystemContext> file_system_context_; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 207 } | 174 } |
| 208 | 175 |
| 209 void SimpleFileWriter::DoWrite( | 176 void SimpleFileWriter::DoWrite( |
| 210 const GURL& path, const GURL& blob_url, int64 offset) { | 177 const GURL& path, const GURL& blob_url, int64 offset) { |
| 211 io_thread_proxy_->Write(path, blob_url, offset); | 178 io_thread_proxy_->Write(path, blob_url, offset); |
| 212 } | 179 } |
| 213 | 180 |
| 214 void SimpleFileWriter::DoCancel() { | 181 void SimpleFileWriter::DoCancel() { |
| 215 io_thread_proxy_->Cancel(); | 182 io_thread_proxy_->Cancel(); |
| 216 } | 183 } |
| OLD | NEW |