[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; |
Glen Robertson | 41d3c0f | 2020-07-15 03:42:15 | [diff] [blame] | 48 | int g_ideal_monochrome_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]; |
Glen Robertson | 41d3c0f | 2020-07-15 03:42:15 | [diff] [blame] | 71 | g_ideal_monochrome_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()) |
danakj | 5cc14ce | 2020-11-10 22:00:40 | [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, |
Dave Tapuska | 36eb28d | 2020-07-14 20:59:06 | [diff] [blame] | 121 | is_icon_maskable, static_cast<int>(info.display), |
| 122 | static_cast<int>(info.orientation), info.source, |
| 123 | OptionalSkColorToJavaColor(info.theme_color), |
Matt Jones | 2cd761c | 2019-09-18 18:53:40 | [diff] [blame] | 124 | OptionalSkColorToJavaColor(info.background_color), callback_pointer); |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 125 | } |
benwells | 840ae90 | 2015-02-17 21:13:28 | [diff] [blame] | 126 | |
pkotwicz | 9747871 | 2017-03-01 03:39:54 | [diff] [blame] | 127 | // Adds a shortcut which opens in a browser tab to the launcher. |
Michael Thiessen | e01199c94 | 2019-06-25 16:32:53 | [diff] [blame] | 128 | void AddShortcutWithSkBitmap(content::WebContents* web_contents, |
| 129 | const ShortcutInfo& info, |
pkotwicz | 9747871 | 2017-03-01 03:39:54 | [diff] [blame] | 130 | const std::string& id, |
Peter E Conn | b955ae4a | 2018-12-03 13:02:09 | [diff] [blame] | 131 | const SkBitmap& icon_bitmap, |
| 132 | bool is_icon_maskable) { |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 133 | JNIEnv* env = base::android::AttachCurrentThread(); |
martiw | 12166f5 | 2017-02-20 03:05:45 | [diff] [blame] | 134 | ScopedJavaLocalRef<jstring> java_id = |
| 135 | base::android::ConvertUTF8ToJavaString(env, id); |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 136 | ScopedJavaLocalRef<jstring> java_url = |
| 137 | base::android::ConvertUTF8ToJavaString(env, info.url.spec()); |
| 138 | ScopedJavaLocalRef<jstring> java_user_title = |
| 139 | base::android::ConvertUTF16ToJavaString(env, info.user_title); |
Michael Thiessen | e01199c94 | 2019-06-25 16:32:53 | [diff] [blame] | 140 | ScopedJavaLocalRef<jstring> java_best_primary_icon_url = |
| 141 | base::android::ConvertUTF8ToJavaString(env, |
| 142 | info.best_primary_icon_url.spec()); |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 143 | ScopedJavaLocalRef<jobject> java_bitmap; |
zpeng | febce761 | 2017-06-07 14:29:04 | [diff] [blame] | 144 | if (!icon_bitmap.drawsNothing()) |
danakj | 5cc14ce | 2020-11-10 22:00:40 | [diff] [blame^] | 145 | java_bitmap = gfx::ConvertToJavaBitmap(icon_bitmap); |
Evan Stade | d7392a79 | 2020-07-16 20:59:21 | [diff] [blame] | 146 | Java_ShortcutHelper_addShortcut(env, java_id, java_url, java_user_title, |
Michael Thiessen | e01199c94 | 2019-06-25 16:32:53 | [diff] [blame] | 147 | java_bitmap, is_icon_maskable, info.source, |
| 148 | java_best_primary_icon_url); |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 149 | } |
| 150 | |
pkotwicz | 9747871 | 2017-03-01 03:39:54 | [diff] [blame] | 151 | } // anonymous namespace |
| 152 | |
| 153 | // static |
Dominick Ng | 203b493 | 2018-02-02 05:32:45 | [diff] [blame] | 154 | std::unique_ptr<ShortcutInfo> ShortcutHelper::CreateShortcutInfo( |
| 155 | const GURL& manifest_url, |
Dmitry Gozman | 88ca5a99 | 2018-05-18 00:13:33 | [diff] [blame] | 156 | const blink::Manifest& manifest, |
Ella Ge | efae3e00 | 2020-02-25 00:53:45 | [diff] [blame] | 157 | const GURL& primary_icon_url) { |
Dominick Ng | 203b493 | 2018-02-02 05:32:45 | [diff] [blame] | 158 | auto shortcut_info = std::make_unique<ShortcutInfo>(GURL()); |
| 159 | if (!manifest.IsEmpty()) { |
| 160 | shortcut_info->UpdateFromManifest(manifest); |
| 161 | shortcut_info->manifest_url = manifest_url; |
| 162 | shortcut_info->best_primary_icon_url = primary_icon_url; |
Dominick Ng | 203b493 | 2018-02-02 05:32:45 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | shortcut_info->ideal_splash_image_size_in_px = GetIdealSplashImageSizeInPx(); |
| 166 | shortcut_info->minimum_splash_image_size_in_px = |
| 167 | GetMinimumSplashImageSizeInPx(); |
| 168 | shortcut_info->splash_image_url = |
Sahel Sharify | c067b74f | 2019-04-17 23:19:07 | [diff] [blame] | 169 | blink::ManifestIconSelector::FindBestMatchingSquareIcon( |
Dominick Ng | 203b493 | 2018-02-02 05:32:45 | [diff] [blame] | 170 | manifest.icons, shortcut_info->ideal_splash_image_size_in_px, |
| 171 | shortcut_info->minimum_splash_image_size_in_px, |
Gyuyoung Kim | 361bbfe | 2020-10-28 00:21:10 | [diff] [blame] | 172 | blink::mojom::ManifestImageResource_Purpose::ANY); |
Dominick Ng | 203b493 | 2018-02-02 05:32:45 | [diff] [blame] | 173 | |
| 174 | return shortcut_info; |
| 175 | } |
| 176 | |
| 177 | // static |
pkotwicz | 9747871 | 2017-03-01 03:39:54 | [diff] [blame] | 178 | void ShortcutHelper::AddToLauncherWithSkBitmap( |
pkotwicz | d0d459e | 2017-03-01 22:55:10 | [diff] [blame] | 179 | content::WebContents* web_contents, |
pkotwicz | 9747871 | 2017-03-01 03:39:54 | [diff] [blame] | 180 | const ShortcutInfo& info, |
Peter E Conn | b955ae4a | 2018-12-03 13:02:09 | [diff] [blame] | 181 | const SkBitmap& icon_bitmap, |
| 182 | bool is_icon_maskable) { |
pkotwicz | d0d459e | 2017-03-01 22:55:10 | [diff] [blame] | 183 | std::string webapp_id = base::GenerateGUID(); |
Eric Willigers | 052f043 | 2019-10-04 04:06:57 | [diff] [blame] | 184 | if (info.display == blink::mojom::DisplayMode::kStandalone || |
| 185 | info.display == blink::mojom::DisplayMode::kFullscreen || |
| 186 | info.display == blink::mojom::DisplayMode::kMinimalUi) { |
pkotwicz | d0d459e | 2017-03-01 22:55:10 | [diff] [blame] | 187 | AddWebappWithSkBitmap( |
Peter E Conn | b955ae4a | 2018-12-03 13:02:09 | [diff] [blame] | 188 | info, webapp_id, icon_bitmap, is_icon_maskable, |
Dominick Ng | 40be818 | 2019-03-11 23:21:03 | [diff] [blame] | 189 | base::BindOnce(&ShortcutHelper::FetchSplashScreenImage, web_contents, |
| 190 | info.splash_image_url, |
| 191 | info.ideal_splash_image_size_in_px, |
| 192 | info.minimum_splash_image_size_in_px, webapp_id)); |
pkotwicz | 9747871 | 2017-03-01 03:39:54 | [diff] [blame] | 193 | return; |
| 194 | } |
Michael Thiessen | e01199c94 | 2019-06-25 16:32:53 | [diff] [blame] | 195 | AddShortcutWithSkBitmap(web_contents, info, webapp_id, icon_bitmap, |
| 196 | is_icon_maskable); |
pkotwicz | 9747871 | 2017-03-01 03:39:54 | [diff] [blame] | 197 | } |
| 198 | |
hanxi | 070d10a | 2017-01-09 15:56:50 | [diff] [blame] | 199 | void ShortcutHelper::ShowWebApkInstallInProgressToast() { |
| 200 | Java_ShortcutHelper_showWebApkInstallInProgressToast( |
| 201 | base::android::AttachCurrentThread()); |
| 202 | } |
| 203 | |
zpeng | 5d8fdfc | 2017-01-05 15:45:06 | [diff] [blame] | 204 | int ShortcutHelper::GetIdealHomescreenIconSizeInPx() { |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 205 | if (g_ideal_homescreen_icon_size == -1) |
Rayan Kanso | 3a2f476 | 2020-01-28 13:34:16 | [diff] [blame] | 206 | GetIconSizes(); |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 207 | return g_ideal_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::GetMinimumHomescreenIconSizeInPx() { |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 211 | if (g_minimum_homescreen_icon_size == -1) |
Rayan Kanso | 3a2f476 | 2020-01-28 13:34:16 | [diff] [blame] | 212 | GetIconSizes(); |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 213 | return g_minimum_homescreen_icon_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::GetIdealSplashImageSizeInPx() { |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 217 | if (g_ideal_splash_image_size == -1) |
Rayan Kanso | 3a2f476 | 2020-01-28 13:34:16 | [diff] [blame] | 218 | GetIconSizes(); |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 219 | return g_ideal_splash_image_size; |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 220 | } |
| 221 | |
zpeng | 5d8fdfc | 2017-01-05 15:45:06 | [diff] [blame] | 222 | int ShortcutHelper::GetMinimumSplashImageSizeInPx() { |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 223 | if (g_minimum_splash_image_size == -1) |
Rayan Kanso | 3a2f476 | 2020-01-28 13:34:16 | [diff] [blame] | 224 | GetIconSizes(); |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 225 | return g_minimum_splash_image_size; |
| 226 | } |
| 227 | |
Henry Jian | 96ec251 | 2019-09-13 00:34:09 | [diff] [blame] | 228 | int ShortcutHelper::GetIdealAdaptiveLauncherIconSizeInPx() { |
| 229 | if (g_ideal_adaptive_launcher_icon_size == -1) |
Rayan Kanso | 3a2f476 | 2020-01-28 13:34:16 | [diff] [blame] | 230 | GetIconSizes(); |
Henry Jian | 96ec251 | 2019-09-13 00:34:09 | [diff] [blame] | 231 | return g_ideal_adaptive_launcher_icon_size; |
| 232 | } |
| 233 | |
Rayan Kanso | 3a2f476 | 2020-01-28 13:34:16 | [diff] [blame] | 234 | int ShortcutHelper::GetIdealShortcutIconSizeInPx() { |
| 235 | if (g_ideal_shortcut_icon_size == -1) |
| 236 | GetIconSizes(); |
| 237 | return g_ideal_shortcut_icon_size; |
| 238 | } |
| 239 | |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 240 | // static |
| 241 | void ShortcutHelper::FetchSplashScreenImage( |
| 242 | content::WebContents* web_contents, |
| 243 | const GURL& image_url, |
zpeng | 5d8fdfc | 2017-01-05 15:45:06 | [diff] [blame] | 244 | const int ideal_splash_image_size_in_px, |
| 245 | const int minimum_splash_image_size_in_px, |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 246 | const std::string& webapp_id) { |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 247 | // This is a fire and forget task. It is not vital for the splash screen image |
| 248 | // to be downloaded so if the downloader returns false there is no fallback. |
jinho.bang | 84cadc8 | 2017-06-14 16:59:34 | [diff] [blame] | 249 | content::ManifestIconDownloader::Download( |
zpeng | 5d8fdfc | 2017-01-05 15:45:06 | [diff] [blame] | 250 | web_contents, image_url, ideal_splash_image_size_in_px, |
| 251 | minimum_splash_image_size_in_px, |
Dominick Ng | 40be818 | 2019-03-11 23:21:03 | [diff] [blame] | 252 | base::BindOnce(&ShortcutHelper::StoreWebappSplashImage, webapp_id)); |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | // static |
zpeng | a7856eef | 2017-02-07 11:42:44 | [diff] [blame] | 256 | void ShortcutHelper::StoreWebappSplashImage(const std::string& webapp_id, |
| 257 | const SkBitmap& splash_image) { |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 258 | if (splash_image.drawsNothing()) |
| 259 | return; |
| 260 | |
| 261 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 262 | ScopedJavaLocalRef<jstring> java_webapp_id = |
| 263 | base::android::ConvertUTF8ToJavaString(env, webapp_id); |
| 264 | ScopedJavaLocalRef<jobject> java_splash_image = |
danakj | 5cc14ce | 2020-11-10 22:00:40 | [diff] [blame^] | 265 | gfx::ConvertToJavaBitmap(splash_image); |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 266 | |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame] | 267 | Java_ShortcutHelper_storeWebappSplashImage(env, java_webapp_id, |
| 268 | java_splash_image); |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 269 | } |
| 270 | |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 271 | // static |
pkotwicz | 5774087e | 2016-08-10 17:36:40 | [diff] [blame] | 272 | SkBitmap ShortcutHelper::FinalizeLauncherIconInBackground( |
| 273 | const SkBitmap& bitmap, |
Peter E Conn | b955ae4a | 2018-12-03 13:02:09 | [diff] [blame] | 274 | bool is_icon_maskable, |
pkotwicz | 5774087e | 2016-08-10 17:36:40 | [diff] [blame] | 275 | const GURL& url, |
| 276 | bool* is_generated) { |
Etienne Pierre-doray | 4f2e41bed | 2018-09-24 22:10:11 | [diff] [blame] | 277 | base::AssertLongCPUWorkAllowed(); |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 278 | |
| 279 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 280 | ScopedJavaLocalRef<jobject> result; |
| 281 | *is_generated = false; |
| 282 | |
| 283 | if (!bitmap.isNull()) { |
pkotwicz | 45fc42b6 | 2016-06-07 00:07:10 | [diff] [blame] | 284 | if (Java_ShortcutHelper_isIconLargeEnoughForLauncher(env, bitmap.width(), |
| 285 | bitmap.height())) { |
newt | a584b9e | 2015-10-29 22:29:43 | [diff] [blame] | 286 | ScopedJavaLocalRef<jobject> java_bitmap = |
danakj | 5cc14ce | 2020-11-10 22:00:40 | [diff] [blame^] | 287 | gfx::ConvertToJavaBitmap(bitmap); |
Peter E Conn | b955ae4a | 2018-12-03 13:02:09 | [diff] [blame] | 288 | result = Java_ShortcutHelper_createHomeScreenIconFromWebIcon( |
| 289 | env, java_bitmap, is_icon_maskable); |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
| 293 | if (result.is_null()) { |
| 294 | ScopedJavaLocalRef<jstring> java_url = |
| 295 | base::android::ConvertUTF8ToJavaString(env, url.spec()); |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 296 | SkColor mean_color = |
| 297 | SkColorSetRGB(g_default_rgb_icon_value, g_default_rgb_icon_value, |
| 298 | g_default_rgb_icon_value); |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 299 | |
| 300 | if (!bitmap.isNull()) |
| 301 | mean_color = color_utils::CalculateKMeanColorOfBitmap(bitmap); |
| 302 | |
| 303 | *is_generated = true; |
newt | a584b9e | 2015-10-29 22:29:43 | [diff] [blame] | 304 | result = Java_ShortcutHelper_generateHomeScreenIcon( |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame] | 305 | env, java_url, SkColorGetR(mean_color), SkColorGetG(mean_color), |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 306 | SkColorGetB(mean_color)); |
| 307 | } |
| 308 | |
pkotwicz | 964382b | 2016-08-04 01:24:55 | [diff] [blame] | 309 | return result.obj() |
torne | d64eb513 | 2016-10-24 12:51:28 | [diff] [blame] | 310 | ? gfx::CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(result)) |
pkotwicz | 964382b | 2016-08-04 01:24:55 | [diff] [blame] | 311 | : SkBitmap(); |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 312 | } |
| 313 | |
pkotwicz | cda82fe | 2016-07-08 18:56:54 | [diff] [blame] | 314 | // static |
Peter Kotwicz | 4b358c16 | 2019-01-03 01:10:58 | [diff] [blame] | 315 | std::string ShortcutHelper::QueryFirstWebApkPackage(const GURL& url) { |
zpeng | 4bb5896 | 2016-10-04 02:42:29 | [diff] [blame] | 316 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 317 | ScopedJavaLocalRef<jstring> java_url = |
| 318 | base::android::ConvertUTF8ToJavaString(env, url.spec()); |
| 319 | ScopedJavaLocalRef<jstring> java_webapk_package_name = |
Peter Kotwicz | 4b358c16 | 2019-01-03 01:10:58 | [diff] [blame] | 320 | Java_ShortcutHelper_queryFirstWebApkPackage(env, java_url); |
zpeng | 4bb5896 | 2016-10-04 02:42:29 | [diff] [blame] | 321 | |
Lei Zhang | 9b0be80 | 2020-05-05 21:11:48 | [diff] [blame] | 322 | std::string webapk_package_name; |
zpeng | 4bb5896 | 2016-10-04 02:42:29 | [diff] [blame] | 323 | if (java_webapk_package_name.obj()) { |
zpeng | a7856eef | 2017-02-07 11:42:44 | [diff] [blame] | 324 | webapk_package_name = |
| 325 | base::android::ConvertJavaStringToUTF8(env, java_webapk_package_name); |
zpeng | 4bb5896 | 2016-10-04 02:42:29 | [diff] [blame] | 326 | } |
| 327 | return webapk_package_name; |
| 328 | } |
| 329 | |
| 330 | // static |
zpeng | a7856eef | 2017-02-07 11:42:44 | [diff] [blame] | 331 | bool ShortcutHelper::IsWebApkInstalled(content::BrowserContext* browser_context, |
Dominick Ng | b1c047e7 | 2020-05-04 00:41:10 | [diff] [blame] | 332 | const GURL& url) { |
| 333 | return !QueryFirstWebApkPackage(url).empty(); |
Dominick Ng | c824482 | 2020-04-29 06:22:30 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | // static |
| 337 | bool ShortcutHelper::DoesOriginContainAnyInstalledWebApk(const GURL& origin) { |
| 338 | DCHECK_EQ(origin, origin.GetOrigin()); |
| 339 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 340 | base::android::ScopedJavaLocalRef<jstring> java_origin = |
| 341 | base::android::ConvertUTF8ToJavaString(env, origin.spec()); |
| 342 | return Java_ShortcutHelper_doesOriginContainAnyInstalledWebApk(env, |
| 343 | java_origin); |
| 344 | } |
| 345 | |
| 346 | bool ShortcutHelper::DoesOriginContainAnyInstalledTrustedWebActivity( |
| 347 | const GURL& origin) { |
| 348 | DCHECK_EQ(origin, origin.GetOrigin()); |
| 349 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 350 | base::android::ScopedJavaLocalRef<jstring> java_origin = |
| 351 | base::android::ConvertUTF8ToJavaString(env, origin.spec()); |
| 352 | return Java_ShortcutHelper_doesOriginContainAnyInstalledTwa(env, java_origin); |
pkotwicz | cda82fe | 2016-07-08 18:56:54 | [diff] [blame] | 353 | } |
| 354 | |
Jarryd | 583dc4d | 2020-06-11 20:31:34 | [diff] [blame] | 355 | std::set<GURL> ShortcutHelper::GetOriginsWithInstalledWebApksOrTwas() { |
| 356 | std::set<GURL> installed_origins; |
| 357 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 358 | |
| 359 | base::android::ScopedJavaLocalRef<jobjectArray> j_installed_origins = |
| 360 | Java_ShortcutHelper_getOriginsWithInstalledWebApksOrTwas(env); |
| 361 | |
| 362 | if (j_installed_origins) { |
| 363 | std::vector<std::string> installed_origins_list; |
| 364 | base::android::AppendJavaStringArrayToStringVector(env, j_installed_origins, |
| 365 | &installed_origins_list); |
| 366 | for (auto& origin : installed_origins_list) |
| 367 | installed_origins.emplace(GURL(origin)); |
| 368 | } |
| 369 | return installed_origins; |
| 370 | } |
| 371 | |
ckitagawa | 1fa69c0 | 2019-07-24 19:20:44 | [diff] [blame] | 372 | void ShortcutHelper::SetForceWebApkUpdate(const std::string& id) { |
| 373 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 374 | Java_ShortcutHelper_setForceWebApkUpdate( |
| 375 | env, base::android::ConvertUTF8ToJavaString(env, id)); |
gonzalon | 87192d77 | 2017-02-07 22:31:10 | [diff] [blame] | 376 | } |
| 377 | |
Henry Jian | f78dce6 | 2019-09-22 16:58:41 | [diff] [blame] | 378 | // static |
| 379 | bool ShortcutHelper::DoesAndroidSupportMaskableIcons() { |
| 380 | return base::FeatureList::IsEnabled(chrome::android::kWebApkAdaptiveIcon) && |
| 381 | base::android::BuildInfo::GetInstance()->sdk_int() >= |
| 382 | base::android::SDK_VERSION_OREO; |
| 383 | } |
| 384 | |
Rayan Kanso | 3a2f476 | 2020-01-28 13:34:16 | [diff] [blame] | 385 | // static |
| 386 | void ShortcutHelper::SetIdealShortcutSizeForTesting(int size) { |
| 387 | g_ideal_shortcut_icon_size = size; |
| 388 | } |
| 389 | |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 390 | // Callback used by Java when the shortcut has been created. |
Dominick Ng | 40be818 | 2019-03-11 23:21:03 | [diff] [blame] | 391 | // |splash_image_callback| is a pointer to a base::OnceClosure allocated in |
pkotwicz | c67e6ac | 2016-08-12 19:56:44 | [diff] [blame] | 392 | // AddShortcutWithSkBitmap, so reinterpret_cast it back and run it. |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 393 | // |
| 394 | // This callback should only ever be called when the shortcut was for a |
| 395 | // webapp-capable site; otherwise, |splash_image_callback| will have never been |
| 396 | // allocated and doesn't need to be run or deleted. |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 397 | void JNI_ShortcutHelper_OnWebappDataStored(JNIEnv* env, |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 398 | jlong jsplash_image_callback) { |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 399 | DCHECK(jsplash_image_callback); |
Dominick Ng | 40be818 | 2019-03-11 23:21:03 | [diff] [blame] | 400 | base::OnceClosure* splash_image_callback = |
| 401 | reinterpret_cast<base::OnceClosure*>(jsplash_image_callback); |
| 402 | std::move(*splash_image_callback).Run(); |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 403 | delete splash_image_callback; |
| 404 | } |