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