| 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 #include "chrome/browser/media/router/media_router_base.h" | 5 #include "chrome/browser/media/router/media_router_base.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/guid.h" | 8 #include "base/guid.h" |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "chrome/browser/chrome_notification_types.h" | 11 #include "chrome/browser/chrome_notification_types.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 13 | 13 |
| 14 namespace media_router { | 14 namespace media_router { |
| 15 | 15 |
| 16 MediaRouterBase::MediaRouterBase() = default; | 16 // A MediaRoutesObserver that maintains state about the current set of media |
| 17 // routes. |
| 18 class MediaRouterBase::InternalMediaRoutesObserver |
| 19 : public MediaRoutesObserver { |
| 20 public: |
| 21 explicit InternalMediaRoutesObserver(MediaRouter* router) |
| 22 : MediaRoutesObserver(router), has_local_route(false) {} |
| 23 ~InternalMediaRoutesObserver() override {} |
| 17 | 24 |
| 18 MediaRouterBase::~MediaRouterBase() = default; | 25 // MediaRoutesObserver |
| 26 void OnRoutesUpdated( |
| 27 const std::vector<MediaRoute>& routes, |
| 28 const std::vector<MediaRoute::Id>& joinable_route_ids) override { |
| 29 off_the_record_route_ids.clear(); |
| 30 has_local_route = false; |
| 31 for (const auto& route : routes) { |
| 32 has_local_route |= route.is_local(); |
| 33 if (route.off_the_record()) |
| 34 off_the_record_route_ids.push_back(route.media_route_id()); |
| 35 } |
| 36 } |
| 37 |
| 38 bool has_local_route; |
| 39 std::vector<MediaRoute::Id> off_the_record_route_ids; |
| 40 |
| 41 private: |
| 42 DISALLOW_COPY_AND_ASSIGN(InternalMediaRoutesObserver); |
| 43 }; |
| 44 |
| 45 MediaRouterBase::~MediaRouterBase() { |
| 46 CHECK(!internal_routes_observer_); |
| 47 } |
| 19 | 48 |
| 20 std::unique_ptr<PresentationConnectionStateSubscription> | 49 std::unique_ptr<PresentationConnectionStateSubscription> |
| 21 MediaRouterBase::AddPresentationConnectionStateChangedCallback( | 50 MediaRouterBase::AddPresentationConnectionStateChangedCallback( |
| 22 const MediaRoute::Id& route_id, | 51 const MediaRoute::Id& route_id, |
| 23 const content::PresentationConnectionStateChangedCallback& callback) { | 52 const content::PresentationConnectionStateChangedCallback& callback) { |
| 24 DCHECK(thread_checker_.CalledOnValidThread()); | 53 DCHECK(thread_checker_.CalledOnValidThread()); |
| 25 | 54 |
| 26 auto* callbacks = presentation_connection_state_callbacks_.get(route_id); | 55 auto* callbacks = presentation_connection_state_callbacks_.get(route_id); |
| 27 if (!callbacks) { | 56 if (!callbacks) { |
| 28 callbacks = new PresentationConnectionStateChangedCallbacks; | 57 callbacks = new PresentationConnectionStateChangedCallbacks; |
| 29 callbacks->set_removal_callback(base::Bind( | 58 callbacks->set_removal_callback(base::Bind( |
| 30 &MediaRouterBase::OnPresentationConnectionStateCallbackRemoved, | 59 &MediaRouterBase::OnPresentationConnectionStateCallbackRemoved, |
| 31 base::Unretained(this), route_id)); | 60 base::Unretained(this), route_id)); |
| 32 presentation_connection_state_callbacks_.add(route_id, | 61 presentation_connection_state_callbacks_.add(route_id, |
| 33 base::WrapUnique(callbacks)); | 62 base::WrapUnique(callbacks)); |
| 34 } | 63 } |
| 35 | 64 |
| 36 return callbacks->Add(callback); | 65 return callbacks->Add(callback); |
| 37 } | 66 } |
| 38 | 67 |
| 39 void MediaRouterBase::OnOffTheRecordProfileShutdown() { | 68 void MediaRouterBase::OnOffTheRecordProfileShutdown() { |
| 40 // TODO(mfoltz): There is a race condition where off-the-record routes created | 69 for (const auto& route_id : |
| 41 // by pending CreateRoute requests won't be terminated. Fixing this would | 70 internal_routes_observer_->off_the_record_route_ids) |
| 42 // extra bookeeping of route requests in progress, and a way to cancel them | |
| 43 // in-flight. | |
| 44 for (auto route_ids_it = off_the_record_route_ids_.begin(); | |
| 45 route_ids_it != off_the_record_route_ids_.end(); | |
| 46 /* no-op */) { | |
| 47 // TerminateRoute will erase |route_id| from |off_the_record_route_ids_|, | |
| 48 // make a copy as the iterator will be invalidated. | |
| 49 const MediaRoute::Id route_id = *route_ids_it++; | |
| 50 TerminateRoute(route_id); | 71 TerminateRoute(route_id); |
| 51 } | |
| 52 } | 72 } |
| 53 | 73 |
| 74 MediaRouterBase::MediaRouterBase() : initialized_(false) {} |
| 75 |
| 54 // static | 76 // static |
| 55 std::string MediaRouterBase::CreatePresentationId() { | 77 std::string MediaRouterBase::CreatePresentationId() { |
| 56 return "mr_" + base::GenerateGUID(); | 78 return "mr_" + base::GenerateGUID(); |
| 57 } | 79 } |
| 58 | 80 |
| 59 void MediaRouterBase::NotifyPresentationConnectionStateChange( | 81 void MediaRouterBase::NotifyPresentationConnectionStateChange( |
| 60 const MediaRoute::Id& route_id, | 82 const MediaRoute::Id& route_id, |
| 61 content::PresentationConnectionState state) { | 83 content::PresentationConnectionState state) { |
| 62 if (state == content::PRESENTATION_CONNECTION_STATE_TERMINATED) | |
| 63 OnRouteTerminated(route_id); | |
| 64 | |
| 65 auto* callbacks = presentation_connection_state_callbacks_.get(route_id); | 84 auto* callbacks = presentation_connection_state_callbacks_.get(route_id); |
| 66 if (!callbacks) | 85 if (!callbacks) |
| 67 return; | 86 return; |
| 68 | 87 |
| 69 callbacks->Notify(content::PresentationConnectionStateChangeInfo(state)); | 88 callbacks->Notify(content::PresentationConnectionStateChangeInfo(state)); |
| 70 } | 89 } |
| 71 | 90 |
| 72 void MediaRouterBase::NotifyPresentationConnectionClose( | 91 void MediaRouterBase::NotifyPresentationConnectionClose( |
| 73 const MediaRoute::Id& route_id, | 92 const MediaRoute::Id& route_id, |
| 74 content::PresentationConnectionCloseReason reason, | 93 content::PresentationConnectionCloseReason reason, |
| 75 const std::string& message) { | 94 const std::string& message) { |
| 76 auto* callbacks = presentation_connection_state_callbacks_.get(route_id); | 95 auto* callbacks = presentation_connection_state_callbacks_.get(route_id); |
| 77 if (!callbacks) | 96 if (!callbacks) |
| 78 return; | 97 return; |
| 79 | 98 |
| 80 content::PresentationConnectionStateChangeInfo info( | 99 content::PresentationConnectionStateChangeInfo info( |
| 81 content::PRESENTATION_CONNECTION_STATE_CLOSED); | 100 content::PRESENTATION_CONNECTION_STATE_CLOSED); |
| 82 info.close_reason = reason; | 101 info.close_reason = reason; |
| 83 info.message = message; | 102 info.message = message; |
| 84 callbacks->Notify(info); | 103 callbacks->Notify(info); |
| 85 } | 104 } |
| 86 | 105 |
| 87 void MediaRouterBase::OnOffTheRecordRouteCreated( | 106 bool MediaRouterBase::HasLocalRoute() const { |
| 88 const MediaRoute::Id& route_id) { | 107 return internal_routes_observer_->has_local_route; |
| 89 DCHECK(!ContainsKey(off_the_record_route_ids_, route_id)); | |
| 90 off_the_record_route_ids_.insert(route_id); | |
| 91 } | 108 } |
| 92 | 109 |
| 93 void MediaRouterBase::OnRouteTerminated(const MediaRoute::Id& route_id) { | 110 void MediaRouterBase::Initialize() { |
| 94 // NOTE: This is called for all routes (off the record or not). | 111 DCHECK(!initialized_); |
| 95 off_the_record_route_ids_.erase(route_id); | 112 // The observer calls virtual methods on MediaRouter; it must be created |
| 113 // outside of the ctor |
| 114 internal_routes_observer_.reset(new InternalMediaRoutesObserver(this)); |
| 115 initialized_ = true; |
| 96 } | 116 } |
| 97 | 117 |
| 98 void MediaRouterBase::OnPresentationConnectionStateCallbackRemoved( | 118 void MediaRouterBase::OnPresentationConnectionStateCallbackRemoved( |
| 99 const MediaRoute::Id& route_id) { | 119 const MediaRoute::Id& route_id) { |
| 100 auto* callbacks = presentation_connection_state_callbacks_.get(route_id); | 120 auto* callbacks = presentation_connection_state_callbacks_.get(route_id); |
| 101 if (callbacks && callbacks->empty()) | 121 if (callbacks && callbacks->empty()) |
| 102 presentation_connection_state_callbacks_.erase(route_id); | 122 presentation_connection_state_callbacks_.erase(route_id); |
| 103 } | 123 } |
| 104 | 124 |
| 125 void MediaRouterBase::Shutdown() { |
| 126 // The observer calls virtual methods on MediaRouter; it must be destroyed |
| 127 // outside of the dtor |
| 128 internal_routes_observer_.reset(); |
| 129 } |
| 130 |
| 105 } // namespace media_router | 131 } // namespace media_router |
| OLD | NEW |