[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 | |
Henry Jian | f78dce6 | 2019-09-22 16:58:41 | [diff] [blame] | 10 | #include "base/android/build_info.h" |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 11 | #include "base/android/jni_android.h" |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 12 | #include "base/android/jni_array.h" |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 13 | #include "base/android/jni_string.h" |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 14 | #include "base/bind.h" |
| 15 | #include "base/callback.h" |
Henry Jian | f78dce6 | 2019-09-22 16:58:41 | [diff] [blame] | 16 | #include "base/feature_list.h" |
pkotwicz | d0d459e | 2017-03-01 22:55:10 | [diff] [blame] | 17 | #include "base/guid.h" |
Daniel Cheng | 8fedab2 | 2018-05-17 00:39:27 | [diff] [blame] | 18 | #include "base/optional.h" |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 19 | #include "base/strings/string16.h" |
mlamouri | c679bbf | 2014-09-24 21:24:49 | [diff] [blame] | 20 | #include "base/strings/utf_string_conversions.h" |
Etienne Pierre-doray | 4f2e41bed | 2018-09-24 22:10:11 | [diff] [blame] | 21 | #include "base/threading/thread_restrictions.h" |
Andrew Grieve | 4a42c22e | 2019-06-24 16:14:29 | [diff] [blame] | 22 | #include "chrome/android/chrome_jni_headers/ShortcutHelper_jni.h" |
Daniel Cheng | 8fedab2 | 2018-05-17 00:39:27 | [diff] [blame] | 23 | #include "chrome/browser/android/color_helpers.h" |
Michael Thiessen | e01199c94 | 2019-06-25 16:32:53 | [diff] [blame] | 24 | #include "chrome/browser/android/tab_android.h" |
hanxi | 070d10a | 2017-01-09 15:56:50 | [diff] [blame] | 25 | #include "chrome/browser/android/webapk/webapk_install_service.h" |
pkotwicz | f4dddb3 | 2017-02-25 01:21:55 | [diff] [blame] | 26 | #include "chrome/browser/android/webapk/webapk_metrics.h" |
Henrique Nakashima | 476b0b5 | 2020-01-24 22:28:03 | [diff] [blame] | 27 | #include "chrome/browser/flags/android/chrome_feature_list.h" |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 28 | #include "chrome/common/chrome_switches.h" |
dfalcantara | aec56da | 2015-05-06 03:33:56 | [diff] [blame] | 29 | #include "content/public/browser/browser_thread.h" |
jinho.bang | 84cadc8 | 2017-06-14 16:59:34 | [diff] [blame] | 30 | #include "content/public/browser/manifest_icon_downloader.h" |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 31 | #include "content/public/browser/web_contents.h" |
Dmitry Gozman | 88ca5a99 | 2018-05-18 00:13:33 | [diff] [blame] | 32 | #include "third_party/blink/public/common/manifest/manifest.h" |
Mugdha Lakhani | 62db8de | 2018-07-10 16:00:48 | [diff] [blame] | 33 | #include "third_party/blink/public/common/manifest/manifest_icon_selector.h" |
Daniel Cheng | 8fedab2 | 2018-05-17 00:39:27 | [diff] [blame] | 34 | #include "third_party/skia/include/core/SkColor.h" |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 35 | #include "ui/gfx/android/java_bitmap.h" |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 36 | #include "ui/gfx/color_analysis.h" |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 37 | #include "url/gurl.h" |
| 38 | |
torne | 8656011 | 2016-08-04 15:59:04 | [diff] [blame] | 39 | using base::android::JavaParamRef; |
| 40 | using base::android::ScopedJavaLocalRef; |
mlamouri | c679bbf | 2014-09-24 21:24:49 | [diff] [blame] | 41 | |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 42 | namespace { |
| 43 | |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 44 | int g_ideal_homescreen_icon_size = -1; |
| 45 | int g_minimum_homescreen_icon_size = -1; |
| 46 | int g_ideal_splash_image_size = -1; |
| 47 | int g_minimum_splash_image_size = -1; |
| 48 | int g_ideal_badge_icon_size = -1; |
Henry Jian | 96ec251 | 2019-09-13 00:34:09 | [diff] [blame] | 49 | int g_ideal_adaptive_launcher_icon_size = -1; |
Rayan Kanso | 3a2f476 | 2020-01-28 13:34:16 | [diff] [blame] | 50 | int g_ideal_shortcut_icon_size = -1; |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 51 | |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 52 | int g_default_rgb_icon_value = 145; |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 53 | |
Rayan Kanso | 3a2f476 | 2020-01-28 13:34:16 | [diff] [blame] | 54 | // Retrieves and caches the ideal and minimum sizes of the Home screen icon, |
| 55 | // the splash screen image, and the shortcut icons. |
| 56 | void GetIconSizes() { |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 57 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 58 | ScopedJavaLocalRef<jintArray> java_size_array = |
Rayan Kanso | 3a2f476 | 2020-01-28 13:34:16 | [diff] [blame] | 59 | Java_ShortcutHelper_getIconSizes(env); |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 60 | std::vector<int> sizes; |
Torne (Richard Coles) | 3c22e830 | 2018-10-12 18:34:22 | [diff] [blame] | 61 | base::android::JavaIntArrayToIntVector(env, java_size_array, &sizes); |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 62 | |
| 63 | // Check that the size returned is what is expected. |
Rayan Kanso | 3a2f476 | 2020-01-28 13:34:16 | [diff] [blame] | 64 | DCHECK_EQ(7u, sizes.size()); |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 65 | |
| 66 | // This ordering must be kept up to date with the Java ShortcutHelper. |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 67 | g_ideal_homescreen_icon_size = sizes[0]; |
| 68 | g_minimum_homescreen_icon_size = sizes[1]; |
| 69 | g_ideal_splash_image_size = sizes[2]; |
| 70 | g_minimum_splash_image_size = sizes[3]; |
| 71 | g_ideal_badge_icon_size = sizes[4]; |
Henry Jian | 96ec251 | 2019-09-13 00:34:09 | [diff] [blame] | 72 | g_ideal_adaptive_launcher_icon_size = sizes[5]; |
Rayan Kanso | 3a2f476 | 2020-01-28 13:34:16 | [diff] [blame] | 73 | g_ideal_shortcut_icon_size = sizes[6]; |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 74 | |
| 75 | // Try to ensure that the data returned is sane. |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 76 | DCHECK(g_minimum_homescreen_icon_size <= g_ideal_homescreen_icon_size); |
| 77 | DCHECK(g_minimum_splash_image_size <= g_ideal_splash_image_size); |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 78 | } |
| 79 | |
pkotwicz | 9747871 | 2017-03-01 03:39:54 | [diff] [blame] | 80 | // Adds a shortcut which opens in a fullscreen window to the launcher. |
| 81 | // |splash_image_callback| will be invoked once the Java-side operation has |
| 82 | // completed. This is necessary as Java will asynchronously create and |
| 83 | // populate a WebappDataStorage object for standalone-capable sites. This must |
| 84 | // exist before the splash image can be stored. |
| 85 | void AddWebappWithSkBitmap(const ShortcutInfo& info, |
| 86 | const std::string& webapp_id, |
| 87 | const SkBitmap& icon_bitmap, |
Peter E Conn | b955ae4a | 2018-12-03 13:02:09 | [diff] [blame] | 88 | bool is_icon_maskable, |
Dominick Ng | 40be818 | 2019-03-11 23:21:03 | [diff] [blame] | 89 | base::OnceClosure splash_image_callback) { |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 90 | // Send the data to the Java side to create the shortcut. |
| 91 | JNIEnv* env = base::android::AttachCurrentThread(); |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 92 | ScopedJavaLocalRef<jstring> java_webapp_id = |
| 93 | base::android::ConvertUTF8ToJavaString(env, webapp_id); |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 94 | ScopedJavaLocalRef<jstring> java_url = |
dfalcantara | 16e84de | 2015-02-03 22:07:40 | [diff] [blame] | 95 | base::android::ConvertUTF8ToJavaString(env, info.url.spec()); |
pkotwicz | 6bdfbe1b | 2016-07-08 00:26:43 | [diff] [blame] | 96 | ScopedJavaLocalRef<jstring> java_scope_url = |
| 97 | base::android::ConvertUTF8ToJavaString(env, info.scope.spec()); |
lalitm | f3ee5185 | 2015-07-21 18:13:11 | [diff] [blame] | 98 | ScopedJavaLocalRef<jstring> java_user_title = |
| 99 | base::android::ConvertUTF16ToJavaString(env, info.user_title); |
| 100 | ScopedJavaLocalRef<jstring> java_name = |
| 101 | base::android::ConvertUTF16ToJavaString(env, info.name); |
| 102 | ScopedJavaLocalRef<jstring> java_short_name = |
| 103 | base::android::ConvertUTF16ToJavaString(env, info.short_name); |
zpeng | a7856eef | 2017-02-07 11:42:44 | [diff] [blame] | 104 | ScopedJavaLocalRef<jstring> java_best_primary_icon_url = |
| 105 | base::android::ConvertUTF8ToJavaString(env, |
| 106 | info.best_primary_icon_url.spec()); |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 107 | ScopedJavaLocalRef<jobject> java_bitmap; |
zpeng | febce761 | 2017-06-07 14:29:04 | [diff] [blame] | 108 | if (!icon_bitmap.drawsNothing()) |
mlamouri | c679bbf | 2014-09-24 21:24:49 | [diff] [blame] | 109 | java_bitmap = gfx::ConvertToJavaBitmap(&icon_bitmap); |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 110 | |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 111 | // The callback will need to be run after shortcut creation completes in order |
| 112 | // to download the splash image and save it to the WebappDataStorage. Create a |
| 113 | // copy of the callback here and send the pointer to Java, which will send it |
| 114 | // back once the asynchronous shortcut creation process finishes. |
Dominick Ng | 40be818 | 2019-03-11 23:21:03 | [diff] [blame] | 115 | uintptr_t callback_pointer = reinterpret_cast<uintptr_t>( |
| 116 | new base::OnceClosure(std::move(splash_image_callback))); |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 117 | |
zpeng | a7856eef | 2017-02-07 11:42:44 | [diff] [blame] | 118 | Java_ShortcutHelper_addWebapp( |
| 119 | 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] | 120 | java_short_name, java_best_primary_icon_url, java_bitmap, |
Eric Willigers | 052f043 | 2019-10-04 04:06:57 | [diff] [blame] | 121 | is_icon_maskable, static_cast<int>(info.display), info.orientation, |
| 122 | info.source, OptionalSkColorToJavaColor(info.theme_color), |
Matt Jones | 2cd761c | 2019-09-18 18:53:40 | [diff] [blame] | 123 | OptionalSkColorToJavaColor(info.background_color), callback_pointer); |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 124 | } |
benwells | 840ae90 | 2015-02-17 21:13:28 | [diff] [blame] | 125 | |
pkotwicz | 9747871 | 2017-03-01 03:39:54 | [diff] [blame] | 126 | // Adds a shortcut which opens in a browser tab to the launcher. |
Michael Thiessen | e01199c94 | 2019-06-25 16:32:53 | [diff] [blame] | 127 | void AddShortcutWithSkBitmap(content::WebContents* web_contents, |
| 128 | const ShortcutInfo& info, |
pkotwicz | 9747871 | 2017-03-01 03:39:54 | [diff] [blame] | 129 | const std::string& id, |
Peter E Conn | b955ae4a | 2018-12-03 13:02:09 | [diff] [blame] | 130 | const SkBitmap& icon_bitmap, |
| 131 | bool is_icon_maskable) { |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 132 | JNIEnv* env = base::android::AttachCurrentThread(); |
martiw | 12166f5 | 2017-02-20 03:05:45 | [diff] [blame] | 133 | ScopedJavaLocalRef<jstring> java_id = |
| 134 | base::android::ConvertUTF8ToJavaString(env, id); |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 135 | ScopedJavaLocalRef<jstring> java_url = |
| 136 | base::android::ConvertUTF8ToJavaString(env, info.url.spec()); |
| 137 | ScopedJavaLocalRef<jstring> java_user_title = |
| 138 | base::android::ConvertUTF16ToJavaString(env, info.user_title); |
Michael Thiessen | e01199c94 | 2019-06-25 16:32:53 | [diff] [blame] | 139 | ScopedJavaLocalRef<jstring> java_best_primary_icon_url = |
| 140 | base::android::ConvertUTF8ToJavaString(env, |
| 141 | info.best_primary_icon_url.spec()); |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 142 | ScopedJavaLocalRef<jobject> java_bitmap; |
zpeng | febce761 | 2017-06-07 14:29:04 | [diff] [blame] | 143 | if (!icon_bitmap.drawsNothing()) |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 144 | java_bitmap = gfx::ConvertToJavaBitmap(&icon_bitmap); |
Michael Thiessen | e01199c94 | 2019-06-25 16:32:53 | [diff] [blame] | 145 | TabAndroid* tab = TabAndroid::FromWebContents(web_contents); |
| 146 | Java_ShortcutHelper_addShortcut(env, tab ? tab->GetJavaObject() : nullptr, |
| 147 | java_id, java_url, java_user_title, |
| 148 | java_bitmap, is_icon_maskable, info.source, |
| 149 | java_best_primary_icon_url); |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 150 | } |
| 151 | |
pkotwicz | 9747871 | 2017-03-01 03:39:54 | [diff] [blame] | 152 | } // anonymous namespace |
| 153 | |
| 154 | // static |
Dominick Ng | 203b493 | 2018-02-02 05:32:45 | [diff] [blame] | 155 | std::unique_ptr<ShortcutInfo> ShortcutHelper::CreateShortcutInfo( |
| 156 | const GURL& manifest_url, |
Dmitry Gozman | 88ca5a99 | 2018-05-18 00:13:33 | [diff] [blame] | 157 | const blink::Manifest& manifest, |
Ella Ge | efae3e00 | 2020-02-25 00:53:45 | [diff] [blame] | 158 | const GURL& primary_icon_url) { |
Dominick Ng | 203b493 | 2018-02-02 05:32:45 | [diff] [blame] | 159 | auto shortcut_info = std::make_unique<ShortcutInfo>(GURL()); |
| 160 | if (!manifest.IsEmpty()) { |
| 161 | shortcut_info->UpdateFromManifest(manifest); |
| 162 | shortcut_info->manifest_url = manifest_url; |
| 163 | shortcut_info->best_primary_icon_url = primary_icon_url; |
Dominick Ng | 203b493 | 2018-02-02 05:32:45 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | shortcut_info->ideal_splash_image_size_in_px = GetIdealSplashImageSizeInPx(); |
| 167 | shortcut_info->minimum_splash_image_size_in_px = |
| 168 | GetMinimumSplashImageSizeInPx(); |
| 169 | shortcut_info->splash_image_url = |
Sahel Sharify | c067b74f | 2019-04-17 23:19:07 | [diff] [blame] | 170 | blink::ManifestIconSelector::FindBestMatchingSquareIcon( |
Dominick Ng | 203b493 | 2018-02-02 05:32:45 | [diff] [blame] | 171 | manifest.icons, shortcut_info->ideal_splash_image_size_in_px, |
| 172 | shortcut_info->minimum_splash_image_size_in_px, |
Rayan Kanso | 393cb223 | 2018-06-06 09:34:05 | [diff] [blame] | 173 | blink::Manifest::ImageResource::Purpose::ANY); |
Dominick Ng | 203b493 | 2018-02-02 05:32:45 | [diff] [blame] | 174 | |
| 175 | return shortcut_info; |
| 176 | } |
| 177 | |
| 178 | // static |
pkotwicz | 9747871 | 2017-03-01 03:39:54 | [diff] [blame] | 179 | void ShortcutHelper::AddToLauncherWithSkBitmap( |
pkotwicz | d0d459e | 2017-03-01 22:55:10 | [diff] [blame] | 180 | content::WebContents* web_contents, |
pkotwicz | 9747871 | 2017-03-01 03:39:54 | [diff] [blame] | 181 | const ShortcutInfo& info, |
Peter E Conn | b955ae4a | 2018-12-03 13:02:09 | [diff] [blame] | 182 | const SkBitmap& icon_bitmap, |
| 183 | bool is_icon_maskable) { |
pkotwicz | d0d459e | 2017-03-01 22:55:10 | [diff] [blame] | 184 | std::string webapp_id = base::GenerateGUID(); |
Eric Willigers | 052f043 | 2019-10-04 04:06:57 | [diff] [blame] | 185 | if (info.display == blink::mojom::DisplayMode::kStandalone || |
| 186 | info.display == blink::mojom::DisplayMode::kFullscreen || |
| 187 | info.display == blink::mojom::DisplayMode::kMinimalUi) { |
pkotwicz | d0d459e | 2017-03-01 22:55:10 | [diff] [blame] | 188 | AddWebappWithSkBitmap( |
Peter E Conn | b955ae4a | 2018-12-03 13:02:09 | [diff] [blame] | 189 | info, webapp_id, icon_bitmap, is_icon_maskable, |
Dominick Ng | 40be818 | 2019-03-11 23:21:03 | [diff] [blame] | 190 | base::BindOnce(&ShortcutHelper::FetchSplashScreenImage, web_contents, |
| 191 | info.splash_image_url, |
| 192 | info.ideal_splash_image_size_in_px, |
| 193 | info.minimum_splash_image_size_in_px, webapp_id)); |
pkotwicz | 9747871 | 2017-03-01 03:39:54 | [diff] [blame] | 194 | return; |
| 195 | } |
Michael Thiessen | e01199c94 | 2019-06-25 16:32:53 | [diff] [blame] | 196 | AddShortcutWithSkBitmap(web_contents, info, webapp_id, icon_bitmap, |
| 197 | is_icon_maskable); |
pkotwicz | 9747871 | 2017-03-01 03:39:54 | [diff] [blame] | 198 | } |
| 199 | |
hanxi | 070d10a | 2017-01-09 15:56:50 | [diff] [blame] | 200 | void ShortcutHelper::ShowWebApkInstallInProgressToast() { |
| 201 | Java_ShortcutHelper_showWebApkInstallInProgressToast( |
| 202 | base::android::AttachCurrentThread()); |
| 203 | } |
| 204 | |
zpeng | 5d8fdfc | 2017-01-05 15:45:06 | [diff] [blame] | 205 | int ShortcutHelper::GetIdealHomescreenIconSizeInPx() { |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 206 | if (g_ideal_homescreen_icon_size == -1) |
Rayan Kanso | 3a2f476 | 2020-01-28 13:34:16 | [diff] [blame] | 207 | GetIconSizes(); |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 208 | return g_ideal_homescreen_icon_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::GetMinimumHomescreenIconSizeInPx() { |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 212 | if (g_minimum_homescreen_icon_size == -1) |
Rayan Kanso | 3a2f476 | 2020-01-28 13:34:16 | [diff] [blame] | 213 | GetIconSizes(); |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 214 | return g_minimum_homescreen_icon_size; |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 215 | } |
| 216 | |
zpeng | 5d8fdfc | 2017-01-05 15:45:06 | [diff] [blame] | 217 | int ShortcutHelper::GetIdealSplashImageSizeInPx() { |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 218 | if (g_ideal_splash_image_size == -1) |
Rayan Kanso | 3a2f476 | 2020-01-28 13:34:16 | [diff] [blame] | 219 | GetIconSizes(); |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 220 | return g_ideal_splash_image_size; |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 221 | } |
| 222 | |
zpeng | 5d8fdfc | 2017-01-05 15:45:06 | [diff] [blame] | 223 | int ShortcutHelper::GetMinimumSplashImageSizeInPx() { |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 224 | if (g_minimum_splash_image_size == -1) |
Rayan Kanso | 3a2f476 | 2020-01-28 13:34:16 | [diff] [blame] | 225 | GetIconSizes(); |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 226 | return g_minimum_splash_image_size; |
| 227 | } |
| 228 | |
Henry Jian | 96ec251 | 2019-09-13 00:34:09 | [diff] [blame] | 229 | int ShortcutHelper::GetIdealAdaptiveLauncherIconSizeInPx() { |
| 230 | if (g_ideal_adaptive_launcher_icon_size == -1) |
Rayan Kanso | 3a2f476 | 2020-01-28 13:34:16 | [diff] [blame] | 231 | GetIconSizes(); |
Henry Jian | 96ec251 | 2019-09-13 00:34:09 | [diff] [blame] | 232 | return g_ideal_adaptive_launcher_icon_size; |
| 233 | } |
| 234 | |
Rayan Kanso | 3a2f476 | 2020-01-28 13:34:16 | [diff] [blame] | 235 | int ShortcutHelper::GetIdealShortcutIconSizeInPx() { |
| 236 | if (g_ideal_shortcut_icon_size == -1) |
| 237 | GetIconSizes(); |
| 238 | return g_ideal_shortcut_icon_size; |
| 239 | } |
| 240 | |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 241 | // static |
| 242 | void ShortcutHelper::FetchSplashScreenImage( |
| 243 | content::WebContents* web_contents, |
| 244 | const GURL& image_url, |
zpeng | 5d8fdfc | 2017-01-05 15:45:06 | [diff] [blame] | 245 | const int ideal_splash_image_size_in_px, |
| 246 | const int minimum_splash_image_size_in_px, |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 247 | const std::string& webapp_id) { |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 248 | // This is a fire and forget task. It is not vital for the splash screen image |
| 249 | // to be downloaded so if the downloader returns false there is no fallback. |
jinho.bang | 84cadc8 | 2017-06-14 16:59:34 | [diff] [blame] | 250 | content::ManifestIconDownloader::Download( |
zpeng | 5d8fdfc | 2017-01-05 15:45:06 | [diff] [blame] | 251 | web_contents, image_url, ideal_splash_image_size_in_px, |
| 252 | minimum_splash_image_size_in_px, |
Dominick Ng | 40be818 | 2019-03-11 23:21:03 | [diff] [blame] | 253 | base::BindOnce(&ShortcutHelper::StoreWebappSplashImage, webapp_id)); |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | // static |
zpeng | a7856eef | 2017-02-07 11:42:44 | [diff] [blame] | 257 | void ShortcutHelper::StoreWebappSplashImage(const std::string& webapp_id, |
| 258 | const SkBitmap& splash_image) { |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 259 | if (splash_image.drawsNothing()) |
| 260 | return; |
| 261 | |
| 262 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 263 | ScopedJavaLocalRef<jstring> java_webapp_id = |
| 264 | base::android::ConvertUTF8ToJavaString(env, webapp_id); |
| 265 | ScopedJavaLocalRef<jobject> java_splash_image = |
| 266 | gfx::ConvertToJavaBitmap(&splash_image); |
| 267 | |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame] | 268 | Java_ShortcutHelper_storeWebappSplashImage(env, java_webapp_id, |
| 269 | java_splash_image); |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 270 | } |
| 271 | |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 272 | // static |
pkotwicz | 5774087e | 2016-08-10 17:36:40 | [diff] [blame] | 273 | SkBitmap ShortcutHelper::FinalizeLauncherIconInBackground( |
| 274 | const SkBitmap& bitmap, |
Peter E Conn | b955ae4a | 2018-12-03 13:02:09 | [diff] [blame] | 275 | bool is_icon_maskable, |
pkotwicz | 5774087e | 2016-08-10 17:36:40 | [diff] [blame] | 276 | const GURL& url, |
| 277 | bool* is_generated) { |
Etienne Pierre-doray | 4f2e41bed | 2018-09-24 22:10:11 | [diff] [blame] | 278 | base::AssertLongCPUWorkAllowed(); |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 279 | |
| 280 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 281 | ScopedJavaLocalRef<jobject> result; |
| 282 | *is_generated = false; |
| 283 | |
| 284 | if (!bitmap.isNull()) { |
pkotwicz | 45fc42b6 | 2016-06-07 00:07:10 | [diff] [blame] | 285 | if (Java_ShortcutHelper_isIconLargeEnoughForLauncher(env, bitmap.width(), |
| 286 | bitmap.height())) { |
newt | a584b9e | 2015-10-29 22:29:43 | [diff] [blame] | 287 | ScopedJavaLocalRef<jobject> java_bitmap = |
| 288 | gfx::ConvertToJavaBitmap(&bitmap); |
Peter E Conn | b955ae4a | 2018-12-03 13:02:09 | [diff] [blame] | 289 | result = Java_ShortcutHelper_createHomeScreenIconFromWebIcon( |
| 290 | env, java_bitmap, is_icon_maskable); |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
| 294 | if (result.is_null()) { |
| 295 | ScopedJavaLocalRef<jstring> java_url = |
| 296 | base::android::ConvertUTF8ToJavaString(env, url.spec()); |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 297 | SkColor mean_color = |
| 298 | SkColorSetRGB(g_default_rgb_icon_value, g_default_rgb_icon_value, |
| 299 | g_default_rgb_icon_value); |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 300 | |
| 301 | if (!bitmap.isNull()) |
| 302 | mean_color = color_utils::CalculateKMeanColorOfBitmap(bitmap); |
| 303 | |
| 304 | *is_generated = true; |
newt | a584b9e | 2015-10-29 22:29:43 | [diff] [blame] | 305 | result = Java_ShortcutHelper_generateHomeScreenIcon( |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame] | 306 | env, java_url, SkColorGetR(mean_color), SkColorGetG(mean_color), |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 307 | SkColorGetB(mean_color)); |
| 308 | } |
| 309 | |
pkotwicz | 964382b | 2016-08-04 01:24:55 | [diff] [blame] | 310 | return result.obj() |
torne | d64eb513 | 2016-10-24 12:51:28 | [diff] [blame] | 311 | ? gfx::CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(result)) |
pkotwicz | 964382b | 2016-08-04 01:24:55 | [diff] [blame] | 312 | : SkBitmap(); |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 313 | } |
| 314 | |
pkotwicz | cda82fe | 2016-07-08 18:56:54 | [diff] [blame] | 315 | // static |
Peter Kotwicz | 4b358c16 | 2019-01-03 01:10:58 | [diff] [blame] | 316 | std::string ShortcutHelper::QueryFirstWebApkPackage(const GURL& url) { |
zpeng | 4bb5896 | 2016-10-04 02:42:29 | [diff] [blame] | 317 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 318 | ScopedJavaLocalRef<jstring> java_url = |
| 319 | base::android::ConvertUTF8ToJavaString(env, url.spec()); |
| 320 | ScopedJavaLocalRef<jstring> java_webapk_package_name = |
Peter Kotwicz | 4b358c16 | 2019-01-03 01:10:58 | [diff] [blame] | 321 | Java_ShortcutHelper_queryFirstWebApkPackage(env, java_url); |
zpeng | 4bb5896 | 2016-10-04 02:42:29 | [diff] [blame] | 322 | |
Lei Zhang | 9b0be80 | 2020-05-05 21:11:48 | [diff] [blame] | 323 | std::string webapk_package_name; |
zpeng | 4bb5896 | 2016-10-04 02:42:29 | [diff] [blame] | 324 | if (java_webapk_package_name.obj()) { |
zpeng | a7856eef | 2017-02-07 11:42:44 | [diff] [blame] | 325 | webapk_package_name = |
| 326 | base::android::ConvertJavaStringToUTF8(env, java_webapk_package_name); |
zpeng | 4bb5896 | 2016-10-04 02:42:29 | [diff] [blame] | 327 | } |
| 328 | return webapk_package_name; |
| 329 | } |
| 330 | |
| 331 | // static |
zpeng | a7856eef | 2017-02-07 11:42:44 | [diff] [blame] | 332 | bool ShortcutHelper::IsWebApkInstalled(content::BrowserContext* browser_context, |
Dominick Ng | b1c047e7 | 2020-05-04 00:41:10 | [diff] [blame] | 333 | const GURL& url) { |
| 334 | return !QueryFirstWebApkPackage(url).empty(); |
Dominick Ng | c824482 | 2020-04-29 06:22:30 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | // static |
| 338 | bool ShortcutHelper::DoesOriginContainAnyInstalledWebApk(const GURL& origin) { |
| 339 | DCHECK_EQ(origin, origin.GetOrigin()); |
| 340 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 341 | base::android::ScopedJavaLocalRef<jstring> java_origin = |
| 342 | base::android::ConvertUTF8ToJavaString(env, origin.spec()); |
| 343 | return Java_ShortcutHelper_doesOriginContainAnyInstalledWebApk(env, |
| 344 | java_origin); |
| 345 | } |
| 346 | |
| 347 | bool ShortcutHelper::DoesOriginContainAnyInstalledTrustedWebActivity( |
| 348 | const GURL& origin) { |
| 349 | DCHECK_EQ(origin, origin.GetOrigin()); |
| 350 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 351 | base::android::ScopedJavaLocalRef<jstring> java_origin = |
| 352 | base::android::ConvertUTF8ToJavaString(env, origin.spec()); |
| 353 | return Java_ShortcutHelper_doesOriginContainAnyInstalledTwa(env, java_origin); |
pkotwicz | cda82fe | 2016-07-08 18:56:54 | [diff] [blame] | 354 | } |
| 355 | |
Jarryd | 583dc4d | 2020-06-11 20:31:34 | [diff] [blame^] | 356 | std::set<GURL> ShortcutHelper::GetOriginsWithInstalledWebApksOrTwas() { |
| 357 | std::set<GURL> installed_origins; |
| 358 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 359 | |
| 360 | base::android::ScopedJavaLocalRef<jobjectArray> j_installed_origins = |
| 361 | Java_ShortcutHelper_getOriginsWithInstalledWebApksOrTwas(env); |
| 362 | |
| 363 | if (j_installed_origins) { |
| 364 | std::vector<std::string> installed_origins_list; |
| 365 | base::android::AppendJavaStringArrayToStringVector(env, j_installed_origins, |
| 366 | &installed_origins_list); |
| 367 | for (auto& origin : installed_origins_list) |
| 368 | installed_origins.emplace(GURL(origin)); |
| 369 | } |
| 370 | return installed_origins; |
| 371 | } |
| 372 | |
ckitagawa | 1fa69c0 | 2019-07-24 19:20:44 | [diff] [blame] | 373 | void ShortcutHelper::SetForceWebApkUpdate(const std::string& id) { |
| 374 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 375 | Java_ShortcutHelper_setForceWebApkUpdate( |
| 376 | env, base::android::ConvertUTF8ToJavaString(env, id)); |
gonzalon | 87192d77 | 2017-02-07 22:31:10 | [diff] [blame] | 377 | } |
| 378 | |
Henry Jian | f78dce6 | 2019-09-22 16:58:41 | [diff] [blame] | 379 | // static |
| 380 | bool ShortcutHelper::DoesAndroidSupportMaskableIcons() { |
| 381 | return base::FeatureList::IsEnabled(chrome::android::kWebApkAdaptiveIcon) && |
| 382 | base::android::BuildInfo::GetInstance()->sdk_int() >= |
| 383 | base::android::SDK_VERSION_OREO; |
| 384 | } |
| 385 | |
Rayan Kanso | 3a2f476 | 2020-01-28 13:34:16 | [diff] [blame] | 386 | // static |
| 387 | void ShortcutHelper::SetIdealShortcutSizeForTesting(int size) { |
| 388 | g_ideal_shortcut_icon_size = size; |
| 389 | } |
| 390 | |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 391 | // Callback used by Java when the shortcut has been created. |
Dominick Ng | 40be818 | 2019-03-11 23:21:03 | [diff] [blame] | 392 | // |splash_image_callback| is a pointer to a base::OnceClosure allocated in |
pkotwicz | c67e6ac | 2016-08-12 19:56:44 | [diff] [blame] | 393 | // AddShortcutWithSkBitmap, so reinterpret_cast it back and run it. |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 394 | // |
| 395 | // This callback should only ever be called when the shortcut was for a |
| 396 | // webapp-capable site; otherwise, |splash_image_callback| will have never been |
| 397 | // allocated and doesn't need to be run or deleted. |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 398 | void JNI_ShortcutHelper_OnWebappDataStored(JNIEnv* env, |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 399 | jlong jsplash_image_callback) { |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 400 | DCHECK(jsplash_image_callback); |
Dominick Ng | 40be818 | 2019-03-11 23:21:03 | [diff] [blame] | 401 | base::OnceClosure* splash_image_callback = |
| 402 | reinterpret_cast<base::OnceClosure*>(jsplash_image_callback); |
| 403 | std::move(*splash_image_callback).Run(); |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 404 | delete splash_image_callback; |
| 405 | } |