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 package org.chromium.chrome.browser.banners;
7 import org.chromium.chrome.browser.EmptyTabObserver;
8 import org.chromium.chrome.browser.TabBase;
9 import org.chromium.chrome.browser.TabObserver;
10 import org.chromium.content.browser.ContentView;
11 import org.chromium.content_public.browser.WebContents;
14 * Manages an AppBannerView for a TabBase and its ContentView.
16 * The AppBannerManager manages a single AppBannerView, dismissing it when the user navigates to a
17 * new page or creating a new one when it detects that the current webpage is requesting a banner
18 * to be built. The actual observeration of the WebContents (which triggers the automatic creation
19 * and removal of banners, among other things) is done by the native-side AppBannerManager.
21 * This Java-side class owns its native-side counterpart.
23 public class AppBannerManager {
24 /** Pointer to the native side AppBannerManager. */
25 private final long mNativePointer;
27 /** TabBase that the AppBannerView/AppBannerManager is owned by. */
28 private final TabBase mTabBase;
30 /** ContentView that the AppBannerView/AppBannerManager is currently attached to. */
31 private ContentView mContentView;
34 * Constructs an AppBannerManager for the given tab.
35 * @param tab Tab that the AppBannerManager will be attached to.
37 public AppBannerManager(TabBase tab) {
38 mNativePointer = nativeInit();
40 mTabBase.addObserver(createTabObserver());
45 * Creates a TabObserver for monitoring a TabBase, used to react to changes in the ContentView
46 * or to trigger its own destruction.
47 * @return TabObserver that can be used to monitor a TabBase.
49 private TabObserver createTabObserver() {
50 return new EmptyTabObserver() {
52 public void onWebContentsSwapped(TabBase tab, boolean didStartLoad,
53 boolean didFinishLoad) {
58 public void onContentChanged(TabBase tab) {
63 public void onDestroyed(TabBase tab) {
64 nativeDestroy(mNativePointer);
70 * Updates which ContentView and WebContents the AppBannerView is monitoring.
72 private void updatePointers() {
73 if (mContentView != mTabBase.getContentView()) mContentView = mTabBase.getContentView();
74 nativeReplaceWebContents(mNativePointer, mTabBase.getWebContents());
78 * Checks if app banners are enabled.
79 * @return True if banners are enabled, false otherwise.
81 public static boolean isEnabled() {
82 return nativeIsEnabled();
85 private static native boolean nativeIsEnabled();
86 private native long nativeInit();
87 private native void nativeDestroy(long nativeAppBannerManager);
88 private native void nativeReplaceWebContents(
89 long nativeAppBannerManager, WebContents webContents);