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