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/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" |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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(GetNewOperation()); | 83 operation_->Cancel(CallbackDispatcher::Create(this)); |
84 } | 84 } |
85 | 85 |
86 private: | 86 private: |
87 // Inner class to receive callbacks from FileSystemOperation. | 87 // Inner class to receive callbacks from FileSystemOperation. |
88 class CallbackDispatcher : public FileSystemCallbackDispatcher { | 88 class CallbackDispatcher : public FileSystemCallbackDispatcher { |
89 public: | 89 public: |
90 explicit CallbackDispatcher(IOThreadProxy* proxy) : proxy_(proxy) { | 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)); |
91 } | 96 } |
92 | 97 |
93 ~CallbackDispatcher() { | 98 ~CallbackDispatcher() { |
94 proxy_->ClearOperation(); | 99 proxy_->ClearOperation(); |
95 } | 100 } |
96 | 101 |
97 virtual void DidSucceed() { | 102 virtual void DidSucceed() { |
98 proxy_->DidSucceed(); | 103 proxy_->DidSucceed(); |
99 } | 104 } |
100 | 105 |
(...skipping 16 matching lines...) Expand all Loading... |
117 bool has_more) { | 122 bool has_more) { |
118 NOTREACHED(); | 123 NOTREACHED(); |
119 } | 124 } |
120 | 125 |
121 virtual void DidOpenFileSystem( | 126 virtual void DidOpenFileSystem( |
122 const std::string& name, | 127 const std::string& name, |
123 const GURL& root) { | 128 const GURL& root) { |
124 NOTREACHED(); | 129 NOTREACHED(); |
125 } | 130 } |
126 | 131 |
| 132 private: |
| 133 explicit CallbackDispatcher(IOThreadProxy* proxy) : proxy_(proxy) {} |
127 scoped_refptr<IOThreadProxy> proxy_; | 134 scoped_refptr<IOThreadProxy> proxy_; |
128 }; | 135 }; |
129 | 136 |
130 FileSystemOperation* GetNewOperation() { | 137 FileSystemOperation* GetNewOperation() { |
131 // The FileSystemOperation takes ownership of the CallbackDispatcher. | 138 // The FileSystemOperation takes ownership of the CallbackDispatcher. |
132 return new FileSystemOperation(new CallbackDispatcher(this), | 139 return new FileSystemOperation(CallbackDispatcher::Create(this), |
133 io_thread_, file_system_context_.get()); | 140 io_thread_, file_system_context_.get()); |
134 } | 141 } |
135 | 142 |
136 void DidSucceed() { | 143 void DidSucceed() { |
137 if (!main_thread_->BelongsToCurrentThread()) { | 144 if (!main_thread_->BelongsToCurrentThread()) { |
138 main_thread_->PostTask( | 145 main_thread_->PostTask( |
139 FROM_HERE, | 146 FROM_HERE, |
140 base::Bind(&IOThreadProxy::DidSucceed, this)); | 147 base::Bind(&IOThreadProxy::DidSucceed, this)); |
141 return; | 148 return; |
142 } | 149 } |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 } | 207 } |
201 | 208 |
202 void SimpleFileWriter::DoWrite( | 209 void SimpleFileWriter::DoWrite( |
203 const GURL& path, const GURL& blob_url, int64 offset) { | 210 const GURL& path, const GURL& blob_url, int64 offset) { |
204 io_thread_proxy_->Write(path, blob_url, offset); | 211 io_thread_proxy_->Write(path, blob_url, offset); |
205 } | 212 } |
206 | 213 |
207 void SimpleFileWriter::DoCancel() { | 214 void SimpleFileWriter::DoCancel() { |
208 io_thread_proxy_->Cancel(); | 215 io_thread_proxy_->Cancel(); |
209 } | 216 } |
OLD | NEW |