blob: 6c4469059144a971b1d7208b19ad9f9f71e9adcb [file] [log] [blame]
[email protected]51208252013-08-19 21:05:301// 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"
lalitm45a03c72015-09-16 13:00:4310#include "base/android/jni_array.h"
[email protected]51208252013-08-19 21:05:3011#include "base/android/jni_string.h"
dominickn6509a4de2016-04-06 08:29:0612#include "base/bind.h"
13#include "base/callback.h"
pkotwicz206673142016-07-19 19:13:3014#include "base/command_line.h"
[email protected]51208252013-08-19 21:05:3015#include "base/strings/string16.h"
mlamouric679bbf2014-09-24 21:24:4916#include "base/strings/utf_string_conversions.h"
hanxi563f3532016-08-19 20:09:0117#include "chrome/browser/android/webapk/chrome_webapk_host.h"
pkotwicz879b1ed2016-08-06 00:48:2218#include "chrome/browser/android/webapk/webapk_installer.h"
lalitmd93c2ed2015-09-04 16:22:1219#include "chrome/browser/manifest/manifest_icon_downloader.h"
pkotwicz206673142016-07-19 19:13:3020#include "chrome/common/chrome_switches.h"
dfalcantaraaec56da2015-05-06 03:33:5621#include "content/public/browser/browser_thread.h"
[email protected]51208252013-08-19 21:05:3022#include "content/public/browser/web_contents.h"
[email protected]51208252013-08-19 21:05:3023#include "jni/ShortcutHelper_jni.h"
24#include "ui/gfx/android/java_bitmap.h"
mlamouribc6e8792015-10-22 20:41:1325#include "ui/gfx/color_analysis.h"
[email protected]51208252013-08-19 21:05:3026#include "url/gurl.h"
27
torne86560112016-08-04 15:59:0428using base::android::JavaParamRef;
29using base::android::ScopedJavaLocalRef;
mlamouric679bbf2014-09-24 21:24:4930using content::Manifest;
31
lalitm45a03c72015-09-16 13:00:4332namespace {
33
34static int kIdealHomescreenIconSize = -1;
35static int kMinimumHomescreenIconSize = -1;
36static int kIdealSplashImageSize = -1;
37static int kMinimumSplashImageSize = -1;
38
mlamouribc6e8792015-10-22 20:41:1339static int kDefaultRGBIconValue = 145;
40
lalitm45a03c72015-09-16 13:00:4341// Retrieves and caches the ideal and minimum sizes of the Home screen icon
42// and the splash screen image.
43void GetHomescreenIconAndSplashImageSizes() {
44 JNIEnv* env = base::android::AttachCurrentThread();
45 ScopedJavaLocalRef<jintArray> java_size_array =
pkotwicz45fc42b62016-06-07 00:07:1046 Java_ShortcutHelper_getHomeScreenIconAndSplashImageSizes(env);
lalitm45a03c72015-09-16 13:00:4347 std::vector<int> sizes;
48 base::android::JavaIntArrayToIntVector(
49 env, java_size_array.obj(), &sizes);
50
51 // Check that the size returned is what is expected.
52 DCHECK(sizes.size() == 4);
53
54 // This ordering must be kept up to date with the Java ShortcutHelper.
55 kIdealHomescreenIconSize = sizes[0];
56 kMinimumHomescreenIconSize = sizes[1];
57 kIdealSplashImageSize = sizes[2];
58 kMinimumSplashImageSize = sizes[3];
59
60 // Try to ensure that the data returned is sane.
61 DCHECK(kMinimumHomescreenIconSize <= kIdealHomescreenIconSize);
62 DCHECK(kMinimumSplashImageSize <= kIdealSplashImageSize);
63}
64
65} // anonymous namespace
66
dfalcantaraaec56da2015-05-06 03:33:5667// static
pkotwiczc67e6ac2016-08-12 19:56:4468void ShortcutHelper::AddToLauncherWithSkBitmap(
pkotwicz879b1ed2016-08-06 00:48:2269 content::BrowserContext* browser_context,
pkotwicz206673142016-07-19 19:13:3070 const ShortcutInfo& info,
71 const std::string& webapp_id,
72 const SkBitmap& icon_bitmap,
73 const base::Closure& splash_image_callback) {
pkotwicz206673142016-07-19 19:13:3074 if (info.display == blink::WebDisplayModeStandalone ||
75 info.display == blink::WebDisplayModeFullscreen) {
hanxi563f3532016-08-19 20:09:0176 if (ChromeWebApkHost::AreWebApkEnabled()) {
pkotwiczc67e6ac2016-08-12 19:56:4477 InstallWebApkWithSkBitmap(browser_context, info, icon_bitmap);
pkotwicz206673142016-07-19 19:13:3078 return;
79 }
pkotwiczc67e6ac2016-08-12 19:56:4480 AddWebappWithSkBitmap(info, webapp_id, icon_bitmap, splash_image_callback);
pkotwicz206673142016-07-19 19:13:3081 return;
82 }
pkotwiczc67e6ac2016-08-12 19:56:4483 AddShortcutWithSkBitmap(info, icon_bitmap);
pkotwicz206673142016-07-19 19:13:3084}
85
86// static
pkotwiczc67e6ac2016-08-12 19:56:4487void ShortcutHelper::InstallWebApkWithSkBitmap(
pkotwicz879b1ed2016-08-06 00:48:2288 content::BrowserContext* browser_context,
pkotwicz206673142016-07-19 19:13:3089 const ShortcutInfo& info,
pkotwicz206673142016-07-19 19:13:3090 const SkBitmap& icon_bitmap) {
pkotwicz879b1ed2016-08-06 00:48:2291 // 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));
pkotwicz206673142016-07-19 19:13:3095}
96
97// static
pkotwiczc67e6ac2016-08-12 19:56:4498void ShortcutHelper::AddWebappWithSkBitmap(
dfalcantara16e84de2015-02-03 22:07:4099 const ShortcutInfo& info,
lalitmd93c2ed2015-09-04 16:22:12100 const std::string& webapp_id,
dominickn6509a4de2016-04-06 08:29:06101 const SkBitmap& icon_bitmap,
102 const base::Closure& splash_image_callback) {
[email protected]51208252013-08-19 21:05:30103 // Send the data to the Java side to create the shortcut.
104 JNIEnv* env = base::android::AttachCurrentThread();
lalitmd93c2ed2015-09-04 16:22:12105 ScopedJavaLocalRef<jstring> java_webapp_id =
106 base::android::ConvertUTF8ToJavaString(env, webapp_id);
[email protected]51208252013-08-19 21:05:30107 ScopedJavaLocalRef<jstring> java_url =
dfalcantara16e84de2015-02-03 22:07:40108 base::android::ConvertUTF8ToJavaString(env, info.url.spec());
pkotwicz6bdfbe1b2016-07-08 00:26:43109 ScopedJavaLocalRef<jstring> java_scope_url =
110 base::android::ConvertUTF8ToJavaString(env, info.scope.spec());
lalitmf3ee51852015-07-21 18:13:11111 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);
pkotwiczb8c25a12016-07-01 20:54:55117 ScopedJavaLocalRef<jstring> java_icon_url =
118 base::android::ConvertUTF8ToJavaString(env, info.icon_url.spec());
[email protected]51208252013-08-19 21:05:30119 ScopedJavaLocalRef<jobject> java_bitmap;
mlamouric679bbf2014-09-24 21:24:49120 if (icon_bitmap.getSize())
121 java_bitmap = gfx::ConvertToJavaBitmap(&icon_bitmap);
[email protected]51208252013-08-19 21:05:30122
pkotwicz206673142016-07-19 19:13:30123 // 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));
dominickn6509a4de2016-04-06 08:29:06129
torne948f3662016-08-16 15:10:44130 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]51208252013-08-19 21:05:30135}
benwells840ae902015-02-17 21:13:28136
pkotwiczc67e6ac2016-08-12 19:56:44137void ShortcutHelper::AddShortcutWithSkBitmap(
pkotwicz206673142016-07-19 19:13:30138 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
torne948f3662016-08-16 15:10:44149 Java_ShortcutHelper_addShortcut(env, java_url, java_user_title, java_bitmap,
150 info.source);
pkotwicz206673142016-07-19 19:13:30151}
152
pkotwicz879b1ed2016-08-06 00:48:22153void 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
lalitm45a03c72015-09-16 13:00:43163int ShortcutHelper::GetIdealHomescreenIconSizeInDp() {
164 if (kIdealHomescreenIconSize == -1)
165 GetHomescreenIconAndSplashImageSizes();
166 return kIdealHomescreenIconSize;
167}
168
169int ShortcutHelper::GetMinimumHomescreenIconSizeInDp() {
170 if (kMinimumHomescreenIconSize == -1)
171 GetHomescreenIconAndSplashImageSizes();
172 return kMinimumHomescreenIconSize;
173}
174
175int ShortcutHelper::GetIdealSplashImageSizeInDp() {
176 if (kIdealSplashImageSize == -1)
177 GetHomescreenIconAndSplashImageSizes();
178 return kIdealSplashImageSize;
179}
180
181int ShortcutHelper::GetMinimumSplashImageSizeInDp() {
182 if (kMinimumSplashImageSize == -1)
183 GetHomescreenIconAndSplashImageSizes();
184 return kMinimumSplashImageSize;
185}
186
lalitmd93c2ed2015-09-04 16:22:12187// static
188void ShortcutHelper::FetchSplashScreenImage(
189 content::WebContents* web_contents,
190 const GURL& image_url,
191 const int ideal_splash_image_size_in_dp,
lalitm45a03c72015-09-16 13:00:43192 const int minimum_splash_image_size_in_dp,
dominickn6509a4de2016-04-06 08:29:06193 const std::string& webapp_id) {
lalitmd93c2ed2015-09-04 16:22:12194 // 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(
dominickn6e400422016-03-25 02:02:47197 web_contents, image_url, ideal_splash_image_size_in_dp,
lalitm45a03c72015-09-16 13:00:43198 minimum_splash_image_size_in_dp,
dominickn6509a4de2016-04-06 08:29:06199 base::Bind(&ShortcutHelper::StoreWebappSplashImage, webapp_id));
lalitmd93c2ed2015-09-04 16:22:12200}
201
202// static
dominickn6509a4de2016-04-06 08:29:06203void ShortcutHelper::StoreWebappSplashImage(
lalitmd93c2ed2015-09-04 16:22:12204 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
torne948f3662016-08-16 15:10:44215 Java_ShortcutHelper_storeWebappSplashImage(env, java_webapp_id,
216 java_splash_image);
lalitmd93c2ed2015-09-04 16:22:12217}
218
mlamouribc6e8792015-10-22 20:41:13219// static
pkotwicz5774087e2016-08-10 17:36:40220SkBitmap ShortcutHelper::FinalizeLauncherIconInBackground(
221 const SkBitmap& bitmap,
222 const GURL& url,
223 bool* is_generated) {
224 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
mlamouribc6e8792015-10-22 20:41:13225
226 JNIEnv* env = base::android::AttachCurrentThread();
227 ScopedJavaLocalRef<jobject> result;
228 *is_generated = false;
229
230 if (!bitmap.isNull()) {
pkotwicz45fc42b62016-06-07 00:07:10231 if (Java_ShortcutHelper_isIconLargeEnoughForLauncher(env, bitmap.width(),
232 bitmap.height())) {
newta584b9e2015-10-29 22:29:43233 ScopedJavaLocalRef<jobject> java_bitmap =
234 gfx::ConvertToJavaBitmap(&bitmap);
torne948f3662016-08-16 15:10:44235 result =
236 Java_ShortcutHelper_createHomeScreenIconFromWebIcon(env, java_bitmap);
mlamouribc6e8792015-10-22 20:41:13237 }
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;
newta584b9e2015-10-29 22:29:43250 result = Java_ShortcutHelper_generateHomeScreenIcon(
torne948f3662016-08-16 15:10:44251 env, java_url, SkColorGetR(mean_color), SkColorGetG(mean_color),
mlamouribc6e8792015-10-22 20:41:13252 SkColorGetB(mean_color));
253 }
254
pkotwicz964382b2016-08-04 01:24:55255 return result.obj()
256 ? gfx::CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(result.obj()))
257 : SkBitmap();
mlamouribc6e8792015-10-22 20:41:13258}
259
pkotwiczcda82fe2016-07-08 18:56:54260// static
261bool ShortcutHelper::IsWebApkInstalled(const GURL& url) {
262 JNIEnv* env = base::android::AttachCurrentThread();
263 ScopedJavaLocalRef<jstring> java_url =
264 base::android::ConvertUTF8ToJavaString(env, url.spec());
torne948f3662016-08-16 15:10:44265 return Java_ShortcutHelper_isWebApkInstalled(env, java_url);
pkotwiczcda82fe2016-07-08 18:56:54266}
267
pkotwicz47136bc2016-08-06 23:55:39268GURL 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 =
torne948f3662016-08-16 15:10:44273 Java_ShortcutHelper_getScopeFromUrl(env, java_url);
pkotwicz47136bc2016-08-06 23:55:39274 return GURL(base::android::ConvertJavaStringToUTF16(env, java_scope_url));
275}
276
dominickn6509a4de2016-04-06 08:29:06277// Callback used by Java when the shortcut has been created.
278// |splash_image_callback| is a pointer to a base::Closure allocated in
pkotwiczc67e6ac2016-08-12 19:56:44279// AddShortcutWithSkBitmap, so reinterpret_cast it back and run it.
dominickn6509a4de2016-04-06 08:29:06280//
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.
284void 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
lalitm870920e2015-08-20 22:06:03294bool ShortcutHelper::RegisterShortcutHelper(JNIEnv* env) {
295 return RegisterNativesImpl(env);
benwells840ae902015-02-17 21:13:28296}