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
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
#include "atom/browser/api/event_emitter.h"
|
|
|
|
#include "base/callback.h"
|
|
|
|
#include "native_mate/handle.h"
|
2015-07-07 14:43:13 +00:00
|
|
|
#include "net/base/completion_callback.h"
|
2014-04-21 08:33:32 +00:00
|
|
|
|
|
|
|
namespace net {
|
|
|
|
class URLRequest;
|
|
|
|
}
|
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 {
|
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
class Protocol : public mate::EventEmitter {
|
2013-08-24 07:26:10 +00:00
|
|
|
public:
|
2015-07-05 17:53:07 +00:00
|
|
|
using JsProtocolHandler =
|
2015-07-07 14:43:13 +00:00
|
|
|
base::Callback<v8::Local<v8::Value>(const net::URLRequest*)>;
|
|
|
|
using JsCompletionCallback = base::Callback<void(v8::Local<v8::Value>)>;
|
2014-04-21 08:33:32 +00:00
|
|
|
|
2015-07-09 09:18:45 +00:00
|
|
|
enum {
|
|
|
|
OK = 0,
|
|
|
|
ERR_SCHEME_REGISTERED,
|
|
|
|
ERR_SCHEME_UNREGISTERED,
|
|
|
|
ERR_SCHEME_INTERCEPTED,
|
|
|
|
ERR_SCHEME_UNINTERCEPTED,
|
|
|
|
ERR_NO_SCHEME,
|
|
|
|
ERR_SCHEME
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
|
|
|
JsProtocolHandler GetProtocolHandler(const std::string& scheme);
|
|
|
|
|
2015-06-18 09:18:11 +00:00
|
|
|
AtomBrowserContext* browser_context() const { return 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:
|
2014-04-21 08:33:32 +00:00
|
|
|
typedef std::map<std::string, JsProtocolHandler> ProtocolHandlersMap;
|
|
|
|
|
2015-07-09 09:18:45 +00:00
|
|
|
// Callback called after performing action on IO thread.
|
|
|
|
void OnIOActionCompleted(const JsCompletionCallback& callback,
|
|
|
|
int error);
|
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-07-09 09:18:45 +00:00
|
|
|
// Returns whether a scheme has been registered.
|
|
|
|
void IsHandledProtocol(const std::string& scheme,
|
|
|
|
const net::CompletionCallback& callback);
|
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
// Register/unregister an networking |scheme| which would be handled by
|
|
|
|
// |callback|.
|
2015-06-10 06:21:09 +00:00
|
|
|
void RegisterProtocol(v8::Isolate* isolate,
|
|
|
|
const std::string& scheme,
|
2015-07-07 14:43:13 +00:00
|
|
|
const JsProtocolHandler& handler,
|
|
|
|
const JsCompletionCallback& callback);
|
|
|
|
void UnregisterProtocol(v8::Isolate* isolate, const std::string& scheme,
|
|
|
|
const JsCompletionCallback& callback);
|
2014-04-21 08:33:32 +00:00
|
|
|
|
|
|
|
// Intercept/unintercept an existing protocol handler.
|
2015-06-10 06:21:09 +00:00
|
|
|
void InterceptProtocol(v8::Isolate* isolate,
|
|
|
|
const std::string& scheme,
|
2015-07-07 14:43:13 +00:00
|
|
|
const JsProtocolHandler& handler,
|
|
|
|
const JsCompletionCallback& callback);
|
|
|
|
void UninterceptProtocol(v8::Isolate* isolate, const std::string& scheme,
|
|
|
|
const JsCompletionCallback& callback);
|
2013-12-15 09:09:35 +00:00
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
// The networking related operations have to be done in IO thread.
|
2015-07-09 09:18:45 +00:00
|
|
|
int RegisterProtocolInIO(const std::string& scheme,
|
|
|
|
const JsProtocolHandler& handler);
|
|
|
|
int UnregisterProtocolInIO(const std::string& scheme);
|
|
|
|
int InterceptProtocolInIO(const std::string& scheme,
|
|
|
|
const JsProtocolHandler& handler);
|
|
|
|
int UninterceptProtocolInIO(const std::string& scheme);
|
2013-08-30 02:15:15 +00:00
|
|
|
|
2015-06-18 08:59:03 +00:00
|
|
|
AtomBrowserContext* browser_context_;
|
2014-04-21 08:33:32 +00:00
|
|
|
AtomURLRequestJobFactory* job_factory_;
|
|
|
|
ProtocolHandlersMap protocol_handlers_;
|
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_
|