Chromium Code Reviews| 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 // Mojo service exposing Media Router functionality to the Media Route Provider | |
| 37 // Manager. | |
| 38 class MediaRouterMojoImpl : public MediaRouter, | |
| 39 public interfaces::MediaRouterObserver, | |
| 40 public mojo::ErrorHandler, | |
| 41 public KeyedService { | |
| 42 public: | |
| 43 ~MediaRouterMojoImpl() override; | |
| 44 | |
| 45 // Binds the MediaRouterMojoImpl BrowserContext service to a Mojo request. | |
| 46 // Called by the Mojo module registry. | |
| 47 // Stores the |extension_id| of the component extension in | |
| 48 // the MediaRouterMojoImpl instance. | |
| 49 // Also stores a reference to the extension's ProcessManager using | |
|
Wez
2015/05/23 00:45:47
Let's re-word this to make clear what "binding to
imcheng (use chromium acct)
2015/05/26 17:58:31
Done.
| |
| 50 // |context|, which is a reference to the BrowserContext that owns this | |
| 51 // instance and extension. | |
| 52 // |extension_id|: The ID of the component extension, used for querying | |
| 53 // suspension state. | |
| 54 // |context|: The BrowserContext which owns the extension process. | |
| 55 // |request|: The Mojo connection request used for binding. | |
| 56 static void BindToRequest( | |
| 57 const std::string& extension_id, | |
| 58 content::BrowserContext* context, | |
| 59 mojo::InterfaceRequest<interfaces::MediaRouterObserver> request); | |
| 60 | |
| 61 // mojo::ErrorHandler implementation. | |
| 62 void OnConnectionError() override; | |
| 63 | |
| 64 // interfaces::MediaRouterObserver implementation. | |
|
Wez
2015/05/23 00:45:47
So IIUC this is the implementation of the Mojo int
imcheng (use chromium acct)
2015/05/26 17:58:31
Done.
| |
| 65 void ProvideMediaRouter( | |
| 66 interfaces::MediaRouterPtr media_router_ptr, | |
| 67 const interfaces::MediaRouterObserver::ProvideMediaRouterCallback& | |
| 68 callback) override; | |
| 69 void OnMessage(const mojo::String& route_id, | |
| 70 const mojo::String& message) override; | |
| 71 void OnIssue(interfaces::IssuePtr issue) override; | |
| 72 void OnSinksReceived(const mojo::String& media_source, | |
| 73 mojo::Array<interfaces::MediaSinkPtr> sinks) override; | |
| 74 void OnRoutesUpdated(mojo::Array<interfaces::MediaRoutePtr> routes) override; | |
| 75 | |
| 76 // MediaRouter implementation. | |
| 77 // Execution of the requests is delegated to the Do* methods, which can be | |
| 78 // enqueued for later use if the extension is temporarily suspended. | |
| 79 void CreateRoute(const MediaSourceId& source_id, | |
| 80 const MediaSinkId& sink_id, | |
| 81 const MediaRouteResponseCallback& callback) override; | |
| 82 void CloseRoute(const MediaRouteId& route_id) override; | |
| 83 void PostMessage(const MediaRouteId& route_id, | |
| 84 const std::string& message) override; | |
| 85 void ClearIssue(const Issue::IssueId& issue_id) override; | |
| 86 void RegisterMediaSinksObserver(MediaSinksObserver* observer) override; | |
| 87 void UnregisterMediaSinksObserver(MediaSinksObserver* observer) override; | |
| 88 void RegisterMediaRoutesObserver(MediaRoutesObserver* observer) override; | |
| 89 void UnregisterMediaRoutesObserver(MediaRoutesObserver* observer) override; | |
| 90 void AddIssuesObserver(IssuesObserver* observer) override; | |
| 91 void RemoveIssuesObserver(IssuesObserver* observer) override; | |
| 92 | |
| 93 private: | |
| 94 friend class MediaRouterMojoImplFactory; | |
| 95 friend class MediaRouterMojoTest; | |
| 96 | |
| 97 FRIEND_TEST_ALL_PREFIXES(MediaRouterMojoExtensionTest, | |
| 98 DeferredBindingAndSuspension); | |
| 99 | |
| 100 // Standard constructor, used by | |
| 101 // MediaRouterMojoImplFactory::GetApiForBrowserContext. | |
| 102 MediaRouterMojoImpl(); | |
| 103 | |
| 104 // Constructor used only for testing. | |
| 105 MediaRouterMojoImpl( | |
| 106 const std::string& mojo_media_router_ext_id, | |
| 107 extensions::EventPageTracker* event_page_tracker_for_test); | |
| 108 | |
| 109 // Binds |this| to a Mojo interface request, so that clients can acquire a | |
| 110 // handle to a MediaRouterMojoImpl instance via the Mojo service connector. | |
| 111 void BindToMojoRequest( | |
| 112 mojo::InterfaceRequest<interfaces::MediaRouterObserver> request); | |
| 113 | |
| 114 void InitExtensionInfo(const std::string& extension_id, | |
| 115 content::BrowserContext* context); | |
| 116 | |
| 117 // Enqueues a closure for later execution by ExecutePendingRequests(). | |
| 118 void EnqueueTask(const base::Closure& closure); | |
| 119 | |
| 120 // Runs a closure if the extension monitored by |extension_monitor_| is | |
| 121 // active, or defers it for later execution if the extension is suspended. | |
| 122 void RunOrDefer(const base::Closure& request); | |
| 123 | |
| 124 // Dispatches the Mojo requests queued in |pending_requests_|. | |
| 125 void ExecutePendingRequests(); | |
| 126 | |
| 127 // These calls invoke methods in the MRPM via Mojo. | |
| 128 void DoCreateRoute(const MediaSourceId& source_id, | |
| 129 const MediaSinkId& sink_id, | |
| 130 const MediaRouteResponseCallback& callback); | |
| 131 void DoCloseRoute(const MediaRouteId& route_id); | |
| 132 void DoPostMessage(const MediaRouteId& route_id, const std::string& message); | |
| 133 void DoClearIssue(const Issue::IssueId& issue_id); | |
| 134 void DoStartObservingMediaSinks(const std::string& source_id); | |
| 135 void DoStopObservingMediaSinks(const std::string& source_id); | |
| 136 void DoStartObservingMediaRoutes(); | |
| 137 void DoStopObservingMediaRoutes(); | |
| 138 void DoStartObservingIssues(); | |
| 139 void DoStopObservingIssues(); | |
| 140 | |
| 141 // Pending requests queued to be executed once MRPM becomes ready. | |
| 142 std::vector<base::Closure> pending_requests_; | |
| 143 | |
| 144 base::ScopedPtrHashMap<MediaSourceId, | |
| 145 scoped_ptr<ObserverList<MediaSinksObserver>>> | |
| 146 sinks_observers_; | |
| 147 | |
| 148 ObserverList<MediaRoutesObserver> routes_observers_; | |
| 149 | |
| 150 // Binds |this| to listen for updates from the component extension Media | |
| 151 // Router. | |
| 152 scoped_ptr<mojo::Binding<interfaces::MediaRouterObserver>> binding_; | |
| 153 | |
| 154 // Mojo proxy object for the component extension Media Router. | |
| 155 // Set to null initially, and set to the proxy to the component extension | |
| 156 // on |ProvideMediaRouter()|. This is set to null again when the component | |
| 157 // extension becomes suspended or if there is a connection error. | |
| 158 interfaces::MediaRouterPtr mojo_media_router_; | |
| 159 | |
| 160 // Id of the component extension. Used for managing its suspend/wake state | |
| 161 // via event_page_tracker_. | |
| 162 std::string mojo_media_router_extension_id_; | |
| 163 | |
| 164 // Allows the extension to be monitored for suspend, and woken. | |
| 165 // This is a reference to a BrowserContext keyed service that outlives this | |
| 166 // instance. | |
| 167 extensions::EventPageTracker* event_page_tracker_; | |
| 168 | |
| 169 // GUID unique to each browser run. Component extension uses this to detect | |
| 170 // when its persisted state was written by an older browser instance, and is | |
| 171 // therefore stale. | |
| 172 std::string instance_id_; | |
| 173 | |
| 174 base::ThreadChecker thread_checker_; | |
| 175 | |
| 176 DISALLOW_COPY_AND_ASSIGN(MediaRouterMojoImpl); | |
| 177 }; | |
| 178 | |
| 179 } // namespace media_router | |
| 180 | |
| 181 #endif // CHROME_BROWSER_MEDIA_ROUTER_MEDIA_ROUTER_MOJO_IMPL_H_ | |
| OLD | NEW |