[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" |
Daniel Cheng | 8fedab2 | 2018-05-17 00:39:27 | [diff] [blame^] | 16 | #include "base/optional.h" |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 17 | #include "base/strings/string16.h" |
mlamouri | c679bbf | 2014-09-24 21:24:49 | [diff] [blame] | 18 | #include "base/strings/utf_string_conversions.h" |
Peter Kotwicz | 9d0c67e | 2018-03-08 23:38:23 | [diff] [blame] | 19 | #include "base/time/time.h" |
Daniel Cheng | 8fedab2 | 2018-05-17 00:39:27 | [diff] [blame^] | 20 | #include "chrome/browser/android/color_helpers.h" |
hanxi | 563f353 | 2016-08-19 20:09:01 | [diff] [blame] | 21 | #include "chrome/browser/android/webapk/chrome_webapk_host.h" |
hanxi | 070d10a | 2017-01-09 15:56:50 | [diff] [blame] | 22 | #include "chrome/browser/android/webapk/webapk_install_service.h" |
pkotwicz | f4dddb3 | 2017-02-25 01:21:55 | [diff] [blame] | 23 | #include "chrome/browser/android/webapk/webapk_metrics.h" |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 24 | #include "chrome/common/chrome_switches.h" |
dfalcantara | aec56da | 2015-05-06 03:33:56 | [diff] [blame] | 25 | #include "content/public/browser/browser_thread.h" |
jinho.bang | 84cadc8 | 2017-06-14 16:59:34 | [diff] [blame] | 26 | #include "content/public/browser/manifest_icon_downloader.h" |
Dominick Ng | 203b493 | 2018-02-02 05:32:45 | [diff] [blame] | 27 | #include "content/public/browser/manifest_icon_selector.h" |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 28 | #include "content/public/browser/web_contents.h" |
Findit | 093657e | 2018-05-05 04:07:30 | [diff] [blame] | 29 | #include "content/public/common/manifest.h" |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 30 | #include "jni/ShortcutHelper_jni.h" |
Daniel Cheng | 8fedab2 | 2018-05-17 00:39:27 | [diff] [blame^] | 31 | #include "third_party/skia/include/core/SkColor.h" |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 32 | #include "ui/gfx/android/java_bitmap.h" |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 33 | #include "ui/gfx/color_analysis.h" |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 34 | #include "url/gurl.h" |
| 35 | |
torne | 8656011 | 2016-08-04 15:59:04 | [diff] [blame] | 36 | using base::android::JavaParamRef; |
| 37 | using base::android::ScopedJavaLocalRef; |
mlamouri | c679bbf | 2014-09-24 21:24:49 | [diff] [blame] | 38 | |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 39 | namespace { |
| 40 | |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 41 | int g_ideal_homescreen_icon_size = -1; |
| 42 | int g_minimum_homescreen_icon_size = -1; |
| 43 | int g_ideal_splash_image_size = -1; |
| 44 | int g_minimum_splash_image_size = -1; |
| 45 | int g_ideal_badge_icon_size = -1; |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 46 | |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 47 | int g_default_rgb_icon_value = 145; |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 48 | |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 49 | // Retrieves and caches the ideal and minimum sizes of the Home screen icon |
| 50 | // and the splash screen image. |
| 51 | void GetHomescreenIconAndSplashImageSizes() { |
| 52 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 53 | ScopedJavaLocalRef<jintArray> java_size_array = |
pkotwicz | 45fc42b6 | 2016-06-07 00:07:10 | [diff] [blame] | 54 | Java_ShortcutHelper_getHomeScreenIconAndSplashImageSizes(env); |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 55 | std::vector<int> sizes; |
zpeng | a7856eef | 2017-02-07 11:42:44 | [diff] [blame] | 56 | base::android::JavaIntArrayToIntVector(env, java_size_array.obj(), &sizes); |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 57 | |
| 58 | // Check that the size returned is what is expected. |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 59 | DCHECK(sizes.size() == 5); |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 60 | |
| 61 | // This ordering must be kept up to date with the Java ShortcutHelper. |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 62 | g_ideal_homescreen_icon_size = sizes[0]; |
| 63 | g_minimum_homescreen_icon_size = sizes[1]; |
| 64 | g_ideal_splash_image_size = sizes[2]; |
| 65 | g_minimum_splash_image_size = sizes[3]; |
| 66 | g_ideal_badge_icon_size = sizes[4]; |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 67 | |
| 68 | // Try to ensure that the data returned is sane. |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 69 | DCHECK(g_minimum_homescreen_icon_size <= g_ideal_homescreen_icon_size); |
| 70 | DCHECK(g_minimum_splash_image_size <= g_ideal_splash_image_size); |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 71 | } |
| 72 | |
pkotwicz | 9747871 | 2017-03-01 03:39:54 | [diff] [blame] | 73 | // Adds a shortcut which opens in a fullscreen window to the launcher. |
| 74 | // |splash_image_callback| will be invoked once the Java-side operation has |
| 75 | // completed. This is necessary as Java will asynchronously create and |
| 76 | // populate a WebappDataStorage object for standalone-capable sites. This must |
| 77 | // exist before the splash image can be stored. |
| 78 | void AddWebappWithSkBitmap(const ShortcutInfo& info, |
| 79 | const std::string& webapp_id, |
| 80 | const SkBitmap& icon_bitmap, |
| 81 | const base::Closure& splash_image_callback) { |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 82 | // Send the data to the Java side to create the shortcut. |
| 83 | JNIEnv* env = base::android::AttachCurrentThread(); |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 84 | ScopedJavaLocalRef<jstring> java_webapp_id = |
| 85 | base::android::ConvertUTF8ToJavaString(env, webapp_id); |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 86 | ScopedJavaLocalRef<jstring> java_url = |
dfalcantara | 16e84de | 2015-02-03 22:07:40 | [diff] [blame] | 87 | base::android::ConvertUTF8ToJavaString(env, info.url.spec()); |
pkotwicz | 6bdfbe1b | 2016-07-08 00:26:43 | [diff] [blame] | 88 | ScopedJavaLocalRef<jstring> java_scope_url = |
| 89 | base::android::ConvertUTF8ToJavaString(env, info.scope.spec()); |
lalitm | f3ee5185 | 2015-07-21 18:13:11 | [diff] [blame] | 90 | ScopedJavaLocalRef<jstring> java_user_title = |
| 91 | base::android::ConvertUTF16ToJavaString(env, info.user_title); |
| 92 | ScopedJavaLocalRef<jstring> java_name = |
| 93 | base::android::ConvertUTF16ToJavaString(env, info.name); |
| 94 | ScopedJavaLocalRef<jstring> java_short_name = |
| 95 | base::android::ConvertUTF16ToJavaString(env, info.short_name); |
zpeng | a7856eef | 2017-02-07 11:42:44 | [diff] [blame] | 96 | ScopedJavaLocalRef<jstring> java_best_primary_icon_url = |
| 97 | base::android::ConvertUTF8ToJavaString(env, |
| 98 | info.best_primary_icon_url.spec()); |
Piotr Swigon | 98256a78 | 2017-11-03 03:44:56 | [diff] [blame] | 99 | ScopedJavaLocalRef<jstring> java_splash_screen_url = |
| 100 | base::android::ConvertUTF8ToJavaString(env, |
| 101 | info.splash_screen_url.spec()); |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 102 | ScopedJavaLocalRef<jobject> java_bitmap; |
zpeng | febce761 | 2017-06-07 14:29:04 | [diff] [blame] | 103 | if (!icon_bitmap.drawsNothing()) |
mlamouri | c679bbf | 2014-09-24 21:24:49 | [diff] [blame] | 104 | java_bitmap = gfx::ConvertToJavaBitmap(&icon_bitmap); |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 105 | |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 106 | // The callback will need to be run after shortcut creation completes in order |
| 107 | // to download the splash image and save it to the WebappDataStorage. Create a |
| 108 | // copy of the callback here and send the pointer to Java, which will send it |
| 109 | // back once the asynchronous shortcut creation process finishes. |
| 110 | uintptr_t callback_pointer = |
| 111 | reinterpret_cast<uintptr_t>(new base::Closure(splash_image_callback)); |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 112 | |
zpeng | a7856eef | 2017-02-07 11:42:44 | [diff] [blame] | 113 | Java_ShortcutHelper_addWebapp( |
| 114 | env, java_webapp_id, java_url, java_scope_url, java_user_title, java_name, |
| 115 | java_short_name, java_best_primary_icon_url, java_bitmap, info.display, |
Daniel Cheng | 8fedab2 | 2018-05-17 00:39:27 | [diff] [blame^] | 116 | info.orientation, info.source, |
| 117 | OptionalSkColorToJavaColor(info.theme_color), |
| 118 | OptionalSkColorToJavaColor(info.background_color), java_splash_screen_url, |
| 119 | callback_pointer); |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 120 | } |
benwells | 840ae90 | 2015-02-17 21:13:28 | [diff] [blame] | 121 | |
pkotwicz | 9747871 | 2017-03-01 03:39:54 | [diff] [blame] | 122 | // Adds a shortcut which opens in a browser tab to the launcher. |
| 123 | void AddShortcutWithSkBitmap(const ShortcutInfo& info, |
| 124 | const std::string& id, |
| 125 | const SkBitmap& icon_bitmap) { |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 126 | JNIEnv* env = base::android::AttachCurrentThread(); |
martiw | 12166f5 | 2017-02-20 03:05:45 | [diff] [blame] | 127 | ScopedJavaLocalRef<jstring> java_id = |
| 128 | base::android::ConvertUTF8ToJavaString(env, id); |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 129 | ScopedJavaLocalRef<jstring> java_url = |
| 130 | base::android::ConvertUTF8ToJavaString(env, info.url.spec()); |
| 131 | ScopedJavaLocalRef<jstring> java_user_title = |
| 132 | base::android::ConvertUTF16ToJavaString(env, info.user_title); |
| 133 | ScopedJavaLocalRef<jobject> java_bitmap; |
zpeng | febce761 | 2017-06-07 14:29:04 | [diff] [blame] | 134 | if (!icon_bitmap.drawsNothing()) |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 135 | java_bitmap = gfx::ConvertToJavaBitmap(&icon_bitmap); |
| 136 | |
martiw | 12166f5 | 2017-02-20 03:05:45 | [diff] [blame] | 137 | Java_ShortcutHelper_addShortcut(env, java_id, java_url, java_user_title, |
| 138 | java_bitmap, info.source); |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 139 | } |
| 140 | |
pkotwicz | 9747871 | 2017-03-01 03:39:54 | [diff] [blame] | 141 | } // anonymous namespace |
| 142 | |
| 143 | // static |
Dominick Ng | 203b493 | 2018-02-02 05:32:45 | [diff] [blame] | 144 | std::unique_ptr<ShortcutInfo> ShortcutHelper::CreateShortcutInfo( |
| 145 | const GURL& manifest_url, |
Findit | 093657e | 2018-05-05 04:07:30 | [diff] [blame] | 146 | const content::Manifest& manifest, |
Dominick Ng | 203b493 | 2018-02-02 05:32:45 | [diff] [blame] | 147 | const GURL& primary_icon_url, |
| 148 | const GURL& badge_icon_url) { |
| 149 | auto shortcut_info = std::make_unique<ShortcutInfo>(GURL()); |
| 150 | if (!manifest.IsEmpty()) { |
| 151 | shortcut_info->UpdateFromManifest(manifest); |
| 152 | shortcut_info->manifest_url = manifest_url; |
| 153 | shortcut_info->best_primary_icon_url = primary_icon_url; |
| 154 | shortcut_info->best_badge_icon_url = badge_icon_url; |
| 155 | } |
| 156 | |
| 157 | shortcut_info->ideal_splash_image_size_in_px = GetIdealSplashImageSizeInPx(); |
| 158 | shortcut_info->minimum_splash_image_size_in_px = |
| 159 | GetMinimumSplashImageSizeInPx(); |
| 160 | shortcut_info->splash_image_url = |
| 161 | content::ManifestIconSelector::FindBestMatchingIcon( |
| 162 | manifest.icons, shortcut_info->ideal_splash_image_size_in_px, |
| 163 | shortcut_info->minimum_splash_image_size_in_px, |
Findit | 093657e | 2018-05-05 04:07:30 | [diff] [blame] | 164 | content::Manifest::Icon::IconPurpose::ANY); |
Dominick Ng | 203b493 | 2018-02-02 05:32:45 | [diff] [blame] | 165 | |
| 166 | return shortcut_info; |
| 167 | } |
| 168 | |
| 169 | // static |
pkotwicz | 9747871 | 2017-03-01 03:39:54 | [diff] [blame] | 170 | void ShortcutHelper::AddToLauncherWithSkBitmap( |
pkotwicz | d0d459e | 2017-03-01 22:55:10 | [diff] [blame] | 171 | content::WebContents* web_contents, |
pkotwicz | 9747871 | 2017-03-01 03:39:54 | [diff] [blame] | 172 | const ShortcutInfo& info, |
pkotwicz | d0d459e | 2017-03-01 22:55:10 | [diff] [blame] | 173 | const SkBitmap& icon_bitmap) { |
| 174 | std::string webapp_id = base::GenerateGUID(); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 175 | if (info.display == blink::kWebDisplayModeStandalone || |
Piotr Swigon | 549c043 | 2017-08-16 02:48:41 | [diff] [blame] | 176 | info.display == blink::kWebDisplayModeFullscreen || |
Piotr Swigon | 57e79c5c | 2017-12-29 01:11:15 | [diff] [blame] | 177 | info.display == blink::kWebDisplayModeMinimalUi) { |
pkotwicz | d0d459e | 2017-03-01 22:55:10 | [diff] [blame] | 178 | AddWebappWithSkBitmap( |
| 179 | info, webapp_id, icon_bitmap, |
| 180 | base::Bind(&ShortcutHelper::FetchSplashScreenImage, web_contents, |
| 181 | info.splash_image_url, info.ideal_splash_image_size_in_px, |
| 182 | info.minimum_splash_image_size_in_px, webapp_id)); |
pkotwicz | 9747871 | 2017-03-01 03:39:54 | [diff] [blame] | 183 | return; |
| 184 | } |
| 185 | AddShortcutWithSkBitmap(info, webapp_id, icon_bitmap); |
| 186 | } |
| 187 | |
hanxi | 070d10a | 2017-01-09 15:56:50 | [diff] [blame] | 188 | void ShortcutHelper::ShowWebApkInstallInProgressToast() { |
| 189 | Java_ShortcutHelper_showWebApkInstallInProgressToast( |
| 190 | base::android::AttachCurrentThread()); |
| 191 | } |
| 192 | |
zpeng | 5d8fdfc | 2017-01-05 15:45:06 | [diff] [blame] | 193 | int ShortcutHelper::GetIdealHomescreenIconSizeInPx() { |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 194 | if (g_ideal_homescreen_icon_size == -1) |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 195 | GetHomescreenIconAndSplashImageSizes(); |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 196 | return g_ideal_homescreen_icon_size; |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 197 | } |
| 198 | |
zpeng | 5d8fdfc | 2017-01-05 15:45:06 | [diff] [blame] | 199 | int ShortcutHelper::GetMinimumHomescreenIconSizeInPx() { |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 200 | if (g_minimum_homescreen_icon_size == -1) |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 201 | GetHomescreenIconAndSplashImageSizes(); |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 202 | return g_minimum_homescreen_icon_size; |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 203 | } |
| 204 | |
zpeng | 5d8fdfc | 2017-01-05 15:45:06 | [diff] [blame] | 205 | int ShortcutHelper::GetIdealSplashImageSizeInPx() { |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 206 | if (g_ideal_splash_image_size == -1) |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 207 | GetHomescreenIconAndSplashImageSizes(); |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 208 | return g_ideal_splash_image_size; |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 209 | } |
| 210 | |
zpeng | 5d8fdfc | 2017-01-05 15:45:06 | [diff] [blame] | 211 | int ShortcutHelper::GetMinimumSplashImageSizeInPx() { |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 212 | if (g_minimum_splash_image_size == -1) |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 213 | GetHomescreenIconAndSplashImageSizes(); |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 214 | return g_minimum_splash_image_size; |
| 215 | } |
| 216 | |
| 217 | int ShortcutHelper::GetIdealBadgeIconSizeInPx() { |
| 218 | if (g_ideal_badge_icon_size == -1) |
| 219 | GetHomescreenIconAndSplashImageSizes(); |
| 220 | return g_ideal_badge_icon_size; |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 221 | } |
| 222 | |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 223 | // static |
| 224 | void ShortcutHelper::FetchSplashScreenImage( |
| 225 | content::WebContents* web_contents, |
| 226 | const GURL& image_url, |
zpeng | 5d8fdfc | 2017-01-05 15:45:06 | [diff] [blame] | 227 | const int ideal_splash_image_size_in_px, |
| 228 | const int minimum_splash_image_size_in_px, |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 229 | const std::string& webapp_id) { |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 230 | // This is a fire and forget task. It is not vital for the splash screen image |
| 231 | // to be downloaded so if the downloader returns false there is no fallback. |
jinho.bang | 84cadc8 | 2017-06-14 16:59:34 | [diff] [blame] | 232 | content::ManifestIconDownloader::Download( |
zpeng | 5d8fdfc | 2017-01-05 15:45:06 | [diff] [blame] | 233 | web_contents, image_url, ideal_splash_image_size_in_px, |
| 234 | minimum_splash_image_size_in_px, |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 235 | base::Bind(&ShortcutHelper::StoreWebappSplashImage, webapp_id)); |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | // static |
zpeng | a7856eef | 2017-02-07 11:42:44 | [diff] [blame] | 239 | void ShortcutHelper::StoreWebappSplashImage(const std::string& webapp_id, |
| 240 | const SkBitmap& splash_image) { |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 241 | if (splash_image.drawsNothing()) |
| 242 | return; |
| 243 | |
| 244 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 245 | ScopedJavaLocalRef<jstring> java_webapp_id = |
| 246 | base::android::ConvertUTF8ToJavaString(env, webapp_id); |
| 247 | ScopedJavaLocalRef<jobject> java_splash_image = |
| 248 | gfx::ConvertToJavaBitmap(&splash_image); |
| 249 | |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame] | 250 | Java_ShortcutHelper_storeWebappSplashImage(env, java_webapp_id, |
| 251 | java_splash_image); |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 252 | } |
| 253 | |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 254 | // static |
pkotwicz | 5774087e | 2016-08-10 17:36:40 | [diff] [blame] | 255 | SkBitmap ShortcutHelper::FinalizeLauncherIconInBackground( |
| 256 | const SkBitmap& bitmap, |
| 257 | const GURL& url, |
| 258 | bool* is_generated) { |
Francois Doray | 66bdfd8 | 2017-10-20 13:50:37 | [diff] [blame] | 259 | base::AssertBlockingAllowed(); |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 260 | |
| 261 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 262 | ScopedJavaLocalRef<jobject> result; |
| 263 | *is_generated = false; |
| 264 | |
| 265 | if (!bitmap.isNull()) { |
pkotwicz | 45fc42b6 | 2016-06-07 00:07:10 | [diff] [blame] | 266 | if (Java_ShortcutHelper_isIconLargeEnoughForLauncher(env, bitmap.width(), |
| 267 | bitmap.height())) { |
newt | a584b9e | 2015-10-29 22:29:43 | [diff] [blame] | 268 | ScopedJavaLocalRef<jobject> java_bitmap = |
| 269 | gfx::ConvertToJavaBitmap(&bitmap); |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame] | 270 | result = |
| 271 | Java_ShortcutHelper_createHomeScreenIconFromWebIcon(env, java_bitmap); |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
| 275 | if (result.is_null()) { |
| 276 | ScopedJavaLocalRef<jstring> java_url = |
| 277 | base::android::ConvertUTF8ToJavaString(env, url.spec()); |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 278 | SkColor mean_color = |
| 279 | SkColorSetRGB(g_default_rgb_icon_value, g_default_rgb_icon_value, |
| 280 | g_default_rgb_icon_value); |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 281 | |
| 282 | if (!bitmap.isNull()) |
| 283 | mean_color = color_utils::CalculateKMeanColorOfBitmap(bitmap); |
| 284 | |
| 285 | *is_generated = true; |
newt | a584b9e | 2015-10-29 22:29:43 | [diff] [blame] | 286 | result = Java_ShortcutHelper_generateHomeScreenIcon( |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame] | 287 | env, java_url, SkColorGetR(mean_color), SkColorGetG(mean_color), |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 288 | SkColorGetB(mean_color)); |
| 289 | } |
| 290 | |
pkotwicz | 964382b | 2016-08-04 01:24:55 | [diff] [blame] | 291 | return result.obj() |
torne | d64eb513 | 2016-10-24 12:51:28 | [diff] [blame] | 292 | ? gfx::CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(result)) |
pkotwicz | 964382b | 2016-08-04 01:24:55 | [diff] [blame] | 293 | : SkBitmap(); |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 294 | } |
| 295 | |
pkotwicz | cda82fe | 2016-07-08 18:56:54 | [diff] [blame] | 296 | // static |
zpeng | 4bb5896 | 2016-10-04 02:42:29 | [diff] [blame] | 297 | std::string ShortcutHelper::QueryWebApkPackage(const GURL& url) { |
| 298 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 299 | ScopedJavaLocalRef<jstring> java_url = |
| 300 | base::android::ConvertUTF8ToJavaString(env, url.spec()); |
| 301 | ScopedJavaLocalRef<jstring> java_webapk_package_name = |
| 302 | Java_ShortcutHelper_queryWebApkPackage(env, java_url); |
| 303 | |
| 304 | std::string webapk_package_name = ""; |
| 305 | if (java_webapk_package_name.obj()) { |
zpeng | a7856eef | 2017-02-07 11:42:44 | [diff] [blame] | 306 | webapk_package_name = |
| 307 | base::android::ConvertJavaStringToUTF8(env, java_webapk_package_name); |
zpeng | 4bb5896 | 2016-10-04 02:42:29 | [diff] [blame] | 308 | } |
| 309 | return webapk_package_name; |
| 310 | } |
| 311 | |
| 312 | // static |
zpeng | a7856eef | 2017-02-07 11:42:44 | [diff] [blame] | 313 | bool ShortcutHelper::IsWebApkInstalled(content::BrowserContext* browser_context, |
| 314 | const GURL& start_url, |
| 315 | const GURL& manifest_url) { |
hanxi | 070d10a | 2017-01-09 15:56:50 | [diff] [blame] | 316 | return !QueryWebApkPackage(start_url).empty() || |
zpeng | a7856eef | 2017-02-07 11:42:44 | [diff] [blame] | 317 | WebApkInstallService::Get(browser_context) |
| 318 | ->IsInstallInProgress(manifest_url); |
pkotwicz | cda82fe | 2016-07-08 18:56:54 | [diff] [blame] | 319 | } |
| 320 | |
pkotwicz | 47136bc | 2016-08-06 23:55:39 | [diff] [blame] | 321 | GURL ShortcutHelper::GetScopeFromURL(const GURL& url) { |
| 322 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 323 | ScopedJavaLocalRef<jstring> java_url = |
| 324 | base::android::ConvertUTF8ToJavaString(env, url.spec()); |
| 325 | ScopedJavaLocalRef<jstring> java_scope_url = |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame] | 326 | Java_ShortcutHelper_getScopeFromUrl(env, java_url); |
pkotwicz | 47136bc | 2016-08-06 23:55:39 | [diff] [blame] | 327 | return GURL(base::android::ConvertJavaStringToUTF16(env, java_scope_url)); |
| 328 | } |
| 329 | |
gonzalon | 87192d77 | 2017-02-07 22:31:10 | [diff] [blame] | 330 | void ShortcutHelper::RetrieveWebApks(const WebApkInfoCallback& callback) { |
| 331 | uintptr_t callback_pointer = |
| 332 | reinterpret_cast<uintptr_t>(new WebApkInfoCallback(callback)); |
| 333 | Java_ShortcutHelper_retrieveWebApks(base::android::AttachCurrentThread(), |
| 334 | callback_pointer); |
| 335 | } |
| 336 | |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 337 | // Callback used by Java when the shortcut has been created. |
| 338 | // |splash_image_callback| is a pointer to a base::Closure allocated in |
pkotwicz | c67e6ac | 2016-08-12 19:56:44 | [diff] [blame] | 339 | // AddShortcutWithSkBitmap, so reinterpret_cast it back and run it. |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 340 | // |
| 341 | // This callback should only ever be called when the shortcut was for a |
| 342 | // webapp-capable site; otherwise, |splash_image_callback| will have never been |
| 343 | // allocated and doesn't need to be run or deleted. |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 344 | void JNI_ShortcutHelper_OnWebappDataStored(JNIEnv* env, |
| 345 | const JavaParamRef<jclass>& clazz, |
| 346 | jlong jsplash_image_callback) { |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 347 | DCHECK(jsplash_image_callback); |
| 348 | base::Closure* splash_image_callback = |
| 349 | reinterpret_cast<base::Closure*>(jsplash_image_callback); |
| 350 | splash_image_callback->Run(); |
| 351 | delete splash_image_callback; |
| 352 | } |
| 353 | |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 354 | void JNI_ShortcutHelper_OnWebApksRetrieved( |
| 355 | JNIEnv* env, |
| 356 | const JavaParamRef<jclass>& clazz, |
| 357 | const jlong jcallback_pointer, |
| 358 | const JavaParamRef<jobjectArray>& jnames, |
| 359 | const JavaParamRef<jobjectArray>& jshort_names, |
| 360 | const JavaParamRef<jobjectArray>& jpackage_names, |
| 361 | const JavaParamRef<jintArray>& jshell_apk_versions, |
| 362 | const JavaParamRef<jintArray>& jversion_codes, |
| 363 | const JavaParamRef<jobjectArray>& juris, |
| 364 | const JavaParamRef<jobjectArray>& jscopes, |
| 365 | const JavaParamRef<jobjectArray>& jmanifest_urls, |
| 366 | const JavaParamRef<jobjectArray>& jmanifest_start_urls, |
| 367 | const JavaParamRef<jintArray>& jdisplay_modes, |
| 368 | const JavaParamRef<jintArray>& jorientations, |
| 369 | const JavaParamRef<jlongArray>& jtheme_colors, |
Peter Kotwicz | 9d0c67e | 2018-03-08 23:38:23 | [diff] [blame] | 370 | const JavaParamRef<jlongArray>& jbackground_colors, |
| 371 | const JavaParamRef<jlongArray>& jlast_update_check_times_ms, |
| 372 | const JavaParamRef<jbooleanArray>& jrelax_updates) { |
gonzalon | 87192d77 | 2017-02-07 22:31:10 | [diff] [blame] | 373 | DCHECK(jcallback_pointer); |
gonzalon | 85e97545 | 2017-03-03 23:58:42 | [diff] [blame] | 374 | std::vector<std::string> names; |
| 375 | base::android::AppendJavaStringArrayToStringVector(env, jnames, &names); |
gonzalon | 87192d77 | 2017-02-07 22:31:10 | [diff] [blame] | 376 | std::vector<std::string> short_names; |
| 377 | base::android::AppendJavaStringArrayToStringVector(env, jshort_names, |
| 378 | &short_names); |
| 379 | std::vector<std::string> package_names; |
| 380 | base::android::AppendJavaStringArrayToStringVector(env, jpackage_names, |
| 381 | &package_names); |
| 382 | std::vector<int> shell_apk_versions; |
| 383 | base::android::JavaIntArrayToIntVector(env, jshell_apk_versions, |
| 384 | &shell_apk_versions); |
| 385 | std::vector<int> version_codes; |
| 386 | base::android::JavaIntArrayToIntVector(env, jversion_codes, &version_codes); |
gonzalon | 85e97545 | 2017-03-03 23:58:42 | [diff] [blame] | 387 | std::vector<std::string> uris; |
| 388 | base::android::AppendJavaStringArrayToStringVector(env, juris, &uris); |
| 389 | std::vector<std::string> scopes; |
| 390 | base::android::AppendJavaStringArrayToStringVector(env, jscopes, &scopes); |
| 391 | std::vector<std::string> manifest_urls; |
| 392 | base::android::AppendJavaStringArrayToStringVector(env, jmanifest_urls, |
| 393 | &manifest_urls); |
| 394 | std::vector<std::string> manifest_start_urls; |
| 395 | base::android::AppendJavaStringArrayToStringVector(env, jmanifest_start_urls, |
| 396 | &manifest_start_urls); |
| 397 | std::vector<int> display_modes; |
| 398 | base::android::JavaIntArrayToIntVector(env, jdisplay_modes, &display_modes); |
| 399 | std::vector<int> orientations; |
| 400 | base::android::JavaIntArrayToIntVector(env, jorientations, &orientations); |
| 401 | std::vector<int64_t> theme_colors; |
| 402 | base::android::JavaLongArrayToInt64Vector(env, jtheme_colors, &theme_colors); |
| 403 | std::vector<int64_t> background_colors; |
| 404 | base::android::JavaLongArrayToInt64Vector(env, jbackground_colors, |
| 405 | &background_colors); |
Peter Kotwicz | 9d0c67e | 2018-03-08 23:38:23 | [diff] [blame] | 406 | std::vector<int64_t> last_update_check_times_ms; |
| 407 | base::android::JavaLongArrayToInt64Vector(env, jlast_update_check_times_ms, |
| 408 | &last_update_check_times_ms); |
| 409 | std::vector<bool> relax_updates; |
| 410 | base::android::JavaBooleanArrayToBoolVector(env, jrelax_updates, |
| 411 | &relax_updates); |
gonzalon | 87192d77 | 2017-02-07 22:31:10 | [diff] [blame] | 412 | |
gonzalon | 85e97545 | 2017-03-03 23:58:42 | [diff] [blame] | 413 | DCHECK(short_names.size() == names.size()); |
gonzalon | 87192d77 | 2017-02-07 22:31:10 | [diff] [blame] | 414 | DCHECK(short_names.size() == package_names.size()); |
| 415 | DCHECK(short_names.size() == shell_apk_versions.size()); |
| 416 | DCHECK(short_names.size() == version_codes.size()); |
gonzalon | 85e97545 | 2017-03-03 23:58:42 | [diff] [blame] | 417 | DCHECK(short_names.size() == uris.size()); |
| 418 | DCHECK(short_names.size() == scopes.size()); |
| 419 | DCHECK(short_names.size() == manifest_urls.size()); |
| 420 | DCHECK(short_names.size() == manifest_start_urls.size()); |
| 421 | DCHECK(short_names.size() == display_modes.size()); |
| 422 | DCHECK(short_names.size() == orientations.size()); |
| 423 | DCHECK(short_names.size() == theme_colors.size()); |
| 424 | DCHECK(short_names.size() == background_colors.size()); |
Peter Kotwicz | 9d0c67e | 2018-03-08 23:38:23 | [diff] [blame] | 425 | DCHECK(short_names.size() == last_update_check_times_ms.size()); |
| 426 | DCHECK(short_names.size() == relax_updates.size()); |
gonzalon | 87192d77 | 2017-02-07 22:31:10 | [diff] [blame] | 427 | |
| 428 | std::vector<WebApkInfo> webapk_list; |
| 429 | webapk_list.reserve(short_names.size()); |
| 430 | for (size_t i = 0; i < short_names.size(); ++i) { |
gonzalon | 85e97545 | 2017-03-03 23:58:42 | [diff] [blame] | 431 | webapk_list.push_back(WebApkInfo( |
| 432 | std::move(names[i]), std::move(short_names[i]), |
| 433 | std::move(package_names[i]), shell_apk_versions[i], version_codes[i], |
| 434 | std::move(uris[i]), std::move(scopes[i]), std::move(manifest_urls[i]), |
| 435 | std::move(manifest_start_urls[i]), |
| 436 | static_cast<blink::WebDisplayMode>(display_modes[i]), |
| 437 | static_cast<blink::WebScreenOrientationLockType>(orientations[i]), |
Daniel Cheng | 8fedab2 | 2018-05-17 00:39:27 | [diff] [blame^] | 438 | JavaColorToOptionalSkColor(theme_colors[i]), |
| 439 | JavaColorToOptionalSkColor(background_colors[i]), |
Peter Kotwicz | 9d0c67e | 2018-03-08 23:38:23 | [diff] [blame] | 440 | base::Time::FromJavaTime(last_update_check_times_ms[i]), |
| 441 | relax_updates[i])); |
gonzalon | 87192d77 | 2017-02-07 22:31:10 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | ShortcutHelper::WebApkInfoCallback* webapk_list_callback = |
| 445 | reinterpret_cast<ShortcutHelper::WebApkInfoCallback*>(jcallback_pointer); |
| 446 | webapk_list_callback->Run(webapk_list); |
| 447 | delete webapk_list_callback; |
| 448 | } |