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_SERVER_H_
16#define WEBSERVER_LIBWEBSERV_SERVER_H_
17
18// In our own Android.mk, we set these flags for ourselves.  However, for
19// libraries consuming libwebserv, they don't have any of that logic.  Leave us
20// with DBus bindings until the Binder interface is ready.
21#if !defined(WEBSERV_USE_DBUS) && !defined(WEBSERV_USE_BINDER)
22#define WEBSERV_USE_DBUS
23#endif
24
25#include <memory>
26#include <string>
27
28#include <base/callback.h>
29#include <base/macros.h>
30#include <libwebserv/export.h>
31
32#if defined(WEBSERV_USE_DBUS)
33#include <base/memory/ref_counted.h>
34#include <brillo/dbus/async_event_sequencer.h>
35#include <dbus/bus.h>
36#endif  // defined(WEBSERV_USE_DBUS)
37
38#if defined(WEBSERV_USE_BINDER)
39#include <brillo/message_loops/message_loop.h>
40#endif  // defined(WEBSERV_USE_BINDER)
41
42namespace libwebserv {
43
44class ProtocolHandler;
45
46// Top-level wrapper class around HTTP server and provides an interface to
47// the web server.
48class LIBWEBSERV_EXPORT Server {
49 public:
50  Server() = default;
51  virtual ~Server() = default;
52
53#if defined(WEBSERV_USE_DBUS)
54  // Establish a connection to the system webserver.
55  //
56  // |service_name| is the well known D-Bus name of the client's process, used
57  // to expose a callback D-Bus object the web server calls back with incoming
58  // requests.
59  // |on_server_online| and |on_server_offline| will notify the caller when the
60  // server comes up and down.
61  //
62  // Note that you can use the returned Server instance as if the webserver
63  // process is actually running (ignoring webserver crashes and restarts).
64  // All registered request handlers will simply be re-registered when the
65  // webserver appears again.
66  static std::unique_ptr<Server> ConnectToServerViaDBus(
67      const scoped_refptr<dbus::Bus>& bus,
68      const std::string& service_name,
69      const brillo::dbus_utils::AsyncEventSequencer::CompletionAction& cb,
70      const base::Closure& on_server_online,
71      const base::Closure& on_server_offline);
72#endif  // defined(WEBSERV_USE_DBUS)
73
74#if defined(WEBSERV_USE_BINDER)
75  // Establish a connection to the system webserver.
76  //
77  // |on_server_online| and |on_server_offline| will notify the caller when the
78  // server comes up and down.
79  //
80  // Note that you can use the returned Server instance as if the webserver
81  // process is actually running (ignoring webserver crashes and restarts).
82  // All registered request handlers will simply be re-registered when the
83  // webserver appears again.
84  static std::unique_ptr<Server> ConnectToServerViaBinder(
85      brillo::MessageLoop* message_loop,
86      const base::Closure& on_server_online,
87      const base::Closure& on_server_offline);
88#endif  // defined(WEBSERV_USE_BINDER)
89
90  // A helper method that returns the default handler for "http".
91  virtual ProtocolHandler* GetDefaultHttpHandler() = 0;
92
93  // A helper method that returns the default handler for "https".
94  virtual ProtocolHandler* GetDefaultHttpsHandler() = 0;
95
96  // Returns an existing protocol handler by name.  If the handler with the
97  // requested |name| does not exist, a new one will be created.
98  //
99  // The created handler is purely client side, and depends on the server
100  // being configured to open a corresponding handler with the given name.
101  // Because clients and the server come up asynchronously, we allow clients
102  // to register anticipated handlers before server starts up.
103  virtual ProtocolHandler* GetProtocolHandler(const std::string& name) = 0;
104
105  // Returns true if |this| is connected to the web server daemon via IPC.
106  virtual bool IsConnected() const = 0;
107
108  // Set a user-callback to be invoked when a protocol handler is connect to the
109  // server daemon.  Multiple calls to this method will overwrite previously set
110  // callbacks.
111  virtual void OnProtocolHandlerConnected(
112      const base::Callback<void(ProtocolHandler*)>& callback) = 0;
113
114  // Set a user-callback to be invoked when a protocol handler is disconnected
115  // from the server daemon (e.g. on shutdown).  Multiple calls to this method
116  // will overwrite previously set callbacks.
117  virtual void OnProtocolHandlerDisconnected(
118      const base::Callback<void(ProtocolHandler*)>& callback) = 0;
119
120  // Returns the default request timeout used to process incoming requests.
121  // The reply to an incoming request should be sent within this timeout or
122  // else the web server will automatically abort the connection. If the timeout
123  // is not set, the returned value will be base::TimeDelta::Max().
124  virtual base::TimeDelta GetDefaultRequestTimeout() const = 0;
125
126 private:
127  DISALLOW_COPY_AND_ASSIGN(Server);
128};
129
130}  // namespace libwebserv
131
132#endif  // WEBSERVER_LIBWEBSERV_SERVER_H_
133