blob: 70e5dcd7589a898607b1991def6dbed24f2f381d [file] [log] [blame]
Daniel Erat35f65872015-08-17 20:59:29 -06001// 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 Vakulenko039da312015-02-03 08:58:55 -080014
15#ifndef WEBSERVER_LIBWEBSERV_SERVER_H_
16#define WEBSERVER_LIBWEBSERV_SERVER_H_
17
18#include <map>
19#include <memory>
20#include <string>
21
Alex Vakulenko039da312015-02-03 08:58:55 -080022#include <base/macros.h>
Alex Vakulenko31a63792015-02-03 12:44:57 -080023#include <dbus/bus.h>
24#include <chromeos/dbus/async_event_sequencer.h>
25#include <chromeos/dbus/dbus_object.h>
Alex Vakulenko039da312015-02-03 08:58:55 -080026#include <libwebserv/export.h>
27#include <libwebserv/request_handler_interface.h>
28
Alex Vakulenko31a63792015-02-03 12:44:57 -080029namespace org {
30namespace chromium {
31namespace WebServer {
Alex Vakulenko039da312015-02-03 08:58:55 -080032
Alex Vakulenko31a63792015-02-03 12:44:57 -080033class ObjectManagerProxy;
34class ProtocolHandlerProxy;
35class RequestHandlerAdaptor;
36class ServerProxy;
37
38} // namespace WebServer
39} // namespace chromium
40} // namespace org
Alex Vakulenko039da312015-02-03 08:58:55 -080041
42namespace libwebserv {
43
44// Top-level wrapper class around HTTP server and provides an interface to
Alex Vakulenko31a63792015-02-03 12:44:57 -080045// the web server.
Alex Vakulenko039da312015-02-03 08:58:55 -080046class LIBWEBSERV_EXPORT Server final {
47 public:
48 Server();
49 ~Server();
50
Alex Vakulenko31a63792015-02-03 12:44:57 -080051 // 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 Vakulenko039da312015-02-03 08:58:55 -080065
Alex Vakulenko31a63792015-02-03 12:44:57 -080066 // Disconnects from the web server and removes the library interface from
67 // D-Bus.
68 void Disconnect();
Alex Vakulenko039da312015-02-03 08:58:55 -080069
Alex Vakulenko31a63792015-02-03 12:44:57 -080070 // A helper method that returns the default handler for "http".
Alex Vakulenkoa9352552015-02-17 12:49:59 -080071 ProtocolHandler* GetDefaultHttpHandler();
Alex Vakulenko039da312015-02-03 08:58:55 -080072
Alex Vakulenko31a63792015-02-03 12:44:57 -080073 // A helper method that returns the default handler for "https".
Alex Vakulenkoa9352552015-02-17 12:49:59 -080074 ProtocolHandler* GetDefaultHttpsHandler();
75
Alex Vakulenko3d2d5632015-03-02 15:58:32 -080076 // 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 Vakulenkoa9352552015-02-17 12:49:59 -080078 //
79 // The created handler is purely client side, and depends on the server
Alex Vakulenko3d2d5632015-03-02 15:58:32 -080080 // being configured to open a corresponding handler with the given name.
Alex Vakulenkoa9352552015-02-17 12:49:59 -080081 // Because clients and the server come up asynchronously, we allow clients
82 // to register anticipated handlers before server starts up.
Alex Vakulenko3d2d5632015-03-02 15:58:32 -080083 ProtocolHandler* GetProtocolHandler(const std::string& name);
Alex Vakulenko039da312015-02-03 08:58:55 -080084
Alex Vakulenko31a63792015-02-03 12:44:57 -080085 // 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 Vakulenko039da312015-02-03 08:58:55 -080088
Alex Vakulenko31a63792015-02-03 12:44:57 -080089 // 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 Vakulenko039da312015-02-03 08:58:55 -080094
Alex Vakulenko31a63792015-02-03 12:44:57 -080095 // 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 Vakulenko039da312015-02-03 08:58:55 -0800100
101 private:
Alex Vakulenko31a63792015-02-03 12:44:57 -0800102 friend class ProtocolHandler;
103 class RequestHandler;
Alex Vakulenko039da312015-02-03 08:58:55 -0800104
Alex Vakulenko31a63792015-02-03 12:44:57 -0800105 // Handler invoked when a connection is established to web server daemon.
106 LIBWEBSERV_PRIVATE void Online(org::chromium::WebServer::ServerProxy* server);
Alex Vakulenko039da312015-02-03 08:58:55 -0800107
Alex Vakulenko31a63792015-02-03 12:44:57 -0800108 // 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 Vakulenko3d2d5632015-03-02 15:58:32 -0800120 // 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 Vakulenko31a63792015-02-03 12:44:57 -0800124 // 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 Vakulenko3d2d5632015-03-02 15:58:32 -0800133 // A mapping of protocol handler name to the associated object.
134 std::map<std::string, std::unique_ptr<ProtocolHandler>>
135 protocol_handlers_names_;
Alex Vakulenko31a63792015-02-03 12:44:57 -0800136 // A mapping of protocol handler IDs to the associated object.
Alex Vakulenko3d2d5632015-03-02 15:58:32 -0800137 std::map<std::string, ProtocolHandler*> protocol_handlers_ids_;
Alex Vakulenko31a63792015-02-03 12:44:57 -0800138 // 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 Vakulenko039da312015-02-03 08:58:55 -0800158 DISALLOW_COPY_AND_ASSIGN(Server);
159};
160
161} // namespace libwebserv
162
163#endif // WEBSERVER_LIBWEBSERV_SERVER_H_