feat: migrate protocol module to NetworkService (Part 1) (#17899)

This commit is contained in:
Cheng Zhao 2019-04-24 06:39:21 +09:00 committed by Jeremy Apthorp
parent 341592119f
commit 132137081a
8 changed files with 326 additions and 2 deletions

View file

@ -0,0 +1,129 @@
// Copyright (c) 2019 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/api/atom_api_protocol_ns.h"
#include <memory>
#include "atom/browser/atom_browser_context.h"
#include "atom/browser/net/atom_url_loader_factory.h"
#include "atom/common/native_mate_converters/callback.h"
#include "atom/common/native_mate_converters/value_converter.h"
#include "atom/common/promise_util.h"
#include "base/stl_util.h"
namespace atom {
namespace api {
namespace {
// Convert error code to string.
std::string ErrorCodeToString(ProtocolError error) {
switch (error) {
case PROTOCOL_REGISTERED:
return "The scheme has been registered";
case PROTOCOL_NOT_REGISTERED:
return "The scheme has not been registered";
case PROTOCOL_INTERCEPTED:
return "The scheme has been intercepted";
case PROTOCOL_NOT_INTERCEPTED:
return "The scheme has not been intercepted";
default:
return "Unexpected error";
}
}
void Noop() {}
} // namespace
ProtocolNS::ProtocolNS(v8::Isolate* isolate,
AtomBrowserContext* browser_context) {
Init(isolate);
AttachAsUserData(browser_context);
}
ProtocolNS::~ProtocolNS() = default;
void ProtocolNS::RegisterURLLoaderFactories(
content::ContentBrowserClient::NonNetworkURLLoaderFactoryMap* factories) {
for (const auto& it : handlers_)
factories->emplace(it.first, std::make_unique<AtomURLLoaderFactory>());
}
int ProtocolNS::RegisterProtocol(const std::string& scheme,
const Handler& handler,
mate::Arguments* args) {
ProtocolError error = PROTOCOL_OK;
if (!base::ContainsKey(handlers_, scheme))
handlers_[scheme] = handler;
else
error = PROTOCOL_REGISTERED;
HandleOptionalCallback(args, error);
return error;
}
void ProtocolNS::UnregisterProtocol(const std::string& scheme,
mate::Arguments* args) {
ProtocolError error = PROTOCOL_OK;
if (base::ContainsKey(handlers_, scheme))
handlers_.erase(scheme);
else
error = PROTOCOL_NOT_REGISTERED;
HandleOptionalCallback(args, error);
}
bool ProtocolNS::IsProtocolRegistered(const std::string& scheme) {
return base::ContainsKey(handlers_, scheme);
}
v8::Local<v8::Promise> ProtocolNS::IsProtocolHandled(
const std::string& scheme) {
util::Promise promise(isolate());
promise.Resolve(IsProtocolRegistered(scheme));
return promise.GetHandle();
}
void ProtocolNS::HandleOptionalCallback(mate::Arguments* args,
ProtocolError error) {
CompletionCallback callback;
if (args->GetNext(&callback)) {
if (error == PROTOCOL_OK)
callback.Run(v8::Null(args->isolate()));
else
callback.Run(v8::Exception::Error(
mate::StringToV8(isolate(), ErrorCodeToString(error))));
}
}
// static
mate::Handle<ProtocolNS> ProtocolNS::Create(
v8::Isolate* isolate,
AtomBrowserContext* browser_context) {
return mate::CreateHandle(isolate, new ProtocolNS(isolate, browser_context));
}
// static
void ProtocolNS::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(mate::StringToV8(isolate, "Protocol"));
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.SetMethod("registerStringProtocol", &ProtocolNS::RegisterProtocol)
.SetMethod("registerBufferProtocol", &ProtocolNS::RegisterProtocol)
.SetMethod("registerFileProtocol", &ProtocolNS::RegisterProtocol)
.SetMethod("registerHttpProtocol", &ProtocolNS::RegisterProtocol)
.SetMethod("registerStreamProtocol", &ProtocolNS::RegisterProtocol)
.SetMethod("unregisterProtocol", &ProtocolNS::UnregisterProtocol)
.SetMethod("isProtocolRegistered", &ProtocolNS::IsProtocolRegistered)
.SetMethod("isProtocolHandled", &ProtocolNS::IsProtocolHandled)
.SetMethod("interceptStringProtocol", &Noop)
.SetMethod("interceptBufferProtocol", &Noop)
.SetMethod("interceptFileProtocol", &Noop)
.SetMethod("interceptHttpProtocol", &Noop)
.SetMethod("interceptStreamProtocol", &Noop)
.SetMethod("uninterceptProtocol", &Noop);
}
} // namespace api
} // namespace atom

View file

@ -0,0 +1,74 @@
// Copyright (c) 2019 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_BROWSER_API_ATOM_API_PROTOCOL_NS_H_
#define ATOM_BROWSER_API_ATOM_API_PROTOCOL_NS_H_
#include <map>
#include <string>
#include "atom/browser/api/trackable_object.h"
#include "content/public/browser/content_browser_client.h"
#include "native_mate/dictionary.h"
#include "native_mate/handle.h"
namespace atom {
class AtomBrowserContext;
namespace api {
// Possible errors.
enum ProtocolError {
PROTOCOL_OK, // no error
PROTOCOL_REGISTERED,
PROTOCOL_NOT_REGISTERED,
PROTOCOL_INTERCEPTED,
PROTOCOL_NOT_INTERCEPTED,
};
// Protocol implementation based on network services.
class ProtocolNS : public mate::TrackableObject<ProtocolNS> {
public:
static mate::Handle<ProtocolNS> Create(v8::Isolate* isolate,
AtomBrowserContext* browser_context);
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);
// Used by AtomBrowserClient for creating URLLoaderFactory.
void RegisterURLLoaderFactories(
content::ContentBrowserClient::NonNetworkURLLoaderFactoryMap* factories);
private:
ProtocolNS(v8::Isolate* isolate, AtomBrowserContext* browser_context);
~ProtocolNS() override;
// Callback types.
using Handler =
base::Callback<void(const base::DictionaryValue&, v8::Local<v8::Value>)>;
using CompletionCallback = base::Callback<void(v8::Local<v8::Value>)>;
// JS APIs.
int RegisterProtocol(const std::string& scheme,
const Handler& handler,
mate::Arguments* args);
void UnregisterProtocol(const std::string& scheme, mate::Arguments* args);
bool IsProtocolRegistered(const std::string& scheme);
// Old async version of IsProtocolRegistered.
v8::Local<v8::Promise> IsProtocolHandled(const std::string& scheme);
// Be compatible with old interface, which accepts optional callback.
void HandleOptionalCallback(mate::Arguments* args, ProtocolError error);
// scheme => handler.
std::map<std::string, Handler> handlers_;
};
} // namespace api
} // namespace atom
#endif // ATOM_BROWSER_API_ATOM_API_PROTOCOL_NS_H_

View file

