Actually set and remove protocol handlers in IO thread..

This commit is contained in:
Cheng Zhao 2013-08-24 17:59:34 +08:00
parent 96c173217f
commit 33279b1a2f

View file

@ -4,14 +4,46 @@
#include "browser/api/atom_api_protocol.h" #include "browser/api/atom_api_protocol.h"
#include "browser/atom_browser_context.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "net/url_request/url_request_job_manager.h" #include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"
#include "net/url_request/url_request_job_factory_impl.h"
#include "vendor/node/src/node.h" #include "vendor/node/src/node.h"
namespace atom { namespace atom {
namespace api { namespace api {
namespace {
net::URLRequestJobFactoryImpl* GetRequestJobFactory() {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
// Get the job factory.
net::URLRequestJobFactoryImpl* job_factory =
static_cast<net::URLRequestJobFactoryImpl*>(
const_cast<net::URLRequestJobFactory*>(
static_cast<content::BrowserContext*>(AtomBrowserContext::Get())->
GetRequestContext()->GetURLRequestContext()->job_factory()));
return job_factory;
}
class AdapterProtocolHandler
: public net::URLRequestJobFactory::ProtocolHandler {
public:
AdapterProtocolHandler() {}
virtual net::URLRequestJob* MaybeCreateJob(
net::URLRequest* request,
net::NetworkDelegate* network_delegate) const OVERRIDE {
return NULL;
}
private:
DISALLOW_COPY_AND_ASSIGN(AdapterProtocolHandler);
};
} // namespace
Protocol::HandlersMap Protocol::handlers_; Protocol::HandlersMap Protocol::handlers_;
// static // static
@ -53,11 +85,15 @@ v8::Handle<v8::Value> Protocol::UnregisterProtocol(const v8::Arguments& args) {
// static // static
void Protocol::RegisterProtocolInIO(const std::string& scheme) { void Protocol::RegisterProtocolInIO(const std::string& scheme) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
net::URLRequestJobFactoryImpl* job_factory(GetRequestJobFactory());
job_factory->SetProtocolHandler(scheme, new AdapterProtocolHandler);
} }
// static // static
void Protocol::UnregisterProtocolInIO(const std::string& scheme) { void Protocol::UnregisterProtocolInIO(const std::string& scheme) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
net::URLRequestJobFactoryImpl* job_factory(GetRequestJobFactory());
job_factory->SetProtocolHandler(scheme, NULL);
} }
// static // static