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>
|
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
#include "atom/browser/api/event_emitter.h"
|
|
|
|
#include "base/callback.h"
|
|
|
|
#include "native_mate/handle.h"
|
|
|
|
|
|
|
|
namespace net {
|
|
|
|
class URLRequest;
|
|
|
|
}
|
2013-08-24 07:26:10 +00:00
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
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-05-22 11:11:22 +00:00
|
|
|
typedef base::Callback<v8::Local<v8::Value>(const net::URLRequest*)>
|
2014-04-21 08:33:32 +00:00
|
|
|
JsProtocolHandler;
|
|
|
|
|
|
|
|
static mate::Handle<Protocol> Create(v8::Isolate* isolate);
|
|
|
|
|
|
|
|
JsProtocolHandler GetProtocolHandler(const std::string& scheme);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
Protocol();
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
// 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,
|
2014-04-21 08:33:32 +00:00
|
|
|
const JsProtocolHandler& callback);
|
2015-06-10 06:21:09 +00:00
|
|
|
void UnregisterProtocol(v8::Isolate* isolate, const std::string& scheme);
|
2014-04-21 08:33:32 +00:00
|
|
|
|
|
|
|
// Returns whether a scheme has been registered.
|
|
|
|
// FIXME Should accept a callback and be asynchronous so we do not have to use
|
|
|
|
// locks.
|
|
|
|
bool IsHandledProtocol(const std::string& scheme);
|
|
|
|
|
|
|
|
// Intercept/unintercept an existing protocol handler.
|
2015-06-10 06:21:09 +00:00
|
|
|
void InterceptProtocol(v8::Isolate* isolate,
|
|
|
|
const std::string& scheme,
|
2014-04-21 08:33:32 +00:00
|
|
|
const JsProtocolHandler& callback);
|
2015-06-10 06:21:09 +00:00
|
|
|
void UninterceptProtocol(v8::Isolate* isolate, const std::string& scheme);
|
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
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
// Do protocol.emit(event, parameter) under UI thread.
|
|
|
|
void EmitEventInUI(const std::string& event, const std::string& parameter);
|
2013-08-24 08:38:19 +00:00
|
|
|
|
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_
|