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