File: presentation_controller.h

package info (click to toggle)
android-platform-tools 35.0.2-1~exp6
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 211,716 kB
  • sloc: cpp: 995,749; java: 290,495; ansic: 145,647; xml: 58,531; python: 39,608; sh: 14,500; javascript: 5,198; asm: 4,866; makefile: 3,115; yacc: 769; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (223 lines) | stat: -rw-r--r-- 8,467 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef OSP_PUBLIC_PRESENTATION_PRESENTATION_CONTROLLER_H_
#define OSP_PUBLIC_PRESENTATION_PRESENTATION_CONTROLLER_H_

#include <map>
#include <memory>
#include <string>

#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "osp/public/presentation/presentation_connection.h"
#include "osp/public/protocol_connection.h"
#include "osp/public/service_listener.h"
#include "platform/api/time.h"
#include "platform/base/error.h"

namespace openscreen {
namespace osp {

class UrlAvailabilityRequester;

class RequestDelegate {
 public:
  virtual ~RequestDelegate() = default;

  virtual void OnConnection(std::unique_ptr<Connection> connection) = 0;
  virtual void OnError(const Error& error) = 0;
};

class ReceiverObserver {
 public:
  virtual ~ReceiverObserver() = default;

  // Called when there is an unrecoverable error in requesting availability.
  // This means the availability is unknown and there is no further response to
  // wait for.
  virtual void OnRequestFailed(const std::string& presentation_url,
                               const std::string& service_id) = 0;

