feat: migrate protocol module to NetworkService (Part 2) (#17965)

* Pass protocol type and handler to factory

* Add converter for network::ResourceRequest

* Implement Buffer and String protocol handler

* Implement file protocol
This commit is contained in:
Cheng Zhao 2019-04-29 11:37:45 +09:00 committed by GitHub
parent fe618631f1
commit 6f83977f47
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 266 additions and 39 deletions

View file

@ -7,9 +7,8 @@
#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/native_mate_converters/net_converter.h"
#include "atom/common/native_mate_converters/once_callback.h"
#include "atom/common/promise_util.h"
#include "base/stl_util.h"
@ -48,19 +47,20 @@ ProtocolNS::~ProtocolNS() = default;
void ProtocolNS::RegisterURLLoaderFactories(
content::ContentBrowserClient::NonNetworkURLLoaderFactoryMap* factories) {
for (const auto& it : handlers_)
factories->emplace(it.first, std::make_unique<AtomURLLoaderFactory>());
for (const auto& it : handlers_) {
factories->emplace(it.first, std::make_unique<AtomURLLoaderFactory>(
it.second.first, it.second.second));
}
}
int ProtocolNS::RegisterProtocol(const std::string& scheme,
const Handler& handler,
mate::Arguments* args) {
ProtocolError ProtocolNS::RegisterProtocol(ProtocolType type,
const std::string& scheme,
const ProtocolHandler& handler) {
ProtocolError error = PROTOCOL_OK;
if (!base::ContainsKey(handlers_, scheme))
handlers_[scheme] = handler;
handlers_[scheme] = std::make_pair(type, handler);
else
error = PROTOCOL_REGISTERED;
HandleOptionalCallback(args, error);
return error;
}
@ -109,11 +109,16 @@ 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("registerStringProtocol",
&ProtocolNS::RegisterProtocolFor<ProtocolType::kString>)
.SetMethod("registerBufferProtocol",
&ProtocolNS::RegisterProtocolFor<ProtocolType::kBuffer>)
.SetMethod("registerFileProtocol",
&ProtocolNS::RegisterProtocolFor<ProtocolType::kFile>)
.SetMethod("registerHttpProtocol",
&ProtocolNS::RegisterProtocolFor<ProtocolType::kHttp>)
.SetMethod("registerStreamProtocol",
&ProtocolNS::RegisterProtocolFor<ProtocolType::kStream>)
.SetMethod("unregisterProtocol", &ProtocolNS::UnregisterProtocol)
.SetMethod("isProtocolRegistered", &ProtocolNS::IsProtocolRegistered)
.SetMethod("isProtocolHandled", &ProtocolNS::IsProtocolHandled)

View file

@ -7,8 +7,10 @@
#include <map>
#include <string>
#include <utility>
#include "atom/browser/api/trackable_object.h"
#include "atom/browser/net/atom_url_loader_factory.h"
#include "content/public/browser/content_browser_client.h"
#include "native_mate/dictionary.h"
#include "native_mate/handle.h"
@ -46,25 +48,31 @@ class ProtocolNS : public mate::TrackableObject<ProtocolNS> {
~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);
ProtocolError RegisterProtocol(ProtocolType type,
const std::string& scheme,
const ProtocolHandler& handler);
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);
// Helper for converting old registration APIs to new RegisterProtocol API.
template <ProtocolType type>
void RegisterProtocolFor(const std::string& scheme,
const ProtocolHandler& handler,
mate::Arguments* args) {
HandleOptionalCallback(args, RegisterProtocol(type, scheme, handler));
}
// Be compatible with old interface, which accepts optional callback.
void HandleOptionalCallback(mate::Arguments* args, ProtocolError error);
// scheme => handler.
std::map<std::string, Handler> handlers_;
// scheme => (type, handler).
std::map<std::string, std::pair<ProtocolType, ProtocolHandler>> handlers_;
};
} // namespace api