Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 <string> |
| 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 |
| 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/profiles/profile_manager.h" | |
| 16 #include "chrome/common/url_constants.h" | |
| 16 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/browser/storage_partition.h" | |
| 17 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
| 18 #include "net/http/http_byte_range.h" | 20 #include "net/http/http_byte_range.h" |
| 19 #include "net/http/http_request_headers.h" | 21 #include "net/http/http_request_headers.h" |
| 20 #include "net/http/http_response_info.h" | 22 #include "net/http/http_response_info.h" |
| 21 #include "net/http/http_util.h" | 23 #include "net/http/http_util.h" |
| 22 #include "net/url_request/url_request.h" | 24 #include "net/url_request/url_request.h" |
| 23 #include "net/url_request/url_request_status.h" | 25 #include "net/url_request/url_request_status.h" |
| 26 #include "storage/browser/fileapi/file_system_backend.h" | |
| 27 #include "storage/browser/fileapi/file_system_context.h" | |
| 24 | 28 |
| 25 using content::BrowserThread; | 29 using content::BrowserThread; |
| 26 | 30 |
| 27 namespace drive { | 31 namespace drive { |
| 28 namespace { | 32 namespace { |
| 29 | 33 // Obtains Profile from |profile_id|. Returns NULL on failure. |
|
mtomasz
2014/09/18 07:21:06
nit: How about \n after namespace { and before clo
hirono
2014/09/18 08:18:55
Done.
| |
| 30 struct MimeTypeReplacement { | 34 Profile* GetProfile(void* profile_id) { |
| 31 const char* original_type; | 35 // |profile_id| needs to be checked with ProfileManager::IsValidProfile |
| 32 const char* new_type; | 36 // before using it. |
| 33 }; | 37 Profile* const profile = reinterpret_cast<Profile*>(profile_id); |
| 34 | 38 if (!g_browser_process->profile_manager()->IsValidProfile(profile)) |
|
kinaba
2014/09/16 06:46:52
As far as I understand, this check and the convers
hirono
2014/09/17 16:25:53
Done.
| |
| 35 const MimeTypeReplacement kMimeTypeReplacements[] = { | 39 return NULL; |
| 36 {"message/rfc822", "multipart/related"} // Fixes MHTML | 40 return profile; |
| 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 } | 41 } |
| 46 | 42 |
| 43 // Obtains file system context from |profile|. | |
| 44 storage::FileSystemContext* GetFileSystemContext(Profile* profile) { | |
| 45 content::StoragePartition* const storage = | |
| 46 content::BrowserContext::GetDefaultStoragePartition(profile); | |
|
kinaba
2014/09/16 06:46:52
GetDefaultStoragePartition() is marked as "DON't U
hirono
2014/09/17 16:25:53
Done.
| |
| 47 DCHECK(storage); | |
| 48 storage::FileSystemContext* const context = storage->GetFileSystemContext(); | |
| 49 DCHECK(context); | |
| 50 return context; | |
| 51 } | |
| 47 } // namespace | 52 } // namespace |
| 48 | 53 |
| 49 DriveURLRequestJob::DriveURLRequestJob( | 54 DriveURLRequestJob::DriveURLRequestJob( |
| 50 const FileSystemGetter& file_system_getter, | 55 void* profile_id, |
| 51 base::SequencedTaskRunner* file_task_runner, | |
| 52 net::URLRequest* request, | 56 net::URLRequest* request, |
| 53 net::NetworkDelegate* network_delegate) | 57 net::NetworkDelegate* network_delegate) |
| 54 : net::URLRequestJob(request, network_delegate), | 58 : net::URLRequestJob(request, network_delegate), |
| 55 file_system_getter_(file_system_getter), | 59 profile_id_(profile_id), |
| 56 file_task_runner_(file_task_runner), | |
| 57 weak_ptr_factory_(this) { | 60 weak_ptr_factory_(this) { |
| 58 } | 61 } |
| 59 | 62 |
| 60 void DriveURLRequestJob::SetExtraRequestHeaders( | 63 void DriveURLRequestJob::SetExtraRequestHeaders( |
| 61 const net::HttpRequestHeaders& headers) { | 64 const net::HttpRequestHeaders& headers) { |
| 62 std::string range_header; | 65 std::string range_header; |
| 63 if (headers.GetHeader(net::HttpRequestHeaders::kRange, &range_header)) { | 66 if (headers.GetHeader(net::HttpRequestHeaders::kRange, &range_header)) { |
| 64 // Note: We only support single range requests. | 67 // Note: We only support single range requests. |
| 65 std::vector<net::HttpByteRange> ranges; | 68 std::vector<net::HttpByteRange> ranges; |
| 66 if (net::HttpUtil::ParseRangeHeader(range_header, &ranges) && | 69 if (net::HttpUtil::ParseRangeHeader(range_header, &ranges) && |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 82 | 85 |
| 83 // We only support GET request. | 86 // We only support GET request. |
| 84 if (request()->method() != "GET") { | 87 if (request()->method() != "GET") { |
| 85 LOG(WARNING) << "Failed to start request: " | 88 LOG(WARNING) << "Failed to start request: " |
| 86 << request()->method() << " method is not supported"; | 89 << request()->method() << " method is not supported"; |
| 87 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, | 90 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
| 88 net::ERR_METHOD_NOT_SUPPORTED)); | 91 net::ERR_METHOD_NOT_SUPPORTED)); |
| 89 return; | 92 return; |
| 90 } | 93 } |
| 91 | 94 |
| 92 base::FilePath drive_file_path(util::DriveURLToFilePath(request_->url())); | 95 // Obtain profile. |
| 93 if (drive_file_path.empty()) { | 96 Profile* const profile = GetProfile(profile_id_); |
| 94 // Not a valid url. | 97 if (!profile) { |
| 98 NotifyStartError( | |
| 99 net::URLRequestStatus(net::URLRequestStatus::FAILED, net::ERR_FAILED)); | |
| 100 return; | |
| 101 } | |
| 102 | |
| 103 // Obtain file system context. | |
| 104 storage::FileSystemContext* const context = GetFileSystemContext(profile); | |
| 105 DCHECK(context); | |
| 106 | |
| 107 if (!request()->url().SchemeIs(chrome::kDriveScheme)) { | |
| 95 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, | 108 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
| 96 net::ERR_INVALID_URL)); | 109 net::ERR_INVALID_URL)); |
| 97 return; | 110 return; |
| 98 } | 111 } |
| 99 | 112 |
| 100 // Initialize the stream reader. | 113 storage::ExternalFileSystemBackend* const backend = |
| 101 stream_reader_.reset( | 114 context->external_backend(); |
| 102 new DriveFileStreamReader(file_system_getter_, file_task_runner_.get())); | 115 DCHECK(backend); |
| 103 stream_reader_->Initialize( | 116 |
| 104 drive_file_path, | 117 // Obtain the absolute path in the file system. |
| 105 byte_range_, | 118 base::FilePath path = drive::util::GetDriveMountPointPath(profile); |
| 106 base::Bind(&DriveURLRequestJob::OnDriveFileStreamReaderInitialized, | 119 drive::util::GetDriveGrandRootPath().AppendRelativePath( |
| 120 util::DriveURLToFilePath(request()->url()), &path); | |
| 121 | |
| 122 // Obtain the virtual path. | |
| 123 base::FilePath virtual_path; | |
| 124 if (!backend->GetVirtualPath(path, &virtual_path)) { | |
| 125 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, | |
| 126 net::ERR_FILE_NOT_FOUND)); | |
| 127 return; | |
| 128 } | |
| 129 | |
| 130 // Obtain the file system URL. | |
| 131 file_system_url_ = context->CreateCrackedFileSystemURL( | |
| 132 GURL(std::string(chrome::kDriveScheme) + ":"), | |
| 133 storage::kFileSystemTypeExternal, | |
| 134 virtual_path); | |
| 135 | |
| 136 // Prepare offset. | |
| 137 int64 offset = byte_range_.first_byte_position(); | |
| 138 if (offset < 0) | |
| 139 offset = 0; | |
| 140 | |
| 141 // Create file stream reader. | |
| 142 stream_reader_ = | |
| 143 context->CreateFileStreamReader(file_system_url_, offset, base::Time()); | |
| 144 if (!stream_reader_) { | |
| 145 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, | |
| 146 net::ERR_FILE_NOT_FOUND)); | |
| 147 return; | |
| 148 } | |
| 149 | |
| 150 // Detect mime type from the extension. | |
|
kinaba
2014/09/16 06:46:52
c/b/cros/fm/filesystem_api_util.h has a utility fu
hirono
2014/09/17 16:25:53
Done.
| |
| 151 net::GetMimeTypeFromExtension(file_system_url_.virtual_path().Extension(), | |
| 152 &mime_type_); | |
| 153 if (mime_type_.empty() && | |
| 154 file_system_url_.virtual_path().Extension() == ".mhtml") { | |
| 155 mime_type_ = "multipart/related"; | |
|
kinaba
2014/09/16 06:46:52
Why not FixupMimeType()?
hirono
2014/09/17 16:25:53
Done.
| |
| 156 } | |
| 157 | |
| 158 // Check if the entry has a redirect URL. | |
| 159 backend->GetRedirectURLForContents( | |
| 160 file_system_url_, | |
| 161 base::Bind(&DriveURLRequestJob::OnRedirectURLObtained, | |
| 107 weak_ptr_factory_.GetWeakPtr())); | 162 weak_ptr_factory_.GetWeakPtr())); |
| 108 } | 163 } |
| 109 | 164 |
| 165 void DriveURLRequestJob::OnRedirectURLObtained(const GURL& redirect_url) { | |
| 166 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 167 redirect_url_ = redirect_url; | |
| 168 NotifyHeadersComplete(); | |
| 169 } | |
| 170 | |
| 110 void DriveURLRequestJob::Kill() { | 171 void DriveURLRequestJob::Kill() { |
| 111 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 112 | 173 |
| 113 stream_reader_.reset(); | 174 stream_reader_.reset(); |
| 114 net::URLRequestJob::Kill(); | 175 net::URLRequestJob::Kill(); |
| 115 weak_ptr_factory_.InvalidateWeakPtrs(); | 176 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 116 } | 177 } |
| 117 | 178 |
| 118 bool DriveURLRequestJob::GetMimeType(std::string* mime_type) const { | 179 bool DriveURLRequestJob::GetMimeType(std::string* mime_type) const { |
| 119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 120 | 181 |
| 121 if (!entry_) { | 182 mime_type->assign(mime_type_); |
| 122 return false; | |
| 123 } | |
| 124 | |
| 125 mime_type->assign( | |
| 126 FixupMimeType(entry_->file_specific_info().content_mime_type())); | |
| 127 return !mime_type->empty(); | 183 return !mime_type->empty(); |
| 128 } | 184 } |
| 129 | 185 |
| 130 bool DriveURLRequestJob::IsRedirectResponse( | 186 bool DriveURLRequestJob::IsRedirectResponse( |
| 131 GURL* location, int* http_status_code) { | 187 GURL* location, int* http_status_code) { |
| 132 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 133 | 189 if (redirect_url_.is_empty()) |
| 134 if (!entry_ || !entry_->file_specific_info().is_hosted_document()) { | |
| 135 return false; | 190 return false; |
| 136 } | |
| 137 | 191 |
| 138 // Redirect a hosted document. | 192 // Redirect a hosted document. |
| 139 *location = GURL(entry_->file_specific_info().alternate_url()); | 193 *location = redirect_url_; |
| 140 const int kHttpFound = 302; | 194 const int kHttpFound = 302; |
| 141 *http_status_code = kHttpFound; | 195 *http_status_code = kHttpFound; |
| 142 return true; | 196 return true; |
| 143 } | 197 } |
| 144 | 198 |
| 145 bool DriveURLRequestJob::ReadRawData( | 199 bool DriveURLRequestJob::ReadRawData( |
| 146 net::IOBuffer* buf, int buf_size, int* bytes_read) { | 200 net::IOBuffer* buf, int buf_size, int* bytes_read) { |
| 147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 201 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 148 DCHECK(stream_reader_ && stream_reader_->IsInitialized()); | 202 DCHECK(stream_reader_); |
| 149 | 203 |
| 150 int result = stream_reader_->Read( | 204 const int result = |
| 151 buf, buf_size, | 205 stream_reader_->Read(buf, |
| 152 base::Bind(&DriveURLRequestJob::OnReadCompleted, | 206 buf_size, |
| 153 weak_ptr_factory_.GetWeakPtr())); | 207 base::Bind(&DriveURLRequestJob::OnReadCompleted, |
| 208 weak_ptr_factory_.GetWeakPtr())); | |
| 154 | 209 |
| 155 if (result == net::ERR_IO_PENDING) { | 210 if (result == net::ERR_IO_PENDING) { |
| 156 // The data is not yet available. | 211 // The data is not yet available. |
| 157 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); | 212 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); |
| 158 return false; | 213 return false; |
| 159 } | 214 } |
| 160 if (result < 0) { | 215 if (result < 0) { |
| 161 // An error occurs. | 216 // An error occurs. |
| 162 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED, result)); | 217 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED, result)); |
| 163 return false; | 218 return false; |
| 164 } | 219 } |
| 165 | 220 |
| 166 // Reading has been finished immediately. | 221 // Reading has been finished immediately. |
| 167 *bytes_read = result; | 222 *bytes_read = result; |
| 168 return true; | 223 return true; |
| 169 } | 224 } |
| 170 | 225 |
| 171 DriveURLRequestJob::~DriveURLRequestJob() { | 226 DriveURLRequestJob::~DriveURLRequestJob() { |
| 172 } | 227 } |
| 173 | 228 |
| 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) { | 229 void DriveURLRequestJob::OnReadCompleted(int read_result) { |
| 198 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 230 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 199 | 231 |
| 200 if (read_result < 0) { | 232 if (read_result < 0) { |
| 201 DCHECK_NE(read_result, net::ERR_IO_PENDING); | 233 DCHECK_NE(read_result, net::ERR_IO_PENDING); |
| 202 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED, | 234 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
| 203 read_result)); | 235 read_result)); |
| 204 } | 236 } |
| 205 | 237 |
| 206 SetStatus(net::URLRequestStatus()); // Clear the IO_PENDING status. | 238 SetStatus(net::URLRequestStatus()); // Clear the IO_PENDING status. |
| 207 NotifyReadComplete(read_result); | 239 NotifyReadComplete(read_result); |
| 208 } | 240 } |
| 209 | 241 |
| 210 } // namespace drive | 242 } // namespace drive |
| OLD | NEW |