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/page_navigator.h" | 9 #include "content/public/browser/page_navigator.h" |
10 #include "content/public/browser/web_contents.h" | |
10 #include "jni/ServiceTabLauncher_jni.h" | 11 #include "jni/ServiceTabLauncher_jni.h" |
11 | 12 |
12 using base::android::AttachCurrentThread; | 13 using base::android::AttachCurrentThread; |
13 using base::android::ConvertUTF8ToJavaString; | 14 using base::android::ConvertUTF8ToJavaString; |
14 using base::android::GetApplicationContext; | 15 using base::android::GetApplicationContext; |
15 | 16 |
17 // Called by Java when the WebContents instance for a request Id is available. | |
18 void OnWebContentsForRequestAvailable( | |
19 JNIEnv* env, jclass clazz, jint request_id, jobject android_web_contents) { | |
20 chrome::android::ServiceTabLauncher::GetInstance()->OnTabLaunched( | |
21 request_id, | |
22 content::WebContents::FromJavaWebContents(android_web_contents)); | |
23 } | |
24 | |
16 namespace chrome { | 25 namespace chrome { |
17 namespace android { | 26 namespace android { |
18 | 27 |
19 // static | 28 // static |
20 ServiceTabLauncher* ServiceTabLauncher::GetInstance() { | 29 ServiceTabLauncher* ServiceTabLauncher::GetInstance() { |
21 return Singleton<ServiceTabLauncher>::get(); | 30 return Singleton<ServiceTabLauncher>::get(); |
22 } | 31 } |
23 | 32 |
24 ServiceTabLauncher::ServiceTabLauncher() { | 33 ServiceTabLauncher::ServiceTabLauncher() |
34 : last_request_id_(0) { | |
25 java_object_.Reset( | 35 java_object_.Reset( |
26 Java_ServiceTabLauncher_create( | 36 Java_ServiceTabLauncher_create( |
27 AttachCurrentThread(), | 37 AttachCurrentThread(), |
28 reinterpret_cast<intptr_t>(this), | 38 reinterpret_cast<intptr_t>(this), |
29 GetApplicationContext())); | 39 GetApplicationContext())); |
30 } | 40 } |
31 | 41 |
32 ServiceTabLauncher::~ServiceTabLauncher() {} | 42 ServiceTabLauncher::~ServiceTabLauncher() {} |
33 | 43 |
34 void ServiceTabLauncher::LaunchTab( | 44 void ServiceTabLauncher::LaunchTab( |
35 content::BrowserContext* browser_context, | 45 content::BrowserContext* browser_context, |
Bernhard Bauer
2015/02/04 18:09:12
You don't really seem to need the browser context.
Peter Beverloo
2015/02/19 16:03:06
Rebased. We do now per a previous patch, to check
| |
36 const content::OpenURLParams& params, | 46 const content::OpenURLParams& params, |
37 base::Callback<void(content::WebContents*)> callback) { | 47 base::Callback<void(content::WebContents*)> callback) { |
38 JNIEnv* env = AttachCurrentThread(); | 48 JNIEnv* env = AttachCurrentThread(); |
39 ScopedJavaLocalRef<jstring> url = ConvertUTF8ToJavaString( | 49 ScopedJavaLocalRef<jstring> url = ConvertUTF8ToJavaString( |
40 env, params.url.spec()); | 50 env, params.url.spec()); |
41 | 51 |
42 Java_ServiceTabLauncher_launchTab(env, java_object_.obj(), url.obj()); | 52 tab_launched_callbacks_[++last_request_id_] = callback; |
53 Java_ServiceTabLauncher_launchTab(env, | |
54 java_object_.obj(), | |
55 last_request_id_, | |
56 url.obj()); | |
57 } | |
43 | 58 |
44 // TODO(peter): We need to wait for the Android Activity to reply to the | 59 void ServiceTabLauncher::OnTabLaunched(int request_id, |
45 // launch intent with the ID of the launched Web Contents, so that the Java | 60 content::WebContents* web_contents) { |
Ted C
2015/02/06 01:18:18
+1 indent
Peter Beverloo
2015/02/19 16:03:06
Done.
| |
46 // side can invoke a method on the native side with the request id and the | 61 DCHECK_EQ(tab_launched_callbacks_.count(request_id), 1u); |
47 // WebContents enabling us to invoke |callback|. See https://crbug.com/454809. | 62 DCHECK(web_contents); |
63 | |
64 tab_launched_callbacks_[request_id].Run(web_contents); | |
65 tab_launched_callbacks_.erase(request_id); | |
48 } | 66 } |
49 | 67 |
50 bool ServiceTabLauncher::RegisterServiceTabLauncher(JNIEnv* env) { | 68 bool ServiceTabLauncher::RegisterServiceTabLauncher(JNIEnv* env) { |
51 return RegisterNativesImpl(env); | 69 return RegisterNativesImpl(env); |
52 } | 70 } |
53 | 71 |
54 } // namespace android | 72 } // namespace android |
55 } // namespace chrome | 73 } // namespace chrome |
OLD | NEW |