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