Add 'registered' and 'unregistered' events for protocol module.

This is only used for writing specs.
This commit is contained in:
Cheng Zhao 2013-08-29 20:56:25 +08:00
parent bc4201f911
commit 261f50701a
2 changed files with 32 additions and 3 deletions

View file

@ -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<v8::Object> g_protocol_object;
typedef std::map<std::string, v8::Persistent<v8::Function>> 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<v8::Value> 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<v8::Object> target) {
g_protocol_object = v8::Persistent<v8::Object>::New(
node::node_isolate, target);
node::SetMethod(target, "registerProtocol", RegisterProtocol);
node::SetMethod(target, "unregisterProtocol", UnregisterProtocol);
node::SetMethod(target, "isHandledProtocol", IsHandledProtocol);

View file

@ -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