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_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" |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 39 request(TakePresentationRequest()); | 39 request(TakePresentationRequest()); |
| 40 | 40 |
| 41 const std::string& source_id = request->media_source().id(); | 41 const std::string& source_id = request->media_source().id(); |
| 42 const GURL& origin = request->frame_url().GetOrigin(); | 42 const GURL& origin = request->frame_url().GetOrigin(); |
| 43 | 43 |
| 44 std::vector<MediaRouteResponseCallback> route_response_callbacks; | 44 std::vector<MediaRouteResponseCallback> route_response_callbacks; |
| 45 route_response_callbacks.push_back( | 45 route_response_callbacks.push_back( |
| 46 base::Bind(&CreatePresentationSessionRequest::HandleRouteResponse, | 46 base::Bind(&CreatePresentationSessionRequest::HandleRouteResponse, |
| 47 base::Passed(&request))); | 47 base::Passed(&request))); |
| 48 | 48 |
| 49 MediaRouter* router = MediaRouterFactory::GetApiForBrowserContext( | 49 router_->CreateRoute( |
| 50 initiator()->GetBrowserContext()); | |
| 51 router->CreateRoute( | |
| 52 source_id, | 50 source_id, |
| 53 ConvertJavaStringToUTF8(env, jsink_id), | 51 ConvertJavaStringToUTF8(env, jsink_id), |
| 54 origin, | 52 origin, |
| 55 initiator(), | 53 initiator(), |
| 56 route_response_callbacks); | 54 route_response_callbacks); |
| 57 } | 55 } |
| 58 | 56 |
| 59 void MediaRouterDialogControllerAndroid::OnRouteClosed( | 57 void MediaRouterDialogControllerAndroid::OnRouteClosed( |
| 60 JNIEnv* env, | 58 JNIEnv* env, |
| 61 jobject obj, | 59 jobject obj, |
| 62 jstring jmedia_route_id) { | 60 jstring jmedia_route_id) { |
| 63 std::string media_route_id = ConvertJavaStringToUTF8(env, jmedia_route_id); | 61 std::string media_route_id = ConvertJavaStringToUTF8(env, jmedia_route_id); |
| 64 | 62 |
| 65 MediaRouter* router = MediaRouterFactory::GetApiForBrowserContext( | 63 router_->CloseRoute(media_route_id); |
| 66 initiator()->GetBrowserContext()); | |
| 67 | |
| 68 router->CloseRoute(media_route_id); | |
| 69 | 64 |
| 70 CancelPresentationRequest(); | 65 CancelPresentationRequest(); |
| 71 } | 66 } |
| 72 | 67 |
| 73 void MediaRouterDialogControllerAndroid::OnDialogCancelled( | 68 void MediaRouterDialogControllerAndroid::OnDialogCancelled( |
| 74 JNIEnv* env, jobject obj) { | 69 JNIEnv* env, jobject obj) { |
| 75 CancelPresentationRequest(); | 70 CancelPresentationRequest(); |
| 76 } | 71 } |
| 77 | 72 |
| 78 void MediaRouterDialogControllerAndroid::CancelPresentationRequest() { | 73 void MediaRouterDialogControllerAndroid::CancelPresentationRequest() { |
| 79 scoped_ptr<CreatePresentationSessionRequest> request( | 74 scoped_ptr<CreatePresentationSessionRequest> request( |
| 80 TakePresentationRequest()); | 75 TakePresentationRequest()); |
| 81 | 76 |
| 82 DCHECK(request); | 77 DCHECK(request); |
| 83 | 78 |
| 84 request->InvokeErrorCallback(content::PresentationError( | 79 request->InvokeErrorCallback(content::PresentationError( |
| 85 content::PRESENTATION_ERROR_SESSION_REQUEST_CANCELLED, | 80 content::PRESENTATION_ERROR_SESSION_REQUEST_CANCELLED, |
| 86 "Dialog closed.")); | 81 "Dialog closed.")); |
| 87 } | 82 } |
| 88 | 83 |
| 89 MediaRouterDialogControllerAndroid::MediaRouterDialogControllerAndroid( | 84 MediaRouterDialogControllerAndroid::MediaRouterDialogControllerAndroid( |
| 90 WebContents* web_contents) | 85 WebContents* web_contents) |
| 91 : MediaRouterDialogController(web_contents), | 86 : MediaRouterDialogController(web_contents) { |
| 92 MediaRoutesObserver(MediaRouterFactory::GetApiForBrowserContext( | |
| 93 initiator()->GetBrowserContext())) { | |
| 94 JNIEnv* env = base::android::AttachCurrentThread(); | 87 JNIEnv* env = base::android::AttachCurrentThread(); |
| 88 router_ = static_cast<MediaRouter*>( | |
|
imcheng
2015/11/26 00:49:45
Can the static_cast be removed?
matt.boetger
2015/12/01 01:26:55
Reverting.
| |
| 89 MediaRouterFactory::GetApiForBrowserContext( | |
| 90 initiator()->GetBrowserContext())); | |
| 95 java_dialog_controller_.Reset(Java_ChromeMediaRouterDialogController_create( | 91 java_dialog_controller_.Reset(Java_ChromeMediaRouterDialogController_create( |
| 96 env, | 92 env, |
| 97 reinterpret_cast<jlong>(this), | 93 reinterpret_cast<jlong>(this), |
| 98 base::android::GetApplicationContext())); | 94 base::android::GetApplicationContext())); |
| 99 } | 95 } |
| 100 | 96 |
| 101 // static | 97 // static |
| 102 bool MediaRouterDialogControllerAndroid::Register(JNIEnv* env) { | 98 bool MediaRouterDialogControllerAndroid::Register(JNIEnv* env) { |
| 103 return RegisterNativesImpl(env); | 99 return RegisterNativesImpl(env); |
| 104 } | 100 } |
| 105 | 101 |
| 106 MediaRouterDialogControllerAndroid::~MediaRouterDialogControllerAndroid() { | 102 MediaRouterDialogControllerAndroid::~MediaRouterDialogControllerAndroid() { |
| 107 } | 103 } |
| 108 | 104 |
| 109 void MediaRouterDialogControllerAndroid::CreateMediaRouterDialog() { | 105 void MediaRouterDialogControllerAndroid::CreateMediaRouterDialog() { |
| 110 JNIEnv* env = base::android::AttachCurrentThread(); | 106 JNIEnv* env = base::android::AttachCurrentThread(); |
| 111 | 107 |
| 112 const MediaSource::Id& media_source_id = | 108 const MediaSource::Id& media_source_id = |
| 113 presentation_request()->media_source().id(); | 109 presentation_request()->media_source().id(); |
| 114 ScopedJavaLocalRef<jstring> jsource_urn = | 110 ScopedJavaLocalRef<jstring> jsource_urn = |
| 115 base::android::ConvertUTF8ToJavaString(env, media_source_id); | 111 base::android::ConvertUTF8ToJavaString(env, media_source_id); |
| 116 | 112 |
| 113 routes_observer_.reset(new UIMediaRoutesObserver( | |
| 114 router_, | |
| 115 media_source_id, | |
|
imcheng
2015/11/26 00:49:45
You might want to check with Anton/Mounir if this
matt.boetger
2015/12/01 01:26:55
Reverting.
| |
| 116 base::Bind(&MediaRouterDialogControllerAndroid::OnRoutesUpdated, | |
| 117 base::Unretained(this)))); | |
| 118 | |
| 117 // If it's a single route with the same source, show the controller dialog | 119 // If it's a single route with the same source, show the controller dialog |
| 118 // instead of the device picker. | 120 // instead of the device picker. |
| 119 // TODO(avayvod): maybe this logic should be in | 121 // TODO(avayvod): maybe this logic should be in |
| 120 // PresentationServiceDelegateImpl: if the route exists for the same frame | 122 // PresentationServiceDelegateImpl: if the route exists for the same frame |
| 121 // and tab, show the route controller dialog, if not, show the device picker. | 123 // and tab, show the route controller dialog, if not, show the device picker. |
| 122 if (single_existing_route_.get() && | 124 if (single_existing_route_.get() && |
| 123 single_existing_route_->media_source().id() == media_source_id) { | 125 single_existing_route_->media_source().id() == media_source_id) { |
| 124 ScopedJavaLocalRef<jstring> jmedia_route_id = | 126 ScopedJavaLocalRef<jstring> jmedia_route_id = |
| 125 base::android::ConvertUTF8ToJavaString( | 127 base::android::ConvertUTF8ToJavaString( |
| 126 env, single_existing_route_->media_route_id()); | 128 env, single_existing_route_->media_route_id()); |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 142 env, java_dialog_controller_.obj()); | 144 env, java_dialog_controller_.obj()); |
| 143 } | 145 } |
| 144 | 146 |
| 145 bool MediaRouterDialogControllerAndroid::IsShowingMediaRouterDialog() const { | 147 bool MediaRouterDialogControllerAndroid::IsShowingMediaRouterDialog() const { |
| 146 JNIEnv* env = base::android::AttachCurrentThread(); | 148 JNIEnv* env = base::android::AttachCurrentThread(); |
| 147 return Java_ChromeMediaRouterDialogController_isShowingDialog( | 149 return Java_ChromeMediaRouterDialogController_isShowingDialog( |
| 148 env, java_dialog_controller_.obj()); | 150 env, java_dialog_controller_.obj()); |
| 149 } | 151 } |
| 150 | 152 |
| 151 void MediaRouterDialogControllerAndroid::OnRoutesUpdated( | 153 void MediaRouterDialogControllerAndroid::OnRoutesUpdated( |
| 152 const std::vector<MediaRoute>& routes) { | 154 const std::vector<MediaRoute>& routes, |
| 155 const std::vector<MediaRoute::Id>& joinable_route_ids) { | |
| 156 | |
| 153 if (routes.size() != 1) { | 157 if (routes.size() != 1) { |
| 154 single_existing_route_.reset(); | 158 single_existing_route_.reset(); |
| 155 return; | 159 return; |
| 156 } | 160 } |
| 157 | 161 |
| 158 if (single_existing_route_.get() && | 162 if (single_existing_route_.get() && |
| 159 single_existing_route_->media_route_id() == routes[0].media_route_id()) { | 163 single_existing_route_->media_route_id() == routes[0].media_route_id()) { |
| 160 return; | 164 return; |
| 161 } | 165 } |
| 162 | 166 |
| 163 single_existing_route_.reset(new MediaRoute(routes[0])); | 167 single_existing_route_.reset(new MediaRoute(routes[0])); |
| 164 } | 168 } |
| 165 | 169 |
| 170 | |
| 171 MediaRouterDialogControllerAndroid:: | |
|
imcheng
2015/11/26 00:49:45
This looks copied from MediaRouterUI. I am not sur
matt.boetger
2015/12/01 01:26:55
Reverting.
| |
| 172 UIMediaRoutesObserver::UIMediaRoutesObserver( | |
| 173 MediaRouter* router, const MediaSource::Id source_id, | |
| 174 const RoutesUpdatedCallback& callback) | |
| 175 : MediaRoutesObserver(router, source_id), callback_(callback) { | |
| 176 DCHECK(!callback_.is_null()); | |
| 177 } | |
| 178 | |
| 179 MediaRouterDialogControllerAndroid:: | |
| 180 UIMediaRoutesObserver::~UIMediaRoutesObserver() {} | |
| 181 | |
| 182 void MediaRouterDialogControllerAndroid::UIMediaRoutesObserver::OnRoutesUpdated( | |
| 183 const std::vector<MediaRoute>& routes, | |
| 184 const std::vector<MediaRoute::Id>& joinable_route_ids) { | |
| 185 | |
| 186 std::vector<MediaRoute> routes_for_display; | |
| 187 std::vector<MediaRoute::Id> joinable_route_ids_for_display; | |
| 188 for (const MediaRoute& route : routes) { | |
| 189 if (route.for_display()) { | |
| 190 #ifndef NDEBUG | |
| 191 for (const MediaRoute& existing_route : routes_for_display) { | |
| 192 if (existing_route.media_sink_id() == route.media_sink_id()) { | |
| 193 DVLOG(2) << "Received another route for display with the same sink" | |
| 194 << " id as an existing route. " | |
| 195 << route.media_route_id() << " has the same sink id as " | |
| 196 << existing_route.media_sink_id() << "."; | |
| 197 } | |
| 198 } | |
| 199 #endif | |
| 200 if (std::find(joinable_route_ids.begin(), | |
|
imcheng
2015/11/26 00:49:45
Presumably the MRP would only consider a route as
matt.boetger
2015/12/01 01:26:55
Reverting.
| |
| 201 joinable_route_ids.end(), route.media_route_id()) != | |
| 202 joinable_route_ids.end()) { | |
| 203 joinable_route_ids_for_display.push_back(route.media_route_id()); | |
| 204 } | |
| 205 routes_for_display.push_back(route); | |
| 206 } | |
| 207 } | |
| 208 | |
| 209 callback_.Run(routes_for_display, joinable_route_ids_for_display); | |
| 210 } | |
| 211 | |
| 166 } // namespace media_router | 212 } // namespace media_router |
| 167 | 213 |
| OLD | NEW |