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 "components/service_tab_launcher/browser/android/service_tab_launcher.h"
7 #include "base/android/jni_string.h"
8 #include "base/callback.h"
9 #include "content/public/browser/browser_context.h"
10 #include "content/public/browser/page_navigator.h"
11 #include "content/public/browser/web_contents.h"
12 #include "jni/ServiceTabLauncher_jni.h"
14 using base::android::AttachCurrentThread
;
15 using base::android::ConvertUTF8ToJavaString
;
16 using base::android::GetApplicationContext
;
18 // Called by Java when the WebContents instance for a request Id is available.
19 void OnWebContentsForRequestAvailable(
21 const JavaParamRef
<jclass
>& clazz
,
23 const JavaParamRef
<jobject
>& android_web_contents
) {
24 service_tab_launcher::ServiceTabLauncher::GetInstance()->OnTabLaunched(
26 content::WebContents::FromJavaWebContents(android_web_contents
));
29 namespace service_tab_launcher
{
32 ServiceTabLauncher
* ServiceTabLauncher::GetInstance() {
33 return base::Singleton
<ServiceTabLauncher
>::get();
36 ServiceTabLauncher::ServiceTabLauncher() {
38 Java_ServiceTabLauncher_getInstance(AttachCurrentThread(),
39 GetApplicationContext()));
42 ServiceTabLauncher::~ServiceTabLauncher() {}
44 void ServiceTabLauncher::LaunchTab(
45 content::BrowserContext
* browser_context
,
46 const content::OpenURLParams
& params
,
47 const TabLaunchedCallback
& callback
) {
48 if (!java_object_
.obj()) {
49 LOG(ERROR
) << "No ServiceTabLauncher is available to launch a new tab.";
50 callback
.Run(nullptr);
54 WindowOpenDisposition disposition
= params
.disposition
;
55 if (disposition
!= NEW_WINDOW
&& disposition
!= NEW_POPUP
&&
56 disposition
!= NEW_FOREGROUND_TAB
&& disposition
!= NEW_BACKGROUND_TAB
) {
57 // ServiceTabLauncher can currently only launch new tabs.
62 JNIEnv
* env
= AttachCurrentThread();
63 ScopedJavaLocalRef
<jstring
> url
= ConvertUTF8ToJavaString(
64 env
, params
.url
.spec());
65 ScopedJavaLocalRef
<jstring
> referrer_url
=
66 ConvertUTF8ToJavaString(env
, params
.referrer
.url
.spec());
67 ScopedJavaLocalRef
<jstring
> headers
= ConvertUTF8ToJavaString(
68 env
, params
.extra_headers
);
70 ScopedJavaLocalRef
<jbyteArray
> post_data
;
72 int request_id
= tab_launched_callbacks_
.Add(
73 new TabLaunchedCallback(callback
));
74 DCHECK_GE(request_id
, 1);
76 Java_ServiceTabLauncher_launchTab(env
,
78 GetApplicationContext(),
80 browser_context
->IsOffTheRecord(),
84 params
.referrer
.policy
,
89 void ServiceTabLauncher::OnTabLaunched(int request_id
,
90 content::WebContents
* web_contents
) {
91 TabLaunchedCallback
* callback
= tab_launched_callbacks_
.Lookup(request_id
);
95 callback
->Run(web_contents
);
97 tab_launched_callbacks_
.Remove(request_id
);
100 bool ServiceTabLauncher::RegisterServiceTabLauncher(JNIEnv
* env
) {
101 return RegisterNativesImpl(env
);
104 } // namespace service_tab_launcher