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.
|
|
|
|
|
|
|
|
#ifndef ATOM_BROWSER_API_ATOM_API_PROTOCOL_H_
|
|
|
|
#define ATOM_BROWSER_API_ATOM_API_PROTOCOL_H_
|
|
|
|
|
2013-08-24 08:38:19 +00:00
|
|
|
#include <string>
|
|
|
|
#include <map>
|
2015-06-12 07:58:23 +00:00
|
|
|
#include <vector>
|
2013-08-24 08:38:19 +00:00
|
|
|
|
2015-08-12 05:30:19 +00:00
|
|
|
#include "atom/browser/net/atom_url_request_job_factory.h"
|
2014-04-21 08:33:32 +00:00
|
|
|
#include "base/callback.h"
|
2015-08-12 05:30:19 +00:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
2015-08-12 13:32:52 +00:00
|
|
|
#include "native_mate/arguments.h"
|
|
|
|
#include "native_mate/dictionary.h"
|
2014-04-21 08:33:32 +00:00
|
|
|
#include "native_mate/handle.h"
|
2015-08-12 05:30:19 +00:00
|
|
|
#include "native_mate/wrappable.h"
|
2014-04-21 08:33:32 +00:00
|
|
|
|
|
|
|
namespace net {
|
|
|
|
class URLRequest;
|
2015-08-10 07:28:18 +00:00
|
|
|
class URLRequestContextGetter;
|
2014-04-21 08:33:32 +00:00
|
|
|
}
|
2013-08-24 07:26:10 +00:00
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
2015-06-18 08:59:03 +00:00
|
|
|
class AtomBrowserContext;
|
2014-04-21 08:33:32 +00:00
|
|
|
class AtomURLRequestJobFactory;
|
|
|
|
|
2013-08-24 07:26:10 +00:00
|
|
|
namespace api {
|
|
|
|
|
2015-08-12 05:30:19 +00:00
|
|
|
class Protocol : public mate::Wrappable {
|
2013-08-24 07:26:10 +00:00
|
|
|
public:
|
2015-08-12 05:30:19 +00:00
|
|
|
using Handler =
|
|
|
|
base::Callback<void(const net::URLRequest*, v8::Local<v8::Value>)>;
|
|
|
|
using CompletionCallback = base::Callback<void(v8::Local<v8::Value>)>;
|
2015-07-09 09:18:45 +00:00
|
|
|
|
2015-06-18 08:59:03 +00:00
|
|
|
static mate::Handle<Protocol> Create(
|
|
|
|
v8::Isolate* isolate, AtomBrowserContext* browser_context);
|
2014-04-21 08:33:32 +00:00
|
|
|
|
|
|
|
protected:
|
2015-06-18 08:59:03 +00:00
|
|
|
explicit Protocol(AtomBrowserContext* browser_context);
|
2014-04-21 08:33:32 +00:00
|
|
|
|
|
|
|
// mate::Wrappable implementations:
|
|
|
|
virtual mate::ObjectTemplateBuilder GetObjectTemplateBuilder(
|
|
|
|
v8::Isolate* isolate);
|
2013-08-24 07:26:10 +00:00
|
|
|
|
|
|
|
private:
|
2015-08-12 05:30:19 +00:00
|
|
|
// Possible errors.
|
|
|
|
enum ProtocolError {
|
|
|
|
PROTOCOL_OK, // no error
|
|
|
|
PROTOCOL_FAIL, // operation failed, should never occur
|
|
|
|
PROTOCOL_REGISTERED,
|
|
|
|
PROTOCOL_NOT_REGISTERED,
|
|
|
|
PROTOCOL_INTERCEPTED,
|
|
|
|
PROTOCOL_NOT_INTERCEPTED,
|
|
|
|
};
|
2014-04-21 08:33:32 +00:00
|
|
|
|
2015-08-12 05:30:19 +00:00
|
|
|
// The protocol handler that will create a protocol handler for certain
|
|
|
|
// request job.
|
|
|
|
template<typename RequestJob>
|
|
|
|
class CustomProtocolHandler
|
|
|
|
: public net::URLRequestJobFactory::ProtocolHandler {
|
|
|
|
public:
|
|
|
|
CustomProtocolHandler(v8::Isolate* isolate, const Handler& handler)
|
|
|
|
: isolate_(isolate), handler_(handler) {}
|
|
|
|
~CustomProtocolHandler() override {}
|
|
|
|
|
|
|
|
net::URLRequestJob* MaybeCreateJob(
|
|
|
|
net::URLRequest* request,
|
|
|
|
net::NetworkDelegate* network_delegate) const override {
|
|
|
|
return new RequestJob(request, network_delegate, isolate_, handler_);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
v8::Isolate* isolate_;
|
|
|
|
Protocol::Handler handler_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(CustomProtocolHandler);
|
|
|
|
};
|
2015-07-05 17:53:07 +00:00
|
|
|
|
2015-06-12 07:58:23 +00:00
|
|
|
// Register schemes to standard scheme list.
|
|
|
|
void RegisterStandardSchemes(const std::vector<std::string>& schemes);
|
|
|
|
|
2015-08-12 05:30:19 +00:00
|
|
|
// Register the protocol with certain request job.
|
|
|
|
template<typename RequestJob>
|
2015-06-10 06:21:09 +00:00
|
|
|
void RegisterProtocol(v8::Isolate* isolate,
|
|
|
|
const std::string& scheme,
|
2015-08-12 05:30:19 +00:00
|
|
|
const Handler& handler,
|
|
|
|
const CompletionCallback& callback) {
|
|
|
|
content::BrowserThread::PostTaskAndReplyWithResult(
|
|
|
|
content::BrowserThread::IO, FROM_HERE,
|
|
|
|
base::Bind(&Protocol::RegisterProtocolInIO<RequestJob>,
|
|
|
|
base::Unretained(this), scheme, handler),
|
|
|
|
base::Bind(&Protocol::OnIOCompleted,
|
|
|
|
base::Unretained(this), callback));
|
|
|
|
}
|
|
|
|
template<typename RequestJob>
|
|
|
|
ProtocolError RegisterProtocolInIO(const std::string& scheme,
|
|
|
|
const Handler& handler) {
|
|
|
|
if (job_factory_->IsHandledProtocol(scheme))
|
|
|
|
return PROTOCOL_REGISTERED;
|
|
|
|
scoped_ptr<CustomProtocolHandler<RequestJob>> protocol_handler(
|
|
|
|
new CustomProtocolHandler<RequestJob>(isolate(), handler));
|
|
|
|
if (job_factory_->SetProtocolHandler(scheme, protocol_handler.Pass()))
|
|
|
|
return PROTOCOL_OK;
|
|
|
|
else
|
|
|
|
return PROTOCOL_FAIL;
|
|
|
|
}
|
|
|
|
|
2015-08-12 13:32:52 +00:00
|
|
|
// Parse optional parameters for registerProtocol.
|
|
|
|
template<typename RequestJob>
|
|
|
|
void JavaScriptRegisterProtocol(v8::Isolate* isolate,
|
|
|
|
const std::string& scheme,
|
|
|
|
mate::Arguments* args) {
|
|
|
|
// protocol.registerProtocol(scheme[, options], handler[, callback]);
|
|
|
|
mate::Dictionary options = mate::Dictionary::CreateEmpty(isolate);
|
|
|
|
Handler handler;
|
|
|
|
CompletionCallback callback;
|
|
|
|
args->GetNext(&options);
|
|
|
|
if (!args->GetNext(&handler)) {
|
|
|
|
args->ThrowError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
args->GetNext(&callback);
|
|
|
|
|
|
|
|
RegisterProtocol<RequestJob>(isolate, scheme, handler, callback);
|
|
|
|
}
|
|
|
|
|
2015-08-12 05:30:19 +00:00
|
|
|
// Convert error code to JS exception and call the callback.
|
|
|
|
void OnIOCompleted(const CompletionCallback& callback, ProtocolError error);
|
|
|
|
|
|
|
|
// Convert error code to string.
|
|
|
|
std::string ErrorCodeToString(ProtocolError error);
|
2013-08-30 02:15:15 +00:00
|
|
|
|
2015-08-10 07:28:18 +00:00
|
|
|
scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
|
|
|
|
|
2015-08-12 05:30:19 +00:00
|
|
|
AtomURLRequestJobFactory* job_factory_; // weak ref.
|
2013-08-30 02:15:15 +00:00
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(Protocol);
|
2013-08-24 07:26:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace api
|
|
|
|
|
|
|
|
} // namespace atom
|
|
|
|
|
|
|
|
#endif // ATOM_BROWSER_API_ATOM_API_PROTOCOL_H_
|