From 261f50701a911d83139693178c20321c69846e64 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 29 Aug 2013 20:56:25 +0800 Subject: [PATCH] Add 'registered' and 'unregistered' events for protocol module. This is only used for writing specs. --- browser/api/atom_api_protocol.cc | 30 ++++++++++++++++++++++++++++++ browser/api/lib/protocol.coffee | 5 ++--- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/browser/api/atom_api_protocol.cc b/browser/api/atom_api_protocol.cc index 69f7ebe7..843e1534 100644 --- a/browser/api/atom_api_protocol.cc +++ b/browser/api/atom_api_protocol.cc @@ -14,6 +14,7 @@ #include "net/url_request/url_request_job_factory_impl.h" #include "net/url_request/url_request_simple_job.h" #include "vendor/node/src/node.h" +#include "vendor/node/src/node_internals.h" namespace atom { @@ -21,9 +22,23 @@ namespace api { namespace { +// Remember the protocol module object. +v8::Persistent g_protocol_object; + typedef std::map> HandlersMap; static HandlersMap g_handlers; +// Emit an event for the protocol module. +void EmitEventInUI(const std::string& event, const std::string& parameter) { + v8::HandleScope scope; + + v8::Local argv[] = { + v8::String::New(event.data(), event.size()), + v8::String::New(parameter.data(), parameter.size()), + }; + node::MakeCallback(g_protocol_object, "emit", arraysize(argv), argv); +} + net::URLRequestJobFactoryImpl* GetRequestJobFactory() { DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); // Get the job factory. @@ -309,6 +324,12 @@ void Protocol::RegisterProtocolInIO(const std::string& scheme) { DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); net::URLRequestJobFactoryImpl* job_factory(GetRequestJobFactory()); job_factory->SetProtocolHandler(scheme, new AdapterProtocolHandler); + + content::BrowserThread::PostTask(content::BrowserThread::UI, + FROM_HERE, + base::Bind(&EmitEventInUI, + "registered", + scheme)); } // static @@ -316,10 +337,19 @@ void Protocol::UnregisterProtocolInIO(const std::string& scheme) { DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); net::URLRequestJobFactoryImpl* job_factory(GetRequestJobFactory()); job_factory->SetProtocolHandler(scheme, NULL); + + content::BrowserThread::PostTask(content::BrowserThread::UI, + FROM_HERE, + base::Bind(&EmitEventInUI, + "unregistered", + scheme)); } // static void Protocol::Initialize(v8::Handle target) { + g_protocol_object = v8::Persistent::New( + node::node_isolate, target); + node::SetMethod(target, "registerProtocol", RegisterProtocol); node::SetMethod(target, "unregisterProtocol", UnregisterProtocol); node::SetMethod(target, "isHandledProtocol", IsHandledProtocol); diff --git a/browser/api/lib/protocol.coffee b/browser/api/lib/protocol.coffee index 6d9daaff..937c6748 100644 --- a/browser/api/lib/protocol.coffee +++ b/browser/api/lib/protocol.coffee @@ -1,8 +1,7 @@ -bindings = process.atomBinding 'protocol' +protocol = process.atomBinding 'protocol' EventEmitter = require('events').EventEmitter -protocol = new EventEmitter -protocol[key] = value for key, value of bindings +protocol[key] = value for key, value of EventEmitter.prototype protocol.RequestStringJob = class RequestStringJob -- 2.43.0