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

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: 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 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 return 200; 163 return 200;
219 return URLRequestJob::GetResponseCode(); 164 return URLRequestJob::GetResponseCode();
220 } 165 }
221 166
222 void FileSystemURLRequestJob::StartAsync() { 167 void FileSystemURLRequestJob::StartAsync() {
223 if (!request_) 168 if (!request_)
224 return; 169 return;
225 FileSystemOperationInterface* operation = 170 FileSystemOperationInterface* operation =
226 file_system_context_->CreateFileSystemOperation( 171 file_system_context_->CreateFileSystemOperation(
227 request_->url(), 172 request_->url(),
228 CallbackDispatcher::Create(this),
229 file_thread_proxy_); 173 file_thread_proxy_);
230 if (!operation) { 174 if (!operation) {
231 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, 175 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED,
232 net::ERR_INVALID_URL)); 176 net::ERR_INVALID_URL));
233 return; 177 return;
234 } 178 }
235 operation->GetMetadata(request_->url()); 179 operation->GetMetadata(
180 request_->url(),
181 base::Bind(&FileSystemURLRequestJob::DidGetMetadata, this));
236 } 182 }
237 183
238 void FileSystemURLRequestJob::DidGetMetadata( 184 void FileSystemURLRequestJob::DidGetMetadata(
185 base::PlatformFileError error_code,
239 const base::PlatformFileInfo& file_info, 186 const base::PlatformFileInfo& file_info,
240 const FilePath& platform_path) { 187 const FilePath& platform_path) {
188 if (error_code != base::PLATFORM_FILE_OK) {
189 NotifyFailed(error_code == base::PLATFORM_FILE_ERROR_INVALID_URL ?
190 net::ERR_INVALID_URL : net::ERR_FILE_NOT_FOUND);
191 return;
192 }
193
241 // We may have been orphaned... 194 // We may have been orphaned...
242 if (!request_) 195 if (!request_)
243 return; 196 return;
244 197
245 is_directory_ = file_info.is_directory; 198 is_directory_ = file_info.is_directory;
246 199
247 if (!byte_range_.ComputeBounds(file_info.size)) { 200 if (!byte_range_.ComputeBounds(file_info.size)) {
248 NotifyFailed(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE); 201 NotifyFailed(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE);
249 return; 202 return;
250 } 203 }
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
321 } 274 }
322 275
323 return false; 276 return false;
324 } 277 }
325 278
326 void FileSystemURLRequestJob::NotifyFailed(int rv) { 279 void FileSystemURLRequestJob::NotifyFailed(int rv) {
327 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv)); 280 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv));
328 } 281 }
329 282
330 } // namespace fileapi 283 } // namespace fileapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698