media_router_base.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. #include "components/media_router/browser/media_router_base.h"
  5. #include <string>
  6. #include "base/bind.h"
  7. #include "base/check_op.h"
  8. #include "base/guid.h"
  9. #include "content/public/browser/browser_thread.h"
  10. using blink::mojom::PresentationConnectionState;
  11. namespace media_router {
  12. MediaRouterBase::~MediaRouterBase() = default;
  13. base::CallbackListSubscription
  14. MediaRouterBase::AddPresentationConnectionStateChangedCallback(
  15. const MediaRoute::Id& route_id,
  16. const content::PresentationConnectionStateChangedCallback& callback) {
  17. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  18. auto& callbacks = presentation_connection_state_callbacks_[route_id];
  19. if (!callbacks) {
  20. callbacks = std::make_unique<PresentationConnectionStateChangedCallbacks>();
  21. callbacks->set_removal_callback(base::BindRepeating(
  22. &MediaRouterBase::OnPresentationConnectionStateCallbackRemoved,
  23. base::Unretained(this), route_id));
  24. }
  25. return callbacks->Add(callback);
  26. }
  27. MediaRouterBase::MediaRouterBase() = default;
  28. // static
  29. std::string MediaRouterBase::CreatePresentationId() {
  30. return "mr_" + base::GenerateGUID();
  31. }
  32. void MediaRouterBase::NotifyPresentationConnectionStateChange(
  33. const MediaRoute::Id& route_id,
  34. PresentationConnectionState state) {
  35. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  36. // We should call NotifyPresentationConnectionClose() for the CLOSED state.
  37. DCHECK_NE(state, PresentationConnectionState::CLOSED);
  38. auto it = presentation_connection_state_callbacks_.find(route_id);
  39. if (it == presentation_connection_state_callbacks_.end())
  40. return;
  41. it->second->Notify(content::PresentationConnectionStateChangeInfo(state));
  42. }
  43. void MediaRouterBase::NotifyPresentationConnectionClose(
  44. const MediaRoute::Id& route_id,
  45. blink::mojom::PresentationConnectionCloseReason reason,
  46. const std::string& message) {
  47. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  48. auto it = presentation_connection_state_callbacks_.find(route_id);
  49. if (it == presentation_connection_state_callbacks_.end())
  50. return;
  51. content::PresentationConnectionStateChangeInfo info(
  52. PresentationConnectionState::CLOSED);
  53. info.close_reason = reason;
  54. info.message = message;
  55. it->second->Notify(info);
  56. }
  57. void MediaRouterBase::OnPresentationConnectionStateCallbackRemoved(
  58. const MediaRoute::Id& route_id) {
  59. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  60. auto it = presentation_connection_state_callbacks_.find(route_id);
  61. if (it != presentation_connection_state_callbacks_.end() &&
  62. it->second->empty()) {
  63. presentation_connection_state_callbacks_.erase(route_id);
  64. }
  65. }
  66. } // namespace media_router