1 // Copyright 2014 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/logo_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 "chrome/browser/android/logo_service.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/profiles/profile_android.h"
16 #include "components/search_provider_logos/logo_tracker.h"
17 #include "jni/LogoBridge_jni.h"
18 #include "third_party/skia/include/core/SkBitmap.h"
19 #include "ui/gfx/android/java_bitmap.h"
21 using base::android::ScopedJavaLocalRef
;
22 using base::android::ConvertUTF8ToJavaString
;
26 // Converts a C++ Logo to a Java Logo.
27 ScopedJavaLocalRef
<jobject
> ConvertLogoToJavaObject(
29 const search_provider_logos::Logo
* logo
) {
31 return ScopedJavaLocalRef
<jobject
>();
33 ScopedJavaLocalRef
<jobject
> j_bitmap
= gfx::ConvertToJavaBitmap(&logo
->image
);
35 ScopedJavaLocalRef
<jstring
> j_on_click_url
;
36 if (!logo
->metadata
.on_click_url
.empty())
37 j_on_click_url
= ConvertUTF8ToJavaString(env
, logo
->metadata
.on_click_url
);
39 ScopedJavaLocalRef
<jstring
> j_alt_text
;
40 if (!logo
->metadata
.alt_text
.empty())
41 j_alt_text
= ConvertUTF8ToJavaString(env
, logo
->metadata
.alt_text
);
43 return Java_LogoBridge_createLogo(
44 env
, j_bitmap
.obj(), j_on_click_url
.obj(), j_alt_text
.obj());
47 class LogoObserverAndroid
: public search_provider_logos::LogoObserver
{
49 LogoObserverAndroid(base::WeakPtr
<LogoBridge
> logo_bridge
,
51 jobject j_logo_observer
)
52 : logo_bridge_(logo_bridge
) {
53 j_logo_observer_
.Reset(env
, j_logo_observer
);
56 ~LogoObserverAndroid() override
{}
58 // seach_provider_logos::LogoObserver:
59 void OnLogoAvailable(const search_provider_logos::Logo
* logo
,
60 bool from_cache
) override
{
64 JNIEnv
* env
= base::android::AttachCurrentThread();
65 ScopedJavaLocalRef
<jobject
> j_logo
= ConvertLogoToJavaObject(env
, logo
);
66 Java_LogoObserver_onLogoAvailable(
67 env
, j_logo_observer_
.obj(), j_logo
.obj(), from_cache
);
70 void OnObserverRemoved() override
{ delete this; }
73 // The associated LogoBridge. We won't call back to Java if the LogoBridge has
75 base::WeakPtr
<LogoBridge
> logo_bridge_
;
77 base::android::ScopedJavaGlobalRef
<jobject
> j_logo_observer_
;
82 static jlong
Init(JNIEnv
* env
, jobject obj
, jobject j_profile
) {
83 LogoBridge
* logo_bridge
= new LogoBridge(j_profile
);
84 return reinterpret_cast<intptr_t>(logo_bridge
);
87 LogoBridge::LogoBridge(jobject j_profile
) : weak_ptr_factory_(this) {
88 Profile
* profile
= ProfileAndroid::FromProfileAndroid(j_profile
);
90 logo_service_
= LogoServiceFactory::GetForProfile(profile
);
93 LogoBridge::~LogoBridge() {}
95 void LogoBridge::Destroy(JNIEnv
* env
, jobject obj
) {
99 void LogoBridge::GetCurrentLogo(JNIEnv
* env
,
101 jobject j_logo_observer
) {
105 // |observer| is deleted in LogoObserverAndroid::OnObserverRemoved().
106 LogoObserverAndroid
* observer
= new LogoObserverAndroid(
107 weak_ptr_factory_
.GetWeakPtr(), env
, j_logo_observer
);
108 logo_service_
->GetLogo(observer
);
112 bool RegisterLogoBridge(JNIEnv
* env
) {
113 return RegisterNativesImpl(env
);