OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #ifndef CHROME_BROWSER_MEDIA_ROUTER_MEDIA_ROUTER_MOJO_IMPL_H_ |
| 6 #define CHROME_BROWSER_MEDIA_ROUTER_MEDIA_ROUTER_MOJO_IMPL_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <string> |
| 10 #include <vector> |
| 11 |
| 12 #include "base/containers/hash_tables.h" |
| 13 #include "base/containers/scoped_ptr_hash_map.h" |
| 14 #include "base/macros.h" |
| 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/memory/weak_ptr.h" |
| 18 #include "base/message_loop/message_loop_proxy.h" |
| 19 #include "base/observer_list.h" |
| 20 #include "base/threading/thread_checker.h" |
| 21 #include "chrome/browser/media/router/issue.h" |
| 22 #include "chrome/browser/media/router/media_router.h" |
| 23 #include "chrome/browser/media/router/media_router.mojom.h" |
| 24 #include "components/keyed_service/core/keyed_service.h" |
| 25 |
| 26 namespace content { |
| 27 class BrowserContext; |
| 28 } |
| 29 |
| 30 namespace extensions { |
| 31 class EventPageTracker; |
| 32 } |
| 33 |
| 34 namespace media_router { |
| 35 |
| 36 // MediaRouter implementation that delegates calls to the component extension. |
| 37 // Also handles the suspension and wakeup of the component extension. |
| 38 class MediaRouterMojoImpl : public MediaRouter, |
| 39 public interfaces::MediaRouterObserver, |
| 40 public mojo::ErrorHandler, |
| 41 public KeyedService { |
| 42 public: |
| 43 ~MediaRouterMojoImpl() override; |
| 44 |
| 45 // Sets up the MediaRouterMojoImpl instance owned by |context| to handle |
| 46 // MediaRouterObserver requests from the component extension given by |
| 47 // |extension_id|. Creates the MediaRouterMojoImpl instance if it does not |
| 48 // exist. |
| 49 // Called by the Mojo module registry. |
| 50 // |extension_id|: The ID of the component extension, used for querying |
| 51 // suspension state. |
| 52 // |context|: The BrowserContext which owns the extension process. |
| 53 // |request|: The Mojo connection request used for binding. |
| 54 static void BindToRequest( |
| 55 const std::string& extension_id, |
| 56 content::BrowserContext* context, |
| 57 mojo::InterfaceRequest<interfaces::MediaRouterObserver> request); |
| 58 |
| 59 // MediaRouter implementation. |
| 60 // Execution of the requests is delegated to the Do* methods, which can be |
| 61 // enqueued for later use if the extension is temporarily suspended. |
| 62 void CreateRoute(const MediaSourceId& source_id, |
| 63 const MediaSinkId& sink_id, |
| 64 const MediaRouteResponseCallback& callback) override; |
| 65 void CloseRoute(const MediaRouteId& route_id) override; |
| 66 void PostMessage(const MediaRouteId& route_id, |
| 67 const std::string& message) override; |
| 68 void ClearIssue(const Issue::IssueId& issue_id) override; |
| 69 void RegisterMediaSinksObserver(MediaSinksObserver* observer) override; |
| 70 void UnregisterMediaSinksObserver(MediaSinksObserver* observer) override; |
| 71 void RegisterMediaRoutesObserver(MediaRoutesObserver* observer) override; |
| 72 void UnregisterMediaRoutesObserver(MediaRoutesObserver* observer) override; |
| 73 void AddIssuesObserver(IssuesObserver* observer) override; |
| 74 void RemoveIssuesObserver(IssuesObserver* observer) override; |
| 75 |
| 76 void set_instance_id_for_test(const std::string& instance_id) { |
| 77 instance_id_ = instance_id; |
| 78 } |
| 79 |
| 80 private: |
| 81 friend class MediaRouterMojoImplFactory; |
| 82 friend class MediaRouterMojoTest; |
| 83 |
| 84 FRIEND_TEST_ALL_PREFIXES(MediaRouterMojoExtensionTest, |
| 85 DeferredBindingAndSuspension); |
| 86 |
| 87 // Standard constructor, used by |
| 88 // MediaRouterMojoImplFactory::GetApiForBrowserContext. |
| 89 MediaRouterMojoImpl(); |
| 90 |
| 91 // Binds |this| to a Mojo interface request, so that clients can acquire a |
| 92 // handle to a MediaRouterMojoImpl instance via the Mojo service connector. |
| 93 // Stores the |extension_id| of the component extension. |
| 94 // Also obtains a reference to the extension's ProcessManager using |
| 95 // |context|, which is a reference to the BrowserContext that owns this |
| 96 // instance and extension. |
| 97 void BindToMojoRequest( |
| 98 mojo::InterfaceRequest<interfaces::MediaRouterObserver> request, |
| 99 const std::string& extension_id, |
| 100 content::BrowserContext* context); |
| 101 |
| 102 void BindToMojoRequestForTest( |
| 103 mojo::InterfaceRequest<interfaces::MediaRouterObserver> request, |
| 104 const std::string& extension_id, |
| 105 extensions::EventPageTracker* event_page_tracker); |
| 106 |
| 107 // Enqueues a closure for later execution by ExecutePendingRequests(). |
| 108 void EnqueueTask(const base::Closure& closure); |
| 109 |
| 110 // Runs a closure if the extension monitored by |extension_monitor_| is |
| 111 // active, or defers it for later execution if the extension is suspended. |
| 112 void RunOrDefer(const base::Closure& request); |
| 113 |
| 114 // Dispatches the Mojo requests queued in |pending_requests_|. |
| 115 void ExecutePendingRequests(); |
| 116 |
| 117 // These calls invoke methods in the component extension via Mojo. |
| 118 void DoCreateRoute(const MediaSourceId& source_id, |
| 119 const MediaSinkId& sink_id, |
| 120 const MediaRouteResponseCallback& callback); |
| 121 void DoCloseRoute(const MediaRouteId& route_id); |
| 122 void DoPostMessage(const MediaRouteId& route_id, const std::string& message); |
| 123 void DoClearIssue(const Issue::IssueId& issue_id); |
| 124 void DoStartObservingMediaSinks(const std::string& source_id); |
| 125 void DoStopObservingMediaSinks(const std::string& source_id); |
| 126 void DoStartObservingMediaRoutes(); |
| 127 void DoStopObservingMediaRoutes(); |
| 128 void DoStartObservingIssues(); |
| 129 void DoStopObservingIssues(); |
| 130 |
| 131 // mojo::ErrorHandler implementation. |
| 132 void OnConnectionError() override; |
| 133 |
| 134 // interfaces::MediaRouterObserver implementation. |
| 135 void ProvideMediaRouter( |
| 136 interfaces::MediaRouterPtr media_router_ptr, |
| 137 const interfaces::MediaRouterObserver::ProvideMediaRouterCallback& |
| 138 callback) override; |
| 139 void OnMessage(const mojo::String& route_id, |
| 140 const mojo::String& message) override; |
| 141 void OnIssue(interfaces::IssuePtr issue) override; |
| 142 void OnSinksReceived(const mojo::String& media_source, |
| 143 mojo::Array<interfaces::MediaSinkPtr> sinks) override; |
| 144 void OnRoutesUpdated(mojo::Array<interfaces::MediaRoutePtr> routes) override; |
| 145 |
| 146 // Pending requests queued to be executed once component extension |
| 147 // becomes ready. |
| 148 std::vector<base::Closure> pending_requests_; |
| 149 |
| 150 base::ScopedPtrHashMap<MediaSourceId, |
| 151 scoped_ptr<ObserverList<MediaSinksObserver>>> |
| 152 sinks_observers_; |
| 153 |
| 154 ObserverList<MediaRoutesObserver> routes_observers_; |
| 155 |
| 156 // Binds |this| to listen for updates from the component extension Media |
| 157 // Router. |
| 158 scoped_ptr<mojo::Binding<interfaces::MediaRouterObserver>> binding_; |
| 159 |
| 160 // Mojo proxy object for the component extension Media Router. |
| 161 // Set to null initially, and set to the proxy to the component extension |
| 162 // on |ProvideMediaRouter()|. This is set to null again when the component |
| 163 // extension becomes suspended or if there is a connection error. |
| 164 interfaces::MediaRouterPtr mojo_media_router_; |
| 165 |
| 166 // Id of the component extension. Used for managing its suspend/wake state |
| 167 // via event_page_tracker_. |
| 168 std::string mojo_media_router_extension_id_; |
| 169 |
| 170 // Allows the extension to be monitored for suspend, and woken. |
| 171 // This is a reference to a BrowserContext keyed service that outlives this |
| 172 // instance. |
| 173 extensions::EventPageTracker* event_page_tracker_; |
| 174 |
| 175 // GUID unique to each browser run. Component extension uses this to detect |
| 176 // when its persisted state was written by an older browser instance, and is |
| 177 // therefore stale. |
| 178 std::string instance_id_; |
| 179 |
| 180 base::ThreadChecker thread_checker_; |
| 181 |
| 182 DISALLOW_COPY_AND_ASSIGN(MediaRouterMojoImpl); |
| 183 }; |
| 184 |
| 185 } // namespace media_router |
| 186 |
| 187 #endif // CHROME_BROWSER_MEDIA_ROUTER_MEDIA_ROUTER_MOJO_IMPL_H_ |
OLD | NEW |