// 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 #include #include #include #include #include #include #include #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 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 CreateSendTextMessageRequest( const blink::WebURL& presentationUrl, const blink::WebString& presentationId, const blink::WebString& message, const blink::WebPresentationConnectionProxy* connection_proxy); static std::unique_ptr 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& presentationUrls, std::unique_ptr callback) override; void ReconnectPresentation( const blink::WebVector& presentationUrls, const blink::WebString& presentationId, std::unique_ptr 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& availabilityUrls, std::unique_ptr callbacks) override; void StartListening(blink::WebPresentationAvailabilityObserver*) override; void StopListening(blink::WebPresentationAvailabilityObserver*) override; void SetDefaultPresentationUrls( const blink::WebVector& 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 messages) override; void OnDefaultPresentationStarted( const PresentationInfo& presentation_info) override; void OnConnectionCreated( std::unique_ptr callback, const base::Optional& presentation_info, const base::Optional& 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 binding_; // Message requests are queued here and only one message at a time is sent // over mojo channel. using MessageRequestQueue = std::deque>; 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>; using AvailabilityObserversSet = std::set; // Tracks listeners of presentation displays availability for // |availability_urls|. struct AvailabilityListener { explicit AvailabilityListener(const std::vector& availability_urls); ~AvailabilityListener(); const std::vector 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> listening_status_; // Set of AvailabilityListener for known PresentationRequest. std::set> 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& 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& urls) const; DISALLOW_COPY_AND_ASSIGN(PresentationDispatcher); }; } // namespace content #endif // CONTENT_RENDERER_PRESENTATION_PRESENTATION_DISPATCHER_H_