Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(47)

Side by Side Diff: webkit/tools/test_shell/simple_file_writer.cc

Issue 9372044: Refactor FileSystemOperation to take callback for each method. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Reflected kinuko's comments + Fixture for failing-Write -> Cancel pattern. Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « webkit/tools/test_shell/simple_file_system.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 DidFailOnMainThread(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 DidSucceedOnMainThread() {
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::DidSucceedOnMainThread, 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 DidFailOnMainThread(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::DidFailOnMainThread, 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 DidWriteOnMainThread(int64 bytes, bool complete) {
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::DidWriteOnMainThread,
118 this, bytes, complete));
170 return; 119 return;
171 } 120 }
172 if (simple_writer_) 121 if (simple_writer_)
173 simple_writer_->DidWrite(bytes, complete); 122 simple_writer_->DidWrite(bytes, complete);
174 } 123 }
175 124
176 void ClearOperation() { 125 void ClearOperation() {
177 DCHECK(io_thread_->BelongsToCurrentThread()); 126 DCHECK(io_thread_->BelongsToCurrentThread());
178 operation_ = NULL; 127 operation_ = NULL;
179 } 128 }
180 129
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
181 scoped_refptr<base::MessageLoopProxy> io_thread_; 149 scoped_refptr<base::MessageLoopProxy> io_thread_;
182 scoped_refptr<base::MessageLoopProxy> main_thread_; 150 scoped_refptr<base::MessageLoopProxy> main_thread_;
183 151
184 // Only used on the main thread. 152 // Only used on the main thread.
185 base::WeakPtr<SimpleFileWriter> simple_writer_; 153 base::WeakPtr<SimpleFileWriter> simple_writer_;
186 154
187 // Only used on the io thread. 155 // Only used on the io thread.
188 FileSystemOperationInterface* operation_; 156 FileSystemOperationInterface* operation_;
189 157
190 scoped_refptr<FileSystemContext> file_system_context_; 158 scoped_refptr<FileSystemContext> file_system_context_;
(...skipping 16 matching lines...) Expand all
207 } 175 }
208 176
209 void SimpleFileWriter::DoWrite( 177 void SimpleFileWriter::DoWrite(
210 const GURL& path, const GURL& blob_url, int64 offset) { 178 const GURL& path, const GURL& blob_url, int64 offset) {
211 io_thread_proxy_->Write(path, blob_url, offset); 179 io_thread_proxy_->Write(path, blob_url, offset);
212 } 180 }
213 181
214 void SimpleFileWriter::DoCancel() { 182 void SimpleFileWriter::DoCancel() {
215 io_thread_proxy_->Cancel(); 183 io_thread_proxy_->Cancel();
216 } 184 }
OLDNEW
« no previous file with comments | « webkit/tools/test_shell/simple_file_system.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698