| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_ | 5 #ifndef CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_ |
| 6 #define CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_ | 6 #define CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_ |
| 7 | 7 |
| 8 #include "base/basictypes.h" | |
| 9 #include "base/containers/hash_tables.h" | |
| 10 #include "base/gtest_prod_util.h" | |
| 11 #include "base/macros.h" | 8 #include "base/macros.h" |
| 12 #include "base/memory/linked_ptr.h" | |
| 13 #include "content/common/presentation/presentation_service.mojom.h" | 9 #include "content/common/presentation/presentation_service.mojom.h" |
| 14 #include "content/public/browser/navigation_details.h" | 10 #include "mojo/public/cpp/bindings/interface_request.h" |
| 15 #include "content/public/browser/presentation_screen_availability_listener.h" | |
| 16 #include "content/public/browser/presentation_service_delegate.h" | |
| 17 #include "content/public/browser/web_contents_observer.h" | |
| 18 #include "content/public/common/frame_navigate_params.h" | |
| 19 | 11 |
| 20 namespace content { | 12 namespace content { |
| 21 | 13 |
| 22 struct FrameNavigateParams; | 14 // Implements the PresentationService Mojo interface. |
| 23 struct LoadCommittedDetails; | 15 // This service can be created from a RenderFrameHost. |
| 24 class RenderFrameHost; | 16 class PresentationServiceImpl : |
| 25 | 17 public mojo::InterfaceImpl<presentation::PresentationService> { |
| 26 // Implementation of Mojo PresentationService. | |
| 27 // It handles Presentation API requests coming from Blink / renderer process | |
| 28 // and delegates the requests to the embedder's media router via | |
| 29 // PresentationServiceDelegate. | |
| 30 // An instance of this class tied to a RenderFrameHost and listens to events | |
| 31 // related to the RFH via implementing WebContentsObserver. | |
| 32 // This class is instantiated on-demand via Mojo's ConnectToRemoteService | |
| 33 // from the renderer when the first presentation API request is handled. | |
| 34 class PresentationServiceImpl | |
| 35 : public mojo::InterfaceImpl<presentation::PresentationService>, | |
| 36 public WebContentsObserver, | |
| 37 public PresentationServiceDelegate::Observer { | |
| 38 public: | 18 public: |
| 39 ~PresentationServiceImpl() override; | 19 ~PresentationServiceImpl() override; |
| 40 | 20 |
| 41 // Static factory method to create an instance of PresentationServiceImpl. | |
| 42 // |render_frame_host|: The RFH the instance is associated with. | |
| 43 // |request|: The instance will be bound to this request. Used for Mojo setup. | |
| 44 static void CreateMojoService( | 21 static void CreateMojoService( |
| 45 RenderFrameHost* render_frame_host, | |
| 46 mojo::InterfaceRequest<presentation::PresentationService> request); | 22 mojo::InterfaceRequest<presentation::PresentationService> request); |
| 47 | 23 |
| 24 protected: |
| 25 PresentationServiceImpl(); |
| 26 |
| 48 private: | 27 private: |
| 49 typedef mojo::Callback<void(bool)> ScreenAvailabilityMojoCallback; | 28 typedef mojo::Callback<void(bool)> AvailabilityCallback; |
| 50 | |
| 51 friend class PresentationServiceImplTest; | |
| 52 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, RemoveAllListeners); | |
| 53 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | |
| 54 DidNavigateThisFrame); | |
| 55 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | |
| 56 DidNavigateNotThisFrame); | |
| 57 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | |
| 58 ThisRenderFrameDeleted); | |
| 59 FRIEND_TEST_ALL_PREFIXES(PresentationServiceImplTest, | |
| 60 NotThisRenderFrameDeleted); | |
| 61 | |
| 62 // |render_frame_host|: The RFH this instance is associated with. | |
| 63 // |web_contents|: The WebContents to observe. | |
| 64 // |delegate|: Where Presentation API requests are delegated to. Not owned | |
| 65 // by this class. | |
| 66 PresentationServiceImpl( | |
| 67 RenderFrameHost* render_frame_host, | |
| 68 WebContents* web_contents, | |
| 69 PresentationServiceDelegate* delegate); | |
| 70 | 29 |
| 71 // PresentationService implementation. | 30 // PresentationService implementation. |
| 72 void GetScreenAvailability( | 31 void GetScreenAvailability( |
| 73 const mojo::String& presentation_url, | 32 const mojo::String& presentation_url, |
| 74 const ScreenAvailabilityMojoCallback& callback) override; | 33 const AvailabilityCallback& callback) override; |
| 75 void OnScreenAvailabilityListenerRemoved() override; | 34 void OnScreenAvailabilityListenerRemoved() override; |
| 76 | 35 |
| 77 // mojo::InterfaceImpl override. | |
| 78 // Note that this is called when the RenderFrameHost is deleted. | |
| 79 void OnConnectionError() override; | |
| 80 | |
| 81 // WebContentsObserver override. | |
| 82 void DidNavigateAnyFrame( | |
| 83 content::RenderFrameHost* render_frame_host, | |
| 84 const content::LoadCommittedDetails& details, | |
| 85 const content::FrameNavigateParams& params) override; | |
| 86 void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override; | |
| 87 | |
| 88 // PresentationServiceDelegate::Observer | |
| 89 void OnDelegateDestroyed() override; | |
| 90 | |
| 91 // Removes all listeners on this instance and informs the | |
| 92 // PresentationServiceDelegate of such. | |
| 93 void RemoveAllListeners(); | |
| 94 | |
| 95 RenderFrameHost* render_frame_host_; | |
| 96 PresentationServiceDelegate* delegate_; | |
| 97 | |
| 98 // A helper data class used by PresentationServiceImpl to do bookkeeping | |
| 99 // of currently registered screen availability listeners. | |
| 100 // An instance of this class is a simple state machine that waits for both | |
| 101 // the available bit and the Mojo callback to become available. | |
| 102 // Once this happens, the Mojo callback will be invoked with the available | |
| 103 // bit, and the state machine will reset. | |
| 104 // The available bit is obtained from the embedder's media router. | |
| 105 // The callback is obtained from the renderer via PresentationServiceImpl's | |
| 106 // GetScreenAvailability(). | |
| 107 class ScreenAvailabilityContext | |
| 108 : public PresentationScreenAvailabilityListener { | |
| 109 public: | |
| 110 explicit ScreenAvailabilityContext( | |
| 111 const std::string& presentation_url); | |
| 112 ~ScreenAvailabilityContext() override; | |
| 113 | |
| 114 // If available bit exists, |callback| will be invoked with the bit and | |
| 115 // this state machine will reset. | |
| 116 // Otherwise |callback| is saved for later use. | |
| 117 // |callback|: Callback to the client of PresentationService | |
| 118 // (i.e. the renderer) that screen availability has changed, via Mojo. | |
| 119 void CallbackReceived(const ScreenAvailabilityMojoCallback& callback); | |
| 120 | |
| 121 // Clears both callback and available bit. | |
| 122 void Reset(); | |
| 123 | |
| 124 // PresentationScreenAvailabilityListener implementation. | |
| 125 std::string GetPresentationUrl() const override; | |
| 126 // If callback exists, it will be invoked with |available| and | |
| 127 // this state machine will reset. | |
| 128 // Otherwise |available| is saved for later use. | |
| 129 // |available|: New screen availability for the presentation URL. | |
| 130 void OnScreenAvailabilityChanged(bool available) override; | |
| 131 | |
| 132 private: | |
| 133 std::string presentation_url_; | |
| 134 scoped_ptr<ScreenAvailabilityMojoCallback> callback_ptr_; | |
| 135 scoped_ptr<bool> available_ptr_; | |
| 136 }; | |
| 137 | |
| 138 // Map from presentation URL to its ScreenAvailabilityContext state machine. | |
| 139 base::hash_map<std::string, linked_ptr<ScreenAvailabilityContext>> | |
| 140 availability_contexts_; | |
| 141 | |
| 142 std::string default_presentation_url_; | |
| 143 | |
| 144 DISALLOW_COPY_AND_ASSIGN(PresentationServiceImpl); | 36 DISALLOW_COPY_AND_ASSIGN(PresentationServiceImpl); |
| 145 }; | 37 }; |
| 146 | 38 |
| 147 } // namespace content | 39 } // namespace content |
| 148 | 40 |
| 149 #endif // CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_ | 41 #endif // CONTENT_BROWSER_PRESENTATION_PRESENTATION_SERVICE_IMPL_H_ |
| OLD | NEW |