Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / media / router / media_router_mojo_impl.h
blobcc96fb20fb7cc68925cfcafc7bf298996c2abf9f
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.
5 #ifndef CHROME_BROWSER_MEDIA_ROUTER_MEDIA_ROUTER_MOJO_IMPL_H_
6 #define CHROME_BROWSER_MEDIA_ROUTER_MEDIA_ROUTER_MOJO_IMPL_H_
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <vector>
13 #include "base/containers/hash_tables.h"
14 #include "base/containers/scoped_ptr_hash_map.h"
15 #include "base/macros.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/memory/weak_ptr.h"
19 #include "base/observer_list.h"
20 #include "base/thread_task_runner_handle.h"
21 #include "base/threading/thread_checker.h"
22 #include "chrome/browser/media/router/issue.h"
23 #include "chrome/browser/media/router/issue_manager.h"
24 #include "chrome/browser/media/router/media_router.h"
25 #include "chrome/browser/media/router/media_router.mojom.h"
26 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h"
28 namespace content {
29 class BrowserContext;
32 namespace extensions {
33 class EventPageTracker;
36 namespace media_router {
38 // MediaRouter implementation that delegates calls to the component extension.
39 // Also handles the suspension and wakeup of the component extension.
40 class MediaRouterMojoImpl : public MediaRouter,
41 public interfaces::MediaRouter {
42 public:
43 ~MediaRouterMojoImpl() override;
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::MediaRouter> request);
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(
63 const MediaSource::Id& source_id,
64 const MediaSink::Id& sink_id,
65 const GURL& origin,
66 int tab_id,
67 const std::vector<MediaRouteResponseCallback>& callbacks) override;
68 void JoinRoute(
69 const MediaSource::Id& source_id,
70 const std::string& presentation_id,
71 const GURL& origin,
72 int tab_id,
73 const std::vector<MediaRouteResponseCallback>& callbacks) override;
74 void CloseRoute(const MediaRoute::Id& route_id) override;
75 void SendRouteMessage(const MediaRoute::Id& route_id,
76 const std::string& message,
77 const SendRouteMessageCallback& callback) override;
78 void SendRouteBinaryMessage(
79 const MediaRoute::Id& route_id,
80 scoped_ptr<std::vector<uint8>> data,
81 const SendRouteMessageCallback& callback) override;
82 void ClearIssue(const Issue::Id& issue_id) override;
83 void OnPresentationSessionDetached(const MediaRoute::Id& route_id) override;
85 const std::string& media_route_provider_extension_id() const {
86 return media_route_provider_extension_id_;
89 void set_instance_id_for_test(const std::string& instance_id) {
90 instance_id_ = instance_id;
93 private:
94 friend class MediaRouterFactory;
95 friend class MediaRouterMojoTest;
97 FRIEND_TEST_ALL_PREFIXES(MediaRouterMojoImplTest,
98 RegisterAndUnregisterMediaSinksObserver);
99 FRIEND_TEST_ALL_PREFIXES(MediaRouterMojoImplTest,
100 RegisterAndUnregisterMediaRoutesObserver);
101 FRIEND_TEST_ALL_PREFIXES(MediaRouterMojoImplTest, HandleIssue);
102 FRIEND_TEST_ALL_PREFIXES(MediaRouterMojoExtensionTest,
103 DeferredBindingAndSuspension);
105 // Standard constructor, used by
106 // MediaRouterMojoImplFactory::GetApiForBrowserContext.
107 explicit MediaRouterMojoImpl(
108 extensions::EventPageTracker* event_page_tracker);
110 // Binds |this| to a Mojo interface request, so that clients can acquire a
111 // handle to a MediaRouterMojoImpl instance via the Mojo service connector.
112 // Stores the |extension_id| of the component extension.
113 void BindToMojoRequest(
114 mojo::InterfaceRequest<interfaces::MediaRouter> request,
115 const std::string& extension_id);
117 // Enqueues a closure for later execution by ExecutePendingRequests().
118 void EnqueueTask(const base::Closure& closure);
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);
124 // Dispatches the Mojo requests queued in |pending_requests_|.
125 void ExecutePendingRequests();
127 // MediaRouter implementation.
128 void RegisterMediaSinksObserver(MediaSinksObserver* observer) override;
129 void UnregisterMediaSinksObserver(MediaSinksObserver* observer) override;
130 void RegisterMediaRoutesObserver(MediaRoutesObserver* observer) override;
131 void UnregisterMediaRoutesObserver(MediaRoutesObserver* observer) override;
132 void RegisterIssuesObserver(IssuesObserver* observer) override;
133 void UnregisterIssuesObserver(IssuesObserver* observer) override;
134 void RegisterPresentationSessionMessagesObserver(
135 PresentationSessionMessagesObserver* observer) override;
136 void UnregisterPresentationSessionMessagesObserver(
137 PresentationSessionMessagesObserver* observer) override;
139 // These calls invoke methods in the component extension via Mojo.
140 void DoCreateRoute(const MediaSource::Id& source_id,
141 const MediaSink::Id& sink_id,
142 const std::string& origin,
143 int tab_id,
144 const std::vector<MediaRouteResponseCallback>& callbacks);
145 void DoJoinRoute(const MediaSource::Id& source_id,
146 const std::string& presentation_id,
147 const std::string& origin,
148 int tab_id,
149 const std::vector<MediaRouteResponseCallback>& callbacks);
150 void DoCloseRoute(const MediaRoute::Id& route_id);
151 void DoSendSessionMessage(const MediaRoute::Id& route_id,
152 const std::string& message,
153 const SendRouteMessageCallback& callback);
154 void DoSendSessionBinaryMessage(const MediaRoute::Id& route_id,
155 scoped_ptr<std::vector<uint8>> data,
156 const SendRouteMessageCallback& callback);
157 void DoListenForRouteMessages(const MediaRoute::Id& route_id);
158 void DoStopListeningForRouteMessages(const MediaRoute::Id& route_id);
159 void DoOnPresentationSessionDetached(const MediaRoute::Id& route_id);
160 void DoStartObservingMediaSinks(const MediaSource::Id& source_id);
161 void DoStopObservingMediaSinks(const MediaSource::Id& source_id);
162 void DoStartObservingMediaRoutes();
163 void DoStopObservingMediaRoutes();
165 // Invoked when the next batch of messages arrives.
166 // |route_id|: ID of route of the messages.
167 // |messages|: A list of messages received.
168 // |error|: true if an error occurred.
169 void OnRouteMessagesReceived(
170 const MediaRoute::Id& route_id,
171 mojo::Array<interfaces::RouteMessagePtr> messages,
172 bool error);
174 // Error handler callback for |binding_| and |media_route_provider_|.
175 void OnConnectionError();
177 // interfaces::MediaRouter implementation.
178 void RegisterMediaRouteProvider(
179 interfaces::MediaRouteProviderPtr media_route_provider_ptr,
180 const interfaces::MediaRouter::RegisterMediaRouteProviderCallback&
181 callback) override;
182 void OnIssue(interfaces::IssuePtr issue) override;
183 void OnSinksReceived(const mojo::String& media_source,
184 mojo::Array<interfaces::MediaSinkPtr> sinks) override;
185 void OnRoutesUpdated(mojo::Array<interfaces::MediaRoutePtr> routes) override;
187 // Pending requests queued to be executed once component extension
188 // becomes ready.
189 std::vector<base::Closure> pending_requests_;
191 base::ScopedPtrHashMap<MediaSource::Id,
192 scoped_ptr<base::ObserverList<MediaSinksObserver>>>
193 sinks_observers_;
195 base::ObserverList<MediaRoutesObserver> routes_observers_;
197 using PresentationSessionMessagesObserverList =
198 base::ObserverList<PresentationSessionMessagesObserver>;
199 base::ScopedPtrHashMap<MediaRoute::Id,
200 scoped_ptr<PresentationSessionMessagesObserverList>>
201 messages_observers_;
202 // IDs of MediaRoutes being listened for messages. Note that this is
203 // different from |message_observers_| because we might be waiting for
204 // |OnRouteMessagesReceived()| to be invoked after all observers for that
205 // route have been removed.
206 std::set<MediaRoute::Id> route_ids_listening_for_messages_;
208 IssueManager issue_manager_;
210 // Binds |this| to a Mojo connection stub for interfaces::MediaRouter.
211 scoped_ptr<mojo::Binding<interfaces::MediaRouter>> binding_;
213 // Mojo proxy object for the Media Route Provider Manager.
214 // Set to null initially, and later set to the Provider Manager proxy object
215 // passed in via |RegisterMediaRouteProvider()|.
216 // This is set to null again when the component extension is suspended
217 // if or a Mojo channel error occured.
218 interfaces::MediaRouteProviderPtr media_route_provider_;
220 // Id of the component extension. Used for managing its suspend/wake state
221 // via event_page_tracker_.
222 std::string media_route_provider_extension_id_;
224 // Allows the extension to be monitored for suspend, and woken.
225 // This is a reference to a BrowserContext keyed service that outlives this
226 // instance.
227 extensions::EventPageTracker* event_page_tracker_;
229 // GUID unique to each browser run. Component extension uses this to detect
230 // when its persisted state was written by an older browser instance, and is
231 // therefore stale.
232 std::string instance_id_;
234 base::ThreadChecker thread_checker_;
236 DISALLOW_COPY_AND_ASSIGN(MediaRouterMojoImpl);
239 } // namespace media_router
241 #endif // CHROME_BROWSER_MEDIA_ROUTER_MEDIA_ROUTER_MOJO_IMPL_H_