| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 1 | // 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 Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 12 | #include <base/macros.h> |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 13 | #include <dbus/bus.h> |
| 14 | #include <chromeos/dbus/async_event_sequencer.h> |
| 15 | #include <chromeos/dbus/dbus_object.h> |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 16 | #include <libwebserv/export.h> |
| 17 | #include <libwebserv/request_handler_interface.h> |
| 18 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 19 | namespace org { |
| 20 | namespace chromium { |
| 21 | namespace WebServer { |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 22 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 23 | class ObjectManagerProxy; |
| 24 | class ProtocolHandlerProxy; |
| 25 | class RequestHandlerAdaptor; |
| 26 | class ServerProxy; |
| 27 | |
| 28 | } // namespace WebServer |
| 29 | } // namespace chromium |
| 30 | } // namespace org |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 31 | |
| 32 | namespace libwebserv { |
| 33 | |
| 34 | // Top-level wrapper class around HTTP server and provides an interface to |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 35 | // the web server. |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 36 | class LIBWEBSERV_EXPORT Server final { |
| 37 | public: |
| 38 | Server(); |
| 39 | ~Server(); |
| 40 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 41 | // 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 Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 55 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 56 | // Disconnects from the web server and removes the library interface from |
| 57 | // D-Bus. |
| 58 | void Disconnect(); |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 59 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 60 | // A helper method that returns the default handler for "http". |
| Alex Vakulenko | a935255 | 2015-02-17 12:49:59 -0800 | [diff] [blame] | 61 | ProtocolHandler* GetDefaultHttpHandler(); |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 62 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 63 | // A helper method that returns the default handler for "https". |
| Alex Vakulenko | a935255 | 2015-02-17 12:49:59 -0800 | [diff] [blame] | 64 | ProtocolHandler* GetDefaultHttpsHandler(); |
| 65 | |
| Alex Vakulenko | 3d2d563 | 2015-03-02 15:58:32 -0800 | [diff] [blame^] | 66 | // 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 Vakulenko | a935255 | 2015-02-17 12:49:59 -0800 | [diff] [blame] | 68 | // |
| 69 | // The created handler is purely client side, and depends on the server |
| Alex Vakulenko | 3d2d563 | 2015-03-02 15:58:32 -0800 | [diff] [blame^] | 70 | // being configured to open a corresponding handler with the given name. |
| Alex Vakulenko | a935255 | 2015-02-17 12:49:59 -0800 | [diff] [blame] | 71 | // Because clients and the server come up asynchronously, we allow clients |
| 72 | // to register anticipated handlers before server starts up. |
| Alex Vakulenko | 3d2d563 | 2015-03-02 15:58:32 -0800 | [diff] [blame^] | 73 | ProtocolHandler* GetProtocolHandler(const std::string& name); |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 74 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 75 | // 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 Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 78 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 79 | // 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 Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 84 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 85 | // 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 Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 90 | |
| 91 | private: |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 92 | friend class ProtocolHandler; |
| 93 | class RequestHandler; |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 94 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 95 | // Handler invoked when a connection is established to web server daemon. |
| 96 | LIBWEBSERV_PRIVATE void Online(org::chromium::WebServer::ServerProxy* server); |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 97 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 98 | // 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 Vakulenko | 3d2d563 | 2015-03-02 15:58:32 -0800 | [diff] [blame^] | 110 | // 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 Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 114 | // 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 Vakulenko | 3d2d563 | 2015-03-02 15:58:32 -0800 | [diff] [blame^] | 123 | // A mapping of protocol handler name to the associated object. |
| 124 | std::map<std::string, std::unique_ptr<ProtocolHandler>> |
| 125 | protocol_handlers_names_; |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 126 | // A mapping of protocol handler IDs to the associated object. |
| Alex Vakulenko | 3d2d563 | 2015-03-02 15:58:32 -0800 | [diff] [blame^] | 127 | std::map<std::string, ProtocolHandler*> protocol_handlers_ids_; |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 128 | // 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 Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 148 | DISALLOW_COPY_AND_ASSIGN(Server); |
| 149 | }; |
| 150 | |
| 151 | } // namespace libwebserv |
| 152 | |
| 153 | #endif // WEBSERVER_LIBWEBSERV_SERVER_H_ |