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
3 // found in the LICENSE file.
5 #include "chrome/browser/android/large_icon_bridge.h"
9 #include "base/android/jni_android.h"
10 #include "base/android/jni_string.h"
11 #include "base/android/scoped_java_ref.h"
12 #include "base/bind.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/favicon/large_icon_service_factory.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/profiles/profile_android.h"
17 #include "components/favicon/core/large_icon_service.h"
18 #include "components/favicon_base/fallback_icon_style.h"
19 #include "components/favicon_base/favicon_types.h"
20 #include "jni/LargeIconBridge_jni.h"
21 #include "third_party/skia/include/core/SkBitmap.h"
22 #include "ui/gfx/android/java_bitmap.h"
23 #include "ui/gfx/codec/png_codec.h"
25 using base::android::ScopedJavaGlobalRef
;
26 using base::android::ScopedJavaLocalRef
;
27 using base::android::AttachCurrentThread
;
28 using base::android::ConvertJavaStringToUTF16
;
32 const SkColor kDefaultBackgroundColor
= SkColorSetRGB(0x78, 0x78, 0x78);
34 void OnLargeIconAvailable(
35 ScopedJavaGlobalRef
<jobject
>* j_callback
,
36 const favicon_base::LargeIconResult
& result
) {
37 JNIEnv
* env
= AttachCurrentThread();
39 // Convert the result to a Java Bitmap.
41 ScopedJavaLocalRef
<jobject
> j_bitmap
;
42 if (result
.bitmap
.is_valid()) {
43 gfx::PNGCodec::Decode(result
.bitmap
.bitmap_data
->front(),
44 result
.bitmap
.bitmap_data
->size(),
47 j_bitmap
= gfx::ConvertToJavaBitmap(&bitmap
);
50 jint background_color
= kDefaultBackgroundColor
;
51 if (result
.fallback_icon_style
)
52 background_color
= result
.fallback_icon_style
->background_color
;
54 Java_LargeIconCallback_onLargeIconAvailable(env
,
62 static jlong
Init(JNIEnv
* env
, jclass clazz
) {
63 return reinterpret_cast<intptr_t>(new LargeIconBridge());
66 LargeIconBridge::LargeIconBridge() {
69 LargeIconBridge::~LargeIconBridge() {
72 void LargeIconBridge::Destroy(JNIEnv
* env
, jobject obj
) {
76 jboolean
LargeIconBridge::GetLargeIconForURL(
81 jint min_source_size_px
,
83 Profile
* profile
= ProfileAndroid::FromProfileAndroid(j_profile
);
87 favicon::LargeIconService
* large_icon_service
=
88 LargeIconServiceFactory::GetForBrowserContext(profile
);
89 if (!large_icon_service
)
92 ScopedJavaGlobalRef
<jobject
>* j_global_callback
=
93 new ScopedJavaGlobalRef
<jobject
>();
94 j_global_callback
->Reset(env
, j_callback
);
96 favicon_base::LargeIconCallback callback_runner
=
97 base::Bind(&OnLargeIconAvailable
, base::Owned(j_global_callback
));
99 large_icon_service
->GetLargeIconOrFallbackStyle(
100 GURL(ConvertJavaStringToUTF16(env
, j_page_url
)),
104 &cancelable_task_tracker_
);
110 bool LargeIconBridge::RegisterLargeIconBridge(JNIEnv
* env
) {
111 return RegisterNativesImpl(env
);