OLD | NEW |
| (Empty) |
1 // Copyright 2013 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 "chrome/browser/chromeos/drive/drive_url_request_job.h" | |
6 | |
7 #include <algorithm> | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/bind.h" | |
12 #include "base/logging.h" | |
13 #include "base/memory/ref_counted.h" | |
14 #include "chrome/browser/browser_process.h" | |
15 #include "chrome/browser/chromeos/drive/file_system_util.h" | |
16 #include "chrome/browser/extensions/api/file_handlers/mime_util.h" | |
17 #include "chrome/browser/profiles/profile_manager.h" | |
18 #include "chrome/common/url_constants.h" | |
19 #include "content/public/browser/browser_thread.h" | |
20 #include "content/public/browser/storage_partition.h" | |
21 #include "net/base/net_errors.h" | |
22 #include "net/http/http_byte_range.h" | |
23 #include "net/http/http_request_headers.h" | |
24 #include "net/http/http_response_info.h" | |
25 #include "net/http/http_util.h" | |
26 #include "net/url_request/url_request.h" | |
27 #include "net/url_request/url_request_status.h" | |
28 #include "storage/browser/fileapi/file_system_backend.h" | |
29 #include "storage/browser/fileapi/file_system_context.h" | |
30 #include "storage/browser/fileapi/file_system_operation_runner.h" | |
31 | |
32 using content::BrowserThread; | |
33 | |
34 namespace drive { | |
35 namespace { | |
36 | |
37 struct MimeTypeReplacement { | |
38 const char* original_type; | |
39 const char* new_type; | |
40 }; | |
41 | |
42 const MimeTypeReplacement kMimeTypeReplacements[] = { | |
43 {"message/rfc822", "multipart/related"} // Fixes MHTML | |
44 }; | |
45 | |
46 // Check if the |url| points a valid location or not. | |
47 bool IsValidURL(const storage::FileSystemURL& url) { | |
48 switch (url.type()) { | |
49 case storage::kFileSystemTypeDrive: { | |
50 const base::FilePath my_drive_path = util::GetDriveMyDriveRootPath(); | |
51 const base::FilePath drive_other_path = | |
52 util::GetDriveGrandRootPath().Append(util::kDriveOtherDirName); | |
53 const base::FilePath url_drive_path = | |
54 util::ExtractDrivePathFromFileSystemUrl(url); | |
55 return my_drive_path == url_drive_path || | |
56 my_drive_path.IsParent(url_drive_path) || | |
57 drive_other_path.IsParent(url_drive_path); | |
58 } | |
59 default: | |
60 return false; | |
61 } | |
62 } | |
63 | |
64 // Delegate for obtaining FileSystemContext, FileSystemURL, and mime type on | |
65 // the UI thread. | |
66 class GetFileSystemContextDelegate { | |
67 public: | |
68 GetFileSystemContextDelegate( | |
69 void* profile_id, | |
70 const GURL& url, | |
71 const DriveURLRequestJob::DelegateCallback& callback) | |
72 : profile_id_(profile_id), url_(url), callback_(callback) { | |
73 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
74 BrowserThread::PostTask( | |
75 BrowserThread::UI, | |
76 FROM_HERE, | |
77 base::Bind(&GetFileSystemContextDelegate::RunOnUIThread, | |
78 base::Unretained(this))); | |
79 } | |
80 | |
81 private: | |
82 void RunOnUIThread() { | |
83 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
84 Profile* const profile = reinterpret_cast<Profile*>(profile_id_); | |
85 if (!g_browser_process->profile_manager()->IsValidProfile(profile)) { | |
86 ReplyResult(net::ERR_FAILED); | |
87 return; | |
88 } | |
89 content::StoragePartition* const storage = | |
90 content::BrowserContext::GetStoragePartitionForSite(profile, url_); | |
91 DCHECK(storage); | |
92 | |
93 scoped_refptr<storage::FileSystemContext> context = | |
94 storage->GetFileSystemContext(); | |
95 DCHECK(context.get()); | |
96 | |
97 // Obtain the absolute path in the file system. | |
98 base::FilePath path = drive::util::GetDriveMountPointPath(profile); | |
99 drive::util::GetDriveGrandRootPath().AppendRelativePath( | |
100 util::DriveURLToFilePath(url_), &path); | |
101 | |
102 storage::ExternalFileSystemBackend* const backend = | |
103 context->external_backend(); | |
104 DCHECK(backend); | |
105 | |
106 // Obtain the virtual path. | |
107 base::FilePath virtual_path; | |
108 if (!backend->GetVirtualPath(path, &virtual_path)) { | |
109 ReplyResult(net::ERR_FILE_NOT_FOUND); | |
110 return; | |
111 } | |
112 | |
113 // Obtain the file system URL. | |
114 // TODO(hirono): After removing MHTML support, stop to use the special | |
115 // drive: scheme and use filesystem: URL directly. crbug.com/415455 | |
116 file_system_url_ = context->CreateCrackedFileSystemURL( | |
117 GURL(std::string(chrome::kDriveScheme) + ":"), | |
118 storage::kFileSystemTypeExternal, | |
119 virtual_path); | |
120 if (!IsValidURL(file_system_url_)) { | |
121 ReplyResult(net::ERR_INVALID_URL); | |
122 return; | |
123 } | |
124 | |
125 file_system_context_ = context; | |
126 | |
127 extensions::app_file_handler_util::GetMimeTypeForLocalPath( | |
128 profile, | |
129 file_system_url_.path(), | |
130 base::Bind(&GetFileSystemContextDelegate::OnGotMimeTypeOnUIThread, | |
131 base::Unretained(this))); | |
132 } | |
133 | |
134 void OnGotMimeTypeOnUIThread(const std::string& mime_type) { | |
135 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
136 mime_type_ = mime_type; | |
137 for (size_t i = 0; i < arraysize(kMimeTypeReplacements); i++) { | |
138 if (mime_type_ == kMimeTypeReplacements[i].original_type) { | |
139 mime_type_ = kMimeTypeReplacements[i].new_type; | |
140 break; | |
141 } | |
142 } | |
143 ReplyResult(net::OK); | |
144 } | |
145 | |
146 void ReplyResult(net::Error error) { | |
147 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
148 | |
149 BrowserThread::PostTask( | |
150 BrowserThread::IO, | |
151 FROM_HERE, | |
152 base::Bind(callback_, | |
153 error, | |
154 file_system_context_, | |
155 file_system_url_, | |
156 mime_type_)); | |
157 delete this; | |
158 } | |
159 | |
160 void* const profile_id_; | |
161 const GURL url_; | |
162 const DriveURLRequestJob::DelegateCallback callback_; | |
163 scoped_refptr<storage::FileSystemContext> file_system_context_; | |
164 storage::FileSystemURL file_system_url_; | |
165 std::string mime_type_; | |
166 | |
167 DISALLOW_COPY_AND_ASSIGN(GetFileSystemContextDelegate); | |
168 }; | |
169 | |
170 } // namespace | |
171 | |
172 DriveURLRequestJob::DriveURLRequestJob(void* profile_id, | |
173 net::URLRequest* request, | |
174 net::NetworkDelegate* network_delegate) | |
175 : net::URLRequestJob(request, network_delegate), | |
176 profile_id_(profile_id), | |
177 remaining_bytes_(0), | |
178 weak_ptr_factory_(this) { | |
179 } | |
180 | |
181 void DriveURLRequestJob::SetExtraRequestHeaders( | |
182 const net::HttpRequestHeaders& headers) { | |
183 std::string range_header; | |
184 if (headers.GetHeader(net::HttpRequestHeaders::kRange, &range_header)) { | |
185 // Note: We only support single range requests. | |
186 std::vector<net::HttpByteRange> ranges; | |
187 if (net::HttpUtil::ParseRangeHeader(range_header, &ranges) && | |
188 ranges.size() == 1) { | |
189 byte_range_ = ranges[0]; | |
190 } else { | |
191 // Failed to parse Range: header, so notify the error. | |
192 NotifyDone( | |
193 net::URLRequestStatus(net::URLRequestStatus::FAILED, | |
194 net::ERR_REQUEST_RANGE_NOT_SATISFIABLE)); | |
195 } | |
196 } | |
197 } | |
198 | |
199 void DriveURLRequestJob::Start() { | |
200 DVLOG(1) << "Starting request"; | |
201 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
202 DCHECK(!stream_reader_); | |
203 | |
204 // We only support GET request. | |
205 if (request()->method() != "GET") { | |
206 LOG(WARNING) << "Failed to start request: " | |
207 << request()->method() << " method is not supported"; | |
208 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, | |
209 net::ERR_METHOD_NOT_SUPPORTED)); | |
210 return; | |
211 } | |
212 | |
213 // Check if the scheme is correct. | |
214 if (!request()->url().SchemeIs(chrome::kDriveScheme)) { | |
215 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, | |
216 net::ERR_INVALID_URL)); | |
217 return; | |
218 } | |
219 | |
220 // Owned by itself. | |
221 new GetFileSystemContextDelegate( | |
222 profile_id_, | |
223 request()->url(), | |
224 base::Bind(&DriveURLRequestJob::OnDelegateResultObtained, | |
225 weak_ptr_factory_.GetWeakPtr())); | |
226 } | |
227 | |
228 void DriveURLRequestJob::OnDelegateResultObtained( | |
229 net::Error error, | |
230 const scoped_refptr<storage::FileSystemContext>& file_system_context, | |
231 const storage::FileSystemURL& file_system_url, | |
232 const std::string& mime_type) { | |
233 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
234 | |
235 if (error != net::OK) { | |
236 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, | |
237 error)); | |
238 return; | |
239 } | |
240 | |
241 DCHECK(file_system_context.get()); | |
242 file_system_context_ = file_system_context; | |
243 file_system_url_ = file_system_url; | |
244 mime_type_ = mime_type; | |
245 | |
246 // Check if the entry has a redirect URL. | |
247 file_system_context_->external_backend()->GetRedirectURLForContents( | |
248 file_system_url_, | |
249 base::Bind(&DriveURLRequestJob::OnRedirectURLObtained, | |
250 weak_ptr_factory_.GetWeakPtr())); | |
251 } | |
252 | |
253 void DriveURLRequestJob::OnRedirectURLObtained(const GURL& redirect_url) { | |
254 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
255 redirect_url_ = redirect_url; | |
256 if (!redirect_url_.is_empty()) { | |
257 NotifyHeadersComplete(); | |
258 return; | |
259 } | |
260 | |
261 // Obtain file system context. | |
262 file_system_context_->operation_runner()->GetMetadata( | |
263 file_system_url_, | |
264 base::Bind(&DriveURLRequestJob::OnFileInfoObtained, | |
265 weak_ptr_factory_.GetWeakPtr())); | |
266 } | |
267 | |
268 void DriveURLRequestJob::OnFileInfoObtained(base::File::Error result, | |
269 const base::File::Info& file_info) { | |
270 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
271 | |
272 if (result == base::File::FILE_ERROR_NOT_FOUND) { | |
273 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, | |
274 net::ERR_FILE_NOT_FOUND)); | |
275 return; | |
276 } | |
277 | |
278 if (result != base::File::FILE_OK || file_info.is_directory) { | |
279 NotifyStartError( | |
280 net::URLRequestStatus(net::URLRequestStatus::FAILED, net::ERR_FAILED)); | |
281 return; | |
282 } | |
283 | |
284 // Compute content size. | |
285 int64 offset = 0; | |
286 int64 size = storage::kMaximumLength; | |
287 if (byte_range_.ComputeBounds(file_info.size)) { | |
288 offset = byte_range_.first_byte_position(); | |
289 size = byte_range_.last_byte_position() + 1 - | |
290 byte_range_.first_byte_position(); | |
291 set_expected_content_size(size); | |
292 } | |
293 remaining_bytes_ = size; | |
294 | |
295 // Create file stream reader. | |
296 stream_reader_ = file_system_context_->CreateFileStreamReader( | |
297 file_system_url_, offset, size, base::Time()); | |
298 if (!stream_reader_) { | |
299 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, | |
300 net::ERR_FILE_NOT_FOUND)); | |
301 return; | |
302 } | |
303 | |
304 NotifyHeadersComplete(); | |
305 } | |
306 | |
307 void DriveURLRequestJob::Kill() { | |
308 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
309 | |
310 stream_reader_.reset(); | |
311 file_system_context_ = NULL; | |
312 net::URLRequestJob::Kill(); | |
313 weak_ptr_factory_.InvalidateWeakPtrs(); | |
314 } | |
315 | |
316 bool DriveURLRequestJob::GetMimeType(std::string* mime_type) const { | |
317 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
318 mime_type->assign(mime_type_); | |
319 return !mime_type->empty(); | |
320 } | |
321 | |
322 bool DriveURLRequestJob::IsRedirectResponse( | |
323 GURL* location, int* http_status_code) { | |
324 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
325 if (redirect_url_.is_empty()) | |
326 return false; | |
327 | |
328 // Redirect a hosted document. | |
329 *location = redirect_url_; | |
330 const int kHttpFound = 302; | |
331 *http_status_code = kHttpFound; | |
332 return true; | |
333 } | |
334 | |
335 bool DriveURLRequestJob::ReadRawData( | |
336 net::IOBuffer* buf, int buf_size, int* bytes_read) { | |
337 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
338 DCHECK(stream_reader_); | |
339 | |
340 if (remaining_bytes_ == 0) { | |
341 *bytes_read = 0; | |
342 return true; | |
343 } | |
344 | |
345 const int result = stream_reader_->Read( | |
346 buf, | |
347 std::min(static_cast<int64>(buf_size), remaining_bytes_), | |
348 base::Bind(&DriveURLRequestJob::OnReadCompleted, | |
349 weak_ptr_factory_.GetWeakPtr())); | |
350 | |
351 if (result == net::ERR_IO_PENDING) { | |
352 // The data is not yet available. | |
353 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); | |
354 return false; | |
355 } | |
356 if (result < 0) { | |
357 // An error occurs. | |
358 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED, result)); | |
359 return false; | |
360 } | |
361 | |
362 // Reading has been finished immediately. | |
363 *bytes_read = result; | |
364 remaining_bytes_ -= result; | |
365 return true; | |
366 } | |
367 | |
368 DriveURLRequestJob::~DriveURLRequestJob() { | |
369 } | |
370 | |
371 void DriveURLRequestJob::OnReadCompleted(int read_result) { | |
372 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
373 | |
374 if (read_result < 0) { | |
375 DCHECK_NE(read_result, net::ERR_IO_PENDING); | |
376 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED, | |
377 read_result)); | |
378 } | |
379 | |
380 remaining_bytes_ -= read_result; | |
381 SetStatus(net::URLRequestStatus()); // Clear the IO_PENDING status. | |
382 NotifyReadComplete(read_result); | |
383 } | |
384 | |
385 } // namespace drive | |
OLD | NEW |