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

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

Powered by Google App Engine
This is Rietveld 408576698