electron/atom/browser/api/atom_api_protocol.h

80 lines
2.3 KiB
C
Raw Normal View History

2013-08-24 07:26:10 +00:00
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// 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:
2014-04-21 08:33:32 +00:00
typedef base::Callback<v8::Handle<v8::Value>(const net::URLRequest*)>
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|.
void RegisterProtocol(const std::string& scheme,
const JsProtocolHandler& callback);
void UnregisterProtocol(const std::string& scheme);
// 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.
void InterceptProtocol(const std::string& scheme,
const JsProtocolHandler& callback);
void UninterceptProtocol(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_