blob: aa309f6060dc9ecac2cfd5e1ca1f71a8dc6112d3 [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>
gonzalon87192d772017-02-07 22:31:108#include <utility>
[email protected]51208252013-08-19 21:05:309
10#include "base/android/jni_android.h"
lalitm45a03c72015-09-16 13:00:4311#include "base/android/jni_array.h"
[email protected]51208252013-08-19 21:05:3012#include "base/android/jni_string.h"
dominickn6509a4de2016-04-06 08:29:0613#include "base/bind.h"
14#include "base/callback.h"
pkotwicz206673142016-07-19 19:13:3015#include "base/command_line.h"
[email protected]51208252013-08-19 21:05:3016#include "base/strings/string16.h"
mlamouric679bbf2014-09-24 21:24:4917#include "base/strings/utf_string_conversions.h"
tzika1fd66f2017-02-15 07:58:4518#include "base/threading/sequenced_worker_pool.h"
hanxi563f3532016-08-19 20:09:0119#include "chrome/browser/android/webapk/chrome_webapk_host.h"
hanxi070d10a2017-01-09 15:56:5020#include "chrome/browser/android/webapk/webapk_install_service.h"
lalitmd93c2ed2015-09-04 16:22:1221#include "chrome/browser/manifest/manifest_icon_downloader.h"
pkotwicz206673142016-07-19 19:13:3022#include "chrome/common/chrome_switches.h"
dfalcantaraaec56da2015-05-06 03:33:5623#include "content/public/browser/browser_thread.h"
[email protected]51208252013-08-19 21:05:3024#include "content/public/browser/web_contents.h"
[email protected]51208252013-08-19 21:05:3025#include "jni/ShortcutHelper_jni.h"
26#include "ui/gfx/android/java_bitmap.h"
mlamouribc6e8792015-10-22 20:41:1327#include "ui/gfx/color_analysis.h"
[email protected]51208252013-08-19 21:05:3028#include "url/gurl.h"
29
torne86560112016-08-04 15:59:0430using base::android::JavaParamRef;
31using base::android::ScopedJavaLocalRef;
mlamouric679bbf2014-09-24 21:24:4932using content::Manifest;
33
lalitm45a03c72015-09-16 13:00:4334namespace {
35
zpenge33ba852017-02-01 20:54:4236int g_ideal_homescreen_icon_size = -1;
37int g_minimum_homescreen_icon_size = -1;
38int g_ideal_splash_image_size = -1;
39int g_minimum_splash_image_size = -1;
40int g_ideal_badge_icon_size = -1;
lalitm45a03c72015-09-16 13:00:4341
zpenge33ba852017-02-01 20:54:4242int g_default_rgb_icon_value = 145;
mlamouribc6e8792015-10-22 20:41:1343
lalitm45a03c72015-09-16 13:00:4344// Retrieves and caches the ideal and minimum sizes of the Home screen icon
45// and the splash screen image.
46void GetHomescreenIconAndSplashImageSizes() {
47 JNIEnv* env = base::android::AttachCurrentThread();
48 ScopedJavaLocalRef<jintArray> java_size_array =
pkotwicz45fc42b62016-06-07 00:07:1049 Java_ShortcutHelper_getHomeScreenIconAndSplashImageSizes(env);
lalitm45a03c72015-09-16 13:00:4350 std::vector<int> sizes;
zpenga7856eef2017-02-07 11:42:4451 base::android::JavaIntArrayToIntVector(env, java_size_array.obj(), &sizes);
lalitm45a03c72015-09-16 13:00:4352
53 // Check that the size returned is what is expected.
zpenge33ba852017-02-01 20:54:4254 DCHECK(sizes.size() == 5);
lalitm45a03c72015-09-16 13:00:4355
56 // This ordering must be kept up to date with the Java ShortcutHelper.
zpenge33ba852017-02-01 20:54:4257 g_ideal_homescreen_icon_size = sizes[0];
58 g_minimum_homescreen_icon_size = sizes[1];
59 g_ideal_splash_image_size = sizes[2];
60 g_minimum_splash_image_size = sizes[3];
61 g_ideal_badge_icon_size = sizes[4];
lalitm45a03c72015-09-16 13:00:4362
63 // Try to ensure that the data returned is sane.
zpenge33ba852017-02-01 20:54:4264 DCHECK(g_minimum_homescreen_icon_size <= g_ideal_homescreen_icon_size);
65 DCHECK(g_minimum_splash_image_size <= g_ideal_splash_image_size);
lalitm45a03c72015-09-16 13:00:4366}
67
zpenga7856eef2017-02-07 11:42:4468} // anonymous namespace
lalitm45a03c72015-09-16 13:00:4369
dfalcantaraaec56da2015-05-06 03:33:5670// static
pkotwiczc67e6ac2016-08-12 19:56:4471void ShortcutHelper::AddToLauncherWithSkBitmap(
pkotwicz879b1ed2016-08-06 00:48:2272 content::BrowserContext* browser_context,
pkotwicz206673142016-07-19 19:13:3073 const ShortcutInfo& info,
74 const std::string& webapp_id,
75 const SkBitmap& icon_bitmap,
76 const base::Closure& splash_image_callback) {
pkotwicz206673142016-07-19 19:13:3077 if (info.display == blink::WebDisplayModeStandalone ||
78 info.display == blink::WebDisplayModeFullscreen) {
pkotwiczc67e6ac2016-08-12 19:56:4479 AddWebappWithSkBitmap(info, webapp_id, icon_bitmap, splash_image_callback);
pkotwicz206673142016-07-19 19:13:3080 return;
81 }
pkotwiczc67e6ac2016-08-12 19:56:4482 AddShortcutWithSkBitmap(info, icon_bitmap);
pkotwicz206673142016-07-19 19:13:3083}
84
85// static
pkotwiczc67e6ac2016-08-12 19:56:4486void ShortcutHelper::InstallWebApkWithSkBitmap(
pkotwicz879b1ed2016-08-06 00:48:2287 content::BrowserContext* browser_context,
pkotwicz206673142016-07-19 19:13:3088 const ShortcutInfo& info,
hanxi9f38f742016-08-30 18:34:2089 const SkBitmap& icon_bitmap,
90 const WebApkInstaller::FinishCallback& callback) {
hanxi070d10a2017-01-09 15:56:5091 WebApkInstallService::Get(browser_context)
92 ->InstallAsync(info, icon_bitmap, callback);
pkotwicz206673142016-07-19 19:13:3093}
94
95// static
pkotwiczc67e6ac2016-08-12 19:56:4496void ShortcutHelper::AddWebappWithSkBitmap(
dfalcantara16e84de2015-02-03 22:07:4097 const ShortcutInfo& info,
lalitmd93c2ed2015-09-04 16:22:1298 const std::string& webapp_id,
dominickn6509a4de2016-04-06 08:29:0699 const SkBitmap& icon_bitmap,
100 const base::Closure& splash_image_callback) {
[email protected]51208252013-08-19 21:05:30101 // Send the data to the Java side to create the shortcut.
102 JNIEnv* env = base::android::AttachCurrentThread();
lalitmd93c2ed2015-09-04 16:22:12103 ScopedJavaLocalRef<jstring> java_webapp_id =
104 base::android::ConvertUTF8ToJavaString(env, webapp_id);
[email protected]51208252013-08-19 21:05:30105 ScopedJavaLocalRef<jstring> java_url =
dfalcantara16e84de2015-02-03 22:07:40106 base::android::ConvertUTF8ToJavaString(env, info.url.spec());
pkotwicz6bdfbe1b2016-07-08 00:26:43107 ScopedJavaLocalRef<jstring> java_scope_url =
108 base::android::ConvertUTF8ToJavaString(env, info.scope.spec());
lalitmf3ee51852015-07-21 18:13:11109 ScopedJavaLocalRef<jstring> java_user_title =
110 base::android::ConvertUTF16ToJavaString(env, info.user_title);
111 ScopedJavaLocalRef<jstring> java_name =
112 base::android::ConvertUTF16ToJavaString(env, info.name);
113 ScopedJavaLocalRef<jstring> java_short_name =
114 base::android::ConvertUTF16ToJavaString(env, info.short_name);
zpenga7856eef2017-02-07 11:42:44115 ScopedJavaLocalRef<jstring> java_best_primary_icon_url =
116 base::android::ConvertUTF8ToJavaString(env,
117 info.best_primary_icon_url.spec());
[email protected]51208252013-08-19 21:05:30118 ScopedJavaLocalRef<jobject> java_bitmap;
mlamouric679bbf2014-09-24 21:24:49119 if (icon_bitmap.getSize())
120 java_bitmap = gfx::ConvertToJavaBitmap(&icon_bitmap);
[email protected]51208252013-08-19 21:05:30121
pkotwicz206673142016-07-19 19:13:30122 // The callback will need to be run after shortcut creation completes in order
123 // to download the splash image and save it to the WebappDataStorage. Create a
124 // copy of the callback here and send the pointer to Java, which will send it
125 // back once the asynchronous shortcut creation process finishes.
126 uintptr_t callback_pointer =
127 reinterpret_cast<uintptr_t>(new base::Closure(splash_image_callback));
dominickn6509a4de2016-04-06 08:29:06128
zpenga7856eef2017-02-07 11:42:44129 Java_ShortcutHelper_addWebapp(
130 env, java_webapp_id, java_url, java_scope_url, java_user_title, java_name,
131 java_short_name, java_best_primary_icon_url, java_bitmap, info.display,
132 info.orientation, info.source, info.theme_color, info.background_color,
133 callback_pointer);
[email protected]51208252013-08-19 21:05:30134}
benwells840ae902015-02-17 21:13:28135
zpenga7856eef2017-02-07 11:42:44136void ShortcutHelper::AddShortcutWithSkBitmap(const ShortcutInfo& info,
137 const SkBitmap& icon_bitmap) {
pkotwicz206673142016-07-19 19:13:30138 JNIEnv* env = base::android::AttachCurrentThread();
139 ScopedJavaLocalRef<jstring> java_url =
140 base::android::ConvertUTF8ToJavaString(env, info.url.spec());
141 ScopedJavaLocalRef<jstring> java_user_title =
142 base::android::ConvertUTF16ToJavaString(env, info.user_title);
143 ScopedJavaLocalRef<jobject> java_bitmap;
144 if (icon_bitmap.getSize())
145 java_bitmap = gfx::ConvertToJavaBitmap(&icon_bitmap);
146
torne948f3662016-08-16 15:10:44147 Java_ShortcutHelper_addShortcut(env, java_url, java_user_title, java_bitmap,
148 info.source);
pkotwicz206673142016-07-19 19:13:30149}
150
hanxi070d10a2017-01-09 15:56:50151void ShortcutHelper::ShowWebApkInstallInProgressToast() {
152 Java_ShortcutHelper_showWebApkInstallInProgressToast(
153 base::android::AttachCurrentThread());
154}
155
zpeng5d8fdfc2017-01-05 15:45:06156int ShortcutHelper::GetIdealHomescreenIconSizeInPx() {
zpenge33ba852017-02-01 20:54:42157 if (g_ideal_homescreen_icon_size == -1)
lalitm45a03c72015-09-16 13:00:43158 GetHomescreenIconAndSplashImageSizes();
zpenge33ba852017-02-01 20:54:42159 return g_ideal_homescreen_icon_size;
lalitm45a03c72015-09-16 13:00:43160}
161
zpeng5d8fdfc2017-01-05 15:45:06162int ShortcutHelper::GetMinimumHomescreenIconSizeInPx() {
zpenge33ba852017-02-01 20:54:42163 if (g_minimum_homescreen_icon_size == -1)
lalitm45a03c72015-09-16 13:00:43164 GetHomescreenIconAndSplashImageSizes();
zpenge33ba852017-02-01 20:54:42165 return g_minimum_homescreen_icon_size;
lalitm45a03c72015-09-16 13:00:43166}
167
zpeng5d8fdfc2017-01-05 15:45:06168int ShortcutHelper::GetIdealSplashImageSizeInPx() {
zpenge33ba852017-02-01 20:54:42169 if (g_ideal_splash_image_size == -1)
lalitm45a03c72015-09-16 13:00:43170 GetHomescreenIconAndSplashImageSizes();
zpenge33ba852017-02-01 20:54:42171 return g_ideal_splash_image_size;
lalitm45a03c72015-09-16 13:00:43172}
173
zpeng5d8fdfc2017-01-05 15:45:06174int ShortcutHelper::GetMinimumSplashImageSizeInPx() {
zpenge33ba852017-02-01 20:54:42175 if (g_minimum_splash_image_size == -1)
lalitm45a03c72015-09-16 13:00:43176 GetHomescreenIconAndSplashImageSizes();
zpenge33ba852017-02-01 20:54:42177 return g_minimum_splash_image_size;
178}
179
180int ShortcutHelper::GetIdealBadgeIconSizeInPx() {
181 if (g_ideal_badge_icon_size == -1)
182 GetHomescreenIconAndSplashImageSizes();
183 return g_ideal_badge_icon_size;
lalitm45a03c72015-09-16 13:00:43184}
185
lalitmd93c2ed2015-09-04 16:22:12186// static
187void ShortcutHelper::FetchSplashScreenImage(
188 content::WebContents* web_contents,
189 const GURL& image_url,
zpeng5d8fdfc2017-01-05 15:45:06190 const int ideal_splash_image_size_in_px,
191 const int minimum_splash_image_size_in_px,
dominickn6509a4de2016-04-06 08:29:06192 const std::string& webapp_id) {
lalitmd93c2ed2015-09-04 16:22:12193 // This is a fire and forget task. It is not vital for the splash screen image
194 // to be downloaded so if the downloader returns false there is no fallback.
195 ManifestIconDownloader::Download(
zpeng5d8fdfc2017-01-05 15:45:06196 web_contents, image_url, ideal_splash_image_size_in_px,
197 minimum_splash_image_size_in_px,
dominickn6509a4de2016-04-06 08:29:06198 base::Bind(&ShortcutHelper::StoreWebappSplashImage, webapp_id));
lalitmd93c2ed2015-09-04 16:22:12199}
200
201// static
zpenga7856eef2017-02-07 11:42:44202void ShortcutHelper::StoreWebappSplashImage(const std::string& webapp_id,
203 const SkBitmap& splash_image) {
lalitmd93c2ed2015-09-04 16:22:12204 if (splash_image.drawsNothing())
205 return;
206
207 JNIEnv* env = base::android::AttachCurrentThread();
208 ScopedJavaLocalRef<jstring> java_webapp_id =
209 base::android::ConvertUTF8ToJavaString(env, webapp_id);
210 ScopedJavaLocalRef<jobject> java_splash_image =
211 gfx::ConvertToJavaBitmap(&splash_image);
212
torne948f3662016-08-16 15:10:44213 Java_ShortcutHelper_storeWebappSplashImage(env, java_webapp_id,
214 java_splash_image);
lalitmd93c2ed2015-09-04 16:22:12215}
216
mlamouribc6e8792015-10-22 20:41:13217// static
pkotwicz5774087e2016-08-10 17:36:40218SkBitmap ShortcutHelper::FinalizeLauncherIconInBackground(
219 const SkBitmap& bitmap,
220 const GURL& url,
221 bool* is_generated) {
222 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
mlamouribc6e8792015-10-22 20:41:13223
224 JNIEnv* env = base::android::AttachCurrentThread();
225 ScopedJavaLocalRef<jobject> result;
226 *is_generated = false;
227
228 if (!bitmap.isNull()) {
pkotwicz45fc42b62016-06-07 00:07:10229 if (Java_ShortcutHelper_isIconLargeEnoughForLauncher(env, bitmap.width(),
230 bitmap.height())) {
newta584b9e2015-10-29 22:29:43231 ScopedJavaLocalRef<jobject> java_bitmap =
232 gfx::ConvertToJavaBitmap(&bitmap);
torne948f3662016-08-16 15:10:44233 result =
234 Java_ShortcutHelper_createHomeScreenIconFromWebIcon(env, java_bitmap);
mlamouribc6e8792015-10-22 20:41:13235 }
236 }
237
238 if (result.is_null()) {
239 ScopedJavaLocalRef<jstring> java_url =
240 base::android::ConvertUTF8ToJavaString(env, url.spec());
zpenge33ba852017-02-01 20:54:42241 SkColor mean_color =
242 SkColorSetRGB(g_default_rgb_icon_value, g_default_rgb_icon_value,
243 g_default_rgb_icon_value);
mlamouribc6e8792015-10-22 20:41:13244
245 if (!bitmap.isNull())
246 mean_color = color_utils::CalculateKMeanColorOfBitmap(bitmap);
247
248 *is_generated = true;
newta584b9e2015-10-29 22:29:43249 result = Java_ShortcutHelper_generateHomeScreenIcon(
torne948f3662016-08-16 15:10:44250 env, java_url, SkColorGetR(mean_color), SkColorGetG(mean_color),
mlamouribc6e8792015-10-22 20:41:13251 SkColorGetB(mean_color));
252 }
253
pkotwicz964382b2016-08-04 01:24:55254 return result.obj()
torned64eb5132016-10-24 12:51:28255 ? gfx::CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(result))
pkotwicz964382b2016-08-04 01:24:55256 : SkBitmap();
mlamouribc6e8792015-10-22 20:41:13257}
258
pkotwiczcda82fe2016-07-08 18:56:54259// static
zpeng4bb58962016-10-04 02:42:29260std::string ShortcutHelper::QueryWebApkPackage(const GURL& url) {
261 JNIEnv* env = base::android::AttachCurrentThread();
262 ScopedJavaLocalRef<jstring> java_url =
263 base::android::ConvertUTF8ToJavaString(env, url.spec());
264 ScopedJavaLocalRef<jstring> java_webapk_package_name =
265 Java_ShortcutHelper_queryWebApkPackage(env, java_url);
266
267 std::string webapk_package_name = "";
268 if (java_webapk_package_name.obj()) {
zpenga7856eef2017-02-07 11:42:44269 webapk_package_name =
270 base::android::ConvertJavaStringToUTF8(env, java_webapk_package_name);
zpeng4bb58962016-10-04 02:42:29271 }
272 return webapk_package_name;
273}
274
275// static
zpenga7856eef2017-02-07 11:42:44276bool ShortcutHelper::IsWebApkInstalled(content::BrowserContext* browser_context,
277 const GURL& start_url,
278 const GURL& manifest_url) {
hanxi070d10a2017-01-09 15:56:50279 return !QueryWebApkPackage(start_url).empty() ||
zpenga7856eef2017-02-07 11:42:44280 WebApkInstallService::Get(browser_context)
281 ->IsInstallInProgress(manifest_url);
pkotwiczcda82fe2016-07-08 18:56:54282}
283
pkotwicz47136bc2016-08-06 23:55:39284GURL ShortcutHelper::GetScopeFromURL(const GURL& url) {
285 JNIEnv* env = base::android::AttachCurrentThread();
286 ScopedJavaLocalRef<jstring> java_url =
287 base::android::ConvertUTF8ToJavaString(env, url.spec());
288 ScopedJavaLocalRef<jstring> java_scope_url =
torne948f3662016-08-16 15:10:44289 Java_ShortcutHelper_getScopeFromUrl(env, java_url);
pkotwicz47136bc2016-08-06 23:55:39290 return GURL(base::android::ConvertJavaStringToUTF16(env, java_scope_url));
291}
292
gonzalon87192d772017-02-07 22:31:10293void ShortcutHelper::RetrieveWebApks(const WebApkInfoCallback& callback) {
294 uintptr_t callback_pointer =
295 reinterpret_cast<uintptr_t>(new WebApkInfoCallback(callback));
296 Java_ShortcutHelper_retrieveWebApks(base::android::AttachCurrentThread(),
297 callback_pointer);
298}
299
dominickn6509a4de2016-04-06 08:29:06300// Callback used by Java when the shortcut has been created.
301// |splash_image_callback| is a pointer to a base::Closure allocated in
pkotwiczc67e6ac2016-08-12 19:56:44302// AddShortcutWithSkBitmap, so reinterpret_cast it back and run it.
dominickn6509a4de2016-04-06 08:29:06303//
304// This callback should only ever be called when the shortcut was for a
305// webapp-capable site; otherwise, |splash_image_callback| will have never been
306// allocated and doesn't need to be run or deleted.
307void OnWebappDataStored(JNIEnv* env,
308 const JavaParamRef<jclass>& clazz,
309 jlong jsplash_image_callback) {
310 DCHECK(jsplash_image_callback);
311 base::Closure* splash_image_callback =
312 reinterpret_cast<base::Closure*>(jsplash_image_callback);
313 splash_image_callback->Run();
314 delete splash_image_callback;
315}
316
gonzalon87192d772017-02-07 22:31:10317void OnWebApksRetrieved(JNIEnv* env,
318 const JavaParamRef<jclass>& clazz,
319 const jlong jcallback_pointer,
320 const JavaParamRef<jobjectArray>& jshort_names,
321 const JavaParamRef<jobjectArray>& jpackage_names,
322 const JavaParamRef<jintArray>& jshell_apk_versions,
323 const JavaParamRef<jintArray>& jversion_codes) {
324 DCHECK(jcallback_pointer);
325 std::vector<std::string> short_names;
326 base::android::AppendJavaStringArrayToStringVector(env, jshort_names,
327 &short_names);
328 std::vector<std::string> package_names;
329 base::android::AppendJavaStringArrayToStringVector(env, jpackage_names,
330 &package_names);
331 std::vector<int> shell_apk_versions;
332 base::android::JavaIntArrayToIntVector(env, jshell_apk_versions,
333 &shell_apk_versions);
334 std::vector<int> version_codes;
335 base::android::JavaIntArrayToIntVector(env, jversion_codes, &version_codes);
336
337 DCHECK(short_names.size() == package_names.size());
338 DCHECK(short_names.size() == shell_apk_versions.size());
339 DCHECK(short_names.size() == version_codes.size());
340
341 std::vector<WebApkInfo> webapk_list;
342 webapk_list.reserve(short_names.size());
343 for (size_t i = 0; i < short_names.size(); ++i) {
344 webapk_list.push_back(WebApkInfo(std::move(short_names[i]),
345 std::move(package_names[i]),
346 shell_apk_versions[i], version_codes[i]));
347 }
348
349 ShortcutHelper::WebApkInfoCallback* webapk_list_callback =
350 reinterpret_cast<ShortcutHelper::WebApkInfoCallback*>(jcallback_pointer);
351 webapk_list_callback->Run(webapk_list);
352 delete webapk_list_callback;
353}
354
lalitm870920e2015-08-20 22:06:03355bool ShortcutHelper::RegisterShortcutHelper(JNIEnv* env) {
356 return RegisterNativesImpl(env);
benwells840ae902015-02-17 21:13:28357}