2020-03-19 16:34:15 +01:00
|
|
|
// Copyright (c) 2020 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
|
|
|
#ifndef CEF_LIBCEF_BROWSER_MEDIA_ROUTER_MEDIA_ROUTER_MANAGER_H_
|
|
|
|
#define CEF_LIBCEF_BROWSER_MEDIA_ROUTER_MEDIA_ROUTER_MANAGER_H_
|
|
|
|
#pragma once
|
|
|
|
|
2024-05-11 17:48:38 +02:00
|
|
|
#include "base/memory/raw_ptr.h"
|
2020-03-19 16:34:15 +01:00
|
|
|
#include "base/memory/weak_ptr.h"
|
|
|
|
#include "base/observer_list.h"
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/include/cef_media_router.h"
|
2020-03-19 16:34:15 +01:00
|
|
|
#include "chrome/browser/ui/media_router/query_result_manager.h"
|
2020-10-08 21:54:42 +02:00
|
|
|
#include "components/media_router/browser/media_router.h"
|
|
|
|
#include "components/media_router/common/mojom/media_router.mojom.h"
|
2020-03-19 16:34:15 +01:00
|
|
|
|
2020-07-01 02:57:00 +02:00
|
|
|
namespace content {
|
|
|
|
class BrowserContext;
|
|
|
|
}
|
|
|
|
|
2020-03-19 16:34:15 +01:00
|
|
|
class CefMediaRoutesObserver;
|
|
|
|
class CefPresentationConnection;
|
2023-01-03 00:34:43 +01:00
|
|
|
class CefPresentationConnectionMessageObserver;
|
2020-03-19 16:34:15 +01:00
|
|
|
|
|
|
|
// Manages CEF usage of MediaRouter. Owned by CefBrowserContext and only
|
|
|
|
// accessed on the UI thread.
|
|
|
|
class CefMediaRouterManager
|
2022-04-21 20:58:48 +02:00
|
|
|
: public media_router::MediaSinkWithCastModesObserver {
|
2020-03-19 16:34:15 +01:00
|
|
|
public:
|
|
|
|
using MediaRouteVector = std::vector<media_router::MediaRoute>;
|
|
|
|
using MediaSinkVector = std::vector<media_router::MediaSinkWithCastModes>;
|
|
|
|
using MediaMessageVector = std::vector<media_router::mojom::RouteMessagePtr>;
|
|
|
|
|
|
|
|
class Observer : public base::CheckedObserver {
|
|
|
|
public:
|
|
|
|
virtual void OnMediaRouterDestroyed() = 0;
|
|
|
|
|
|
|
|
virtual void OnMediaSinks(const MediaSinkVector& sinks) = 0;
|
|
|
|
virtual void OnMediaRoutes(const MediaRouteVector& routes) = 0;
|
|
|
|
|
|
|
|
virtual void OnMediaRouteMessages(const media_router::MediaRoute& route,
|
|
|
|
const MediaMessageVector& messages) = 0;
|
|
|
|
virtual void OnMediaRouteStateChange(
|
|
|
|
const media_router::MediaRoute& route,
|
|
|
|
const content::PresentationConnectionStateChangeInfo& info) = 0;
|
|
|
|
|
|
|
|
protected:
|
2024-01-20 23:48:57 +01:00
|
|
|
~Observer() override = default;
|
2020-03-19 16:34:15 +01:00
|
|
|
};
|
|
|
|
|
2020-07-01 02:57:00 +02:00
|
|
|
explicit CefMediaRouterManager(content::BrowserContext* browser_context);
|
2021-12-06 21:40:25 +01:00
|
|
|
|
|
|
|
CefMediaRouterManager(const CefMediaRouterManager&) = delete;
|
|
|
|
CefMediaRouterManager& operator=(const CefMediaRouterManager&) = delete;
|
|
|
|
|
2020-03-19 16:34:15 +01:00
|
|
|
~CefMediaRouterManager() override;
|
|
|
|
|
|
|
|
// |observer| must outlive this object or be removed.
|
|
|
|
void AddObserver(Observer* observer);
|
|
|
|
void RemoveObserver(Observer* observer);
|
|
|
|
|
|
|
|
void NotifyCurrentSinks();
|
|
|
|
void NotifyCurrentRoutes();
|
|
|
|
|
|
|
|
using CreateRouteResultCallback =
|
|
|
|
base::OnceCallback<void(const media_router::RouteRequestResult& result)>;
|
|
|
|
|
|
|
|
void CreateRoute(const media_router::MediaSource::Id& source_id,
|
|
|
|
const media_router::MediaSink::Id& sink_id,
|
|
|
|
const url::Origin& origin,
|
|
|
|
CreateRouteResultCallback callback);
|
|
|
|
|
|
|
|
void SendRouteMessage(const media_router::MediaRoute::Id& route_id,
|
|
|
|
const std::string& message);
|
|
|
|
void TerminateRoute(const media_router::MediaRoute::Id& route_id);
|
|
|
|
|
2022-04-21 20:58:48 +02:00
|
|
|
// MediaSinkWithCastModesObserver methods.
|
|
|
|
void OnSinksUpdated(const MediaSinkVector& sinks) override;
|
2020-03-19 16:34:15 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend class CefMediaRoutesObserver;
|
|
|
|
friend class CefPresentationConnection;
|
2023-01-03 00:34:43 +01:00
|
|
|
friend class CefPresentationConnectionMessageObserver;
|
2020-03-19 16:34:15 +01:00
|
|
|
|
|
|
|
// Do not keep a reference to the object returned by this method.
|
|
|
|
media_router::MediaRouter* GetMediaRouter() const;
|
|
|
|
|
|
|
|
void OnCreateRoute(
|
|
|
|
CreateRouteResultCallback callback,
|
|
|
|
media_router::mojom::RoutePresentationConnectionPtr connection,
|
|
|
|
const media_router::RouteRequestResult& result);
|
|
|
|
void OnRouteStateChange(
|
|
|
|
const media_router::MediaRoute& route,
|
|
|
|
const content::PresentationConnectionStateChangeInfo& info);
|
|
|
|
void OnMessagesReceived(const media_router::MediaRoute& route,
|
|
|
|
const MediaMessageVector& messages);
|
|
|
|
|
|
|
|
struct RouteState {
|
|
|
|
std::unique_ptr<CefPresentationConnection> presentation_connection_;
|
|
|
|
|
|
|
|
// Used if there is no RoutePresentationConnectionPtr.
|
2023-01-03 00:34:43 +01:00
|
|
|
std::unique_ptr<CefPresentationConnectionMessageObserver> message_observer_;
|
2021-01-28 00:13:12 +01:00
|
|
|
base::CallbackListSubscription state_subscription_;
|
2020-03-19 16:34:15 +01:00
|
|
|
};
|
|
|
|
void CreateRouteState(
|
|
|
|
const media_router::MediaRoute& route,
|
|
|
|
media_router::mojom::RoutePresentationConnectionPtr connection);
|
|
|
|
RouteState* GetRouteState(const media_router::MediaRoute::Id& route_id);
|
|
|
|
void RemoveRouteState(const media_router::MediaRoute::Id& route_id);
|
|
|
|
|
2024-05-11 17:48:38 +02:00
|
|
|
const raw_ptr<content::BrowserContext> browser_context_;
|
2020-03-19 16:34:15 +01:00
|
|
|
|
|
|
|
base::ObserverList<Observer> observers_;
|
|
|
|
|
|
|
|
media_router::QueryResultManager query_result_manager_;
|
|
|
|
std::unique_ptr<CefMediaRoutesObserver> routes_observer_;
|
|
|
|
|
|
|
|
MediaRouteVector routes_;
|
|
|
|
MediaSinkVector sinks_;
|
|
|
|
|
|
|
|
using RouteStateMap =
|
|
|
|
std::map<media_router::MediaRoute::Id, std::unique_ptr<RouteState>>;
|
|
|
|
RouteStateMap route_state_map_;
|
|
|
|
|
|
|
|
base::WeakPtrFactory<CefMediaRouterManager> weak_ptr_factory_;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // CEF_LIBCEF_BROWSER_MEDIA_ROUTER_MEDIA_ROUTER_MANAGER_H_
|