2013-08-24 07:26:10 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
|
2014-04-25 09:49:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2013-08-24 07:26:10 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/browser/api/atom_api_protocol.h"
|
2013-08-24 07:26:10 +00:00
|
|
|
|
2013-09-03 08:50:10 +00:00
|
|
|
#include "base/stl_util.h"
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/browser/atom_browser_context.h"
|
|
|
|
#include "atom/browser/net/adapter_request_job.h"
|
|
|
|
#include "atom/browser/net/atom_url_request_context_getter.h"
|
|
|
|
#include "atom/browser/net/atom_url_request_job_factory.h"
|
2014-04-21 08:33:32 +00:00
|
|
|
#include "atom/common/native_mate_converters/file_path_converter.h"
|
|
|
|
#include "atom/common/native_mate_converters/function_converter.h"
|
2013-08-24 08:38:19 +00:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
2014-04-21 08:33:32 +00:00
|
|
|
#include "native_mate/dictionary.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
|
|
|
|
2014-04-17 05:45:14 +00:00
|
|
|
#include "atom/common/node_includes.h"
|
2013-08-24 07:26:10 +00:00
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
using content::BrowserThread;
|
2013-08-24 07:26:10 +00:00
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
namespace mate {
|
2013-08-24 09:59:34 +00:00
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
template<>
|
|
|
|
struct Converter<const net::URLRequest*> {
|
|
|
|
static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
|
|
|
|
const net::URLRequest* val) {
|
|
|
|
return mate::ObjectTemplateBuilder(isolate)
|
|
|
|
.SetValue("method", val->method())
|
|
|
|
.SetValue("url", val->url().spec())
|
|
|
|
.SetValue("referrer", val->referrer())
|
|
|
|
.Build()->NewInstance();
|
|
|
|
}
|
|
|
|
};
|
2013-08-29 12:56:25 +00:00
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
} // namespace mate
|
2013-08-24 12:18:12 +00:00
|
|
|
|
2013-09-20 10:45:53 +00:00
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
namespace atom {
|
2013-08-29 12:56:25 +00:00
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
namespace api {
|
2013-08-29 12:56:25 +00:00
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
namespace {
|
2013-08-29 13:12:48 +00:00
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
typedef net::URLRequestJobFactory::ProtocolHandler ProtocolHandler;
|
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:
|
2014-04-21 08:33:32 +00:00
|
|
|
CustomProtocolRequestJob(Protocol* registry,
|
|
|
|
ProtocolHandler* protocol_handler,
|
2013-08-30 12:02:17 +00:00
|
|
|
net::URLRequest* request,
|
|
|
|
net::NetworkDelegate* network_delegate)
|
2014-04-21 08:33:32 +00:00
|
|
|
: AdapterRequestJob(protocol_handler, request, network_delegate),
|
|
|
|
registry_(registry) {
|
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 {
|
2014-04-21 08:33:32 +00:00
|
|
|
DCHECK(BrowserThread::CurrentlyOn(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.
|
2014-04-21 08:33:32 +00:00
|
|
|
Protocol::JsProtocolHandler callback =
|
|
|
|
registry_->GetProtocolHandler(request()->url().scheme());
|
|
|
|
v8::Handle<v8::Value> result = callback.Run(request());
|
2013-08-24 12:18:12 +00:00
|
|
|
|
|
|
|
// Determine the type of the job we are going to create.
|
|
|
|
if (result->IsString()) {
|
2014-04-21 08:33:32 +00:00
|
|
|
std::string data = mate::V8ToString(result);
|
|
|
|
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
|
2013-08-25 04:36:06 +00:00
|
|
|
base::Bind(&AdapterRequestJob::CreateStringJobAndStart,
|
2014-04-21 08:33:32 +00:00
|
|
|
GetWeakPtr(), "text/plain", "UTF-8", data));
|
2013-08-25 04:36:06 +00:00
|
|
|
return;
|
|
|
|
} else if (result->IsObject()) {
|
|
|
|
v8::Handle<v8::Object> obj = result->ToObject();
|
2014-04-21 08:33:32 +00:00
|
|
|
mate::Dictionary dict(node_isolate, obj);
|
|
|
|
std::string name = mate::V8ToString(obj->GetConstructorName());
|
2013-08-25 04:36:06 +00:00
|
|
|
if (name == "RequestStringJob") {
|
2014-04-21 08:33:32 +00:00
|
|
|
std::string mime_type, charset, data;
|
|
|
|
dict.Get("mimeType", &mime_type);
|
|
|
|
dict.Get("charset", &charset);
|
|
|
|
dict.Get("data", &data);
|
|
|
|
|
|
|
|
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
|
2013-08-25 04:36:06 +00:00
|
|
|
base::Bind(&AdapterRequestJob::CreateStringJobAndStart,
|
2014-04-21 08:33:32 +00:00
|
|
|
GetWeakPtr(), mime_type, charset, data));
|
2013-08-25 04:36:06 +00:00
|
|
|
return;
|
2013-08-25 08:06:29 +00:00
|
|
|
} else if (name == "RequestFileJob") {
|
2014-04-21 08:33:32 +00:00
|
|
|
base::FilePath path;
|
|
|
|
dict.Get("path", &path);
|
2013-08-25 08:06:29 +00:00
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
|
2013-08-25 08:06:29 +00:00
|
|
|
base::Bind(&AdapterRequestJob::CreateFileJobAndStart,
|
2014-04-21 08:33:32 +00:00
|
|
|
GetWeakPtr(), path));
|
2013-08-25 08:06:29 +00:00
|
|
|
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()) {
|
2014-04-21 08:33:32 +00:00
|
|
|
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
|
2013-08-30 07:48:57 +00:00
|
|
|
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.
|
2014-04-21 08:33:32 +00:00
|
|
|
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
|
2013-08-25 04:36:06 +00:00
|
|
|
base::Bind(&AdapterRequestJob::CreateErrorJobAndStart,
|
2014-04-21 08:33:32 +00:00
|
|
|
GetWeakPtr(), net::ERR_NOT_IMPLEMENTED));
|
2013-08-24 11:33:08 +00:00
|
|
|
}
|
2014-04-21 08:33:32 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
Protocol* registry_; // Weak, the Protocol class is expected to live forever.
|
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:
|
2014-04-21 08:33:32 +00:00
|
|
|
CustomProtocolHandler(api::Protocol* registry,
|
|
|
|
ProtocolHandler* protocol_handler = NULL)
|
|
|
|
: registry_(registry), protocol_handler_(protocol_handler) {
|
2013-08-30 07:48:57 +00:00
|
|
|
}
|
|
|
|
|
2013-08-24 09:59:34 +00:00
|
|
|
virtual net::URLRequestJob* MaybeCreateJob(
|
|
|
|
net::URLRequest* request,
|
|
|
|
net::NetworkDelegate* network_delegate) const OVERRIDE {
|
2014-04-21 08:33:32 +00:00
|
|
|
return new CustomProtocolRequestJob(registry_, 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:
|
2014-04-21 08:33:32 +00:00
|
|
|
Protocol* registry_; // Weak, the Protocol class is expected to live forever.
|
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
|
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
Protocol::Protocol() : job_factory_(
|
|
|
|
AtomBrowserContext::Get()->url_request_context_getter()->job_factory()) {
|
|
|
|
}
|
2013-09-20 10:45:53 +00:00
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
Protocol::JsProtocolHandler Protocol::GetProtocolHandler(
|
|
|
|
const std::string& scheme) {
|
|
|
|
return protocol_handlers_[scheme];
|
|
|
|
}
|
2013-08-24 08:38:19 +00:00
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
mate::ObjectTemplateBuilder Protocol::GetObjectTemplateBuilder(
|
|
|
|
v8::Isolate* isolate) {
|
|
|
|
return mate::ObjectTemplateBuilder(isolate)
|
|
|
|
.SetMethod("registerProtocol",
|
|
|
|
base::Bind(&Protocol::RegisterProtocol,
|
|
|
|
base::Unretained(this)))
|
|
|
|
.SetMethod("unregisterProtocol",
|
|
|
|
base::Bind(&Protocol::UnregisterProtocol,
|
|
|
|
base::Unretained(this)))
|
|
|
|
.SetMethod("isHandledProtocol",
|
|
|
|
base::Bind(&Protocol::IsHandledProtocol,
|
|
|
|
base::Unretained(this)))
|
|
|
|
.SetMethod("interceptProtocol",
|
|
|
|
base::Bind(&Protocol::InterceptProtocol,
|
|
|
|
base::Unretained(this)))
|
|
|
|
.SetMethod("uninterceptProtocol",
|
|
|
|
base::Bind(&Protocol::UninterceptProtocol,
|
|
|
|
base::Unretained(this)));
|
2013-08-24 07:26:10 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
void Protocol::RegisterProtocol(const std::string& scheme,
|
|
|
|
const JsProtocolHandler& callback) {
|
|
|
|
if (ContainsKey(protocol_handlers_, scheme) ||
|
|
|
|
job_factory_->IsHandledProtocol(scheme))
|
|
|
|
return node::ThrowError("The scheme is already registered");
|
2013-08-24 08:38:19 +00:00
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
protocol_handlers_[scheme] = callback;
|
|
|
|
BrowserThread::PostTask(BrowserThread::IO,
|
|
|
|
FROM_HERE,
|
|
|
|
base::Bind(&Protocol::RegisterProtocolInIO,
|
|
|
|
base::Unretained(this), scheme));
|
2013-08-24 07:26:10 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
void Protocol::UnregisterProtocol(const std::string& scheme) {
|
|
|
|
ProtocolHandlersMap::iterator it(protocol_handlers_.find(scheme));
|
|
|
|
if (it == protocol_handlers_.end())
|
|
|
|
return node::ThrowError("The scheme has not been registered");
|
2013-12-15 08:53:07 +00:00
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
protocol_handlers_.erase(it);
|
|
|
|
BrowserThread::PostTask(BrowserThread::IO,
|
|
|
|
FROM_HERE,
|
|
|
|
base::Bind(&Protocol::UnregisterProtocolInIO,
|
|
|
|
base::Unretained(this), scheme));
|
2013-08-29 12:22:52 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
bool Protocol::IsHandledProtocol(const std::string& scheme) {
|
|
|
|
return job_factory_->IsHandledProtocol(scheme);
|
|
|
|
}
|
2013-09-24 01:41:54 +00:00
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
void Protocol::InterceptProtocol(const std::string& scheme,
|
|
|
|
const JsProtocolHandler& callback) {
|
|
|
|
if (!job_factory_->HasProtocolHandler(scheme))
|
|
|
|
return node::ThrowError("Scheme does not exist.");
|
2013-09-03 08:50:10 +00:00
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
if (ContainsKey(protocol_handlers_, scheme))
|
2013-09-03 08:50:10 +00:00
|
|
|
return node::ThrowError("Cannot intercept custom procotols");
|
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
protocol_handlers_[scheme] = callback;
|
|
|
|
BrowserThread::PostTask(BrowserThread::IO,
|
|
|
|
FROM_HERE,
|
|
|
|
base::Bind(&Protocol::InterceptProtocolInIO,
|
|
|
|
base::Unretained(this), scheme));
|
2013-08-30 02:15:15 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
void Protocol::UninterceptProtocol(const std::string& scheme) {
|
|
|
|
ProtocolHandlersMap::iterator it(protocol_handlers_.find(scheme));
|
|
|
|
if (it == protocol_handlers_.end())
|
2013-09-03 08:50:10 +00:00
|
|
|
return node::ThrowError("The scheme has not been registered");
|
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
protocol_handlers_.erase(it);
|
|
|
|
BrowserThread::PostTask(BrowserThread::IO,
|
|
|
|
FROM_HERE,
|
|
|
|
base::Bind(&Protocol::UninterceptProtocolInIO,
|
|
|
|
base::Unretained(this), scheme));
|
2013-08-30 02:15:15 +00:00
|
|
|
}
|
|
|
|
|
2013-08-24 08:38:19 +00:00
|
|
|
void Protocol::RegisterProtocolInIO(const std::string& scheme) {
|
2014-04-21 08:33:32 +00:00
|
|
|
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
|
|
|
|
|
|
|
|
job_factory_->SetProtocolHandler(scheme, new CustomProtocolHandler(this));
|
|
|
|
BrowserThread::PostTask(BrowserThread::UI,
|
|
|
|
FROM_HERE,
|
|
|
|
base::Bind(&Protocol::EmitEventInUI,
|
|
|
|
base::Unretained(this),
|
|
|
|
"registered", scheme));
|
2013-08-24 08:38:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Protocol::UnregisterProtocolInIO(const std::string& scheme) {
|
2014-04-21 08:33:32 +00:00
|
|
|
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
|
|
|
|
|
|
|
|
job_factory_->SetProtocolHandler(scheme, NULL);
|
|
|
|
BrowserThread::PostTask(BrowserThread::UI,
|
|
|
|
FROM_HERE,
|
|
|
|
base::Bind(&Protocol::EmitEventInUI,
|
|
|
|
base::Unretained(this),
|
|
|
|
"unregistered", scheme));
|
2013-08-24 08:38:19 +00:00
|
|
|
}
|
|
|
|
|
2013-08-30 02:15:15 +00:00
|
|
|
void Protocol::InterceptProtocolInIO(const std::string& scheme) {
|
2014-04-21 08:33:32 +00:00
|
|
|
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
|
|
|
|
|
|
|
|
ProtocolHandler* original_handler = job_factory_->GetProtocolHandler(scheme);
|
2013-09-03 09:21:10 +00:00
|
|
|
if (original_handler == NULL) {
|
2014-04-21 08:33:32 +00:00
|
|
|
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
|
|
|
|
&Protocol::EmitEventInUI,
|
|
|
|
base::Unretained(this),
|
|
|
|
"error", "There is no protocol handler to intercpet"));
|
2013-09-03 09:21:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
job_factory_->ReplaceProtocol(
|
|
|
|
scheme, new CustomProtocolHandler(this, original_handler));
|
|
|
|
BrowserThread::PostTask(BrowserThread::UI,
|
|
|
|
FROM_HERE,
|
|
|
|
base::Bind(&Protocol::EmitEventInUI,
|
|
|
|
base::Unretained(this),
|
|
|
|
"intercepted", scheme));
|
2013-08-30 02:15:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Protocol::UninterceptProtocolInIO(const std::string& scheme) {
|
2014-04-21 08:33:32 +00:00
|
|
|
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
|
2013-09-03 08:50:10 +00:00
|
|
|
|
|
|
|
CustomProtocolHandler* handler = static_cast<CustomProtocolHandler*>(
|
2014-04-21 08:33:32 +00:00
|
|
|
job_factory_->GetProtocolHandler(scheme));
|
2013-09-03 09:21:10 +00:00
|
|
|
if (handler->original_handler() == NULL) {
|
2014-04-21 08:33:32 +00:00
|
|
|
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
|
|
|
|
&Protocol::EmitEventInUI,
|
|
|
|
base::Unretained(this),
|
|
|
|
"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
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
// Reset the protocol handler to the orignal one and delete current protocol
|
|
|
|
// handler.
|
2013-09-03 08:50:10 +00:00
|
|
|
ProtocolHandler* original_handler = handler->ReleaseDefaultProtocolHandler();
|
2014-04-21 08:33:32 +00:00
|
|
|
delete job_factory_->ReplaceProtocol(scheme, original_handler);
|
|
|
|
BrowserThread::PostTask(BrowserThread::UI,
|
|
|
|
FROM_HERE,
|
|
|
|
base::Bind(&Protocol::EmitEventInUI,
|
|
|
|
base::Unretained(this),
|
|
|
|
"unintercepted", scheme));
|
|
|
|
}
|
2013-09-03 08:50:10 +00:00
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
void Protocol::EmitEventInUI(const std::string& event,
|
|
|
|
const std::string& parameter) {
|
|
|
|
base::ListValue args;
|
|
|
|
args.AppendString(parameter);
|
|
|
|
Emit(event, args);
|
2013-08-30 02:15:15 +00:00
|
|
|
}
|
|
|
|
|
2013-08-24 07:26:10 +00:00
|
|
|
// static
|
2014-04-21 08:33:32 +00:00
|
|
|
mate::Handle<Protocol> Protocol::Create(v8::Isolate* isolate) {
|
|
|
|
return CreateHandle(isolate, new Protocol);
|
2013-08-24 07:26:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace api
|
|
|
|
|
|
|
|
} // namespace atom
|
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
void Initialize(v8::Handle<v8::Object> exports) {
|
2014-05-07 01:16:45 +00:00
|
|
|
// Make sure the job factory has been created.
|
|
|
|
atom::AtomBrowserContext::Get()->url_request_context_getter()->
|
|
|
|
GetURLRequestContext();
|
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
|
|
|
mate::Dictionary dict(isolate, exports);
|
|
|
|
dict.Set("protocol", atom::api::Protocol::Create(isolate));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
NODE_MODULE(atom_browser_protocol, Initialize)
|