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