blob: c3e60da4248922f589a3f4da5c3911f43826aab4 [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"
pkotwiczf4dddb32017-02-25 01:21:5521#include "chrome/browser/android/webapk/webapk_metrics.h"
lalitmd93c2ed2015-09-04 16:22:1222#include "chrome/browser/manifest/manifest_icon_downloader.h"
pkotwicz206673142016-07-19 19:13:3023#include "chrome/common/chrome_switches.h"
dfalcantaraaec56da2015-05-06 03:33:5624#include "content/public/browser/browser_thread.h"
[email protected]51208252013-08-19 21:05:3025#include "content/public/browser/web_contents.h"
[email protected]51208252013-08-19 21:05:3026#include "jni/ShortcutHelper_jni.h"
27#include "ui/gfx/android/java_bitmap.h"
mlamouribc6e8792015-10-22 20:41:1328#include "ui/gfx/color_analysis.h"
[email protected]51208252013-08-19 21:05:3029#include "url/gurl.h"
30
torne86560112016-08-04 15:59:0431using base::android::JavaParamRef;
32using base::android::ScopedJavaLocalRef;
mlamouric679bbf2014-09-24 21:24:4933using content::Manifest;
34
lalitm45a03c72015-09-16 13:00:4335namespace {
36
zpenge33ba852017-02-01 20:54:4237int g_ideal_homescreen_icon_size = -1;
38int g_minimum_homescreen_icon_size = -1;
39int g_ideal_splash_image_size = -1;
40int g_minimum_splash_image_size = -1;
41int g_ideal_badge_icon_size = -1;
lalitm45a03c72015-09-16 13:00:4342
zpenge33ba852017-02-01 20:54:4243int g_default_rgb_icon_value = 145;
mlamouribc6e8792015-10-22 20:41:1344
lalitm45a03c72015-09-16 13:00:4345// Retrieves and caches the ideal and minimum sizes of the Home screen icon
46// and the splash screen image.
47void GetHomescreenIconAndSplashImageSizes() {
48 JNIEnv* env = base::android::AttachCurrentThread();
49 ScopedJavaLocalRef<jintArray> java_size_array =
pkotwicz45fc42b62016-06-07 00:07:1050 Java_ShortcutHelper_getHomeScreenIconAndSplashImageSizes(env);
lalitm45a03c72015-09-16 13:00:4351 std::vector<int> sizes;
zpenga7856eef2017-02-07 11:42:4452 base::android::JavaIntArrayToIntVector(env, java_size_array.obj(), &sizes);
lalitm45a03c72015-09-16 13:00:4353
54 // Check that the size returned is what is expected.
zpenge33ba852017-02-01 20:54:4255 DCHECK(sizes.size() == 5);
lalitm45a03c72015-09-16 13:00:4356
57 // This ordering must be kept up to date with the Java ShortcutHelper.
zpenge33ba852017-02-01 20:54:4258 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];
lalitm45a03c72015-09-16 13:00:4363
64 // Try to ensure that the data returned is sane.
zpenge33ba852017-02-01 20:54:4265 DCHECK(g_minimum_homescreen_icon_size <= g_ideal_homescreen_icon_size);
66 DCHECK(g_minimum_splash_image_size <= g_ideal_splash_image_size);
lalitm45a03c72015-09-16 13:00:4367}
68
pkotwicz97478712017-03-01 03:39:5469// 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.
74void AddWebappWithSkBitmap(const ShortcutInfo& info,
75 const std::string& webapp_id,
76 const SkBitmap& icon_bitmap,
77 const base::Closure& splash_image_callback) {
[email protected]51208252013-08-19 21:05:3078 // Send the data to the Java side to create the shortcut.
79 JNIEnv* env = base::android::AttachCurrentThread();
lalitmd93c2ed2015-09-04 16:22:1280 ScopedJavaLocalRef<jstring> java_webapp_id =
81 base::android::ConvertUTF8ToJavaString(env, webapp_id);
[email protected]51208252013-08-19 21:05:3082 ScopedJavaLocalRef<jstring> java_url =
dfalcantara16e84de2015-02-03 22:07:4083 base::android::ConvertUTF8ToJavaString(env, info.url.spec());
pkotwicz6bdfbe1b2016-07-08 00:26:4384 ScopedJavaLocalRef<jstring> java_scope_url =
85 base::android::ConvertUTF8ToJavaString(env, info.scope.spec());
lalitmf3ee51852015-07-21 18:13:1186 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);
zpenga7856eef2017-02-07 11:42:4492 ScopedJavaLocalRef<jstring> java_best_primary_icon_url =
93 base::android::ConvertUTF8ToJavaString(env,
94 info.best_primary_icon_url.spec());
[email protected]51208252013-08-19 21:05:3095 ScopedJavaLocalRef<jobject> java_bitmap;
mlamouric679bbf2014-09-24 21:24:4996 if (icon_bitmap.getSize())
97 java_bitmap = gfx::ConvertToJavaBitmap(&icon_bitmap);
[email protected]51208252013-08-19 21:05:3098
pkotwicz206673142016-07-19 19:13:3099 // 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));
dominickn6509a4de2016-04-06 08:29:06105
zpenga7856eef2017-02-07 11:42:44106 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]51208252013-08-19 21:05:30111}
benwells840ae902015-02-17 21:13:28112
pkotwicz97478712017-03-01 03:39:54113// Adds a shortcut which opens in a browser tab to the launcher.
114void AddShortcutWithSkBitmap(const ShortcutInfo& info,
115 const std::string& id,
116 const SkBitmap& icon_bitmap) {
pkotwicz206673142016-07-19 19:13:30117 JNIEnv* env = base::android::AttachCurrentThread();
martiw12166f52017-02-20 03:05:45118 ScopedJavaLocalRef<jstring> java_id =
119 base::android::ConvertUTF8ToJavaString(env, id);
pkotwicz206673142016-07-19 19:13:30120 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
martiw12166f52017-02-20 03:05:45128 Java_ShortcutHelper_addShortcut(env, java_id, java_url, java_user_title,
129 java_bitmap, info.source);
pkotwicz206673142016-07-19 19:13:30130}
131
pkotwicz97478712017-03-01 03:39:54132} // anonymous namespace
133
134// static
135void ShortcutHelper::AddToLauncherWithSkBitmap(
136 content::BrowserContext* browser_context,
137 const ShortcutInfo& info,
138 const std::string& webapp_id,
139 const SkBitmap& icon_bitmap,
140 const base::Closure& splash_image_callback) {
141 if (info.display == blink::WebDisplayModeStandalone ||
142 info.display == blink::WebDisplayModeFullscreen) {
143 AddWebappWithSkBitmap(info, webapp_id, icon_bitmap, splash_image_callback);
144 GooglePlayInstallState state =
145 ChromeWebApkHost::GetGooglePlayInstallState();
146 if (state != GooglePlayInstallState::SUPPORTED)
147 webapk::TrackGooglePlayInstallState(state);
148 return;
149 }
150 AddShortcutWithSkBitmap(info, webapp_id, icon_bitmap);
151}
152
153// static
154void ShortcutHelper::InstallWebApkWithSkBitmap(
155 content::BrowserContext* browser_context,
156 const ShortcutInfo& info,
157 const SkBitmap& icon_bitmap,
158 const WebApkInstaller::FinishCallback& callback) {
159 WebApkInstallService::Get(browser_context)
160 ->InstallAsync(info, icon_bitmap, callback);
161 // Don't record metric for users who install WebAPKs via "unsigned sources"
162 // flow.
163 if (ChromeWebApkHost::GetGooglePlayInstallState() ==
164 GooglePlayInstallState::SUPPORTED) {
165 webapk::TrackGooglePlayInstallState(GooglePlayInstallState::SUPPORTED);
166 }
167}
168
hanxi070d10a2017-01-09 15:56:50169void ShortcutHelper::ShowWebApkInstallInProgressToast() {
170 Java_ShortcutHelper_showWebApkInstallInProgressToast(
171 base::android::AttachCurrentThread());
172}
173
zpeng5d8fdfc2017-01-05 15:45:06174int ShortcutHelper::GetIdealHomescreenIconSizeInPx() {
zpenge33ba852017-02-01 20:54:42175 if (g_ideal_homescreen_icon_size == -1)
lalitm45a03c72015-09-16 13:00:43176 GetHomescreenIconAndSplashImageSizes();
zpenge33ba852017-02-01 20:54:42177 return g_ideal_homescreen_icon_size;
lalitm45a03c72015-09-16 13:00:43178}
179
zpeng5d8fdfc2017-01-05 15:45:06180int ShortcutHelper::GetMinimumHomescreenIconSizeInPx() {
zpenge33ba852017-02-01 20:54:42181 if (g_minimum_homescreen_icon_size == -1)
lalitm45a03c72015-09-16 13:00:43182 GetHomescreenIconAndSplashImageSizes();
zpenge33ba852017-02-01 20:54:42183 return g_minimum_homescreen_icon_size;
lalitm45a03c72015-09-16 13:00:43184}
185
zpeng5d8fdfc2017-01-05 15:45:06186int ShortcutHelper::GetIdealSplashImageSizeInPx() {
zpenge33ba852017-02-01 20:54:42187 if (g_ideal_splash_image_size == -1)
lalitm45a03c72015-09-16 13:00:43188 GetHomescreenIconAndSplashImageSizes();
zpenge33ba852017-02-01 20:54:42189 return g_ideal_splash_image_size;
lalitm45a03c72015-09-16 13:00:43190}
191
zpeng5d8fdfc2017-01-05 15:45:06192int ShortcutHelper::GetMinimumSplashImageSizeInPx() {
zpenge33ba852017-02-01 20:54:42193 if (g_minimum_splash_image_size == -1)
lalitm45a03c72015-09-16 13:00:43194 GetHomescreenIconAndSplashImageSizes();
zpenge33ba852017-02-01 20:54:42195 return g_minimum_splash_image_size;
196}
197
198int ShortcutHelper::GetIdealBadgeIconSizeInPx() {
199 if (g_ideal_badge_icon_size == -1)
200 GetHomescreenIconAndSplashImageSizes();
201 return g_ideal_badge_icon_size;
lalitm45a03c72015-09-16 13:00:43202}
203
lalitmd93c2ed2015-09-04 16:22:12204// static
205void ShortcutHelper::FetchSplashScreenImage(
206 content::WebContents* web_contents,
207 const GURL& image_url,
zpeng5d8fdfc2017-01-05 15:45:06208 const int ideal_splash_image_size_in_px,
209 const int minimum_splash_image_size_in_px,
dominickn6509a4de2016-04-06 08:29:06210 const std::string& webapp_id) {
lalitmd93c2ed2015-09-04 16:22:12211 // This is a fire and forget task. It is not vital for the splash screen image
212 // to be downloaded so if the downloader returns false there is no fallback.
213 ManifestIconDownloader::Download(
zpeng5d8fdfc2017-01-05 15:45:06214 web_contents, image_url, ideal_splash_image_size_in_px,
215 minimum_splash_image_size_in_px,
dominickn6509a4de2016-04-06 08:29:06216 base::Bind(&ShortcutHelper::StoreWebappSplashImage, webapp_id));
lalitmd93c2ed2015-09-04 16:22:12217}
218
219// static
zpenga7856eef2017-02-07 11:42:44220void ShortcutHelper::StoreWebappSplashImage(const std::string& webapp_id,
221 const SkBitmap& splash_image) {
lalitmd93c2ed2015-09-04 16:22:12222 if (splash_image.drawsNothing())
223 return;
224
225 JNIEnv* env = base::android::AttachCurrentThread();
226 ScopedJavaLocalRef<jstring> java_webapp_id =
227 base::android::ConvertUTF8ToJavaString(env, webapp_id);
228 ScopedJavaLocalRef<jobject> java_splash_image =
229 gfx::ConvertToJavaBitmap(&splash_image);
230
torne948f3662016-08-16 15:10:44231 Java_ShortcutHelper_storeWebappSplashImage(env, java_webapp_id,
232 java_splash_image);
lalitmd93c2ed2015-09-04 16:22:12233}
234
mlamouribc6e8792015-10-22 20:41:13235// static
pkotwicz5774087e2016-08-10 17:36:40236SkBitmap ShortcutHelper::FinalizeLauncherIconInBackground(
237 const SkBitmap& bitmap,
238 const GURL& url,
239 bool* is_generated) {
240 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
mlamouribc6e8792015-10-22 20:41:13241
242 JNIEnv* env = base::android::AttachCurrentThread();
243 ScopedJavaLocalRef<jobject> result;
244 *is_generated = false;
245
246 if (!bitmap.isNull()) {
pkotwicz45fc42b62016-06-07 00:07:10247 if (Java_ShortcutHelper_isIconLargeEnoughForLauncher(env, bitmap.width(),
248 bitmap.height())) {
newta584b9e2015-10-29 22:29:43249 ScopedJavaLocalRef<jobject> java_bitmap =
250 gfx::ConvertToJavaBitmap(&bitmap);
torne948f3662016-08-16 15:10:44251 result =
252 Java_ShortcutHelper_createHomeScreenIconFromWebIcon(env, java_bitmap);
mlamouribc6e8792015-10-22 20:41:13253 }
254 }
255
256 if (result.is_null()) {
257 ScopedJavaLocalRef<jstring> java_url =
258 base::android::ConvertUTF8ToJavaString(env, url.spec());
zpenge33ba852017-02-01 20:54:42259 SkColor mean_color =
260 SkColorSetRGB(g_default_rgb_icon_value, g_default_rgb_icon_value,
261 g_default_rgb_icon_value);
mlamouribc6e8792015-10-22 20:41:13262
263 if (!bitmap.isNull())
264 mean_color = color_utils::CalculateKMeanColorOfBitmap(bitmap);
265
266 *is_generated = true;
newta584b9e2015-10-29 22:29:43267 result = Java_ShortcutHelper_generateHomeScreenIcon(
torne948f3662016-08-16 15:10:44268 env, java_url, SkColorGetR(mean_color), SkColorGetG(mean_color),
mlamouribc6e8792015-10-22 20:41:13269 SkColorGetB(mean_color));
270 }
271
pkotwicz964382b2016-08-04 01:24:55272 return result.obj()
torned64eb5132016-10-24 12:51:28273 ? gfx::CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(result))
pkotwicz964382b2016-08-04 01:24:55274 : SkBitmap();
mlamouribc6e8792015-10-22 20:41:13275}
276
pkotwiczcda82fe2016-07-08 18:56:54277// static
zpeng4bb58962016-10-04 02:42:29278std::string ShortcutHelper::QueryWebApkPackage(const GURL& url) {
279 JNIEnv* env = base::android::AttachCurrentThread();
280 ScopedJavaLocalRef<jstring> java_url =
281 base::android::ConvertUTF8ToJavaString(env, url.spec());
282 ScopedJavaLocalRef<jstring> java_webapk_package_name =
283 Java_ShortcutHelper_queryWebApkPackage(env, java_url);
284
285 std::string webapk_package_name = "";
286 if (java_webapk_package_name.obj()) {
zpenga7856eef2017-02-07 11:42:44287 webapk_package_name =
288 base::android::ConvertJavaStringToUTF8(env, java_webapk_package_name);
zpeng4bb58962016-10-04 02:42:29289 }
290 return webapk_package_name;
291}
292
293// static
zpenga7856eef2017-02-07 11:42:44294bool ShortcutHelper::IsWebApkInstalled(content::BrowserContext* browser_context,
295 const GURL& start_url,
296 const GURL& manifest_url) {
hanxi070d10a2017-01-09 15:56:50297 return !QueryWebApkPackage(start_url).empty() ||
zpenga7856eef2017-02-07 11:42:44298 WebApkInstallService::Get(browser_context)
299 ->IsInstallInProgress(manifest_url);
pkotwiczcda82fe2016-07-08 18:56:54300}
301
pkotwicz47136bc2016-08-06 23:55:39302GURL ShortcutHelper::GetScopeFromURL(const GURL& url) {
303 JNIEnv* env = base::android::AttachCurrentThread();
304 ScopedJavaLocalRef<jstring> java_url =
305 base::android::ConvertUTF8ToJavaString(env, url.spec());
306 ScopedJavaLocalRef<jstring> java_scope_url =
torne948f3662016-08-16 15:10:44307 Java_ShortcutHelper_getScopeFromUrl(env, java_url);
pkotwicz47136bc2016-08-06 23:55:39308 return GURL(base::android::ConvertJavaStringToUTF16(env, java_scope_url));
309}
310
gonzalon87192d772017-02-07 22:31:10311void ShortcutHelper::RetrieveWebApks(const WebApkInfoCallback& callback) {
312 uintptr_t callback_pointer =
313 reinterpret_cast<uintptr_t>(new WebApkInfoCallback(callback));
314 Java_ShortcutHelper_retrieveWebApks(base::android::AttachCurrentThread(),
315 callback_pointer);
316}
317
dominickn6509a4de2016-04-06 08:29:06318// Callback used by Java when the shortcut has been created.
319// |splash_image_callback| is a pointer to a base::Closure allocated in
pkotwiczc67e6ac2016-08-12 19:56:44320// AddShortcutWithSkBitmap, so reinterpret_cast it back and run it.
dominickn6509a4de2016-04-06 08:29:06321//
322// This callback should only ever be called when the shortcut was for a
323// webapp-capable site; otherwise, |splash_image_callback| will have never been
324// allocated and doesn't need to be run or deleted.
325void OnWebappDataStored(JNIEnv* env,
326 const JavaParamRef<jclass>& clazz,
327 jlong jsplash_image_callback) {
328 DCHECK(jsplash_image_callback);
329 base::Closure* splash_image_callback =
330 reinterpret_cast<base::Closure*>(jsplash_image_callback);
331 splash_image_callback->Run();
332 delete splash_image_callback;
333}
334
gonzalon87192d772017-02-07 22:31:10335void OnWebApksRetrieved(JNIEnv* env,
336 const JavaParamRef<jclass>& clazz,
337 const jlong jcallback_pointer,
338 const JavaParamRef<jobjectArray>& jshort_names,
339 const JavaParamRef<jobjectArray>& jpackage_names,
340 const JavaParamRef<jintArray>& jshell_apk_versions,
341 const JavaParamRef<jintArray>& jversion_codes) {
342 DCHECK(jcallback_pointer);
343 std::vector<std::string> short_names;
344 base::android::AppendJavaStringArrayToStringVector(env, jshort_names,
345 &short_names);
346 std::vector<std::string> package_names;
347 base::android::AppendJavaStringArrayToStringVector(env, jpackage_names,
348 &package_names);
349 std::vector<int> shell_apk_versions;
350 base::android::JavaIntArrayToIntVector(env, jshell_apk_versions,
351 &shell_apk_versions);
352 std::vector<int> version_codes;
353 base::android::JavaIntArrayToIntVector(env, jversion_codes, &version_codes);
354
355 DCHECK(short_names.size() == package_names.size());
356 DCHECK(short_names.size() == shell_apk_versions.size());
357 DCHECK(short_names.size() == version_codes.size());
358
359 std::vector<WebApkInfo> webapk_list;
360 webapk_list.reserve(short_names.size());
361 for (size_t i = 0; i < short_names.size(); ++i) {
362 webapk_list.push_back(WebApkInfo(std::move(short_names[i]),
363 std::move(package_names[i]),
364 shell_apk_versions[i], version_codes[i]));
365 }
366
367 ShortcutHelper::WebApkInfoCallback* webapk_list_callback =
368 reinterpret_cast<ShortcutHelper::WebApkInfoCallback*>(jcallback_pointer);
369 webapk_list_callback->Run(webapk_list);
370 delete webapk_list_callback;
371}
372
lalitm870920e2015-08-20 22:06:03373bool ShortcutHelper::RegisterShortcutHelper(JNIEnv* env) {
374 return RegisterNativesImpl(env);
benwells840ae902015-02-17 21:13:28375}