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

Side by Side Diff: content/browser/android/url_request_content_job.cc

Issue 739033003: Support content scheme uri for Chrome on Android (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: moving content:// protocol handler to content/browser/android Created 6 years 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
OLDNEW
(Empty)
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "content/browser/android/url_request_content_job.h"
6
7 #include "base/android/content_uri_utils.h"
8 #include "base/bind.h"
9 #include "base/files/file_util.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/task_runner.h"
12 #include "net/base/file_stream.h"
13 #include "net/base/io_buffer.h"
14 #include "net/base/net_errors.h"
15 #include "net/http/http_util.h"
16 #include "net/url_request/url_request_error_job.h"
17 #include "url/gurl.h"
18
19 namespace content {
20
21 // TODO(qinmin): Refactor this class to reuse the common code in
22 // url_request_file_job.cc.
23 URLRequestContentJob::ContentMetaInfo::ContentMetaInfo()
24 : content_exists(false),
25 content_size(0) {
26 }
27
28 URLRequestContentJob::URLRequestContentJob(
29 net::URLRequest* request,
30 net::NetworkDelegate* network_delegate,
31 const base::FilePath& content_path,
32 const scoped_refptr<base::TaskRunner>& content_task_runner)
33 : net::URLRequestJob(request, network_delegate),
34 content_path_(content_path),
35 stream_(new net::FileStream(content_task_runner)),
36 content_task_runner_(content_task_runner),
37 remaining_bytes_(0),
38 weak_ptr_factory_(this) {}
39
40 void URLRequestContentJob::Start() {
41 ContentMetaInfo* meta_info = new ContentMetaInfo();
42 content_task_runner_->PostTaskAndReply(
no sievers 2014/11/25 20:50:18 nit: Why do you want DidFetchMetaInfo to run as a
qinmin 2014/11/25 23:37:02 FetchMetaInfo() runs on the content_task_runner an
43 FROM_HERE,
44 base::Bind(&URLRequestContentJob::FetchMetaInfo, content_path_,
45 base::Unretained(meta_info)),
46 base::Bind(&URLRequestContentJob::DidFetchMetaInfo,
47 weak_ptr_factory_.GetWeakPtr(),
48 base::Owned(meta_info)));
49 }
50
51 void URLRequestContentJob::Kill() {
52 stream_.reset();
53 weak_ptr_factory_.InvalidateWeakPtrs();
54
55 net::URLRequestJob::Kill();
56 }
57
58 bool URLRequestContentJob::ReadRawData(net::IOBuffer* dest,
59 int dest_size,
60 int* bytes_read) {
61 DCHECK_NE(dest_size, 0);
no sievers 2014/11/25 20:50:18 DCHECK_GT?
qinmin 2014/11/25 23:37:02 Done.
62 DCHECK(bytes_read);
63 DCHECK_GE(remaining_bytes_, 0);
64
65 if (remaining_bytes_ < dest_size)
66 dest_size = static_cast<int>(remaining_bytes_);
67
68 // If we should copy zero bytes because |remaining_bytes_| is zero, short
69 // circuit here.
70 if (!dest_size) {
71 *bytes_read = 0;
72 return true;
73 }
74
75 int rv = stream_->Read(dest,
76 dest_size,
77 base::Bind(&URLRequestContentJob::DidRead,
78 weak_ptr_factory_.GetWeakPtr(),
79 make_scoped_refptr(dest)));
80 if (rv >= 0) {
81 // Data is immediately available.
82 *bytes_read = rv;
83 remaining_bytes_ -= rv;
84 DCHECK_GE(remaining_bytes_, 0);
no sievers 2014/11/25 20:50:18 I don't see this being implemented in FileStream.
qinmin 2014/11/25 23:37:02 Done.
85 return true;
86 }
87
88 // Otherwise, a read error occured. We may just need to wait...
89 if (rv == net::ERR_IO_PENDING) {
90 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0));
91 } else {
92 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED, rv));
93 }
94 return false;
95 }
96
97 bool URLRequestContentJob::IsRedirectResponse(GURL* location,
98 int* http_status_code) {
99 return false;
100 }
101
102 bool URLRequestContentJob::GetMimeType(std::string* mime_type) const {
103 DCHECK(request_);
104 if (!meta_info_.mime_type.empty()) {
105 *mime_type = meta_info_.mime_type;
106 return true;
107 }
108 return false;
109 }
110
111 void URLRequestContentJob::SetExtraRequestHeaders(
112 const net::HttpRequestHeaders& headers) {
113 std::string range_header;
114 if (!headers.GetHeader(net::HttpRequestHeaders::kRange, &range_header))
115 return;
116
117 // We only care about "Range" header here.
118 std::vector<net::HttpByteRange> ranges;
119 if (net::HttpUtil::ParseRangeHeader(range_header, &ranges)) {
120 if (ranges.size() == 1) {
121 byte_range_ = ranges[0];
122 } else {
123 // We don't support multiple range requests.
124 NotifyDone(net::URLRequestStatus(
125 net::URLRequestStatus::FAILED,
126 net::ERR_REQUEST_RANGE_NOT_SATISFIABLE));
127 }
128 }
129 }
130
131 URLRequestContentJob::~URLRequestContentJob() {}
132
133 void URLRequestContentJob::FetchMetaInfo(const base::FilePath& content_path,
134 ContentMetaInfo* meta_info) {
135 base::File::Info file_info;
136 meta_info->content_exists = base::GetFileInfo(content_path, &file_info);
137 if (meta_info->content_exists) {
138 meta_info->content_size = file_info.size;
139 meta_info->mime_type = base::GetContentUriMimeType(content_path);
140 }
141 }
142
143 void URLRequestContentJob::DidFetchMetaInfo(const ContentMetaInfo* meta_info) {
144 meta_info_ = *meta_info;
145
146 if (!meta_info_.content_exists) {
147 DidOpen(net::ERR_FILE_NOT_FOUND);
148 return;
149 }
150
151 int flags = base::File::FLAG_OPEN |
152 base::File::FLAG_READ |
153 base::File::FLAG_ASYNC;
154 int rv = stream_->Open(content_path_, flags,
155 base::Bind(&URLRequestContentJob::DidOpen,
156 weak_ptr_factory_.GetWeakPtr()));
157 if (rv != net::ERR_IO_PENDING)
158 DidOpen(rv);
159 }
160
161 void URLRequestContentJob::DidOpen(int result) {
162 if (result != net::OK) {
163 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED, result));
164 return;
165 }
166
167 if (!byte_range_.ComputeBounds(meta_info_.content_size)) {
168 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED,
169 net::ERR_REQUEST_RANGE_NOT_SATISFIABLE));
170 return;
171 }
172
173 remaining_bytes_ = byte_range_.last_byte_position() -
174 byte_range_.first_byte_position() + 1;
175 DCHECK_GE(remaining_bytes_, 0);
176
177 if (remaining_bytes_ > 0 && byte_range_.first_byte_position() != 0) {
178 int rv = stream_->Seek(base::File::FROM_BEGIN,
179 byte_range_.first_byte_position(),
180 base::Bind(&URLRequestContentJob::DidSeek,
181 weak_ptr_factory_.GetWeakPtr()));
182 if (rv != net::ERR_IO_PENDING) {
183 // stream_->Seek() failed, so pass an intentionally erroneous value
184 // into DidSeek().
185 DidSeek(-1);
186 }
187 } else {
188 // We didn't need to call stream_->Seek() at all, so we pass to DidSeek()
189 // the value that would mean seek success. This way we skip the code
190 // handling seek failure.
191 DidSeek(byte_range_.first_byte_position());
192 }
193 }
194
195 void URLRequestContentJob::DidSeek(int64 result) {
196 if (result != byte_range_.first_byte_position()) {
197 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED,
198 net::ERR_REQUEST_RANGE_NOT_SATISFIABLE));
199 return;
200 }
201
202 set_expected_content_size(remaining_bytes_);
203 NotifyHeadersComplete();
204 }
205
206 void URLRequestContentJob::DidRead(
207 scoped_refptr<net::IOBuffer> buf, int result) {
208 if (result > 0) {
209 SetStatus(net::URLRequestStatus()); // Clear the IO_PENDING status
210 remaining_bytes_ -= result;
211 DCHECK_GE(remaining_bytes_, 0);
212 }
213
214 buf = NULL;
no sievers 2014/11/25 20:50:18 Either remove this or put a comment why this needs
qinmin 2014/11/25 23:37:02 removed.
215
216 if (result == 0) {
no sievers 2014/11/25 20:50:18 why not 'if (result >= 0)'?
qinmin 2014/11/25 23:37:02 all errors have negative value, and non-failure ca
no sievers 2014/11/25 23:51:52 But above you are handling |result > 0|.
qinmin 2014/11/26 00:56:46 ah... if resule>=0, the return value is the size r
no sievers 2014/11/26 01:09:49 So DidRead() gets called twice, once with result =
qinmin 2014/11/26 01:38:42 The actual implementation should be in file_stream
no sievers 2014/11/26 01:46:08 Ok as long as it works fine and we end up calling
217 NotifyDone(net::URLRequestStatus());
218 } else if (result < 0) {
219 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED, result));
220 }
221
222 NotifyReadComplete(result);
no sievers 2014/11/25 20:50:18 Does the order of NotifyDone and NotifyReadComplet
qinmin 2014/11/25 23:37:02 I think the ordering shouldn't matter as NotifyDon
223 }
224
225 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698