Chromium Code Reviews| 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 "chrome/browser/android/service_tab_launcher.h" | 5 #include "chrome/browser/android/service_tab_launcher.h" |
| 6 | 6 |
| 7 #include "base/android/jni_string.h" | 7 #include "base/android/jni_string.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "content/public/browser/browser_context.h" | 9 #include "content/public/browser/browser_context.h" |
| 10 #include "content/public/browser/page_navigator.h" | 10 #include "content/public/browser/page_navigator.h" |
| 11 #include "content/public/browser/web_contents.h" | |
| 11 #include "jni/ServiceTabLauncher_jni.h" | 12 #include "jni/ServiceTabLauncher_jni.h" |
| 12 | 13 |
| 13 using base::android::AttachCurrentThread; | 14 using base::android::AttachCurrentThread; |
| 14 using base::android::ConvertUTF8ToJavaString; | 15 using base::android::ConvertUTF8ToJavaString; |
| 15 using base::android::GetApplicationContext; | 16 using base::android::GetApplicationContext; |
| 16 | 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 | |
| 17 namespace chrome { | 26 namespace chrome { |
| 18 namespace android { | 27 namespace android { |
| 19 | 28 |
| 20 // static | 29 // static |
| 21 ServiceTabLauncher* ServiceTabLauncher::GetInstance() { | 30 ServiceTabLauncher* ServiceTabLauncher::GetInstance() { |
| 22 return Singleton<ServiceTabLauncher>::get(); | 31 return Singleton<ServiceTabLauncher>::get(); |
| 23 } | 32 } |
| 24 | 33 |
| 25 ServiceTabLauncher::ServiceTabLauncher() { | 34 ServiceTabLauncher::ServiceTabLauncher() { |
| 26 java_object_.Reset( | 35 java_object_.Reset( |
| 27 Java_ServiceTabLauncher_getInstance(AttachCurrentThread(), | 36 Java_ServiceTabLauncher_getInstance(AttachCurrentThread(), |
| 28 GetApplicationContext())); | 37 GetApplicationContext())); |
| 29 } | 38 } |
| 30 | 39 |
| 31 ServiceTabLauncher::~ServiceTabLauncher() {} | 40 ServiceTabLauncher::~ServiceTabLauncher() {} |
| 32 | 41 |
| 33 void ServiceTabLauncher::LaunchTab( | 42 void ServiceTabLauncher::LaunchTab( |
| 34 content::BrowserContext* browser_context, | 43 content::BrowserContext* browser_context, |
| 35 const content::OpenURLParams& params, | 44 const content::OpenURLParams& params, |
| 36 const base::Callback<void(content::WebContents*)>& callback) { | 45 const TabLaunchedCallback& callback) { |
| 37 if (!java_object_.obj()) { | 46 if (!java_object_.obj()) { |
| 38 LOG(ERROR) << "No ServiceTabLauncher is available to launch a new tab."; | 47 LOG(ERROR) << "No ServiceTabLauncher is available to launch a new tab."; |
| 39 callback.Run(nullptr); | 48 callback.Run(nullptr); |
| 40 return; | 49 return; |
| 41 } | 50 } |
| 42 | 51 |
| 43 WindowOpenDisposition disposition = params.disposition; | 52 WindowOpenDisposition disposition = params.disposition; |
| 44 if (disposition != NEW_WINDOW && disposition != NEW_POPUP && | 53 if (disposition != NEW_WINDOW && disposition != NEW_POPUP && |
| 45 disposition != NEW_FOREGROUND_TAB && disposition != NEW_BACKGROUND_TAB) { | 54 disposition != NEW_FOREGROUND_TAB && disposition != NEW_BACKGROUND_TAB) { |
| 46 // ServiceTabLauncher can currently only launch new tabs. | 55 // ServiceTabLauncher can currently only launch new tabs. |
| 47 NOTIMPLEMENTED(); | 56 NOTIMPLEMENTED(); |
| 48 return; | 57 return; |
| 49 } | 58 } |
| 50 | 59 |
| 51 JNIEnv* env = AttachCurrentThread(); | 60 JNIEnv* env = AttachCurrentThread(); |
| 52 ScopedJavaLocalRef<jstring> url = ConvertUTF8ToJavaString( | 61 ScopedJavaLocalRef<jstring> url = ConvertUTF8ToJavaString( |
| 53 env, params.url.spec()); | 62 env, params.url.spec()); |
| 54 ScopedJavaLocalRef<jstring> referrer_url = | 63 ScopedJavaLocalRef<jstring> referrer_url = |
| 55 ConvertUTF8ToJavaString(env, params.referrer.url.spec()); | 64 ConvertUTF8ToJavaString(env, params.referrer.url.spec()); |
| 56 ScopedJavaLocalRef<jstring> headers = ConvertUTF8ToJavaString( | 65 ScopedJavaLocalRef<jstring> headers = ConvertUTF8ToJavaString( |
| 57 env, params.extra_headers); | 66 env, params.extra_headers); |
| 58 | 67 |
| 59 ScopedJavaLocalRef<jbyteArray> post_data; | 68 ScopedJavaLocalRef<jbyteArray> post_data; |
| 60 | 69 |
| 70 int request_id = tab_launched_callbacks_.Add( | |
| 71 new TabLaunchedCallback(callback)); | |
| 72 DCHECK_GE(request_id, 1); | |
| 73 | |
| 61 Java_ServiceTabLauncher_launchTab(env, | 74 Java_ServiceTabLauncher_launchTab(env, |
| 62 java_object_.obj(), | 75 java_object_.obj(), |
| 63 GetApplicationContext(), | 76 GetApplicationContext(), |
| 64 0 /* request_id */, | 77 request_id, |
| 65 browser_context->IsOffTheRecord(), | 78 browser_context->IsOffTheRecord(), |
| 66 url.obj(), | 79 url.obj(), |
| 67 disposition, | 80 disposition, |
| 68 referrer_url.obj(), | 81 referrer_url.obj(), |
| 69 params.referrer.policy, | 82 params.referrer.policy, |
| 70 headers.obj(), | 83 headers.obj(), |
| 71 post_data.obj()); | 84 post_data.obj()); |
| 85 } | |
| 72 | 86 |
| 73 // TODO(peter): We need to wait for the Android Activity to reply to the | 87 void ServiceTabLauncher::OnTabLaunched(int request_id, |
| 74 // launch intent with the ID of the launched Web Contents, so that the Java | 88 content::WebContents* web_contents) { |
| 75 // side can invoke a method on the native side with the request id and the | 89 TabLaunchedCallback* callback = tab_launched_callbacks_.Lookup(request_id); |
|
Ted C
2015/02/19 21:55:22
the one gotcha here is that if the intent is ever
Peter Beverloo
2015/02/20 12:09:18
That's a really good point actually, thank you.
| |
| 76 // WebContents enabling us to invoke |callback|. See https://crbug.com/454809. | 90 DCHECK(callback); |
| 91 | |
| 92 if (callback) | |
| 93 callback->Run(web_contents); | |
| 94 | |
| 95 tab_launched_callbacks_.Remove(request_id); | |
| 77 } | 96 } |
| 78 | 97 |
| 79 bool ServiceTabLauncher::RegisterServiceTabLauncher(JNIEnv* env) { | 98 bool ServiceTabLauncher::RegisterServiceTabLauncher(JNIEnv* env) { |
| 80 return RegisterNativesImpl(env); | 99 return RegisterNativesImpl(env); |
| 81 } | 100 } |
| 82 | 101 |
| 83 } // namespace android | 102 } // namespace android |
| 84 } // namespace chrome | 103 } // namespace chrome |
| OLD | NEW |