[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 | |
torne | da6e7c9e | 2015-11-25 18:25:50 | [diff] [blame] | 9 | #include "base/android/context_utils.h" |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 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" |
[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" |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 17 | #include "chrome/browser/manifest/manifest_icon_downloader.h" |
dfalcantara | aec56da | 2015-05-06 03:33:56 | [diff] [blame] | 18 | #include "content/public/browser/browser_thread.h" |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 19 | #include "content/public/browser/web_contents.h" |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 20 | #include "jni/ShortcutHelper_jni.h" |
| 21 | #include "ui/gfx/android/java_bitmap.h" |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 22 | #include "ui/gfx/color_analysis.h" |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 23 | #include "url/gurl.h" |
| 24 | |
mlamouri | c679bbf | 2014-09-24 21:24:49 | [diff] [blame] | 25 | using content::Manifest; |
| 26 | |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 27 | namespace { |
| 28 | |
| 29 | static int kIdealHomescreenIconSize = -1; |
| 30 | static int kMinimumHomescreenIconSize = -1; |
| 31 | static int kIdealSplashImageSize = -1; |
| 32 | static int kMinimumSplashImageSize = -1; |
| 33 | |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 34 | static int kDefaultRGBIconValue = 145; |
| 35 | |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 36 | // Retrieves and caches the ideal and minimum sizes of the Home screen icon |
| 37 | // and the splash screen image. |
| 38 | void GetHomescreenIconAndSplashImageSizes() { |
| 39 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 40 | ScopedJavaLocalRef<jintArray> java_size_array = |
newt | a584b9e | 2015-10-29 22:29:43 | [diff] [blame] | 41 | Java_ShortcutHelper_getHomeScreenIconAndSplashImageSizes(env, |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 42 | base::android::GetApplicationContext()); |
| 43 | std::vector<int> sizes; |
| 44 | base::android::JavaIntArrayToIntVector( |
| 45 | env, java_size_array.obj(), &sizes); |
| 46 | |
| 47 | // Check that the size returned is what is expected. |
| 48 | DCHECK(sizes.size() == 4); |
| 49 | |
| 50 | // This ordering must be kept up to date with the Java ShortcutHelper. |
| 51 | kIdealHomescreenIconSize = sizes[0]; |
| 52 | kMinimumHomescreenIconSize = sizes[1]; |
| 53 | kIdealSplashImageSize = sizes[2]; |
| 54 | kMinimumSplashImageSize = sizes[3]; |
| 55 | |
| 56 | // Try to ensure that the data returned is sane. |
| 57 | DCHECK(kMinimumHomescreenIconSize <= kIdealHomescreenIconSize); |
| 58 | DCHECK(kMinimumSplashImageSize <= kIdealSplashImageSize); |
| 59 | } |
| 60 | |
| 61 | } // anonymous namespace |
| 62 | |
dfalcantara | aec56da | 2015-05-06 03:33:56 | [diff] [blame] | 63 | // static |
mlamouri | c679bbf | 2014-09-24 21:24:49 | [diff] [blame] | 64 | void ShortcutHelper::AddShortcutInBackgroundWithSkBitmap( |
dfalcantara | 16e84de | 2015-02-03 22:07:40 | [diff] [blame] | 65 | const ShortcutInfo& info, |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 66 | const std::string& webapp_id, |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame^] | 67 | const SkBitmap& icon_bitmap, |
| 68 | const base::Closure& splash_image_callback) { |
dfalcantara | aec56da | 2015-05-06 03:33:56 | [diff] [blame] | 69 | DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 70 | |
| 71 | // Send the data to the Java side to create the shortcut. |
| 72 | JNIEnv* env = base::android::AttachCurrentThread(); |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 73 | ScopedJavaLocalRef<jstring> java_webapp_id = |
| 74 | base::android::ConvertUTF8ToJavaString(env, webapp_id); |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 75 | ScopedJavaLocalRef<jstring> java_url = |
dfalcantara | 16e84de | 2015-02-03 22:07:40 | [diff] [blame] | 76 | base::android::ConvertUTF8ToJavaString(env, info.url.spec()); |
lalitm | f3ee5185 | 2015-07-21 18:13:11 | [diff] [blame] | 77 | ScopedJavaLocalRef<jstring> java_user_title = |
| 78 | base::android::ConvertUTF16ToJavaString(env, info.user_title); |
| 79 | ScopedJavaLocalRef<jstring> java_name = |
| 80 | base::android::ConvertUTF16ToJavaString(env, info.name); |
| 81 | ScopedJavaLocalRef<jstring> java_short_name = |
| 82 | base::android::ConvertUTF16ToJavaString(env, info.short_name); |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 83 | ScopedJavaLocalRef<jobject> java_bitmap; |
mlamouri | c679bbf | 2014-09-24 21:24:49 | [diff] [blame] | 84 | if (icon_bitmap.getSize()) |
| 85 | java_bitmap = gfx::ConvertToJavaBitmap(&icon_bitmap); |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 86 | |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame^] | 87 | uintptr_t callback_pointer = 0; |
| 88 | bool is_webapp_capable = info.display == blink::WebDisplayModeStandalone; |
| 89 | |
| 90 | if (is_webapp_capable) { |
| 91 | // The callback will need to be run after shortcut creation completes in |
| 92 | // order to download the splash image and save it to the WebappDataStorage. |
| 93 | // Create a copy of the callback here and send the pointer to Java, which |
| 94 | // will send it back once the asynchronous shortcut creation process |
| 95 | // finishes. |
| 96 | callback_pointer = |
| 97 | reinterpret_cast<uintptr_t>(new base::Closure(splash_image_callback)); |
| 98 | } |
| 99 | |
mlamouri | 89ccc63 | 2014-09-16 19:29:58 | [diff] [blame] | 100 | Java_ShortcutHelper_addShortcut( |
| 101 | env, |
| 102 | base::android::GetApplicationContext(), |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 103 | java_webapp_id.obj(), |
mlamouri | 89ccc63 | 2014-09-16 19:29:58 | [diff] [blame] | 104 | java_url.obj(), |
lalitm | f3ee5185 | 2015-07-21 18:13:11 | [diff] [blame] | 105 | java_user_title.obj(), |
| 106 | java_name.obj(), |
| 107 | java_short_name.obj(), |
mlamouri | 89ccc63 | 2014-09-16 19:29:58 | [diff] [blame] | 108 | java_bitmap.obj(), |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame^] | 109 | is_webapp_capable, |
dominickn | 32fe304 | 2015-07-07 22:42:50 | [diff] [blame] | 110 | info.orientation, |
lalitm | 2f5beca | 2015-08-12 10:53:43 | [diff] [blame] | 111 | info.source, |
lalitm | 4b2dde0a6 | 2015-08-19 22:35:32 | [diff] [blame] | 112 | info.theme_color, |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 113 | info.background_color, |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame^] | 114 | info.is_icon_generated, |
| 115 | callback_pointer); |
[email protected] | 5120825 | 2013-08-19 21:05:30 | [diff] [blame] | 116 | } |
benwells | 840ae90 | 2015-02-17 21:13:28 | [diff] [blame] | 117 | |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 118 | int ShortcutHelper::GetIdealHomescreenIconSizeInDp() { |
| 119 | if (kIdealHomescreenIconSize == -1) |
| 120 | GetHomescreenIconAndSplashImageSizes(); |
| 121 | return kIdealHomescreenIconSize; |
| 122 | } |
| 123 | |
| 124 | int ShortcutHelper::GetMinimumHomescreenIconSizeInDp() { |
| 125 | if (kMinimumHomescreenIconSize == -1) |
| 126 | GetHomescreenIconAndSplashImageSizes(); |
| 127 | return kMinimumHomescreenIconSize; |
| 128 | } |
| 129 | |
| 130 | int ShortcutHelper::GetIdealSplashImageSizeInDp() { |
| 131 | if (kIdealSplashImageSize == -1) |
| 132 | GetHomescreenIconAndSplashImageSizes(); |
| 133 | return kIdealSplashImageSize; |
| 134 | } |
| 135 | |
| 136 | int ShortcutHelper::GetMinimumSplashImageSizeInDp() { |
| 137 | if (kMinimumSplashImageSize == -1) |
| 138 | GetHomescreenIconAndSplashImageSizes(); |
| 139 | return kMinimumSplashImageSize; |
| 140 | } |
| 141 | |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 142 | // static |
| 143 | void ShortcutHelper::FetchSplashScreenImage( |
| 144 | content::WebContents* web_contents, |
| 145 | const GURL& image_url, |
| 146 | const int ideal_splash_image_size_in_dp, |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 147 | const int minimum_splash_image_size_in_dp, |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame^] | 148 | const std::string& webapp_id) { |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 149 | // This is a fire and forget task. It is not vital for the splash screen image |
| 150 | // to be downloaded so if the downloader returns false there is no fallback. |
| 151 | ManifestIconDownloader::Download( |
dominickn | 6e40042 | 2016-03-25 02:02:47 | [diff] [blame] | 152 | web_contents, image_url, ideal_splash_image_size_in_dp, |
lalitm | 45a03c7 | 2015-09-16 13:00:43 | [diff] [blame] | 153 | minimum_splash_image_size_in_dp, |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame^] | 154 | base::Bind(&ShortcutHelper::StoreWebappSplashImage, webapp_id)); |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | // static |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame^] | 158 | void ShortcutHelper::StoreWebappSplashImage( |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 159 | const std::string& webapp_id, |
| 160 | const SkBitmap& splash_image) { |
| 161 | if (splash_image.drawsNothing()) |
| 162 | return; |
| 163 | |
| 164 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 165 | ScopedJavaLocalRef<jstring> java_webapp_id = |
| 166 | base::android::ConvertUTF8ToJavaString(env, webapp_id); |
| 167 | ScopedJavaLocalRef<jobject> java_splash_image = |
| 168 | gfx::ConvertToJavaBitmap(&splash_image); |
| 169 | |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame^] | 170 | Java_ShortcutHelper_storeWebappSplashImage( |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 171 | env, |
| 172 | base::android::GetApplicationContext(), |
| 173 | java_webapp_id.obj(), |
lalitm | d93c2ed | 2015-09-04 16:22:12 | [diff] [blame] | 174 | java_splash_image.obj()); |
| 175 | } |
| 176 | |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 177 | // static |
| 178 | SkBitmap ShortcutHelper::FinalizeLauncherIcon(const SkBitmap& bitmap, |
| 179 | const GURL& url, |
| 180 | bool* is_generated) { |
| 181 | DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 182 | |
| 183 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 184 | ScopedJavaLocalRef<jobject> result; |
| 185 | *is_generated = false; |
| 186 | |
| 187 | if (!bitmap.isNull()) { |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 188 | if (Java_ShortcutHelper_isIconLargeEnoughForLauncher( |
newt | a584b9e | 2015-10-29 22:29:43 | [diff] [blame] | 189 | env, base::android::GetApplicationContext(), bitmap.width(), |
| 190 | bitmap.height())) { |
| 191 | ScopedJavaLocalRef<jobject> java_bitmap = |
| 192 | gfx::ConvertToJavaBitmap(&bitmap); |
| 193 | result = Java_ShortcutHelper_createHomeScreenIconFromWebIcon( |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 194 | env, base::android::GetApplicationContext(), java_bitmap.obj()); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | if (result.is_null()) { |
| 199 | ScopedJavaLocalRef<jstring> java_url = |
| 200 | base::android::ConvertUTF8ToJavaString(env, url.spec()); |
| 201 | SkColor mean_color = SkColorSetRGB( |
| 202 | kDefaultRGBIconValue, kDefaultRGBIconValue, kDefaultRGBIconValue); |
| 203 | |
| 204 | if (!bitmap.isNull()) |
| 205 | mean_color = color_utils::CalculateKMeanColorOfBitmap(bitmap); |
| 206 | |
| 207 | *is_generated = true; |
newt | a584b9e | 2015-10-29 22:29:43 | [diff] [blame] | 208 | result = Java_ShortcutHelper_generateHomeScreenIcon( |
mlamouri | bc6e879 | 2015-10-22 20:41:13 | [diff] [blame] | 209 | env, base::android::GetApplicationContext(), java_url.obj(), |
| 210 | SkColorGetR(mean_color), SkColorGetG(mean_color), |
| 211 | SkColorGetB(mean_color)); |
| 212 | } |
| 213 | |
| 214 | return gfx::CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(result.obj())); |
| 215 | } |
| 216 | |
dominickn | 6509a4de | 2016-04-06 08:29:06 | [diff] [blame^] | 217 | // Callback used by Java when the shortcut has been created. |
| 218 | // |splash_image_callback| is a pointer to a base::Closure allocated in |
| 219 | // AddShortcutInBackgroundWithSkBitmap, so reinterpret_cast it back and run it. |
| 220 | // |
| 221 | // This callback should only ever be called when the shortcut was for a |
| 222 | // webapp-capable site; otherwise, |splash_image_callback| will have never been |
| 223 | // allocated and doesn't need to be run or deleted. |
| 224 | void OnWebappDataStored(JNIEnv* env, |
| 225 | const JavaParamRef<jclass>& clazz, |
| 226 | jlong jsplash_image_callback) { |
| 227 | DCHECK(jsplash_image_callback); |
| 228 | base::Closure* splash_image_callback = |
| 229 | reinterpret_cast<base::Closure*>(jsplash_image_callback); |
| 230 | splash_image_callback->Run(); |
| 231 | delete splash_image_callback; |
| 232 | } |
| 233 | |
lalitm | 870920e | 2015-08-20 22:06:03 | [diff] [blame] | 234 | bool ShortcutHelper::RegisterShortcutHelper(JNIEnv* env) { |
| 235 | return RegisterNativesImpl(env); |
benwells | 840ae90 | 2015-02-17 21:13:28 | [diff] [blame] | 236 | } |