| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_MEDIA_ROUTER_MEDIA_ROUTER_MOJO_IMPL_H_ | 5 #ifndef CHROME_BROWSER_MEDIA_ROUTER_MEDIA_ROUTER_MOJO_IMPL_H_ |
| 6 #define CHROME_BROWSER_MEDIA_ROUTER_MEDIA_ROUTER_MOJO_IMPL_H_ | 6 #define CHROME_BROWSER_MEDIA_ROUTER_MEDIA_ROUTER_MOJO_IMPL_H_ |
| 7 | 7 |
| 8 #include <deque> |
| 8 #include <map> | 9 #include <map> |
| 9 #include <set> | 10 #include <set> |
| 10 #include <string> | 11 #include <string> |
| 11 #include <vector> | 12 #include <vector> |
| 12 | 13 |
| 13 #include "base/containers/hash_tables.h" | 14 #include "base/containers/hash_tables.h" |
| 14 #include "base/containers/scoped_ptr_hash_map.h" | 15 #include "base/containers/scoped_ptr_hash_map.h" |
| 15 #include "base/gtest_prod_util.h" | 16 #include "base/gtest_prod_util.h" |
| 16 #include "base/macros.h" | 17 #include "base/macros.h" |
| 17 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 } | 92 } |
| 92 | 93 |
| 93 void set_instance_id_for_test(const std::string& instance_id) { | 94 void set_instance_id_for_test(const std::string& instance_id) { |
| 94 instance_id_ = instance_id; | 95 instance_id_ = instance_id; |
| 95 } | 96 } |
| 96 | 97 |
| 97 private: | 98 private: |
| 98 friend class MediaRouterFactory; | 99 friend class MediaRouterFactory; |
| 99 friend class MediaRouterMojoExtensionTest; | 100 friend class MediaRouterMojoExtensionTest; |
| 100 friend class MediaRouterMojoTest; | 101 friend class MediaRouterMojoTest; |
| 101 | |
| 102 FRIEND_TEST_ALL_PREFIXES(MediaRouterMojoImplTest, | 102 FRIEND_TEST_ALL_PREFIXES(MediaRouterMojoImplTest, |
| 103 RegisterAndUnregisterMediaSinksObserver); | 103 RegisterAndUnregisterMediaSinksObserver); |
| 104 FRIEND_TEST_ALL_PREFIXES(MediaRouterMojoImplTest, | 104 FRIEND_TEST_ALL_PREFIXES(MediaRouterMojoImplTest, |
| 105 RegisterAndUnregisterMediaRoutesObserver); | 105 RegisterAndUnregisterMediaRoutesObserver); |
| 106 FRIEND_TEST_ALL_PREFIXES(MediaRouterMojoImplTest, HandleIssue); | 106 FRIEND_TEST_ALL_PREFIXES(MediaRouterMojoImplTest, HandleIssue); |
| 107 FRIEND_TEST_ALL_PREFIXES(MediaRouterMojoExtensionTest, | 107 FRIEND_TEST_ALL_PREFIXES(MediaRouterMojoExtensionTest, |
| 108 DeferredBindingAndSuspension); | 108 DeferredBindingAndSuspension); |
| 109 FRIEND_TEST_ALL_PREFIXES(MediaRouterMojoExtensionTest, |
| 110 DrainPendingRequestQueue); |
| 111 FRIEND_TEST_ALL_PREFIXES(MediaRouterMojoExtensionTest, |
| 112 DropOldestPendingRequest); |
| 113 FRIEND_TEST_ALL_PREFIXES(MediaRouterMojoExtensionTest, |
| 114 AttemptedWakeupTooManyTimes); |
| 115 FRIEND_TEST_ALL_PREFIXES(MediaRouterMojoExtensionTest, |
| 116 WakeupFailedDrainsQueue); |
| 117 |
| 118 // The max number of pending requests allowed. When number of pending requests |
| 119 // exceeds this number, the oldest request will be dropped. |
| 120 static const int kMaxPendingRequests = 30; |
| 121 |
| 122 // Max consecutive attempts to wake up the component extension before |
| 123 // giving up and draining the pending request queue. |
| 124 static const int kMaxWakeupAttemptCount = 3; |
| 109 | 125 |
| 110 class MediaRouterMediaRoutesObserver : | 126 class MediaRouterMediaRoutesObserver : |
| 111 public media_router::MediaRoutesObserver { | 127 public media_router::MediaRoutesObserver { |
| 112 public: | 128 public: |
| 113 explicit MediaRouterMediaRoutesObserver(MediaRouterMojoImpl* router); | 129 explicit MediaRouterMediaRoutesObserver(MediaRouterMojoImpl* router); |
| 114 ~MediaRouterMediaRoutesObserver() override; | 130 ~MediaRouterMediaRoutesObserver() override; |
| 115 | 131 |
| 116 // media_router::MediaRoutesObserver: | 132 // media_router::MediaRoutesObserver: |
| 117 void OnRoutesUpdated(const std::vector<media_router::MediaRoute>& routes) | 133 void OnRoutesUpdated(const std::vector<media_router::MediaRoute>& routes) |
| 118 override; | 134 override; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 138 // Enqueues a closure for later execution by ExecutePendingRequests(). | 154 // Enqueues a closure for later execution by ExecutePendingRequests(). |
| 139 void EnqueueTask(const base::Closure& closure); | 155 void EnqueueTask(const base::Closure& closure); |
| 140 | 156 |
| 141 // Runs a closure if the extension monitored by |extension_monitor_| is | 157 // Runs a closure if the extension monitored by |extension_monitor_| is |
| 142 // active, or defers it for later execution if the extension is suspended. | 158 // active, or defers it for later execution if the extension is suspended. |
| 143 void RunOrDefer(const base::Closure& request); | 159 void RunOrDefer(const base::Closure& request); |
| 144 | 160 |
| 145 // Dispatches the Mojo requests queued in |pending_requests_|. | 161 // Dispatches the Mojo requests queued in |pending_requests_|. |
| 146 void ExecutePendingRequests(); | 162 void ExecutePendingRequests(); |
| 147 | 163 |
| 164 // Drops all pending requests. Called when we have a connection error to |
| 165 // component extension and further reattempts are unlikely to help. |
| 166 void DrainPendingRequests(); |
| 167 |
| 148 // MediaRouter implementation. | 168 // MediaRouter implementation. |
| 149 bool RegisterMediaSinksObserver(MediaSinksObserver* observer) override; | 169 bool RegisterMediaSinksObserver(MediaSinksObserver* observer) override; |
| 150 void UnregisterMediaSinksObserver(MediaSinksObserver* observer) override; | 170 void UnregisterMediaSinksObserver(MediaSinksObserver* observer) override; |
| 151 void RegisterMediaRoutesObserver(MediaRoutesObserver* observer) override; | 171 void RegisterMediaRoutesObserver(MediaRoutesObserver* observer) override; |
| 152 void UnregisterMediaRoutesObserver(MediaRoutesObserver* observer) override; | 172 void UnregisterMediaRoutesObserver(MediaRoutesObserver* observer) override; |
| 153 void RegisterIssuesObserver(IssuesObserver* observer) override; | 173 void RegisterIssuesObserver(IssuesObserver* observer) override; |
| 154 void UnregisterIssuesObserver(IssuesObserver* observer) override; | 174 void UnregisterIssuesObserver(IssuesObserver* observer) override; |
| 155 void RegisterPresentationSessionMessagesObserver( | 175 void RegisterPresentationSessionMessagesObserver( |
| 156 PresentationSessionMessagesObserver* observer) override; | 176 PresentationSessionMessagesObserver* observer) override; |
| 157 void UnregisterPresentationSessionMessagesObserver( | 177 void UnregisterPresentationSessionMessagesObserver( |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 // Converts the callback result of calling Mojo CreateRoute()/JoinRoute() | 232 // Converts the callback result of calling Mojo CreateRoute()/JoinRoute() |
| 213 // into a local callback. | 233 // into a local callback. |
| 214 void RouteResponseReceived( | 234 void RouteResponseReceived( |
| 215 const std::string& presentation_id, | 235 const std::string& presentation_id, |
| 216 const std::vector<MediaRouteResponseCallback>& callbacks, | 236 const std::vector<MediaRouteResponseCallback>& callbacks, |
| 217 interfaces::MediaRoutePtr media_route, | 237 interfaces::MediaRoutePtr media_route, |
| 218 const mojo::String& error_text); | 238 const mojo::String& error_text); |
| 219 | 239 |
| 220 void UpdateHasLocalRoute(bool has_local_route); | 240 void UpdateHasLocalRoute(bool has_local_route); |
| 221 | 241 |
| 242 // Callback invoked by |event_page_tracker_| after an attempt to wake the |
| 243 // component extension. If |success| is false, the pending request queue is |
| 244 // drained. |
| 245 void EventPageWakeComplete(bool success); |
| 246 |
| 247 // Removes all requests from the pending requests queue. Called when there is |
| 248 // a permanent error connecting to component extension. |
| 249 void DrainRequestQueue(); |
| 250 |
| 251 // Calls to |event_page_tracker_| to wake the component extension. |
| 252 // |media_route_provider_extension_id_| must not be empty and the extension |
| 253 // should be currently suspended. |
| 254 // If there have already been too many wakeup attempts, give up and drain |
| 255 // the pending request queue. |
| 256 void AttemptWakeEventPage(); |
| 257 |
| 222 // Pending requests queued to be executed once component extension | 258 // Pending requests queued to be executed once component extension |
| 223 // becomes ready. | 259 // becomes ready. |
| 224 std::vector<base::Closure> pending_requests_; | 260 std::deque<base::Closure> pending_requests_; |
| 225 | 261 |
| 226 base::ScopedPtrHashMap<MediaSource::Id, | 262 base::ScopedPtrHashMap<MediaSource::Id, |
| 227 scoped_ptr<base::ObserverList<MediaSinksObserver>>> | 263 scoped_ptr<base::ObserverList<MediaSinksObserver>>> |
| 228 sinks_observers_; | 264 sinks_observers_; |
| 229 | 265 |
| 230 base::ObserverList<LocalMediaRoutesObserver> local_routes_observers_; | 266 base::ObserverList<LocalMediaRoutesObserver> local_routes_observers_; |
| 231 | 267 |
| 232 base::ObserverList<MediaRoutesObserver> routes_observers_; | 268 base::ObserverList<MediaRoutesObserver> routes_observers_; |
| 233 | 269 |
| 234 using PresentationSessionMessagesObserverList = | 270 using PresentationSessionMessagesObserverList = |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 // therefore stale. | 304 // therefore stale. |
| 269 std::string instance_id_; | 305 std::string instance_id_; |
| 270 | 306 |
| 271 // Set to true if there are routes started on this instance. | 307 // Set to true if there are routes started on this instance. |
| 272 bool has_local_route_; | 308 bool has_local_route_; |
| 273 | 309 |
| 274 // Observes local routes in order to notify LocalMediaRoutesObservers when | 310 // Observes local routes in order to notify LocalMediaRoutesObservers when |
| 275 // there are no more local routes. | 311 // there are no more local routes. |
| 276 scoped_ptr<MediaRoutesObserver> routes_observer_; | 312 scoped_ptr<MediaRoutesObserver> routes_observer_; |
| 277 | 313 |
| 314 int wakeup_attempt_count_; |
| 315 |
| 278 base::ThreadChecker thread_checker_; | 316 base::ThreadChecker thread_checker_; |
| 279 | 317 |
| 318 base::WeakPtrFactory<MediaRouterMojoImpl> weak_factory_; |
| 319 |
| 280 DISALLOW_COPY_AND_ASSIGN(MediaRouterMojoImpl); | 320 DISALLOW_COPY_AND_ASSIGN(MediaRouterMojoImpl); |
| 281 }; | 321 }; |
| 282 | 322 |
| 283 } // namespace media_router | 323 } // namespace media_router |
| 284 | 324 |
| 285 #endif // CHROME_BROWSER_MEDIA_ROUTER_MEDIA_ROUTER_MOJO_IMPL_H_ | 325 #endif // CHROME_BROWSER_MEDIA_ROUTER_MEDIA_ROUTER_MOJO_IMPL_H_ |
| OLD | NEW |