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