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