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

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

Powered by Google App Engine
This is Rietveld 408576698