2013-08-24 07:26:10 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "browser/api/atom_api_protocol.h"
|
|
|
|
|
2013-09-03 08:50:10 +00:00
|
|
|
#include "base/stl_util.h"
|
2013-08-24 09:59:34 +00:00
|
|
|
#include "browser/atom_browser_context.h"
|
2013-08-30 12:02:17 +00:00
|
|
|
#include "browser/net/adapter_request_job.h"
|
2013-09-20 08:47:47 +00:00
|
|
|
#include "browser/net/atom_url_request_context_getter.h"
|
2013-08-30 08:10:36 +00:00
|
|
|
#include "browser/net/atom_url_request_job_factory.h"
|
2013-12-11 07:48:19 +00:00
|
|
|
#include "common/v8/native_type_conversions.h"
|
2013-08-24 08:38:19 +00:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
2013-08-24 09:59:34 +00:00
|
|
|
#include "net/url_request/url_request_context.h"
|
2013-12-11 07:48:19 +00:00
|
|
|
|
|
|
|
#include "common/v8/node_common.h"
|
2013-08-24 07:26:10 +00:00
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
2013-08-30 07:48:57 +00:00
|
|
|
typedef net::URLRequestJobFactory::ProtocolHandler ProtocolHandler;
|
|
|
|
|
2013-08-24 09:59:34 +00:00
|
|
|
namespace {
|
|
|
|
|
2013-08-30 04:04:51 +00:00
|
|
|
// The protocol module object.
|
2013-12-11 07:48:19 +00:00
|
|
|
ScopedPersistent<v8::Object> g_protocol_object;
|
2013-08-29 12:56:25 +00:00
|
|
|
|
2013-08-30 02:15:15 +00:00
|
|
|
// Registered protocol handlers.
|
2013-12-11 07:48:19 +00:00
|
|
|
typedef std::map<std::string, RefCountedV8Function> HandlersMap;
|
2013-08-24 12:18:12 +00:00
|
|
|
static HandlersMap g_handlers;
|
|
|
|
|
2013-09-20 10:45:53 +00:00
|
|
|
static const char* kEarlyUseProtocolError = "This method can only be used"
|
|
|
|
"after the application has finished launching.";
|
|
|
|
|
2013-08-29 12:56:25 +00:00
|
|
|
// Emit an event for the protocol module.
|
|
|
|
void EmitEventInUI(const std::string& event, const std::string& parameter) {
|
2014-01-27 07:14:11 +00:00
|
|
|
v8::Locker locker(node_isolate);
|
2013-12-11 07:48:19 +00:00
|
|
|
v8::HandleScope handle_scope(node_isolate);
|
2013-08-29 12:56:25 +00:00
|
|
|
|
2013-09-24 01:41:54 +00:00
|
|
|
v8::Handle<v8::Value> argv[] = {
|
2013-12-11 07:48:19 +00:00
|
|
|
ToV8Value(event),
|
|
|
|
ToV8Value(parameter),
|
2013-08-29 12:56:25 +00:00
|
|
|
};
|
2013-12-11 07:48:19 +00:00
|
|
|
node::MakeCallback(g_protocol_object.NewHandle(node_isolate),
|
|
|
|
"emit", 2, argv);
|
2013-08-29 12:56:25 +00:00
|
|
|
}
|
|
|
|
|
2013-08-29 13:12:48 +00:00
|
|
|
// Convert the URLRequest object to V8 object.
|
|
|
|
v8::Handle<v8::Object> ConvertURLRequestToV8Object(
|
|
|
|
const net::URLRequest* request) {
|
|
|
|
v8::Local<v8::Object> obj = v8::Object::New();
|
2013-12-11 07:48:19 +00:00
|
|
|
obj->Set(ToV8Value("method"), ToV8Value(request->method()));
|
|
|
|
obj->Set(ToV8Value("url"), ToV8Value(request->url().spec()));
|
|
|
|
obj->Set(ToV8Value("referrer"), ToV8Value(request->referrer()));
|
2013-08-29 13:12:48 +00:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2013-08-30 04:04:51 +00:00
|
|
|
// Get the job factory.
|
|
|
|
AtomURLRequestJobFactory* GetRequestJobFactory() {
|
2013-09-20 08:47:47 +00:00
|
|
|
return AtomBrowserContext::Get()->url_request_context_getter()->job_factory();
|
2013-08-24 09:59:34 +00:00
|
|
|
}
|
|
|
|
|
2013-08-30 12:02:17 +00:00
|
|
|
class CustomProtocolRequestJob : public AdapterRequestJob {
|
2013-08-24 11:33:08 +00:00
|
|
|
public:
|
2013-08-30 12:02:17 +00:00
|
|
|
CustomProtocolRequestJob(ProtocolHandler* protocol_handler,
|
|
|
|
net::URLRequest* request,
|
|
|
|
net::NetworkDelegate* network_delegate)
|
|
|
|
: AdapterRequestJob(protocol_handler, request, network_delegate) {
|
2013-08-24 12:32:12 +00:00
|
|
|
}
|
2013-08-24 11:33:08 +00:00
|
|
|
|
2013-08-30 12:02:17 +00:00
|
|
|
// AdapterRequestJob:
|
|
|
|
virtual void GetJobTypeInUI() OVERRIDE {
|
2013-08-24 11:46:38 +00:00
|
|
|
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
|
2013-08-24 12:18:12 +00:00
|
|
|
|
2014-01-27 07:14:11 +00:00
|
|
|
v8::Locker locker(node_isolate);
|
2013-12-11 07:48:19 +00:00
|
|
|
v8::HandleScope handle_scope(node_isolate);
|
|
|
|
|
2013-08-24 12:18:12 +00:00
|
|
|
// Call the JS handler.
|
|
|
|
v8::Handle<v8::Value> argv[] = {
|
2013-08-29 13:12:48 +00:00
|
|
|
ConvertURLRequestToV8Object(request()),
|
2013-08-24 12:18:12 +00:00
|
|
|
};
|
2013-12-11 07:48:19 +00:00
|
|
|
RefCountedV8Function callback = g_handlers[request()->url().scheme()];
|
|
|
|
v8::Handle<v8::Value> result = callback->NewHandle(node_isolate)->Call(
|
|
|
|
v8::Context::GetCurrent()->Global(), 1, argv);
|
2013-08-24 12:18:12 +00:00
|
|
|
|
|
|
|
// Determine the type of the job we are going to create.
|
|
|
|
if (result->IsString()) {
|
2013-09-24 01:41:54 +00:00
|
|
|
std::string data = FromV8Value(result);
|
2013-08-25 04:36:06 +00:00
|
|
|
content::BrowserThread::PostTask(
|
|
|
|
content::BrowserThread::IO,
|
|
|
|
FROM_HERE,
|
|
|
|
base::Bind(&AdapterRequestJob::CreateStringJobAndStart,
|
2013-08-30 12:02:17 +00:00
|
|
|
GetWeakPtr(),
|
2013-08-25 04:36:06 +00:00
|
|
|
"text/plain",
|
|
|
|
"UTF-8",
|
|
|
|
data));
|
|
|
|
return;
|
|
|
|
} else if (result->IsObject()) {
|
|
|
|
v8::Handle<v8::Object> obj = result->ToObject();
|
2013-09-24 01:41:54 +00:00
|
|
|
std::string name = FromV8Value(obj->GetConstructorName());
|
2013-08-25 04:36:06 +00:00
|
|
|
if (name == "RequestStringJob") {
|
2013-09-24 01:41:54 +00:00
|
|
|
std::string mime_type = FromV8Value(obj->Get(
|
2013-08-25 04:36:06 +00:00
|
|
|
v8::String::New("mimeType")));
|
2013-09-24 01:41:54 +00:00
|
|
|
std::string charset = FromV8Value(obj->Get(v8::String::New("charset")));
|
|
|
|
std::string data = FromV8Value(obj->Get(v8::String::New("data")));
|
2013-08-25 04:36:06 +00:00
|
|
|
|
|
|
|
content::BrowserThread::PostTask(
|
|
|
|
content::BrowserThread::IO,
|
|
|
|
FROM_HERE,
|
|
|
|
base::Bind(&AdapterRequestJob::CreateStringJobAndStart,
|
2013-08-30 12:02:17 +00:00
|
|
|
GetWeakPtr(),
|
2013-08-25 04:36:06 +00:00
|
|
|
mime_type,
|
|
|
|
charset,
|
|
|
|
data));
|
|
|
|
return;
|
2013-08-25 08:06:29 +00:00
|
|
|
} else if (name == "RequestFileJob") {
|
2013-09-24 01:41:54 +00:00
|
|
|
base::FilePath path = FromV8Value(obj->Get(v8::String::New("path")));
|
2013-08-25 08:06:29 +00:00
|
|
|
|
|
|
|
content::BrowserThread::PostTask(
|
|
|
|
content::BrowserThread::IO,
|
|
|
|
FROM_HERE,
|
|
|
|
base::Bind(&AdapterRequestJob::CreateFileJobAndStart,
|
2013-08-30 12:02:17 +00:00
|
|
|
GetWeakPtr(),
|
2013-08-25 08:06:29 +00:00
|
|
|
path));
|
|
|
|
return;
|
2013-08-25 04:36:06 +00:00
|
|
|
}
|
2013-08-24 12:18:12 +00:00
|
|
|
}
|
|
|
|
|
2013-08-30 07:48:57 +00:00
|
|
|
// Try the default protocol handler if we have.
|
2013-09-03 08:50:10 +00:00
|
|
|
if (default_protocol_handler()) {
|
2013-08-30 07:48:57 +00:00
|
|
|
content::BrowserThread::PostTask(
|
|
|
|
content::BrowserThread::IO,
|
|
|
|
FROM_HERE,
|
|
|
|
base::Bind(&AdapterRequestJob::CreateJobFromProtocolHandlerAndStart,
|
2013-08-30 12:02:17 +00:00
|
|
|
GetWeakPtr()));
|
2013-09-03 08:50:10 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-08-30 07:48:57 +00:00
|
|
|
|
2013-08-25 04:36:06 +00:00
|
|
|
// Fallback to the not implemented error.
|
2013-08-24 11:46:38 +00:00
|
|
|
content::BrowserThread::PostTask(
|
|
|
|
content::BrowserThread::IO,
|
|
|
|
FROM_HERE,
|
2013-08-25 04:36:06 +00:00
|
|
|
base::Bind(&AdapterRequestJob::CreateErrorJobAndStart,
|
2013-08-30 12:02:17 +00:00
|
|
|
GetWeakPtr(),
|
2013-08-25 04:36:06 +00:00
|
|
|
net::ERR_NOT_IMPLEMENTED));
|
2013-08-24 11:33:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-30 12:02:17 +00:00
|
|
|
// Always return the same CustomProtocolRequestJob for all requests, because
|
|
|
|
// the content API needs the ProtocolHandler to return a job immediately, and
|
2013-08-24 11:33:08 +00:00
|
|
|
// getting the real job from the JS requires asynchronous calls, so we have
|
|
|
|
// to create an adapter job first.
|
2013-08-30 07:48:57 +00:00
|
|
|
// Users can also pass an extra ProtocolHandler as the fallback one when
|
|
|
|
// registered handler doesn't want to deal with the request.
|
2013-08-30 12:02:17 +00:00
|
|
|
class CustomProtocolHandler : public ProtocolHandler {
|
2013-08-24 09:59:34 +00:00
|
|
|
public:
|
2013-08-30 12:24:42 +00:00
|
|
|
explicit CustomProtocolHandler(ProtocolHandler* protocol_handler = NULL)
|
2013-08-30 07:48:57 +00:00
|
|
|
: protocol_handler_(protocol_handler) {
|
|
|
|
}
|
|
|
|
|
2013-08-24 09:59:34 +00:00
|
|
|
virtual net::URLRequestJob* MaybeCreateJob(
|
|
|
|
net::URLRequest* request,
|
|
|
|
net::NetworkDelegate* network_delegate) const OVERRIDE {
|
2013-08-30 12:02:17 +00:00
|
|
|
return new CustomProtocolRequestJob(protocol_handler_.get(),
|
|
|
|
request,
|
|
|
|
network_delegate);
|
2013-08-24 09:59:34 +00:00
|
|
|
}
|
|
|
|
|
2013-09-03 08:50:10 +00:00
|
|
|
ProtocolHandler* ReleaseDefaultProtocolHandler() {
|
|
|
|
return protocol_handler_.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
ProtocolHandler* original_handler() { return protocol_handler_.get(); }
|
|
|
|
|
2013-08-24 09:59:34 +00:00
|
|
|
private:
|
2013-08-30 07:48:57 +00:00
|
|
|
scoped_ptr<ProtocolHandler> protocol_handler_;
|
|
|
|
|
2013-08-30 12:02:17 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(CustomProtocolHandler);
|
2013-08-24 09:59:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2013-08-24 07:26:10 +00:00
|
|
|
// static
|
2013-12-11 07:48:19 +00:00
|
|
|
void Protocol::RegisterProtocol(
|
|
|
|
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2013-09-24 01:41:54 +00:00
|
|
|
std::string scheme;
|
2013-12-11 07:48:19 +00:00
|
|
|
RefCountedV8Function callback;
|
2013-09-24 01:41:54 +00:00
|
|
|
if (!FromV8Arguments(args, &scheme, &callback))
|
|
|
|
return node::ThrowTypeError("Bad argument");
|
|
|
|
|
2013-08-30 05:04:02 +00:00
|
|
|
if (g_handlers.find(scheme) != g_handlers.end() ||
|
2013-12-15 08:53:07 +00:00
|
|
|
GetRequestJobFactory()->IsHandledProtocol(scheme))
|
2013-08-24 08:38:19 +00:00
|
|
|
return node::ThrowError("The scheme is already registered");
|
|
|
|
|
2013-09-20 10:45:53 +00:00
|
|
|
if (AtomBrowserContext::Get()->url_request_context_getter() == NULL)
|
|
|
|
return node::ThrowError(kEarlyUseProtocolError);
|
|
|
|
|
2013-08-24 08:38:19 +00:00
|
|
|
// Store the handler in a map.
|
2013-09-24 01:41:54 +00:00
|
|
|
g_handlers[scheme] = callback;
|
2013-08-24 08:38:19 +00:00
|
|
|
|
|
|
|
content::BrowserThread::PostTask(content::BrowserThread::IO,
|
2013-08-24 11:46:38 +00:00
|
|
|
FROM_HERE,
|
2013-08-24 08:38:19 +00:00
|
|
|
base::Bind(&RegisterProtocolInIO, scheme));
|
2013-08-24 07:26:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2013-12-11 07:48:19 +00:00
|
|
|
void Protocol::UnregisterProtocol(
|
|
|
|
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2013-09-24 01:41:54 +00:00
|
|
|
std::string scheme;
|
|
|
|
if (!FromV8Arguments(args, &scheme))
|
|
|
|
return node::ThrowTypeError("Bad argument");
|
2013-08-24 08:38:19 +00:00
|
|
|
|
2013-09-20 10:45:53 +00:00
|
|
|
if (AtomBrowserContext::Get()->url_request_context_getter() == NULL)
|
|
|
|
return node::ThrowError(kEarlyUseProtocolError);
|
|
|
|
|
2013-08-24 08:38:19 +00:00
|
|
|
// Erase the handler from map.
|
2013-08-24 12:18:12 +00:00
|
|
|
HandlersMap::iterator it(g_handlers.find(scheme));
|
|
|
|
if (it == g_handlers.end())
|
2013-08-24 08:38:19 +00:00
|
|
|
return node::ThrowError("The scheme has not been registered");
|
2013-08-24 12:18:12 +00:00
|
|
|
g_handlers.erase(it);
|
2013-08-24 08:38:19 +00:00
|
|
|
|
|
|
|
content::BrowserThread::PostTask(content::BrowserThread::IO,
|
2013-08-24 11:46:38 +00:00
|
|
|
FROM_HERE,
|
2013-08-24 08:38:19 +00:00
|
|
|
base::Bind(&UnregisterProtocolInIO, scheme));
|
2013-08-24 07:26:10 +00:00
|
|
|
}
|
|
|
|
|
2013-08-29 12:22:52 +00:00
|
|
|
// static
|
2013-12-11 07:48:19 +00:00
|
|
|
void Protocol::IsHandledProtocol(
|
|
|
|
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2013-12-15 08:53:07 +00:00
|
|
|
std::string scheme;
|
|
|
|
if (!FromV8Arguments(args, &scheme))
|
|
|
|
return node::ThrowTypeError("Bad argument");
|
|
|
|
|
|
|
|
args.GetReturnValue().Set(GetRequestJobFactory()->IsHandledProtocol(scheme));
|
2013-08-29 12:22:52 +00:00
|
|
|
}
|
|
|
|
|
2013-08-30 02:15:15 +00:00
|
|
|
// static
|
2013-12-11 07:48:19 +00:00
|
|
|
void Protocol::InterceptProtocol(
|
|
|
|
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2013-09-24 01:41:54 +00:00
|
|
|
std::string scheme;
|
2013-12-11 07:48:19 +00:00
|
|
|
RefCountedV8Function callback;
|
2013-09-24 01:41:54 +00:00
|
|
|
if (!FromV8Arguments(args, &scheme, &callback))
|
|
|
|
return node::ThrowTypeError("Bad argument");
|
|
|
|
|
2013-09-03 08:50:10 +00:00
|
|
|
if (!GetRequestJobFactory()->HasProtocolHandler(scheme))
|
|
|
|
return node::ThrowError("Cannot intercept procotol");
|
|
|
|
|
|
|
|
if (ContainsKey(g_handlers, scheme))
|
|
|
|
return node::ThrowError("Cannot intercept custom procotols");
|
|
|
|
|
2013-09-20 10:45:53 +00:00
|
|
|
if (AtomBrowserContext::Get()->url_request_context_getter() == NULL)
|
|
|
|
return node::ThrowError(kEarlyUseProtocolError);
|
|
|
|
|
2013-09-03 08:50:10 +00:00
|
|
|
// Store the handler in a map.
|
2013-09-24 01:41:54 +00:00
|
|
|
g_handlers[scheme] = callback;
|
2013-08-30 07:48:57 +00:00
|
|
|
|
|
|
|
content::BrowserThread::PostTask(content::BrowserThread::IO,
|
|
|
|
FROM_HERE,
|
2013-09-03 08:50:10 +00:00
|
|
|
base::Bind(&InterceptProtocolInIO, scheme));
|
2013-08-30 02:15:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2013-12-11 07:48:19 +00:00
|
|
|
void Protocol::UninterceptProtocol(
|
|
|
|
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2013-09-24 01:41:54 +00:00
|
|
|
std::string scheme;
|
|
|
|
if (!FromV8Arguments(args, &scheme))
|
|
|
|
return node::ThrowTypeError("Bad argument");
|
2013-09-03 08:50:10 +00:00
|
|
|
|
2013-09-20 10:45:53 +00:00
|
|
|
if (AtomBrowserContext::Get()->url_request_context_getter() == NULL)
|
|
|
|
return node::ThrowError(kEarlyUseProtocolError);
|
|
|
|
|
2013-09-03 08:50:10 +00:00
|
|
|
// Erase the handler from map.
|
|
|
|
HandlersMap::iterator it(g_handlers.find(scheme));
|
|
|
|
if (it == g_handlers.end())
|
|
|
|
return node::ThrowError("The scheme has not been registered");
|
|
|
|
g_handlers.erase(it);
|
|
|
|
|
|
|
|
content::BrowserThread::PostTask(content::BrowserThread::IO,
|
|
|
|
FROM_HERE,
|
|
|
|
base::Bind(&UninterceptProtocolInIO,
|
|
|
|
scheme));
|
2013-08-30 02:15:15 +00:00
|
|
|
}
|
|
|
|
|
2013-08-24 08:38:19 +00:00
|
|
|
// static
|
|
|
|
void Protocol::RegisterProtocolInIO(const std::string& scheme) {
|
|
|
|
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
|
2013-08-30 04:04:51 +00:00
|
|
|
AtomURLRequestJobFactory* job_factory(GetRequestJobFactory());
|
2013-08-30 12:02:17 +00:00
|
|
|
job_factory->SetProtocolHandler(scheme, new CustomProtocolHandler);
|
2013-08-29 12:56:25 +00:00
|
|
|
|
|
|
|
content::BrowserThread::PostTask(content::BrowserThread::UI,
|
|
|
|
FROM_HERE,
|
|
|
|
base::Bind(&EmitEventInUI,
|
|
|
|
"registered",
|
|
|
|
scheme));
|
2013-08-24 08:38:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void Protocol::UnregisterProtocolInIO(const std::string& scheme) {
|
|
|
|
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
|
2013-08-30 04:04:51 +00:00
|
|
|
AtomURLRequestJobFactory* job_factory(GetRequestJobFactory());
|
2013-08-24 09:59:34 +00:00
|
|
|
job_factory->SetProtocolHandler(scheme, NULL);
|
2013-08-29 12:56:25 +00:00
|
|
|
|
|
|
|
content::BrowserThread::PostTask(content::BrowserThread::UI,
|
|
|
|
FROM_HERE,
|
|
|
|
base::Bind(&EmitEventInUI,
|
|
|
|
"unregistered",
|
|
|
|
scheme));
|
2013-08-24 08:38:19 +00:00
|
|
|
}
|
|
|
|
|
2013-08-30 02:15:15 +00:00
|
|
|
// static
|
|
|
|
void Protocol::InterceptProtocolInIO(const std::string& scheme) {
|
|
|
|
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
|
2013-09-03 08:50:10 +00:00
|
|
|
AtomURLRequestJobFactory* job_factory(GetRequestJobFactory());
|
|
|
|
ProtocolHandler* original_handler = job_factory->GetProtocolHandler(scheme);
|
2013-09-03 09:21:10 +00:00
|
|
|
if (original_handler == NULL) {
|
|
|
|
content::BrowserThread::PostTask(
|
|
|
|
content::BrowserThread::UI,
|
|
|
|
FROM_HERE,
|
|
|
|
base::Bind(&EmitEventInUI,
|
|
|
|
"error",
|
|
|
|
"There is no protocol handler to intercpet"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-03 08:50:10 +00:00
|
|
|
job_factory->ReplaceProtocol(scheme,
|
|
|
|
new CustomProtocolHandler(original_handler));
|
|
|
|
|
|
|
|
content::BrowserThread::PostTask(content::BrowserThread::UI,
|
|
|
|
FROM_HERE,
|
|
|
|
base::Bind(&EmitEventInUI,
|
|
|
|
"intercepted",
|
|
|
|
scheme));
|
2013-08-30 02:15:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void Protocol::UninterceptProtocolInIO(const std::string& scheme) {
|
2013-08-30 07:48:57 +00:00
|
|
|
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
|
2013-09-03 08:50:10 +00:00
|
|
|
AtomURLRequestJobFactory* job_factory(GetRequestJobFactory());
|
|
|
|
|
|
|
|
// Check if the protocol handler is intercepted.
|
|
|
|
CustomProtocolHandler* handler = static_cast<CustomProtocolHandler*>(
|
|
|
|
job_factory->GetProtocolHandler(scheme));
|
2013-09-03 09:21:10 +00:00
|
|
|
if (handler->original_handler() == NULL) {
|
|
|
|
content::BrowserThread::PostTask(
|
|
|
|
content::BrowserThread::UI,
|
|
|
|
FROM_HERE,
|
|
|
|
base::Bind(&EmitEventInUI,
|
|
|
|
"error",
|
|
|
|
"The protocol is not intercpeted"));
|
2013-09-03 08:50:10 +00:00
|
|
|
return;
|
2013-09-03 09:21:10 +00:00
|
|
|
}
|
2013-09-03 08:50:10 +00:00
|
|
|
|
|
|
|
// Reset the protocol handler to the orignal one and delete current
|
|
|
|
// protocol handler.
|
|
|
|
ProtocolHandler* original_handler = handler->ReleaseDefaultProtocolHandler();
|
|
|
|
delete job_factory->ReplaceProtocol(scheme, original_handler);
|
|
|
|
|
|
|
|
content::BrowserThread::PostTask(content::BrowserThread::UI,
|
|
|
|
FROM_HERE,
|
|
|
|
base::Bind(&EmitEventInUI,
|
|
|
|
"unintercepted",
|
|
|
|
scheme));
|
2013-08-30 02:15:15 +00:00
|
|
|
}
|
|
|
|
|
2013-08-24 07:26:10 +00:00
|
|
|
// static
|
|
|
|
void Protocol::Initialize(v8::Handle<v8::Object> target) {
|
2013-08-30 04:04:51 +00:00
|
|
|
// Remember the protocol object, used for emitting event later.
|
2013-12-15 08:53:07 +00:00
|
|
|
g_protocol_object.reset(target);
|
2013-12-11 07:48:19 +00:00
|
|
|
|
2014-02-19 11:39:01 +00:00
|
|
|
#if defined(OS_LINUX)
|
|
|
|
// Make sure the job factory has been created.
|
|
|
|
AtomBrowserContext::Get()->url_request_context_getter()->
|
|
|
|
GetURLRequestContext();
|
|
|
|
#endif
|
|
|
|
|
2013-12-11 07:48:19 +00:00
|
|
|
NODE_SET_METHOD(target, "registerProtocol", RegisterProtocol);
|
|
|
|
NODE_SET_METHOD(target, "unregisterProtocol", UnregisterProtocol);
|
|
|
|
NODE_SET_METHOD(target, "isHandledProtocol", IsHandledProtocol);
|
|
|
|
NODE_SET_METHOD(target, "interceptProtocol", InterceptProtocol);
|
|
|
|
NODE_SET_METHOD(target, "uninterceptProtocol", UninterceptProtocol);
|
2013-08-24 07:26:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace api
|
|
|
|
|
|
|
|
} // namespace atom
|
|
|
|
|
|
|
|
NODE_MODULE(atom_browser_protocol, atom::api::Protocol::Initialize)
|