Commit effc1026 authored by groby@chromium.org's avatar groby@chromium.org

[AiS] Java bridge for AnswersImageService

BUG=380916

Review URL: https://codereview.chromium.org/312203005

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@278546 0039d316-1c4b-4281-b951-d872f2087c98
parent 50f2db4e
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.omnibox;
import android.graphics.Bitmap;
import org.chromium.base.CalledByNative;
import org.chromium.chrome.browser.profiles.Profile;
/**
* Provides access to images used by Answers in Suggest.
*/
public class AnswersImage {
/**
* Observer for updating an image when it is available.
*/
public interface AnswersImageObserver {
/**
* Called when the image is updated.
*
* @param answersImage the image
*/
@CalledByNative("AnswersImageObserver")
public void onAnswersImageChanged(Bitmap bitmap);
}
/**
* Request image, observer is notified when image is loaded.
* @param profile Profile that the request is for.
* @param imageUrl URL for image data.
* @param observer Observer to be notified when image is updated. The C++ side will hold a
* strong reference to this.
* @return A request_id.
*/
public static int requestAnswersImage(
Profile profile, String imageUrl, AnswersImageObserver observer) {
return nativeRequestAnswersImage(profile, imageUrl, observer);
}
/**
* Cancel a pending image request.
* @param profile Profile the request was issued for.
* @param requestId The ID of the request to be cancelled.
*/
public static void cancelAnswersImageRequest(Profile profile, int requestId) {
nativeCancelAnswersImageRequest(profile, requestId);
}
/**
* Requests an image at |imageUrl| for the given |profile| with |observer| being notified.
* @returns an AnswersImageRequest
*/
private static native int nativeRequestAnswersImage(
Profile profile, String imageUrl, AnswersImageObserver observer);
/**
* Cancels a pending request.
*/
private static native void nativeCancelAnswersImageRequest(Profile profile, int requestId);
}
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include "chrome/browser/android/logo_bridge.h" #include "chrome/browser/android/logo_bridge.h"
#include "chrome/browser/android/most_visited_sites.h" #include "chrome/browser/android/most_visited_sites.h"
#include "chrome/browser/android/new_tab_page_prefs.h" #include "chrome/browser/android/new_tab_page_prefs.h"
#include "chrome/browser/android/omnibox/answers_image_bridge.h"
#include "chrome/browser/android/omnibox/autocomplete_controller_android.h" #include "chrome/browser/android/omnibox/autocomplete_controller_android.h"
#include "chrome/browser/android/omnibox/omnibox_prerender.h" #include "chrome/browser/android/omnibox/omnibox_prerender.h"
#include "chrome/browser/android/password_authentication_manager.h" #include "chrome/browser/android/password_authentication_manager.h"
...@@ -97,6 +98,7 @@ static base::android::RegistrationMethod kChromeRegisteredMethods[] = { ...@@ -97,6 +98,7 @@ static base::android::RegistrationMethod kChromeRegisteredMethods[] = {
{ "AccountManagementScreenHelper", AccountManagementScreenHelper::Register }, { "AccountManagementScreenHelper", AccountManagementScreenHelper::Register },
{ "AndroidProfileOAuth2TokenService", { "AndroidProfileOAuth2TokenService",
AndroidProfileOAuth2TokenService::Register }, AndroidProfileOAuth2TokenService::Register },
{ "AnswersImageBridge", RegisterAnswersImageBridge },
{ "AppBannerManager", banners::RegisterAppBannerManager }, { "AppBannerManager", banners::RegisterAppBannerManager },
{ "ApplicationLifetime", RegisterApplicationLifetimeAndroid }, { "ApplicationLifetime", RegisterApplicationLifetimeAndroid },
{ "AutocompleteControllerAndroid", RegisterAutocompleteControllerAndroid }, { "AutocompleteControllerAndroid", RegisterAutocompleteControllerAndroid },
...@@ -176,7 +178,7 @@ static base::android::RegistrationMethod kChromeRegisteredMethods[] = { ...@@ -176,7 +178,7 @@ static base::android::RegistrationMethod kChromeRegisteredMethods[] = {
WebsiteSettingsPopupAndroid::RegisterWebsiteSettingsPopupAndroid }, WebsiteSettingsPopupAndroid::RegisterWebsiteSettingsPopupAndroid },
#if defined(ENABLE_PRINTING) && !defined(ENABLE_FULL_PRINTING) #if defined(ENABLE_PRINTING) && !defined(ENABLE_FULL_PRINTING)
{ "PrintingContext", { "PrintingContext",
printing::PrintingContextAndroid::RegisterPrintingContext}, printing::PrintingContextAndroid::RegisterPrintingContext},
#endif #endif
}; };
......
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/android/omnibox/answers_image_bridge.h"
#include <jni.h>
#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "base/android/scoped_java_ref.h"
#include "base/bind.h"
#include "chrome/browser/bitmap_fetcher/bitmap_fetcher_service.h"
#include "chrome/browser/bitmap_fetcher/bitmap_fetcher_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_android.h"
#include "jni/AnswersImage_jni.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/android/java_bitmap.h"
#include "url/gurl.h"
using base::android::ScopedJavaLocalRef;
using base::android::ConvertUTF8ToJavaString;
namespace {
class AnswersImageObserverAndroid : public BitmapFetcherService::Observer {
public:
explicit AnswersImageObserverAndroid(JNIEnv* env,
jobject java_answers_image_observer) {
java_answers_image_observer_.Reset(env, java_answers_image_observer);
}
virtual ~AnswersImageObserverAndroid() {}
// AnswersImageObserver:
virtual void OnImageChanged(BitmapFetcherService::RequestId request_id,
const SkBitmap& answers_image) OVERRIDE {
// An empty image signals a completed request.
if (answers_image.empty()) {
delete this;
return;
}
JNIEnv* env = base::android::AttachCurrentThread();
ScopedJavaLocalRef<jobject> java_bitmap =
gfx::ConvertToJavaBitmap(&answers_image);
Java_AnswersImageObserver_onAnswersImageChanged(
env, java_answers_image_observer_.obj(), java_bitmap.obj());
}
private:
base::android::ScopedJavaGlobalRef<jobject> java_answers_image_observer_;
};
} // namespace
static void CancelAnswersImageRequest(JNIEnv* env,
jclass,
jobject java_profile,
jint java_request_id) {
Profile* profile = ProfileAndroid::FromProfileAndroid(java_profile);
DCHECK(profile);
BitmapFetcherService* bitmap_fetcher_service =
BitmapFetcherServiceFactory::GetForBrowserContext(profile);
bitmap_fetcher_service->CancelRequest(java_request_id);
}
static int RequestAnswersImage(JNIEnv* env,
jclass,
jobject java_profile,
jstring java_url,
jobject java_callback) {
Profile* profile = ProfileAndroid::FromProfileAndroid(java_profile);
DCHECK(profile);
BitmapFetcherService* bitmap_fetcher_service =
BitmapFetcherServiceFactory::GetForBrowserContext(profile);
std::string url;
base::android::ConvertJavaStringToUTF8(env, java_url, &url);
return bitmap_fetcher_service->RequestImage(
GURL(url), new AnswersImageObserverAndroid(env, java_callback));
}
// static
bool RegisterAnswersImageBridge(JNIEnv* env) {
return RegisterNativesImpl(env);
}
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_ANDROID_OMNIBOX_ANSWERS_IMAGE_BRIDGE_H_
#define CHROME_BROWSER_ANDROID_OMNIBOX_ANSWERS_IMAGE_BRIDGE_H_
#include <jni.h>
// Registers native methods.
bool RegisterAnswersImageBridge(JNIEnv* env);
#endif // CHROME_BROWSER_ANDROID_OMNIBOX_ANSWERS_IMAGE_BRIDGE_H_
...@@ -179,6 +179,8 @@ ...@@ -179,6 +179,8 @@
'browser/android/intercept_download_resource_throttle.h', 'browser/android/intercept_download_resource_throttle.h',
'browser/android/most_visited_sites.cc', 'browser/android/most_visited_sites.cc',
'browser/android/most_visited_sites.h', 'browser/android/most_visited_sites.h',
'browser/android/omnibox/answers_image_bridge.cc',
'browser/android/omnibox/answers_image_bridge.h',
'browser/android/omnibox/autocomplete_controller_android.cc', 'browser/android/omnibox/autocomplete_controller_android.cc',
'browser/android/omnibox/autocomplete_controller_android.h', 'browser/android/omnibox/autocomplete_controller_android.h',
'browser/android/omnibox/omnibox_prerender.cc', 'browser/android/omnibox/omnibox_prerender.cc',
...@@ -3702,6 +3704,7 @@ ...@@ -3702,6 +3704,7 @@
'android/java/src/org/chromium/chrome/browser/JavascriptAppModalDialog.java', 'android/java/src/org/chromium/chrome/browser/JavascriptAppModalDialog.java',
'android/java/src/org/chromium/chrome/browser/NavigationPopup.java', 'android/java/src/org/chromium/chrome/browser/NavigationPopup.java',
'android/java/src/org/chromium/chrome/browser/net/spdyproxy/DataReductionProxySettings.java', 'android/java/src/org/chromium/chrome/browser/net/spdyproxy/DataReductionProxySettings.java',
'android/java/src/org/chromium/chrome/browser/omnibox/AnswersImage.java',
'android/java/src/org/chromium/chrome/browser/omnibox/AutocompleteController.java', 'android/java/src/org/chromium/chrome/browser/omnibox/AutocompleteController.java',
'android/java/src/org/chromium/chrome/browser/omnibox/OmniboxPrerender.java', 'android/java/src/org/chromium/chrome/browser/omnibox/OmniboxPrerender.java',
'android/java/src/org/chromium/chrome/browser/omnibox/OmniboxViewUtil.java', 'android/java/src/org/chromium/chrome/browser/omnibox/OmniboxViewUtil.java',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment