REVIEW: obtain mime type from file path only on IO allowed sequence
This commit is contained in:
parent
e072213923
commit
d29c27dc78
3 changed files with 55 additions and 40 deletions
|
@ -101,24 +101,20 @@ void URLRequestAsarJob::InitializeFileJob(
|
|||
}
|
||||
|
||||
void URLRequestAsarJob::Start() {
|
||||
if (type_ == TYPE_ASAR) {
|
||||
int flags = base::File::FLAG_OPEN |
|
||||
base::File::FLAG_READ |
|
||||
base::File::FLAG_ASYNC;
|
||||
int rv = stream_->Open(archive_->path(), flags,
|
||||
base::Bind(&URLRequestAsarJob::DidOpen,
|
||||
weak_ptr_factory_.GetWeakPtr()));
|
||||
if (rv != net::ERR_IO_PENDING)
|
||||
DidOpen(rv);
|
||||
} else if (type_ == TYPE_FILE) {
|
||||
if (type_ == TYPE_ASAR || type_ == TYPE_FILE) {
|
||||
auto* meta_info = new FileMetaInfo();
|
||||
if (type_ == TYPE_ASAR) {
|
||||
meta_info->file_path = archive_->path();
|
||||
meta_info->file_exists = true;
|
||||
meta_info->is_directory = false;
|
||||
meta_info->file_size = file_info_.size;
|
||||
}
|
||||
file_task_runner_->PostTaskAndReply(
|
||||
FROM_HERE,
|
||||
base::Bind(&URLRequestAsarJob::FetchMetaInfo, file_path_,
|
||||
base::Bind(&URLRequestAsarJob::FetchMetaInfo, file_path_, type_,
|
||||
base::Unretained(meta_info)),
|
||||
base::Bind(&URLRequestAsarJob::DidFetchMetaInfo,
|
||||
weak_ptr_factory_.GetWeakPtr(),
|
||||
base::Owned(meta_info)));
|
||||
weak_ptr_factory_.GetWeakPtr(), base::Owned(meta_info)));
|
||||
} else {
|
||||
base::ThreadTaskRunnerHandle::Get()->PostTask(
|
||||
FROM_HERE,
|
||||
|
@ -194,16 +190,12 @@ std::unique_ptr<net::SourceStream> URLRequestAsarJob::SetUpSourceStream() {
|
|||
}
|
||||
|
||||
bool URLRequestAsarJob::GetMimeType(std::string* mime_type) const {
|
||||
if (type_ == TYPE_ASAR) {
|
||||
return net::GetMimeTypeFromFile(file_path_, mime_type);
|
||||
} else {
|
||||
if (meta_info_.mime_type_result) {
|
||||
*mime_type = meta_info_.mime_type;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void URLRequestAsarJob::SetExtraRequestHeaders(
|
||||
const net::HttpRequestHeaders& headers) {
|
||||
|
@ -238,13 +230,17 @@ void URLRequestAsarJob::GetResponseInfo(net::HttpResponseInfo* info) {
|
|||
}
|
||||
|
||||
void URLRequestAsarJob::FetchMetaInfo(const base::FilePath& file_path,
|
||||
JobType type,
|
||||
FileMetaInfo* meta_info) {
|
||||
if (type == TYPE_FILE) {
|
||||
base::File::Info file_info;
|
||||
meta_info->file_exists = base::GetFileInfo(file_path, &file_info);
|
||||
if (meta_info->file_exists) {
|
||||
meta_info->file_path = file_path;
|
||||
meta_info->file_size = file_info.size;
|
||||
meta_info->is_directory = file_info.is_directory;
|
||||
}
|
||||
}
|
||||
// On Windows GetMimeTypeFromFile() goes to the registry. Thus it should be
|
||||
// done in WorkerPool.
|
||||
meta_info->mime_type_result =
|
||||
|
@ -261,9 +257,9 @@ void URLRequestAsarJob::DidFetchMetaInfo(const FileMetaInfo* meta_info) {
|
|||
int flags = base::File::FLAG_OPEN |
|
||||
base::File::FLAG_READ |
|
||||
base::File::FLAG_ASYNC;
|
||||
int rv = stream_->Open(file_path_, flags,
|
||||
base::Bind(&URLRequestAsarJob::DidOpen,
|
||||
weak_ptr_factory_.GetWeakPtr()));
|
||||
int rv = stream_->Open(
|
||||
meta_info_.file_path, flags,
|
||||
base::Bind(&URLRequestAsarJob::DidOpen, weak_ptr_factory_.GetWeakPtr()));
|
||||
if (rv != net::ERR_IO_PENDING)
|
||||
DidOpen(rv);
|
||||
}
|
||||
|
|
|
@ -63,6 +63,13 @@ class URLRequestAsarJob : public net::URLRequestJob {
|
|||
void GetResponseInfo(net::HttpResponseInfo* info) override;
|
||||
|
||||
private:
|
||||
// The type of this job.
|
||||
enum JobType {
|
||||
TYPE_ERROR,
|
||||
TYPE_ASAR,
|
||||
TYPE_FILE,
|
||||
};
|
||||
|
||||
// Meta information about the file. It's used as a member in the
|
||||
// URLRequestFileJob and also passed between threads because disk access is
|
||||
// necessary to obtain it.
|
||||
|
@ -80,10 +87,13 @@ class URLRequestAsarJob : public net::URLRequestJob {
|
|||
bool file_exists;
|
||||
// Flag showing whether the file name actually refers to a directory.
|
||||
bool is_directory;
|
||||
// Path to the file.
|
||||
base::FilePath file_path;
|
||||
};
|
||||
|
||||
// Fetches file info on a background thread.
|
||||
static void FetchMetaInfo(const base::FilePath& file_path,
|
||||
JobType type,
|
||||
FileMetaInfo* meta_info);
|
||||
|
||||
// Callback after fetching file info on a background thread.
|
||||
|
@ -99,12 +109,6 @@ class URLRequestAsarJob : public net::URLRequestJob {
|
|||
// Callback after data is asynchronously read from the file into |buf|.
|
||||
void DidRead(scoped_refptr<net::IOBuffer> buf, int result);
|
||||
|
||||
// The type of this job.
|
||||
enum JobType {
|
||||
TYPE_ERROR,
|
||||
TYPE_ASAR,
|
||||
TYPE_FILE,
|
||||
};
|
||||
JobType type_;
|
||||
|
||||
std::shared_ptr<Archive> archive_;
|
||||
|
|
|
@ -29,7 +29,6 @@
|
|||
#include "content/public/browser/web_contents.h"
|
||||
#include "grit/pdf_viewer_resources_map.h"
|
||||
#include "net/base/load_flags.h"
|
||||
#include "net/base/mime_util.h"
|
||||
#include "net/url_request/url_request.h"
|
||||
#include "net/url_request/url_request_context.h"
|
||||
#include "ui/base/resource/resource_bundle.h"
|
||||
|
@ -79,10 +78,26 @@ class BundledDataSource : public content::URLDataSource {
|
|||
|
||||
std::string GetMimeType(const std::string& path) const override {
|
||||
std::string filename = PathWithoutParams(path);
|
||||
std::string mime_type;
|
||||
net::GetMimeTypeFromFile(
|
||||
base::FilePath::FromUTF8Unsafe(filename), &mime_type);
|
||||
return mime_type;
|
||||
if (base::EndsWith(filename, ".html",
|
||||
base::CompareCase::INSENSITIVE_ASCII)) {
|
||||
return "text/html";
|
||||
} else if (base::EndsWith(filename, ".css",
|
||||
base::CompareCase::INSENSITIVE_ASCII)) {
|
||||
return "text/css";
|
||||
} else if (base::EndsWith(filename, ".js",
|
||||
base::CompareCase::INSENSITIVE_ASCII)) {
|
||||
return "application/javascript";
|
||||
} else if (base::EndsWith(filename, ".png",
|
||||
base::CompareCase::INSENSITIVE_ASCII)) {
|
||||
return "image/png";
|
||||
} else if (base::EndsWith(filename, ".gif",
|
||||
base::CompareCase::INSENSITIVE_ASCII)) {
|
||||
return "image/gif";
|
||||
} else if (base::EndsWith(filename, ".svg",
|
||||
base::CompareCase::INSENSITIVE_ASCII)) {
|
||||
return "image/svg+xml";
|
||||
}
|
||||
return "text/html";
|
||||
}
|
||||
|
||||
bool ShouldAddContentSecurityPolicy() const override { return false; }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue