Chromium Code Reviews| Index: chrome/browser/chromeos/drive/drive_url_request_job.cc |
| diff --git a/chrome/browser/chromeos/drive/drive_url_request_job.cc b/chrome/browser/chromeos/drive/drive_url_request_job.cc |
| index 2e1ebb123a0b238ae43517717b4f88577450556b..2505974a502dfdc554a5544a35ae8cecadf262b1 100644 |
| --- a/chrome/browser/chromeos/drive/drive_url_request_job.cc |
| +++ b/chrome/browser/chromeos/drive/drive_url_request_job.cc |
| @@ -5,15 +5,17 @@ |
| #include "chrome/browser/chromeos/drive/drive_url_request_job.h" |
| #include <string> |
| +#include <vector> |
| #include "base/bind.h" |
| #include "base/logging.h" |
| -#include "base/memory/scoped_ptr.h" |
| -#include "chrome/browser/chromeos/drive/drive.pb.h" |
| -#include "chrome/browser/chromeos/drive/drive_file_stream_reader.h" |
| -#include "chrome/browser/chromeos/drive/file_system_interface.h" |
| + |
| +#include "chrome/browser/browser_process.h" |
| #include "chrome/browser/chromeos/drive/file_system_util.h" |
| +#include "chrome/browser/profiles/profile_manager.h" |
| +#include "chrome/common/url_constants.h" |
| #include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/storage_partition.h" |
| #include "net/base/net_errors.h" |
| #include "net/http/http_byte_range.h" |
| #include "net/http/http_request_headers.h" |
| @@ -21,39 +23,40 @@ |
| #include "net/http/http_util.h" |
| #include "net/url_request/url_request.h" |
| #include "net/url_request/url_request_status.h" |
| +#include "storage/browser/fileapi/file_system_backend.h" |
| +#include "storage/browser/fileapi/file_system_context.h" |
| using content::BrowserThread; |
| namespace drive { |
| namespace { |
| - |
| -struct MimeTypeReplacement { |
| - const char* original_type; |
| - const char* new_type; |
| -}; |
| - |
| -const MimeTypeReplacement kMimeTypeReplacements[] = { |
| - {"message/rfc822", "multipart/related"} // Fixes MHTML |
| -}; |
| - |
| -std::string FixupMimeType(const std::string& type) { |
| - for (size_t i = 0; i < arraysize(kMimeTypeReplacements); i++) { |
| - if (type == kMimeTypeReplacements[i].original_type) |
| - return kMimeTypeReplacements[i].new_type; |
| - } |
| - return type; |
| +// 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.
|
| +Profile* GetProfile(void* profile_id) { |
| + // |profile_id| needs to be checked with ProfileManager::IsValidProfile |
| + // before using it. |
| + Profile* const profile = reinterpret_cast<Profile*>(profile_id); |
| + 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.
|
| + return NULL; |
| + return profile; |
| } |
| +// Obtains file system context from |profile|. |
| +storage::FileSystemContext* GetFileSystemContext(Profile* profile) { |
| + content::StoragePartition* const storage = |
| + 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.
|
| + DCHECK(storage); |
| + storage::FileSystemContext* const context = storage->GetFileSystemContext(); |
| + DCHECK(context); |
| + return context; |
| +} |
| } // namespace |
| DriveURLRequestJob::DriveURLRequestJob( |
| - const FileSystemGetter& file_system_getter, |
| - base::SequencedTaskRunner* file_task_runner, |
| + void* profile_id, |
| net::URLRequest* request, |
| net::NetworkDelegate* network_delegate) |
| : net::URLRequestJob(request, network_delegate), |
| - file_system_getter_(file_system_getter), |
| - file_task_runner_(file_task_runner), |
| + profile_id_(profile_id), |
| weak_ptr_factory_(this) { |
| } |
| @@ -89,24 +92,82 @@ void DriveURLRequestJob::Start() { |
| return; |
| } |
| - base::FilePath drive_file_path(util::DriveURLToFilePath(request_->url())); |
| - if (drive_file_path.empty()) { |
| - // Not a valid url. |
| + // Obtain profile. |
| + Profile* const profile = GetProfile(profile_id_); |
| + if (!profile) { |
| + NotifyStartError( |
| + net::URLRequestStatus(net::URLRequestStatus::FAILED, net::ERR_FAILED)); |
| + return; |
| + } |
| + |
| + // Obtain file system context. |
| + storage::FileSystemContext* const context = GetFileSystemContext(profile); |
| + DCHECK(context); |
| + |
| + if (!request()->url().SchemeIs(chrome::kDriveScheme)) { |
| NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
| net::ERR_INVALID_URL)); |
| return; |
| } |
| - // Initialize the stream reader. |
| - stream_reader_.reset( |
| - new DriveFileStreamReader(file_system_getter_, file_task_runner_.get())); |
| - stream_reader_->Initialize( |
| - drive_file_path, |
| - byte_range_, |
| - base::Bind(&DriveURLRequestJob::OnDriveFileStreamReaderInitialized, |
| + storage::ExternalFileSystemBackend* const backend = |
| + context->external_backend(); |
| + DCHECK(backend); |
| + |
| + // Obtain the absolute path in the file system. |
| + base::FilePath path = drive::util::GetDriveMountPointPath(profile); |
| + drive::util::GetDriveGrandRootPath().AppendRelativePath( |
| + util::DriveURLToFilePath(request()->url()), &path); |
| + |
| + // Obtain the virtual path. |
| + base::FilePath virtual_path; |
| + if (!backend->GetVirtualPath(path, &virtual_path)) { |
| + NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
| + net::ERR_FILE_NOT_FOUND)); |
| + return; |
| + } |
| + |
| + // Obtain the file system URL. |
| + file_system_url_ = context->CreateCrackedFileSystemURL( |
| + GURL(std::string(chrome::kDriveScheme) + ":"), |
| + storage::kFileSystemTypeExternal, |
| + virtual_path); |
| + |
| + // Prepare offset. |
| + int64 offset = byte_range_.first_byte_position(); |
| + if (offset < 0) |
| + offset = 0; |
| + |
| + // Create file stream reader. |
| + stream_reader_ = |
| + context->CreateFileStreamReader(file_system_url_, offset, base::Time()); |
| + if (!stream_reader_) { |
| + NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
| + net::ERR_FILE_NOT_FOUND)); |
| + return; |
| + } |
| + |
| + // 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.
|
| + net::GetMimeTypeFromExtension(file_system_url_.virtual_path().Extension(), |
| + &mime_type_); |
| + if (mime_type_.empty() && |
| + file_system_url_.virtual_path().Extension() == ".mhtml") { |
| + mime_type_ = "multipart/related"; |
|
kinaba
2014/09/16 06:46:52
Why not FixupMimeType()?
hirono
2014/09/17 16:25:53
Done.
|
| + } |
| + |
| + // Check if the entry has a redirect URL. |
| + backend->GetRedirectURLForContents( |
| + file_system_url_, |
| + base::Bind(&DriveURLRequestJob::OnRedirectURLObtained, |
| weak_ptr_factory_.GetWeakPtr())); |
| } |
| +void DriveURLRequestJob::OnRedirectURLObtained(const GURL& redirect_url) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + redirect_url_ = redirect_url; |
| + NotifyHeadersComplete(); |
| +} |
| + |
| void DriveURLRequestJob::Kill() { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| @@ -118,25 +179,18 @@ void DriveURLRequestJob::Kill() { |
| bool DriveURLRequestJob::GetMimeType(std::string* mime_type) const { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| - if (!entry_) { |
| - return false; |
| - } |
| - |
| - mime_type->assign( |
| - FixupMimeType(entry_->file_specific_info().content_mime_type())); |
| + mime_type->assign(mime_type_); |
| return !mime_type->empty(); |
| } |
| bool DriveURLRequestJob::IsRedirectResponse( |
| GURL* location, int* http_status_code) { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| - |
| - if (!entry_ || !entry_->file_specific_info().is_hosted_document()) { |
| + if (redirect_url_.is_empty()) |
| return false; |
| - } |
| // Redirect a hosted document. |
| - *location = GURL(entry_->file_specific_info().alternate_url()); |
| + *location = redirect_url_; |
| const int kHttpFound = 302; |
| *http_status_code = kHttpFound; |
| return true; |
| @@ -145,12 +199,13 @@ bool DriveURLRequestJob::IsRedirectResponse( |
| bool DriveURLRequestJob::ReadRawData( |
| net::IOBuffer* buf, int buf_size, int* bytes_read) { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| - DCHECK(stream_reader_ && stream_reader_->IsInitialized()); |
| + DCHECK(stream_reader_); |
| - int result = stream_reader_->Read( |
| - buf, buf_size, |
| - base::Bind(&DriveURLRequestJob::OnReadCompleted, |
| - weak_ptr_factory_.GetWeakPtr())); |
| + const int result = |
| + stream_reader_->Read(buf, |
| + buf_size, |
| + base::Bind(&DriveURLRequestJob::OnReadCompleted, |
| + weak_ptr_factory_.GetWeakPtr())); |
| if (result == net::ERR_IO_PENDING) { |
| // The data is not yet available. |
| @@ -171,29 +226,6 @@ bool DriveURLRequestJob::ReadRawData( |
| DriveURLRequestJob::~DriveURLRequestJob() { |
| } |
| -void DriveURLRequestJob::OnDriveFileStreamReaderInitialized( |
| - int error, scoped_ptr<ResourceEntry> entry) { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| - DCHECK(stream_reader_); |
| - |
| - if (error != FILE_ERROR_OK) { |
| - NotifyStartError( |
| - net::URLRequestStatus(net::URLRequestStatus::FAILED, error)); |
| - return; |
| - } |
| - |
| - DCHECK(entry && entry->has_file_specific_info()); |
| - entry_ = entry.Pass(); |
| - |
| - if (!entry_->file_specific_info().is_hosted_document()) { |
| - // We don't need to set content size for hosted documents, |
| - // because it will be redirected. |
| - set_expected_content_size(entry_->file_info().size()); |
| - } |
| - |
| - NotifyHeadersComplete(); |
| -} |
| - |
| void DriveURLRequestJob::OnReadCompleted(int read_result) { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |