2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
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
|
|
|
|
2015-06-12 07:58:23 +00:00
|
|
|
#include "atom/browser/atom_browser_client.h"
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/browser/atom_browser_context.h"
|
2015-06-18 08:59:03 +00:00
|
|
|
#include "atom/browser/atom_browser_main_parts.h"
|
2015-07-15 12:35:38 +00:00
|
|
|
#include "atom/browser/api/atom_api_session.h"
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/browser/net/adapter_request_job.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"
|
2015-05-29 15:54:00 +00:00
|
|
|
#include "atom/common/native_mate_converters/gurl_converter.h"
|
2013-08-24 08:38:19 +00:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
2014-08-10 11:14:20 +00:00
|
|
|
#include "native_mate/callback.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*> {
|
2015-05-22 11:11:22 +00:00
|
|
|
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
2015-07-07 14:43:13 +00:00
|
|
|
const net::URLRequest* val) {
|
|
|
|
return mate::ObjectTemplateBuilder(isolate)
|
|
|
|
.SetValue("method", val->method())
|
|
|
|
.SetValue("url", val->url().spec())
|
|
|
|
.SetValue("referrer", val->referrer())
|
|
|
|
.Build()->NewInstance();
|
2014-04-21 08:33:32 +00:00
|
|
|
}
|
|
|
|
};
|
2013-08-29 12:56:25 +00:00
|
|
|
|
2015-07-15 12:35:38 +00:00
|
|
|
template<>
|
|
|
|
struct Converter<net::URLRequestContextGetter*> {
|
|
|
|
static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
|
|
|
|
net::URLRequestContextGetter** out) {
|
2015-07-16 20:30:43 +00:00
|
|
|
if (val->IsNull()) {
|
2015-07-15 12:35:38 +00:00
|
|
|
*out = nullptr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
atom::api::Session* session;
|
|
|
|
if (!Converter<atom::api::Session*>::FromV8(isolate, val, &session))
|
|
|
|
return false;
|
2015-07-16 20:30:43 +00:00
|
|
|
*out = session->browser_context()->GetRequestContext();
|
2015-07-15 12:35:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
} // namespace mate
|
2013-08-24 12:18:12 +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
|
|
|
|
2015-05-22 14:15:13 +00:00
|
|
|
scoped_refptr<base::RefCountedBytes> BufferToRefCountedBytes(
|
|
|
|
v8::Local<v8::Value> buf) {
|
|
|
|
scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes);
|
|
|
|
auto start = reinterpret_cast<const unsigned char*>(node::Buffer::Data(buf));
|
|
|
|
data->data().assign(start, start + node::Buffer::Length(buf));
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2015-07-22 16:24:12 +00:00
|
|
|
void GetJobTypeInUI(const Protocol::JsProtocolHandler& callback) {
|
2015-07-05 17:53:07 +00:00
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
2013-08-24 12:18:12 +00:00
|
|
|
|
2015-06-23 09:22:14 +00:00
|
|
|
v8::Locker locker(registry_->isolate());
|
|
|
|
v8::HandleScope handle_scope(registry_->isolate());
|
2013-12-11 07:48:19 +00:00
|
|
|
|
2013-08-24 12:18:12 +00:00
|
|
|
// Call the JS handler.
|
2015-07-07 14:43:13 +00:00
|
|
|
v8::Local<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()) {
|
2015-05-22 11:11:22 +00:00
|
|
|
v8::Local<v8::Object> obj = result->ToObject();
|
2015-06-23 09:22:14 +00:00
|
|
|
mate::Dictionary dict(registry_->isolate(), obj);
|
2014-04-21 08:33:32 +00:00
|
|
|
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;
|
2015-03-16 12:53:45 +00:00
|
|
|
} else if (name == "RequestBufferJob") {
|
|
|
|
std::string mime_type, encoding;
|
2015-05-22 11:11:22 +00:00
|
|
|
v8::Local<v8::Value> buffer;
|
2015-03-16 12:53:45 +00:00
|
|
|
dict.Get("mimeType", &mime_type);
|
|
|
|
dict.Get("encoding", &encoding);
|
|
|
|
dict.Get("data", &buffer);
|
|
|
|
|
|
|
|
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
|
|
|
|
base::Bind(&AdapterRequestJob::CreateBufferJobAndStart,
|
2015-05-22 14:15:13 +00:00
|
|
|
GetWeakPtr(), mime_type, encoding,
|
|
|
|
BufferToRefCountedBytes(buffer)));
|
2015-03-16 12:53:45 +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;
|
2015-05-07 07:42:35 +00:00
|
|
|
} else if (name == "RequestErrorJob") {
|
2015-06-17 01:31:33 +00:00
|
|
|
int error = net::ERR_NOT_IMPLEMENTED;
|
2015-05-07 07:42:35 +00:00
|
|
|
dict.Get("error", &error);
|
|
|
|
|
|
|
|
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
|
|
|
|
base::Bind(&AdapterRequestJob::CreateErrorJobAndStart,
|
|
|
|
GetWeakPtr(), error));
|
|
|
|
return;
|
2015-05-29 15:54:00 +00:00
|
|
|
} else if (name == "RequestHttpJob") {
|
|
|
|
GURL url;
|
2015-06-17 03:20:09 +00:00
|
|
|
std::string method, referrer;
|
2015-07-16 20:30:43 +00:00
|
|
|
net::URLRequestContextGetter* getter =
|
|
|
|
registry_->browser_context()->GetRequestContext();
|
2015-05-29 15:54:00 +00:00
|
|
|
dict.Get("url", &url);
|
2015-06-17 02:57:26 +00:00
|
|
|
dict.Get("method", &method);
|
2015-06-17 03:20:09 +00:00
|
|
|
dict.Get("referrer", &referrer);
|
2015-07-15 12:35:38 +00:00
|
|
|
dict.Get("session", &getter);
|
2015-05-29 15:54:00 +00:00
|
|
|
|
|
|
|
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
|
2015-06-18 09:18:11 +00:00
|
|
|
base::Bind(&AdapterRequestJob::CreateHttpJobAndStart, GetWeakPtr(),
|
2015-07-15 12:35:38 +00:00
|
|
|
base::Unretained(getter), url, method, referrer));
|
2015-05-29 15:54:00 +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
|
|
|
|
2015-07-22 16:24:12 +00:00
|
|
|
// AdapterRequestJob:
|
|
|
|
void GetJobType() override {
|
|
|
|
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
|
|
|
|
base::Bind(&CustomProtocolRequestJob::GetJobTypeInUI,
|
|
|
|
base::Unretained(this),
|
|
|
|
registry_->GetProtocolHandler(request()->url().scheme())));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-01-10 01:24:36 +00:00
|
|
|
net::URLRequestJob* MaybeCreateJob(
|
2013-08-24 09:59:34 +00:00
|
|
|
net::URLRequest* request,
|
2015-01-10 01:24:36 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2015-07-09 09:18:45 +00:00
|
|
|
std::string ConvertErrorCode(int error_code) {
|
|
|
|
switch (error_code) {
|
|
|
|
case Protocol::ERR_SCHEME_REGISTERED:
|
|
|
|
return "The Scheme is already registered";
|
|
|
|
case Protocol::ERR_SCHEME_UNREGISTERED:
|
|
|
|
return "The Scheme has not been registered";
|
|
|
|
case Protocol::ERR_SCHEME_INTERCEPTED:
|
|
|
|
return "There is no protocol handler to intercept";
|
|
|
|
case Protocol::ERR_SCHEME_UNINTERCEPTED:
|
|
|
|
return "The protocol is not intercepted";
|
|
|
|
case Protocol::ERR_NO_SCHEME:
|
|
|
|
return "The Scheme does not exist.";
|
|
|
|
case Protocol::ERR_SCHEME:
|
|
|
|
return "Cannot intercept custom protocols";
|
|
|
|
default:
|
|
|
|
NOTREACHED();
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-24 09:59:34 +00:00
|
|
|
} // namespace
|
|
|
|
|
2015-06-18 08:59:03 +00:00
|
|
|
Protocol::Protocol(AtomBrowserContext* browser_context)
|
2015-07-16 20:30:43 +00:00
|
|
|
: browser_context_(browser_context),
|
|
|
|
job_factory_(browser_context->job_factory()) {
|
2014-08-15 03:35:13 +00:00
|
|
|
CHECK(job_factory_);
|
2014-04-21 08:33:32 +00:00
|
|
|
}
|
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
|
|
|
|
2015-07-09 09:18:45 +00:00
|
|
|
void Protocol::OnIOActionCompleted(const JsCompletionCallback& callback,
|
|
|
|
int error) {
|
2015-07-05 17:53:07 +00:00
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
|
|
|
v8::Locker locker(isolate());
|
|
|
|
v8::HandleScope handle_scope(isolate());
|
|
|
|
|
2015-07-09 09:18:45 +00:00
|
|
|
if (error) {
|
2015-07-05 17:53:07 +00:00
|
|
|
callback.Run(v8::Exception::Error(
|
2015-07-09 09:18:45 +00:00
|
|
|
mate::StringToV8(isolate(), ConvertErrorCode(error))));
|
2015-07-05 17:53:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-09 09:18:45 +00:00
|
|
|
callback.Run(v8::Null(isolate()));
|
2015-07-05 17:53:07 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
mate::ObjectTemplateBuilder Protocol::GetObjectTemplateBuilder(
|
|
|
|
v8::Isolate* isolate) {
|
|
|
|
return mate::ObjectTemplateBuilder(isolate)
|
2015-06-12 07:58:23 +00:00
|
|
|
.SetMethod("registerStandardSchemes", &Protocol::RegisterStandardSchemes)
|
2015-06-10 06:21:09 +00:00
|
|
|
.SetMethod("isHandledProtocol", &Protocol::IsHandledProtocol)
|
2015-07-09 09:18:45 +00:00
|
|
|
.SetMethod("_registerProtocol", &Protocol::RegisterProtocol)
|
|
|
|
.SetMethod("_unregisterProtocol", &Protocol::UnregisterProtocol)
|
2015-07-07 14:43:13 +00:00
|
|
|
.SetMethod("_interceptProtocol", &Protocol::InterceptProtocol)
|
|
|
|
.SetMethod("_uninterceptProtocol", &Protocol::UninterceptProtocol);
|
2013-08-24 07:26:10 +00:00
|
|
|
}
|
|
|
|
|
2015-06-12 07:58:23 +00:00
|
|
|
void Protocol::RegisterStandardSchemes(
|
|
|
|
const std::vector<std::string>& schemes) {
|
|
|
|
atom::AtomBrowserClient::SetCustomSchemes(schemes);
|
|
|
|
}
|
|
|
|
|
2015-07-05 17:53:07 +00:00
|
|
|
void Protocol::IsHandledProtocol(const std::string& scheme,
|
2015-07-07 14:43:13 +00:00
|
|
|
const net::CompletionCallback& callback) {
|
2015-07-05 17:53:07 +00:00
|
|
|
BrowserThread::PostTaskAndReplyWithResult(BrowserThread::IO, FROM_HERE,
|
|
|
|
base::Bind(&AtomURLRequestJobFactory::IsHandledProtocol,
|
|
|
|
base::Unretained(job_factory_), scheme),
|
|
|
|
callback);
|
2014-04-21 08:33:32 +00:00
|
|
|
}
|
2013-09-24 01:41:54 +00:00
|
|
|
|
2015-07-09 09:18:45 +00:00
|
|
|
void Protocol::RegisterProtocol(v8::Isolate* isolate,
|
|
|
|
const std::string& scheme,
|
|
|
|
const JsProtocolHandler& handler,
|
|
|
|
const JsCompletionCallback& callback) {
|
|
|
|
BrowserThread::PostTaskAndReplyWithResult(BrowserThread::IO, FROM_HERE,
|
|
|
|
base::Bind(&Protocol::RegisterProtocolInIO,
|
|
|
|
base::Unretained(this), scheme, handler),
|
|
|
|
base::Bind(&Protocol::OnIOActionCompleted,
|
|
|
|
base::Unretained(this), callback));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Protocol::UnregisterProtocol(v8::Isolate* isolate,
|
|
|
|
const std::string& scheme,
|
|
|
|
const JsCompletionCallback& callback) {
|
|
|
|
BrowserThread::PostTaskAndReplyWithResult(BrowserThread::IO, FROM_HERE,
|
|
|
|
base::Bind(&Protocol::UnregisterProtocolInIO,
|
|
|
|
base::Unretained(this), scheme),
|
|
|
|
base::Bind(&Protocol::OnIOActionCompleted,
|
|
|
|
base::Unretained(this), callback));
|
|
|
|
}
|
|
|
|
|
2015-06-10 06:21:09 +00:00
|
|
|
void Protocol::InterceptProtocol(v8::Isolate* isolate,
|
|
|
|
const std::string& scheme,
|
2015-07-07 14:43:13 +00:00
|
|
|
const JsProtocolHandler& handler,
|
|
|
|
const JsCompletionCallback& callback) {
|
2015-07-05 17:53:07 +00:00
|
|
|
BrowserThread::PostTaskAndReplyWithResult(BrowserThread::IO, FROM_HERE,
|
2015-07-09 09:18:45 +00:00
|
|
|
base::Bind(&Protocol::InterceptProtocolInIO,
|
|
|
|
base::Unretained(this), scheme, handler),
|
|
|
|
base::Bind(&Protocol::OnIOActionCompleted,
|
|
|
|
base::Unretained(this), callback));
|
2013-08-30 02:15:15 +00:00
|
|
|
}
|
|
|
|
|
2015-06-10 06:21:09 +00:00
|
|
|
void Protocol::UninterceptProtocol(v8::Isolate* isolate,
|
2015-07-07 14:43:13 +00:00
|
|
|
const std::string& scheme,
|
|
|
|
const JsCompletionCallback& callback) {
|
2015-07-09 09:18:45 +00:00
|
|
|
BrowserThread::PostTaskAndReplyWithResult(BrowserThread::IO, FROM_HERE,
|
2015-07-07 14:43:13 +00:00
|
|
|
base::Bind(&Protocol::UninterceptProtocolInIO,
|
|
|
|
base::Unretained(this), scheme),
|
2015-07-09 09:18:45 +00:00
|
|
|
base::Bind(&Protocol::OnIOActionCompleted,
|
|
|
|
base::Unretained(this), callback));
|
2013-08-30 02:15:15 +00:00
|
|
|
}
|
|
|
|
|
2015-07-09 09:18:45 +00:00
|
|
|
int Protocol::RegisterProtocolInIO(const std::string& scheme,
|
|
|
|
const JsProtocolHandler& handler) {
|
2015-07-05 17:53:07 +00:00
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
2014-04-21 08:33:32 +00:00
|
|
|
|
2015-07-09 09:18:45 +00:00
|
|
|
if (ContainsKey(protocol_handlers_, scheme) ||
|
|
|
|
job_factory_->IsHandledProtocol(scheme)) {
|
|
|
|
return ERR_SCHEME_REGISTERED;
|
|
|
|
}
|
|
|
|
|
|
|
|
protocol_handlers_[scheme] = handler;
|
2014-04-21 08:33:32 +00:00
|
|
|
job_factory_->SetProtocolHandler(scheme, new CustomProtocolHandler(this));
|
2015-07-09 09:18:45 +00:00
|
|
|
|
|
|
|
return OK;
|
2013-08-24 08:38:19 +00:00
|
|
|
}
|
|
|
|
|
2015-07-09 09:18:45 +00:00
|
|
|
int Protocol::UnregisterProtocolInIO(const std::string& scheme) {
|
2015-07-05 17:53:07 +00:00
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
2014-04-21 08:33:32 +00:00
|
|
|
|
2015-07-09 09:18:45 +00:00
|
|
|
ProtocolHandlersMap::iterator it(protocol_handlers_.find(scheme));
|
|
|
|
if (it == protocol_handlers_.end()) {
|
|
|
|
return ERR_SCHEME_UNREGISTERED;
|
|
|
|
}
|
|
|
|
|
|
|
|
protocol_handlers_.erase(it);
|
2014-04-21 08:33:32 +00:00
|
|
|
job_factory_->SetProtocolHandler(scheme, NULL);
|
2015-07-09 09:18:45 +00:00
|
|
|
|
|
|
|
return OK;
|
2013-08-24 08:38:19 +00:00
|
|
|
}
|
|
|
|
|
2015-07-09 09:18:45 +00:00
|
|
|
int Protocol::InterceptProtocolInIO(const std::string& scheme,
|
|
|
|
const JsProtocolHandler& handler) {
|
2015-07-05 17:53:07 +00:00
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
2014-04-21 08:33:32 +00:00
|
|
|
|
2015-07-09 09:18:45 +00:00
|
|
|
if (!job_factory_->HasProtocolHandler(scheme))
|
|
|
|
return ERR_NO_SCHEME;
|
|
|
|
|
|
|
|
if (ContainsKey(protocol_handlers_, scheme))
|
|
|
|
return ERR_SCHEME;
|
|
|
|
|
|
|
|
protocol_handlers_[scheme] = handler;
|
2014-04-21 08:33:32 +00:00
|
|
|
ProtocolHandler* original_handler = job_factory_->GetProtocolHandler(scheme);
|
2015-07-07 14:43:13 +00:00
|
|
|
if (original_handler == nullptr) {
|
2015-07-09 09:18:45 +00:00
|
|
|
return ERR_SCHEME_INTERCEPTED;
|
2013-09-03 09:21:10 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
job_factory_->ReplaceProtocol(
|
|
|
|
scheme, new CustomProtocolHandler(this, original_handler));
|
2015-07-09 09:18:45 +00:00
|
|
|
|
|
|
|
return OK;
|
2013-08-30 02:15:15 +00:00
|
|
|
}
|
|
|
|
|
2015-07-09 09:18:45 +00:00
|
|
|
int Protocol::UninterceptProtocolInIO(const std::string& scheme) {
|
2015-07-05 17:53:07 +00:00
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
2013-09-03 08:50:10 +00:00
|
|
|
|
2015-07-09 09:18:45 +00:00
|
|
|
ProtocolHandlersMap::iterator it(protocol_handlers_.find(scheme));
|
|
|
|
if (it == protocol_handlers_.end())
|
|
|
|
return ERR_SCHEME_UNREGISTERED;
|
|
|
|
|
|
|
|
protocol_handlers_.erase(it);
|
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));
|
2015-07-07 14:43:13 +00:00
|
|
|
if (handler->original_handler() == nullptr) {
|
2015-07-09 09:18:45 +00:00
|
|
|
return ERR_SCHEME_UNINTERCEPTED;
|
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);
|
2015-07-09 09:18:45 +00:00
|
|
|
|
|
|
|
return OK;
|
2013-08-30 02:15:15 +00:00
|
|
|
}
|
|
|
|
|
2013-08-24 07:26:10 +00:00
|
|
|
// static
|
2015-06-18 08:59:03 +00:00
|
|
|
mate::Handle<Protocol> Protocol::Create(
|
|
|
|
v8::Isolate* isolate, AtomBrowserContext* browser_context) {
|
2015-06-23 02:18:43 +00:00
|
|
|
return mate::CreateHandle(isolate, new Protocol(browser_context));
|
2013-08-24 07:26:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace api
|
|
|
|
|
|
|
|
} // namespace atom
|
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
namespace {
|
|
|
|
|
2015-05-22 11:11:22 +00:00
|
|
|
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
|
|
|
|
v8::Local<v8::Context> context, void* priv) {
|
2014-06-29 12:48:44 +00:00
|
|
|
v8::Isolate* isolate = context->GetIsolate();
|
2014-04-21 08:33:32 +00:00
|
|
|
mate::Dictionary dict(isolate, exports);
|
2015-06-18 08:59:03 +00:00
|
|
|
auto browser_context = static_cast<atom::AtomBrowserContext*>(
|
|
|
|
atom::AtomBrowserMainParts::Get()->browser_context());
|
|
|
|
dict.Set("protocol", atom::api::Protocol::Create(isolate, browser_context));
|
2014-04-21 08:33:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2014-06-29 12:48:44 +00:00
|
|
|
NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_protocol, Initialize)
|