summaryrefslogtreecommitdiff
path: root/chromium/content/renderer/presentation/presentation_dispatcher.h
blob: e717cb1341f9063f6fa7b5986f687206041a6320 (plain)
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Copyright 2015 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 CONTENT_RENDERER_PRESENTATION_PRESENTATION_DISPATCHER_H_
#define CONTENT_RENDERER_PRESENTATION_PRESENTATION_DISPATCHER_H_

#include <stddef.h>
#include <stdint.h>

#include <deque>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <vector>

#include "base/compiler_specific.h"
#include "base/id_map.h"
#include "base/macros.h"
#include "content/common/content_export.h"
#include "content/public/common/presentation_connection_message.h"
#include "content/public/common/presentation_info.h"
#include "content/public/renderer/render_frame_observer.h"
#include "mojo/public/cpp/bindings/binding.h"
#include "third_party/WebKit/public/platform/modules/presentation/WebPresentationClient.h"
#include "third_party/WebKit/public/platform/modules/presentation/presentation.mojom.h"
#include "url/gurl.h"

namespace blink {
class WebPresentationAvailabilityObserver;
class WebPresentationConnection;
class WebPresentationReceiver;
class WebString;
class WebURL;
template <typename T>
class WebVector;
}  // namespace blink

namespace content {

class TestPresentationDispatcher;

// PresentationDispatcher is a delegate for Presentation API messages used by
// Blink. It forwards the calls to the Mojo PresentationService.
class CONTENT_EXPORT PresentationDispatcher
    : public RenderFrameObserver,
      public NON_EXPORTED_BASE(blink::WebPresentationClient),
      public NON_EXPORTED_BASE(blink::mojom::PresentationServiceClient) {
 public:
  explicit PresentationDispatcher(RenderFrame* render_frame);
  ~PresentationDispatcher() override;

 private:
  // TODO(crbug.com/684116): remove friend class after moving message queue
  // logic from PresentationDispatcher to PresentationConnectionProxy.
  friend class PresentationConnectionProxy;
  friend class TestPresentationDispatcher;
  friend class PresentationDispatcherTest;
  FRIEND_TEST_ALL_PREFIXES(PresentationDispatcherTest, TestStartPresentation);
  FRIEND_TEST_ALL_PREFIXES(PresentationDispatcherTest,
                           TestStartPresentationError);
  FRIEND_TEST_ALL_PREFIXES(PresentationDispatcherTest,
                           TestReconnectPresentation);
  FRIEND_TEST_ALL_PREFIXES(PresentationDispatcherTest,
                           TestReconnectPresentationError);
  FRIEND_TEST_ALL_PREFIXES(PresentationDispatcherTest, TestSendString);
  FRIEND_TEST_ALL_PREFIXES(PresentationDispatcherTest, TestSendArrayBuffer);
  FRIEND_TEST_ALL_PREFIXES(PresentationDispatcherTest, TestSendBlobData);
  FRIEND_TEST_ALL_PREFIXES(PresentationDispatcherTest,
                           TestOnReceiverConnectionAvailable);
  FRIEND_TEST_ALL_PREFIXES(PresentationDispatcherTest, TestCloseConnection);
  FRIEND_TEST_ALL_PREFIXES(PresentationDispatcherTest,
                           TestTerminatePresentation);
  FRIEND_TEST_ALL_PREFIXES(PresentationDispatcherTest,
                           TestListenForScreenAvailability);
  FRIEND_TEST_ALL_PREFIXES(PresentationDispatcherTest,
                           TestSetDefaultPresentationUrls);

  struct SendMessageRequest {
    SendMessageRequest(
        const PresentationInfo& presentation_info,
        PresentationConnectionMessage connection_message,
        const blink::WebPresentationConnectionProxy* connection_proxy);

    ~SendMessageRequest();

    PresentationInfo presentation_info;
    PresentationConnectionMessage message;
    // Proxy of Blink connection object |connection| calling connection.send().
    // It does not take ownership of proxy object. Proxy object is owned by
    // Blink connection. Blink connection is destroyed after
    // PresentationDispatcher so |connection_proxy| should always be valid.
    const blink::WebPresentationConnectionProxy* connection_proxy;
  };

  static std::unique_ptr<SendMessageRequest> CreateSendTextMessageRequest(
      const blink::WebURL& presentationUrl,
      const blink::WebString& presentationId,
      const blink::WebString& message,
      const blink::WebPresentationConnectionProxy* connection_proxy);
  static std::unique_ptr<SendMessageRequest> CreateSendBinaryMessageRequest(
      const blink::WebURL& presentationUrl,
      const blink::WebString& presentationId,
      const uint8_t* data,
      size_t length,
      const blink::WebPresentationConnectionProxy* connection_proxy);

  // WebPresentationClient implementation.
  void SetController(blink::WebPresentationController* controller) override;
  void SetReceiver(blink::WebPresentationReceiver*) override;
  void StartPresentation(
      const blink::WebVector<blink::WebURL>& presentationUrls,
      std::unique_ptr<blink::WebPresentationConnectionCallbacks> callback)
      override;
  void ReconnectPresentation(
      const blink::WebVector<blink::WebURL>& presentationUrls,
      const blink::WebString& presentationId,
      std::unique_ptr<blink::WebPresentationConnectionCallbacks> callback)
      override;
  void TerminatePresentation(const blink::WebURL& presentationUrl,
                             const blink::WebString& presentationId) override;
  void SendString(
      const blink::WebURL& presentationUrl,
      const blink::WebString& presentationId,
      const blink::WebString& message,
      const blink::WebPresentationConnectionProxy* connection_proxy) override;
  void SendArrayBuffer(
      const blink::WebURL& presentationUrl,
      const blink::WebString& presentationId,
      const uint8_t* data,
      size_t length,
      const blink::WebPresentationConnectionProxy* connection_proxy) override;
  void SendBlobData(
      const blink::WebURL& presentationUrl,
      const blink::WebString& presentationId,
      const uint8_t* data,
      size_t length,
      const blink::WebPresentationConnectionProxy* connection_proxy) override;
  void CloseConnection(
      const blink::WebURL& presentationUrl,
      const blink::WebString& presentationId,
      const blink::WebPresentationConnectionProxy* connection_proxy) override;
  void GetAvailability(
      const blink::WebVector<blink::WebURL>& availabilityUrls,
      std::unique_ptr<blink::WebPresentationAvailabilityCallbacks> callbacks)
      override;
  void StartListening(blink::WebPresentationAvailabilityObserver*) override;
  void StopListening(blink::WebPresentationAvailabilityObserver*) override;
  void SetDefaultPresentationUrls(
      const blink::WebVector<blink::WebURL>& presentationUrls) override;

  // RenderFrameObserver implementation.
  void DidCommitProvisionalLoad(bool is_new_navigation,
                                bool is_same_document_navigation) override;
  void DidFinishDocumentLoad() override;
  void OnDestruct() override;
  void WidgetWillClose() override;

  // blink::mojom::PresentationServiceClient
  void OnScreenAvailabilityNotSupported(const GURL& url) override;
  void OnScreenAvailabilityUpdated(const GURL& url, bool available) override;
  void OnConnectionStateChanged(const PresentationInfo& presentation_info,
                                PresentationConnectionState state) override;
  void OnConnectionClosed(const PresentationInfo& presentation_info,
                          PresentationConnectionCloseReason reason,
                          const std::string& message) override;
  void OnConnectionMessagesReceived(
      const PresentationInfo& presentation_info,
      std::vector<PresentationConnectionMessage> messages) override;
  void OnDefaultPresentationStarted(
      const PresentationInfo& presentation_info) override;

  void OnConnectionCreated(
      std::unique_ptr<blink::WebPresentationConnectionCallbacks> callback,
      const base::Optional<PresentationInfo>& presentation_info,
      const base::Optional<PresentationError>& error);
  void OnReceiverConnectionAvailable(
      const PresentationInfo& presentation_info,
      blink::mojom::PresentationConnectionPtr /*connection*/,
      blink::mojom::PresentationConnectionRequest /*connection_request*/)
      override;

  // Call to PresentationService to send the message in |request|.
  // |presentation_info| and |message| of |reuqest| will be consumed.
  // |HandleSendMessageRequests| will be invoked after the send is attempted.
  void DoSendMessage(SendMessageRequest* request);
  void HandleSendMessageRequests(bool success);

  // Creates ControllerConnectionProxy object |controller_connection_proxy| with
  // |connection|. Sends mojo interface ptr of |controller_connection_proxy|
  // and mojo interface request of |controller_connection_proxy|'s
  // |target_connection_| to PresentationService.
  // |presentation_info|: |connection|'s id and url;
  // |connection|: |controller_connection_proxy|'s |source_connection_|. Raw
  // pointer to Blink connection owning proxy object. It does not take object
  // ownership.
  void SetControllerConnection(const PresentationInfo& presentation_info,
                               blink::WebPresentationConnection* connection);

  virtual void ConnectToPresentationServiceIfNeeded();

  void UpdateListeningState();

  // Used as a weak reference. Can be null since lifetime is bound to the frame.
  blink::WebPresentationController* controller_;
  blink::WebPresentationReceiver* receiver_;
  blink::mojom::PresentationServicePtr presentation_service_;
  mojo::Binding<blink::mojom::PresentationServiceClient> binding_;

  // Message requests are queued here and only one message at a time is sent
  // over mojo channel.
  using MessageRequestQueue = std::deque<std::unique_ptr<SendMessageRequest>>;
  MessageRequestQueue message_request_queue_;

  enum class ListeningState {
    INACTIVE,
    WAITING,
    ACTIVE,
  };

  // Do not change order or add new enum values. |GetScreenAvailability| impl
  // depends on the order of the enum values.
  enum class ScreenAvailability {
    UNKNOWN = 0,
    UNAVAILABLE,
    UNSUPPORTED,
    AVAILABLE
  };

  using AvailabilityCallbacksMap =
      IDMap<std::unique_ptr<blink::WebPresentationAvailabilityCallbacks>>;
  using AvailabilityObserversSet =
      std::set<blink::WebPresentationAvailabilityObserver*>;

  // Tracks listeners of presentation displays availability for
  // |availability_urls|.
  struct AvailabilityListener {
    explicit AvailabilityListener(const std::vector<GURL>& availability_urls);
    ~AvailabilityListener();

    const std::vector<GURL> urls;
    AvailabilityCallbacksMap availability_callbacks;
    AvailabilityObserversSet availability_observers;
  };

  // Tracks listening status of |availability_url|.
  struct ListeningStatus {
    explicit ListeningStatus(const GURL& availability_url);
    ~ListeningStatus();

    const GURL url;
    ScreenAvailability last_known_availability;
    ListeningState listening_state;
  };

  // Map of ListeningStatus for known URLs.
  std::map<GURL, std::unique_ptr<ListeningStatus>> listening_status_;

  // Set of AvailabilityListener for known PresentationRequest.
  std::set<std::unique_ptr<AvailabilityListener>> availability_set_;

  // Starts listening to |url|.
  void StartListeningToURL(const GURL& url);

  // Stops listening to |url| if no PresentationAvailability is observing |url|.
  // StartListening() must have been called first.
  void MaybeStopListeningToURL(const GURL& url);

  // Returns nullptr if there is no status for |url|.
  ListeningStatus* GetListeningStatus(const GURL& url) const;

  // Returns nullptr if there is no availability listener for |urls|.
  AvailabilityListener* GetAvailabilityListener(
      const std::vector<GURL>& urls) const;

  // Removes |listener| from |availability_set_| if |listener| has no callbacks
  // and no observers.
  void TryRemoveAvailabilityListener(AvailabilityListener* listener);

  // Returns AVAILABLE if any url in |urls| has screen availability AVAILABLE;
  // Returns UNSUPPORTED if any url in |urls| have screen availability
  // UNSUPPORTED, and no url has screen availability AVAILABLE;
  // Returns UNAVAILABLE if at least one url in |urls| has screen availability
  // UNAVAILABLE, and no url has screen availability AVAILABLE or UNSUPPORTED;
  // Returns UNKNOWN if all urls in |urls| have screen availability
  // UNKNOWN.
  ScreenAvailability GetScreenAvailability(const std::vector<GURL>& urls) const;

  DISALLOW_COPY_AND_ASSIGN(PresentationDispatcher);
};

}  // namespace content

#endif  // CONTENT_RENDERER_PRESENTATION_PRESENTATION_DISPATCHER_H_