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, | |
Wez
2015/05/22 00:05:38
Sorry, I meant to mention this fundamental questio
imcheng (use chromium acct)
2015/05/22 00:54:07
I think "really only intended to expose MediaRoute
Wez
2015/05/23 00:45:46
Right - thinking about this last night, I realised
imcheng (use chromium acct)
2015/05/26 17:58:31
ok, I changed the class level comments as you sugg
| |
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 // |extension_id|: The ID of the component extension, used for querying | |
48 // suspension state. | |
49 // |context|: The BrowserContext which owns the extension process. | |
50 // |request|: The Mojo connection request used for binding. | |
51 static void BindToRequest( | |
52 const std::string& extension_id, | |
53 content::BrowserContext* context, | |
54 mojo::InterfaceRequest<interfaces::MediaRouterObserver> request); | |
55 | |
56 // mojo::ErrorHandler implementation. | |
57 void OnConnectionError() override; | |
58 | |
59 // interfaces::MediaRouterObserver implementation. | |
60 void ProvideMediaRouter( | |
61 interfaces::MediaRouterPtr media_router_ptr, | |
62 const interfaces::MediaRouterObserver::ProvideMediaRouterCallback& | |
63 callback) override; | |
64 void OnMessage(const mojo::String& route_id, | |
65 const mojo::String& message) override; | |
66 void OnIssue(interfaces::IssuePtr issue) override; | |
67 void OnSinksReceived(const mojo::String& media_source, | |
68 mojo::Array<interfaces::MediaSinkPtr> sinks) override; | |
69 void OnRoutesUpdated(mojo::Array<interfaces::MediaRoutePtr> routes) override; | |
70 | |
71 // MediaRouter implementation. | |
72 // Execution of the requests is delegated to the Do* methods, which can be | |
73 // enqueued for later use if the extension is temporarily suspended. | |
74 void CreateRoute(const MediaSourceId& source_id, | |
75 const MediaSinkId& sink_id, | |
76 const MediaRouteResponseCallback& callback) override; | |
77 void CloseRoute(const MediaRouteId& route_id) override; | |
78 void PostMessage(const MediaRouteId& route_id, | |
79 const std::string& message) override; | |
80 void ClearIssue(const Issue::IssueId& issue_id) override; | |
81 void RegisterMediaSinksObserver(MediaSinksObserver* observer) override; | |
82 void UnregisterMediaSinksObserver(MediaSinksObserver* observer) override; | |
83 void RegisterMediaRoutesObserver(MediaRoutesObserver* observer) override; | |
84 void UnregisterMediaRoutesObserver(MediaRoutesObserver* observer) override; | |
85 void AddIssuesObserver(IssuesObserver* observer) override; | |
86 void RemoveIssuesObserver(IssuesObserver* observer) override; | |
87 | |
88 private: | |
89 friend class MediaRouterMojoImplFactory; | |
90 friend class MediaRouterMojoTest; | |
91 | |
92 FRIEND_TEST_ALL_PREFIXES(MediaRouterMojoExtensionTest, | |
93 DeferredBindingAndSuspension); | |
94 | |
95 // Standard constructor, used by | |
96 // MediaRouterMojoImplFactory::GetApiForBrowserContext. | |
97 MediaRouterMojoImpl(); | |
98 | |
99 // Constructor used only for testing. | |
100 MediaRouterMojoImpl( | |
101 const std::string& mojo_media_router_ext_id, | |
102 extensions::EventPageTracker* event_page_tracker_for_test); | |
103 | |
104 // Binds |this| to a Mojo interface request, so that clients can acquire a | |
105 // handle to a MediaRouterMojoImpl instance via the Mojo service connector. | |
106 void Bind(mojo::InterfaceRequest<interfaces::MediaRouterObserver> request); | |
Wez
2015/05/22 00:05:39
I think you're right that there's no real value in
imcheng (use chromium acct)
2015/05/22 00:54:07
When I tried to change it I realized why it was st
Wez
2015/05/23 00:45:46
That's fine, but in that case you need to make it
imcheng (use chromium acct)
2015/05/26 17:58:31
Both are used in production code, so *ForTest isn'
| |
107 | |
108 // Stores the |extension_id| of the component extension in this instance. | |
109 // Also obtains a reference to the extension's ProcessManager using | |
110 // |context|, which is a reference to the BrowserContext that this | |
111 // instance and extension are keyed on. | |
Wez
2015/05/22 00:05:38
Does "keyed on" mean "belong to", in effect?
imcheng (use chromium acct)
2015/05/22 00:54:07
Yes
Wez
2015/05/23 00:45:46
OK - in that case I'd suggest using "belong to", s
imcheng (use chromium acct)
2015/05/26 17:58:31
Done.
| |
112 void InitExtensionInfo(const std::string& extension_id, | |
113 content::BrowserContext* context); | |
114 | |
115 // Enqueues a closure for later execution by ExecutePendingRequests(). | |
116 void EnqueueTask(const base::Closure& closure); | |
117 | |
118 // Runs a closure if the extension monitored by |extension_monitor_| is | |
119 // active, or defers it for later execution if the extension is suspended. | |
120 void RunOrDefer(const base::Closure& request); | |
121 | |
122 // Runs the closures queued in |pending_requests_|. | |
Wez
2015/05/22 00:05:38
nit: Suggest "Dispatches the Mojo requests queued
imcheng (use chromium acct)
2015/05/22 00:54:07
Done.
| |
123 void ExecutePendingRequests(); | |
124 | |
125 // These calls invoke methods in the MRPM via Mojo. | |
126 void DoCreateRoute(const MediaSourceId& source_id, | |
127 const MediaSinkId& sink_id, | |
128 const MediaRouteResponseCallback& callback); | |
129 void DoCloseRoute(const MediaRouteId& route_id); | |
130 void DoPostMessage(const MediaRouteId& route_id, const std::string& message); | |
131 void DoClearIssue(const Issue::IssueId& issue_id); | |
132 void DoStartObservingMediaSinks(const std::string& source_id); | |
133 void DoStopObservingMediaSinks(const std::string& source_id); | |
134 void DoStartObservingMediaRoutes(); | |
135 void DoStopObservingMediaRoutes(); | |
136 void DoStartObservingIssues(); | |
137 void DoStopObservingIssues(); | |
138 | |
139 // Pending requests queued to be executed once MRPM becomes ready. | |
140 std::vector<base::Closure> pending_requests_; | |
141 | |
142 base::ScopedPtrHashMap<MediaSourceId, | |
143 scoped_ptr<ObserverList<MediaSinksObserver>>> | |
144 sinks_observers_; | |
145 | |
146 ObserverList<MediaRoutesObserver> routes_observers_; | |
147 | |
148 // Binds |this| to a Mojo MediaRouterObserver interface. | |
149 // This binding exists so that MediaRouterObserver calls from the | |
Wez
2015/05/22 00:05:38
That is fairly intuitive from the class names; the
imcheng (use chromium acct)
2015/05/22 00:54:07
Yes. The component extension is a MediaRouter. Jus
Wez
2015/05/23 00:45:47
See above re the class level comment :)
| |
150 // component extensions are routed here. | |
151 // The binding is released when binding_ is deleted. | |
152 scoped_ptr<mojo::Binding<interfaces::MediaRouterObserver>> binding_; | |
153 | |
154 // Mojo proxy object for the Provider Manager. | |
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. | |
Wez
2015/05/22 00:05:38
So this is basically the API through which we send
imcheng (use chromium acct)
2015/05/22 00:54:07
We should really get rid of any references to the
Wez
2015/05/23 00:45:46
Acknowledged.
| |
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 |