Rename GetIconID to GetIconId
[chromium-blink-merge.git] / chrome / browser / android / large_icon_bridge.cc
blob5cc8601abe847444973c6dc9c87bec1976f430a5
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"
7 #include <jni.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;
30 namespace {
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.
40 SkBitmap 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(),
45 &bitmap);
46 if (!bitmap.isNull())
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,
55 j_callback->obj(),
56 j_bitmap.obj(),
57 background_color);
60 } // namespace
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) {
73 delete this;
76 jboolean LargeIconBridge::GetLargeIconForURL(
77 JNIEnv* env,
78 jobject obj,
79 jobject j_profile,
80 jstring j_page_url,
81 jint min_source_size_px,
82 jobject j_callback) {
83 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile);
84 if (!profile)
85 return false;
87 favicon::LargeIconService* large_icon_service =
88 LargeIconServiceFactory::GetForBrowserContext(profile);
89 if (!large_icon_service)
90 return false;
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)),
101 min_source_size_px,
102 0, // Do not resize.
103 callback_runner,
104 &cancelable_task_tracker_);
106 return true;
109 // static
110 bool LargeIconBridge::RegisterLargeIconBridge(JNIEnv* env) {
111 return RegisterNativesImpl(env);