| 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 "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_string.h" | 8 #include "base/android/jni_string.h" |
| 9 #include "base/guid.h" |
| 9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 10 #include "chrome/browser/media/router/media_sinks_observer.h" | 11 #include "chrome/browser/media/router/media_sinks_observer.h" |
| 11 #include "jni/ChromeMediaRouter_jni.h" | 12 #include "jni/ChromeMediaRouter_jni.h" |
| 12 | 13 |
| 13 using base::android::ConvertUTF8ToJavaString; | 14 using base::android::ConvertUTF8ToJavaString; |
| 14 using base::android::ConvertJavaStringToUTF8; | 15 using base::android::ConvertJavaStringToUTF8; |
| 15 using base::android::ScopedJavaLocalRef; | 16 using base::android::ScopedJavaLocalRef; |
| 16 using base::android::AttachCurrentThread; | 17 using base::android::AttachCurrentThread; |
| 17 | 18 |
| 18 namespace media_router { | 19 namespace media_router { |
| 19 | 20 |
| 21 MediaRouterAndroid::CreateMediaRouteRequest::CreateMediaRouteRequest( |
| 22 const MediaSource& source, |
| 23 const MediaSink& sink, |
| 24 const std::string& presentation_id, |
| 25 const std::vector<MediaRouteResponseCallback>& callbacks) |
| 26 : media_source(source), |
| 27 media_sink(sink), |
| 28 presentation_id(presentation_id), |
| 29 callbacks(callbacks) {} |
| 30 MediaRouterAndroid::CreateMediaRouteRequest::~CreateMediaRouteRequest() {} |
| 31 |
| 20 MediaRouterAndroid::MediaRouterAndroid(content::BrowserContext*) { | 32 MediaRouterAndroid::MediaRouterAndroid(content::BrowserContext*) { |
| 21 JNIEnv* env = base::android::AttachCurrentThread(); | 33 JNIEnv* env = base::android::AttachCurrentThread(); |
| 22 java_media_router_.Reset(Java_ChromeMediaRouter_create( | 34 java_media_router_.Reset(Java_ChromeMediaRouter_create( |
| 23 env, | 35 env, |
| 24 reinterpret_cast<jlong>(this), | 36 reinterpret_cast<jlong>(this), |
| 25 base::android::GetApplicationContext())); | 37 base::android::GetApplicationContext())); |
| 26 } | 38 } |
| 27 | 39 |
| 28 MediaRouterAndroid::~MediaRouterAndroid() { | 40 MediaRouterAndroid::~MediaRouterAndroid() { |
| 29 } | 41 } |
| 30 | 42 |
| 31 // static | 43 // static |
| 32 bool MediaRouterAndroid::Register(JNIEnv* env) { | 44 bool MediaRouterAndroid::Register(JNIEnv* env) { |
| 33 bool ret = RegisterNativesImpl(env); | 45 bool ret = RegisterNativesImpl(env); |
| 34 DCHECK(g_ChromeMediaRouter_clazz); | 46 DCHECK(g_ChromeMediaRouter_clazz); |
| 35 return ret; | 47 return ret; |
| 36 } | 48 } |
| 37 | 49 |
| 38 void MediaRouterAndroid::CreateRoute( | 50 void MediaRouterAndroid::CreateRoute( |
| 39 const MediaSource::Id& source_id, | 51 const MediaSource::Id& source_id, |
| 40 const MediaSink::Id& sink_id, | 52 const MediaSink::Id& sink_id, |
| 41 const GURL& origin, | 53 const GURL& origin, |
| 42 int tab_id, | 54 int tab_id, |
| 43 const std::vector<MediaRouteResponseCallback>& callbacks) { | 55 const std::vector<MediaRouteResponseCallback>& callbacks) { |
| 44 NOTIMPLEMENTED(); | 56 if (!origin.is_valid()) { |
| 57 for (const MediaRouteResponseCallback& callback : callbacks) |
| 58 callback.Run(nullptr, "", "Invalid origin"); |
| 59 return; |
| 60 } |
| 61 |
| 62 // TODO(avayvod): unify presentation id generation code between platforms. |
| 63 // https://crbug.com/522239 |
| 64 std::string presentation_id("mr_"); |
| 65 presentation_id += base::GenerateGUID(); |
| 66 |
| 67 CreateMediaRouteRequest* request = new CreateMediaRouteRequest( |
| 68 MediaSource(source_id), |
| 69 MediaSink(sink_id, std::string()), |
| 70 presentation_id, |
| 71 callbacks); |
| 72 int create_route_request_id = create_route_requests_.Add(request); |
| 73 |
| 74 JNIEnv* env = base::android::AttachCurrentThread(); |
| 75 ScopedJavaLocalRef<jstring> jsource_id = |
| 76 base::android::ConvertUTF8ToJavaString(env, source_id); |
| 77 ScopedJavaLocalRef<jstring> jsink_id = |
| 78 base::android::ConvertUTF8ToJavaString(env, sink_id); |
| 79 ScopedJavaLocalRef<jstring> jpresentation_id = |
| 80 base::android::ConvertUTF8ToJavaString(env, presentation_id); |
| 81 |
| 82 Java_ChromeMediaRouter_createRoute( |
| 83 env, |
| 84 java_media_router_.obj(), |
| 85 jsource_id.obj(), |
| 86 jsink_id.obj(), |
| 87 jpresentation_id.obj(), |
| 88 create_route_request_id); |
| 45 } | 89 } |
| 46 | 90 |
| 47 void MediaRouterAndroid::JoinRoute( | 91 void MediaRouterAndroid::JoinRoute( |
| 48 const MediaSource::Id& source, | 92 const MediaSource::Id& source, |
| 49 const std::string& presentation_id, | 93 const std::string& presentation_id, |
| 50 const GURL& origin, | 94 const GURL& origin, |
| 51 int tab_id, | 95 int tab_id, |
| 52 const std::vector<MediaRouteResponseCallback>& callbacks) { | 96 const std::vector<MediaRouteResponseCallback>& callbacks) { |
| 53 NOTIMPLEMENTED(); | 97 NOTIMPLEMENTED(); |
| 54 } | 98 } |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 } | 209 } |
| 166 | 210 |
| 167 std::string source_urn = ConvertJavaStringToUTF8(env, jsource_urn); | 211 std::string source_urn = ConvertJavaStringToUTF8(env, jsource_urn); |
| 168 auto it = sinks_observers_.find(source_urn); | 212 auto it = sinks_observers_.find(source_urn); |
| 169 if (it != sinks_observers_.end()) { | 213 if (it != sinks_observers_.end()) { |
| 170 FOR_EACH_OBSERVER(MediaSinksObserver, *it->second, | 214 FOR_EACH_OBSERVER(MediaSinksObserver, *it->second, |
| 171 OnSinksReceived(sinks_converted)); | 215 OnSinksReceived(sinks_converted)); |
| 172 } | 216 } |
| 173 } | 217 } |
| 174 | 218 |
| 219 void MediaRouterAndroid::OnRouteCreated( |
| 220 JNIEnv* env, |
| 221 jobject obj, |
| 222 jstring jmedia_route_id, |
| 223 jint jcreate_route_request_id, |
| 224 jboolean jis_local) { |
| 225 CreateMediaRouteRequest* request = |
| 226 create_route_requests_.Lookup(jcreate_route_request_id); |
| 227 if (!request) |
| 228 return; |
| 229 |
| 230 scoped_ptr<MediaRoute> route(new MediaRoute( |
| 231 ConvertJavaStringToUTF8(env, jmedia_route_id), |
| 232 request->media_source, |
| 233 request->media_sink, |
| 234 std::string(), |
| 235 jis_local, |
| 236 std::string())); |
| 237 |
| 238 for (const MediaRouteResponseCallback& callback : request->callbacks) |
| 239 callback.Run(route.get(), request->presentation_id, std::string()); |
| 240 |
| 241 create_route_requests_.Remove(jcreate_route_request_id); |
| 242 } |
| 243 |
| 244 void MediaRouterAndroid::OnRouteCreationError( |
| 245 JNIEnv* env, |
| 246 jobject obj, |
| 247 jstring jerror_text, |
| 248 jint jcreate_route_request_id) { |
| 249 CreateMediaRouteRequest* request = |
| 250 create_route_requests_.Lookup(jcreate_route_request_id); |
| 251 if (!request) |
| 252 return; |
| 253 |
| 254 std::string error_text = ConvertJavaStringToUTF8(env, jerror_text); |
| 255 |
| 256 for (const MediaRouteResponseCallback& callback : request->callbacks) |
| 257 callback.Run(nullptr, std::string(), error_text); |
| 258 |
| 259 create_route_requests_.Remove(jcreate_route_request_id); |
| 260 } |
| 175 | 261 |
| 176 } // namespace media_router | 262 } // namespace media_router |
| OLD | NEW |