| Daniel Erat | 35f6587 | 2015-08-17 20:59:29 -0600 | [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. |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 14 | |
| 15 | #ifndef WEBSERVER_LIBWEBSERV_SERVER_H_ |
| 16 | #define WEBSERVER_LIBWEBSERV_SERVER_H_ |
| 17 | |
| 18 | #include <map> |
| 19 | #include <memory> |
| 20 | #include <string> |
| 21 | |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 22 | #include <base/macros.h> |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 23 | #include <dbus/bus.h> |
| 24 | #include <chromeos/dbus/async_event_sequencer.h> |
| 25 | #include <chromeos/dbus/dbus_object.h> |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 26 | #include <libwebserv/export.h> |
| 27 | #include <libwebserv/request_handler_interface.h> |
| 28 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 29 | namespace org { |
| 30 | namespace chromium { |
| 31 | namespace WebServer { |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 32 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 33 | class ObjectManagerProxy; |
| 34 | class ProtocolHandlerProxy; |
| 35 | class RequestHandlerAdaptor; |
| 36 | class ServerProxy; |
| 37 | |
| 38 | } // namespace WebServer |
| 39 | } // namespace chromium |
| 40 | } // namespace org |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 41 | |
| 42 | namespace libwebserv { |
| 43 | |
| 44 | // Top-level wrapper class around HTTP server and provides an interface to |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 45 | // the web server. |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 46 | class LIBWEBSERV_EXPORT Server final { |
| 47 | public: |
| 48 | Server(); |
| 49 | ~Server(); |
| 50 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 51 | // Establish a connection to the system webserver. |
| 52 | // |service_name| is the well known D-Bus name of the client's process, used |
| 53 | // to expose a callback D-Bus object the web server calls back with incoming |
| 54 | // requests. |
| 55 | // |on_server_online| and |on_server_offline| will notify the caller when the |
| 56 | // server comes up and down. |
| 57 | // Note that we can Connect() even before the webserver attaches to D-Bus, |
| 58 | // and appropriate state will be built up when the webserver appears on D-Bus. |
| 59 | void Connect( |
| 60 | const scoped_refptr<dbus::Bus>& bus, |
| 61 | const std::string& service_name, |
| 62 | const chromeos::dbus_utils::AsyncEventSequencer::CompletionAction& cb, |
| 63 | const base::Closure& on_server_online, |
| 64 | const base::Closure& on_server_offline); |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 65 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 66 | // Disconnects from the web server and removes the library interface from |
| 67 | // D-Bus. |
| 68 | void Disconnect(); |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 69 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 70 | // A helper method that returns the default handler for "http". |
| Alex Vakulenko | a935255 | 2015-02-17 12:49:59 -0800 | [diff] [blame] | 71 | ProtocolHandler* GetDefaultHttpHandler(); |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 72 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 73 | // A helper method that returns the default handler for "https". |
| Alex Vakulenko | a935255 | 2015-02-17 12:49:59 -0800 | [diff] [blame] | 74 | ProtocolHandler* GetDefaultHttpsHandler(); |
| 75 | |
| Alex Vakulenko | 3d2d563 | 2015-03-02 15:58:32 -0800 | [diff] [blame] | 76 | // Returns an existing protocol handler by name. If the handler with the |
| 77 | // requested |name| does not exist, a new one will be created. |
| Alex Vakulenko | a935255 | 2015-02-17 12:49:59 -0800 | [diff] [blame] | 78 | // |
| 79 | // The created handler is purely client side, and depends on the server |
| Alex Vakulenko | 3d2d563 | 2015-03-02 15:58:32 -0800 | [diff] [blame] | 80 | // being configured to open a corresponding handler with the given name. |
| Alex Vakulenko | a935255 | 2015-02-17 12:49:59 -0800 | [diff] [blame] | 81 | // Because clients and the server come up asynchronously, we allow clients |
| 82 | // to register anticipated handlers before server starts up. |
| Alex Vakulenko | 3d2d563 | 2015-03-02 15:58:32 -0800 | [diff] [blame] | 83 | ProtocolHandler* GetProtocolHandler(const std::string& name); |
| 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 | // Returns true if the web server daemon is connected to DBus and our |
| 86 | // connection to it has been established. |
| 87 | bool IsConnected() const { return proxy_ != nullptr; } |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 88 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 89 | // Set a user-callback to be invoked when a protocol handler is connect to the |
| 90 | // server daemon. Multiple calls to this method will overwrite previously set |
| 91 | // callbacks. |
| 92 | void OnProtocolHandlerConnected( |
| 93 | const base::Callback<void(ProtocolHandler*)>& callback); |
| 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 | // Set a user-callback to be invoked when a protocol handler is disconnected |
| 96 | // from the server daemon (e.g. on shutdown). Multiple calls to this method |
| 97 | // will overwrite previously set callbacks. |
| 98 | void OnProtocolHandlerDisconnected( |
| 99 | const base::Callback<void(ProtocolHandler*)>& callback); |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 100 | |
| 101 | private: |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 102 | friend class ProtocolHandler; |
| 103 | class RequestHandler; |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 104 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 105 | // Handler invoked when a connection is established to web server daemon. |
| 106 | LIBWEBSERV_PRIVATE void Online(org::chromium::WebServer::ServerProxy* server); |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 107 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 108 | // Handler invoked when the web server daemon connection is dropped. |
| 109 | LIBWEBSERV_PRIVATE void Offline(const dbus::ObjectPath& object_path); |
| 110 | |
| 111 | // Handler invoked when a new protocol handler D-Bus proxy object becomes |
| 112 | // available. |
| 113 | LIBWEBSERV_PRIVATE void ProtocolHandlerAdded( |
| 114 | org::chromium::WebServer::ProtocolHandlerProxy* handler); |
| 115 | |
| 116 | // Handler invoked when a protocol handler D-Bus proxy object disappears. |
| 117 | LIBWEBSERV_PRIVATE void ProtocolHandlerRemoved( |
| 118 | const dbus::ObjectPath& object_path); |
| 119 | |
| Alex Vakulenko | 3d2d563 | 2015-03-02 15:58:32 -0800 | [diff] [blame] | 120 | // Looks up a protocol handler by ID. If not found, returns nullptr. |
| 121 | LIBWEBSERV_PRIVATE ProtocolHandler* GetProtocolHandlerByID( |
| 122 | const std::string& id) const; |
| 123 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 124 | // Private implementation of D-Bus RequestHandlerInterface called by the web |
| 125 | // server daemon whenever a new request is available to be processed. |
| 126 | std::unique_ptr<RequestHandler> request_handler_; |
| 127 | // D-Bus object adaptor for RequestHandlerInterface. |
| 128 | std::unique_ptr<org::chromium::WebServer::RequestHandlerAdaptor> |
| 129 | dbus_adaptor_; |
| 130 | // D-Bus object to handler registration of RequestHandlerInterface. |
| 131 | std::unique_ptr<chromeos::dbus_utils::DBusObject> dbus_object_; |
| 132 | |
| Alex Vakulenko | 3d2d563 | 2015-03-02 15:58:32 -0800 | [diff] [blame] | 133 | // A mapping of protocol handler name to the associated object. |
| 134 | std::map<std::string, std::unique_ptr<ProtocolHandler>> |
| 135 | protocol_handlers_names_; |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 136 | // A mapping of protocol handler IDs to the associated object. |
| Alex Vakulenko | 3d2d563 | 2015-03-02 15:58:32 -0800 | [diff] [blame] | 137 | std::map<std::string, ProtocolHandler*> protocol_handlers_ids_; |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 138 | // A map between D-Bus object path of protocol handler and remote protocol |
| 139 | // handler ID. |
| 140 | std::map<dbus::ObjectPath, std::string> protocol_handler_id_map_; |
| 141 | |
| 142 | // User-specified callbacks for server and protocol handler life-time events. |
| 143 | base::Closure on_server_online_; |
| 144 | base::Closure on_server_offline_; |
| 145 | base::Callback<void(ProtocolHandler*)> on_protocol_handler_connected_; |
| 146 | base::Callback<void(ProtocolHandler*)> on_protocol_handler_disconnected_; |
| 147 | |
| 148 | // D-Bus object manager proxy that receives notification of web server |
| 149 | // daemon's D-Bus object creation and destruction. |
| 150 | std::unique_ptr<org::chromium::WebServer::ObjectManagerProxy> object_manager_; |
| 151 | |
| 152 | // D-Bus proxy for the web server main object. |
| 153 | org::chromium::WebServer::ServerProxy* proxy_{nullptr}; |
| 154 | |
| 155 | // D-Bus service name used by the daemon hosting this object. |
| 156 | std::string service_name_; |
| 157 | |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 158 | DISALLOW_COPY_AND_ASSIGN(Server); |
| 159 | }; |
| 160 | |
| 161 | } // namespace libwebserv |
| 162 | |
| 163 | #endif // WEBSERVER_LIBWEBSERV_SERVER_H_ |