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 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 14 | 14 |
| 15 namespace media_router { | 15 namespace media_router { |
| 16 | 16 |
| 17 // A MediaRoutesObserver that maintains state about the current set of media | 17 // A MediaRoutesObserver that maintains state about the current set of media |
| 18 // routes. | 18 // routes. |
| 19 class MediaRouterBase::InternalMediaRoutesObserver | 19 class MediaRouterBase::InternalMediaRoutesObserver |
| 20 : public MediaRoutesObserver { | 20 : public MediaRoutesObserver { |
| 21 public: | 21 public: |
| 22 explicit InternalMediaRoutesObserver(MediaRouter* router) | 22 explicit InternalMediaRoutesObserver(MediaRouter* router) |
| 23 : MediaRoutesObserver(router), has_route(false) {} | 23 : MediaRoutesObserver(router), has_route(false) {} |
| 24 ~InternalMediaRoutesObserver() override {} | 24 ~InternalMediaRoutesObserver() override {} |
| 25 | 25 |
| 26 // MediaRoutesObserver | 26 // MediaRoutesObserver |
| 27 void OnRoutesUpdated( | 27 void OnRoutesUpdated( |
| 28 const std::vector<MediaRoute>& routes, | 28 const std::vector<MediaRoute>& routes, |
| 29 const std::vector<MediaRoute::Id>& joinable_route_ids) override { | 29 const std::vector<MediaRoute::Id>& joinable_route_ids) override { |
| 30 std::vector<MediaRoute> old_routes = std::move(current_routes); | |
| 30 current_routes = routes; | 31 current_routes = routes; |
| 32 | |
| 33 // Restore presentation id from old route. | |
| 34 for (auto& route : current_routes) { | |
| 35 if (route.presentation_id().has_value()) | |
| 36 continue; | |
| 37 | |
| 38 auto old_route_it = std::find_if(old_routes.begin(), old_routes.end(), | |
| 39 [&route](const MediaRoute& old_route) { | |
| 40 return route.media_route_id() == | |
| 41 old_route.media_route_id(); | |
| 42 }); | |
| 43 | |
| 44 if (old_route_it == old_routes.end()) | |
| 45 continue; | |
| 46 | |
| 47 if (old_route_it->presentation_id().has_value()) | |
| 48 route.set_presentation_id(old_route_it->presentation_id().value()); | |
| 49 } | |
| 50 | |
| 31 incognito_route_ids.clear(); | 51 incognito_route_ids.clear(); |
| 32 // TODO(crbug.com/611486): Have the MRPM pass a list of joinable route ids | 52 // TODO(crbug.com/611486): Have the MRPM pass a list of joinable route ids |
| 33 // via |joinable_route_ids|, and check here if it is non-empty. | 53 // via |joinable_route_ids|, and check here if it is non-empty. |
| 34 has_route = !routes.empty(); | 54 has_route = !routes.empty(); |
| 35 for (const auto& route : routes) { | 55 for (const auto& route : routes) { |
| 36 if (route.is_incognito()) | 56 if (route.is_incognito()) |
| 37 incognito_route_ids.push_back(route.media_route_id()); | 57 incognito_route_ids.push_back(route.media_route_id()); |
| 38 } | 58 } |
| 39 } | 59 } |
| 40 | 60 |
| 61 const MediaRoute* GetPresentationRoute( | |
| 62 const std::string& presentation_id) const { | |
| 63 for (const auto& route : current_routes) { | |
| 64 if (route.presentation_id().has_value() && | |
| 65 route.presentation_id().value() == presentation_id) | |
| 66 return &route; | |
| 67 } | |
| 68 | |
| 69 return nullptr; | |
| 70 } | |
| 71 | |
| 72 void SetPresentationId(const MediaRoute::Id& route_id, | |
|
mark a. foltz
2017/03/13 17:52:11
Can the presentation ID just be set on the Route i
| |
| 73 const std::string& presentation_id) { | |
| 74 auto route_it = std::find_if(current_routes.begin(), current_routes.end(), | |
| 75 [&route_id](const MediaRoute& route) { | |
| 76 return route.media_route_id() == route_id; | |
| 77 }); | |
| 78 | |
| 79 if (route_it == current_routes.end()) | |
| 80 return; | |
| 81 | |
| 82 route_it->set_presentation_id(presentation_id); | |
| 83 } | |
| 84 | |
| 41 bool has_route; | 85 bool has_route; |
| 42 std::vector<MediaRoute> current_routes; | 86 std::vector<MediaRoute> current_routes; |
| 43 std::vector<MediaRoute::Id> incognito_route_ids; | 87 std::vector<MediaRoute::Id> incognito_route_ids; |
| 44 | 88 |
| 45 private: | 89 private: |
| 46 DISALLOW_COPY_AND_ASSIGN(InternalMediaRoutesObserver); | 90 DISALLOW_COPY_AND_ASSIGN(InternalMediaRoutesObserver); |
| 47 }; | 91 }; |
| 48 | 92 |
| 49 MediaRouterBase::~MediaRouterBase() { | 93 MediaRouterBase::~MediaRouterBase() { |
| 50 CHECK(!internal_routes_observer_); | 94 CHECK(!internal_routes_observer_); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 69 | 113 |
| 70 void MediaRouterBase::OnIncognitoProfileShutdown() { | 114 void MediaRouterBase::OnIncognitoProfileShutdown() { |
| 71 for (const auto& route_id : internal_routes_observer_->incognito_route_ids) | 115 for (const auto& route_id : internal_routes_observer_->incognito_route_ids) |
| 72 TerminateRoute(route_id); | 116 TerminateRoute(route_id); |
| 73 } | 117 } |
| 74 | 118 |
| 75 std::vector<MediaRoute> MediaRouterBase::GetCurrentRoutes() const { | 119 std::vector<MediaRoute> MediaRouterBase::GetCurrentRoutes() const { |
| 76 return internal_routes_observer_->current_routes; | 120 return internal_routes_observer_->current_routes; |
| 77 } | 121 } |
| 78 | 122 |
| 123 const MediaRoute* MediaRouterBase::GetPresentationRoute( | |
| 124 const std::string& presentation_id) const { | |
| 125 DCHECK(internal_routes_observer_); | |
| 126 return internal_routes_observer_->GetPresentationRoute(presentation_id); | |
| 127 } | |
| 128 | |
| 129 void MediaRouterBase::SetPresentationId(const MediaRoute::Id& route_id, | |
| 130 const std::string& presentation_id) { | |
| 131 DCHECK(internal_routes_observer_); | |
| 132 return internal_routes_observer_->SetPresentationId(route_id, | |
| 133 presentation_id); | |
| 134 } | |
| 135 | |
| 79 MediaRouterBase::MediaRouterBase() : initialized_(false) {} | 136 MediaRouterBase::MediaRouterBase() : initialized_(false) {} |
| 80 | 137 |
| 81 // static | 138 // static |
| 82 std::string MediaRouterBase::CreatePresentationId() { | 139 std::string MediaRouterBase::CreatePresentationId() { |
| 83 return "mr_" + base::GenerateGUID(); | 140 return "mr_" + base::GenerateGUID(); |
| 84 } | 141 } |
| 85 | 142 |
| 86 void MediaRouterBase::NotifyPresentationConnectionStateChange( | 143 void MediaRouterBase::NotifyPresentationConnectionStateChange( |
| 87 const MediaRoute::Id& route_id, | 144 const MediaRoute::Id& route_id, |
| 88 content::PresentationConnectionState state) { | 145 content::PresentationConnectionState state) { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 132 } | 189 } |
| 133 } | 190 } |
| 134 | 191 |
| 135 void MediaRouterBase::Shutdown() { | 192 void MediaRouterBase::Shutdown() { |
| 136 // The observer calls virtual methods on MediaRouter; it must be destroyed | 193 // The observer calls virtual methods on MediaRouter; it must be destroyed |
| 137 // outside of the dtor | 194 // outside of the dtor |
| 138 internal_routes_observer_.reset(); | 195 internal_routes_observer_.reset(); |
| 139 } | 196 } |
| 140 | 197 |
| 141 } // namespace media_router | 198 } // namespace media_router |
| OLD | NEW |