blob: bb45204aca4859342619ae05ad99166a4e6d4eed [file] [log] [blame]
Alex Vakulenko039da312015-02-03 08:58:55 -08001// Copyright 2014 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef WEBSERVER_LIBWEBSERV_SERVER_H_
6#define WEBSERVER_LIBWEBSERV_SERVER_H_
7
8#include <map>
9#include <memory>
10#include <string>
11
Alex Vakulenko039da312015-02-03 08:58:55 -080012#include <base/macros.h>
Alex Vakulenko31a63792015-02-03 12:44:57 -080013#include <dbus/bus.h>
14#include <chromeos/dbus/async_event_sequencer.h>
15#include <chromeos/dbus/dbus_object.h>
Alex Vakulenko039da312015-02-03 08:58:55 -080016#include <libwebserv/export.h>
17#include <libwebserv/request_handler_interface.h>
18
Alex Vakulenko31a63792015-02-03 12:44:57 -080019namespace org {
20namespace chromium {
21namespace WebServer {
Alex Vakulenko039da312015-02-03 08:58:55 -080022
Alex Vakulenko31a63792015-02-03 12:44:57 -080023class ObjectManagerProxy;
24class ProtocolHandlerProxy;
25class RequestHandlerAdaptor;
26class ServerProxy;
27
28} // namespace WebServer
29} // namespace chromium
30} // namespace org
Alex Vakulenko039da312015-02-03 08:58:55 -080031
32namespace libwebserv {
33
34// Top-level wrapper class around HTTP server and provides an interface to
Alex Vakulenko31a63792015-02-03 12:44:57 -080035// the web server.
Alex Vakulenko039da312015-02-03 08:58:55 -080036class LIBWEBSERV_EXPORT Server final {
37 public:
38 Server();
39 ~Server();
40
Alex Vakulenko31a63792015-02-03 12:44:57 -080041 // Establish a connection to the system webserver.
42 // |service_name| is the well known D-Bus name of the client's process, used
43 // to expose a callback D-Bus object the web server calls back with incoming
44 // requests.
45 // |on_server_online| and |on_server_offline| will notify the caller when the
46 // server comes up and down.
47 // Note that we can Connect() even before the webserver attaches to D-Bus,
48 // and appropriate state will be built up when the webserver appears on D-Bus.
49 void Connect(
50 const scoped_refptr<dbus::Bus>& bus,
51 const std::string& service_name,
52 const chromeos::dbus_utils::AsyncEventSequencer::CompletionAction& cb,
53 const base::Closure& on_server_online,
54 const base::Closure& on_server_offline);
Alex Vakulenko039da312015-02-03 08:58:55 -080055
Alex Vakulenko31a63792015-02-03 12:44:57 -080056 // Disconnects from the web server and removes the library interface from
57 // D-Bus.
58 void Disconnect();
Alex Vakulenko039da312015-02-03 08:58:55 -080059
Alex Vakulenko31a63792015-02-03 12:44:57 -080060 // A helper method that returns the default handler for "http".
Alex Vakulenkoa9352552015-02-17 12:49:59 -080061 ProtocolHandler* GetDefaultHttpHandler();
Alex Vakulenko039da312015-02-03 08:58:55 -080062
Alex Vakulenko31a63792015-02-03 12:44:57 -080063 // A helper method that returns the default handler for "https".
Alex Vakulenkoa9352552015-02-17 12:49:59 -080064 ProtocolHandler* GetDefaultHttpsHandler();
65
Alex Vakulenko3d2d5632015-03-02 15:58:32 -080066 // Returns an existing protocol handler by name. If the handler with the
67 // requested |name| does not exist, a new one will be created.
Alex Vakulenkoa9352552015-02-17 12:49:59 -080068 //
69 // The created handler is purely client side, and depends on the server
Alex Vakulenko3d2d5632015-03-02 15:58:32 -080070 // being configured to open a corresponding handler with the given name.
Alex Vakulenkoa9352552015-02-17 12:49:59 -080071 // Because clients and the server come up asynchronously, we allow clients
72 // to register anticipated handlers before server starts up.
Alex Vakulenko3d2d5632015-03-02 15:58:32 -080073 ProtocolHandler* GetProtocolHandler(const std::string& name);
Alex Vakulenko039da312015-02-03 08:58:55 -080074
Alex Vakulenko31a63792015-02-03 12:44:57 -080075 // Returns true if the web server daemon is connected to DBus and our
76 // connection to it has been established.
77 bool IsConnected() const { return proxy_ != nullptr; }
Alex Vakulenko039da312015-02-03 08:58:55 -080078
Alex Vakulenko31a63792015-02-03 12:44:57 -080079 // Set a user-callback to be invoked when a protocol handler is connect to the
80 // server daemon. Multiple calls to this method will overwrite previously set
81 // callbacks.
82 void OnProtocolHandlerConnected(
83 const base::Callback<void(ProtocolHandler*)>& callback);
Alex Vakulenko039da312015-02-03 08:58:55 -080084
Alex Vakulenko31a63792015-02-03 12:44:57 -080085 // Set a user-callback to be invoked when a protocol handler is disconnected
86 // from the server daemon (e.g. on shutdown). Multiple calls to this method
87 // will overwrite previously set callbacks.
88 void OnProtocolHandlerDisconnected(
89 const base::Callback<void(ProtocolHandler*)>& callback);
Alex Vakulenko039da312015-02-03 08:58:55 -080090
91 private:
Alex Vakulenko31a63792015-02-03 12:44:57 -080092 friend class ProtocolHandler;
93 class RequestHandler;
Alex Vakulenko039da312015-02-03 08:58:55 -080094
Alex Vakulenko31a63792015-02-03 12:44:57 -080095 // Handler invoked when a connection is established to web server daemon.
96 LIBWEBSERV_PRIVATE void Online(org::chromium::WebServer::ServerProxy* server);
Alex Vakulenko039da312015-02-03 08:58:55 -080097
Alex Vakulenko31a63792015-02-03 12:44:57 -080098 // Handler invoked when the web server daemon connection is dropped.
99 LIBWEBSERV_PRIVATE void Offline(const dbus::ObjectPath& object_path);
100
101 // Handler invoked when a new protocol handler D-Bus proxy object becomes
102 // available.
103 LIBWEBSERV_PRIVATE void ProtocolHandlerAdded(
104 org::chromium::WebServer::ProtocolHandlerProxy* handler);
105
106 // Handler invoked when a protocol handler D-Bus proxy object disappears.
107 LIBWEBSERV_PRIVATE void ProtocolHandlerRemoved(
108 const dbus::ObjectPath& object_path);
109
Alex Vakulenko3d2d5632015-03-02 15:58:32 -0800110 // Looks up a protocol handler by ID. If not found, returns nullptr.
111 LIBWEBSERV_PRIVATE ProtocolHandler* GetProtocolHandlerByID(
112 const std::string& id) const;
113
Alex Vakulenko31a63792015-02-03 12:44:57 -0800114 // Private implementation of D-Bus RequestHandlerInterface called by the web
115 // server daemon whenever a new request is available to be processed.
116 std::unique_ptr<RequestHandler> request_handler_;
117 // D-Bus object adaptor for RequestHandlerInterface.
118 std::unique_ptr<org::chromium::WebServer::RequestHandlerAdaptor>
119 dbus_adaptor_;
120 // D-Bus object to handler registration of RequestHandlerInterface.
121 std::unique_ptr<chromeos::dbus_utils::DBusObject> dbus_object_;
122
Alex Vakulenko3d2d5632015-03-02 15:58:32 -0800123 // A mapping of protocol handler name to the associated object.
124 std::map<std::string, std::unique_ptr<ProtocolHandler>>
125 protocol_handlers_names_;
Alex Vakulenko31a63792015-02-03 12:44:57 -0800126 // A mapping of protocol handler IDs to the associated object.
Alex Vakulenko3d2d5632015-03-02 15:58:32 -0800127 std::map<std::string, ProtocolHandler*> protocol_handlers_ids_;
Alex Vakulenko31a63792015-02-03 12:44:57 -0800128 // A map between D-Bus object path of protocol handler and remote protocol
129 // handler ID.
130 std::map<dbus::ObjectPath, std::string> protocol_handler_id_map_;
131
132 // User-specified callbacks for server and protocol handler life-time events.
133 base::Closure on_server_online_;
134 base::Closure on_server_offline_;
135 base::Callback<void(ProtocolHandler*)> on_protocol_handler_connected_;
136 base::Callback<void(ProtocolHandler*)> on_protocol_handler_disconnected_;
137
138 // D-Bus object manager proxy that receives notification of web server
139 // daemon's D-Bus object creation and destruction.
140 std::unique_ptr<org::chromium::WebServer::ObjectManagerProxy> object_manager_;
141
142 // D-Bus proxy for the web server main object.
143 org::chromium::WebServer::ServerProxy* proxy_{nullptr};
144
145 // D-Bus service name used by the daemon hosting this object.
146 std::string service_name_;
147
Alex Vakulenko039da312015-02-03 08:58:55 -0800148 DISALLOW_COPY_AND_ASSIGN(Server);
149};
150
151} // namespace libwebserv
152
153#endif // WEBSERVER_LIBWEBSERV_SERVER_H_