16a460fede07b768210d256191cd126013a0df273Christopher Wiley// Copyright 2015 The Android Open Source Project 26a460fede07b768210d256191cd126013a0df273Christopher Wiley// 36a460fede07b768210d256191cd126013a0df273Christopher Wiley// Licensed under the Apache License, Version 2.0 (the "License"); 46a460fede07b768210d256191cd126013a0df273Christopher Wiley// you may not use this file except in compliance with the License. 56a460fede07b768210d256191cd126013a0df273Christopher Wiley// You may obtain a copy of the License at 66a460fede07b768210d256191cd126013a0df273Christopher Wiley// 76a460fede07b768210d256191cd126013a0df273Christopher Wiley// http://www.apache.org/licenses/LICENSE-2.0 86a460fede07b768210d256191cd126013a0df273Christopher Wiley// 96a460fede07b768210d256191cd126013a0df273Christopher Wiley// Unless required by applicable law or agreed to in writing, software 106a460fede07b768210d256191cd126013a0df273Christopher Wiley// distributed under the License is distributed on an "AS IS" BASIS, 116a460fede07b768210d256191cd126013a0df273Christopher Wiley// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 126a460fede07b768210d256191cd126013a0df273Christopher Wiley// See the License for the specific language governing permissions and 136a460fede07b768210d256191cd126013a0df273Christopher Wiley// limitations under the License. 146a460fede07b768210d256191cd126013a0df273Christopher Wiley 156a460fede07b768210d256191cd126013a0df273Christopher Wiley#ifndef WEBSERVER_LIBWEBSERV_DBUS_SERVER_H_ 166a460fede07b768210d256191cd126013a0df273Christopher Wiley#define WEBSERVER_LIBWEBSERV_DBUS_SERVER_H_ 176a460fede07b768210d256191cd126013a0df273Christopher Wiley 186a460fede07b768210d256191cd126013a0df273Christopher Wiley#include <map> 196a460fede07b768210d256191cd126013a0df273Christopher Wiley#include <memory> 206a460fede07b768210d256191cd126013a0df273Christopher Wiley#include <string> 216a460fede07b768210d256191cd126013a0df273Christopher Wiley 226a460fede07b768210d256191cd126013a0df273Christopher Wiley#include <base/macros.h> 236a460fede07b768210d256191cd126013a0df273Christopher Wiley#include <dbus/bus.h> 246a460fede07b768210d256191cd126013a0df273Christopher Wiley#include <brillo/dbus/async_event_sequencer.h> 256a460fede07b768210d256191cd126013a0df273Christopher Wiley#include <brillo/dbus/dbus_object.h> 266a460fede07b768210d256191cd126013a0df273Christopher Wiley 276a460fede07b768210d256191cd126013a0df273Christopher Wiley#include "libwebserv/export.h" 286a460fede07b768210d256191cd126013a0df273Christopher Wiley#include "libwebserv/request_handler_interface.h" 296a460fede07b768210d256191cd126013a0df273Christopher Wiley#include "libwebserv/server.h" 306a460fede07b768210d256191cd126013a0df273Christopher Wiley 316a460fede07b768210d256191cd126013a0df273Christopher Wileynamespace org { 326a460fede07b768210d256191cd126013a0df273Christopher Wileynamespace chromium { 336a460fede07b768210d256191cd126013a0df273Christopher Wileynamespace WebServer { 346a460fede07b768210d256191cd126013a0df273Christopher Wiley 356a460fede07b768210d256191cd126013a0df273Christopher Wileyclass ObjectManagerProxy; 366a460fede07b768210d256191cd126013a0df273Christopher Wileyclass ProtocolHandlerProxyInterface; 376a460fede07b768210d256191cd126013a0df273Christopher Wileyclass RequestHandlerAdaptor; 386a460fede07b768210d256191cd126013a0df273Christopher Wileyclass ServerProxyInterface; 396a460fede07b768210d256191cd126013a0df273Christopher Wiley 406a460fede07b768210d256191cd126013a0df273Christopher Wiley} // namespace WebServer 416a460fede07b768210d256191cd126013a0df273Christopher Wiley} // namespace chromium 426a460fede07b768210d256191cd126013a0df273Christopher Wiley} // namespace org 436a460fede07b768210d256191cd126013a0df273Christopher Wiley 446a460fede07b768210d256191cd126013a0df273Christopher Wileynamespace libwebserv { 456a460fede07b768210d256191cd126013a0df273Christopher Wiley 463320726e9ccf678a9accdd6994e28f0cb8454eabChristopher Wileyclass DBusProtocolHandler; 473320726e9ccf678a9accdd6994e28f0cb8454eabChristopher Wiley 486a460fede07b768210d256191cd126013a0df273Christopher Wileyclass LIBWEBSERV_PRIVATE DBusServer : public Server { 496a460fede07b768210d256191cd126013a0df273Christopher Wiley public: 506a460fede07b768210d256191cd126013a0df273Christopher Wiley DBusServer(); 516a460fede07b768210d256191cd126013a0df273Christopher Wiley ~DBusServer() override = default; 526a460fede07b768210d256191cd126013a0df273Christopher Wiley 536a460fede07b768210d256191cd126013a0df273Christopher Wiley // Establish a connection to the system webserver. 546a460fede07b768210d256191cd126013a0df273Christopher Wiley // |service_name| is the well known D-Bus name of the client's process, used 556a460fede07b768210d256191cd126013a0df273Christopher Wiley // to expose a callback D-Bus object the web server calls back with incoming 566a460fede07b768210d256191cd126013a0df273Christopher Wiley // requests. 576a460fede07b768210d256191cd126013a0df273Christopher Wiley // |on_server_online| and |on_server_offline| will notify the caller when the 586a460fede07b768210d256191cd126013a0df273Christopher Wiley // server comes up and down. 596a460fede07b768210d256191cd126013a0df273Christopher Wiley // Note that we can Connect() even before the webserver attaches to D-Bus, 606a460fede07b768210d256191cd126013a0df273Christopher Wiley // and appropriate state will be built up when the webserver appears on D-Bus. 616a460fede07b768210d256191cd126013a0df273Christopher Wiley void Connect( 626a460fede07b768210d256191cd126013a0df273Christopher Wiley const scoped_refptr<dbus::Bus>& bus, 636a460fede07b768210d256191cd126013a0df273Christopher Wiley const std::string& service_name, 646a460fede07b768210d256191cd126013a0df273Christopher Wiley const brillo::dbus_utils::AsyncEventSequencer::CompletionAction& cb, 656a460fede07b768210d256191cd126013a0df273Christopher Wiley const base::Closure& on_server_online, 666a460fede07b768210d256191cd126013a0df273Christopher Wiley const base::Closure& on_server_offline); 676a460fede07b768210d256191cd126013a0df273Christopher Wiley 686a460fede07b768210d256191cd126013a0df273Christopher Wiley ProtocolHandler* GetDefaultHttpHandler() override; 696a460fede07b768210d256191cd126013a0df273Christopher Wiley 706a460fede07b768210d256191cd126013a0df273Christopher Wiley ProtocolHandler* GetDefaultHttpsHandler() override; 716a460fede07b768210d256191cd126013a0df273Christopher Wiley ProtocolHandler* GetProtocolHandler(const std::string& name) override; 726a460fede07b768210d256191cd126013a0df273Christopher Wiley 736a460fede07b768210d256191cd126013a0df273Christopher Wiley bool IsConnected() const override; 746a460fede07b768210d256191cd126013a0df273Christopher Wiley 756a460fede07b768210d256191cd126013a0df273Christopher Wiley void OnProtocolHandlerConnected( 766a460fede07b768210d256191cd126013a0df273Christopher Wiley const base::Callback<void(ProtocolHandler*)>& callback) override; 776a460fede07b768210d256191cd126013a0df273Christopher Wiley 786a460fede07b768210d256191cd126013a0df273Christopher Wiley void OnProtocolHandlerDisconnected( 796a460fede07b768210d256191cd126013a0df273Christopher Wiley const base::Callback<void(ProtocolHandler*)>& callback) override; 806a460fede07b768210d256191cd126013a0df273Christopher Wiley 816a460fede07b768210d256191cd126013a0df273Christopher Wiley base::TimeDelta GetDefaultRequestTimeout() const override; 826a460fede07b768210d256191cd126013a0df273Christopher Wiley 836a460fede07b768210d256191cd126013a0df273Christopher Wiley private: 843320726e9ccf678a9accdd6994e28f0cb8454eabChristopher Wiley friend class DBusProtocolHandler; 856a460fede07b768210d256191cd126013a0df273Christopher Wiley class RequestHandler; 866a460fede07b768210d256191cd126013a0df273Christopher Wiley 876a460fede07b768210d256191cd126013a0df273Christopher Wiley // Handler invoked when a connection is established to web server daemon. 886a460fede07b768210d256191cd126013a0df273Christopher Wiley void Online(org::chromium::WebServer::ServerProxyInterface* server); 896a460fede07b768210d256191cd126013a0df273Christopher Wiley 906a460fede07b768210d256191cd126013a0df273Christopher Wiley // Handler invoked when the web server daemon connection is dropped. 916a460fede07b768210d256191cd126013a0df273Christopher Wiley void Offline(const dbus::ObjectPath& object_path); 926a460fede07b768210d256191cd126013a0df273Christopher Wiley 936a460fede07b768210d256191cd126013a0df273Christopher Wiley // Handler invoked when a new protocol handler D-Bus proxy object becomes 946a460fede07b768210d256191cd126013a0df273Christopher Wiley // available. 956a460fede07b768210d256191cd126013a0df273Christopher Wiley void ProtocolHandlerAdded( 966a460fede07b768210d256191cd126013a0df273Christopher Wiley org::chromium::WebServer::ProtocolHandlerProxyInterface* handler); 976a460fede07b768210d256191cd126013a0df273Christopher Wiley 986a460fede07b768210d256191cd126013a0df273Christopher Wiley // Handler invoked when a protocol handler D-Bus proxy object disappears. 996a460fede07b768210d256191cd126013a0df273Christopher Wiley void ProtocolHandlerRemoved( 1006a460fede07b768210d256191cd126013a0df273Christopher Wiley const dbus::ObjectPath& object_path); 1016a460fede07b768210d256191cd126013a0df273Christopher Wiley 1026a460fede07b768210d256191cd126013a0df273Christopher Wiley // Looks up a protocol handler by ID. If not found, returns nullptr. 1033320726e9ccf678a9accdd6994e28f0cb8454eabChristopher Wiley DBusProtocolHandler* GetProtocolHandlerByID( 1046a460fede07b768210d256191cd126013a0df273Christopher Wiley const std::string& id) const; 1056a460fede07b768210d256191cd126013a0df273Christopher Wiley 1063320726e9ccf678a9accdd6994e28f0cb8454eabChristopher Wiley // Like the public version, but returns our specific handler type. 1073320726e9ccf678a9accdd6994e28f0cb8454eabChristopher Wiley DBusProtocolHandler* GetProtocolHandlerImpl(const std::string& name); 1083320726e9ccf678a9accdd6994e28f0cb8454eabChristopher Wiley 1096a460fede07b768210d256191cd126013a0df273Christopher Wiley // Private implementation of D-Bus RequestHandlerInterface called by the web 1106a460fede07b768210d256191cd126013a0df273Christopher Wiley // server daemon whenever a new request is available to be processed. 1116a460fede07b768210d256191cd126013a0df273Christopher Wiley std::unique_ptr<RequestHandler> request_handler_; 112ffc49cf8f42832a34e4eb29e82b8bb141a73c74fAlex Deymo 113ffc49cf8f42832a34e4eb29e82b8bb141a73c74fAlex Deymo // D-Bus object to handler registration of RequestHandlerInterface. 114ffc49cf8f42832a34e4eb29e82b8bb141a73c74fAlex Deymo std::unique_ptr<brillo::dbus_utils::DBusObject> dbus_object_; 115ffc49cf8f42832a34e4eb29e82b8bb141a73c74fAlex Deymo 1166a460fede07b768210d256191cd126013a0df273Christopher Wiley // D-Bus object adaptor for RequestHandlerInterface. 1176a460fede07b768210d256191cd126013a0df273Christopher Wiley std::unique_ptr<org::chromium::WebServer::RequestHandlerAdaptor> 1186a460fede07b768210d256191cd126013a0df273Christopher Wiley dbus_adaptor_; 119ffc49cf8f42832a34e4eb29e82b8bb141a73c74fAlex Deymo 120ffc49cf8f42832a34e4eb29e82b8bb141a73c74fAlex Deymo // D-Bus object manager proxy that receives notification of web server 121ffc49cf8f42832a34e4eb29e82b8bb141a73c74fAlex Deymo // daemon's D-Bus object creation and destruction. 122ffc49cf8f42832a34e4eb29e82b8bb141a73c74fAlex Deymo std::unique_ptr<org::chromium::WebServer::ObjectManagerProxy> object_manager_; 1236a460fede07b768210d256191cd126013a0df273Christopher Wiley 1246a460fede07b768210d256191cd126013a0df273Christopher Wiley // A mapping of protocol handler name to the associated object. 1253320726e9ccf678a9accdd6994e28f0cb8454eabChristopher Wiley std::map<std::string, std::unique_ptr<DBusProtocolHandler>> 1266a460fede07b768210d256191cd126013a0df273Christopher Wiley protocol_handlers_names_; 1276a460fede07b768210d256191cd126013a0df273Christopher Wiley // A mapping of protocol handler IDs to the associated object. 1283320726e9ccf678a9accdd6994e28f0cb8454eabChristopher Wiley std::map<std::string, DBusProtocolHandler*> protocol_handlers_ids_; 1296a460fede07b768210d256191cd126013a0df273Christopher Wiley // A map between D-Bus object path of protocol handler and remote protocol 1306a460fede07b768210d256191cd126013a0df273Christopher Wiley // handler ID. 1316a460fede07b768210d256191cd126013a0df273Christopher Wiley std::map<dbus::ObjectPath, std::string> protocol_handler_id_map_; 1326a460fede07b768210d256191cd126013a0df273Christopher Wiley 1336a460fede07b768210d256191cd126013a0df273Christopher Wiley // User-specified callbacks for server and protocol handler life-time events. 1346a460fede07b768210d256191cd126013a0df273Christopher Wiley base::Closure on_server_online_; 1356a460fede07b768210d256191cd126013a0df273Christopher Wiley base::Closure on_server_offline_; 1366a460fede07b768210d256191cd126013a0df273Christopher Wiley base::Callback<void(ProtocolHandler*)> on_protocol_handler_connected_; 1376a460fede07b768210d256191cd126013a0df273Christopher Wiley base::Callback<void(ProtocolHandler*)> on_protocol_handler_disconnected_; 1386a460fede07b768210d256191cd126013a0df273Christopher Wiley 1396a460fede07b768210d256191cd126013a0df273Christopher Wiley // D-Bus proxy for the web server main object. 1406a460fede07b768210d256191cd126013a0df273Christopher Wiley org::chromium::WebServer::ServerProxyInterface* proxy_{nullptr}; 1416a460fede07b768210d256191cd126013a0df273Christopher Wiley 1426a460fede07b768210d256191cd126013a0df273Christopher Wiley // D-Bus service name used by the daemon hosting this object. 1436a460fede07b768210d256191cd126013a0df273Christopher Wiley std::string service_name_; 1446a460fede07b768210d256191cd126013a0df273Christopher Wiley 1456a460fede07b768210d256191cd126013a0df273Christopher Wiley DISALLOW_COPY_AND_ASSIGN(DBusServer); 1466a460fede07b768210d256191cd126013a0df273Christopher Wiley}; 1476a460fede07b768210d256191cd126013a0df273Christopher Wiley 1486a460fede07b768210d256191cd126013a0df273Christopher Wiley} // namespace libwebserv 1496a460fede07b768210d256191cd126013a0df273Christopher Wiley 1506a460fede07b768210d256191cd126013a0df273Christopher Wiley#endif // WEBSERVER_LIBWEBSERV_DBUS_SERVER_H_ 151