Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/banners/AppBannerManager.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/banners/AppBannerManager.java b/chrome/android/java/src/org/chromium/chrome/browser/banners/AppBannerManager.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2e2052e86c76bb2284b008bef6844a41490cf610 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/banners/AppBannerManager.java |
| @@ -0,0 +1,97 @@ |
| +// 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.banners; |
| + |
| +import org.chromium.chrome.browser.EmptyTabObserver; |
| +import org.chromium.chrome.browser.TabBase; |
| +import org.chromium.chrome.browser.TabObserver; |
| +import org.chromium.content.browser.ContentView; |
| + |
| +/** |
| + * Manages an AppBannerView for a TabBase and its ContentView. |
| + * |
| + * The AppBannerManager manages a single AppBannerView, dismissing it when the user navigates to a |
| + * new page or creating a new one when it detects that the current webpage is requesting a banner |
| + * to be built. The actual observeration of the WebContents (which triggers the automatic creation |
| + * and removal of banners, among other things) is done by the native-side AppBannerManager. |
| + * |
| + * This Java-side class owns its native-side counterpart. |
| + */ |
| +public class AppBannerManager { |
| + /** Pointer to the native side AppBannerManager. */ |
| + private final long mNativePointer; |
| + |
| + /** Monitors changes in the TabBase that owns the AppBannerManager. */ |
| + private final TabObserver mTabObserver; |
| + |
| + /** ContentView that the AppBannerView/AppBannerManager is currently attached to. */ |
| + private ContentView mContentView; |
| + |
| + /** |
| + * Constructs an AppBannerManager for the given tab. |
| + * @param tab Tab that the AppBannerManager will be attached to. |
| + */ |
| + public AppBannerManager(TabBase tab) { |
| + mNativePointer = nativeInit(); |
| + mTabObserver = createTabObserver(); |
| + tab.addObserver(mTabObserver); |
| + setContentView(tab.getContentView()); |
| + } |
| + |
| + /** |
| + * Creates a TabObserver for monitoring a TabBase, used to react to changes in the ContentView |
| + * or to trigger its own destruction. |
| + * @return TabObserver that can be used to monitor a TabBase. |
| + */ |
| + private TabObserver createTabObserver() { |
| + return new EmptyTabObserver() { |
|
Ted C
2014/02/11 17:48:45
Hmm...I wonder if we are missing the callback for
gone
2014/02/11 19:04:14
Added the observer callback you mentioned.
|
| + @Override |
| + public void onWebContentsSwapped(TabBase tab, boolean didStartLoad, |
| + boolean didFinishLoad) { |
| + setContentView(tab.getContentView()); |
| + } |
| + |
| + @Override |
| + public void onContentChanged(TabBase tab) { |
| + setContentView(tab.getContentView()); |
| + } |
| + |
| + @Override |
| + public void onDestroyed(TabBase tab) { |
| + nativeDestroy(mNativePointer); |
| + } |
| + }; |
| + } |
| + |
| + /** |
| + * Updates which ContentView the AppBannerView is monitoring. |
| + * @param contentView ContentView to monitor. |
| + */ |
| + private void setContentView(ContentView contentView) { |
| + if (mContentView != contentView) { |
|
Ted C
2014/02/11 17:48:45
this looks like it will all fit on one line
gone
2014/02/11 19:04:14
Done.
|
| + mContentView = contentView; |
| + } |
| + |
| + // Alert the native side AppBannerManager that it may need to observe a new WebContents. |
| + if (mContentView != null) { |
| + nativeReplaceWebContents( |
| + mNativePointer, mContentView.getContentViewCore().getNativeContentViewCore()); |
| + } |
| + } |
| + |
| + /** |
| + * Checks if app banners are enabled. |
| + * @return True if banners are enabled, false otherwise. |
| + */ |
| + public static boolean isEnabled() { |
| + return nativeIsEnabled(); |
| + } |
| + |
| + private static native boolean nativeIsEnabled(); |
| + private native long nativeInit(); |
| + private native void nativeDestroy(long nativeAppBannerManager); |
| + private native void nativeReplaceWebContents( |
| + long nativeAppBannerManager, long contentViewCorePtr); |
| +} |