| Christopher Wiley | 6a460fe | 2015-12-15 11:29:57 -0800 | [diff] [blame] | 1 | // Copyright 2015 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #ifndef WEBSERVER_LIBWEBSERV_DBUS_SERVER_H_ |
| 16 | #define WEBSERVER_LIBWEBSERV_DBUS_SERVER_H_ |
| 17 | |
| 18 | #include <map> |
| 19 | #include <memory> |
| 20 | #include <string> |
| 21 | |
| 22 | #include <base/macros.h> |
| 23 | #include <dbus/bus.h> |
| 24 | #include <brillo/dbus/async_event_sequencer.h> |
| 25 | #include <brillo/dbus/dbus_object.h> |
| 26 | |
| 27 | #include "libwebserv/export.h" |
| 28 | #include "libwebserv/request_handler_interface.h" |
| 29 | #include "libwebserv/server.h" |
| 30 | |
| 31 | namespace org { |
| 32 | namespace chromium { |
| 33 | namespace WebServer { |
| 34 | |
| 35 | class ObjectManagerProxy; |
| 36 | class ProtocolHandlerProxyInterface; |
| 37 | class RequestHandlerAdaptor; |
| 38 | class ServerProxyInterface; |
| 39 | |
| 40 | } // namespace WebServer |
| 41 | } // namespace chromium |
| 42 | } // namespace org |
| 43 | |
| 44 | namespace libwebserv { |
| 45 | |
| Christopher Wiley | 3320726 | 2015-12-15 18:33:50 -0800 | [diff] [blame^] | 46 | class DBusProtocolHandler; |
| 47 | |
| Christopher Wiley | 6a460fe | 2015-12-15 11:29:57 -0800 | [diff] [blame] | 48 | class LIBWEBSERV_PRIVATE DBusServer : public Server { |
| 49 | public: |
| 50 | DBusServer(); |
| 51 | ~DBusServer() override = default; |
| 52 | |
| 53 | // Establish a connection to the system webserver. |
| 54 | // |service_name| is the well known D-Bus name of the client's process, used |
| 55 | // to expose a callback D-Bus object the web server calls back with incoming |
| 56 | // requests. |
| 57 | // |on_server_online| and |on_server_offline| will notify the caller when the |
| 58 | // server comes up and down. |
| 59 | // Note that we can Connect() even before the webserver attaches to D-Bus, |
| 60 | // and appropriate state will be built up when the webserver appears on D-Bus. |
| 61 | void Connect( |
| 62 | const scoped_refptr<dbus::Bus>& bus, |
| 63 | const std::string& service_name, |
| 64 | const brillo::dbus_utils::AsyncEventSequencer::CompletionAction& cb, |
| 65 | const base::Closure& on_server_online, |
| 66 | const base::Closure& on_server_offline); |
| 67 | |
| 68 | ProtocolHandler* GetDefaultHttpHandler() override; |
| 69 | |
| 70 | ProtocolHandler* GetDefaultHttpsHandler() override; |
| 71 | ProtocolHandler* GetProtocolHandler(const std::string& name) override; |
| 72 | |
| 73 | bool IsConnected() const override; |
| 74 | |
| 75 | void OnProtocolHandlerConnected( |
| 76 | const base::Callback<void(ProtocolHandler*)>& callback) override; |
| 77 | |
| 78 | void OnProtocolHandlerDisconnected( |
| 79 | const base::Callback<void(ProtocolHandler*)>& callback) override; |
| 80 | |
| 81 | base::TimeDelta GetDefaultRequestTimeout() const override; |
| 82 | |
| 83 | private: |
| Christopher Wiley | 3320726 | 2015-12-15 18:33:50 -0800 | [diff] [blame^] | 84 | friend class DBusProtocolHandler; |
| Christopher Wiley | 6a460fe | 2015-12-15 11:29:57 -0800 | [diff] [blame] | 85 | class RequestHandler; |
| 86 | |
| 87 | void Disconnect(); |
| 88 | |
| 89 | // Handler invoked when a connection is established to web server daemon. |
| 90 | void Online(org::chromium::WebServer::ServerProxyInterface* server); |
| 91 | |
| 92 | // Handler invoked when the web server daemon connection is dropped. |
| 93 | void Offline(const dbus::ObjectPath& object_path); |
| 94 | |
| 95 | // Handler invoked when a new protocol handler D-Bus proxy object becomes |
| 96 | // available. |
| 97 | void ProtocolHandlerAdded( |
| 98 | org::chromium::WebServer::ProtocolHandlerProxyInterface* handler); |
| 99 | |
| 100 | // Handler invoked when a protocol handler D-Bus proxy object disappears. |
| 101 | void ProtocolHandlerRemoved( |
| 102 | const dbus::ObjectPath& object_path); |
| 103 | |
| 104 | // Looks up a protocol handler by ID. If not found, returns nullptr. |
| Christopher Wiley | 3320726 | 2015-12-15 18:33:50 -0800 | [diff] [blame^] | 105 | DBusProtocolHandler* GetProtocolHandlerByID( |
| Christopher Wiley | 6a460fe | 2015-12-15 11:29:57 -0800 | [diff] [blame] | 106 | const std::string& id) const; |
| 107 | |
| Christopher Wiley | 3320726 | 2015-12-15 18:33:50 -0800 | [diff] [blame^] | 108 | // Like the public version, but returns our specific handler type. |
| 109 | DBusProtocolHandler* GetProtocolHandlerImpl(const std::string& name); |
| 110 | |
| Christopher Wiley | 6a460fe | 2015-12-15 11:29:57 -0800 | [diff] [blame] | 111 | // Private implementation of D-Bus RequestHandlerInterface called by the web |
| 112 | // server daemon whenever a new request is available to be processed. |
| 113 | std::unique_ptr<RequestHandler> request_handler_; |
| 114 | // D-Bus object adaptor for RequestHandlerInterface. |
| 115 | std::unique_ptr<org::chromium::WebServer::RequestHandlerAdaptor> |
| 116 | dbus_adaptor_; |
| 117 | // D-Bus object to handler registration of RequestHandlerInterface. |
| 118 | std::unique_ptr<brillo::dbus_utils::DBusObject> dbus_object_; |
| 119 | |
| 120 | // A mapping of protocol handler name to the associated object. |
| Christopher Wiley | 3320726 | 2015-12-15 18:33:50 -0800 | [diff] [blame^] | 121 | std::map<std::string, std::unique_ptr<DBusProtocolHandler>> |
| Christopher Wiley | 6a460fe | 2015-12-15 11:29:57 -0800 | [diff] [blame] | 122 | protocol_handlers_names_; |
| 123 | // A mapping of protocol handler IDs to the associated object. |
| Christopher Wiley | 3320726 | 2015-12-15 18:33:50 -0800 | [diff] [blame^] | 124 | std::map<std::string, DBusProtocolHandler*> protocol_handlers_ids_; |
| Christopher Wiley | 6a460fe | 2015-12-15 11:29:57 -0800 | [diff] [blame] | 125 | // A map between D-Bus object path of protocol handler and remote protocol |
| 126 | // handler ID. |
| 127 | std::map<dbus::ObjectPath, std::string> protocol_handler_id_map_; |
| 128 | |
| 129 | // User-specified callbacks for server and protocol handler life-time events. |
| 130 | base::Closure on_server_online_; |
| 131 | base::Closure on_server_offline_; |
| 132 | base::Callback<void(ProtocolHandler*)> on_protocol_handler_connected_; |
| 133 | base::Callback<void(ProtocolHandler*)> on_protocol_handler_disconnected_; |
| 134 | |
| 135 | // D-Bus object manager proxy that receives notification of web server |
| 136 | // daemon's D-Bus object creation and destruction. |
| 137 | std::unique_ptr<org::chromium::WebServer::ObjectManagerProxy> object_manager_; |
| 138 | |
| 139 | // D-Bus proxy for the web server main object. |
| 140 | org::chromium::WebServer::ServerProxyInterface* proxy_{nullptr}; |
| 141 | |
| 142 | // D-Bus service name used by the daemon hosting this object. |
| 143 | std::string service_name_; |
| 144 | |
| 145 | DISALLOW_COPY_AND_ASSIGN(DBusServer); |
| 146 | }; |
| 147 | |
| 148 | } // namespace libwebserv |
| 149 | |
| 150 | #endif // WEBSERVER_LIBWEBSERV_DBUS_SERVER_H_ |