namespace api {
Protocol::Protocol(v8::Isolate* isolate, AtomBrowserContext* browser_context)
- : request_context_getter_(browser_context->GetRequestContext()),
+ : request_context_getter_(static_cast<brightray::URLRequestContextGetter*>(
+ browser_context->GetRequestContext())),
weak_factory_(this) {
Init(isolate);
}
content::BrowserThread::PostTaskAndReplyWithResult(
content::BrowserThread::IO, FROM_HERE,
base::Bind(&Protocol::UnregisterProtocolInIO,
- base::Unretained(this), scheme),
+ request_context_getter_, scheme),
base::Bind(&Protocol::OnIOCompleted,
GetWeakPtr(), callback));
}
+// static
Protocol::ProtocolError Protocol::UnregisterProtocolInIO(
+ scoped_refptr<brightray::URLRequestContextGetter> request_context_getter,
const std::string& scheme) {
- if (!job_factory()->HasProtocolHandler(scheme))
+ auto job_factory = static_cast<AtomURLRequestJobFactory*>(
+ request_context_getter->job_factory());
+ if (!job_factory->HasProtocolHandler(scheme))
return PROTOCOL_NOT_REGISTERED;
- job_factory()->SetProtocolHandler(scheme, nullptr);
+ job_factory->SetProtocolHandler(scheme, nullptr);
return PROTOCOL_OK;
}
content::BrowserThread::PostTaskAndReplyWithResult(
content::BrowserThread::IO, FROM_HERE,
base::Bind(&Protocol::IsProtocolHandledInIO,
- base::Unretained(this), scheme),
+ request_context_getter_, scheme),
callback);
}
-bool Protocol::IsProtocolHandledInIO(const std::string& scheme) {
- return job_factory()->IsHandledProtocol(scheme);
+// static
+bool Protocol::IsProtocolHandledInIO(
+ scoped_refptr<brightray::URLRequestContextGetter> request_context_getter,
+ const std::string& scheme) {
+ return request_context_getter->job_factory()->IsHandledProtocol(scheme);
}
void Protocol::UninterceptProtocol(
content::BrowserThread::PostTaskAndReplyWithResult(
content::BrowserThread::IO, FROM_HERE,
base::Bind(&Protocol::UninterceptProtocolInIO,
- base::Unretained(this), scheme),
+ request_context_getter_, scheme),
base::Bind(&Protocol::OnIOCompleted,
GetWeakPtr(), callback));
}
+// static
Protocol::ProtocolError Protocol::UninterceptProtocolInIO(
+ scoped_refptr<brightray::URLRequestContextGetter> request_context_getter,
const std::string& scheme) {
- if (!original_protocols_.contains(scheme))
- return PROTOCOL_NOT_INTERCEPTED;
- job_factory()->ReplaceProtocol(scheme,
- original_protocols_.take_and_erase(scheme));
- return PROTOCOL_OK;
+ return static_cast<AtomURLRequestJobFactory*>(
+ request_context_getter->job_factory())->UninterceptProtocol(scheme) ?
+ PROTOCOL_OK : PROTOCOL_NOT_INTERCEPTED;
}
void Protocol::OnIOCompleted(
}
}
+AtomURLRequestJobFactory* Protocol::GetJobFactoryInIO() const {
+ request_context_getter_->GetURLRequestContext(); // Force init.
+ return static_cast<AtomURLRequestJobFactory*>(
+ static_cast<brightray::URLRequestContextGetter*>(
+ request_context_getter_.get())->job_factory());
+}
+
// static
mate::Handle<Protocol> Protocol::Create(
v8::Isolate* isolate, AtomBrowserContext* browser_context) {
#include "atom/browser/atom_browser_context.h"
#include "atom/browser/net/atom_url_request_job_factory.h"
#include "base/callback.h"
-#include "base/containers/scoped_ptr_hash_map.h"
#include "base/memory/weak_ptr.h"
#include "content/public/browser/browser_thread.h"
#include "native_mate/arguments.h"
protected:
Protocol(v8::Isolate* isolate, AtomBrowserContext* browser_context);
- AtomURLRequestJobFactory* job_factory() const {
- request_context_getter_->GetURLRequestContext(); // Force init.
- return static_cast<AtomURLRequestJobFactory*>(
- static_cast<brightray::URLRequestContextGetter*>(
- request_context_getter_.get())->job_factory());
- }
-
- base::WeakPtr<Protocol> GetWeakPtr() {
- return weak_factory_.GetWeakPtr();
- }
-
private:
// Possible errors.
enum ProtocolError {
net::URLRequest* request,
net::NetworkDelegate* network_delegate) const override {
RequestJob* request_job = new RequestJob(request, network_delegate);
- request_job->SetHandlerInfo(isolate_, request_context_, handler_);
+ request_job->SetHandlerInfo(isolate_, request_context_.get(), handler_);
return request_job;
}
private:
v8::Isolate* isolate_;
- net::URLRequestContextGetter* request_context_;
+ scoped_refptr<net::URLRequestContextGetter> request_context_;
Protocol::Handler handler_;
DISALLOW_COPY_AND_ASSIGN(CustomProtocolHandler);
content::BrowserThread::PostTaskAndReplyWithResult(
content::BrowserThread::IO, FROM_HERE,
base::Bind(&Protocol::RegisterProtocolInIO<RequestJob>,
- base::Unretained(this), scheme, handler),
+ request_context_getter_, isolate(), scheme, handler),
base::Bind(&Protocol::OnIOCompleted,
GetWeakPtr(), callback));
}
template<typename RequestJob>
- ProtocolError RegisterProtocolInIO(const std::string& scheme,
- const Handler& handler) {
- if (job_factory()->IsHandledProtocol(scheme))
+ static ProtocolError RegisterProtocolInIO(
+ scoped_refptr<brightray::URLRequestContextGetter> request_context_getter,
+ v8::Isolate* isolate,
+ const std::string& scheme,
+ const Handler& handler) {
+ auto job_factory = static_cast<AtomURLRequestJobFactory*>(
+ request_context_getter->job_factory());
+ if (job_factory->IsHandledProtocol(scheme))
return PROTOCOL_REGISTERED;
std::unique_ptr<CustomProtocolHandler<RequestJob>> protocol_handler(
new CustomProtocolHandler<RequestJob>(
- isolate(), request_context_getter_.get(), handler));
- if (job_factory()->SetProtocolHandler(scheme, std::move(protocol_handler)))
+ isolate, request_context_getter.get(), handler));
+ if (job_factory->SetProtocolHandler(scheme, std::move(protocol_handler)))
return PROTOCOL_OK;
else
return PROTOCOL_FAIL;
// Unregister the protocol handler that handles |scheme|.
void UnregisterProtocol(const std::string& scheme, mate::Arguments* args);
- ProtocolError UnregisterProtocolInIO(const std::string& scheme);
+ static ProtocolError UnregisterProtocolInIO(
+ scoped_refptr<brightray::URLRequestContextGetter> request_context_getter,
+ const std::string& scheme);
// Whether the protocol has handler registered.
void IsProtocolHandled(const std::string& scheme,
const BooleanCallback& callback);
- bool IsProtocolHandledInIO(const std::string& scheme);
+ static bool IsProtocolHandledInIO(
+ scoped_refptr<brightray::URLRequestContextGetter> request_context_getter,
+ const std::string& scheme);
// Replace the protocol handler with a new one.
template<typename RequestJob>
content::BrowserThread::PostTaskAndReplyWithResult(
content::BrowserThread::IO, FROM_HERE,
base::Bind(&Protocol::InterceptProtocolInIO<RequestJob>,
- base::Unretained(this), scheme, handler),
+ request_context_getter_, isolate(), scheme, handler),
base::Bind(&Protocol::OnIOCompleted,
GetWeakPtr(), callback));
}
template<typename RequestJob>
- ProtocolError InterceptProtocolInIO(const std::string& scheme,
- const Handler& handler) {
- if (!job_factory()->IsHandledProtocol(scheme))
+ static ProtocolError InterceptProtocolInIO(
+ scoped_refptr<brightray::URLRequestContextGetter> request_context_getter,
+ v8::Isolate* isolate,
+ const std::string& scheme,
+ const Handler& handler) {
+ auto job_factory = static_cast<AtomURLRequestJobFactory*>(
+ request_context_getter->job_factory());
+ if (!job_factory->IsHandledProtocol(scheme))
return PROTOCOL_NOT_REGISTERED;
// It is possible a protocol is handled but can not be intercepted.
- if (!job_factory()->HasProtocolHandler(scheme))
+ if (!job_factory->HasProtocolHandler(scheme))
return PROTOCOL_FAIL;
- if (ContainsKey(original_protocols_, scheme))
- return PROTOCOL_INTERCEPTED;
std::unique_ptr<CustomProtocolHandler<RequestJob>> protocol_handler(
new CustomProtocolHandler<RequestJob>(
- isolate(), request_context_getter_.get(), handler));
- original_protocols_.set(
- scheme,
- job_factory()->ReplaceProtocol(scheme, std::move(protocol_handler)));
+ isolate, request_context_getter.get(), handler));
+ if (!job_factory->InterceptProtocol(scheme, std::move(protocol_handler)))
+ return PROTOCOL_INTERCEPTED;
return PROTOCOL_OK;
}
// Restore the |scheme| to its original protocol handler.
void UninterceptProtocol(const std::string& scheme, mate::Arguments* args);
- ProtocolError UninterceptProtocolInIO(const std::string& scheme);
+ static ProtocolError UninterceptProtocolInIO(
+ scoped_refptr<brightray::URLRequestContextGetter> request_context_getter,
+ const std::string& scheme);
// Convert error code to JS exception and call the callback.
void OnIOCompleted(const CompletionCallback& callback, ProtocolError error);
// Convert error code to string.
std::string ErrorCodeToString(ProtocolError error);
- scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
+ AtomURLRequestJobFactory* GetJobFactoryInIO() const;
- // Map that stores the original protocols of schemes.
- using OriginalProtocolsMap = base::ScopedPtrHashMap<
- std::string,
- std::unique_ptr<net::URLRequestJobFactory::ProtocolHandler>>;
- OriginalProtocolsMap original_protocols_;
+ base::WeakPtr<Protocol> GetWeakPtr() {
+ return weak_factory_.GetWeakPtr();
+ }
+ scoped_refptr<brightray::URLRequestContextGetter> request_context_getter_;
base::WeakPtrFactory<Protocol> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(Protocol);