electron/atom/browser/api/atom_api_protocol.h

104 lines
3.5 KiB
C
Raw Normal View History

// 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>
#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-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-05 17:53:07 +00:00
// Callback called if protocol can be registered.
void OnRegisterProtocol(const std::string& scheme,
2015-07-07 14:43:13 +00:00
const JsProtocolHandler& handler,
const JsCompletionCallback& callback,
int is_handled);
2015-07-05 17:53:07 +00:00
// Callback called if protocol can be intercepted.
void OnInterceptProtocol(const std::string& scheme,
2015-07-07 14:43:13 +00:00
const JsProtocolHandler& handler,
const JsCompletionCallback& callback,
int is_handled);
2015-07-05 17:53:07 +00:00
// Register schemes to standard scheme list.
void RegisterStandardSchemes(const std::vector<std::string>& schemes);
2014-04-21 08:33:32 +00:00
// Register/unregister an networking |scheme| which would be handled by
// |callback|.
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
// Returns whether a scheme has been registered.
2015-07-05 17:53:07 +00:00
void IsHandledProtocol(const std::string& scheme,
2015-07-07 14:43:13 +00:00
const net::CompletionCallback& callback);
2014-04-21 08:33:32 +00:00
// Intercept/unintercept an existing protocol handler.
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.
void RegisterProtocolInIO(const std::string& scheme);
void UnregisterProtocolInIO(const std::string& scheme);
void InterceptProtocolInIO(const std::string& scheme);
void 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_