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/macros.h" | |
14 #include "base/memory/linked_ptr.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 } // namespace content | |
29 | |
30 namespace extensions { | |
31 class EventPageTracker; | |
32 } // namespace extensions | |
33 | |
34 namespace media_router { | |
35 | |
36 class IssuesObserver; | |
37 class MediaRouterMojoTest; | |
38 | |
39 // Implementation of the Mojo-based service which bridges the Media Router | |
40 // Provider Manager and the Media Router. | |
41 class MediaRouterMojoImpl : public MediaRouter, | |
42 public interfaces::MediaRouterObserver, | |
43 public mojo::ErrorHandler, | |
44 public KeyedService { | |
45 public: | |
46 MediaRouterMojoImpl(); | |
imcheng (use chromium acct)
2015/05/14 21:30:13
Move this to private and document that one should
Kevin M
2015/05/14 22:46:35
Done.
| |
47 | |
48 ~MediaRouterMojoImpl() override; | |
49 | |
50 // Binds the MediaRouterMojoImpl BrowserContext service to a Mojo request. | |
51 // Called by the Mojo module registry. | |
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 | |
62 void OnConnectionError() override; | |
63 | |
64 // interfaces::MediaRouter implementation. | |
65 // Registers a Mojo MediaRouter proxy object from the extension. | |
66 void ProvideMediaRouter( | |
67 interfaces::MediaRouterPtr media_router_ptr, | |
68 const interfaces::MediaRouterObserver::ProvideMediaRouterCallback& | |
69 callback) override; | |
70 void OnMessage(const mojo::String& route_id, | |
71 const mojo::String& message) override; | |
72 void OnIssue(interfaces::IssuePtr issue) override; | |
73 void OnSinksReceived(const mojo::String& media_source, | |
74 mojo::Array<interfaces::MediaSinkPtr> sinks) override; | |
75 void OnRoutesUpdated(mojo::Array<interfaces::MediaRoutePtr> routes) override; | |
76 | |
77 // MediaRouter implementation. | |
78 // For messages sent to the extension. | |
79 // Execution of the requests is delegated to the Do* methods, which can be | |
80 // enqueued for later use if the extension is temporarily suspended. | |
81 void CreateRoute(const MediaSourceId& source_id, | |
82 const MediaSinkId& sink_id, | |
83 const MediaRouteResponseCallback& callback) override; | |
84 void CloseRoute(const MediaRouteId& route_id) override; | |
85 void PostMessage(const MediaRouteId& route_id, | |
86 const std::string& message) override; | |
87 void ClearIssue(const Issue::IssueId& issue_id) override; | |
88 // These methods send messages to the extension upon first registration | |
89 // and last unregistration. | |
90 bool RegisterMediaSinksObserver(MediaSinksObserver* observer) override; | |
91 void UnregisterMediaSinksObserver(MediaSinksObserver* observer) override; | |
92 bool RegisterMediaRoutesObserver(MediaRoutesObserver* observer) override; | |
93 void UnregisterMediaRoutesObserver(MediaRoutesObserver* observer) override; | |
94 void AddIssuesObserver(IssuesObserver* observer) override; | |
95 void RemoveIssuesObserver(IssuesObserver* observer) override; | |
96 | |
97 private: | |
98 friend class MediaRouterMojoTest; | |
99 | |
100 // Used for testing. | |
101 MediaRouterMojoImpl( | |
102 const std::string& mojo_media_router_ext_id, | |
103 extensions::EventPageTracker* event_page_tracker_for_test); | |
104 | |
105 // Binds |this| to a Mojo interface request, so that clients can acquire a | |
106 // handle to a MediaRouterMojoImpl instance via the Mojo service connector. | |
107 void Bind(mojo::InterfaceRequest<interfaces::MediaRouterObserver> request); | |
108 | |
109 // Enqueues a closure for later execution by ExecutePendingRequests(). | |
110 void EnqueueTask(const base::Closure& closure); | |
111 | |
112 // Starts monitoring the suspend state of |extension_id|. | |
113 void MonitorExtension(const std::string& extension_id, | |
114 content::BrowserContext* context); | |
115 | |
116 // Runs a closure if the extension monitored by |extension_monitor_| is | |
117 // active, or defers it for later execution if the extension is suspended. | |
118 void RunOrDefer(const base::Closure& request_cb); | |
119 | |
120 // Runs pending requests in |pending_requests_|. | |
121 void ExecutePendingRequests(); | |
122 | |
123 // These calls invoke methods in the MRPM via Mojo. | |
124 void DoCreateRoute(const MediaSourceId& source_id, | |
125 const MediaSinkId& sink_id, | |
126 const MediaRouteResponseCallback& callback); | |
127 void DoCloseRoute(const MediaRouteId& route_id); | |
128 void DoPostMessage(const MediaRouteId& route_id, const std::string& message); | |
129 void DoClearIssue(const Issue::IssueId& issue_id); | |
130 void DoStartObservingMediaSinks(const std::string& source_id); | |
131 void DoStopObservingMediaSinks(const std::string& source_id); | |
132 void DoStartObservingMediaRoutes(); | |
133 void DoStopObservingMediaRoutes(); | |
134 void DoStartObservingIssues(); | |
135 void DoStopObservingIssues(); | |
136 | |
137 // Pending requests queued to be executed once MRPM becomes ready. | |
138 std::vector<base::Closure> pending_requests_; | |
139 | |
140 // Multimap of sinks observer objects, keyed by media source ID. | |
141 std::map<MediaSourceId, linked_ptr<ObserverList<MediaSinksObserver>>> | |
imcheng (use chromium acct)
2015/05/14 21:30:13
How about ScopedPtrHashMap?
Kevin M
2015/05/14 22:46:35
Didn't know about this, done.
| |
142 sinks_observers_; | |
143 | |
144 // List of route observer objects. | |
145 ObserverList<MediaRoutesObserver> routes_observers_; | |
146 | |
147 // Binds |this| to a Mojo MediaRouterObserver interface. | |
148 // The binding is released when binding_ is deleted. | |
149 scoped_ptr<mojo::Binding<interfaces::MediaRouterObserver>> binding_; | |
150 | |
151 // Mojo proxy object for the Provider Manager. | |
152 interfaces::MediaRouterPtr mojo_media_router_; | |
153 | |
154 // ID of the component extension. Used for managing its suspend/wake state | |
155 // via event_page_tracker_. | |
156 std::string mojo_media_router_extension_id_; | |
157 | |
158 // Provides querying and waking functionality for the component extension's | |
159 // suspend state. | |
160 extensions::EventPageTracker* event_page_tracker_; | |
161 | |
162 // GUID of this object's instance. | |
163 // Used by the component extension to determine if its persistent cache | |
164 // is stale. | |
165 std::string instance_id_; | |
166 | |
167 base::ThreadChecker thread_checker_; | |
168 | |
169 DISALLOW_COPY_AND_ASSIGN(MediaRouterMojoImpl); | |
170 }; | |
171 | |
172 } // namespace media_router | |
173 | |
174 #endif // CHROME_BROWSER_MEDIA_ROUTER_MEDIA_ROUTER_MOJO_IMPL_H_ | |
OLD | NEW |