blob: 5e8f88a0c726dbd8ae1682f68aa18c9bb3105af1 [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
66 // Returns an existing protocol handler by ID. If the handler with the
67 // requested |id| does not exist, a new one will be created. See
68 // documentation in ProtocolHandler about IDs and how they work with
69 // webservd.
70 //
71 // The created handler is purely client side, and depends on the server
72 // being configured to open a corresponding handler with the given ID.
73 // Because clients and the server come up asynchronously, we allow clients
74 // to register anticipated handlers before server starts up.
75 ProtocolHandler* GetProtocolHandler(const std::string& id);
Alex Vakulenko039da312015-02-03 08:58:55 -080076
Alex Vakulenko31a63792015-02-03 12:44:57 -080077 // Returns true if the web server daemon is connected to DBus and our
78 // connection to it has been established.
79 bool IsConnected() const { return proxy_ != nullptr; }
Alex Vakulenko039da312015-02-03 08:58:55 -080080
Alex Vakulenko31a63792015-02-03 12:44:57 -080081 // Set a user-callback to be invoked when a protocol handler is connect to the
82 // server daemon. Multiple calls to this method will overwrite previously set
83 // callbacks.
84 void OnProtocolHandlerConnected(
85 const base::Callback<void(ProtocolHandler*)>& callback);
Alex Vakulenko039da312015-02-03 08:58:55 -080086
Alex Vakulenko31a63792015-02-03 12:44:57 -080087 // Set a user-callback to be invoked when a protocol handler is disconnected
88 // from the server daemon (e.g. on shutdown). Multiple calls to this method
89 // will overwrite previously set callbacks.
90 void OnProtocolHandlerDisconnected(
91 const base::Callback<void(ProtocolHandler*)>& callback);
Alex Vakulenko039da312015-02-03 08:58:55 -080092
93 private:
Alex Vakulenko31a63792015-02-03 12:44:57 -080094 friend class ProtocolHandler;
95 class RequestHandler;
Alex Vakulenko039da312015-02-03 08:58:55 -080096
Alex Vakulenko31a63792015-02-03 12:44:57 -080097 // Handler invoked when a connection is established to web server daemon.
98 LIBWEBSERV_PRIVATE void Online(org::chromium::WebServer::ServerProxy* server);
Alex Vakulenko039da312015-02-03 08:58:55 -080099
Alex Vakulenko31a63792015-02-03 12:44:57 -0800100 // Handler invoked when the web server daemon connection is dropped.
101 LIBWEBSERV_PRIVATE void Offline(const dbus::ObjectPath& object_path);
102
103 // Handler invoked when a new protocol handler D-Bus proxy object becomes
104 // available.
105 LIBWEBSERV_PRIVATE void ProtocolHandlerAdded(
106 org::chromium::WebServer::ProtocolHandlerProxy* handler);
107
108 // Handler invoked when a protocol handler D-Bus proxy object disappears.
109 LIBWEBSERV_PRIVATE void ProtocolHandlerRemoved(
110 const dbus::ObjectPath& object_path);
111
Alex Vakulenko31a63792015-02-03 12:44:57 -0800112 // Private implementation of D-Bus RequestHandlerInterface called by the web
113 // server daemon whenever a new request is available to be processed.
114 std::unique_ptr<RequestHandler> request_handler_;
115 // D-Bus object adaptor for RequestHandlerInterface.
116 std::unique_ptr<org::chromium::WebServer::RequestHandlerAdaptor>
117 dbus_adaptor_;
118 // D-Bus object to handler registration of RequestHandlerInterface.
119 std::unique_ptr<chromeos::dbus_utils::DBusObject> dbus_object_;
120
121 // A mapping of protocol handler IDs to the associated object.
122 // Handler IDs are either GUIDs or the two well-known handler IDs.
123 std::map<std::string, std::unique_ptr<ProtocolHandler>> protocol_handlers_;
124 // A map between D-Bus object path of protocol handler and remote protocol
125 // handler ID.
126 std::map<dbus::ObjectPath, std::string> protocol_handler_id_map_;
127
128 // User-specified callbacks for server and protocol handler life-time events.
129 base::Closure on_server_online_;
130 base::Closure on_server_offline_;
131 base::Callback<void(ProtocolHandler*)> on_protocol_handler_connected_;
132 base::Callback<void(ProtocolHandler*)> on_protocol_handler_disconnected_;
133
134 // D-Bus object manager proxy that receives notification of web server
135 // daemon's D-Bus object creation and destruction.
136 std::unique_ptr<org::chromium::WebServer::ObjectManagerProxy> object_manager_;
137
138 // D-Bus proxy for the web server main object.
139 org::chromium::WebServer::ServerProxy* proxy_{nullptr};
140
141 // D-Bus service name used by the daemon hosting this object.
142 std::string service_name_;
143
Alex Vakulenko039da312015-02-03 08:58:55 -0800144 DISALLOW_COPY_AND_ASSIGN(Server);
145};
146
147} // namespace libwebserv
148
149#endif // WEBSERVER_LIBWEBSERV_SERVER_H_