| 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_dialog_controller_and
roid.h" | 5 #include "chrome/browser/media/android/router/media_router_dialog_controller_and
roid.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 "chrome/browser/media/android/router/media_router_android.h" |
| 10 #include "chrome/browser/media/router/media_router.h" |
| 11 #include "chrome/browser/media/router/media_router_factory.h" |
| 9 #include "chrome/browser/media/router/media_source.h" | 12 #include "chrome/browser/media/router/media_source.h" |
| 13 #include "chrome/browser/sessions/session_tab_helper.h" |
| 10 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
| 12 #include "content/public/browser/web_contents_delegate.h" | 16 #include "content/public/browser/web_contents_delegate.h" |
| 13 #include "jni/ChromeMediaRouterDialogController_jni.h" | 17 #include "jni/ChromeMediaRouterDialogController_jni.h" |
| 14 | 18 |
| 15 DEFINE_WEB_CONTENTS_USER_DATA_KEY( | 19 DEFINE_WEB_CONTENTS_USER_DATA_KEY( |
| 16 media_router::MediaRouterDialogControllerAndroid); | 20 media_router::MediaRouterDialogControllerAndroid); |
| 17 | 21 |
| 22 using base::android::ConvertJavaStringToUTF8; |
| 18 using content::WebContents; | 23 using content::WebContents; |
| 19 | 24 |
| 20 namespace media_router { | 25 namespace media_router { |
| 21 | 26 |
| 22 // static | 27 // static |
| 23 MediaRouterDialogControllerAndroid* | 28 MediaRouterDialogControllerAndroid* |
| 24 MediaRouterDialogControllerAndroid::GetOrCreateForWebContents( | 29 MediaRouterDialogControllerAndroid::GetOrCreateForWebContents( |
| 25 WebContents* web_contents) { | 30 WebContents* web_contents) { |
| 26 DCHECK(web_contents); | 31 DCHECK(web_contents); |
| 27 // This call does nothing if the controller already exists. | 32 // This call does nothing if the controller already exists. |
| 28 MediaRouterDialogControllerAndroid::CreateForWebContents(web_contents); | 33 MediaRouterDialogControllerAndroid::CreateForWebContents(web_contents); |
| 29 return MediaRouterDialogControllerAndroid::FromWebContents(web_contents); | 34 return MediaRouterDialogControllerAndroid::FromWebContents(web_contents); |
| 30 } | 35 } |
| 31 | 36 |
| 37 void MediaRouterDialogControllerAndroid::OnSinkSelected( |
| 38 JNIEnv* env, jobject obj, jstring jsink_id) { |
| 39 scoped_ptr<CreatePresentationSessionRequest> |
| 40 request(TakePresentationRequest()); |
| 41 |
| 42 const std::string& source_id = request->media_source().id(); |
| 43 const GURL& origin = request->frame_url().GetOrigin(); |
| 44 |
| 45 std::vector<MediaRouteResponseCallback> route_response_callbacks; |
| 46 route_response_callbacks.push_back( |
| 47 base::Bind(&CreatePresentationSessionRequest::HandleRouteResponse, |
| 48 base::Passed(&request))); |
| 49 |
| 50 MediaRouter* router = MediaRouterFactory::GetApiForBrowserContext( |
| 51 initiator()->GetBrowserContext()); |
| 52 router->CreateRoute(source_id, ConvertJavaStringToUTF8(env, jsink_id), origin, |
| 53 SessionTabHelper::IdForTab(initiator()), |
| 54 route_response_callbacks); |
| 55 } |
| 56 |
| 57 void MediaRouterDialogControllerAndroid::OnDialogDismissed( |
| 58 JNIEnv* env, jobject obj) { |
| 59 scoped_ptr<CreatePresentationSessionRequest> request( |
| 60 TakePresentationRequest()); |
| 61 |
| 62 // If OnDialogDismissed is called after OnSinkSelected, do nothing. |
| 63 if (!request) |
| 64 return; |
| 65 |
| 66 request->MaybeInvokeErrorCallback(content::PresentationError( |
| 67 content::PRESENTATION_ERROR_SESSION_REQUEST_CANCELLED, |
| 68 "The device picker dialog has been dismissed")); |
| 69 } |
| 70 |
| 32 MediaRouterDialogControllerAndroid::MediaRouterDialogControllerAndroid( | 71 MediaRouterDialogControllerAndroid::MediaRouterDialogControllerAndroid( |
| 33 WebContents* web_contents) | 72 WebContents* web_contents) |
| 34 : MediaRouterDialogController(web_contents) { | 73 : MediaRouterDialogController(web_contents) { |
| 35 JNIEnv* env = base::android::AttachCurrentThread(); | 74 JNIEnv* env = base::android::AttachCurrentThread(); |
| 36 java_dialog_controller_.Reset(Java_ChromeMediaRouterDialogController_create( | 75 java_dialog_controller_.Reset(Java_ChromeMediaRouterDialogController_create( |
| 37 env, | 76 env, |
| 38 reinterpret_cast<jlong>(this), | 77 reinterpret_cast<jlong>(this), |
| 39 base::android::GetApplicationContext())); | 78 base::android::GetApplicationContext())); |
| 40 } | 79 } |
| 41 | 80 |
| 42 // static | 81 // static |
| 43 bool MediaRouterDialogControllerAndroid::Register(JNIEnv* env) { | 82 bool MediaRouterDialogControllerAndroid::Register(JNIEnv* env) { |
| 44 bool ret = RegisterNativesImpl(env); | 83 return RegisterNativesImpl(env); |
| 45 // No native calls to register yet. | |
| 46 // DCHECK(g_ChromeMediaRouterDialogController_clazz); | |
| 47 return ret; | |
| 48 } | 84 } |
| 49 | 85 |
| 50 MediaRouterDialogControllerAndroid::~MediaRouterDialogControllerAndroid() { | 86 MediaRouterDialogControllerAndroid::~MediaRouterDialogControllerAndroid() { |
| 51 } | 87 } |
| 52 | 88 |
| 53 void MediaRouterDialogControllerAndroid::CreateMediaRouterDialog() { | 89 void MediaRouterDialogControllerAndroid::CreateMediaRouterDialog() { |
| 54 JNIEnv* env = base::android::AttachCurrentThread(); | 90 JNIEnv* env = base::android::AttachCurrentThread(); |
| 55 | 91 |
| 56 scoped_ptr<CreatePresentationSessionRequest> presentation_request( | |
| 57 PassPresentationRequest()); | |
| 58 | |
| 59 ScopedJavaLocalRef<jstring> jsource_urn = | 92 ScopedJavaLocalRef<jstring> jsource_urn = |
| 60 base::android::ConvertUTF8ToJavaString( | 93 base::android::ConvertUTF8ToJavaString( |
| 61 env, presentation_request->media_source().id()); | 94 env, presentation_request()->media_source().id()); |
| 62 | 95 |
| 63 Java_ChromeMediaRouterDialogController_createDialog( | 96 Java_ChromeMediaRouterDialogController_createDialog( |
| 64 env, java_dialog_controller_.obj(), jsource_urn.obj()); | 97 env, java_dialog_controller_.obj(), jsource_urn.obj()); |
| 65 } | 98 } |
| 66 | 99 |
| 67 void MediaRouterDialogControllerAndroid::CloseMediaRouterDialog() { | 100 void MediaRouterDialogControllerAndroid::CloseMediaRouterDialog() { |
| 68 JNIEnv* env = base::android::AttachCurrentThread(); | 101 JNIEnv* env = base::android::AttachCurrentThread(); |
| 69 | 102 |
| 70 Java_ChromeMediaRouterDialogController_closeDialog( | 103 Java_ChromeMediaRouterDialogController_closeDialog( |
| 71 env, java_dialog_controller_.obj()); | 104 env, java_dialog_controller_.obj()); |
| 72 } | 105 } |
| 73 | 106 |
| 74 bool MediaRouterDialogControllerAndroid::IsShowingMediaRouterDialog() const { | 107 bool MediaRouterDialogControllerAndroid::IsShowingMediaRouterDialog() const { |
| 75 JNIEnv* env = base::android::AttachCurrentThread(); | 108 JNIEnv* env = base::android::AttachCurrentThread(); |
| 76 return Java_ChromeMediaRouterDialogController_isShowingDialog( | 109 return Java_ChromeMediaRouterDialogController_isShowingDialog( |
| 77 env, java_dialog_controller_.obj()); | 110 env, java_dialog_controller_.obj()); |
| 78 } | 111 } |
| 79 | 112 |
| 80 } // namespace media_router | 113 } // namespace media_router |
| 81 | 114 |
| OLD | NEW |