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 // WebPresentationClient implementation.
35 virtual void setController(
36 blink::WebPresentationController
* controller
);
37 virtual void updateAvailableChangeWatched(bool watched
);
38 virtual void startSession(
39 const blink::WebString
& presentationUrl
,
40 blink::WebPresentationSessionClientCallbacks
* callback
);
41 virtual void joinSession(
42 const blink::WebString
& presentationUrl
,
43 const blink::WebString
& presentationId
,
44 blink::WebPresentationSessionClientCallbacks
* callback
);
45 virtual void sendString(
46 const blink::WebString
& presentationUrl
,
47 const blink::WebString
& presentationId
,
48 const blink::WebString
& message
);
49 virtual void sendArrayBuffer(
50 const blink::WebString
& presentationUrl
,
51 const blink::WebString
& presentationId
,
54 virtual void sendBlobData(
55 const blink::WebString
& presentationUrl
,
56 const blink::WebString
& presentationId
,
59 virtual void closeSession(
60 const blink::WebString
& presentationUrl
,
61 const blink::WebString
& presentationId
);
62 virtual void getAvailability(
63 const blink::WebString
& presentationUrl
,
64 blink::WebPresentationAvailabilityCallbacks
* callbacks
);
65 virtual void startListening(blink::WebPresentationAvailabilityObserver
*);
66 virtual void stopListening(blink::WebPresentationAvailabilityObserver
*);
67 virtual void setDefaultPresentationUrl(const blink::WebString
& url
);
69 // RenderFrameObserver implementation.
70 void DidCommitProvisionalLoad(
71 bool is_new_navigation
,
72 bool is_same_page_navigation
) override
;
74 // presentation::PresentationServiceClient
75 void OnScreenAvailabilityUpdated(bool available
) override
;
76 void OnSessionStateChanged(
77 presentation::PresentationSessionInfoPtr session_info
,
78 presentation::PresentationSessionState new_state
) override
;
79 void OnScreenAvailabilityNotSupported() override
;
81 void OnSessionCreated(
82 blink::WebPresentationSessionClientCallbacks
* callback
,
83 presentation::PresentationSessionInfoPtr session_info
,
84 presentation::PresentationErrorPtr error
);
85 void OnDefaultSessionStarted(
86 presentation::PresentationSessionInfoPtr session_info
);
87 void OnSessionMessagesReceived(
88 mojo::Array
<presentation::SessionMessagePtr
> messages
);
89 void DoSendMessage(const presentation::SessionMessage
& session_message
);
90 void HandleSendMessageRequests(bool success
);
92 void ConnectToPresentationServiceIfNeeded();
94 void UpdateListeningState();
96 void StartListenForMessages();
98 // Used as a weak reference. Can be null since lifetime is bound to the frame.
99 blink::WebPresentationController
* controller_
;
100 presentation::PresentationServicePtr presentation_service_
;
101 mojo::Binding
<presentation::PresentationServiceClient
> binding_
;
103 // Message requests are queued here and only one message at a time is sent
104 // over mojo channel.
105 using MessageRequestQueue
=
106 std::queue
<linked_ptr
<presentation::SessionMessage
>>;
107 MessageRequestQueue message_request_queue_
;
109 enum class ListeningState
{
115 ListeningState listening_state_
;
116 bool last_known_availability_
;
118 using AvailabilityCallbacksMap
=
119 IDMap
<blink::WebPresentationAvailabilityCallbacks
, IDMapOwnPointer
>;
120 AvailabilityCallbacksMap availability_callbacks_
;
122 using AvailabilityObserversSet
=
123 std::set
<blink::WebPresentationAvailabilityObserver
*>;
124 AvailabilityObserversSet availability_observers_
;
126 bool listening_for_messages_
;
128 DISALLOW_COPY_AND_ASSIGN(PresentationDispatcher
);
131 } // namespace content
133 #endif // CONTENT_RENDERER_PRESENTATION_PRESENTATION_DISPATCHER_H_