| 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 "components/service_tab_launcher/browser/android/service_tab_launcher.h
" | 5 #include "chrome/browser/android/service_tab_launcher.h" |
| 6 | 6 |
| 7 #include "base/android/context_utils.h" | 7 #include "base/android/context_utils.h" |
| 8 #include "base/android/jni_string.h" | 8 #include "base/android/jni_string.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "content/public/browser/browser_context.h" | 10 #include "content/public/browser/browser_context.h" |
| 11 #include "content/public/browser/page_navigator.h" | 11 #include "content/public/browser/page_navigator.h" |
| 12 #include "content/public/browser/web_contents.h" | 12 #include "content/public/browser/web_contents.h" |
| 13 #include "jni/ServiceTabLauncher_jni.h" | 13 #include "jni/ServiceTabLauncher_jni.h" |
| 14 | 14 |
| 15 using base::android::AttachCurrentThread; | 15 using base::android::AttachCurrentThread; |
| 16 using base::android::ConvertUTF8ToJavaString; | 16 using base::android::ConvertUTF8ToJavaString; |
| 17 using base::android::GetApplicationContext; | 17 using base::android::GetApplicationContext; |
| 18 | 18 |
| 19 // Called by Java when the WebContents instance for a request Id is available. | 19 // Called by Java when the WebContents instance for a request Id is available. |
| 20 void OnWebContentsForRequestAvailable( | 20 void OnWebContentsForRequestAvailable( |
| 21 JNIEnv* env, | 21 JNIEnv* env, |
| 22 const JavaParamRef<jclass>& clazz, | 22 const JavaParamRef<jclass>& clazz, |
| 23 jint request_id, | 23 jint request_id, |
| 24 const JavaParamRef<jobject>& android_web_contents) { | 24 const JavaParamRef<jobject>& android_web_contents) { |
| 25 service_tab_launcher::ServiceTabLauncher::GetInstance()->OnTabLaunched( | 25 ServiceTabLauncher::GetInstance()->OnTabLaunched( |
| 26 request_id, | 26 request_id, |
| 27 content::WebContents::FromJavaWebContents(android_web_contents)); | 27 content::WebContents::FromJavaWebContents(android_web_contents)); |
| 28 } | 28 } |
| 29 | 29 |
| 30 namespace service_tab_launcher { | |
| 31 | |
| 32 // static | 30 // static |
| 33 ServiceTabLauncher* ServiceTabLauncher::GetInstance() { | 31 ServiceTabLauncher* ServiceTabLauncher::GetInstance() { |
| 34 return base::Singleton<ServiceTabLauncher>::get(); | 32 return base::Singleton<ServiceTabLauncher>::get(); |
| 35 } | 33 } |
| 36 | 34 |
| 37 ServiceTabLauncher::ServiceTabLauncher() { | 35 ServiceTabLauncher::ServiceTabLauncher() { |
| 38 java_object_.Reset( | |
| 39 Java_ServiceTabLauncher_getInstance(AttachCurrentThread(), | |
| 40 GetApplicationContext())); | |
| 41 } | 36 } |
| 42 | 37 |
| 43 ServiceTabLauncher::~ServiceTabLauncher() {} | 38 ServiceTabLauncher::~ServiceTabLauncher() {} |
| 44 | 39 |
| 45 void ServiceTabLauncher::LaunchTab( | 40 void ServiceTabLauncher::LaunchTab(content::BrowserContext* browser_context, |
| 46 content::BrowserContext* browser_context, | 41 const content::OpenURLParams& params, |
| 47 const content::OpenURLParams& params, | 42 const TabLaunchedCallback& callback) { |
| 48 const TabLaunchedCallback& callback) { | |
| 49 if (!java_object_.obj()) { | |
| 50 LOG(ERROR) << "No ServiceTabLauncher is available to launch a new tab."; | |
| 51 callback.Run(nullptr); | |
| 52 return; | |
| 53 } | |
| 54 | |
| 55 WindowOpenDisposition disposition = params.disposition; | 43 WindowOpenDisposition disposition = params.disposition; |
| 56 if (disposition != NEW_WINDOW && disposition != NEW_POPUP && | 44 if (disposition != NEW_WINDOW && disposition != NEW_POPUP && |
| 57 disposition != NEW_FOREGROUND_TAB && disposition != NEW_BACKGROUND_TAB) { | 45 disposition != NEW_FOREGROUND_TAB && disposition != NEW_BACKGROUND_TAB) { |
| 58 // ServiceTabLauncher can currently only launch new tabs. | 46 // ServiceTabLauncher can currently only launch new tabs. |
| 59 NOTIMPLEMENTED(); | 47 NOTIMPLEMENTED(); |
| 60 return; | 48 return; |
| 61 } | 49 } |
| 62 | 50 |
| 63 JNIEnv* env = AttachCurrentThread(); | 51 JNIEnv* env = AttachCurrentThread(); |
| 64 ScopedJavaLocalRef<jstring> url = ConvertUTF8ToJavaString( | 52 ScopedJavaLocalRef<jstring> url = ConvertUTF8ToJavaString( |
| 65 env, params.url.spec()); | 53 env, params.url.spec()); |
| 66 ScopedJavaLocalRef<jstring> referrer_url = | 54 ScopedJavaLocalRef<jstring> referrer_url = |
| 67 ConvertUTF8ToJavaString(env, params.referrer.url.spec()); | 55 ConvertUTF8ToJavaString(env, params.referrer.url.spec()); |
| 68 ScopedJavaLocalRef<jstring> headers = ConvertUTF8ToJavaString( | 56 ScopedJavaLocalRef<jstring> headers = ConvertUTF8ToJavaString( |
| 69 env, params.extra_headers); | 57 env, params.extra_headers); |
| 70 | 58 |
| 71 ScopedJavaLocalRef<jobject> post_data; | 59 ScopedJavaLocalRef<jobject> post_data; |
| 72 | 60 |
| 73 int request_id = tab_launched_callbacks_.Add( | 61 int request_id = tab_launched_callbacks_.Add( |
| 74 new TabLaunchedCallback(callback)); | 62 new TabLaunchedCallback(callback)); |
| 75 DCHECK_GE(request_id, 1); | 63 DCHECK_GE(request_id, 1); |
| 76 | 64 |
| 77 Java_ServiceTabLauncher_launchTab(env, | 65 Java_ServiceTabLauncher_launchTab(env, |
| 78 java_object_.obj(), | |
| 79 GetApplicationContext(), | 66 GetApplicationContext(), |
| 80 request_id, | 67 request_id, |
| 81 browser_context->IsOffTheRecord(), | 68 browser_context->IsOffTheRecord(), |
| 82 url.obj(), | 69 url.obj(), |
| 83 disposition, | 70 disposition, |
| 84 referrer_url.obj(), | 71 referrer_url.obj(), |
| 85 params.referrer.policy, | 72 params.referrer.policy, |
| 86 headers.obj(), | 73 headers.obj(), |
| 87 post_data.obj()); | 74 post_data.obj()); |
| 88 } | 75 } |
| 89 | 76 |
| 90 void ServiceTabLauncher::OnTabLaunched(int request_id, | 77 void ServiceTabLauncher::OnTabLaunched(int request_id, |
| 91 content::WebContents* web_contents) { | 78 content::WebContents* web_contents) { |
| 92 TabLaunchedCallback* callback = tab_launched_callbacks_.Lookup(request_id); | 79 TabLaunchedCallback* callback = tab_launched_callbacks_.Lookup(request_id); |
| 93 DCHECK(callback); | 80 DCHECK(callback); |
| 94 | 81 |
| 95 if (callback) | 82 if (callback) |
| 96 callback->Run(web_contents); | 83 callback->Run(web_contents); |
| 97 | 84 |
| 98 tab_launched_callbacks_.Remove(request_id); | 85 tab_launched_callbacks_.Remove(request_id); |
| 99 } | 86 } |
| 100 | 87 |
| 101 bool ServiceTabLauncher::RegisterServiceTabLauncher(JNIEnv* env) { | 88 bool ServiceTabLauncher::Register(JNIEnv* env) { |
| 102 return RegisterNativesImpl(env); | 89 return RegisterNativesImpl(env); |
| 103 } | 90 } |
| 104 | |
| 105 } // namespace service_tab_launcher | |
| OLD | NEW |