imcheng | f3e5a01 | 2015-11-20 04:08:37 | [diff] [blame] | 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 | |
Evan Stade | bbb9f524 | 2020-09-25 17:26:46 | [diff] [blame] | 5 | #ifndef COMPONENTS_MEDIA_ROUTER_BROWSER_MEDIA_ROUTER_BASE_H_ |
| 6 | #define COMPONENTS_MEDIA_ROUTER_BROWSER_MEDIA_ROUTER_BASE_H_ |
imcheng | f3e5a01 | 2015-11-20 04:08:37 | [diff] [blame] | 7 | |
mfoltz | b4ba372 | 2016-03-18 17:13:44 | [diff] [blame] | 8 | #include <set> |
avi | d09ea9f | 2016-12-25 23:07:44 | [diff] [blame] | 9 | #include <unordered_map> |
mfoltz | bdabddf | 2016-04-27 06:10:46 | [diff] [blame] | 10 | #include <vector> |
mfoltz | b4ba372 | 2016-03-18 17:13:44 | [diff] [blame] | 11 | |
imcheng | f3e5a01 | 2015-11-20 04:08:37 | [diff] [blame] | 12 | #include "base/callback_list.h" |
imcheng | f3e5a01 | 2015-11-20 04:08:37 | [diff] [blame] | 13 | #include "base/gtest_prod_util.h" |
| 14 | #include "base/macros.h" |
Takumi Fujimoto | adda1f9b | 2017-08-16 05:19:41 | [diff] [blame] | 15 | #include "build/build_config.h" |
Evan Stade | bbb9f524 | 2020-09-25 17:26:46 | [diff] [blame] | 16 | #include "components/media_router/browser/issue_manager.h" |
| 17 | #include "components/media_router/browser/media_router.h" |
| 18 | #include "components/media_router/browser/media_routes_observer.h" |
Evan Stade | 92b69ca | 2020-08-21 23:55:58 | [diff] [blame] | 19 | #include "components/media_router/common/media_route.h" |
mark a. foltz | 6bbe59f | 2018-07-24 20:06:24 | [diff] [blame] | 20 | #include "third_party/blink/public/mojom/presentation/presentation.mojom.h" |
mfoltz | b4ba372 | 2016-03-18 17:13:44 | [diff] [blame] | 21 | |
Takumi Fujimoto | fd94425d | 2019-08-18 22:36:12 | [diff] [blame] | 22 | #if !defined(OS_ANDROID) |
Evan Stade | 92b69ca | 2020-08-21 23:55:58 | [diff] [blame] | 23 | #include "components/media_router/common/mojom/media_controller.mojom.h" |
Takumi Fujimoto | fd94425d | 2019-08-18 22:36:12 | [diff] [blame] | 24 | #include "mojo/public/cpp/bindings/pending_receiver.h" |
Mario Sanchez Prada | 4cdf6ead | 2019-09-24 10:42:01 | [diff] [blame] | 25 | #include "mojo/public/cpp/bindings/pending_remote.h" |
Takumi Fujimoto | fd94425d | 2019-08-18 22:36:12 | [diff] [blame] | 26 | #endif // !defined(OS_ANDROID) |
| 27 | |
imcheng | f3e5a01 | 2015-11-20 04:08:37 | [diff] [blame] | 28 | namespace media_router { |
| 29 | |
| 30 | class MediaRouterBase : public MediaRouter { |
| 31 | public: |
imcheng | f3e5a01 | 2015-11-20 04:08:37 | [diff] [blame] | 32 | ~MediaRouterBase() override; |
| 33 | |
Evan Stade | bbb9f524 | 2020-09-25 17:26:46 | [diff] [blame] | 34 | // Must be called before invoking any other method. |
| 35 | void Initialize(); |
| 36 | |
Derek Cheng | e3fb6f7 | 2017-09-19 00:58:02 | [diff] [blame] | 37 | // MediaRouter implementation. |
dcheng | c16dc809 | 2016-04-08 23:12:56 | [diff] [blame] | 38 | std::unique_ptr<PresentationConnectionStateSubscription> |
imcheng | f3e5a01 | 2015-11-20 04:08:37 | [diff] [blame] | 39 | AddPresentationConnectionStateChangedCallback( |
| 40 | const MediaRoute::Id& route_id, |
| 41 | const content::PresentationConnectionStateChangedCallback& callback) |
| 42 | override; |
trizzofo | 9f8e900b | 2016-07-21 22:24:41 | [diff] [blame] | 43 | void OnIncognitoProfileShutdown() override; |
Derek Cheng | e3fb6f7 | 2017-09-19 00:58:02 | [diff] [blame] | 44 | IssueManager* GetIssueManager() final; |
takumif | 21ad18d | 2016-12-05 18:50:04 | [diff] [blame] | 45 | std::vector<MediaRoute> GetCurrentRoutes() const override; |
Thomas Guilbert | fc4431b | 2018-07-20 19:05:47 | [diff] [blame] | 46 | std::unique_ptr<media::FlingingController> GetFlingingController( |
Thomas Guilbert | 42b4665 | 2018-03-12 23:01:47 | [diff] [blame] | 47 | const MediaRoute::Id& route_id) override; |
Takumi Fujimoto | adda1f9b | 2017-08-16 05:19:41 | [diff] [blame] | 48 | #if !defined(OS_ANDROID) |
Takumi Fujimoto | fd94425d | 2019-08-18 22:36:12 | [diff] [blame] | 49 | void GetMediaController( |
| 50 | const MediaRoute::Id& route_id, |
| 51 | mojo::PendingReceiver<mojom::MediaController> controller, |
Mario Sanchez Prada | 4cdf6ead | 2019-09-24 10:42:01 | [diff] [blame] | 52 | mojo::PendingRemote<mojom::MediaStatusObserver> observer) override; |
Takumi Fujimoto | d470c93 | 2020-08-06 22:50:43 | [diff] [blame] | 53 | base::Value GetLogs() const override; |
Takumi Fujimoto | adda1f9b | 2017-08-16 05:19:41 | [diff] [blame] | 54 | #endif // !defined(OS_ANDROID) |
Bin Zhao | 59c6647 | 2018-03-07 20:30:45 | [diff] [blame] | 55 | base::Value GetState() const override; |
mark a. foltz | e6157156 | 2020-03-09 18:45:41 | [diff] [blame] | 56 | void GetProviderState( |
| 57 | MediaRouteProviderId provider_id, |
| 58 | mojom::MediaRouteProvider::GetStateCallback callback) const override; |
xjz | ef7927f | 2017-07-07 20:58:19 | [diff] [blame] | 59 | |
imcheng | f3e5a01 | 2015-11-20 04:08:37 | [diff] [blame] | 60 | protected: |
| 61 | FRIEND_TEST_ALL_PREFIXES(MediaRouterMojoImplTest, |
| 62 | PresentationConnectionStateChangedCallback); |
imcheng | ed10e96 | 2016-02-23 06:34:44 | [diff] [blame] | 63 | FRIEND_TEST_ALL_PREFIXES(MediaRouterMojoImplTest, |
| 64 | PresentationConnectionStateChangedCallbackRemoved); |
takumif | 6eab02de | 2016-10-11 22:39:55 | [diff] [blame] | 65 | FRIEND_TEST_ALL_PREFIXES(MediaRouterBaseTest, CreatePresentationIds); |
| 66 | FRIEND_TEST_ALL_PREFIXES(MediaRouterBaseTest, NotifyCallbacks); |
imcheng | f3e5a01 | 2015-11-20 04:08:37 | [diff] [blame] | 67 | |
mfoltz | bdabddf | 2016-04-27 06:10:46 | [diff] [blame] | 68 | MediaRouterBase(); |
| 69 | |
avayvod | ca19801 | 2016-04-18 17:21:18 | [diff] [blame] | 70 | // Generates a unique presentation id. Shared between Android and desktop. |
| 71 | static std::string CreatePresentationId(); |
| 72 | |
imcheng | f3e5a01 | 2015-11-20 04:08:37 | [diff] [blame] | 73 | void NotifyPresentationConnectionStateChange( |
| 74 | const MediaRoute::Id& route_id, |
mark a. foltz | e8ad320 | 2018-04-23 22:50:23 | [diff] [blame] | 75 | blink::mojom::PresentationConnectionState state); |
imcheng | ed10e96 | 2016-02-23 06:34:44 | [diff] [blame] | 76 | void NotifyPresentationConnectionClose( |
| 77 | const MediaRoute::Id& route_id, |
mark a. foltz | 67461c8 | 2018-04-13 17:05:41 | [diff] [blame] | 78 | blink::mojom::PresentationConnectionCloseReason reason, |
imcheng | ed10e96 | 2016-02-23 06:34:44 | [diff] [blame] | 79 | const std::string& message); |
imcheng | f3e5a01 | 2015-11-20 04:08:37 | [diff] [blame] | 80 | |
mfoltz | 0ae252e8 | 2016-05-12 19:30:55 | [diff] [blame] | 81 | // Returns true when there is at least one MediaRoute that can be returned by |
| 82 | // JoinRoute(). |
| 83 | bool HasJoinableRoute() const; |
mfoltz | b4ba372 | 2016-03-18 17:13:44 | [diff] [blame] | 84 | |
Derek Cheng | 76870b6a | 2017-09-07 03:08:38 | [diff] [blame] | 85 | // Returns a pointer to the MediaRoute whose ID is |route_id|, or nullptr |
| 86 | // if not found. |
| 87 | const MediaRoute* GetRoute(const MediaRoute::Id& route_id) const; |
takumif | 3c8ebf96 | 2017-04-18 05:29:25 | [diff] [blame] | 88 | |
imcheng | ed10e96 | 2016-02-23 06:34:44 | [diff] [blame] | 89 | using PresentationConnectionStateChangedCallbacks = base::CallbackList<void( |
| 90 | const content::PresentationConnectionStateChangeInfo&)>; |
mfoltz | bdabddf | 2016-04-27 06:10:46 | [diff] [blame] | 91 | |
avi | d09ea9f | 2016-12-25 23:07:44 | [diff] [blame] | 92 | std::unordered_map< |
imcheng | f3e5a01 | 2015-11-20 04:08:37 | [diff] [blame] | 93 | MediaRoute::Id, |
dcheng | c16dc809 | 2016-04-08 23:12:56 | [diff] [blame] | 94 | std::unique_ptr<PresentationConnectionStateChangedCallbacks>> |
imcheng | f3e5a01 | 2015-11-20 04:08:37 | [diff] [blame] | 95 | presentation_connection_state_callbacks_; |
| 96 | |
imcheng | f3e5a01 | 2015-11-20 04:08:37 | [diff] [blame] | 97 | private: |
takumif | 21ad18d | 2016-12-05 18:50:04 | [diff] [blame] | 98 | friend class MediaRouterBaseTest; |
mfoltz | bdabddf | 2016-04-27 06:10:46 | [diff] [blame] | 99 | friend class MediaRouterMojoTest; |
| 100 | |
| 101 | class InternalMediaRoutesObserver; |
| 102 | |
imcheng | f3e5a01 | 2015-11-20 04:08:37 | [diff] [blame] | 103 | // Called when a PresentationConnectionStateChangedCallback associated with |
| 104 | // |route_id| is removed from |presentation_connection_state_callbacks_|. |
| 105 | void OnPresentationConnectionStateCallbackRemoved( |
| 106 | const MediaRoute::Id& route_id); |
| 107 | |
mfoltz | bdabddf | 2016-04-27 06:10:46 | [diff] [blame] | 108 | // KeyedService |
| 109 | void Shutdown() override; |
| 110 | |
Derek Cheng | e3fb6f7 | 2017-09-19 00:58:02 | [diff] [blame] | 111 | IssueManager issue_manager_; |
| 112 | |
mfoltz | bdabddf | 2016-04-27 06:10:46 | [diff] [blame] | 113 | std::unique_ptr<InternalMediaRoutesObserver> internal_routes_observer_; |
| 114 | bool initialized_; |
mfoltz | b4ba372 | 2016-03-18 17:13:44 | [diff] [blame] | 115 | |
imcheng | f3e5a01 | 2015-11-20 04:08:37 | [diff] [blame] | 116 | DISALLOW_COPY_AND_ASSIGN(MediaRouterBase); |
| 117 | }; |
| 118 | |
| 119 | } // namespace media_router |
| 120 | |
Evan Stade | bbb9f524 | 2020-09-25 17:26:46 | [diff] [blame] | 121 | #endif // COMPONENTS_MEDIA_ROUTER_BROWSER_MEDIA_ROUTER_BASE_H_ |