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
3 // found in the LICENSE file.
5 #include "chrome/browser/media/android/router/media_router_dialog_controller_android.h"
7 #include "base/android/jni_android.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"
12 #include "chrome/browser/media/router/media_source.h"
13 #include "chrome/browser/sessions/session_tab_helper.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/browser/web_contents_delegate.h"
17 #include "jni/ChromeMediaRouterDialogController_jni.h"
19 DEFINE_WEB_CONTENTS_USER_DATA_KEY(
20 media_router::MediaRouterDialogControllerAndroid
);
22 using base::android::ConvertJavaStringToUTF8
;
23 using content::WebContents
;
25 namespace media_router
{
28 MediaRouterDialogControllerAndroid
*
29 MediaRouterDialogControllerAndroid::GetOrCreateForWebContents(
30 WebContents
* web_contents
) {
32 // This call does nothing if the controller already exists.
33 MediaRouterDialogControllerAndroid::CreateForWebContents(web_contents
);
34 return MediaRouterDialogControllerAndroid::FromWebContents(web_contents
);
37 void MediaRouterDialogControllerAndroid::OnSinkSelected(
38 JNIEnv
* env
, jobject obj
, jstring jsink_id
) {
39 scoped_ptr
<CreatePresentationSessionRequest
>
40 request(TakePresentationRequest());
42 const std::string
& source_id
= request
->media_source().id();
43 const GURL
& origin
= request
->frame_url().GetOrigin();
45 std::vector
<MediaRouteResponseCallback
> route_response_callbacks
;
46 route_response_callbacks
.push_back(
47 base::Bind(&CreatePresentationSessionRequest::HandleRouteResponse
,
48 base::Passed(&request
)));
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
);
57 void MediaRouterDialogControllerAndroid::OnDialogDismissed(
58 JNIEnv
* env
, jobject obj
) {
59 scoped_ptr
<CreatePresentationSessionRequest
> request(
60 TakePresentationRequest());
62 // If OnDialogDismissed is called after OnSinkSelected, do nothing.
66 request
->InvokeErrorCallback(content::PresentationError(
67 content::PRESENTATION_ERROR_SESSION_REQUEST_CANCELLED
,
68 "The device picker dialog has been dismissed"));
71 MediaRouterDialogControllerAndroid::MediaRouterDialogControllerAndroid(
72 WebContents
* web_contents
)
73 : MediaRouterDialogController(web_contents
) {
74 JNIEnv
* env
= base::android::AttachCurrentThread();
75 java_dialog_controller_
.Reset(Java_ChromeMediaRouterDialogController_create(
77 reinterpret_cast<jlong
>(this),
78 base::android::GetApplicationContext()));
82 bool MediaRouterDialogControllerAndroid::Register(JNIEnv
* env
) {
83 return RegisterNativesImpl(env
);
86 MediaRouterDialogControllerAndroid::~MediaRouterDialogControllerAndroid() {
89 void MediaRouterDialogControllerAndroid::CreateMediaRouterDialog() {
90 JNIEnv
* env
= base::android::AttachCurrentThread();
92 ScopedJavaLocalRef
<jstring
> jsource_urn
=
93 base::android::ConvertUTF8ToJavaString(
94 env
, presentation_request()->media_source().id());
96 Java_ChromeMediaRouterDialogController_createDialog(
97 env
, java_dialog_controller_
.obj(), jsource_urn
.obj());
100 void MediaRouterDialogControllerAndroid::CloseMediaRouterDialog() {
101 JNIEnv
* env
= base::android::AttachCurrentThread();
103 Java_ChromeMediaRouterDialogController_closeDialog(
104 env
, java_dialog_controller_
.obj());
107 bool MediaRouterDialogControllerAndroid::IsShowingMediaRouterDialog() const {
108 JNIEnv
* env
= base::android::AttachCurrentThread();
109 return Java_ChromeMediaRouterDialogController_isShowingDialog(
110 env
, java_dialog_controller_
.obj());
113 } // namespace media_router