@ -14,6 +14,7 @@
#include "atom/browser/api/atom_api_download_item.h"
#include "atom/browser/api/atom_api_net_log.h"
#include "atom/browser/api/atom_api_protocol.h"
#include "atom/browser/api/atom_api_protocol_ns.h"
#include "atom/browser/api/atom_api_web_request.h"
#include "atom/browser/atom_browser_context.h"
#include "atom/browser/atom_browser_main_parts.h"
@ -59,6 +60,7 @@
#include "net/url_request/static_http_user_agent_settings.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"
#include "services/network/public/cpp/features.h"
#include "ui/base/l10n/l10n_util.h"
using content::BrowserThread;
@ -728,8 +730,12 @@ v8::Local<v8::Value> Session::Cookies(v8::Isolate* isolate) {
v8::Local<v8::Value> Session::Protocol(v8::Isolate* isolate) {
if (protocol_.IsEmpty()) {
auto handle = atom::api::Protocol::Create(isolate, browser_context());
protocol_.Reset(isolate, handle.ToV8());
v8::Local<v8::Value> handle;
if (base::FeatureList::IsEnabled(network::features::kNetworkService))
handle = ProtocolNS::Create(isolate, browser_context()).ToV8();
else
handle = Protocol::Create(isolate, browser_context()).ToV8();
protocol_.Reset(isolate, handle);
}
return v8::Local<v8::Value>::New(isolate, protocol_);
}

View file

@ -14,6 +14,7 @@
#include "atom/app/manifests.h"
#include "atom/browser/api/atom_api_app.h"
#include "atom/browser/api/atom_api_protocol.h"
#include "atom/browser/api/atom_api_protocol_ns.h"
#include "atom/browser/api/atom_api_web_contents.h"
#include "atom/browser/atom_browser_context.h"
#include "atom/browser/atom_browser_main_parts.h"
@ -916,6 +917,17 @@ std::string AtomBrowserClient::GetUserAgent() const {
return GetApplicationUserAgent();
}
void AtomBrowserClient::RegisterNonNetworkNavigationURLLoaderFactories(
int frame_tree_node_id,
NonNetworkURLLoaderFactoryMap* factories) {
content::WebContents* web_contents =
content::WebContents::FromFrameTreeNodeId(frame_tree_node_id);
api::ProtocolNS* protocol = api::ProtocolNS::FromWrappedClass(
v8::Isolate::GetCurrent(), web_contents->GetBrowserContext());
if (protocol)
protocol->RegisterURLLoaderFactories(factories);
}
std::string AtomBrowserClient::GetApplicationLocale() {
if (BrowserThread::CurrentlyOn(BrowserThread::IO))
return g_io_thread_application_locale.Get();

View file

@ -157,6 +157,9 @@ class AtomBrowserClient : public content::ContentBrowserClient,
bool ShouldBypassCORB(int render_process_id) const override;
std::string GetProduct() const override;
std::string GetUserAgent() const override;
void RegisterNonNetworkNavigationURLLoaderFactories(
int frame_tree_node_id,
NonNetworkURLLoaderFactoryMap* factories) override;
// content::RenderProcessHostObserver:
void RenderProcessHostDestroyed(content::RenderProcessHost* host) override;

View file

@ -0,0 +1,54 @@
// Copyright (c) 2019 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/net/atom_url_loader_factory.h"
#include <string>
#include <utility>
#include "content/public/browser/browser_thread.h"
#include "services/network/public/cpp/url_loader_completion_status.h"
#include "services/network/public/mojom/url_loader.mojom.h"
using content::BrowserThread;
namespace atom {
AtomURLLoaderFactory::AtomURLLoaderFactory() {}
AtomURLLoaderFactory::~AtomURLLoaderFactory() = default;
void AtomURLLoaderFactory::CreateLoaderAndStart(
network::mojom::URLLoaderRequest loader,
int32_t routing_id,
int32_t request_id,
uint32_t options,
const network::ResourceRequest& request,
network::mojom::URLLoaderClientPtr client,
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation) {
std::string contents = "Not Implemented";
uint32_t size = base::saturated_cast<uint32_t>(contents.size());
mojo::DataPipe pipe(size);
MojoResult result = pipe.producer_handle->WriteData(
contents.data(), &size, MOJO_WRITE_DATA_FLAG_NONE);
if (result != MOJO_RESULT_OK || size < contents.size()) {
client->OnComplete(network::URLLoaderCompletionStatus(net::ERR_FAILED));
return;
}
network::ResourceResponseHead head;
head.mime_type = "text/html";
head.charset = "utf-8";
client->OnReceiveResponse(head);
client->OnStartLoadingResponseBody(std::move(pipe.consumer_handle));
client->OnComplete(network::URLLoaderCompletionStatus(net::OK));
}
void AtomURLLoaderFactory::Clone(
network::mojom::URLLoaderFactoryRequest request) {
bindings_.AddBinding(this, std::move(request));
}
} // namespace atom

View file

@ -0,0 +1,42 @@
// Copyright (c) 2019 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_BROWSER_NET_ATOM_URL_LOADER_FACTORY_H_
#define ATOM_BROWSER_NET_ATOM_URL_LOADER_FACTORY_H_
#include "mojo/public/cpp/bindings/binding_set.h"
#include "net/url_request/url_request_job_factory.h"
#include "services/network/public/mojom/url_loader_factory.mojom.h"
namespace atom {
// Implementation of URLLoaderFactory.
class AtomURLLoaderFactory : public network::mojom::URLLoaderFactory {
public:
AtomURLLoaderFactory();
~AtomURLLoaderFactory() override;
// network::mojom::URLLoaderFactory:
void CreateLoaderAndStart(network::mojom::URLLoaderRequest loader,
int32_t routing_id,
int32_t request_id,
uint32_t options,
const network::ResourceRequest& request,
network::mojom::URLLoaderClientPtr client,
const net::MutableNetworkTrafficAnnotationTag&
traffic_annotation) override;
void Clone(network::mojom::URLLoaderFactoryRequest request) override;
private:
// TODO(zcbenz): This comes from extensions/browser/extension_protocols.cc
// but I don't know what it actually does, find out the meanings of |Clone|
// and |bindings_| and add comments for them.
mojo::BindingSet<network::mojom::URLLoaderFactory> bindings_;
DISALLOW_COPY_AND_ASSIGN(AtomURLLoaderFactory);
};
} // namespace atom
#endif // ATOM_BROWSER_NET_ATOM_URL_LOADER_FACTORY_H_

View file

@ -169,6 +169,8 @@ filenames = {
"atom/browser/api/atom_api_power_save_blocker.h",
"atom/browser/api/atom_api_protocol.cc",
"atom/browser/api/atom_api_protocol.h",
"atom/browser/api/atom_api_protocol_ns.cc",
"atom/browser/api/atom_api_protocol_ns.h",
"atom/browser/api/atom_api_render_process_preferences.cc",
"atom/browser/api/atom_api_render_process_preferences.h",
"atom/browser/api/atom_api_screen.cc",
@ -319,6 +321,8 @@ filenames = {
"atom/browser/net/atom_cert_verifier.h",
"atom/browser/net/atom_network_delegate.cc",
"atom/browser/net/atom_network_delegate.h",
"atom/browser/net/atom_url_loader_factory.cc",
"atom/browser/net/atom_url_loader_factory.h",
"atom/browser/net/atom_url_request.cc",
"atom/browser/net/atom_url_request.h",
"atom/browser/net/atom_url_request_job_factory.cc",