1// Copyright 2016 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_BINDER_SERVER_H_
16#define WEBSERVER_LIBWEBSERV_BINDER_SERVER_H_
17
18#include <map>
19#include <memory>
20#include <string>
21#include <vector>
22
23#include <brillo/message_loops/message_loop.h>
24#include <base/bind.h>
25#include <base/macros.h>
26#include <base/memory/weak_ptr.h>
27
28#include <binderwrapper/binder_wrapper.h>
29#include <utils/StrongPointer.h>
30#include <binder/IBinder.h>
31
32#include "android/webservd/IProtocolHandler.h"
33#include "android/webservd/IServer.h"
34#include "libwebserv/server.h"
35
36namespace libwebserv {
37
38class ProtocolHandler;
39class BinderPHGroup;
40
41class LIBWEBSERV_PRIVATE BinderServer : public Server {
42 public:
43  BinderServer(brillo::MessageLoop* message_loop,
44               const base::Closure& on_server_online,
45               const base::Closure& on_server_offline,
46               android::BinderWrapper* binder_wrapper);
47  ~BinderServer() override = default;
48
49  ProtocolHandler* GetDefaultHttpHandler() override;
50  ProtocolHandler* GetDefaultHttpsHandler() override;
51  ProtocolHandler* GetProtocolHandler(const std::string& name) override;
52
53  bool IsConnected() const override;
54
55  void OnProtocolHandlerConnected(
56      const base::Callback<void(ProtocolHandler*)>& callback) override;
57  void OnProtocolHandlerDisconnected(
58      const base::Callback<void(ProtocolHandler*)>& callback) override;
59
60  base::TimeDelta GetDefaultRequestTimeout() const override;
61
62 private:
63  using RemoteServer = android::webservd::IServer;
64  using RemoteProtocolHandler = android::webservd::IProtocolHandler;
65
66  void TryConnecting();
67  void ClearLocalState();
68  bool BuildLocalState(android::sp<android::IBinder> server);
69
70  // Used to poll for webservd availability and notify the user of changes
71  brillo::MessageLoop* message_loop_;
72  base::Closure on_server_online_;
73  base::Closure on_server_offline_;
74  android::BinderWrapper* binder_wrapper_;
75
76  android::sp<RemoteServer> remote_server_;
77  //std::map<std::string, std::unique_ptr<BinderPHGroup>>
78  //    local_protocol_handlers_;
79
80  base::WeakPtrFactory<BinderServer> weak_ptr_factory_{this};
81  DISALLOW_COPY_AND_ASSIGN(BinderServer);
82};
83
84}  // namespace libwebserv
85
86#endif  // WEBSERVER_LIBWEBSERV_BINDER_SERVER_H_
87