Chromium Code Reviews| 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 MediaRouterBase::~MediaRouterBase() {} |
|
imcheng
2016/04/25 18:59:54
suggestion: Add a CHECK(!internal_routes_observer_
mark a. foltz
2016/04/26 21:11:10
Done.
| |
| 17 | |
| 18 MediaRouterBase::~MediaRouterBase() = default; | |
| 19 | 17 |
| 20 std::unique_ptr<PresentationConnectionStateSubscription> | 18 std::unique_ptr<PresentationConnectionStateSubscription> |
| 21 MediaRouterBase::AddPresentationConnectionStateChangedCallback( | 19 MediaRouterBase::AddPresentationConnectionStateChangedCallback( |
| 22 const MediaRoute::Id& route_id, | 20 const MediaRoute::Id& route_id, |
| 23 const content::PresentationConnectionStateChangedCallback& callback) { | 21 const content::PresentationConnectionStateChangedCallback& callback) { |
| 24 DCHECK(thread_checker_.CalledOnValidThread()); | 22 DCHECK(thread_checker_.CalledOnValidThread()); |
| 25 | 23 |
| 26 auto* callbacks = presentation_connection_state_callbacks_.get(route_id); | 24 auto* callbacks = presentation_connection_state_callbacks_.get(route_id); |
| 27 if (!callbacks) { | 25 if (!callbacks) { |
| 28 callbacks = new PresentationConnectionStateChangedCallbacks; | 26 callbacks = new PresentationConnectionStateChangedCallbacks; |
| 29 callbacks->set_removal_callback(base::Bind( | 27 callbacks->set_removal_callback(base::Bind( |
| 30 &MediaRouterBase::OnPresentationConnectionStateCallbackRemoved, | 28 &MediaRouterBase::OnPresentationConnectionStateCallbackRemoved, |
| 31 base::Unretained(this), route_id)); | 29 base::Unretained(this), route_id)); |
| 32 presentation_connection_state_callbacks_.add(route_id, | 30 presentation_connection_state_callbacks_.add(route_id, |
| 33 base::WrapUnique(callbacks)); | 31 base::WrapUnique(callbacks)); |
| 34 } | 32 } |
| 35 | 33 |
| 36 return callbacks->Add(callback); | 34 return callbacks->Add(callback); |
| 37 } | 35 } |
| 38 | 36 |
| 39 void MediaRouterBase::OnOffTheRecordProfileShutdown() { | 37 void MediaRouterBase::OnOffTheRecordProfileShutdown() { |
| 40 // TODO(mfoltz): There is a race condition where off-the-record routes created | 38 for (const auto& route_id : |
| 41 // by pending CreateRoute requests won't be terminated. Fixing this would | 39 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); | 40 TerminateRoute(route_id); |
| 51 } | |
| 52 } | 41 } |
| 53 | 42 |
| 43 MediaRouterBase::MediaRouterBase() : initialized_(false) {} | |
| 44 | |
| 54 // static | 45 // static |
| 55 std::string MediaRouterBase::CreatePresentationId() { | 46 std::string MediaRouterBase::CreatePresentationId() { |
| 56 return "mr_" + base::GenerateGUID(); | 47 return "mr_" + base::GenerateGUID(); |
| 57 } | 48 } |
| 58 | 49 |
| 59 void MediaRouterBase::NotifyPresentationConnectionStateChange( | 50 void MediaRouterBase::NotifyPresentationConnectionStateChange( |
| 60 const MediaRoute::Id& route_id, | 51 const MediaRoute::Id& route_id, |
| 61 content::PresentationConnectionState state) { | 52 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); | 53 auto* callbacks = presentation_connection_state_callbacks_.get(route_id); |
| 66 if (!callbacks) | 54 if (!callbacks) |
| 67 return; | 55 return; |
| 68 | 56 |
| 69 callbacks->Notify(content::PresentationConnectionStateChangeInfo(state)); | 57 callbacks->Notify(content::PresentationConnectionStateChangeInfo(state)); |
| 70 } | 58 } |
| 71 | 59 |
| 72 void MediaRouterBase::NotifyPresentationConnectionClose( | 60 void MediaRouterBase::NotifyPresentationConnectionClose( |
| 73 const MediaRoute::Id& route_id, | 61 const MediaRoute::Id& route_id, |
| 74 content::PresentationConnectionCloseReason reason, | 62 content::PresentationConnectionCloseReason reason, |
| 75 const std::string& message) { | 63 const std::string& message) { |
| 76 auto* callbacks = presentation_connection_state_callbacks_.get(route_id); | 64 auto* callbacks = presentation_connection_state_callbacks_.get(route_id); |
| 77 if (!callbacks) | 65 if (!callbacks) |
| 78 return; | 66 return; |
| 79 | 67 |
| 80 content::PresentationConnectionStateChangeInfo info( | 68 content::PresentationConnectionStateChangeInfo info( |
| 81 content::PRESENTATION_CONNECTION_STATE_CLOSED); | 69 content::PRESENTATION_CONNECTION_STATE_CLOSED); |
| 82 info.close_reason = reason; | 70 info.close_reason = reason; |
| 83 info.message = message; | 71 info.message = message; |
| 84 callbacks->Notify(info); | 72 callbacks->Notify(info); |
| 85 } | 73 } |
| 86 | 74 |
| 87 void MediaRouterBase::OnOffTheRecordRouteCreated( | 75 bool MediaRouterBase::HasLocalRoute() const { |
| 88 const MediaRoute::Id& route_id) { | 76 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 } | 77 } |
| 92 | 78 |
| 93 void MediaRouterBase::OnRouteTerminated(const MediaRoute::Id& route_id) { | 79 void MediaRouterBase::Initialize() { |
| 94 // NOTE: This is called for all routes (off the record or not). | 80 DCHECK(!initialized_); |
| 95 off_the_record_route_ids_.erase(route_id); | 81 // The observer calls virtual methods on MediaRouter; it must be created |
| 82 // outside of the ctor | |
| 83 internal_routes_observer_.reset(new InternalMediaRoutesObserver(this)); | |
| 84 initialized_ = true; | |
| 85 } | |
| 86 | |
| 87 MediaRouterBase::InternalMediaRoutesObserver::InternalMediaRoutesObserver( | |
| 88 MediaRouter* router) | |
| 89 : MediaRoutesObserver(router), has_local_route(false) {} | |
| 90 | |
| 91 MediaRouterBase::InternalMediaRoutesObserver::~InternalMediaRoutesObserver() {} | |
| 92 | |
| 93 void MediaRouterBase::InternalMediaRoutesObserver::OnRoutesUpdated( | |
| 94 const std::vector<MediaRoute>& routes, | |
| 95 const std::vector<MediaRoute::Id>& joinable_route_ids) { | |
| 96 off_the_record_route_ids.clear(); | |
| 97 has_local_route = false; | |
| 98 for (const auto& route : routes) { | |
| 99 has_local_route |= route.is_local(); | |
| 100 if (route.off_the_record()) | |
| 101 off_the_record_route_ids.push_back(route.media_route_id()); | |
| 102 } | |
| 96 } | 103 } |
| 97 | 104 |
| 98 void MediaRouterBase::OnPresentationConnectionStateCallbackRemoved( | 105 void MediaRouterBase::OnPresentationConnectionStateCallbackRemoved( |
| 99 const MediaRoute::Id& route_id) { | 106 const MediaRoute::Id& route_id) { |
| 100 auto* callbacks = presentation_connection_state_callbacks_.get(route_id); | 107 auto* callbacks = presentation_connection_state_callbacks_.get(route_id); |
| 101 if (callbacks && callbacks->empty()) | 108 if (callbacks && callbacks->empty()) |
| 102 presentation_connection_state_callbacks_.erase(route_id); | 109 presentation_connection_state_callbacks_.erase(route_id); |
| 103 } | 110 } |
| 104 | 111 |
| 112 void MediaRouterBase::Shutdown() { | |
| 113 // The observer calls virtual methods on MediaRouter; it must be destroyed | |
| 114 // outside of the dtor | |
| 115 internal_routes_observer_.reset(); | |
| 116 } | |
| 117 | |
| 105 } // namespace media_router | 118 } // namespace media_router |
| OLD | NEW |