components: Fix 'gn check' errors for web_cache and web_modal.
[chromium-blink-merge.git] / components / service_tab_launcher / browser / android / service_tab_launcher.cc
blob7a1bc0f9ada5c328726476faf704b2d3846b48e8
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(
20 JNIEnv* env, jclass clazz, jint request_id, jobject android_web_contents) {
21 service_tab_launcher::ServiceTabLauncher::GetInstance()->OnTabLaunched(
22 request_id,
23 content::WebContents::FromJavaWebContents(android_web_contents));
26 namespace service_tab_launcher {
28 // static
29 ServiceTabLauncher* ServiceTabLauncher::GetInstance() {
30 return Singleton<ServiceTabLauncher>::get();
33 ServiceTabLauncher::ServiceTabLauncher() {
34 java_object_.Reset(
35 Java_ServiceTabLauncher_getInstance(AttachCurrentThread(),
36 GetApplicationContext()));
39 ServiceTabLauncher::~ServiceTabLauncher() {}
41 void ServiceTabLauncher::LaunchTab(
42 content::BrowserContext* browser_context,
43 const content::OpenURLParams& params,
44 const TabLaunchedCallback& callback) {
45 if (!java_object_.obj()) {
46 LOG(ERROR) << "No ServiceTabLauncher is available to launch a new tab.";
47 callback.Run(nullptr);
48 return;
51 WindowOpenDisposition disposition = params.disposition;
52 if (disposition != NEW_WINDOW && disposition != NEW_POPUP &&
53 disposition != NEW_FOREGROUND_TAB && disposition != NEW_BACKGROUND_TAB) {
54 // ServiceTabLauncher can currently only launch new tabs.
55 NOTIMPLEMENTED();
56 return;
59 JNIEnv* env = AttachCurrentThread();
60 ScopedJavaLocalRef<jstring> url = ConvertUTF8ToJavaString(
61 env, params.url.spec());
62 ScopedJavaLocalRef<jstring> referrer_url =
63 ConvertUTF8ToJavaString(env, params.referrer.url.spec());
64 ScopedJavaLocalRef<jstring> headers = ConvertUTF8ToJavaString(
65 env, params.extra_headers);
67 ScopedJavaLocalRef<jbyteArray> post_data;
69 int request_id = tab_launched_callbacks_.Add(
70 new TabLaunchedCallback(callback));
71 DCHECK_GE(request_id, 1);
73 Java_ServiceTabLauncher_launchTab(env,
74 java_object_.obj(),
75 GetApplicationContext(),
76 request_id,
77 browser_context->IsOffTheRecord(),
78 url.obj(),
79 disposition,
80 referrer_url.obj(),
81 params.referrer.policy,
82 headers.obj(),
83 post_data.obj());
86 void ServiceTabLauncher::OnTabLaunched(int request_id,
87 content::WebContents* web_contents) {
88 TabLaunchedCallback* callback = tab_launched_callbacks_.Lookup(request_id);
89 DCHECK(callback);
91 if (callback)
92 callback->Run(web_contents);
94 tab_launched_callbacks_.Remove(request_id);
97 bool ServiceTabLauncher::RegisterServiceTabLauncher(JNIEnv* env) {
98 return RegisterNativesImpl(env);
101 } // namespace service_tab_launcher