| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/large_icon_bridge.h" | 5 #include "chrome/browser/android/large_icon_bridge.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_string.h" | 10 #include "base/android/jni_string.h" |
| 11 #include "base/android/scoped_java_ref.h" | 11 #include "base/android/scoped_java_ref.h" |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "chrome/browser/favicon/large_icon_service_factory.h" | 14 #include "chrome/browser/favicon/large_icon_service_factory.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/profiles/profile_android.h" | 16 #include "chrome/browser/profiles/profile_android.h" |
| 17 #include "components/favicon/core/large_icon_service.h" | 17 #include "components/favicon/core/large_icon_service.h" |
| 18 #include "components/favicon_base/fallback_icon_style.h" | 18 #include "components/favicon_base/fallback_icon_style.h" |
| 19 #include "components/favicon_base/favicon_types.h" | 19 #include "components/favicon_base/favicon_types.h" |
| 20 #include "jni/LargeIconBridge_jni.h" | 20 #include "jni/LargeIconBridge_jni.h" |
| 21 #include "third_party/skia/include/core/SkBitmap.h" | 21 #include "third_party/skia/include/core/SkBitmap.h" |
| 22 #include "ui/gfx/android/java_bitmap.h" | 22 #include "ui/gfx/android/java_bitmap.h" |
| 23 #include "ui/gfx/codec/png_codec.h" | 23 #include "ui/gfx/codec/png_codec.h" |
| 24 | 24 |
| 25 using base::android::JavaParamRef; | 25 using base::android::JavaParamRef; |
| 26 using base::android::JavaRef; |
| 26 using base::android::ScopedJavaGlobalRef; | 27 using base::android::ScopedJavaGlobalRef; |
| 27 using base::android::ScopedJavaLocalRef; | 28 using base::android::ScopedJavaLocalRef; |
| 28 using base::android::AttachCurrentThread; | 29 using base::android::AttachCurrentThread; |
| 29 using base::android::ConvertJavaStringToUTF16; | 30 using base::android::ConvertJavaStringToUTF16; |
| 30 | 31 |
| 31 namespace { | 32 namespace { |
| 32 | 33 |
| 33 void OnLargeIconAvailable( | 34 void OnLargeIconAvailable(const JavaRef<jobject>& j_callback, |
| 34 ScopedJavaGlobalRef<jobject>* j_callback, | 35 const favicon_base::LargeIconResult& result) { |
| 35 const favicon_base::LargeIconResult& result) { | |
| 36 JNIEnv* env = AttachCurrentThread(); | 36 JNIEnv* env = AttachCurrentThread(); |
| 37 | 37 |
| 38 // Convert the result to a Java Bitmap. | 38 // Convert the result to a Java Bitmap. |
| 39 SkBitmap bitmap; | 39 SkBitmap bitmap; |
| 40 ScopedJavaLocalRef<jobject> j_bitmap; | 40 ScopedJavaLocalRef<jobject> j_bitmap; |
| 41 if (result.bitmap.is_valid()) { | 41 if (result.bitmap.is_valid()) { |
| 42 gfx::PNGCodec::Decode(result.bitmap.bitmap_data->front(), | 42 gfx::PNGCodec::Decode(result.bitmap.bitmap_data->front(), |
| 43 result.bitmap.bitmap_data->size(), | 43 result.bitmap.bitmap_data->size(), |
| 44 &bitmap); | 44 &bitmap); |
| 45 if (!bitmap.isNull()) | 45 if (!bitmap.isNull()) |
| 46 j_bitmap = gfx::ConvertToJavaBitmap(&bitmap); | 46 j_bitmap = gfx::ConvertToJavaBitmap(&bitmap); |
| 47 } | 47 } |
| 48 | 48 |
| 49 favicon_base::FallbackIconStyle fallback; | 49 favicon_base::FallbackIconStyle fallback; |
| 50 if (result.fallback_icon_style) | 50 if (result.fallback_icon_style) |
| 51 fallback = *result.fallback_icon_style; | 51 fallback = *result.fallback_icon_style; |
| 52 | 52 |
| 53 Java_LargeIconCallback_onLargeIconAvailable( | 53 Java_LargeIconCallback_onLargeIconAvailable( |
| 54 env, j_callback->obj(), j_bitmap, fallback.background_color, | 54 env, j_callback, j_bitmap, fallback.background_color, |
| 55 fallback.is_default_background_color); | 55 fallback.is_default_background_color); |
| 56 } | 56 } |
| 57 | 57 |
| 58 } // namespace | 58 } // namespace |
| 59 | 59 |
| 60 static jlong Init(JNIEnv* env, const JavaParamRef<jclass>& clazz) { | 60 static jlong Init(JNIEnv* env, const JavaParamRef<jclass>& clazz) { |
| 61 return reinterpret_cast<intptr_t>(new LargeIconBridge()); | 61 return reinterpret_cast<intptr_t>(new LargeIconBridge()); |
| 62 } | 62 } |
| 63 | 63 |
| 64 LargeIconBridge::LargeIconBridge() { | 64 LargeIconBridge::LargeIconBridge() { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 80 const JavaParamRef<jobject>& j_callback) { | 80 const JavaParamRef<jobject>& j_callback) { |
| 81 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); | 81 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); |
| 82 if (!profile) | 82 if (!profile) |
| 83 return false; | 83 return false; |
| 84 | 84 |
| 85 favicon::LargeIconService* large_icon_service = | 85 favicon::LargeIconService* large_icon_service = |
| 86 LargeIconServiceFactory::GetForBrowserContext(profile); | 86 LargeIconServiceFactory::GetForBrowserContext(profile); |
| 87 if (!large_icon_service) | 87 if (!large_icon_service) |
| 88 return false; | 88 return false; |
| 89 | 89 |
| 90 ScopedJavaGlobalRef<jobject>* j_global_callback = | 90 favicon_base::LargeIconCallback callback_runner = base::Bind( |
| 91 new ScopedJavaGlobalRef<jobject>(); | 91 &OnLargeIconAvailable, ScopedJavaGlobalRef<jobject>(env, j_callback)); |
| 92 j_global_callback->Reset(env, j_callback); | |
| 93 | |
| 94 favicon_base::LargeIconCallback callback_runner = | |
| 95 base::Bind(&OnLargeIconAvailable, base::Owned(j_global_callback)); | |
| 96 | 92 |
| 97 large_icon_service->GetLargeIconOrFallbackStyle( | 93 large_icon_service->GetLargeIconOrFallbackStyle( |
| 98 GURL(ConvertJavaStringToUTF16(env, j_page_url)), | 94 GURL(ConvertJavaStringToUTF16(env, j_page_url)), |
| 99 min_source_size_px, | 95 min_source_size_px, |
| 100 0, // Do not resize. | 96 0, // Do not resize. |
| 101 callback_runner, | 97 callback_runner, |
| 102 &cancelable_task_tracker_); | 98 &cancelable_task_tracker_); |
| 103 | 99 |
| 104 return true; | 100 return true; |
| 105 } | 101 } |
| 106 | 102 |
| 107 // static | 103 // static |
| 108 bool LargeIconBridge::RegisterLargeIconBridge(JNIEnv* env) { | 104 bool LargeIconBridge::RegisterLargeIconBridge(JNIEnv* env) { |
| 109 return RegisterNativesImpl(env); | 105 return RegisterNativesImpl(env); |
| 110 } | 106 } |
| OLD | NEW |