| 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/android/router/media_router_android.h" | 5 #include "chrome/browser/media/android/router/media_router_android.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 for (const auto& route : active_routes_) { | 68 for (const auto& route : active_routes_) { |
| 69 if (route.media_source().id() == source_id) | 69 if (route.media_source().id() == source_id) |
| 70 return &route; | 70 return &route; |
| 71 } | 71 } |
| 72 return nullptr; | 72 return nullptr; |
| 73 } | 73 } |
| 74 | 74 |
| 75 void MediaRouterAndroid::CreateRoute( | 75 void MediaRouterAndroid::CreateRoute( |
| 76 const MediaSource::Id& source_id, | 76 const MediaSource::Id& source_id, |
| 77 const MediaSink::Id& sink_id, | 77 const MediaSink::Id& sink_id, |
| 78 const url::Origin& origin, | 78 const GURL& origin, |
| 79 content::WebContents* web_contents, | 79 content::WebContents* web_contents, |
| 80 const std::vector<MediaRouteResponseCallback>& callbacks, | 80 const std::vector<MediaRouteResponseCallback>& callbacks, |
| 81 base::TimeDelta timeout, | 81 base::TimeDelta timeout, |
| 82 bool incognito) { | 82 bool incognito) { |
| 83 // TODO(avayvod): Implement timeouts (crbug.com/583036). | 83 // TODO(avayvod): Implement timeouts (crbug.com/583036). |
| 84 if (!origin.is_valid()) { |
| 85 std::unique_ptr<RouteRequestResult> result = RouteRequestResult::FromError( |
| 86 "Invalid origin", RouteRequestResult::INVALID_ORIGIN); |
| 87 for (const MediaRouteResponseCallback& callback : callbacks) |
| 88 callback.Run(*result); |
| 89 return; |
| 90 } |
| 91 |
| 84 std::string presentation_id = MediaRouterBase::CreatePresentationId(); | 92 std::string presentation_id = MediaRouterBase::CreatePresentationId(); |
| 85 | 93 |
| 86 int tab_id = -1; | 94 int tab_id = -1; |
| 87 TabAndroid* tab = web_contents | 95 TabAndroid* tab = web_contents |
| 88 ? TabAndroid::FromWebContents(web_contents) : nullptr; | 96 ? TabAndroid::FromWebContents(web_contents) : nullptr; |
| 89 if (tab) | 97 if (tab) |
| 90 tab_id = tab->GetAndroidId(); | 98 tab_id = tab->GetAndroidId(); |
| 91 | 99 |
| 92 bool is_incognito = web_contents | 100 bool is_incognito = web_contents |
| 93 && web_contents->GetBrowserContext()->IsOffTheRecord(); | 101 && web_contents->GetBrowserContext()->IsOffTheRecord(); |
| 94 | 102 |
| 95 int route_request_id = | 103 int route_request_id = |
| 96 route_requests_.Add(base::MakeUnique<MediaRouteRequest>( | 104 route_requests_.Add(base::MakeUnique<MediaRouteRequest>( |
| 97 MediaSource(source_id), presentation_id, callbacks)); | 105 MediaSource(source_id), presentation_id, callbacks)); |
| 98 | 106 |
| 99 JNIEnv* env = base::android::AttachCurrentThread(); | 107 JNIEnv* env = base::android::AttachCurrentThread(); |
| 100 ScopedJavaLocalRef<jstring> jsource_id = | 108 ScopedJavaLocalRef<jstring> jsource_id = |
| 101 base::android::ConvertUTF8ToJavaString(env, source_id); | 109 base::android::ConvertUTF8ToJavaString(env, source_id); |
| 102 ScopedJavaLocalRef<jstring> jsink_id = | 110 ScopedJavaLocalRef<jstring> jsink_id = |
| 103 base::android::ConvertUTF8ToJavaString(env, sink_id); | 111 base::android::ConvertUTF8ToJavaString(env, sink_id); |
| 104 ScopedJavaLocalRef<jstring> jpresentation_id = | 112 ScopedJavaLocalRef<jstring> jpresentation_id = |
| 105 base::android::ConvertUTF8ToJavaString(env, presentation_id); | 113 base::android::ConvertUTF8ToJavaString(env, presentation_id); |
| 106 // TODO(crbug.com/685358): Unique origins should not be considered | |
| 107 // same-origin. | |
| 108 ScopedJavaLocalRef<jstring> jorigin = | 114 ScopedJavaLocalRef<jstring> jorigin = |
| 109 base::android::ConvertUTF8ToJavaString(env, origin.GetURL().spec()); | 115 base::android::ConvertUTF8ToJavaString(env, origin.spec()); |
| 110 | 116 |
| 111 Java_ChromeMediaRouter_createRoute(env, java_media_router_, jsource_id, | 117 Java_ChromeMediaRouter_createRoute(env, java_media_router_, jsource_id, |
| 112 jsink_id, jpresentation_id, jorigin, | 118 jsink_id, jpresentation_id, jorigin, |
| 113 tab_id, is_incognito, route_request_id); | 119 tab_id, is_incognito, route_request_id); |
| 114 } | 120 } |
| 115 | 121 |
| 116 void MediaRouterAndroid::ConnectRouteByRouteId( | 122 void MediaRouterAndroid::ConnectRouteByRouteId( |
| 117 const MediaSource::Id& source, | 123 const MediaSource::Id& source, |
| 118 const MediaRoute::Id& route_id, | 124 const MediaRoute::Id& route_id, |
| 119 const url::Origin& origin, | 125 const GURL& origin, |
| 120 content::WebContents* web_contents, | 126 content::WebContents* web_contents, |
| 121 const std::vector<MediaRouteResponseCallback>& callbacks, | 127 const std::vector<MediaRouteResponseCallback>& callbacks, |
| 122 base::TimeDelta timeout, | 128 base::TimeDelta timeout, |
| 123 bool incognito) { | 129 bool incognito) { |
| 124 NOTIMPLEMENTED(); | 130 NOTIMPLEMENTED(); |
| 125 } | 131 } |
| 126 | 132 |
| 127 void MediaRouterAndroid::JoinRoute( | 133 void MediaRouterAndroid::JoinRoute( |
| 128 const MediaSource::Id& source_id, | 134 const MediaSource::Id& source_id, |
| 129 const std::string& presentation_id, | 135 const std::string& presentation_id, |
| 130 const url::Origin& origin, | 136 const GURL& origin, |
| 131 content::WebContents* web_contents, | 137 content::WebContents* web_contents, |
| 132 const std::vector<MediaRouteResponseCallback>& callbacks, | 138 const std::vector<MediaRouteResponseCallback>& callbacks, |
| 133 base::TimeDelta timeout, | 139 base::TimeDelta timeout, |
| 134 bool incognito) { | 140 bool incognito) { |
| 135 // TODO(avayvod): Implement timeouts (crbug.com/583036). | 141 // TODO(avayvod): Implement timeouts (crbug.com/583036). |
| 142 if (!origin.is_valid()) { |
| 143 std::unique_ptr<RouteRequestResult> result = RouteRequestResult::FromError( |
| 144 "Invalid origin", RouteRequestResult::INVALID_ORIGIN); |
| 145 for (const MediaRouteResponseCallback& callback : callbacks) |
| 146 callback.Run(*result); |
| 147 return; |
| 148 } |
| 149 |
| 136 int tab_id = -1; | 150 int tab_id = -1; |
| 137 TabAndroid* tab = web_contents | 151 TabAndroid* tab = web_contents |
| 138 ? TabAndroid::FromWebContents(web_contents) : nullptr; | 152 ? TabAndroid::FromWebContents(web_contents) : nullptr; |
| 139 if (tab) | 153 if (tab) |
| 140 tab_id = tab->GetAndroidId(); | 154 tab_id = tab->GetAndroidId(); |
| 141 | 155 |
| 142 DVLOG(2) << "JoinRoute: " << source_id << ", " << presentation_id << ", " | 156 DVLOG(2) << "JoinRoute: " << source_id << ", " << presentation_id << ", " |
| 143 << origin.GetURL().spec() << ", " << tab_id; | 157 << origin.spec() << ", " << tab_id; |
| 144 | 158 |
| 145 int request_id = route_requests_.Add(base::MakeUnique<MediaRouteRequest>( | 159 int request_id = route_requests_.Add(base::MakeUnique<MediaRouteRequest>( |
| 146 MediaSource(source_id), presentation_id, callbacks)); | 160 MediaSource(source_id), presentation_id, callbacks)); |
| 147 | 161 |
| 148 JNIEnv* env = base::android::AttachCurrentThread(); | 162 JNIEnv* env = base::android::AttachCurrentThread(); |
| 149 ScopedJavaLocalRef<jstring> jsource_id = | 163 ScopedJavaLocalRef<jstring> jsource_id = |
| 150 base::android::ConvertUTF8ToJavaString(env, source_id); | 164 base::android::ConvertUTF8ToJavaString(env, source_id); |
| 151 ScopedJavaLocalRef<jstring> jpresentation_id = | 165 ScopedJavaLocalRef<jstring> jpresentation_id = |
| 152 base::android::ConvertUTF8ToJavaString(env, presentation_id); | 166 base::android::ConvertUTF8ToJavaString(env, presentation_id); |
| 153 ScopedJavaLocalRef<jstring> jorigin = | 167 ScopedJavaLocalRef<jstring> jorigin = |
| 154 base::android::ConvertUTF8ToJavaString(env, origin.GetURL().spec()); | 168 base::android::ConvertUTF8ToJavaString(env, origin.spec()); |
| 155 | 169 |
| 156 Java_ChromeMediaRouter_joinRoute(env, java_media_router_, jsource_id, | 170 Java_ChromeMediaRouter_joinRoute(env, java_media_router_, jsource_id, |
| 157 jpresentation_id, jorigin, tab_id, | 171 jpresentation_id, jorigin, tab_id, |
| 158 request_id); | 172 request_id); |
| 159 } | 173 } |
| 160 | 174 |
| 161 void MediaRouterAndroid::TerminateRoute(const MediaRoute::Id& route_id) { | 175 void MediaRouterAndroid::TerminateRoute(const MediaRoute::Id& route_id) { |
| 162 JNIEnv* env = base::android::AttachCurrentThread(); | 176 JNIEnv* env = base::android::AttachCurrentThread(); |
| 163 ScopedJavaLocalRef<jstring> jroute_id = | 177 ScopedJavaLocalRef<jstring> jroute_id = |
| 164 base::android::ConvertUTF8ToJavaString(env, route_id); | 178 base::android::ConvertUTF8ToJavaString(env, route_id); |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 MediaSink(ConvertJavaStringToUTF8(env, jsink_urn.obj()), | 341 MediaSink(ConvertJavaStringToUTF8(env, jsink_urn.obj()), |
| 328 ConvertJavaStringToUTF8(env, jsink_name.obj()), | 342 ConvertJavaStringToUTF8(env, jsink_name.obj()), |
| 329 MediaSink::GENERIC)); | 343 MediaSink::GENERIC)); |
| 330 } | 344 } |
| 331 | 345 |
| 332 std::string source_urn = ConvertJavaStringToUTF8(env, jsource_urn); | 346 std::string source_urn = ConvertJavaStringToUTF8(env, jsource_urn); |
| 333 auto it = sinks_observers_.find(source_urn); | 347 auto it = sinks_observers_.find(source_urn); |
| 334 if (it != sinks_observers_.end()) { | 348 if (it != sinks_observers_.end()) { |
| 335 // TODO(imcheng): Pass origins to OnSinksUpdated (crbug.com/594858). | 349 // TODO(imcheng): Pass origins to OnSinksUpdated (crbug.com/594858). |
| 336 for (auto& observer : *it->second) | 350 for (auto& observer : *it->second) |
| 337 observer.OnSinksUpdated(sinks_converted, std::vector<url::Origin>()); | 351 observer.OnSinksUpdated(sinks_converted, std::vector<GURL>()); |
| 338 } | 352 } |
| 339 } | 353 } |
| 340 | 354 |
| 341 void MediaRouterAndroid::OnRouteCreated( | 355 void MediaRouterAndroid::OnRouteCreated( |
| 342 JNIEnv* env, | 356 JNIEnv* env, |
| 343 const JavaParamRef<jobject>& obj, | 357 const JavaParamRef<jobject>& obj, |
| 344 const JavaParamRef<jstring>& jmedia_route_id, | 358 const JavaParamRef<jstring>& jmedia_route_id, |
| 345 const JavaParamRef<jstring>& jsink_id, | 359 const JavaParamRef<jstring>& jsink_id, |
| 346 jint jroute_request_id, | 360 jint jroute_request_id, |
| 347 jboolean jis_local) { | 361 jboolean jis_local) { |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 438 return; | 452 return; |
| 439 | 453 |
| 440 std::vector<RouteMessage> messages(1); | 454 std::vector<RouteMessage> messages(1); |
| 441 messages.front().type = RouteMessage::TEXT; | 455 messages.front().type = RouteMessage::TEXT; |
| 442 messages.front().text = ConvertJavaStringToUTF8(env, jmessage); | 456 messages.front().text = ConvertJavaStringToUTF8(env, jmessage); |
| 443 for (auto& observer : *it->second.get()) | 457 for (auto& observer : *it->second.get()) |
| 444 observer.OnMessagesReceived(messages); | 458 observer.OnMessagesReceived(messages); |
| 445 } | 459 } |
| 446 | 460 |
| 447 } // namespace media_router | 461 } // namespace media_router |
| OLD | NEW |