[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" |
Andrew Grieve | 4a42c22e | 2019-06-24 16:14:29 | [diff] [blame] | 20 | #include "chrome/android/chrome_jni_headers/ShortcutHelper_jni.h" |
Daniel Cheng | 8fedab2 | 2018-05-17 00:39:27 | [diff] [blame] | 21 | #include "chrome/browser/android/color_helpers.h" |
Michael Thiessen | e01199c94 | 2019-06-25 16:32:53 | [diff] [blame] | 22 | #include "chrome/browser/android/tab_android.h" |
hanxi | 563f353 | 2016-08-19 20:09:01 | [diff] [blame] | 23 | #include "chrome/browser/android/webapk/chrome_webapk_host.h" |
hanxi | 070d10a | 2017-01-09 15:56:50 | [diff] [blame] | 24 | #include "chrome/browser/android/webapk/webapk_install_service.h" |
pkotwicz | f4dddb3 | 2017-02-25 01:21:55 | [diff] [blame] | 25 | #include "chrome/browser/android/webapk/webapk_metrics.h" |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 26 | #include "chrome/common/chrome_switches.h" |
dfalcantara | aec56da | 2015-05-06 03:33:56 | [diff] [blame] | 27 | #include "content/public/browser/browser_thread.h" |
jinho.bang | 84cadc8 | 2017-06-14 16:59:34 | [diff] [blame] | 28 | #include "content/public/browser/manifest_icon_downloader.h" |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 29 | #include "content/public/browser/web_contents.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()); |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 101 | ScopedJavaLocalRef<jobject> java_bitmap; |
zpeng | febce761 | 2017-06-07 14:29:04 | [diff] [blame] | 102 | if (!icon_bitmap.drawsNothing()) |
mlamouri | c679bbf | 2014-09-24 21:24:49 | [diff] [blame] | 103 | java_bitmap = gfx::ConvertToJavaBitmap(&icon_bitmap); |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 104 | |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 105 | // The callback will need to be run after shortcut creation completes in order |
| 106 | // to download the splash image and save it to the WebappDataStorage. Create a |
| 107 | // copy of the callback here and send the pointer to Java, which will send it |
| 108 | // back once the asynchronous shortcut creation process finishes. |
Dominick Ng | 40be818 | 2019-03-11 23:21:03 | [diff] [blame] | 109 | uintptr_t callback_pointer = reinterpret_cast<uintptr_t>( |
| 110 | new base::OnceClosure(std::move(splash_image_callback))); |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 111 | |
zpeng | a7856eef | 2017-02-07 11:42:44 | [diff] [blame] | 112 | Java_ShortcutHelper_addWebapp( |
| 113 | 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] | 114 | java_short_name, java_best_primary_icon_url, java_bitmap, |
| 115 | is_icon_maskable, info.display, info.orientation, info.source, |
Daniel Cheng | 8fedab2 | 2018-05-17 00:39:27 | [diff] [blame] | 116 | OptionalSkColorToJavaColor(info.theme_color), |
Michael Thiessen | e01199c94 | 2019-06-25 16:32:53 | [diff] [blame] | 117 | OptionalSkColorToJavaColor(info.background_color), callback_pointer, |
| 118 | false /* isShortcutAsWebapp */); |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 119 | } |
benwells | 840ae90 | 2015-02-17 21:13:28 | [diff] [blame] | 120 | |
pkotwicz | 9747871 | 2017-03-01 03:39:54 | [diff] [blame] | 121 | // Adds a shortcut which opens in a browser tab to the launcher. |
Michael Thiessen | e01199c94 | 2019-06-25 16:32:53 | [diff] [blame] | 122 | void AddShortcutWithSkBitmap(content::WebContents* web_contents, |
| 123 | const ShortcutInfo& info, |
pkotwicz | 9747871 | 2017-03-01 03:39:54 | [diff] [blame] | 124 | const std::string& id, |
Peter E Conn | b955ae4a | 2018-12-03 13:02:09 | [diff] [blame] | 125 | const SkBitmap& icon_bitmap, |
| 126 | bool is_icon_maskable) { |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 127 | JNIEnv* env = base::android::AttachCurrentThread(); |
martiw | 12166f5 | 2017-02-20 03:05:45 | [diff] [blame] | 128 | ScopedJavaLocalRef<jstring> java_id = |
| 129 | base::android::ConvertUTF8ToJavaString(env, id); |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 130 | ScopedJavaLocalRef<jstring> java_url = |
| 131 | base::android::ConvertUTF8ToJavaString(env, info.url.spec()); |
| 132 | ScopedJavaLocalRef<jstring> java_user_title = |
| 133 | base::android::ConvertUTF16ToJavaString(env, info.user_title); |
Michael Thiessen | e01199c94 | 2019-06-25 16:32:53 | [diff] [blame] | 134 | ScopedJavaLocalRef<jstring> java_best_primary_icon_url = |
| 135 | base::android::ConvertUTF8ToJavaString(env, |
| 136 | info.best_primary_icon_url.spec()); |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 137 | ScopedJavaLocalRef<jobject> java_bitmap; |
zpeng | febce761 | 2017-06-07 14:29:04 | [diff] [blame] | 138 | if (!icon_bitmap.drawsNothing()) |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 139 | java_bitmap = gfx::ConvertToJavaBitmap(&icon_bitmap); |
Michael Thiessen | e01199c94 | 2019-06-25 16:32:53 | [diff] [blame] | 140 | TabAndroid* tab = TabAndroid::FromWebContents(web_contents); |
| 141 | Java_ShortcutHelper_addShortcut(env, tab ? tab->GetJavaObject() : nullptr, |
| 142 | java_id, java_url, java_user_title, |
| 143 | java_bitmap, is_icon_maskable, info.source, |
| 144 | java_best_primary_icon_url); |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 145 | } |
| 146 | |
pkotwicz | 9747871 | 2017-03-01 03:39:54 | [diff] [blame] | 147 | } // anonymous namespace |
| 148 | |
| 149 | // static |
Dominick Ng | 203b493 | 2018-02-02 05:32:45 | [diff] [blame] | 150 | std::unique_ptr<ShortcutInfo> ShortcutHelper::CreateShortcutInfo( |
| 151 | const GURL& manifest_url, |
Dmitry Gozman | 88ca5a99 | 2018-05-18 00:13:33 | [diff] [blame] | 152 | const blink::Manifest& manifest, |
Dominick Ng | 203b493 | 2018-02-02 05:32:45 | [diff] [blame] | 153 | const GURL& primary_icon_url, |
| 154 | const GURL& badge_icon_url) { |
| 155 | auto shortcut_info = std::make_unique<ShortcutInfo>(GURL()); |
| 156 | if (!manifest.IsEmpty()) { |
| 157 | shortcut_info->UpdateFromManifest(manifest); |
| 158 | shortcut_info->manifest_url = manifest_url; |
| 159 | shortcut_info->best_primary_icon_url = primary_icon_url; |
| 160 | shortcut_info->best_badge_icon_url = badge_icon_url; |
| 161 | } |
| 162 | |
| 163 | shortcut_info->ideal_splash_image_size_in_px = GetIdealSplashImageSizeInPx(); |
| 164 | shortcut_info->minimum_splash_image_size_in_px = |
| 165 | GetMinimumSplashImageSizeInPx(); |
| 166 | shortcut_info->splash_image_url = |
Sahel Sharify | c067b74f | 2019-04-17 23:19:07 | [diff] [blame] | 167 | blink::ManifestIconSelector::FindBestMatchingSquareIcon( |
Dominick Ng | 203b493 | 2018-02-02 05:32:45 | [diff] [blame] | 168 | manifest.icons, shortcut_info->ideal_splash_image_size_in_px, |
| 169 | shortcut_info->minimum_splash_image_size_in_px, |
Rayan Kanso | 393cb223 | 2018-06-06 09:34:05 | [diff] [blame] | 170 | blink::Manifest::ImageResource::Purpose::ANY); |
Dominick Ng | 203b493 | 2018-02-02 05:32:45 | [diff] [blame] | 171 | |
| 172 | return shortcut_info; |
| 173 | } |
| 174 | |
| 175 | // static |
pkotwicz | 9747871 | 2017-03-01 03:39:54 | [diff] [blame] | 176 | void ShortcutHelper::AddToLauncherWithSkBitmap( |
pkotwicz | d0d459e | 2017-03-01 22:55:10 | [diff] [blame] | 177 | content::WebContents* web_contents, |
pkotwicz | 9747871 | 2017-03-01 03:39:54 | [diff] [blame] | 178 | const ShortcutInfo& info, |
Peter E Conn | b955ae4a | 2018-12-03 13:02:09 | [diff] [blame] | 179 | const SkBitmap& icon_bitmap, |
| 180 | bool is_icon_maskable) { |
pkotwicz | d0d459e | 2017-03-01 22:55:10 | [diff] [blame] | 181 | std::string webapp_id = base::GenerateGUID(); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 182 | if (info.display == blink::kWebDisplayModeStandalone || |
Piotr Swigon | 549c043 | 2017-08-16 02:48:41 | [diff] [blame] | 183 | info.display == blink::kWebDisplayModeFullscreen || |
Piotr Swigon | 57e79c5c | 2017-12-29 01:11:15 | [diff] [blame] | 184 | info.display == blink::kWebDisplayModeMinimalUi) { |
pkotwicz | d0d459e | 2017-03-01 22:55:10 | [diff] [blame] | 185 | AddWebappWithSkBitmap( |
Peter E Conn | b955ae4a | 2018-12-03 13:02:09 | [diff] [blame] | 186 | info, webapp_id, icon_bitmap, is_icon_maskable, |
Dominick Ng | 40be818 | 2019-03-11 23:21:03 | [diff] [blame] | 187 | base::BindOnce(&ShortcutHelper::FetchSplashScreenImage, web_contents, |
| 188 | info.splash_image_url, |
| 189 | info.ideal_splash_image_size_in_px, |
| 190 | info.minimum_splash_image_size_in_px, webapp_id)); |
pkotwicz | 9747871 | 2017-03-01 03:39:54 | [diff] [blame] | 191 | return; |
| 192 | } |
Michael Thiessen | e01199c94 | 2019-06-25 16:32:53 | [diff] [blame] | 193 | AddShortcutWithSkBitmap(web_contents, info, webapp_id, icon_bitmap, |
| 194 | is_icon_maskable); |
pkotwicz | 9747871 | 2017-03-01 03:39:54 | [diff] [blame] | 195 | } |
| 196 | |
hanxi | 070d10a | 2017-01-09 15:56:50 | [diff] [blame] | 197 | void ShortcutHelper::ShowWebApkInstallInProgressToast() { |
| 198 | Java_ShortcutHelper_showWebApkInstallInProgressToast( |
| 199 | base::android::AttachCurrentThread()); |
| 200 | } |
| 201 | |
zpeng | 5d8fdfc | 2017-01-05 15:45:06 | [diff] [blame] | 202 | int ShortcutHelper::GetIdealHomescreenIconSizeInPx() { |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 203 | if (g_ideal_homescreen_icon_size == -1) |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 204 | GetHomescreenIconAndSplashImageSizes(); |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 205 | return g_ideal_homescreen_icon_size; |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 206 | } |
| 207 | |
zpeng | 5d8fdfc | 2017-01-05 15:45:06 | [diff] [blame] | 208 | int ShortcutHelper::GetMinimumHomescreenIconSizeInPx() { |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 209 | if (g_minimum_homescreen_icon_size == -1) |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 210 | GetHomescreenIconAndSplashImageSizes(); |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 211 | return g_minimum_homescreen_icon_size; |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 212 | } |
| 213 | |
zpeng | 5d8fdfc | 2017-01-05 15:45:06 | [diff] [blame] | 214 | int ShortcutHelper::GetIdealSplashImageSizeInPx() { |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 215 | if (g_ideal_splash_image_size == -1) |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 216 | GetHomescreenIconAndSplashImageSizes(); |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 217 | return g_ideal_splash_image_size; |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 218 | } |
| 219 | |
zpeng | 5d8fdfc | 2017-01-05 15:45:06 | [diff] [blame] | 220 | int ShortcutHelper::GetMinimumSplashImageSizeInPx() { |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 221 | if (g_minimum_splash_image_size == -1) |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 222 | GetHomescreenIconAndSplashImageSizes(); |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 223 | return g_minimum_splash_image_size; |
| 224 | } |
| 225 | |
| 226 | int ShortcutHelper::GetIdealBadgeIconSizeInPx() { |
| 227 | if (g_ideal_badge_icon_size == -1) |
| 228 | GetHomescreenIconAndSplashImageSizes(); |
| 229 | return g_ideal_badge_icon_size; |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 230 | } |
| 231 | |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 232 | // static |
| 233 | void ShortcutHelper::FetchSplashScreenImage( |
| 234 | content::WebContents* web_contents, |
| 235 | const GURL& image_url, |
zpeng | 5d8fdfc | 2017-01-05 15:45:06 | [diff] [blame] | 236 | const int ideal_splash_image_size_in_px, |
| 237 | const int minimum_splash_image_size_in_px, |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 238 | const std::string& webapp_id) { |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 239 | // This is a fire and forget task. It is not vital for the splash screen image |
| 240 | // to be downloaded so if the downloader returns false there is no fallback. |
jinho.bang | 84cadc8 | 2017-06-14 16:59:34 | [diff] [blame] | 241 | content::ManifestIconDownloader::Download( |
zpeng | 5d8fdfc | 2017-01-05 15:45:06 | [diff] [blame] | 242 | web_contents, image_url, ideal_splash_image_size_in_px, |
| 243 | minimum_splash_image_size_in_px, |
Dominick Ng | 40be818 | 2019-03-11 23:21:03 | [diff] [blame] | 244 | base::BindOnce(&ShortcutHelper::StoreWebappSplashImage, webapp_id)); |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | // static |
zpeng | a7856eef | 2017-02-07 11:42:44 | [diff] [blame] | 248 | void ShortcutHelper::StoreWebappSplashImage(const std::string& webapp_id, |
| 249 | const SkBitmap& splash_image) { |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 250 | if (splash_image.drawsNothing()) |
| 251 | return; |
| 252 | |
| 253 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 254 | ScopedJavaLocalRef<jstring> java_webapp_id = |
| 255 | base::android::ConvertUTF8ToJavaString(env, webapp_id); |
| 256 | ScopedJavaLocalRef<jobject> java_splash_image = |
| 257 | gfx::ConvertToJavaBitmap(&splash_image); |
| 258 | |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame] | 259 | Java_ShortcutHelper_storeWebappSplashImage(env, java_webapp_id, |
| 260 | java_splash_image); |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 261 | } |
| 262 | |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 263 | // static |
pkotwicz | 5774087e | 2016-08-10 17:36:40 | [diff] [blame] | 264 | SkBitmap ShortcutHelper::FinalizeLauncherIconInBackground( |
| 265 | const SkBitmap& bitmap, |
Peter E Conn | b955ae4a | 2018-12-03 13:02:09 | [diff] [blame] | 266 | bool is_icon_maskable, |
pkotwicz | 5774087e | 2016-08-10 17:36:40 | [diff] [blame] | 267 | const GURL& url, |
| 268 | bool* is_generated) { |
Etienne Pierre-doray | 4f2e41bed | 2018-09-24 22:10:11 | [diff] [blame] | 269 | base::AssertLongCPUWorkAllowed(); |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 270 | |
| 271 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 272 | ScopedJavaLocalRef<jobject> result; |
| 273 | *is_generated = false; |
| 274 | |
| 275 | if (!bitmap.isNull()) { |
pkotwicz | 45fc42b6 | 2016-06-07 00:07:10 | [diff] [blame] | 276 | if (Java_ShortcutHelper_isIconLargeEnoughForLauncher(env, bitmap.width(), |
| 277 | bitmap.height())) { |
newt | a584b9e | 2015-10-29 22:29:43 | [diff] [blame] | 278 | ScopedJavaLocalRef<jobject> java_bitmap = |
| 279 | gfx::ConvertToJavaBitmap(&bitmap); |
Peter E Conn | b955ae4a | 2018-12-03 13:02:09 | [diff] [blame] | 280 | result = Java_ShortcutHelper_createHomeScreenIconFromWebIcon( |
| 281 | env, java_bitmap, is_icon_maskable); |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
| 285 | if (result.is_null()) { |
| 286 | ScopedJavaLocalRef<jstring> java_url = |
| 287 | base::android::ConvertUTF8ToJavaString(env, url.spec()); |
zpeng | e33ba85 | 2017-02-01 20:54:42 | [diff] [blame] | 288 | SkColor mean_color = |
| 289 | SkColorSetRGB(g_default_rgb_icon_value, g_default_rgb_icon_value, |
| 290 | g_default_rgb_icon_value); |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 291 | |
| 292 | if (!bitmap.isNull()) |
| 293 | mean_color = color_utils::CalculateKMeanColorOfBitmap(bitmap); |
| 294 | |
| 295 | *is_generated = true; |
newt | a584b9e | 2015-10-29 22:29:43 | [diff] [blame] | 296 | result = Java_ShortcutHelper_generateHomeScreenIcon( |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame] | 297 | env, java_url, SkColorGetR(mean_color), SkColorGetG(mean_color), |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 298 | SkColorGetB(mean_color)); |
| 299 | } |
| 300 | |
pkotwicz | 964382b | 2016-08-04 01:24:55 | [diff] [blame] | 301 | return result.obj() |
torne | d64eb513 | 2016-10-24 12:51:28 | [diff] [blame] | 302 | ? gfx::CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(result)) |
pkotwicz | 964382b | 2016-08-04 01:24:55 | [diff] [blame] | 303 | : SkBitmap(); |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 304 | } |
| 305 | |
pkotwicz | cda82fe | 2016-07-08 18:56:54 | [diff] [blame] | 306 | // static |
Peter Kotwicz | 4b358c16 | 2019-01-03 01:10:58 | [diff] [blame] | 307 | std::string ShortcutHelper::QueryFirstWebApkPackage(const GURL& url) { |
zpeng | 4bb5896 | 2016-10-04 02:42:29 | [diff] [blame] | 308 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 309 | ScopedJavaLocalRef<jstring> java_url = |
| 310 | base::android::ConvertUTF8ToJavaString(env, url.spec()); |
| 311 | ScopedJavaLocalRef<jstring> java_webapk_package_name = |
Peter Kotwicz | 4b358c16 | 2019-01-03 01:10:58 | [diff] [blame] | 312 | Java_ShortcutHelper_queryFirstWebApkPackage(env, java_url); |
zpeng | 4bb5896 | 2016-10-04 02:42:29 | [diff] [blame] | 313 | |
| 314 | std::string webapk_package_name = ""; |
| 315 | if (java_webapk_package_name.obj()) { |
zpeng | a7856eef | 2017-02-07 11:42:44 | [diff] [blame] | 316 | webapk_package_name = |
| 317 | base::android::ConvertJavaStringToUTF8(env, java_webapk_package_name); |
zpeng | 4bb5896 | 2016-10-04 02:42:29 | [diff] [blame] | 318 | } |
| 319 | return webapk_package_name; |
| 320 | } |
| 321 | |
| 322 | // static |
zpeng | a7856eef | 2017-02-07 11:42:44 | [diff] [blame] | 323 | bool ShortcutHelper::IsWebApkInstalled(content::BrowserContext* browser_context, |
| 324 | const GURL& start_url, |
| 325 | const GURL& manifest_url) { |
Peter Kotwicz | 4b358c16 | 2019-01-03 01:10:58 | [diff] [blame] | 326 | return !QueryFirstWebApkPackage(start_url).empty() || |
zpeng | a7856eef | 2017-02-07 11:42:44 | [diff] [blame] | 327 | WebApkInstallService::Get(browser_context) |
| 328 | ->IsInstallInProgress(manifest_url); |
pkotwicz | cda82fe | 2016-07-08 18:56:54 | [diff] [blame] | 329 | } |
| 330 | |
ckitagawa | 1fa69c0 | 2019-07-24 19:20:44 | [diff] [blame^] | 331 | void ShortcutHelper::SetForceWebApkUpdate(const std::string& id) { |
| 332 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 333 | Java_ShortcutHelper_setForceWebApkUpdate( |
| 334 | env, base::android::ConvertUTF8ToJavaString(env, id)); |
gonzalon | 87192d77 | 2017-02-07 22:31:10 | [diff] [blame] | 335 | } |
| 336 | |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 337 | // Callback used by Java when the shortcut has been created. |
Dominick Ng | 40be818 | 2019-03-11 23:21:03 | [diff] [blame] | 338 | // |splash_image_callback| is a pointer to a base::OnceClosure allocated in |
pkotwicz | c67e6ac | 2016-08-12 19:56:44 | [diff] [blame] | 339 | // AddShortcutWithSkBitmap, so reinterpret_cast it back and run it. |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 340 | // |
| 341 | // This callback should only ever be called when the shortcut was for a |
| 342 | // webapp-capable site; otherwise, |splash_image_callback| will have never been |
| 343 | // allocated and doesn't need to be run or deleted. |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 344 | void JNI_ShortcutHelper_OnWebappDataStored(JNIEnv* env, |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 345 | jlong jsplash_image_callback) { |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 346 | DCHECK(jsplash_image_callback); |
Dominick Ng | 40be818 | 2019-03-11 23:21:03 | [diff] [blame] | 347 | base::OnceClosure* splash_image_callback = |
| 348 | reinterpret_cast<base::OnceClosure*>(jsplash_image_callback); |
| 349 | std::move(*splash_image_callback).Run(); |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 350 | delete splash_image_callback; |
| 351 | } |
| 352 | |