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

Side by Side Diff: chrome/browser/chromeos/drive/drive_url_request_job.cc

Issue 560313002: Use ExternalFileSystemBackend in the DriveURLRequestJob. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix test. Created 6 years, 3 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 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 "chrome/browser/chromeos/drive/drive_url_request_job.h" 5 #include "chrome/browser/chromeos/drive/drive_url_request_job.h"
6 6
7 #include <algorithm>
7 #include <string> 8 #include <string>
9 #include <vector>
8 10
9 #include "base/bind.h" 11 #include "base/bind.h"
10 #include "base/logging.h" 12 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/ref_counted.h"
12 #include "chrome/browser/chromeos/drive/drive.pb.h" 14 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/chromeos/drive/drive_file_stream_reader.h"
14 #include "chrome/browser/chromeos/drive/file_system_interface.h"
15 #include "chrome/browser/chromeos/drive/file_system_util.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"
16 #include "content/public/browser/browser_thread.h" 19 #include "content/public/browser/browser_thread.h"
20 #include "content/public/browser/storage_partition.h"
17 #include "net/base/net_errors.h" 21 #include "net/base/net_errors.h"
18 #include "net/http/http_byte_range.h" 22 #include "net/http/http_byte_range.h"
19 #include "net/http/http_request_headers.h" 23 #include "net/http/http_request_headers.h"
20 #include "net/http/http_response_info.h" 24 #include "net/http/http_response_info.h"
21 #include "net/http/http_util.h" 25 #include "net/http/http_util.h"
22 #include "net/url_request/url_request.h" 26 #include "net/url_request/url_request.h"
23 #include "net/url_request/url_request_status.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"
24 31
25 using content::BrowserThread; 32 using content::BrowserThread;
26 33
27 namespace drive { 34 namespace drive {
28 namespace { 35 namespace {
29 36
30 struct MimeTypeReplacement { 37 struct MimeTypeReplacement {
31 const char* original_type; 38 const char* original_type;
32 const char* new_type; 39 const char* new_type;
33 }; 40 };
34 41
35 const MimeTypeReplacement kMimeTypeReplacements[] = { 42 const MimeTypeReplacement kMimeTypeReplacements[] = {
36 {"message/rfc822", "multipart/related"} // Fixes MHTML 43 {"message/rfc822", "multipart/related"} // Fixes MHTML
37 }; 44 };
38 45
39 std::string FixupMimeType(const std::string& type) { 46 // Check if the |url| points a valid location or not.
40 for (size_t i = 0; i < arraysize(kMimeTypeReplacements); i++) { 47 bool IsValidURL(const storage::FileSystemURL& url) {
41 if (type == kMimeTypeReplacements[i].original_type) 48 switch (url.type()) {
42 return kMimeTypeReplacements[i].new_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;
43 } 61 }
44 return type;
45 } 62 }
46 63
64 // Delegate for obtaining FileSystemContext, FileSystemURL, and mime type on
mtomasz 2014/09/19 00:43:49 I think this is not a delegate. It's more like a h
hirono 2014/09/19 03:46:52 I prefer URLHepler (Parser does not sound to acces
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,
mtomasz 2014/09/19 00:43:48 Is it safe for shutdown? During shutdown the callb
hirono 2014/09/19 03:46:52 Let me do a safer way. Done.
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
47 } // namespace 170 } // namespace
48 171
49 DriveURLRequestJob::DriveURLRequestJob( 172 DriveURLRequestJob::DriveURLRequestJob(void* profile_id,
50 const FileSystemGetter& file_system_getter, 173 net::URLRequest* request,
51 base::SequencedTaskRunner* file_task_runner, 174 net::NetworkDelegate* network_delegate)
52 net::URLRequest* request,
53 net::NetworkDelegate* network_delegate)
54 : net::URLRequestJob(request, network_delegate), 175 : net::URLRequestJob(request, network_delegate),
55 file_system_getter_(file_system_getter), 176 profile_id_(profile_id),
56 file_task_runner_(file_task_runner), 177 remaining_bytes_(0),
57 weak_ptr_factory_(this) { 178 weak_ptr_factory_(this) {
58 } 179 }
59 180
60 void DriveURLRequestJob::SetExtraRequestHeaders( 181 void DriveURLRequestJob::SetExtraRequestHeaders(
61 const net::HttpRequestHeaders& headers) { 182 const net::HttpRequestHeaders& headers) {
62 std::string range_header; 183 std::string range_header;
63 if (headers.GetHeader(net::HttpRequestHeaders::kRange, &range_header)) { 184 if (headers.GetHeader(net::HttpRequestHeaders::kRange, &range_header)) {
64 // Note: We only support single range requests. 185 // Note: We only support single range requests.
65 std::vector<net::HttpByteRange> ranges; 186 std::vector<net::HttpByteRange> ranges;
66 if (net::HttpUtil::ParseRangeHeader(range_header, &ranges) && 187 if (net::HttpUtil::ParseRangeHeader(range_header, &ranges) &&
67 ranges.size() == 1) { 188 ranges.size() == 1) {
68 byte_range_ = ranges[0]; 189 byte_range_ = ranges[0];
69 } else { 190 } else {
70 // Failed to parse Range: header, so notify the error. 191 // Failed to parse Range: header, so notify the error.
71 NotifyDone( 192 NotifyDone(
72 net::URLRequestStatus(net::URLRequestStatus::FAILED, 193 net::URLRequestStatus(net::URLRequestStatus::FAILED,
73 net::ERR_REQUEST_RANGE_NOT_SATISFIABLE)); 194 net::ERR_REQUEST_RANGE_NOT_SATISFIABLE));
74 } 195 }
75 } 196 }
76 } 197 }
77 198
78 void DriveURLRequestJob::Start() { 199 void DriveURLRequestJob::Start() {
79 DVLOG(1) << "Starting request"; 200 DVLOG(1) << "Starting request";
80 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 201 DCHECK_CURRENTLY_ON(BrowserThread::IO);
81 DCHECK(!stream_reader_); 202 DCHECK(!stream_reader_);
82 203
83 // We only support GET request. 204 // We only support GET request.
84 if (request()->method() != "GET") { 205 if (request()->method() != "GET") {
85 LOG(WARNING) << "Failed to start request: " 206 LOG(WARNING) << "Failed to start request: "
86 << request()->method() << " method is not supported"; 207 << request()->method() << " method is not supported";
87 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, 208 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED,
88 net::ERR_METHOD_NOT_SUPPORTED)); 209 net::ERR_METHOD_NOT_SUPPORTED));
89 return; 210 return;
90 } 211 }
91 212
92 base::FilePath drive_file_path(util::DriveURLToFilePath(request_->url())); 213 // Check if the scheme is correct.
93 if (drive_file_path.empty()) { 214 if (!request()->url().SchemeIs(chrome::kDriveScheme)) {
94 // Not a valid url.
95 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, 215 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED,
96 net::ERR_INVALID_URL)); 216 net::ERR_INVALID_URL));
97 return; 217 return;
98 } 218 }
99 219
100 // Initialize the stream reader. 220 // Owned by itself.
101 stream_reader_.reset( 221 new GetFileSystemContextDelegate(
102 new DriveFileStreamReader(file_system_getter_, file_task_runner_.get())); 222 profile_id_,
103 stream_reader_->Initialize( 223 request()->url(),
104 drive_file_path, 224 base::Bind(&DriveURLRequestJob::OnDelegateResultObtained,
105 byte_range_,
106 base::Bind(&DriveURLRequestJob::OnDriveFileStreamReaderInitialized,
107 weak_ptr_factory_.GetWeakPtr())); 225 weak_ptr_factory_.GetWeakPtr()));
108 } 226 }
109 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
110 void DriveURLRequestJob::Kill() { 307 void DriveURLRequestJob::Kill() {
111 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 308 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
112 309
113 stream_reader_.reset(); 310 stream_reader_.reset();
311 file_system_context_ = NULL;
114 net::URLRequestJob::Kill(); 312 net::URLRequestJob::Kill();
115 weak_ptr_factory_.InvalidateWeakPtrs(); 313 weak_ptr_factory_.InvalidateWeakPtrs();
116 } 314 }
117 315
118 bool DriveURLRequestJob::GetMimeType(std::string* mime_type) const { 316 bool DriveURLRequestJob::GetMimeType(std::string* mime_type) const {
119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 317 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
120 318 mime_type->assign(mime_type_);
121 if (!entry_) {
122 return false;
123 }
124
125 mime_type->assign(
126 FixupMimeType(entry_->file_specific_info().content_mime_type()));
127 return !mime_type->empty(); 319 return !mime_type->empty();
128 } 320 }
129 321
130 bool DriveURLRequestJob::IsRedirectResponse( 322 bool DriveURLRequestJob::IsRedirectResponse(
131 GURL* location, int* http_status_code) { 323 GURL* location, int* http_status_code) {
132 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 324 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
133 325 if (redirect_url_.is_empty())
134 if (!entry_ || !entry_->file_specific_info().is_hosted_document()) {
135 return false; 326 return false;
136 }
137 327
138 // Redirect a hosted document. 328 // Redirect a hosted document.
139 *location = GURL(entry_->file_specific_info().alternate_url()); 329 *location = redirect_url_;
140 const int kHttpFound = 302; 330 const int kHttpFound = 302;
141 *http_status_code = kHttpFound; 331 *http_status_code = kHttpFound;
142 return true; 332 return true;
143 } 333 }
144 334
145 bool DriveURLRequestJob::ReadRawData( 335 bool DriveURLRequestJob::ReadRawData(
146 net::IOBuffer* buf, int buf_size, int* bytes_read) { 336 net::IOBuffer* buf, int buf_size, int* bytes_read) {
147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 337 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
148 DCHECK(stream_reader_ && stream_reader_->IsInitialized()); 338 DCHECK(stream_reader_);
149 339
150 int result = stream_reader_->Read( 340 if (remaining_bytes_ == 0) {
151 buf, buf_size, 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_),
152 base::Bind(&DriveURLRequestJob::OnReadCompleted, 348 base::Bind(&DriveURLRequestJob::OnReadCompleted,
153 weak_ptr_factory_.GetWeakPtr())); 349 weak_ptr_factory_.GetWeakPtr()));
154 350
155 if (result == net::ERR_IO_PENDING) { 351 if (result == net::ERR_IO_PENDING) {
156 // The data is not yet available. 352 // The data is not yet available.
157 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); 353 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0));
158 return false; 354 return false;
159 } 355 }
160 if (result < 0) { 356 if (result < 0) {
161 // An error occurs. 357 // An error occurs.
162 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED, result)); 358 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED, result));
163 return false; 359 return false;
164 } 360 }
165 361
166 // Reading has been finished immediately. 362 // Reading has been finished immediately.
167 *bytes_read = result; 363 *bytes_read = result;
364 remaining_bytes_ -= result;
168 return true; 365 return true;
169 } 366 }
170 367
171 DriveURLRequestJob::~DriveURLRequestJob() { 368 DriveURLRequestJob::~DriveURLRequestJob() {
172 } 369 }
173 370
174 void DriveURLRequestJob::OnDriveFileStreamReaderInitialized(
175 int error, scoped_ptr<ResourceEntry> entry) {
176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
177 DCHECK(stream_reader_);
178
179 if (error != FILE_ERROR_OK) {
180 NotifyStartError(
181 net::URLRequestStatus(net::URLRequestStatus::FAILED, error));
182 return;
183 }
184
185 DCHECK(entry && entry->has_file_specific_info());
186 entry_ = entry.Pass();
187
188 if (!entry_->file_specific_info().is_hosted_document()) {
189 // We don't need to set content size for hosted documents,
190 // because it will be redirected.
191 set_expected_content_size(entry_->file_info().size());
192 }
193
194 NotifyHeadersComplete();
195 }
196
197 void DriveURLRequestJob::OnReadCompleted(int read_result) { 371 void DriveURLRequestJob::OnReadCompleted(int read_result) {
198 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 372 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
199 373
200 if (read_result < 0) { 374 if (read_result < 0) {
201 DCHECK_NE(read_result, net::ERR_IO_PENDING); 375 DCHECK_NE(read_result, net::ERR_IO_PENDING);
202 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED, 376 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED,
203 read_result)); 377 read_result));
204 } 378 }
205 379
380 remaining_bytes_ -= read_result;
206 SetStatus(net::URLRequestStatus()); // Clear the IO_PENDING status. 381 SetStatus(net::URLRequestStatus()); // Clear the IO_PENDING status.
207 NotifyReadComplete(read_result); 382 NotifyReadComplete(read_result);
208 } 383 }
209 384
210 } // namespace drive 385 } // namespace drive
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698