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. 4 5 package org.chromium.chrome.browser; 6 7 import android.graphics.Bitmap; 8 9 import org.chromium.base.CalledByNative; 10 import org.chromium.chrome.browser.profiles.Profile; 11 12 /** 13 * Provides access to the search provider's logo via the C++ LogoService. 14 */ 15 public class LogoBridge { 16 17 /** 18 * A logo for a search provider (e.g. the Yahoo! logo or Google doodle). 19 */ 20 public static class Logo { 21 /** 22 * The logo image. Non-null. 23 */ 24 public final Bitmap image; 25 26 /** 27 * The URL to navigate to when the user clicks on the logo. May be null. 28 */ 29 public final String onClickUrl; 30 31 /** 32 * The accessibility text describing the logo. May be null. 33 */ 34 public final String altText; 35 Logo(Bitmap image, String onClickUrl, String altText)36 public Logo(Bitmap image, String onClickUrl, String altText) { 37 this.image = image; 38 this.onClickUrl = onClickUrl; 39 this.altText = altText; 40 } 41 } 42 43 /** 44 * Observer for receiving the logo when it's available. 45 */ 46 public interface LogoObserver { 47 /** 48 * Called when the cached or fresh logo is available. This may be called up to two times, 49 * once with the cached logo and once with a freshly downloaded logo. 50 * 51 * @param logo The search provider's logo. 52 * @param fromCache Whether the logo was loaded from the cache. 53 */ 54 @CalledByNative("LogoObserver") onLogoAvailable(Logo logo, boolean fromCache)55 public void onLogoAvailable(Logo logo, boolean fromCache); 56 } 57 58 private long mNativeLogoBridge; 59 60 /** 61 * Creates a LogoBridge for getting the logo of the default search provider. 62 * 63 * @param profile Profile of the tab that will show the logo. 64 */ LogoBridge(Profile profile)65 public LogoBridge(Profile profile) { 66 mNativeLogoBridge = nativeInit(profile); 67 } 68 69 /** 70 * Cleans up the C++ side of this class. After calling this, LogoObservers passed to 71 * getCurrentLogo() will no longer receive updates. 72 */ destroy()73 public void destroy() { 74 assert mNativeLogoBridge != 0; 75 nativeDestroy(mNativeLogoBridge); 76 mNativeLogoBridge = 0; 77 } 78 79 /** 80 * Gets the current logo for the default search provider. 81 * 82 * @param logoObserver The observer to receive the cached and/or fresh logos when they're 83 * available. logoObserver.onLogoAvailable() may be called synchronously if 84 * the cached logo is already available. 85 */ getCurrentLogo(LogoObserver logoObserver)86 public void getCurrentLogo(LogoObserver logoObserver) { 87 nativeGetCurrentLogo(mNativeLogoBridge, logoObserver); 88 } 89 90 @Override finalize()91 protected void finalize() { 92 // Ensure that destroy() was called. 93 assert mNativeLogoBridge == 0; 94 } 95 96 @CalledByNative createLogo(Bitmap image, String onClickUrl, String altText)97 private static Logo createLogo(Bitmap image, String onClickUrl, String altText) { 98 return new Logo(image, onClickUrl, altText); 99 } 100 nativeInit(Profile profile)101 private native long nativeInit(Profile profile); nativeGetCurrentLogo(long nativeLogoBridge, LogoObserver logoObserver)102 private native void nativeGetCurrentLogo(long nativeLogoBridge, LogoObserver logoObserver); nativeDestroy(long nativeLogoBridge)103 private native void nativeDestroy(long nativeLogoBridge); 104 } 105