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/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. | |
|
mlamouri (slow - plz ping)
2015/08/18 16:04:55
nit: add bug number.
whywhat
2015/08/18 20:06:33
Will send a patch.
| |
| 63 std::string presentation_id("mr_"); | |
| 64 presentation_id += base::GenerateGUID(); | |
| 65 | |
| 66 CreateMediaRouteRequest* request = new CreateMediaRouteRequest( | |
| 67 MediaSource(source_id), | |
| 68 MediaSink(sink_id, std::string()), | |
| 69 presentation_id, | |
| 70 callbacks); | |
| 71 int create_route_request_id = create_route_requests_.Add(request); | |
| 72 | |
| 73 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 74 ScopedJavaLocalRef<jstring> jsource_id = | |
| 75 base::android::ConvertUTF8ToJavaString(env, source_id); | |
| 76 ScopedJavaLocalRef<jstring> jsink_id = | |
| 77 base::android::ConvertUTF8ToJavaString(env, sink_id); | |
| 78 ScopedJavaLocalRef<jstring> jpresentation_id = | |
| 79 base::android::ConvertUTF8ToJavaString(env, presentation_id); | |
| 80 | |
| 81 Java_ChromeMediaRouter_createRoute( | |
| 82 env, | |
| 83 java_media_router_.obj(), | |
| 84 jsource_id.obj(), | |
| 85 jsink_id.obj(), | |
| 86 jpresentation_id.obj(), | |
| 87 create_route_request_id); | |
| 45 } | 88 } |
| 46 | 89 |
| 47 void MediaRouterAndroid::JoinRoute( | 90 void MediaRouterAndroid::JoinRoute( |
| 48 const MediaSource::Id& source, | 91 const MediaSource::Id& source, |
| 49 const std::string& presentation_id, | 92 const std::string& presentation_id, |
| 50 const GURL& origin, | 93 const GURL& origin, |
| 51 int tab_id, | 94 int tab_id, |
| 52 const std::vector<MediaRouteResponseCallback>& callbacks) { | 95 const std::vector<MediaRouteResponseCallback>& callbacks) { |
| 53 NOTIMPLEMENTED(); | 96 NOTIMPLEMENTED(); |
| 54 } | 97 } |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 165 } | 208 } |
| 166 | 209 |
| 167 std::string source_urn = ConvertJavaStringToUTF8(env, jsource_urn); | 210 std::string source_urn = ConvertJavaStringToUTF8(env, jsource_urn); |
| 168 auto it = sinks_observers_.find(source_urn); | 211 auto it = sinks_observers_.find(source_urn); |
| 169 if (it != sinks_observers_.end()) { | 212 if (it != sinks_observers_.end()) { |
| 170 FOR_EACH_OBSERVER(MediaSinksObserver, *it->second, | 213 FOR_EACH_OBSERVER(MediaSinksObserver, *it->second, |
| 171 OnSinksReceived(sinks_converted)); | 214 OnSinksReceived(sinks_converted)); |
| 172 } | 215 } |
| 173 } | 216 } |
| 174 | 217 |
| 218 void MediaRouterAndroid::OnRouteCreated( | |
| 219 JNIEnv* env, | |
| 220 jobject obj, | |
| 221 jstring jmedia_route_id, | |
| 222 jint jcreate_route_request_id, | |
| 223 jboolean jis_local) { | |
| 224 CreateMediaRouteRequest* request = | |
| 225 create_route_requests_.Lookup(jcreate_route_request_id); | |
| 226 if (!request) | |
| 227 return; | |
|
mlamouri (slow - plz ping)
2015/08/18 16:04:55
DCHECK?
whywhat
2015/08/18 20:06:33
Done.
| |
| 228 | |
| 229 scoped_ptr<MediaRoute> route(new MediaRoute( | |
| 230 ConvertJavaStringToUTF8(env, jmedia_route_id), | |
| 231 request->media_source, | |
| 232 request->media_sink, | |
| 233 std::string(), | |
| 234 jis_local, | |
| 235 std::string())); | |
| 236 | |
| 237 for (const MediaRouteResponseCallback& callback : request->callbacks) | |
| 238 callback.Run(route.get(), request->presentation_id, std::string()); | |
| 239 | |
| 240 create_route_requests_.Remove(jcreate_route_request_id); | |
| 241 } | |
| 242 | |
| 243 void MediaRouterAndroid::OnRouteCreationError( | |
| 244 JNIEnv* env, | |
| 245 jobject obj, | |
| 246 jstring jerror_text, | |
| 247 jint jcreate_route_request_id) { | |
| 248 CreateMediaRouteRequest* request = | |
| 249 create_route_requests_.Lookup(jcreate_route_request_id); | |
| 250 if (!request) | |
| 251 return; | |
|
mlamouri (slow - plz ping)
2015/08/18 16:04:55
DCHECK?
whywhat
2015/08/18 20:06:33
Done.
| |
| 252 | |
| 253 std::string error_text = ConvertJavaStringToUTF8(env, jerror_text); | |
| 254 | |
| 255 for (const MediaRouteResponseCallback& callback : request->callbacks) | |
| 256 callback.Run(nullptr, std::string(), error_text); | |
| 257 | |
| 258 create_route_requests_.Remove(jcreate_route_request_id); | |
| 259 } | |
| 175 | 260 |
| 176 } // namespace media_router | 261 } // namespace media_router |
| OLD | NEW |