Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / android / java / src / org / chromium / chrome / browser / banners / AppBannerManager.java
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.banners;
6
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;
12
13 /**
14  * Manages an AppBannerView for a TabBase and its ContentView.
15  *
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.
20  *
21  * This Java-side class owns its native-side counterpart.
22  */
23 public class AppBannerManager {
24     /** Pointer to the native side AppBannerManager. */
25     private final long mNativePointer;
26
27     /** TabBase that the AppBannerView/AppBannerManager is owned by. */
28     private final TabBase mTabBase;
29
30     /** ContentView that the AppBannerView/AppBannerManager is currently attached to. */
31     private ContentView mContentView;
32
33     /**
34      * Constructs an AppBannerManager for the given tab.
35      * @param tab Tab that the AppBannerManager will be attached to.
36      */
37     public AppBannerManager(TabBase tab) {
38         mNativePointer = nativeInit();
39         mTabBase = tab;
40         mTabBase.addObserver(createTabObserver());
41         updatePointers();
42     }
43
44     /**
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.
48      */
49     private TabObserver createTabObserver() {
50         return new EmptyTabObserver() {
51             @Override
52             public void onWebContentsSwapped(TabBase tab, boolean didStartLoad,
53                     boolean didFinishLoad) {
54                 updatePointers();
55             }
56
57             @Override
58             public void onContentChanged(TabBase tab) {
59                 updatePointers();
60             }
61
62             @Override
63             public void onDestroyed(TabBase tab) {
64                 nativeDestroy(mNativePointer);
65             }
66         };
67     }
68
69     /**
70      * Updates which ContentView and WebContents the AppBannerView is monitoring.
71      */
72     private void updatePointers() {
73         if (mContentView != mTabBase.getContentView()) mContentView = mTabBase.getContentView();
74         nativeReplaceWebContents(mNativePointer, mTabBase.getWebContents());
75     }
76
77     /**
78      * Checks if app banners are enabled.
79      * @return True if banners are enabled, false otherwise.
80      */
81     public static boolean isEnabled() {
82         return nativeIsEnabled();
83     }
84
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);
90 }