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 <map>
|
2018-09-13 00:25:56 +00:00
|
|
|
#include <memory>
|
2016-08-26 22:30:02 +00:00
|
|
|
#include <string>
|
2018-09-13 00:25:56 +00:00
|
|
|
#include <utility>
|
2015-06-12 07:58:23 +00:00
|
|
|
#include <vector>
|
2013-08-24 08:38:19 +00:00
|
|
|
|
2016-06-08 14:31:27 +00:00
|
|
|
#include "atom/browser/api/trackable_object.h"
|
2016-06-15 08:12:45 +00:00
|
|
|
#include "atom/browser/atom_browser_context.h"
|
2015-08-12 05:30:19 +00:00
|
|
|
#include "atom/browser/net/atom_url_request_job_factory.h"
|
2019-01-17 17:05:10 +00:00
|
|
|
#include "atom/common/promise_util.h"
|
2014-04-21 08:33:32 +00:00
|
|
|
#include "base/callback.h"
|
2016-06-15 08:12:45 +00:00
|
|
|
#include "base/memory/weak_ptr.h"
|
2019-01-12 01:00:43 +00:00
|
|
|
#include "base/task/post_task.h"
|
|
|
|
#include "content/public/browser/browser_task_traits.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"
|
2016-06-15 11:31:29 +00:00
|
|
|
#include "net/url_request/url_request_context.h"
|
2014-04-21 08:33:32 +00:00
|
|
|
|
2016-06-08 13:52:21 +00:00
|
|
|
namespace base {
|
|
|
|
class DictionaryValue;
|
|
|
|
}
|
|
|
|
|
2013-08-24 07:26:10 +00:00
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
2016-08-05 07:35:37 +00:00
|
|
|
std::vector<std::string> GetStandardSchemes();
|
2019-01-29 07:11:01 +00:00
|
|
|
|
|
|
|
void RegisterSchemesAsPrivileged(v8::Local<v8::Value> val,
|
|
|
|
mate::Arguments* args);
|
2016-08-05 07:35:37 +00:00
|
|
|
|
2016-06-08 14:31:27 +00:00
|
|
|
class Protocol : public mate::TrackableObject<Protocol> {
|
2013-08-24 07:26:10 +00:00
|
|
|
public:
|
2019-05-29 20:02:15 +00:00
|
|
|
using Handler = base::RepeatingCallback<void(const base::DictionaryValue&,
|
|
|
|
v8::Local<v8::Value>)>;
|
|
|
|
using CompletionCallback =
|
|
|
|
base::RepeatingCallback<void(v8::Local<v8::Value>)>;
|
2015-07-09 09:18:45 +00:00
|
|
|
|
2018-04-18 01:44:10 +00:00
|
|
|
static mate::Handle<Protocol> Create(v8::Isolate* isolate,
|
|
|
|
AtomBrowserContext* browser_context);
|
2014-04-21 08:33:32 +00:00
|
|
|
|
2016-04-25 01:17:54 +00:00
|
|
|
static void BuildPrototype(v8::Isolate* isolate,
|
2016-08-02 09:08:12 +00:00
|
|
|
v8::Local<v8::FunctionTemplate> prototype);
|
2014-04-21 08:33:32 +00:00
|
|
|
|
2016-04-25 01:17:54 +00:00
|
|
|
protected:
|
2016-05-08 11:14:14 +00:00
|
|
|
Protocol(v8::Isolate* isolate, AtomBrowserContext* browser_context);
|
2018-04-17 23:03:51 +00:00
|
|
|
~Protocol() override;
|
2016-02-24 05:25:30 +00:00
|
|
|
|
2013-08-24 07:26:10 +00:00
|
|
|
private:
|
2015-08-12 05:30:19 +00:00
|
|
|
// Possible errors.
|
2019-05-03 18:11:41 +00:00
|
|
|
enum class ProtocolError {
|
|
|
|
OK, // no error
|
|
|
|
FAIL, // operation failed, should never occur
|
|
|
|
REGISTERED,
|
|
|
|
NOT_REGISTERED,
|
|
|
|
INTERCEPTED,
|
|
|
|
NOT_INTERCEPTED,
|
2015-08-12 05:30:19 +00:00
|
|
|
};
|
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.
|
2018-04-18 01:44:10 +00:00
|
|
|
template <typename RequestJob>
|
2015-08-12 05:30:19 +00:00
|
|
|
class CustomProtocolHandler
|
|
|
|
: public net::URLRequestJobFactory::ProtocolHandler {
|
|
|
|
public:
|
2018-04-18 01:44:10 +00:00
|
|
|
CustomProtocolHandler(v8::Isolate* isolate,
|
|
|
|
net::URLRequestContextGetter* request_context,
|
|
|
|
const Handler& handler)
|
2015-08-12 14:57:25 +00:00
|
|
|
: isolate_(isolate),
|
|
|
|
request_context_(request_context),
|
|
|
|
handler_(handler) {}
|
2015-08-12 05:30:19 +00:00
|
|
|
~CustomProtocolHandler() override {}
|
|
|
|
|
|
|
|
net::URLRequestJob* MaybeCreateJob(
|
|
|
|
net::URLRequest* request,
|
|
|
|
net::NetworkDelegate* network_delegate) const override {
|
2015-08-12 14:57:25 +00:00
|
|
|
RequestJob* request_job = new RequestJob(request, network_delegate);
|
2018-08-14 21:07:53 +00:00
|
|
|
request_job->SetHandlerInfo(isolate_, request_context_, handler_);
|
2015-08-12 14:57:25 +00:00
|
|
|
return request_job;
|
2015-08-12 05:30:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
v8::Isolate* isolate_;
|
2018-08-14 21:07:53 +00:00
|
|
|
net::URLRequestContextGetter* request_context_;
|
2015-08-12 05:30:19 +00:00
|
|
|
Protocol::Handler handler_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(CustomProtocolHandler);
|
|
|
|
};
|
2015-07-05 17:53:07 +00:00
|
|
|
|
2015-08-12 05:30:19 +00:00
|
|
|
// Register the protocol with certain request job.
|
2018-04-18 01:44:10 +00:00
|
|
|
template <typename RequestJob>
|
2015-08-13 11:26:18 +00:00
|
|
|
void RegisterProtocol(const std::string& scheme,
|
2015-08-12 05:30:19 +00:00
|
|
|
const Handler& handler,
|
2015-08-13 11:26:18 +00:00
|
|
|
mate::Arguments* args) {
|
|
|
|
CompletionCallback callback;
|
|
|
|
args->GetNext(&callback);
|
2018-10-04 18:08:56 +00:00
|
|
|
auto* getter = static_cast<URLRequestContextGetter*>(
|
|
|
|
browser_context_->GetRequestContext());
|
2019-01-12 01:00:43 +00:00
|
|
|
base::PostTaskWithTraitsAndReplyWithResult(
|
|
|
|
FROM_HERE, {content::BrowserThread::IO},
|
2018-03-30 13:24:55 +00:00
|
|
|
base::BindOnce(&Protocol::RegisterProtocolInIO<RequestJob>,
|
|
|
|
base::RetainedRef(getter), isolate(), scheme, handler),
|
|
|
|
base::BindOnce(&Protocol::OnIOCompleted, GetWeakPtr(), callback));
|
2015-08-12 05:30:19 +00:00
|
|
|
}
|
2018-04-18 01:44:10 +00:00
|
|
|
template <typename RequestJob>
|
2016-06-15 12:11:42 +00:00
|
|
|
static ProtocolError RegisterProtocolInIO(
|
2018-10-04 18:08:56 +00:00
|
|
|
scoped_refptr<URLRequestContextGetter> request_context_getter,
|
2016-06-15 12:11:42 +00:00
|
|
|
v8::Isolate* isolate,
|
|
|
|
const std::string& scheme,
|
|
|
|
const Handler& handler) {
|
2018-10-04 18:08:56 +00:00
|
|
|
auto* job_factory = request_context_getter->job_factory();
|
2016-06-15 12:11:42 +00:00
|
|
|
if (job_factory->IsHandledProtocol(scheme))
|
2019-05-03 18:11:41 +00:00
|
|
|
return ProtocolError::REGISTERED;
|
2018-06-18 07:32:55 +00:00
|
|
|
auto protocol_handler = std::make_unique<CustomProtocolHandler<RequestJob>>(
|
|
|
|
isolate, request_context_getter.get(), handler);
|
2016-06-15 12:11:42 +00:00
|
|
|
if (job_factory->SetProtocolHandler(scheme, std::move(protocol_handler)))
|
2019-05-03 18:11:41 +00:00
|
|
|
return ProtocolError::OK;
|
2015-08-12 05:30:19 +00:00
|
|
|
else
|
2019-05-03 18:11:41 +00:00
|
|
|
return ProtocolError::FAIL;
|
2015-08-12 05:30:19 +00:00
|
|
|
}
|
|
|
|
|
2015-08-13 11:33:53 +00:00
|
|
|
// Unregister the protocol handler that handles |scheme|.
|
2015-08-13 11:26:18 +00:00
|
|
|
void UnregisterProtocol(const std::string& scheme, mate::Arguments* args);
|
2016-06-15 12:11:42 +00:00
|
|
|
static ProtocolError UnregisterProtocolInIO(
|
2018-10-04 18:08:56 +00:00
|
|
|
scoped_refptr<URLRequestContextGetter> request_context_getter,
|
2016-06-15 12:11:42 +00:00
|
|
|
const std::string& scheme);
|
2015-08-12 13:32:52 +00:00
|
|
|
|
2015-08-13 11:33:53 +00:00
|
|
|
// Whether the protocol has handler registered.
|
2019-01-17 17:05:10 +00:00
|
|
|
v8::Local<v8::Promise> IsProtocolHandled(const std::string& scheme);
|
2015-08-13 11:33:53 +00:00
|
|
|
|
2015-08-13 12:10:05 +00:00
|
|
|
// Replace the protocol handler with a new one.
|
2018-04-18 01:44:10 +00:00
|
|
|
template <typename RequestJob>
|
2015-08-13 12:10:05 +00:00
|
|
|
void InterceptProtocol(const std::string& scheme,
|
|
|
|
const Handler& handler,
|
|
|
|
mate::Arguments* args) {
|
|
|
|
CompletionCallback callback;
|
|
|
|
args->GetNext(&callback);
|
2018-10-04 18:08:56 +00:00
|
|
|
auto* getter = static_cast<URLRequestContextGetter*>(
|
|
|
|
browser_context_->GetRequestContext());
|
2019-01-12 01:00:43 +00:00
|
|
|
base::PostTaskWithTraitsAndReplyWithResult(
|
|
|
|
FROM_HERE, {content::BrowserThread::IO},
|
2018-03-30 13:24:55 +00:00
|
|
|
base::BindOnce(&Protocol::InterceptProtocolInIO<RequestJob>,
|
|
|
|
base::RetainedRef(getter), isolate(), scheme, handler),
|
|
|
|
base::BindOnce(&Protocol::OnIOCompleted, GetWeakPtr(), callback));
|
2015-08-13 12:10:05 +00:00
|
|
|
}
|
2018-04-18 01:44:10 +00:00
|
|
|
template <typename RequestJob>
|
2016-06-15 12:11:42 +00:00
|
|
|
static ProtocolError InterceptProtocolInIO(
|
2018-10-04 18:08:56 +00:00
|
|
|
scoped_refptr<URLRequestContextGetter> request_context_getter,
|
2016-06-15 12:11:42 +00:00
|
|
|
v8::Isolate* isolate,
|
|
|
|
const std::string& scheme,
|
|
|
|
const Handler& handler) {
|
2018-10-04 18:08:56 +00:00
|
|
|
auto* job_factory = request_context_getter->job_factory();
|
2016-06-15 12:11:42 +00:00
|
|
|
if (!job_factory->IsHandledProtocol(scheme))
|
2019-05-03 18:11:41 +00:00
|
|
|
return ProtocolError::NOT_REGISTERED;
|
2018-08-13 23:16:04 +00:00
|
|
|
// It is possible a protocol is handled but can not be intercepted.
|
|
|
|
if (!job_factory->HasProtocolHandler(scheme))
|
2019-05-03 18:11:41 +00:00
|
|
|
return ProtocolError::FAIL;
|
2018-06-18 07:32:55 +00:00
|
|
|
auto protocol_handler = std::make_unique<CustomProtocolHandler<RequestJob>>(
|
|
|
|
isolate, request_context_getter.get(), handler);
|
2016-06-15 12:11:42 +00:00
|
|
|
if (!job_factory->InterceptProtocol(scheme, std::move(protocol_handler)))
|
2019-05-03 18:11:41 +00:00
|
|
|
return ProtocolError::INTERCEPTED;
|
|
|
|
return ProtocolError::OK;
|
2015-08-13 12:10:05 +00:00
|
|
|
}
|
|
|
|
|
2015-08-13 12:19:02 +00:00
|
|
|
// Restore the |scheme| to its original protocol handler.
|
|
|
|
void UninterceptProtocol(const std::string& scheme, mate::Arguments* args);
|
2016-06-15 12:11:42 +00:00
|
|
|
static ProtocolError UninterceptProtocolInIO(
|
2018-10-04 18:08:56 +00:00
|
|
|
scoped_refptr<URLRequestContextGetter> request_context_getter,
|
2016-06-15 12:11:42 +00:00
|
|
|
const std::string& scheme);
|
2015-08-13 12:19:02 +00:00
|
|
|
|
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
|
|
|
|
2018-04-18 01:44:10 +00:00
|
|
|
base::WeakPtr<Protocol> GetWeakPtr() { return weak_factory_.GetWeakPtr(); }
|
2015-08-13 12:10:05 +00:00
|
|
|
|
2018-03-30 13:24:55 +00:00
|
|
|
scoped_refptr<AtomBrowserContext> browser_context_;
|
2016-06-15 08:12:45 +00:00
|
|
|
base::WeakPtrFactory<Protocol> weak_factory_;
|
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_
|