  // Called when receivers compatible with |presentation_url| are known to be
  // available.
  virtual void OnReceiverAvailable(const std::string& presentation_url,
                                   const std::string& service_id) = 0;
  // Only called for |service_id| values previously advertised as available.
  virtual void OnReceiverUnavailable(const std::string& presentation_url,
                                     const std::string& service_id) = 0;
};

class Controller final : public ServiceListener::Observer,
                         public Connection::ParentDelegate {
 public:
  class ReceiverWatch {
   public:
    ReceiverWatch();
    ReceiverWatch(Controller* controller,
                  const std::vector<std::string>& urls,
                  ReceiverObserver* observer);
    ReceiverWatch(ReceiverWatch&&) noexcept;
    ~ReceiverWatch();

    ReceiverWatch& operator=(ReceiverWatch);

    explicit operator bool() const { return observer_; }

    friend void swap(ReceiverWatch& a, ReceiverWatch& b);

   private:
    std::vector<std::string> urls_;
    ReceiverObserver* observer_ = nullptr;
    Controller* controller_ = nullptr;
  };

  class ConnectRequest {
   public:
    ConnectRequest();
    ConnectRequest(Controller* controller,
                   const std::string& service_id,
                   bool is_reconnect,
                   absl::optional<uint64_t> request_id);
    ConnectRequest(ConnectRequest&&) noexcept;
    ~ConnectRequest();

    ConnectRequest& operator=(ConnectRequest);

    explicit operator bool() const { return request_id_.has_value(); }

    friend void swap(ConnectRequest& a, ConnectRequest& b);

   private:
    std::string service_id_;
    bool is_reconnect_;
    absl::optional<uint64_t> request_id_;
    Controller* controller_;
  };

  explicit Controller(ClockNowFunctionPtr now_function);
  ~Controller();

  // Requests receivers compatible with all urls in |urls| and registers
  // |observer| for availability changes.  The screens will be a subset of the
  // screen list maintained by the ServiceListener.  Returns an RAII object that
  // tracks the registration.
  ReceiverWatch RegisterReceiverWatch(const std::vector<std::string>& urls,
                                      ReceiverObserver* observer);

  // Requests that a new presentation be created on |service_id| using
  // |presentation_url|, with the result passed to |delegate|.
  // |conn_delegate| is passed to the resulting connection.  The returned
  // ConnectRequest object may be destroyed before any |delegate| methods are
  // called to cancel the request.
  ConnectRequest StartPresentation(const std::string& url,
                                   const std::string& service_id,
                                   RequestDelegate* delegate,
                                   Connection::Delegate* conn_delegate);

  // Requests reconnection to the presentation with the given id and URL running
  // on |service_id|, with the result passed to |delegate|.  |conn_delegate| is
  // passed to the resulting connection.  The returned ConnectRequest object may
  // be destroyed before any |delegate| methods are called to cancel the
  // request.
  ConnectRequest ReconnectPresentation(const std::vector<std::string>& urls,
                                       const std::string& presentation_id,
                                       const std::string& service_id,
                                       RequestDelegate* delegate,
                                       Connection::Delegate* conn_delegate);

  // Requests reconnection with a previously-connected connection.  This both
  // avoids having to respecify the parameters and connection delegate but also
  // simplifies the implementation of the Presentation API requirement to return
  // the same connection object where possible.
  ConnectRequest ReconnectConnection(std::unique_ptr<Connection> connection,
                                     RequestDelegate* delegate);

  // Connection::ParentDelegate overrides.
  Error CloseConnection(Connection* connection,
                        Connection::CloseReason reason) override;

  // Also called by the embedder to report that a presentation has been
  // terminated.
  Error OnPresentationTerminated(const std::string& presentation_id,
                                 TerminationReason reason) override;

  void OnConnectionDestroyed(Connection* connection) override;

  // Returns an empty string if no such presentation ID is found.
  std::string GetServiceIdForPresentationId(
      const std::string& presentation_id) const;

  ProtocolConnection* GetConnectionRequestGroupStream(
      const std::string& service_id);

  // TODO(btolsch): still used?
  void SetConnectionRequestGroupStreamForTest(
      const std::string& service_id,
      std::unique_ptr<ProtocolConnection> stream);

 private:
  class TerminationListener;
  class MessageGroupStreams;

  struct ControlledPresentation {
    std::string service_id;
    std::string url;
    std::vector<Connection*> connections;
  };

  static std::string MakePresentationId(const std::string& url,
                                        const std::string& service_id);

  void AddConnection(Connection* connection);
  void OpenConnection(uint64_t connection_id,
                      uint64_t endpoint_id,
                      const std::string& service_id,
                      RequestDelegate* request_delegate,
                      std::unique_ptr<Connection>&& connection,
                      std::unique_ptr<ProtocolConnection>&& stream);

  void TerminatePresentationById(const std::string& presentation_id);

  // Cancels compatible receiver monitoring for the given |urls|, |observer|
  // pair.
  void CancelReceiverWatch(const std::vector<std::string>& urls,
                           ReceiverObserver* observer);

  // Cancels a presentation connect request for the given |request_id| if one is
  // pending.
  void CancelConnectRequest(const std::string& service_id,
                            bool is_reconnect,
                            uint64_t request_id);

  // ServiceListener::Observer overrides.
  void OnStarted() override;
  void OnStopped() override;
  void OnSuspended() override;
  void OnSearching() override;
  void OnReceiverAdded(const ServiceInfo& info) override;
  void OnReceiverChanged(const ServiceInfo& info) override;
  void OnReceiverRemoved(const ServiceInfo& info) override;
  void OnAllReceiversRemoved() override;
  void OnError(ServiceListenerError) override;
  void OnMetrics(ServiceListener::Metrics) override;

  std::map<std::string, uint64_t> next_connection_id_;

  std::map<std::string, ControlledPresentation> presentations_;

  std::unique_ptr<ConnectionManager> connection_manager_;

  std::unique_ptr<UrlAvailabilityRequester> availability_requester_;
  std::map<std::string, IPEndpoint> receiver_endpoints_;

  std::map<std::string, std::unique_ptr<MessageGroupStreams>> group_streams_;
  std::map<std::string, std::unique_ptr<TerminationListener>>
      termination_listener_by_id_;
};

}  // namespace osp
}  // namespace openscreen

#endif  // OSP_PUBLIC_PRESENTATION_PRESENTATION_CONTROLLER_H_