Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / android / java / src / org / chromium / chrome / browser / toolbar / ToolbarModel.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.toolbar;
6
7 import org.chromium.base.CalledByNative;
8 import org.chromium.chrome.browser.ui.toolbar.ToolbarModelSecurityLevel;
9 import org.chromium.content_public.browser.WebContents;
10
11 /**
12  * Provides a way of accessing toolbar data and state.
13  */
14 public class ToolbarModel {
15
16     /**
17      * Delegate for providing additional information to the model.
18      */
19     public interface ToolbarModelDelegate {
20         /**
21          * @return The currently active WebContents being used by the Toolbar.
22          */
23         @CalledByNative("ToolbarModelDelegate")
24         WebContents getActiveWebContents();
25     }
26
27     private long mNativeToolbarModelAndroid;
28
29     /**
30      * Fetch the security level for a given web contents.
31      *
32      * @param webContents The web contents to get the security level for.
33      * @return The ToolbarModelSecurityLevel for the specified web contents.
34      *
35      * @see ToolbarModelSecurityLevel
36      */
37     public static int getSecurityLevelForWebContents(WebContents webContents) {
38         if (webContents == null) return ToolbarModelSecurityLevel.NONE;
39         return nativeGetSecurityLevelForWebContents(webContents);
40     }
41
42     /**
43      * Initialize the native counterpart of this model.
44      * @param delegate The delegate that will be used by the model.
45      */
46     public void initialize(ToolbarModelDelegate delegate) {
47         mNativeToolbarModelAndroid = nativeInit(delegate);
48     }
49
50     /**
51      * Destroys the native ToolbarModel.
52      */
53     public void destroy() {
54         if (mNativeToolbarModelAndroid == 0) return;
55         nativeDestroy(mNativeToolbarModelAndroid);
56         mNativeToolbarModelAndroid = 0;
57     }
58
59     /** @return The formatted text (URL or search terms) for display. */
60     public String getText() {
61         if (mNativeToolbarModelAndroid == 0) return null;
62         return nativeGetText(mNativeToolbarModelAndroid);
63     }
64
65     /** @return The parameter in the url that triggers query extraction. */
66     public String getQueryExtractionParam() {
67         if (mNativeToolbarModelAndroid == 0) return null;
68         return nativeGetQueryExtractionParam(mNativeToolbarModelAndroid);
69     }
70
71     /** @return The chip text from the search URL. */
72     public String getCorpusChipText() {
73         if (mNativeToolbarModelAndroid == 0) return null;
74         return nativeGetCorpusChipText(mNativeToolbarModelAndroid);
75     }
76
77     private static native int nativeGetSecurityLevelForWebContents(WebContents webContents);
78
79     private native long nativeInit(ToolbarModelDelegate delegate);
80     private native void nativeDestroy(long nativeToolbarModelAndroid);
81     private native String nativeGetText(long nativeToolbarModelAndroid);
82     private native String nativeGetQueryExtractionParam(long nativeToolbarModelAndroid);
83     private native String nativeGetCorpusChipText(long nativeToolbarModelAndroid);
84 }