| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/android/shortcut_helper.h" | 5 #include "chrome/browser/android/shortcut_helper.h" |
| 6 | 6 |
| 7 #include <jni.h> | 7 #include <jni.h> |
| 8 | 8 |
| 9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_array.h" | 10 #include "base/android/jni_array.h" |
| 11 #include "base/android/jni_string.h" | 11 #include "base/android/jni_string.h" |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
| 15 #include "base/strings/string16.h" | 15 #include "base/strings/string16.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 17 #include "chrome/browser/manifest/manifest_icon_downloader.h" | 17 #include "chrome/browser/manifest/manifest_icon_downloader.h" |
| 18 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
| 19 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 20 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
| 21 #include "jni/ShortcutHelper_jni.h" | 21 #include "jni/ShortcutHelper_jni.h" |
| 22 #include "third_party/smhasher/src/MurmurHash2.h" |
| 22 #include "ui/gfx/android/java_bitmap.h" | 23 #include "ui/gfx/android/java_bitmap.h" |
| 24 #include "ui/gfx/codec/png_codec.h" |
| 23 #include "ui/gfx/color_analysis.h" | 25 #include "ui/gfx/color_analysis.h" |
| 24 #include "url/gurl.h" | 26 #include "url/gurl.h" |
| 25 | 27 |
| 26 using content::Manifest; | 28 using content::Manifest; |
| 27 | 29 |
| 28 namespace { | 30 namespace { |
| 29 | 31 |
| 30 static int kIdealHomescreenIconSize = -1; | 32 static int kIdealHomescreenIconSize = -1; |
| 31 static int kMinimumHomescreenIconSize = -1; | 33 static int kMinimumHomescreenIconSize = -1; |
| 32 static int kIdealSplashImageSize = -1; | 34 static int kIdealSplashImageSize = -1; |
| 33 static int kMinimumSplashImageSize = -1; | 35 static int kMinimumSplashImageSize = -1; |
| 34 | 36 |
| 35 static int kDefaultRGBIconValue = 145; | 37 static int kDefaultRGBIconValue = 145; |
| 36 | 38 |
| 39 // The seed to use for the murmur2 hash. |
| 40 const uint32_t kMurmur2HashSeed = 0; |
| 41 |
| 42 // Computes a murmur2 hash of |bitmap|'s PNG encoded bytes. |
| 43 uint64_t ComputeBitmapHash(const SkBitmap& bitmap) { |
| 44 std::vector<unsigned char> png_bytes; |
| 45 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &png_bytes); |
| 46 return MurmurHash64B(&png_bytes.front(), png_bytes.size(), kMurmur2HashSeed); |
| 47 } |
| 48 |
| 37 // Retrieves and caches the ideal and minimum sizes of the Home screen icon | 49 // Retrieves and caches the ideal and minimum sizes of the Home screen icon |
| 38 // and the splash screen image. | 50 // and the splash screen image. |
| 39 void GetHomescreenIconAndSplashImageSizes() { | 51 void GetHomescreenIconAndSplashImageSizes() { |
| 40 JNIEnv* env = base::android::AttachCurrentThread(); | 52 JNIEnv* env = base::android::AttachCurrentThread(); |
| 41 ScopedJavaLocalRef<jintArray> java_size_array = | 53 ScopedJavaLocalRef<jintArray> java_size_array = |
| 42 Java_ShortcutHelper_getHomeScreenIconAndSplashImageSizes(env); | 54 Java_ShortcutHelper_getHomeScreenIconAndSplashImageSizes(env); |
| 43 std::vector<int> sizes; | 55 std::vector<int> sizes; |
| 44 base::android::JavaIntArrayToIntVector( | 56 base::android::JavaIntArrayToIntVector( |
| 45 env, java_size_array.obj(), &sizes); | 57 env, java_size_array.obj(), &sizes); |
| 46 | 58 |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 return kIdealSplashImageSize; | 222 return kIdealSplashImageSize; |
| 211 } | 223 } |
| 212 | 224 |
| 213 int ShortcutHelper::GetMinimumSplashImageSizeInDp() { | 225 int ShortcutHelper::GetMinimumSplashImageSizeInDp() { |
| 214 if (kMinimumSplashImageSize == -1) | 226 if (kMinimumSplashImageSize == -1) |
| 215 GetHomescreenIconAndSplashImageSizes(); | 227 GetHomescreenIconAndSplashImageSizes(); |
| 216 return kMinimumSplashImageSize; | 228 return kMinimumSplashImageSize; |
| 217 } | 229 } |
| 218 | 230 |
| 219 // static | 231 // static |
| 232 void ShortcutHelper::OnFetchedHomescreenImage( |
| 233 const base::android::ScopedJavaGlobalRef<jobject> java_callback, |
| 234 const SkBitmap& image) { |
| 235 JNIEnv* env = base::android::AttachCurrentThread(); |
| 236 |
| 237 ScopedJavaLocalRef<jobject> java_bitmap; |
| 238 uint64_t murmur2_hash = 0; |
| 239 if (image.getSize()) { |
| 240 java_bitmap = gfx::ConvertToJavaBitmap(&image); |
| 241 // TODO(pkotwicz): Get hash of untransformed icon's bytes (with no |
| 242 // encoding/decoding). |
| 243 murmur2_hash = ComputeBitmapHash(image); |
| 244 } |
| 245 Java_ShortcutHelper_onFetchedHomescreenImage( |
| 246 env, java_bitmap.obj(), murmur2_hash, java_callback.obj()); |
| 247 } |
| 248 |
| 249 // static |
| 220 void ShortcutHelper::FetchSplashScreenImage( | 250 void ShortcutHelper::FetchSplashScreenImage( |
| 221 content::WebContents* web_contents, | 251 content::WebContents* web_contents, |
| 222 const GURL& image_url, | 252 const GURL& image_url, |
| 223 const int ideal_splash_image_size_in_dp, | 253 const int ideal_splash_image_size_in_dp, |
| 224 const int minimum_splash_image_size_in_dp, | 254 const int minimum_splash_image_size_in_dp, |
| 225 const std::string& webapp_id) { | 255 const std::string& webapp_id) { |
| 226 // This is a fire and forget task. It is not vital for the splash screen image | 256 // This is a fire and forget task. It is not vital for the splash screen image |
| 227 // to be downloaded so if the downloader returns false there is no fallback. | 257 // to be downloaded so if the downloader returns false there is no fallback. |
| 228 ManifestIconDownloader::Download( | 258 ManifestIconDownloader::Download( |
| 229 web_contents, image_url, ideal_splash_image_size_in_dp, | 259 web_contents, image_url, ideal_splash_image_size_in_dp, |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 } | 319 } |
| 290 | 320 |
| 291 // static | 321 // static |
| 292 bool ShortcutHelper::IsWebApkInstalled(const GURL& url) { | 322 bool ShortcutHelper::IsWebApkInstalled(const GURL& url) { |
| 293 JNIEnv* env = base::android::AttachCurrentThread(); | 323 JNIEnv* env = base::android::AttachCurrentThread(); |
| 294 ScopedJavaLocalRef<jstring> java_url = | 324 ScopedJavaLocalRef<jstring> java_url = |
| 295 base::android::ConvertUTF8ToJavaString(env, url.spec()); | 325 base::android::ConvertUTF8ToJavaString(env, url.spec()); |
| 296 return Java_ShortcutHelper_isWebApkInstalled(env, java_url.obj()); | 326 return Java_ShortcutHelper_isWebApkInstalled(env, java_url.obj()); |
| 297 } | 327 } |
| 298 | 328 |
| 329 /** |
| 330 * Called by Java to fetch the image at |java_icon_url| and scale it so that it |
| 331 * can be used on the homscreen. |
| 332 */ |
| 333 void FetchHomescreenImage( |
| 334 JNIEnv* env, |
| 335 const JavaParamRef<jclass>& clazz, |
| 336 const JavaParamRef<jobject>& java_web_contents, |
| 337 const JavaParamRef<jstring>& java_image_url, |
| 338 const JavaParamRef<jobject>& java_callback) { |
| 339 content::WebContents* web_contents = |
| 340 content::WebContents::FromJavaWebContents(java_web_contents); |
| 341 GURL image_url = GURL(ConvertJavaStringToUTF8(env, java_image_url)); |
| 342 base::android::ScopedJavaGlobalRef<jobject> java_global_callback( |
| 343 env, java_callback); |
| 344 |
| 345 ManifestIconDownloader::Download( |
| 346 web_contents, image_url, ShortcutHelper::GetIdealHomescreenIconSizeInDp(), |
| 347 ShortcutHelper::GetMinimumHomescreenIconSizeInDp(), |
| 348 base::Bind(&ShortcutHelper::OnFetchedHomescreenImage, |
| 349 java_global_callback)); |
| 350 } |
| 351 |
| 299 // Callback used by Java when the shortcut has been created. | 352 // Callback used by Java when the shortcut has been created. |
| 300 // |splash_image_callback| is a pointer to a base::Closure allocated in | 353 // |splash_image_callback| is a pointer to a base::Closure allocated in |
| 301 // AddShortcutInBackgroundWithSkBitmap, so reinterpret_cast it back and run it. | 354 // AddShortcutInBackgroundWithSkBitmap, so reinterpret_cast it back and run it. |
| 302 // | 355 // |
| 303 // This callback should only ever be called when the shortcut was for a | 356 // This callback should only ever be called when the shortcut was for a |
| 304 // webapp-capable site; otherwise, |splash_image_callback| will have never been | 357 // webapp-capable site; otherwise, |splash_image_callback| will have never been |
| 305 // allocated and doesn't need to be run or deleted. | 358 // allocated and doesn't need to be run or deleted. |
| 306 void OnWebappDataStored(JNIEnv* env, | 359 void OnWebappDataStored(JNIEnv* env, |
| 307 const JavaParamRef<jclass>& clazz, | 360 const JavaParamRef<jclass>& clazz, |
| 308 jlong jsplash_image_callback) { | 361 jlong jsplash_image_callback) { |
| 309 DCHECK(jsplash_image_callback); | 362 DCHECK(jsplash_image_callback); |
| 310 base::Closure* splash_image_callback = | 363 base::Closure* splash_image_callback = |
| 311 reinterpret_cast<base::Closure*>(jsplash_image_callback); | 364 reinterpret_cast<base::Closure*>(jsplash_image_callback); |
| 312 splash_image_callback->Run(); | 365 splash_image_callback->Run(); |
| 313 delete splash_image_callback; | 366 delete splash_image_callback; |
| 314 } | 367 } |
| 315 | 368 |
| 316 bool ShortcutHelper::RegisterShortcutHelper(JNIEnv* env) { | 369 bool ShortcutHelper::RegisterShortcutHelper(JNIEnv* env) { |
| 317 return RegisterNativesImpl(env); | 370 return RegisterNativesImpl(env); |
| 318 } | 371 } |
| OLD | NEW |