[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/android/shortcut_helper.h" |
| 6 | |
| 7 | #include <jni.h> |
| 8 | |
| 9 | #include "base/android/jni_android.h" |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 10 | #include "base/android/jni_array.h" |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 11 | #include "base/android/jni_string.h" |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 12 | #include "base/bind.h" |
| 13 | #include "base/callback.h" |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 14 | #include "base/command_line.h" |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 15 | #include "base/strings/string16.h" |
mlamouri | c679bbf | 2014-09-24 21:24:49 | [diff] [blame] | 16 | #include "base/strings/utf_string_conversions.h" |
hanxi | 563f353 | 2016-08-19 20:09:01 | [diff] [blame] | 17 | #include "chrome/browser/android/webapk/chrome_webapk_host.h" |
hanxi | 070d10a | 2017-01-09 15:56:50 | [diff] [blame] | 18 | #include "chrome/browser/android/webapk/webapk_install_service.h" |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 19 | #include "chrome/browser/manifest/manifest_icon_downloader.h" |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 20 | #include "chrome/common/chrome_switches.h" |
dfalcantara | aec56da | 2015-05-06 03:33:56 | [diff] [blame] | 21 | #include "content/public/browser/browser_thread.h" |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 22 | #include "content/public/browser/web_contents.h" |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 23 | #include "jni/ShortcutHelper_jni.h" |
| 24 | #include "ui/gfx/android/java_bitmap.h" |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 25 | #include "ui/gfx/color_analysis.h" |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 26 | #include "url/gurl.h" |
| 27 | |
torne | 8656011 | 2016-08-04 15:59:04 | [diff] [blame] | 28 | using base::android::JavaParamRef; |
| 29 | using base::android::ScopedJavaLocalRef; |
mlamouri | c679bbf | 2014-09-24 21:24:49 | [diff] [blame] | 30 | using content::Manifest; |
| 31 | |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 32 | namespace { |
| 33 | |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame^] | 34 | int g_ideal_homescreen_icon_size = -1; |
| 35 | int g_minimum_homescreen_icon_size = -1; |
| 36 | int g_ideal_splash_image_size = -1; |
| 37 | int g_minimum_splash_image_size = -1; |
| 38 | int g_ideal_badge_icon_size = -1; |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 39 | |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame^] | 40 | int g_default_rgb_icon_value = 145; |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 41 | |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 42 | // Retrieves and caches the ideal and minimum sizes of the Home screen icon |
| 43 | // and the splash screen image. |
| 44 | void GetHomescreenIconAndSplashImageSizes() { |
| 45 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 46 | ScopedJavaLocalRef<jintArray> java_size_array = |
pkotwicz | 45fc42b6 | 2016-06-07 00:07:10 | [diff] [blame] | 47 | Java_ShortcutHelper_getHomeScreenIconAndSplashImageSizes(env); |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 48 | std::vector<int> sizes; |
| 49 | base::android::JavaIntArrayToIntVector( |
| 50 | env, java_size_array.obj(), &sizes); |
| 51 | |
| 52 | // Check that the size returned is what is expected. |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame^] | 53 | DCHECK(sizes.size() == 5); |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 54 | |
| 55 | // This ordering must be kept up to date with the Java ShortcutHelper. |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame^] | 56 | g_ideal_homescreen_icon_size = sizes[0]; |
| 57 | g_minimum_homescreen_icon_size = sizes[1]; |
| 58 | g_ideal_splash_image_size = sizes[2]; |
| 59 | g_minimum_splash_image_size = sizes[3]; |
| 60 | g_ideal_badge_icon_size = sizes[4]; |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 61 | |
| 62 | // Try to ensure that the data returned is sane. |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame^] | 63 | DCHECK(g_minimum_homescreen_icon_size <= g_ideal_homescreen_icon_size); |
| 64 | DCHECK(g_minimum_splash_image_size <= g_ideal_splash_image_size); |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | } // anonymous namespace |
| 68 | |
dfalcantara | aec56da | 2015-05-06 03:33:56 | [diff] [blame] | 69 | // static |
pkotwicz | c67e6ac | 2016-08-12 19:56:44 | [diff] [blame] | 70 | void ShortcutHelper::AddToLauncherWithSkBitmap( |
pkotwicz | 879b1ed | 2016-08-06 00:48:22 | [diff] [blame] | 71 | content::BrowserContext* browser_context, |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 72 | const ShortcutInfo& info, |
| 73 | const std::string& webapp_id, |
| 74 | const SkBitmap& icon_bitmap, |
| 75 | const base::Closure& splash_image_callback) { |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 76 | if (info.display == blink::WebDisplayModeStandalone || |
| 77 | info.display == blink::WebDisplayModeFullscreen) { |
pkotwicz | c67e6ac | 2016-08-12 19:56:44 | [diff] [blame] | 78 | AddWebappWithSkBitmap(info, webapp_id, icon_bitmap, splash_image_callback); |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 79 | return; |
| 80 | } |
pkotwicz | c67e6ac | 2016-08-12 19:56:44 | [diff] [blame] | 81 | AddShortcutWithSkBitmap(info, icon_bitmap); |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // static |
pkotwicz | c67e6ac | 2016-08-12 19:56:44 | [diff] [blame] | 85 | void ShortcutHelper::InstallWebApkWithSkBitmap( |
pkotwicz | 879b1ed | 2016-08-06 00:48:22 | [diff] [blame] | 86 | content::BrowserContext* browser_context, |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 87 | const ShortcutInfo& info, |
hanxi | 9f38f74 | 2016-08-30 18:34:20 | [diff] [blame] | 88 | const SkBitmap& icon_bitmap, |
| 89 | const WebApkInstaller::FinishCallback& callback) { |
hanxi | 070d10a | 2017-01-09 15:56:50 | [diff] [blame] | 90 | WebApkInstallService::Get(browser_context) |
| 91 | ->InstallAsync(info, icon_bitmap, callback); |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | // static |
pkotwicz | c67e6ac | 2016-08-12 19:56:44 | [diff] [blame] | 95 | void ShortcutHelper::AddWebappWithSkBitmap( |
dfalcantara | 16e84de | 2015-02-03 22:07:40 | [diff] [blame] | 96 | const ShortcutInfo& info, |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 97 | const std::string& webapp_id, |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 98 | const SkBitmap& icon_bitmap, |
| 99 | const base::Closure& splash_image_callback) { |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 100 | // Send the data to the Java side to create the shortcut. |
| 101 | JNIEnv* env = base::android::AttachCurrentThread(); |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 102 | ScopedJavaLocalRef<jstring> java_webapp_id = |
| 103 | base::android::ConvertUTF8ToJavaString(env, webapp_id); |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 104 | ScopedJavaLocalRef<jstring> java_url = |
dfalcantara | 16e84de | 2015-02-03 22:07:40 | [diff] [blame] | 105 | base::android::ConvertUTF8ToJavaString(env, info.url.spec()); |
pkotwicz | 6bdfbe1b | 2016-07-08 00:26:43 | [diff] [blame] | 106 | ScopedJavaLocalRef<jstring> java_scope_url = |
| 107 | base::android::ConvertUTF8ToJavaString(env, info.scope.spec()); |
lalitm | f3ee5185 | 2015-07-21 18:13:11 | [diff] [blame] | 108 | ScopedJavaLocalRef<jstring> java_user_title = |
| 109 | base::android::ConvertUTF16ToJavaString(env, info.user_title); |
| 110 | ScopedJavaLocalRef<jstring> java_name = |
| 111 | base::android::ConvertUTF16ToJavaString(env, info.name); |
| 112 | ScopedJavaLocalRef<jstring> java_short_name = |
| 113 | base::android::ConvertUTF16ToJavaString(env, info.short_name); |
hanxi | 75ca9beb | 2016-11-18 16:22:59 | [diff] [blame] | 114 | ScopedJavaLocalRef<jstring> java_best_icon_url = |
| 115 | base::android::ConvertUTF8ToJavaString(env, info.best_icon_url.spec()); |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 116 | ScopedJavaLocalRef<jobject> java_bitmap; |
mlamouri | c679bbf | 2014-09-24 21:24:49 | [diff] [blame] | 117 | if (icon_bitmap.getSize()) |
| 118 | java_bitmap = gfx::ConvertToJavaBitmap(&icon_bitmap); |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 119 | |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 120 | // The callback will need to be run after shortcut creation completes in order |
| 121 | // to download the splash image and save it to the WebappDataStorage. Create a |
| 122 | // copy of the callback here and send the pointer to Java, which will send it |
| 123 | // back once the asynchronous shortcut creation process finishes. |
| 124 | uintptr_t callback_pointer = |
| 125 | reinterpret_cast<uintptr_t>(new base::Closure(splash_image_callback)); |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 126 | |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame] | 127 | Java_ShortcutHelper_addWebapp(env, java_webapp_id, java_url, java_scope_url, |
| 128 | java_user_title, java_name, java_short_name, |
hanxi | 75ca9beb | 2016-11-18 16:22:59 | [diff] [blame] | 129 | java_best_icon_url, java_bitmap, info.display, |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame] | 130 | info.orientation, info.source, info.theme_color, |
| 131 | info.background_color, callback_pointer); |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 132 | } |
benwells | 840ae90 | 2015-02-17 21:13:28 | [diff] [blame] | 133 | |
pkotwicz | c67e6ac | 2016-08-12 19:56:44 | [diff] [blame] | 134 | void ShortcutHelper::AddShortcutWithSkBitmap( |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 135 | const ShortcutInfo& info, |
| 136 | const SkBitmap& icon_bitmap) { |
| 137 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 138 | ScopedJavaLocalRef<jstring> java_url = |
| 139 | base::android::ConvertUTF8ToJavaString(env, info.url.spec()); |
| 140 | ScopedJavaLocalRef<jstring> java_user_title = |
| 141 | base::android::ConvertUTF16ToJavaString(env, info.user_title); |
| 142 | ScopedJavaLocalRef<jobject> java_bitmap; |
| 143 | if (icon_bitmap.getSize()) |
| 144 | java_bitmap = gfx::ConvertToJavaBitmap(&icon_bitmap); |
| 145 | |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame] | 146 | Java_ShortcutHelper_addShortcut(env, java_url, java_user_title, java_bitmap, |
| 147 | info.source); |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 148 | } |
| 149 | |
hanxi | 070d10a | 2017-01-09 15:56:50 | [diff] [blame] | 150 | void ShortcutHelper::ShowWebApkInstallInProgressToast() { |
| 151 | Java_ShortcutHelper_showWebApkInstallInProgressToast( |
| 152 | base::android::AttachCurrentThread()); |
| 153 | } |
| 154 | |
zpeng | 5d8fdfc | 2017-01-05 15:45:06 | [diff] [blame] | 155 | int ShortcutHelper::GetIdealHomescreenIconSizeInPx() { |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame^] | 156 | if (g_ideal_homescreen_icon_size == -1) |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 157 | GetHomescreenIconAndSplashImageSizes(); |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame^] | 158 | return g_ideal_homescreen_icon_size; |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 159 | } |
| 160 | |
zpeng | 5d8fdfc | 2017-01-05 15:45:06 | [diff] [blame] | 161 | int ShortcutHelper::GetMinimumHomescreenIconSizeInPx() { |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame^] | 162 | if (g_minimum_homescreen_icon_size == -1) |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 163 | GetHomescreenIconAndSplashImageSizes(); |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame^] | 164 | return g_minimum_homescreen_icon_size; |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 165 | } |
| 166 | |
zpeng | 5d8fdfc | 2017-01-05 15:45:06 | [diff] [blame] | 167 | int ShortcutHelper::GetIdealSplashImageSizeInPx() { |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame^] | 168 | if (g_ideal_splash_image_size == -1) |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 169 | GetHomescreenIconAndSplashImageSizes(); |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame^] | 170 | return g_ideal_splash_image_size; |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 171 | } |
| 172 | |
zpeng | 5d8fdfc | 2017-01-05 15:45:06 | [diff] [blame] | 173 | int ShortcutHelper::GetMinimumSplashImageSizeInPx() { |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame^] | 174 | if (g_minimum_splash_image_size == -1) |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 175 | GetHomescreenIconAndSplashImageSizes(); |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame^] | 176 | return g_minimum_splash_image_size; |
| 177 | } |
| 178 | |
| 179 | int ShortcutHelper::GetIdealBadgeIconSizeInPx() { |
| 180 | if (g_ideal_badge_icon_size == -1) |
| 181 | GetHomescreenIconAndSplashImageSizes(); |
| 182 | return g_ideal_badge_icon_size; |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 183 | } |
| 184 | |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 185 | // static |
| 186 | void ShortcutHelper::FetchSplashScreenImage( |
| 187 | content::WebContents* web_contents, |
| 188 | const GURL& image_url, |
zpeng | 5d8fdfc | 2017-01-05 15:45:06 | [diff] [blame] | 189 | const int ideal_splash_image_size_in_px, |
| 190 | const int minimum_splash_image_size_in_px, |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 191 | const std::string& webapp_id) { |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 192 | // This is a fire and forget task. It is not vital for the splash screen image |
| 193 | // to be downloaded so if the downloader returns false there is no fallback. |
| 194 | ManifestIconDownloader::Download( |
zpeng | 5d8fdfc | 2017-01-05 15:45:06 | [diff] [blame] | 195 | web_contents, image_url, ideal_splash_image_size_in_px, |
| 196 | minimum_splash_image_size_in_px, |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 197 | base::Bind(&ShortcutHelper::StoreWebappSplashImage, webapp_id)); |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | // static |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 201 | void ShortcutHelper::StoreWebappSplashImage( |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 202 | const std::string& webapp_id, |
| 203 | const SkBitmap& splash_image) { |
| 204 | if (splash_image.drawsNothing()) |
| 205 | return; |
| 206 | |
| 207 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 208 | ScopedJavaLocalRef<jstring> java_webapp_id = |
| 209 | base::android::ConvertUTF8ToJavaString(env, webapp_id); |
| 210 | ScopedJavaLocalRef<jobject> java_splash_image = |
| 211 | gfx::ConvertToJavaBitmap(&splash_image); |
| 212 | |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame] | 213 | Java_ShortcutHelper_storeWebappSplashImage(env, java_webapp_id, |
| 214 | java_splash_image); |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 215 | } |
| 216 | |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 217 | // static |
pkotwicz | 5774087e | 2016-08-10 17:36:40 | [diff] [blame] | 218 | SkBitmap ShortcutHelper::FinalizeLauncherIconInBackground( |
| 219 | const SkBitmap& bitmap, |
| 220 | const GURL& url, |
| 221 | bool* is_generated) { |
| 222 | DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 223 | |
| 224 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 225 | ScopedJavaLocalRef<jobject> result; |
| 226 | *is_generated = false; |
| 227 | |
| 228 | if (!bitmap.isNull()) { |
pkotwicz | 45fc42b6 | 2016-06-07 00:07:10 | [diff] [blame] | 229 | if (Java_ShortcutHelper_isIconLargeEnoughForLauncher(env, bitmap.width(), |
| 230 | bitmap.height())) { |
newt | a584b9e | 2015-10-29 22:29:43 | [diff] [blame] | 231 | ScopedJavaLocalRef<jobject> java_bitmap = |
| 232 | gfx::ConvertToJavaBitmap(&bitmap); |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame] | 233 | result = |
| 234 | Java_ShortcutHelper_createHomeScreenIconFromWebIcon(env, java_bitmap); |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
| 238 | if (result.is_null()) { |
| 239 | ScopedJavaLocalRef<jstring> java_url = |
| 240 | base::android::ConvertUTF8ToJavaString(env, url.spec()); |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame^] | 241 | SkColor mean_color = |
| 242 | SkColorSetRGB(g_default_rgb_icon_value, g_default_rgb_icon_value, |
| 243 | g_default_rgb_icon_value); |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 244 | |
| 245 | if (!bitmap.isNull()) |
| 246 | mean_color = color_utils::CalculateKMeanColorOfBitmap(bitmap); |
| 247 | |
| 248 | *is_generated = true; |
newt | a584b9e | 2015-10-29 22:29:43 | [diff] [blame] | 249 | result = Java_ShortcutHelper_generateHomeScreenIcon( |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame] | 250 | env, java_url, SkColorGetR(mean_color), SkColorGetG(mean_color), |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 251 | SkColorGetB(mean_color)); |
| 252 | } |
| 253 | |
pkotwicz | 964382b | 2016-08-04 01:24:55 | [diff] [blame] | 254 | return result.obj() |
torne | d64eb513 | 2016-10-24 12:51:28 | [diff] [blame] | 255 | ? gfx::CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(result)) |
pkotwicz | 964382b | 2016-08-04 01:24:55 | [diff] [blame] | 256 | : SkBitmap(); |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 257 | } |
| 258 | |
pkotwicz | cda82fe | 2016-07-08 18:56:54 | [diff] [blame] | 259 | // static |
zpeng | 4bb5896 | 2016-10-04 02:42:29 | [diff] [blame] | 260 | std::string ShortcutHelper::QueryWebApkPackage(const GURL& url) { |
| 261 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 262 | ScopedJavaLocalRef<jstring> java_url = |
| 263 | base::android::ConvertUTF8ToJavaString(env, url.spec()); |
| 264 | ScopedJavaLocalRef<jstring> java_webapk_package_name = |
| 265 | Java_ShortcutHelper_queryWebApkPackage(env, java_url); |
| 266 | |
| 267 | std::string webapk_package_name = ""; |
| 268 | if (java_webapk_package_name.obj()) { |
| 269 | webapk_package_name = base::android::ConvertJavaStringToUTF8( |
| 270 | env, java_webapk_package_name); |
| 271 | } |
| 272 | return webapk_package_name; |
| 273 | } |
| 274 | |
| 275 | // static |
hanxi | 070d10a | 2017-01-09 15:56:50 | [diff] [blame] | 276 | bool ShortcutHelper::IsWebApkInstalled( |
| 277 | content::BrowserContext* browser_context, |
| 278 | const GURL& start_url, |
| 279 | const GURL& manifest_url) { |
| 280 | return !QueryWebApkPackage(start_url).empty() || |
| 281 | WebApkInstallService::Get(browser_context) |
| 282 | ->IsInstallInProgress(manifest_url); |
pkotwicz | cda82fe | 2016-07-08 18:56:54 | [diff] [blame] | 283 | } |
| 284 | |
pkotwicz | 47136bc | 2016-08-06 23:55:39 | [diff] [blame] | 285 | GURL ShortcutHelper::GetScopeFromURL(const GURL& url) { |
| 286 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 287 | ScopedJavaLocalRef<jstring> java_url = |
| 288 | base::android::ConvertUTF8ToJavaString(env, url.spec()); |
| 289 | ScopedJavaLocalRef<jstring> java_scope_url = |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame] | 290 | Java_ShortcutHelper_getScopeFromUrl(env, java_url); |
pkotwicz | 47136bc | 2016-08-06 23:55:39 | [diff] [blame] | 291 | return GURL(base::android::ConvertJavaStringToUTF16(env, java_scope_url)); |
| 292 | } |
| 293 | |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 294 | // Callback used by Java when the shortcut has been created. |
| 295 | // |splash_image_callback| is a pointer to a base::Closure allocated in |
pkotwicz | c67e6ac | 2016-08-12 19:56:44 | [diff] [blame] | 296 | // AddShortcutWithSkBitmap, so reinterpret_cast it back and run it. |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 297 | // |
| 298 | // This callback should only ever be called when the shortcut was for a |
| 299 | // webapp-capable site; otherwise, |splash_image_callback| will have never been |
| 300 | // allocated and doesn't need to be run or deleted. |
| 301 | void OnWebappDataStored(JNIEnv* env, |
| 302 | const JavaParamRef<jclass>& clazz, |
| 303 | jlong jsplash_image_callback) { |
| 304 | DCHECK(jsplash_image_callback); |
| 305 | base::Closure* splash_image_callback = |
| 306 | reinterpret_cast<base::Closure*>(jsplash_image_callback); |
| 307 | splash_image_callback->Run(); |
| 308 | delete splash_image_callback; |
| 309 | } |
| 310 | |
lalitm | 870920e | 2015-08-20 22:06:03 | [diff] [blame] | 311 | bool ShortcutHelper::RegisterShortcutHelper(JNIEnv* env) { |
| 312 | return RegisterNativesImpl(env); |
benwells | 840ae90 | 2015-02-17 21:13:28 | [diff] [blame] | 313 | } |