1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CONTENT_RENDERER_PRESENTATION_PRESENTATION_DISPATCHER_H_
6 #define CONTENT_RENDERER_PRESENTATION_PRESENTATION_DISPATCHER_H_
8 #include "base/compiler_specific.h"
9 #include "base/id_map.h"
10 #include "base/memory/linked_ptr.h"
11 #include "content/common/content_export.h"
12 #include "content/common/presentation/presentation_service.mojom.h"
13 #include "content/public/renderer/render_frame_observer.h"
14 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentationClient.h"
17 class WebPresentationAvailabilityObserver
;
23 // PresentationDispatcher is a delegate for Presentation API messages used by
24 // Blink. It forwards the calls to the Mojo PresentationService.
25 class CONTENT_EXPORT PresentationDispatcher
26 : public RenderFrameObserver
,
27 public NON_EXPORTED_BASE(blink::WebPresentationClient
),
28 public NON_EXPORTED_BASE(presentation::PresentationServiceClient
) {
30 explicit PresentationDispatcher(RenderFrame
* render_frame
);
31 ~PresentationDispatcher() override
;
34 struct SendMessageRequest
{
35 SendMessageRequest(presentation::PresentationSessionInfoPtr session_info
,
36 presentation::SessionMessagePtr message
);
37 ~SendMessageRequest();
39 presentation::PresentationSessionInfoPtr session_info
;
40 presentation::SessionMessagePtr message
;
43 static SendMessageRequest
* CreateSendTextMessageRequest(
44 const blink::WebString
& presentationUrl
,
45 const blink::WebString
& presentationId
,
46 const blink::WebString
& message
);
47 static SendMessageRequest
* CreateSendBinaryMessageRequest(
48 const blink::WebString
& presentationUrl
,
49 const blink::WebString
& presentationId
,
50 presentation::PresentationMessageType type
,
54 // WebPresentationClient implementation.
55 virtual void setController(
56 blink::WebPresentationController
* controller
);
57 virtual void updateAvailableChangeWatched(bool watched
);
58 virtual void startSession(
59 const blink::WebString
& presentationUrl
,
60 blink::WebPresentationSessionClientCallbacks
* callback
);
61 virtual void joinSession(
62 const blink::WebString
& presentationUrl
,
63 const blink::WebString
& presentationId
,
64 blink::WebPresentationSessionClientCallbacks
* callback
);
65 virtual void sendString(
66 const blink::WebString
& presentationUrl
,
67 const blink::WebString
& presentationId
,
68 const blink::WebString
& message
);
69 virtual void sendArrayBuffer(
70 const blink::WebString
& presentationUrl
,
71 const blink::WebString
& presentationId
,
74 virtual void sendBlobData(
75 const blink::WebString
& presentationUrl
,
76 const blink::WebString
& presentationId
,
79 virtual void closeSession(
80 const blink::WebString
& presentationUrl
,
81 const blink::WebString
& presentationId
);
82 virtual void getAvailability(
83 const blink::WebString
& presentationUrl
,
84 blink::WebPresentationAvailabilityCallbacks
* callbacks
);
85 virtual void startListening(blink::WebPresentationAvailabilityObserver
*);
86 virtual void stopListening(blink::WebPresentationAvailabilityObserver
*);
87 virtual void setDefaultPresentationUrl(const blink::WebString
& url
);
89 // RenderFrameObserver implementation.
90 void DidCommitProvisionalLoad(
91 bool is_new_navigation
,
92 bool is_same_page_navigation
) override
;
94 // presentation::PresentationServiceClient
95 void OnScreenAvailabilityUpdated(bool available
) override
;
96 void OnSessionStateChanged(
97 presentation::PresentationSessionInfoPtr session_info
,
98 presentation::PresentationSessionState new_state
) override
;
99 void OnScreenAvailabilityNotSupported() override
;
100 void OnSessionMessagesReceived(
101 presentation::PresentationSessionInfoPtr session_info
,
102 mojo::Array
<presentation::SessionMessagePtr
> messages
) override
;
104 void OnSessionCreated(
105 blink::WebPresentationSessionClientCallbacks
* callback
,
106 presentation::PresentationSessionInfoPtr session_info
,
107 presentation::PresentationErrorPtr error
);
108 void OnDefaultSessionStarted(
109 presentation::PresentationSessionInfoPtr session_info
);
111 // Call to PresentationService to send the message in |request|.
112 // |session_info| and |message| of |reuqest| will be consumed.
113 // |HandleSendMessageRequests| will be invoked after the send is attempted.
114 void DoSendMessage(SendMessageRequest
* request
);
115 void HandleSendMessageRequests(bool success
);
117 void ConnectToPresentationServiceIfNeeded();
119 void UpdateListeningState();
121 // Used as a weak reference. Can be null since lifetime is bound to the frame.
122 blink::WebPresentationController
* controller_
;
123 presentation::PresentationServicePtr presentation_service_
;
124 mojo::Binding
<presentation::PresentationServiceClient
> binding_
;
126 // Message requests are queued here and only one message at a time is sent
127 // over mojo channel.
128 using MessageRequestQueue
= std::queue
<linked_ptr
<SendMessageRequest
>>;
129 MessageRequestQueue message_request_queue_
;
131 enum class ListeningState
{
137 ListeningState listening_state_
;
138 bool last_known_availability_
;
140 using AvailabilityCallbacksMap
=
141 IDMap
<blink::WebPresentationAvailabilityCallbacks
, IDMapOwnPointer
>;
142 AvailabilityCallbacksMap availability_callbacks_
;
144 using AvailabilityObserversSet
=
145 std::set
<blink::WebPresentationAvailabilityObserver
*>;
146 AvailabilityObserversSet availability_observers_
;
148 DISALLOW_COPY_AND_ASSIGN(PresentationDispatcher
);
151 } // namespace content
153 #endif // CONTENT_RENDERER_PRESENTATION_PRESENTATION_DISPATCHER_H_