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

Side by Side Diff: webkit/fileapi/file_system_url_request_job.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
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/fileapi/file_system_url_request_job.h" 5 #include "webkit/fileapi/file_system_url_request_job.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/compiler_specific.h" 8 #include "base/compiler_specific.h"
9 #include "base/file_path.h" 9 #include "base/file_path.h"
10 #include "base/file_util_proxy.h" 10 #include "base/file_util_proxy.h"
11 #include "base/message_loop.h" 11 #include "base/message_loop.h"
12 #include "base/platform_file.h" 12 #include "base/platform_file.h"
13 #include "base/threading/thread_restrictions.h" 13 #include "base/threading/thread_restrictions.h"
14 #include "build/build_config.h" 14 #include "build/build_config.h"
15 #include "googleurl/src/gurl.h" 15 #include "googleurl/src/gurl.h"
16 #include "net/base/file_stream.h" 16 #include "net/base/file_stream.h"
17 #include "net/base/io_buffer.h" 17 #include "net/base/io_buffer.h"
18 #include "net/base/mime_util.h" 18 #include "net/base/mime_util.h"
19 #include "net/base/net_errors.h" 19 #include "net/base/net_errors.h"
20 #include "net/base/net_util.h" 20 #include "net/base/net_util.h"
21 #include "net/http/http_response_headers.h" 21 #include "net/http/http_response_headers.h"
22 #include "net/http/http_response_info.h" 22 #include "net/http/http_response_info.h"
23 #include "net/http/http_util.h" 23 #include "net/http/http_util.h"
24 #include "net/url_request/url_request.h" 24 #include "net/url_request/url_request.h"
25 #include "webkit/fileapi/file_system_callback_dispatcher.h"
26 #include "webkit/fileapi/file_system_context.h" 25 #include "webkit/fileapi/file_system_context.h"
27 #include "webkit/fileapi/file_system_operation.h" 26 #include "webkit/fileapi/file_system_operation.h"
28 #include "webkit/fileapi/file_system_util.h" 27 #include "webkit/fileapi/file_system_util.h"
29 28
30 using net::URLRequest; 29 using net::URLRequest;
31 using net::URLRequestJob; 30 using net::URLRequestJob;
32 using net::URLRequestStatus; 31 using net::URLRequestStatus;
33 32
34 namespace fileapi { 33 namespace fileapi {
35 34
(...skipping 10 matching lines...) Expand all
46 new net::HttpResponseHeaders(std::string(kStatus, kStatusLen)); 45 new net::HttpResponseHeaders(std::string(kStatus, kStatusLen));
47 46
48 // Tell WebKit never to cache this content. 47 // Tell WebKit never to cache this content.
49 std::string cache_control(net::HttpRequestHeaders::kCacheControl); 48 std::string cache_control(net::HttpRequestHeaders::kCacheControl);
50 cache_control.append(": no-cache"); 49 cache_control.append(": no-cache");
51 headers->AddHeader(cache_control); 50 headers->AddHeader(cache_control);
52 51
53 return headers; 52 return headers;
54 } 53 }
55 54
56 class FileSystemURLRequestJob::CallbackDispatcher
57 : public FileSystemCallbackDispatcher {
58 public:
59 // An instance of this class must be created by Create()
60 // (so that we do not leak ownerships).
61 static scoped_ptr<FileSystemCallbackDispatcher> Create(
62 FileSystemURLRequestJob* job) {
63 return scoped_ptr<FileSystemCallbackDispatcher>(
64 new CallbackDispatcher(job));
65 }
66
67 // fileapi::FileSystemCallbackDispatcher overrides.
68 virtual void DidSucceed() OVERRIDE {
69 NOTREACHED();
70 }
71
72 virtual void DidReadMetadata(const base::PlatformFileInfo& file_info,
73 const FilePath& platform_path) OVERRIDE {
74 job_->DidGetMetadata(file_info, platform_path);
75 }
76
77 virtual void DidReadDirectory(
78 const std::vector<base::FileUtilProxy::Entry>& entries,
79 bool has_more) OVERRIDE {
80 NOTREACHED();
81 }
82
83 virtual void DidWrite(int64 bytes, bool complete) OVERRIDE {
84 NOTREACHED();
85 }
86
87 virtual void DidOpenFileSystem(const std::string& name,
88 const GURL& root_path) OVERRIDE {
89 NOTREACHED();
90 }
91
92 virtual void DidFail(base::PlatformFileError error_code) OVERRIDE {
93 int rv = net::ERR_FILE_NOT_FOUND;
94 if (error_code == base::PLATFORM_FILE_ERROR_INVALID_URL)
95 rv = net::ERR_INVALID_URL;
96 job_->NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv));
97 }
98
99 private:
100 explicit CallbackDispatcher(FileSystemURLRequestJob* job) : job_(job) {
101 DCHECK(job_);
102 }
103
104 // TODO(adamk): Get rid of the need for refcounting here by
105 // allowing FileSystemOperations to be cancelled.
106 scoped_refptr<FileSystemURLRequestJob> job_;
107 DISALLOW_COPY_AND_ASSIGN(CallbackDispatcher);
108 };
109
110 FileSystemURLRequestJob::FileSystemURLRequestJob( 55 FileSystemURLRequestJob::FileSystemURLRequestJob(
111 URLRequest* request, FileSystemContext* file_system_context, 56 URLRequest* request, FileSystemContext* file_system_context,
112 scoped_refptr<base::MessageLoopProxy> file_thread_proxy) 57 scoped_refptr<base::MessageLoopProxy> file_thread_proxy)
113 : URLRequestJob(request), 58 : URLRequestJob(request),
114 file_system_context_(file_system_context), 59 file_system_context_(file_system_context),
115 file_thread_proxy_(file_thread_proxy), 60 file_thread_proxy_(file_thread_proxy),
116 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), 61 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
117 stream_(NULL), 62 stream_(NULL),
118 is_directory_(false), 63 is_directory_(false),
119 remaining_bytes_(0) { 64 remaining_bytes_(0) {
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 return 200; 168 return 200;
224 return URLRequestJob::GetResponseCode(); 169 return URLRequestJob::GetResponseCode();
225 } 170 }
226 171
227 void FileSystemURLRequestJob::StartAsync() { 172 void FileSystemURLRequestJob::StartAsync() {
228 if (!request_) 173 if (!request_)
229 return; 174 return;
230 FileSystemOperationInterface* operation = 175 FileSystemOperationInterface* operation =
231 file_system_context_->CreateFileSystemOperation( 176 file_system_context_->CreateFileSystemOperation(
232 request_->url(), 177 request_->url(),
233 CallbackDispatcher::Create(this),
234 file_thread_proxy_); 178 file_thread_proxy_);
235 if (!operation) { 179 if (!operation) {
236 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, 180 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED,
237 net::ERR_INVALID_URL)); 181 net::ERR_INVALID_URL));
238 return; 182 return;
239 } 183 }
240 operation->GetMetadata(request_->url()); 184 operation->GetMetadata(
185 request_->url(),
186 base::Bind(&FileSystemURLRequestJob::DidGetMetadata, this));
241 } 187 }
242 188
243 void FileSystemURLRequestJob::DidGetMetadata( 189 void FileSystemURLRequestJob::DidGetMetadata(
190 base::PlatformFileError error_code,
244 const base::PlatformFileInfo& file_info, 191 const base::PlatformFileInfo& file_info,
245 const FilePath& platform_path) { 192 const FilePath& platform_path) {
193 if (error_code != base::PLATFORM_FILE_OK) {
194 NotifyFailed(error_code == base::PLATFORM_FILE_ERROR_INVALID_URL ?
195 net::ERR_INVALID_URL : net::ERR_FILE_NOT_FOUND);
196 return;
197 }
198
246 // We may have been orphaned... 199 // We may have been orphaned...
247 if (!request_) 200 if (!request_)
248 return; 201 return;
249 202
250 is_directory_ = file_info.is_directory; 203 is_directory_ = file_info.is_directory;
251 204
252 if (!byte_range_.ComputeBounds(file_info.size)) { 205 if (!byte_range_.ComputeBounds(file_info.size)) {
253 NotifyFailed(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE); 206 NotifyFailed(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE);
254 return; 207 return;
255 } 208 }
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
326 } 279 }
327 280
328 return false; 281 return false;
329 } 282 }
330 283
331 void FileSystemURLRequestJob::NotifyFailed(int rv) { 284 void FileSystemURLRequestJob::NotifyFailed(int rv) {
332 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv)); 285 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv));
333 } 286 }
334 287
335 } // namespace fileapi 288 } // namespace fileapi
OLDNEW
« no previous file with comments | « webkit/fileapi/file_system_url_request_job.h ('k') | webkit/fileapi/file_writer_delegate_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698