OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/shortcut_helper.h" | 5 #include "chrome/browser/android/shortcut_helper.h" |
6 | 6 |
7 #include <jni.h> | 7 #include <jni.h> |
| 8 #include <utility> |
8 | 9 |
9 #include "base/android/jni_android.h" | 10 #include "base/android/jni_android.h" |
10 #include "base/android/jni_array.h" | 11 #include "base/android/jni_array.h" |
11 #include "base/android/jni_string.h" | 12 #include "base/android/jni_string.h" |
12 #include "base/bind.h" | 13 #include "base/bind.h" |
13 #include "base/callback.h" | 14 #include "base/callback.h" |
14 #include "base/command_line.h" | 15 #include "base/command_line.h" |
15 #include "base/strings/string16.h" | 16 #include "base/strings/string16.h" |
16 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
17 #include "chrome/browser/android/webapk/chrome_webapk_host.h" | 18 #include "chrome/browser/android/webapk/chrome_webapk_host.h" |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 | 276 |
276 GURL ShortcutHelper::GetScopeFromURL(const GURL& url) { | 277 GURL ShortcutHelper::GetScopeFromURL(const GURL& url) { |
277 JNIEnv* env = base::android::AttachCurrentThread(); | 278 JNIEnv* env = base::android::AttachCurrentThread(); |
278 ScopedJavaLocalRef<jstring> java_url = | 279 ScopedJavaLocalRef<jstring> java_url = |
279 base::android::ConvertUTF8ToJavaString(env, url.spec()); | 280 base::android::ConvertUTF8ToJavaString(env, url.spec()); |
280 ScopedJavaLocalRef<jstring> java_scope_url = | 281 ScopedJavaLocalRef<jstring> java_scope_url = |
281 Java_ShortcutHelper_getScopeFromUrl(env, java_url); | 282 Java_ShortcutHelper_getScopeFromUrl(env, java_url); |
282 return GURL(base::android::ConvertJavaStringToUTF16(env, java_scope_url)); | 283 return GURL(base::android::ConvertJavaStringToUTF16(env, java_scope_url)); |
283 } | 284 } |
284 | 285 |
| 286 void ShortcutHelper::RetrieveWebApks(const WebApkInfoCallback& callback) { |
| 287 uintptr_t callback_pointer = |
| 288 reinterpret_cast<uintptr_t>(new WebApkInfoCallback(callback)); |
| 289 Java_ShortcutHelper_retrieveWebApks(base::android::AttachCurrentThread(), |
| 290 callback_pointer); |
| 291 } |
| 292 |
285 // Callback used by Java when the shortcut has been created. | 293 // Callback used by Java when the shortcut has been created. |
286 // |splash_image_callback| is a pointer to a base::Closure allocated in | 294 // |splash_image_callback| is a pointer to a base::Closure allocated in |
287 // AddShortcutWithSkBitmap, so reinterpret_cast it back and run it. | 295 // AddShortcutWithSkBitmap, so reinterpret_cast it back and run it. |
288 // | 296 // |
289 // This callback should only ever be called when the shortcut was for a | 297 // This callback should only ever be called when the shortcut was for a |
290 // webapp-capable site; otherwise, |splash_image_callback| will have never been | 298 // webapp-capable site; otherwise, |splash_image_callback| will have never been |
291 // allocated and doesn't need to be run or deleted. | 299 // allocated and doesn't need to be run or deleted. |
292 void OnWebappDataStored(JNIEnv* env, | 300 void OnWebappDataStored(JNIEnv* env, |
293 const JavaParamRef<jclass>& clazz, | 301 const JavaParamRef<jclass>& clazz, |
294 jlong jsplash_image_callback) { | 302 jlong jsplash_image_callback) { |
295 DCHECK(jsplash_image_callback); | 303 DCHECK(jsplash_image_callback); |
296 base::Closure* splash_image_callback = | 304 base::Closure* splash_image_callback = |
297 reinterpret_cast<base::Closure*>(jsplash_image_callback); | 305 reinterpret_cast<base::Closure*>(jsplash_image_callback); |
298 splash_image_callback->Run(); | 306 splash_image_callback->Run(); |
299 delete splash_image_callback; | 307 delete splash_image_callback; |
300 } | 308 } |
301 | 309 |
| 310 void OnWebApksRetrieved(JNIEnv* env, |
| 311 const JavaParamRef<jclass>& clazz, |
| 312 const jlong jcallback_pointer, |
| 313 const JavaParamRef<jobjectArray>& jshort_names, |
| 314 const JavaParamRef<jobjectArray>& jpackage_names, |
| 315 const JavaParamRef<jintArray>& jshell_apk_versions, |
| 316 const JavaParamRef<jintArray>& jversion_codes) { |
| 317 DCHECK(jcallback_pointer); |
| 318 std::vector<std::string> short_names; |
| 319 base::android::AppendJavaStringArrayToStringVector(env, jshort_names, |
| 320 &short_names); |
| 321 std::vector<std::string> package_names; |
| 322 base::android::AppendJavaStringArrayToStringVector(env, jpackage_names, |
| 323 &package_names); |
| 324 std::vector<int> shell_apk_versions; |
| 325 base::android::JavaIntArrayToIntVector(env, jshell_apk_versions, |
| 326 &shell_apk_versions); |
| 327 std::vector<int> version_codes; |
| 328 base::android::JavaIntArrayToIntVector(env, jversion_codes, &version_codes); |
| 329 |
| 330 DCHECK(short_names.size() == package_names.size()); |
| 331 DCHECK(short_names.size() == shell_apk_versions.size()); |
| 332 DCHECK(short_names.size() == version_codes.size()); |
| 333 |
| 334 std::vector<WebApkInfo> webapk_list; |
| 335 webapk_list.reserve(short_names.size()); |
| 336 for (size_t i = 0; i < short_names.size(); ++i) { |
| 337 webapk_list.push_back(WebApkInfo(std::move(short_names[i]), |
| 338 std::move(package_names[i]), |
| 339 shell_apk_versions[i], version_codes[i])); |
| 340 } |
| 341 |
| 342 ShortcutHelper::WebApkInfoCallback* webapk_list_callback = |
| 343 reinterpret_cast<ShortcutHelper::WebApkInfoCallback*>(jcallback_pointer); |
| 344 webapk_list_callback->Run(webapk_list); |
| 345 delete webapk_list_callback; |
| 346 } |
| 347 |
302 bool ShortcutHelper::RegisterShortcutHelper(JNIEnv* env) { | 348 bool ShortcutHelper::RegisterShortcutHelper(JNIEnv* env) { |
303 return RegisterNativesImpl(env); | 349 return RegisterNativesImpl(env); |
304 } | 350 } |
OLD | NEW |