feat: migrate protocol module to NetworkService (Part 3) (#18030)

* Implement http protocol handler

* File protocol handler also accepts options

* Http protocol should inherit headers by default

* Only inherit necessary headers

* Slightly reorder logics
This commit is contained in:
Cheng Zhao 2019-04-30 09:47:04 +09:00 committed by GitHub
parent 7b55ee9d36
commit 277f93653e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 106 additions and 7 deletions

View file

@ -7,11 +7,18 @@
#include <string>
#include <utility>
#include "atom/browser/api/atom_api_session.h"
#include "atom/browser/atom_browser_context.h"
#include "atom/common/atom_constants.h"
#include "atom/common/native_mate_converters/file_path_converter.h"
#include "atom/common/native_mate_converters/gurl_converter.h"
#include "atom/common/native_mate_converters/net_converter.h"
#include "atom/common/native_mate_converters/value_converter.h"
#include "base/guid.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/file_url_loader.h"
#include "gin/dictionary.h"
#include "content/public/browser/storage_partition.h"
#include "native_mate/dictionary.h"
#include "net/base/filename_util.h"
#include "services/network/public/cpp/url_loader_completion_status.h"
#include "services/network/public/mojom/url_loader.mojom.h"
@ -63,6 +70,14 @@ void AtomURLLoaderFactory::CreateLoaderAndStart(
weak_factory_.GetWeakPtr(), std::move(loader),
request, std::move(client), isolate));
break;
case ProtocolType::kHttp:
handler_.Run(
request,
base::BindOnce(&AtomURLLoaderFactory::SendResponseHttp,
weak_factory_.GetWeakPtr(), std::move(loader),
routing_id, request_id, options, request,
std::move(client), traffic_annotation, isolate));
break;
default: {
std::string contents = "Not Implemented";
SendContents(std::move(client), "text/html", "utf-8", contents.data(),
@ -89,7 +104,7 @@ void AtomURLLoaderFactory::SendResponseBuffer(
if (node::Buffer::HasInstance(response)) {
buffer = response;
} else if (response->IsObject()) {
gin::Dictionary dict(
mate::Dictionary dict(
isolate,
response->ToObject(isolate->GetCurrentContext()).ToLocalChecked());
dict.Get("mimeType", &mime_type);
@ -123,7 +138,7 @@ void AtomURLLoaderFactory::SendResponseString(
if (response->IsString()) {
contents = gin::V8ToString(isolate, response);
} else if (response->IsObject()) {
gin::Dictionary dict(
mate::Dictionary dict(
isolate,
response->ToObject(isolate->GetCurrentContext()).ToLocalChecked());
dict.Get("mimeType", &mime_type);
@ -144,16 +159,88 @@ void AtomURLLoaderFactory::SendResponseFile(
return;
base::FilePath path;
if (!mate::ConvertFromV8(isolate, response, &path)) {
scoped_refptr<net::HttpResponseHeaders> response_headers;
if (mate::ConvertFromV8(isolate, response, &path)) {
request.url = net::FilePathToFileURL(path);
} else if (response->IsObject()) {
mate::Dictionary dict(
isolate,
response->ToObject(isolate->GetCurrentContext()).ToLocalChecked());
dict.Get("referrer", &request.referrer);
dict.Get("method", &request.method);
if (dict.Get("path", &path))
request.url = net::FilePathToFileURL(path);
base::DictionaryValue headers;
if (dict.Get("headers", &headers)) {
response_headers = new net::HttpResponseHeaders("HTTP/1.1 200 OK");
response_headers->AddHeader(kCORSHeader);
for (const auto& iter : headers.DictItems())
response_headers->AddHeader(iter.first + ": " +
iter.second.GetString());
}
} else {
network::URLLoaderCompletionStatus status;
status.error_code = net::ERR_NOT_IMPLEMENTED;
client->OnComplete(status);
return;
}
request.url = net::FilePathToFileURL(path);
content::CreateFileURLLoader(request, std::move(loader), std::move(client),
nullptr, false);
nullptr, false, response_headers);
}
void AtomURLLoaderFactory::SendResponseHttp(
network::mojom::URLLoaderRequest loader,
int32_t routing_id,
int32_t request_id,
uint32_t options,
const network::ResourceRequest& original_request,
network::mojom::URLLoaderClientPtr client,
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation,
v8::Isolate* isolate,
v8::Local<v8::Value> response) {
if (HandleError(&client, isolate, response))
return;
if (!response->IsObject()) {
network::URLLoaderCompletionStatus status;
status.error_code = net::ERR_NOT_IMPLEMENTED;
client->OnComplete(status);
return;
}
network::ResourceRequest request;
request.headers = original_request.headers;
request.cors_exempt_headers = original_request.cors_exempt_headers;
mate::Dictionary dict(
isolate,
response->ToObject(isolate->GetCurrentContext()).ToLocalChecked());
dict.Get("url", &request.url);
dict.Get("referrer", &request.referrer);
if (!dict.Get("method", &request.method))
request.method = original_request.method;
scoped_refptr<AtomBrowserContext> browser_context =
AtomBrowserContext::From("", false);
v8::Local<v8::Value> value;
if (dict.Get("session", &value)) {
if (value->IsNull()) {
browser_context = AtomBrowserContext::From(base::GenerateGUID(), true);
} else {
mate::Handle<api::Session> session;
if (mate::ConvertFromV8(isolate, value, &session) && !session.IsEmpty()) {
browser_context = session->browser_context();
}
}
}
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory =
content::BrowserContext::GetDefaultStoragePartition(browser_context.get())
->GetURLLoaderFactoryForBrowserProcess();
url_loader_factory->CreateLoaderAndStart(
std::move(loader), routing_id, request_id, options, std::move(request),
std::move(client), traffic_annotation);
}
bool AtomURLLoaderFactory::HandleError(
@ -165,7 +252,7 @@ bool AtomURLLoaderFactory::HandleError(
v8::Local<v8::Object> obj =
response->ToObject(isolate->GetCurrentContext()).ToLocalChecked();
network::URLLoaderCompletionStatus status;
if (!gin::Dictionary(isolate, obj).Get("error", &status.error_code))
if (!mate::Dictionary(isolate, obj).Get("error", &status.error_code))
return false;
std::move(*client)->OnComplete(status);
return true;
@ -189,6 +276,8 @@ void AtomURLLoaderFactory::SendContents(
network::ResourceResponseHead head;
head.mime_type = std::move(mime_type);
head.charset = std::move(charset);
head.headers = new net::HttpResponseHeaders("HTTP/1.1 200 OK");
head.headers->AddHeader(kCORSHeader);
client->OnReceiveResponse(head);
client->OnStartLoadingResponseBody(std::move(pipe.consumer_handle));
client->OnComplete(network::URLLoaderCompletionStatus(net::OK));

View file

@ -58,6 +58,16 @@ class AtomURLLoaderFactory : public network::mojom::URLLoaderFactory {
network::mojom::URLLoaderClientPtr client,
v8::Isolate* isolate,
v8::Local<v8::Value> response);
void SendResponseHttp(
network::mojom::URLLoaderRequest loader,
int32_t routing_id,
int32_t request_id,
uint32_t options,
const network::ResourceRequest& original_request,
network::mojom::URLLoaderClientPtr client,
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation,
v8::Isolate* isolate,
v8::Local<v8::Value> response);
bool HandleError(network::mojom::URLLoaderClientPtr* client,
v8::Isolate* isolate,