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/omnibox/answers_image_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/bitmap_fetcher/bitmap_fetcher_service.h"
14 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher_service_factory.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/profiles/profile_android.h"
17 #include "jni/AnswersImage_jni.h"
18 #include "third_party/skia/include/core/SkBitmap.h"
19 #include "ui/gfx/android/java_bitmap.h"
22 using base::android::ScopedJavaLocalRef
;
23 using base::android::ConvertUTF8ToJavaString
;
27 class AnswersImageObserverAndroid
: public BitmapFetcherService::Observer
{
29 explicit AnswersImageObserverAndroid(JNIEnv
* env
,
30 jobject java_answers_image_observer
) {
31 java_answers_image_observer_
.Reset(env
, java_answers_image_observer
);
34 ~AnswersImageObserverAndroid() override
{}
36 // AnswersImageObserver:
37 void OnImageChanged(BitmapFetcherService::RequestId request_id
,
38 const SkBitmap
& answers_image
) override
{
39 DCHECK(!answers_image
.empty());
41 JNIEnv
* env
= base::android::AttachCurrentThread();
42 ScopedJavaLocalRef
<jobject
> java_bitmap
=
43 gfx::ConvertToJavaBitmap(&answers_image
);
44 Java_AnswersImageObserver_onAnswersImageChanged(
45 env
, java_answers_image_observer_
.obj(), java_bitmap
.obj());
49 base::android::ScopedJavaGlobalRef
<jobject
> java_answers_image_observer_
;
54 static void CancelAnswersImageRequest(JNIEnv
* env
,
55 const JavaParamRef
<jclass
>&,
56 const JavaParamRef
<jobject
>& java_profile
,
57 jint java_request_id
) {
58 Profile
* profile
= ProfileAndroid::FromProfileAndroid(java_profile
);
60 BitmapFetcherService
* bitmap_fetcher_service
=
61 BitmapFetcherServiceFactory::GetForBrowserContext(profile
);
62 bitmap_fetcher_service
->CancelRequest(java_request_id
);
65 static int RequestAnswersImage(JNIEnv
* env
,
66 const JavaParamRef
<jclass
>&,
67 const JavaParamRef
<jobject
>& java_profile
,
68 const JavaParamRef
<jstring
>& java_url
,
69 const JavaParamRef
<jobject
>& java_callback
) {
70 Profile
* profile
= ProfileAndroid::FromProfileAndroid(java_profile
);
72 BitmapFetcherService
* bitmap_fetcher_service
=
73 BitmapFetcherServiceFactory::GetForBrowserContext(profile
);
75 base::android::ConvertJavaStringToUTF8(env
, java_url
, &url
);
76 return bitmap_fetcher_service
->RequestImage(
77 GURL(url
), new AnswersImageObserverAndroid(env
, java_callback
));
81 bool RegisterAnswersImageBridge(JNIEnv
* env
) {
82 return RegisterNativesImpl(env
);