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