Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / android / omnibox / answers_image_bridge.cc
blob910f48d4a98827676b50022378216fa40567b6ca
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"
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 "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"
20 #include "url/gurl.h"
22 using base::android::ScopedJavaLocalRef;
23 using base::android::ConvertUTF8ToJavaString;
25 namespace {
27 class AnswersImageObserverAndroid : public BitmapFetcherService::Observer {
28 public:
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());
48 private:
49 base::android::ScopedJavaGlobalRef<jobject> java_answers_image_observer_;
52 } // namespace
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);
59 DCHECK(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);
71 DCHECK(profile);
72 BitmapFetcherService* bitmap_fetcher_service =
73 BitmapFetcherServiceFactory::GetForBrowserContext(profile);
74 std::string url;
75 base::android::ConvertJavaStringToUTF8(env, java_url, &url);
76 return bitmap_fetcher_service->RequestImage(
77 GURL(url), new AnswersImageObserverAndroid(env, java_callback));
80 // static
81 bool RegisterAnswersImageBridge(JNIEnv* env) {
82 return RegisterNativesImpl(env);