[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> |
| 8 | |
| 9 | #include "base/android/jni_android.h" |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 10 | #include "base/android/jni_array.h" |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 11 | #include "base/android/jni_string.h" |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 12 | #include "base/bind.h" |
| 13 | #include "base/callback.h" |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 14 | #include "base/command_line.h" |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 15 | #include "base/strings/string16.h" |
mlamouri | c679bbf | 2014-09-24 21:24:49 | [diff] [blame] | 16 | #include "base/strings/utf_string_conversions.h" |
pkotwicz | 879b1ed | 2016-08-06 00:48:22 | [diff] [blame] | 17 | #include "chrome/browser/android/webapk/webapk_installer.h" |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 18 | #include "chrome/browser/manifest/manifest_icon_downloader.h" |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 19 | #include "chrome/common/chrome_switches.h" |
dfalcantara | aec56da | 2015-05-06 03:33:56 | [diff] [blame] | 20 | #include "content/public/browser/browser_thread.h" |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 21 | #include "content/public/browser/web_contents.h" |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 22 | #include "jni/ShortcutHelper_jni.h" |
| 23 | #include "ui/gfx/android/java_bitmap.h" |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 24 | #include "ui/gfx/color_analysis.h" |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 25 | #include "url/gurl.h" |
| 26 | |
torne | 8656011 | 2016-08-04 15:59:04 | [diff] [blame] | 27 | using base::android::JavaParamRef; |
| 28 | using base::android::ScopedJavaLocalRef; |
mlamouri | c679bbf | 2014-09-24 21:24:49 | [diff] [blame] | 29 | using content::Manifest; |
| 30 | |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 31 | namespace { |
| 32 | |
| 33 | static int kIdealHomescreenIconSize = -1; |
| 34 | static int kMinimumHomescreenIconSize = -1; |
| 35 | static int kIdealSplashImageSize = -1; |
| 36 | static int kMinimumSplashImageSize = -1; |
| 37 | |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 38 | static int kDefaultRGBIconValue = 145; |
| 39 | |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 40 | // Retrieves and caches the ideal and minimum sizes of the Home screen icon |
| 41 | // and the splash screen image. |
| 42 | void GetHomescreenIconAndSplashImageSizes() { |
| 43 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 44 | ScopedJavaLocalRef<jintArray> java_size_array = |
pkotwicz | 45fc42b6 | 2016-06-07 00:07:10 | [diff] [blame] | 45 | Java_ShortcutHelper_getHomeScreenIconAndSplashImageSizes(env); |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 46 | std::vector<int> sizes; |
| 47 | base::android::JavaIntArrayToIntVector( |
| 48 | env, java_size_array.obj(), &sizes); |
| 49 | |
| 50 | // Check that the size returned is what is expected. |
| 51 | DCHECK(sizes.size() == 4); |
| 52 | |
| 53 | // This ordering must be kept up to date with the Java ShortcutHelper. |
| 54 | kIdealHomescreenIconSize = sizes[0]; |
| 55 | kMinimumHomescreenIconSize = sizes[1]; |
| 56 | kIdealSplashImageSize = sizes[2]; |
| 57 | kMinimumSplashImageSize = sizes[3]; |
| 58 | |
| 59 | // Try to ensure that the data returned is sane. |
| 60 | DCHECK(kMinimumHomescreenIconSize <= kIdealHomescreenIconSize); |
| 61 | DCHECK(kMinimumSplashImageSize <= kIdealSplashImageSize); |
| 62 | } |
| 63 | |
| 64 | } // anonymous namespace |
| 65 | |
dfalcantara | aec56da | 2015-05-06 03:33:56 | [diff] [blame] | 66 | // static |
pkotwicz | c67e6ac | 2016-08-12 19:56:44 | [diff] [blame] | 67 | void ShortcutHelper::AddToLauncherWithSkBitmap( |
pkotwicz | 879b1ed | 2016-08-06 00:48:22 | [diff] [blame] | 68 | content::BrowserContext* browser_context, |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 69 | const ShortcutInfo& info, |
| 70 | const std::string& webapp_id, |
| 71 | const SkBitmap& icon_bitmap, |
| 72 | const base::Closure& splash_image_callback) { |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 73 | if (info.display == blink::WebDisplayModeStandalone || |
| 74 | info.display == blink::WebDisplayModeFullscreen) { |
| 75 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 76 | switches::kEnableWebApk)) { |
pkotwicz | c67e6ac | 2016-08-12 19:56:44 | [diff] [blame] | 77 | InstallWebApkWithSkBitmap(browser_context, info, icon_bitmap); |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 78 | return; |
| 79 | } |
pkotwicz | c67e6ac | 2016-08-12 19:56:44 | [diff] [blame] | 80 | AddWebappWithSkBitmap(info, webapp_id, icon_bitmap, splash_image_callback); |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 81 | return; |
| 82 | } |
pkotwicz | c67e6ac | 2016-08-12 19:56:44 | [diff] [blame] | 83 | AddShortcutWithSkBitmap(info, icon_bitmap); |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // static |
pkotwicz | c67e6ac | 2016-08-12 19:56:44 | [diff] [blame] | 87 | void ShortcutHelper::InstallWebApkWithSkBitmap( |
pkotwicz | 879b1ed | 2016-08-06 00:48:22 | [diff] [blame] | 88 | content::BrowserContext* browser_context, |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 89 | const ShortcutInfo& info, |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 90 | const SkBitmap& icon_bitmap) { |
pkotwicz | 879b1ed | 2016-08-06 00:48:22 | [diff] [blame] | 91 | // WebApkInstaller destroys itself when it is done. |
| 92 | WebApkInstaller* installer = new WebApkInstaller(info, icon_bitmap); |
| 93 | installer->InstallAsync(browser_context, |
| 94 | base::Bind(&ShortcutHelper::OnBuiltWebApk)); |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | // static |
pkotwicz | c67e6ac | 2016-08-12 19:56:44 | [diff] [blame] | 98 | void ShortcutHelper::AddWebappWithSkBitmap( |
dfalcantara | 16e84de | 2015-02-03 22:07:40 | [diff] [blame] | 99 | const ShortcutInfo& info, |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 100 | const std::string& webapp_id, |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 101 | const SkBitmap& icon_bitmap, |
| 102 | const base::Closure& splash_image_callback) { |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 103 | // Send the data to the Java side to create the shortcut. |
| 104 | JNIEnv* env = base::android::AttachCurrentThread(); |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 105 | ScopedJavaLocalRef<jstring> java_webapp_id = |
| 106 | base::android::ConvertUTF8ToJavaString(env, webapp_id); |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 107 | ScopedJavaLocalRef<jstring> java_url = |
dfalcantara | 16e84de | 2015-02-03 22:07:40 | [diff] [blame] | 108 | base::android::ConvertUTF8ToJavaString(env, info.url.spec()); |
pkotwicz | 6bdfbe1b | 2016-07-08 00:26:43 | [diff] [blame] | 109 | ScopedJavaLocalRef<jstring> java_scope_url = |
| 110 | base::android::ConvertUTF8ToJavaString(env, info.scope.spec()); |
lalitm | f3ee5185 | 2015-07-21 18:13:11 | [diff] [blame] | 111 | ScopedJavaLocalRef<jstring> java_user_title = |
| 112 | base::android::ConvertUTF16ToJavaString(env, info.user_title); |
| 113 | ScopedJavaLocalRef<jstring> java_name = |
| 114 | base::android::ConvertUTF16ToJavaString(env, info.name); |
| 115 | ScopedJavaLocalRef<jstring> java_short_name = |
| 116 | base::android::ConvertUTF16ToJavaString(env, info.short_name); |
pkotwicz | b8c25a1 | 2016-07-01 20:54:55 | [diff] [blame] | 117 | ScopedJavaLocalRef<jstring> java_icon_url = |
| 118 | base::android::ConvertUTF8ToJavaString(env, info.icon_url.spec()); |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 119 | ScopedJavaLocalRef<jobject> java_bitmap; |
mlamouri | c679bbf | 2014-09-24 21:24:49 | [diff] [blame] | 120 | if (icon_bitmap.getSize()) |
| 121 | java_bitmap = gfx::ConvertToJavaBitmap(&icon_bitmap); |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 122 | |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 123 | // The callback will need to be run after shortcut creation completes in order |
| 124 | // to download the splash image and save it to the WebappDataStorage. Create a |
| 125 | // copy of the callback here and send the pointer to Java, which will send it |
| 126 | // back once the asynchronous shortcut creation process finishes. |
| 127 | uintptr_t callback_pointer = |
| 128 | reinterpret_cast<uintptr_t>(new base::Closure(splash_image_callback)); |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 129 | |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame^] | 130 | Java_ShortcutHelper_addWebapp(env, java_webapp_id, java_url, java_scope_url, |
| 131 | java_user_title, java_name, java_short_name, |
| 132 | java_icon_url, java_bitmap, info.display, |
| 133 | info.orientation, info.source, info.theme_color, |
| 134 | info.background_color, callback_pointer); |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 135 | } |
benwells | 840ae90 | 2015-02-17 21:13:28 | [diff] [blame] | 136 | |
pkotwicz | c67e6ac | 2016-08-12 19:56:44 | [diff] [blame] | 137 | void ShortcutHelper::AddShortcutWithSkBitmap( |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 138 | const ShortcutInfo& info, |
| 139 | const SkBitmap& icon_bitmap) { |
| 140 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 141 | ScopedJavaLocalRef<jstring> java_url = |
| 142 | base::android::ConvertUTF8ToJavaString(env, info.url.spec()); |
| 143 | ScopedJavaLocalRef<jstring> java_user_title = |
| 144 | base::android::ConvertUTF16ToJavaString(env, info.user_title); |
| 145 | ScopedJavaLocalRef<jobject> java_bitmap; |
| 146 | if (icon_bitmap.getSize()) |
| 147 | java_bitmap = gfx::ConvertToJavaBitmap(&icon_bitmap); |
| 148 | |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame^] | 149 | Java_ShortcutHelper_addShortcut(env, java_url, java_user_title, java_bitmap, |
| 150 | info.source); |
pkotwicz | 20667314 | 2016-07-19 19:13:30 | [diff] [blame] | 151 | } |
| 152 | |
pkotwicz | 879b1ed | 2016-08-06 00:48:22 | [diff] [blame] | 153 | void ShortcutHelper::OnBuiltWebApk(bool success) { |
| 154 | if (success) { |
| 155 | DVLOG(1) << "Sent request to install WebAPK. Seems to have worked."; |
| 156 | } else { |
| 157 | LOG(ERROR) << "WebAPK install failed."; |
| 158 | } |
| 159 | // TODO(pkotwicz): Figure out what to do when installing WebAPK fails. |
| 160 | // (crbug.com/626950) |
| 161 | } |
| 162 | |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 163 | int ShortcutHelper::GetIdealHomescreenIconSizeInDp() { |
| 164 | if (kIdealHomescreenIconSize == -1) |
| 165 | GetHomescreenIconAndSplashImageSizes(); |
| 166 | return kIdealHomescreenIconSize; |
| 167 | } |
| 168 | |
| 169 | int ShortcutHelper::GetMinimumHomescreenIconSizeInDp() { |
| 170 | if (kMinimumHomescreenIconSize == -1) |
| 171 | GetHomescreenIconAndSplashImageSizes(); |
| 172 | return kMinimumHomescreenIconSize; |
| 173 | } |
| 174 | |
| 175 | int ShortcutHelper::GetIdealSplashImageSizeInDp() { |
| 176 | if (kIdealSplashImageSize == -1) |
| 177 | GetHomescreenIconAndSplashImageSizes(); |
| 178 | return kIdealSplashImageSize; |
| 179 | } |
| 180 | |
| 181 | int ShortcutHelper::GetMinimumSplashImageSizeInDp() { |
| 182 | if (kMinimumSplashImageSize == -1) |
| 183 | GetHomescreenIconAndSplashImageSizes(); |
| 184 | return kMinimumSplashImageSize; |
| 185 | } |
| 186 | |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 187 | // static |
| 188 | void ShortcutHelper::FetchSplashScreenImage( |
| 189 | content::WebContents* web_contents, |
| 190 | const GURL& image_url, |
| 191 | const int ideal_splash_image_size_in_dp, |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 192 | const int minimum_splash_image_size_in_dp, |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 193 | const std::string& webapp_id) { |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 194 | // This is a fire and forget task. It is not vital for the splash screen image |
| 195 | // to be downloaded so if the downloader returns false there is no fallback. |
| 196 | ManifestIconDownloader::Download( |
dominickn | 6e40042 | 2016-03-25 02:02:47 | [diff] [blame] | 197 | web_contents, image_url, ideal_splash_image_size_in_dp, |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 198 | minimum_splash_image_size_in_dp, |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 199 | base::Bind(&ShortcutHelper::StoreWebappSplashImage, webapp_id)); |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | // static |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 203 | void ShortcutHelper::StoreWebappSplashImage( |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 204 | const std::string& webapp_id, |
| 205 | const SkBitmap& splash_image) { |
| 206 | if (splash_image.drawsNothing()) |
| 207 | return; |
| 208 | |
| 209 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 210 | ScopedJavaLocalRef<jstring> java_webapp_id = |
| 211 | base::android::ConvertUTF8ToJavaString(env, webapp_id); |
| 212 | ScopedJavaLocalRef<jobject> java_splash_image = |
| 213 | gfx::ConvertToJavaBitmap(&splash_image); |
| 214 | |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame^] | 215 | Java_ShortcutHelper_storeWebappSplashImage(env, java_webapp_id, |
| 216 | java_splash_image); |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 217 | } |
| 218 | |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 219 | // static |
pkotwicz | 5774087e | 2016-08-10 17:36:40 | [diff] [blame] | 220 | SkBitmap ShortcutHelper::FinalizeLauncherIconInBackground( |
| 221 | const SkBitmap& bitmap, |
| 222 | const GURL& url, |
| 223 | bool* is_generated) { |
| 224 | DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 225 | |
| 226 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 227 | ScopedJavaLocalRef<jobject> result; |
| 228 | *is_generated = false; |
| 229 | |
| 230 | if (!bitmap.isNull()) { |
pkotwicz | 45fc42b6 | 2016-06-07 00:07:10 | [diff] [blame] | 231 | if (Java_ShortcutHelper_isIconLargeEnoughForLauncher(env, bitmap.width(), |
| 232 | bitmap.height())) { |
newt | a584b9e | 2015-10-29 22:29:43 | [diff] [blame] | 233 | ScopedJavaLocalRef<jobject> java_bitmap = |
| 234 | gfx::ConvertToJavaBitmap(&bitmap); |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame^] | 235 | result = |
| 236 | Java_ShortcutHelper_createHomeScreenIconFromWebIcon(env, java_bitmap); |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
| 240 | if (result.is_null()) { |
| 241 | ScopedJavaLocalRef<jstring> java_url = |
| 242 | base::android::ConvertUTF8ToJavaString(env, url.spec()); |
| 243 | SkColor mean_color = SkColorSetRGB( |
| 244 | kDefaultRGBIconValue, kDefaultRGBIconValue, kDefaultRGBIconValue); |
| 245 | |
| 246 | if (!bitmap.isNull()) |
| 247 | mean_color = color_utils::CalculateKMeanColorOfBitmap(bitmap); |
| 248 | |
| 249 | *is_generated = true; |
newt | a584b9e | 2015-10-29 22:29:43 | [diff] [blame] | 250 | result = Java_ShortcutHelper_generateHomeScreenIcon( |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame^] | 251 | env, java_url, SkColorGetR(mean_color), SkColorGetG(mean_color), |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 252 | SkColorGetB(mean_color)); |
| 253 | } |
| 254 | |
pkotwicz | 964382b | 2016-08-04 01:24:55 | [diff] [blame] | 255 | return result.obj() |
| 256 | ? gfx::CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(result.obj())) |
| 257 | : SkBitmap(); |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 258 | } |
| 259 | |
pkotwicz | cda82fe | 2016-07-08 18:56:54 | [diff] [blame] | 260 | // static |
| 261 | bool ShortcutHelper::IsWebApkInstalled(const GURL& url) { |
| 262 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 263 | ScopedJavaLocalRef<jstring> java_url = |
| 264 | base::android::ConvertUTF8ToJavaString(env, url.spec()); |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame^] | 265 | return Java_ShortcutHelper_isWebApkInstalled(env, java_url); |
pkotwicz | cda82fe | 2016-07-08 18:56:54 | [diff] [blame] | 266 | } |
| 267 | |
pkotwicz | 47136bc | 2016-08-06 23:55:39 | [diff] [blame] | 268 | GURL ShortcutHelper::GetScopeFromURL(const GURL& url) { |
| 269 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 270 | ScopedJavaLocalRef<jstring> java_url = |
| 271 | base::android::ConvertUTF8ToJavaString(env, url.spec()); |
| 272 | ScopedJavaLocalRef<jstring> java_scope_url = |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame^] | 273 | Java_ShortcutHelper_getScopeFromUrl(env, java_url); |
pkotwicz | 47136bc | 2016-08-06 23:55:39 | [diff] [blame] | 274 | return GURL(base::android::ConvertJavaStringToUTF16(env, java_scope_url)); |
| 275 | } |
| 276 | |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 277 | // Callback used by Java when the shortcut has been created. |
| 278 | // |splash_image_callback| is a pointer to a base::Closure allocated in |
pkotwicz | c67e6ac | 2016-08-12 19:56:44 | [diff] [blame] | 279 | // AddShortcutWithSkBitmap, so reinterpret_cast it back and run it. |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame] | 280 | // |
| 281 | // This callback should only ever be called when the shortcut was for a |
| 282 | // webapp-capable site; otherwise, |splash_image_callback| will have never been |
| 283 | // allocated and doesn't need to be run or deleted. |
| 284 | void OnWebappDataStored(JNIEnv* env, |
| 285 | const JavaParamRef<jclass>& clazz, |
| 286 | jlong jsplash_image_callback) { |
| 287 | DCHECK(jsplash_image_callback); |
| 288 | base::Closure* splash_image_callback = |
| 289 | reinterpret_cast<base::Closure*>(jsplash_image_callback); |
| 290 | splash_image_callback->Run(); |
| 291 | delete splash_image_callback; |
| 292 | } |
| 293 | |
lalitm | 870920e | 2015-08-20 22:06:03 | [diff] [blame] | 294 | bool ShortcutHelper::RegisterShortcutHelper(JNIEnv* env) { |
| 295 | return RegisterNativesImpl(env); |
benwells | 840ae90 | 2015-02-17 21:13:28 | [diff] [blame] | 296 | } |