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" |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 57 auto* callbacks = presentation_connection_state_callbacks_.get(route_id); | 57 auto* callbacks = presentation_connection_state_callbacks_.get(route_id); |
| 58 if (!callbacks) { | 58 if (!callbacks) { |
| 59 callbacks = new PresentationConnectionStateChangedCallbacks; | 59 callbacks = new PresentationConnectionStateChangedCallbacks; |
| 60 callbacks->set_removal_callback(base::Bind( | 60 callbacks->set_removal_callback(base::Bind( |
| 61 &MediaRouterBase::OnPresentationConnectionStateCallbackRemoved, | 61 &MediaRouterBase::OnPresentationConnectionStateCallbackRemoved, |
| 62 base::Unretained(this), route_id)); | 62 base::Unretained(this), route_id)); |
| 63 presentation_connection_state_callbacks_.add(route_id, | 63 presentation_connection_state_callbacks_.add(route_id, |
| 64 base::WrapUnique(callbacks)); | 64 base::WrapUnique(callbacks)); |
| 65 } | 65 } |
| 66 | 66 |
| 67 return callbacks->Add(callback); | 67 auto subscription = callbacks->Add(callback); |
| 68 auto queued_state_change = queued_state_changes_.find(route_id); | |
| 69 if (queued_state_change != queued_state_changes_.end()) { | |
| 70 callbacks->Notify(queued_state_change->second); | |
| 71 queued_state_changes_.erase(queued_state_change); | |
| 72 } | |
| 73 return subscription; | |
| 68 } | 74 } |
| 69 | 75 |
| 70 void MediaRouterBase::OnIncognitoProfileShutdown() { | 76 void MediaRouterBase::OnIncognitoProfileShutdown() { |
| 71 for (const auto& route_id : internal_routes_observer_->incognito_route_ids) | 77 for (const auto& route_id : internal_routes_observer_->incognito_route_ids) |
| 72 TerminateRoute(route_id); | 78 TerminateRoute(route_id); |
| 73 } | 79 } |
| 74 | 80 |
| 75 MediaRouterBase::MediaRouterBase() : initialized_(false) {} | 81 MediaRouterBase::MediaRouterBase() : initialized_(false) {} |
| 76 | 82 |
| 77 // static | 83 // static |
| 78 std::string MediaRouterBase::CreatePresentationId() { | 84 std::string MediaRouterBase::CreatePresentationId() { |
| 79 return "mr_" + base::GenerateGUID(); | 85 return "mr_" + base::GenerateGUID(); |
| 80 } | 86 } |
| 81 | 87 |
| 82 void MediaRouterBase::NotifyPresentationConnectionStateChange( | 88 void MediaRouterBase::NotifyPresentationConnectionStateChange( |
| 83 const MediaRoute::Id& route_id, | 89 const MediaRoute::Id& route_id, |
| 84 content::PresentationConnectionState state) { | 90 content::PresentationConnectionState state) { |
| 85 // We should call NotifyPresentationConnectionClose() for the CLOSED state. | 91 // We should call NotifyPresentationConnectionClose() for the CLOSED state. |
| 86 DCHECK_NE(state, content::PRESENTATION_CONNECTION_STATE_CLOSED); | 92 DCHECK_NE(state, content::PRESENTATION_CONNECTION_STATE_CLOSED); |
| 87 | 93 |
| 88 auto* callbacks = presentation_connection_state_callbacks_.get(route_id); | 94 content::PresentationConnectionStateChangeInfo info(state); |
| 89 if (!callbacks) | 95 NotifyPresentationConnectionStateChange(route_id, info); |
| 90 return; | |
| 91 | |
| 92 callbacks->Notify(content::PresentationConnectionStateChangeInfo(state)); | |
| 93 } | 96 } |
| 94 | 97 |
| 95 void MediaRouterBase::NotifyPresentationConnectionClose( | 98 void MediaRouterBase::NotifyPresentationConnectionClose( |
| 96 const MediaRoute::Id& route_id, | 99 const MediaRoute::Id& route_id, |
| 97 content::PresentationConnectionCloseReason reason, | 100 content::PresentationConnectionCloseReason reason, |
| 98 const std::string& message) { | 101 const std::string& message) { |
| 99 auto* callbacks = presentation_connection_state_callbacks_.get(route_id); | |
| 100 if (!callbacks) | |
| 101 return; | |
| 102 | |
| 103 content::PresentationConnectionStateChangeInfo info( | 102 content::PresentationConnectionStateChangeInfo info( |
| 104 content::PRESENTATION_CONNECTION_STATE_CLOSED); | 103 content::PRESENTATION_CONNECTION_STATE_CLOSED); |
| 105 info.close_reason = reason; | 104 info.close_reason = reason; |
| 106 info.message = message; | 105 info.message = message; |
| 107 callbacks->Notify(info); | 106 NotifyPresentationConnectionStateChange(route_id, info); |
| 107 } | |
| 108 | |
| 109 void MediaRouterBase::NotifyPresentationConnectionStateChange( | |
| 110 const MediaRoute::Id& route_id, | |
| 111 const content::PresentationConnectionStateChangeInfo& info) { | |
| 112 auto* callbacks = presentation_connection_state_callbacks_.get(route_id); | |
| 113 | |
| 114 if (callbacks) { | |
| 115 callbacks->Notify(info); | |
| 116 return; | |
| 117 } | |
| 118 | |
| 119 auto queued_state_change = queued_state_changes_.find(route_id); | |
| 120 if (queued_state_change == queued_state_changes_.end()) | |
| 121 queued_state_changes_.insert(std::make_pair(route_id, info)); | |
|
imcheng
2016/11/01 20:09:19
If we never added a state change callback, would t
| |
| 122 else | |
| 123 queued_state_change->second = info; | |
| 108 } | 124 } |
| 109 | 125 |
| 110 bool MediaRouterBase::HasJoinableRoute() const { | 126 bool MediaRouterBase::HasJoinableRoute() const { |
| 111 return internal_routes_observer_->has_route; | 127 return internal_routes_observer_->has_route; |
| 112 } | 128 } |
| 113 | 129 |
| 114 void MediaRouterBase::Initialize() { | 130 void MediaRouterBase::Initialize() { |
| 115 DCHECK(!initialized_); | 131 DCHECK(!initialized_); |
| 116 // The observer calls virtual methods on MediaRouter; it must be created | 132 // The observer calls virtual methods on MediaRouter; it must be created |
| 117 // outside of the ctor | 133 // outside of the ctor |
| 118 internal_routes_observer_.reset(new InternalMediaRoutesObserver(this)); | 134 internal_routes_observer_.reset(new InternalMediaRoutesObserver(this)); |
| 119 initialized_ = true; | 135 initialized_ = true; |
| 120 } | 136 } |
| 121 | 137 |
| 122 void MediaRouterBase::OnPresentationConnectionStateCallbackRemoved( | 138 void MediaRouterBase::OnPresentationConnectionStateCallbackRemoved( |
| 123 const MediaRoute::Id& route_id) { | 139 const MediaRoute::Id& route_id) { |
| 124 auto* callbacks = presentation_connection_state_callbacks_.get(route_id); | 140 auto* callbacks = presentation_connection_state_callbacks_.get(route_id); |
| 125 if (callbacks && callbacks->empty()) | 141 if (callbacks && callbacks->empty()) |
| 126 presentation_connection_state_callbacks_.erase(route_id); | 142 presentation_connection_state_callbacks_.erase(route_id); |
| 127 } | 143 } |
| 128 | 144 |
| 129 void MediaRouterBase::Shutdown() { | 145 void MediaRouterBase::Shutdown() { |
| 130 // The observer calls virtual methods on MediaRouter; it must be destroyed | 146 // The observer calls virtual methods on MediaRouter; it must be destroyed |
| 131 // outside of the dtor | 147 // outside of the dtor |
| 132 internal_routes_observer_.reset(); | 148 internal_routes_observer_.reset(); |
| 133 } | 149 } |
| 134 | 150 |
| 135 } // namespace media_router | 151 } // namespace media_router |
| OLD | NEW |