| 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" | 9 #include "chrome/browser/media/android/router/media_router_android.h" |
| 10 #include "chrome/browser/media/router/media_router.h" | 10 #include "chrome/browser/media/router/media_router.h" |
| 11 #include "chrome/browser/media/router/media_router_factory.h" | 11 #include "chrome/browser/media/router/media_router_factory.h" |
| 12 #include "chrome/browser/media/router/media_source.h" | 12 #include "chrome/browser/media/router/media_source.h" |
| 13 #include "chrome/browser/media/router/media_source_helper.h" |
| 14 #include "chrome/browser/media/router/presentation_request.h" |
| 13 #include "chrome/browser/sessions/session_tab_helper.h" | 15 #include "chrome/browser/sessions/session_tab_helper.h" |
| 14 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 15 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
| 16 #include "content/public/browser/web_contents_delegate.h" | 18 #include "content/public/browser/web_contents_delegate.h" |
| 17 #include "jni/ChromeMediaRouterDialogController_jni.h" | 19 #include "jni/ChromeMediaRouterDialogController_jni.h" |
| 18 | 20 |
| 19 DEFINE_WEB_CONTENTS_USER_DATA_KEY( | 21 DEFINE_WEB_CONTENTS_USER_DATA_KEY( |
| 20 media_router::MediaRouterDialogControllerAndroid); | 22 media_router::MediaRouterDialogControllerAndroid); |
| 21 | 23 |
| 22 using base::android::ConvertJavaStringToUTF8; | 24 using base::android::ConvertJavaStringToUTF8; |
| 23 using content::WebContents; | 25 using content::WebContents; |
| 24 | 26 |
| 25 namespace media_router { | 27 namespace media_router { |
| 26 | 28 |
| 27 // static | 29 // static |
| 28 MediaRouterDialogControllerAndroid* | 30 MediaRouterDialogControllerAndroid* |
| 29 MediaRouterDialogControllerAndroid::GetOrCreateForWebContents( | 31 MediaRouterDialogControllerAndroid::GetOrCreateForWebContents( |
| 30 WebContents* web_contents) { | 32 WebContents* web_contents) { |
| 31 DCHECK(web_contents); | 33 DCHECK(web_contents); |
| 32 // This call does nothing if the controller already exists. | 34 // This call does nothing if the controller already exists. |
| 33 MediaRouterDialogControllerAndroid::CreateForWebContents(web_contents); | 35 MediaRouterDialogControllerAndroid::CreateForWebContents(web_contents); |
| 34 return MediaRouterDialogControllerAndroid::FromWebContents(web_contents); | 36 return MediaRouterDialogControllerAndroid::FromWebContents(web_contents); |
| 35 } | 37 } |
| 36 | 38 |
| 37 void MediaRouterDialogControllerAndroid::OnSinkSelected( | 39 void MediaRouterDialogControllerAndroid::OnSinkSelected( |
| 38 JNIEnv* env, jobject obj, jstring jsink_id) { | 40 JNIEnv* env, jobject obj, jstring jsink_id) { |
| 39 scoped_ptr<CreatePresentationSessionRequest> | 41 scoped_ptr<CreatePresentationSessionRequest> create_session_request = |
| 40 request(TakePresentationRequest()); | 42 TakePresentationRequest(); |
| 41 | 43 const PresentationRequest& presentation_request = |
| 42 const std::string& source_id = request->media_source().id(); | 44 create_session_request->presentation_request(); |
| 43 const GURL& origin = request->frame_url().GetOrigin(); | 45 const MediaSource::Id source_id(presentation_request.GetMediaSource().id()); |
| 46 const GURL origin(presentation_request.frame_url().GetOrigin()); |
| 44 | 47 |
| 45 std::vector<MediaRouteResponseCallback> route_response_callbacks; | 48 std::vector<MediaRouteResponseCallback> route_response_callbacks; |
| 46 route_response_callbacks.push_back( | 49 route_response_callbacks.push_back( |
| 47 base::Bind(&CreatePresentationSessionRequest::HandleRouteResponse, | 50 base::Bind(&CreatePresentationSessionRequest::HandleRouteResponse, |
| 48 base::Passed(&request))); | 51 base::Passed(&create_session_request))); |
| 49 | 52 |
| 50 MediaRouter* router = MediaRouterFactory::GetApiForBrowserContext( | 53 MediaRouter* router = MediaRouterFactory::GetApiForBrowserContext( |
| 51 initiator()->GetBrowserContext()); | 54 initiator()->GetBrowserContext()); |
| 52 router->CreateRoute(source_id, ConvertJavaStringToUTF8(env, jsink_id), origin, | 55 router->CreateRoute(source_id, ConvertJavaStringToUTF8(env, jsink_id), origin, |
| 53 SessionTabHelper::IdForTab(initiator()), | 56 SessionTabHelper::IdForTab(initiator()), |
| 54 route_response_callbacks); | 57 route_response_callbacks); |
| 55 } | 58 } |
| 56 | 59 |
| 57 void MediaRouterDialogControllerAndroid::OnRouteClosed( | 60 void MediaRouterDialogControllerAndroid::OnRouteClosed( |
| 58 JNIEnv* env, | 61 JNIEnv* env, |
| 59 jobject obj, | 62 jobject obj, |
| 60 jstring jmedia_route_id) { | 63 jstring jmedia_route_id) { |
| 61 std::string media_route_id = ConvertJavaStringToUTF8(env, jmedia_route_id); | 64 std::string media_route_id = ConvertJavaStringToUTF8(env, jmedia_route_id); |
| 62 | 65 |
| 63 MediaRouter* router = MediaRouterFactory::GetApiForBrowserContext( | 66 MediaRouter* router = MediaRouterFactory::GetApiForBrowserContext( |
| 64 initiator()->GetBrowserContext()); | 67 initiator()->GetBrowserContext()); |
| 65 | 68 |
| 66 router->CloseRoute(media_route_id); | 69 router->CloseRoute(media_route_id); |
| 67 | 70 |
| 68 CancelPresentationRequest(); | 71 CancelPresentationRequest(); |
| 69 } | 72 } |
| 70 | 73 |
| 71 void MediaRouterDialogControllerAndroid::OnDialogCancelled( | 74 void MediaRouterDialogControllerAndroid::OnDialogCancelled( |
| 72 JNIEnv* env, jobject obj) { | 75 JNIEnv* env, jobject obj) { |
| 73 CancelPresentationRequest(); | 76 CancelPresentationRequest(); |
| 74 } | 77 } |
| 75 | 78 |
| 76 void MediaRouterDialogControllerAndroid::CancelPresentationRequest() { | 79 void MediaRouterDialogControllerAndroid::CancelPresentationRequest() { |
| 77 scoped_ptr<CreatePresentationSessionRequest> request( | 80 scoped_ptr<CreatePresentationSessionRequest> request = |
| 78 TakePresentationRequest()); | 81 TakePresentationRequest(); |
| 79 | |
| 80 DCHECK(request); | 82 DCHECK(request); |
| 81 | 83 |
| 82 request->InvokeErrorCallback(content::PresentationError( | 84 request->InvokeErrorCallback(content::PresentationError( |
| 83 content::PRESENTATION_ERROR_SESSION_REQUEST_CANCELLED, | 85 content::PRESENTATION_ERROR_SESSION_REQUEST_CANCELLED, |
| 84 "Dialog closed.")); | 86 "Dialog closed.")); |
| 85 } | 87 } |
| 86 | 88 |
| 87 MediaRouterDialogControllerAndroid::MediaRouterDialogControllerAndroid( | 89 MediaRouterDialogControllerAndroid::MediaRouterDialogControllerAndroid( |
| 88 WebContents* web_contents) | 90 WebContents* web_contents) |
| 89 : MediaRouterDialogController(web_contents), | 91 : MediaRouterDialogController(web_contents), |
| (...skipping 10 matching lines...) Expand all Loading... |
| 100 bool MediaRouterDialogControllerAndroid::Register(JNIEnv* env) { | 102 bool MediaRouterDialogControllerAndroid::Register(JNIEnv* env) { |
| 101 return RegisterNativesImpl(env); | 103 return RegisterNativesImpl(env); |
| 102 } | 104 } |
| 103 | 105 |
| 104 MediaRouterDialogControllerAndroid::~MediaRouterDialogControllerAndroid() { | 106 MediaRouterDialogControllerAndroid::~MediaRouterDialogControllerAndroid() { |
| 105 } | 107 } |
| 106 | 108 |
| 107 void MediaRouterDialogControllerAndroid::CreateMediaRouterDialog() { | 109 void MediaRouterDialogControllerAndroid::CreateMediaRouterDialog() { |
| 108 JNIEnv* env = base::android::AttachCurrentThread(); | 110 JNIEnv* env = base::android::AttachCurrentThread(); |
| 109 | 111 |
| 110 const MediaSource::Id& media_source_id = | 112 const MediaSource::Id source_id( |
| 111 presentation_request()->media_source().id(); | 113 create_session_request()->presentation_request().GetMediaSource().id()); |
| 112 ScopedJavaLocalRef<jstring> jsource_urn = | 114 ScopedJavaLocalRef<jstring> jsource_urn = |
| 113 base::android::ConvertUTF8ToJavaString(env, media_source_id); | 115 base::android::ConvertUTF8ToJavaString(env, source_id); |
| 114 | 116 |
| 115 // If it's a single route with the same source, show the controller dialog | 117 // If it's a single route with the same source, show the controller dialog |
| 116 // instead of the device picker. | 118 // instead of the device picker. |
| 117 // TODO(avayvod): maybe this logic should be in | 119 // TODO(avayvod): maybe this logic should be in |
| 118 // PresentationServiceDelegateImpl: if the route exists for the same frame | 120 // PresentationServiceDelegateImpl: if the route exists for the same frame |
| 119 // and tab, show the route controller dialog, if not, show the device picker. | 121 // and tab, show the route controller dialog, if not, show the device picker. |
| 120 if (single_existing_route_.get() && | 122 if (single_existing_route_.get() && |
| 121 single_existing_route_->media_source().id() == media_source_id) { | 123 single_existing_route_->media_source().id() == source_id) { |
| 122 ScopedJavaLocalRef<jstring> jmedia_route_id = | 124 ScopedJavaLocalRef<jstring> jmedia_route_id = |
| 123 base::android::ConvertUTF8ToJavaString( | 125 base::android::ConvertUTF8ToJavaString( |
| 124 env, single_existing_route_->media_route_id()); | 126 env, single_existing_route_->media_route_id()); |
| 125 | 127 |
| 126 Java_ChromeMediaRouterDialogController_openRouteControllerDialog( | 128 Java_ChromeMediaRouterDialogController_openRouteControllerDialog( |
| 127 env, java_dialog_controller_.obj(), jsource_urn.obj(), | 129 env, java_dialog_controller_.obj(), jsource_urn.obj(), |
| 128 jmedia_route_id.obj()); | 130 jmedia_route_id.obj()); |
| 129 return; | 131 return; |
| 130 } | 132 } |
| 131 | 133 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 156 if (single_existing_route_.get() && | 158 if (single_existing_route_.get() && |
| 157 single_existing_route_->media_route_id() == routes[0].media_route_id()) { | 159 single_existing_route_->media_route_id() == routes[0].media_route_id()) { |
| 158 return; | 160 return; |
| 159 } | 161 } |
| 160 | 162 |
| 161 single_existing_route_.reset(new MediaRoute(routes[0])); | 163 single_existing_route_.reset(new MediaRoute(routes[0])); |
| 162 } | 164 } |
| 163 | 165 |
| 164 } // namespace media_router | 166 } // namespace media_router |
| 165 | 167 |
| OLD | NEW |