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 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 | 282 |
282 GURL ShortcutHelper::GetScopeFromURL(const GURL& url) { | 283 GURL ShortcutHelper::GetScopeFromURL(const GURL& url) { |
283 JNIEnv* env = base::android::AttachCurrentThread(); | 284 JNIEnv* env = base::android::AttachCurrentThread(); |
284 ScopedJavaLocalRef<jstring> java_url = | 285 ScopedJavaLocalRef<jstring> java_url = |
285 base::android::ConvertUTF8ToJavaString(env, url.spec()); | 286 base::android::ConvertUTF8ToJavaString(env, url.spec()); |
286 ScopedJavaLocalRef<jstring> java_scope_url = | 287 ScopedJavaLocalRef<jstring> java_scope_url = |
287 Java_ShortcutHelper_getScopeFromUrl(env, java_url); | 288 Java_ShortcutHelper_getScopeFromUrl(env, java_url); |
288 return GURL(base::android::ConvertJavaStringToUTF16(env, java_scope_url)); | 289 return GURL(base::android::ConvertJavaStringToUTF16(env, java_scope_url)); |
289 } | 290 } |
290 | 291 |
| 292 void ShortcutHelper::RetrieveWebApks(const WebApkInfoCallback& callback) { |
| 293 uintptr_t callback_pointer = |
| 294 reinterpret_cast<uintptr_t>(new WebApkInfoCallback(callback)); |
| 295 Java_ShortcutHelper_retrieveWebApks(base::android::AttachCurrentThread(), |
| 296 callback_pointer); |
| 297 } |
| 298 |
291 // Callback used by Java when the shortcut has been created. | 299 // Callback used by Java when the shortcut has been created. |
292 // |splash_image_callback| is a pointer to a base::Closure allocated in | 300 // |splash_image_callback| is a pointer to a base::Closure allocated in |
293 // AddShortcutWithSkBitmap, so reinterpret_cast it back and run it. | 301 // AddShortcutWithSkBitmap, so reinterpret_cast it back and run it. |
294 // | 302 // |
295 // This callback should only ever be called when the shortcut was for a | 303 // This callback should only ever be called when the shortcut was for a |
296 // webapp-capable site; otherwise, |splash_image_callback| will have never been | 304 // webapp-capable site; otherwise, |splash_image_callback| will have never been |
297 // allocated and doesn't need to be run or deleted. | 305 // allocated and doesn't need to be run or deleted. |
298 void OnWebappDataStored(JNIEnv* env, | 306 void OnWebappDataStored(JNIEnv* env, |
299 const JavaParamRef<jclass>& clazz, | 307 const JavaParamRef<jclass>& clazz, |
300 jlong jsplash_image_callback) { | 308 jlong jsplash_image_callback) { |
301 DCHECK(jsplash_image_callback); | 309 DCHECK(jsplash_image_callback); |
302 base::Closure* splash_image_callback = | 310 base::Closure* splash_image_callback = |
303 reinterpret_cast<base::Closure*>(jsplash_image_callback); | 311 reinterpret_cast<base::Closure*>(jsplash_image_callback); |
304 splash_image_callback->Run(); | 312 splash_image_callback->Run(); |
305 delete splash_image_callback; | 313 delete splash_image_callback; |
306 } | 314 } |
307 | 315 |
| 316 void OnWebApksRetrieved(JNIEnv* env, |
| 317 const JavaParamRef<jclass>& clazz, |
| 318 const jlong jcallback_pointer, |
| 319 const JavaParamRef<jobjectArray>& jshort_names, |
| 320 const JavaParamRef<jobjectArray>& jpackage_names, |
| 321 const JavaParamRef<jintArray>& jshell_apk_versions, |
| 322 const JavaParamRef<jintArray>& jversion_codes) { |
| 323 DCHECK(jcallback_pointer); |
| 324 std::vector<std::string> short_names; |
| 325 base::android::AppendJavaStringArrayToStringVector(env, jshort_names, |
| 326 &short_names); |
| 327 std::vector<std::string> package_names; |
| 328 base::android::AppendJavaStringArrayToStringVector(env, jpackage_names, |
| 329 &package_names); |
| 330 std::vector<int> shell_apk_versions; |
| 331 base::android::JavaIntArrayToIntVector(env, jshell_apk_versions, |
| 332 &shell_apk_versions); |
| 333 std::vector<int> version_codes; |
| 334 base::android::JavaIntArrayToIntVector(env, jversion_codes, &version_codes); |
| 335 |
| 336 DCHECK(short_names.size() == package_names.size()); |
| 337 DCHECK(short_names.size() == shell_apk_versions.size()); |
| 338 DCHECK(short_names.size() == version_codes.size()); |
| 339 |
| 340 std::vector<WebApkInfo> webapk_list; |
| 341 webapk_list.reserve(short_names.size()); |
| 342 for (size_t i = 0; i < short_names.size(); ++i) { |
| 343 webapk_list.push_back(WebApkInfo(std::move(short_names[i]), |
| 344 std::move(package_names[i]), |
| 345 shell_apk_versions[i], version_codes[i])); |
| 346 } |
| 347 |
| 348 ShortcutHelper::WebApkInfoCallback* webapk_list_callback = |
| 349 reinterpret_cast<ShortcutHelper::WebApkInfoCallback*>(jcallback_pointer); |
| 350 webapk_list_callback->Run(webapk_list); |
| 351 delete webapk_list_callback; |
| 352 } |
| 353 |
308 bool ShortcutHelper::RegisterShortcutHelper(JNIEnv* env) { | 354 bool ShortcutHelper::RegisterShortcutHelper(JNIEnv* env) { |
309 return RegisterNativesImpl(env); | 355 return RegisterNativesImpl(env); |
310 } | 356 } |
OLD | NEW |