feat: migrate protocol module to NetworkService (Part 9) (#18374)
* Compare final data instead of url The behavior of did-finish-load and getURL has changed for redirects when using NetworkService, so the test fails for NetworkService. Comparing the finally received data makes the test more reliable. * Implement intercept APIs * Setting mimeType should set "content-type" header * Passing no argument should not throw JS error * Don't access api namespace in ProxyingURLLoaderFactory * No need to create AtomURLLoaderFactory every time * No use of weak factory
This commit is contained in:
parent
646f572b77
commit
54cbe5f749
8 changed files with 120 additions and 38 deletions
|
@ -5,6 +5,7 @@
|
|||
#include "atom/browser/api/atom_api_protocol_ns.h"
|
||||
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
#include "atom/browser/atom_browser_context.h"
|
||||
#include "atom/common/native_mate_converters/net_converter.h"
|
||||
|
@ -37,8 +38,6 @@ std::string ErrorCodeToString(ProtocolError error) {
|
|||
}
|
||||
}
|
||||
|
||||
void Noop() {}
|
||||
|
||||
} // namespace
|
||||
|
||||
ProtocolNS::ProtocolNS(v8::Isolate* isolate,
|
||||
|
@ -82,15 +81,36 @@ bool ProtocolNS::IsProtocolRegistered(const std::string& scheme) {
|
|||
return base::ContainsKey(handlers_, scheme);
|
||||
}
|
||||
|
||||
ProtocolError ProtocolNS::InterceptProtocol(ProtocolType type,
|
||||
const std::string& scheme,
|
||||
const ProtocolHandler& handler) {
|
||||
ProtocolError error = ProtocolError::OK;
|
||||
if (!base::ContainsKey(intercept_handlers_, scheme))
|
||||
intercept_handlers_[scheme] = std::make_pair(type, handler);
|
||||
else
|
||||
error = ProtocolError::INTERCEPTED;
|
||||
return error;
|
||||
}
|
||||
|
||||
void ProtocolNS::UninterceptProtocol(const std::string& scheme,
|
||||
mate::Arguments* args) {
|
||||
HandleOptionalCallback(args, ProtocolError::NOT_INTERCEPTED);
|
||||
ProtocolError error = ProtocolError::OK;
|
||||
if (base::ContainsKey(intercept_handlers_, scheme))
|
||||
intercept_handlers_.erase(scheme);
|
||||
else
|
||||
error = ProtocolError::NOT_INTERCEPTED;
|
||||
HandleOptionalCallback(args, error);
|
||||
}
|
||||
|
||||
bool ProtocolNS::IsProtocolIntercepted(const std::string& scheme) {
|
||||
return base::ContainsKey(intercept_handlers_, scheme);
|
||||
}
|
||||
|
||||
v8::Local<v8::Promise> ProtocolNS::IsProtocolHandled(
|
||||
const std::string& scheme) {
|
||||
util::Promise promise(isolate());
|
||||
promise.Resolve(IsProtocolRegistered(scheme) ||
|
||||
IsProtocolIntercepted(scheme) ||
|
||||
// The |isProtocolHandled| should return true for builtin
|
||||
// schemes, however with NetworkService it is impossible to
|
||||
// know which schemes are registered until a real network
|
||||
|
@ -141,12 +161,20 @@ void ProtocolNS::BuildPrototype(v8::Isolate* isolate,
|
|||
.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", &ProtocolNS::UninterceptProtocol);
|
||||
.SetMethod("interceptStringProtocol",
|
||||
&ProtocolNS::InterceptProtocolFor<ProtocolType::kString>)
|
||||
.SetMethod("interceptBufferProtocol",
|
||||
&ProtocolNS::InterceptProtocolFor<ProtocolType::kBuffer>)
|
||||
.SetMethod("interceptFileProtocol",
|
||||
&ProtocolNS::InterceptProtocolFor<ProtocolType::kFile>)
|
||||
.SetMethod("interceptHttpProtocol",
|
||||
&ProtocolNS::InterceptProtocolFor<ProtocolType::kHttp>)
|
||||
.SetMethod("interceptStreamProtocol",
|
||||
&ProtocolNS::InterceptProtocolFor<ProtocolType::kStream>)
|
||||
.SetMethod("interceptProtocol",
|
||||
&ProtocolNS::InterceptProtocolFor<ProtocolType::kFree>)
|
||||
.SetMethod("uninterceptProtocol", &ProtocolNS::UninterceptProtocol)
|
||||
.SetMethod("isProtocolIntercepted", &ProtocolNS::IsProtocolIntercepted);
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
|
|
|
@ -5,9 +5,7 @@
|
|||
#ifndef ATOM_BROWSER_API_ATOM_API_PROTOCOL_NS_H_
|
||||
#define ATOM_BROWSER_API_ATOM_API_PROTOCOL_NS_H_
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "atom/browser/api/trackable_object.h"
|
||||
#include "atom/browser/net/atom_url_loader_factory.h"
|
||||
|
@ -43,6 +41,8 @@ class ProtocolNS : public mate::TrackableObject<ProtocolNS> {
|
|||
void RegisterURLLoaderFactories(
|
||||
content::ContentBrowserClient::NonNetworkURLLoaderFactoryMap* factories);
|
||||
|
||||
const HandlersMap& intercept_handlers() const { return intercept_handlers_; }
|
||||
|
||||
private:
|
||||
ProtocolNS(v8::Isolate* isolate, AtomBrowserContext* browser_context);
|
||||
~ProtocolNS() override;
|
||||
|
@ -56,7 +56,12 @@ class ProtocolNS : public mate::TrackableObject<ProtocolNS> {
|
|||
const ProtocolHandler& handler);
|
||||
void UnregisterProtocol(const std::string& scheme, mate::Arguments* args);
|
||||
bool IsProtocolRegistered(const std::string& scheme);
|
||||
|
||||
ProtocolError InterceptProtocol(ProtocolType type,
|
||||
const std::string& scheme,
|
||||
const ProtocolHandler& handler);
|
||||
void UninterceptProtocol(const std::string& scheme, mate::Arguments* args);
|
||||
bool IsProtocolIntercepted(const std::string& scheme);
|
||||
|
||||
// Old async version of IsProtocolRegistered.
|
||||
v8::Local<v8::Promise> IsProtocolHandled(const std::string& scheme);
|
||||
|
@ -68,12 +73,18 @@ class ProtocolNS : public mate::TrackableObject<ProtocolNS> {
|
|||
mate::Arguments* args) {
|
||||
HandleOptionalCallback(args, RegisterProtocol(type, scheme, handler));
|
||||
}
|
||||
template <ProtocolType type>
|
||||
void InterceptProtocolFor(const std::string& scheme,
|
||||
const ProtocolHandler& handler,
|
||||
mate::Arguments* args) {
|
||||
HandleOptionalCallback(args, InterceptProtocol(type, scheme, handler));
|
||||
}
|
||||
|
||||
// Be compatible with old interface, which accepts optional callback.
|
||||
void HandleOptionalCallback(mate::Arguments* args, ProtocolError error);
|
||||
|
||||
// scheme => (type, handler).
|
||||
std::map<std::string, std::pair<ProtocolType, ProtocolHandler>> handlers_;
|
||||
HandlersMap handlers_;
|
||||
HandlersMap intercept_handlers_;
|
||||
};
|
||||
|
||||
} // namespace api
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue