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. |
| 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)) |
| 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|. Returns NULL on failure. | |
| 44 storage::FileSystemContext* GetFileSystemContext(Profile* profile) { | |
| 45 content::StoragePartition* const storage = | |
| 46 content::BrowserContext::GetDefaultStoragePartition(profile); | |
| 47 if (!storage) | |
| 48 return NULL; | |
| 49 storage::FileSystemContext* const context = storage->GetFileSystemContext(); | |
| 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 if (!context) { | |
| 106 NotifyStartError( | |
| 107 net::URLRequestStatus(net::URLRequestStatus::FAILED, net::ERR_FAILED)); | |
| 108 return; | |
| 109 } | |
| 110 | |
| 111 if (!request()->url().SchemeIs(chrome::kDriveScheme)) { | |
| 95 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, | 112 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
| 96 net::ERR_INVALID_URL)); | 113 net::ERR_INVALID_URL)); |
| 97 return; | 114 return; |
| 98 } | 115 } |
| 99 | 116 |
| 100 // Initialize the stream reader. | 117 storage::ExternalFileSystemBackend* const backend = |
| 101 stream_reader_.reset( | 118 context->external_backend(); |
| 102 new DriveFileStreamReader(file_system_getter_, file_task_runner_.get())); | 119 if (!backend) { |
|
mtomasz
2014/09/12 05:41:23
Can it ever happen? If not, then DCHECK is enough.
hirono
2014/09/12 07:02:15
Done.
| |
| 103 stream_reader_->Initialize( | 120 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
| 104 drive_file_path, | 121 net::ERR_INVALID_URL)); |
| 105 byte_range_, | 122 return; |
| 106 base::Bind(&DriveURLRequestJob::OnDriveFileStreamReaderInitialized, | 123 } |
| 124 | |
| 125 // Obtain the absolute path in the file system. | |
| 126 base::FilePath path = drive::util::GetDriveMountPointPath(profile); | |
| 127 drive::util::GetDriveGrandRootPath().AppendRelativePath( | |
| 128 util::DriveURLToFilePath(request()->url()), &path); | |
| 129 | |
| 130 // Obtain the virtual path. | |
| 131 base::FilePath virtual_path; | |
| 132 if (!backend->GetVirtualPath(path, &virtual_path)) { | |
| 133 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, | |
| 134 net::ERR_INVALID_URL)); | |
| 135 return; | |
| 136 } | |
| 137 | |
| 138 // Obtain the file system URL. | |
| 139 file_system_url_ = context->CreateCrackedFileSystemURL( | |
| 140 GURL(std::string(chrome::kDriveScheme) + ":"), | |
| 141 storage::kFileSystemTypeExternal, | |
|
mtomasz
2014/09/12 05:41:23
We're creating a URL like: filesystem:drive:foobar
hirono
2014/09/12 07:02:15
This is a difficult point. But we need to generate
mtomasz
2014/09/12 08:24:54
Is it secure? What if an extension which doesn't h
hirono
2014/09/16 02:15:14
This is secure because:
1. If the opener does not
| |
| 142 virtual_path); | |
| 143 | |
| 144 // Prepare offset. | |
| 145 int64 offset = byte_range_.first_byte_position(); | |
| 146 if (offset < 0) | |
| 147 offset = 0; | |
| 148 | |
| 149 // Create file stream reader. | |
| 150 stream_reader_ = | |
| 151 context->CreateFileStreamReader(file_system_url_, offset, base::Time()); | |
| 152 if (!stream_reader_) { | |
| 153 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, | |
| 154 net::ERR_FILE_NOT_FOUND)); | |
| 155 return; | |
| 156 } | |
| 157 | |
| 158 // Detect mime type from the extension. | |
| 159 net::GetMimeTypeFromExtension(file_system_url_.virtual_path().Extension(), | |
| 160 &mime_type_); | |
| 161 if (mime_type_.empty() && | |
| 162 file_system_url_.virtual_path().Extension() == ".mhtml") { | |
| 163 mime_type_ = "multipart/related"; | |
| 164 } | |
| 165 | |
| 166 // Check if the entry has a redirect URL. | |
| 167 backend->GetRedirectURLForContents( | |
| 168 file_system_url_, | |
| 169 base::Bind(&DriveURLRequestJob::OnRedirectURLObtained, | |
| 107 weak_ptr_factory_.GetWeakPtr())); | 170 weak_ptr_factory_.GetWeakPtr())); |
| 108 } | 171 } |
| 109 | 172 |
| 173 void DriveURLRequestJob::OnRedirectURLObtained(const GURL& redirect_url) { | |
| 174 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 175 redirect_url_ = redirect_url; | |
| 176 NotifyHeadersComplete(); | |
| 177 } | |
| 178 | |
| 110 void DriveURLRequestJob::Kill() { | 179 void DriveURLRequestJob::Kill() { |
| 111 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 112 | 181 |
| 113 stream_reader_.reset(); | 182 stream_reader_.reset(); |
| 114 net::URLRequestJob::Kill(); | 183 net::URLRequestJob::Kill(); |
| 115 weak_ptr_factory_.InvalidateWeakPtrs(); | 184 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 116 } | 185 } |
| 117 | 186 |
| 118 bool DriveURLRequestJob::GetMimeType(std::string* mime_type) const { | 187 bool DriveURLRequestJob::GetMimeType(std::string* mime_type) const { |
| 119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 120 | 189 |
| 121 if (!entry_) { | 190 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(); | 191 return !mime_type->empty(); |
| 128 } | 192 } |
| 129 | 193 |
| 130 bool DriveURLRequestJob::IsRedirectResponse( | 194 bool DriveURLRequestJob::IsRedirectResponse( |
| 131 GURL* location, int* http_status_code) { | 195 GURL* location, int* http_status_code) { |
| 132 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 196 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 133 | 197 if (redirect_url_.is_empty()) |
| 134 if (!entry_ || !entry_->file_specific_info().is_hosted_document()) { | |
| 135 return false; | 198 return false; |
| 136 } | |
| 137 | 199 |
| 138 // Redirect a hosted document. | 200 // Redirect a hosted document. |
| 139 *location = GURL(entry_->file_specific_info().alternate_url()); | 201 *location = redirect_url_; |
| 140 const int kHttpFound = 302; | 202 const int kHttpFound = 302; |
| 141 *http_status_code = kHttpFound; | 203 *http_status_code = kHttpFound; |
| 142 return true; | 204 return true; |
| 143 } | 205 } |
| 144 | 206 |
| 145 bool DriveURLRequestJob::ReadRawData( | 207 bool DriveURLRequestJob::ReadRawData( |
| 146 net::IOBuffer* buf, int buf_size, int* bytes_read) { | 208 net::IOBuffer* buf, int buf_size, int* bytes_read) { |
| 147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 148 DCHECK(stream_reader_ && stream_reader_->IsInitialized()); | 210 DCHECK(stream_reader_); |
| 149 | 211 |
| 150 int result = stream_reader_->Read( | 212 const int result = |
| 151 buf, buf_size, | 213 stream_reader_->Read(buf, |
| 152 base::Bind(&DriveURLRequestJob::OnReadCompleted, | 214 buf_size, |
| 153 weak_ptr_factory_.GetWeakPtr())); | 215 base::Bind(&DriveURLRequestJob::OnReadCompleted, |
| 216 weak_ptr_factory_.GetWeakPtr())); | |
| 154 | 217 |
| 155 if (result == net::ERR_IO_PENDING) { | 218 if (result == net::ERR_IO_PENDING) { |
| 156 // The data is not yet available. | 219 // The data is not yet available. |
| 157 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); | 220 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); |
| 158 return false; | 221 return false; |
| 159 } | 222 } |
| 160 if (result < 0) { | 223 if (result < 0) { |
| 161 // An error occurs. | 224 // An error occurs. |
| 162 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED, result)); | 225 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED, result)); |
| 163 return false; | 226 return false; |
| 164 } | 227 } |
| 165 | 228 |
| 166 // Reading has been finished immediately. | 229 // Reading has been finished immediately. |
| 167 *bytes_read = result; | 230 *bytes_read = result; |
| 168 return true; | 231 return true; |
| 169 } | 232 } |
| 170 | 233 |
| 171 DriveURLRequestJob::~DriveURLRequestJob() { | 234 DriveURLRequestJob::~DriveURLRequestJob() { |
| 172 } | 235 } |
| 173 | 236 |
| 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) { | 237 void DriveURLRequestJob::OnReadCompleted(int read_result) { |
| 198 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 238 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 199 | 239 |
| 200 if (read_result < 0) { | 240 if (read_result < 0) { |
| 201 DCHECK_NE(read_result, net::ERR_IO_PENDING); | 241 DCHECK_NE(read_result, net::ERR_IO_PENDING); |
| 202 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED, | 242 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
| 203 read_result)); | 243 read_result)); |
| 204 } | 244 } |
| 205 | 245 |
| 206 SetStatus(net::URLRequestStatus()); // Clear the IO_PENDING status. | 246 SetStatus(net::URLRequestStatus()); // Clear the IO_PENDING status. |
| 207 NotifyReadComplete(read_result); | 247 NotifyReadComplete(read_result); |
| 208 } | 248 } |
| 209 | 249 |
| 210 } // namespace drive | 250 } // namespace drive |
| OLD | NEW |