1 // Copyright 2020 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.omnibox;
6 
7 import android.content.res.ColorStateList;
8 
9 import androidx.annotation.ColorRes;
10 import androidx.annotation.DrawableRes;
11 import androidx.annotation.NonNull;
12 import androidx.annotation.Nullable;
13 import androidx.annotation.StringRes;
14 
15 import org.chromium.chrome.browser.profiles.Profile;
16 import org.chromium.chrome.browser.tab.Tab;
17 import org.chromium.chrome.browser.toolbar.NewTabPageDelegate;
18 import org.chromium.components.security_state.ConnectionSecurityLevel;
19 
20 /**
21  * Interface defining a provider for data needed by the {@link LocationBar}.
22  */
23 // TODO(crbug.com/1142887): Refine split between LocationBar properties and sub-component
24 // properties, e.g. security state, which is only used by the status icon.
25 public interface LocationBarDataProvider {
26     /**
27      * Observer interface for consumers who wish to subscribe to updates of LocationBarData.
28      * Since LocationBarDataProvider data is typically calculated lazily, individual observer
29      * methods don't directly supply the updated value. Instead, the expectation is that the
30      * consumer will query the data it cares about.
31      */
32     interface Observer {
onTitleChanged()33         void onTitleChanged();
34         // TODO(https://crbug.com/1139481): Add methods for other LocationBarDataProvider
35         // data, e.g. NTP and security state.
onUrlChanged()36         void onUrlChanged();
37     }
38 
39     /** Adds an observer of changes to LocationBarDataProvider's data. */
addObserver(Observer observer)40     void addObserver(Observer observer);
41 
42     /** Removes an observer of changes to LocationBarDataProvider's data. */
removeObserver(Observer observer)43     void removeObserver(Observer observer);
44 
45     /** Returns The url for the currently active page.*/
46     @NonNull
getCurrentUrl()47     String getCurrentUrl();
48 
49     /** Returns the delegate for the NewTabPage shown for the current tab. */
50     @NonNull
getNewTabPageDelegate()51     NewTabPageDelegate getNewTabPageDelegate();
52 
53     /** Returns whether the currently active page is loading. */
isLoading()54     default boolean isLoading() {
55         if (isInOverviewAndShowingOmnibox()) return false;
56         Tab tab = getTab();
57         return tab != null && tab.isLoading();
58     }
59 
60     /** Returns whether the current page is in an incognito browser context. */
isIncognito()61     boolean isIncognito();
62 
63     /** Returns the currently active tab, if there is one. */
64     @Nullable
getTab()65     Tab getTab();
66 
67     /** Returns whether the LocationBarDataProvider currently has an active tab. */
hasTab()68     boolean hasTab();
69 
70     /**
71      * Returns whether the LocationBar's embedder is currently being displayed in overview mode and
72      *         showing the
73      *  omnibox.
74      */
isInOverviewAndShowingOmnibox()75     boolean isInOverviewAndShowingOmnibox();
76 
77     /** Returns the current {@link Profile}. */
getProfile()78     Profile getProfile();
79 
80     /** Returns the contents of the {@link org.chromium.chrome.browser.omnibox.UrlBar}. */
getUrlBarData()81     UrlBarData getUrlBarData();
82 
83     /** Returns the title of the current page, or the empty string if there is currently no tab. */
getTitle()84     String getTitle();
85 
86     /** Returns the primary color to use for the background.*/
getPrimaryColor()87     int getPrimaryColor();
88 
89     /** Returns whether the current primary color is a brand color. */
isUsingBrandColor()90     boolean isUsingBrandColor();
91 
92     /** Returns whether the page currently shown is an offline page. */
isOfflinePage()93     boolean isOfflinePage();
94 
95     /** Returns whether the page currently shown is a preview. */
isPreview()96     boolean isPreview();
97 
98     /** Returns whether the page currently shown is a paint preview. */
isPaintPreview()99     default boolean isPaintPreview() {
100         return false;
101     }
102 
103     /** Returns the current {@link ConnectionSecurityLevel}. */
104     @ConnectionSecurityLevel
getSecurityLevel()105     int getSecurityLevel();
106 
107     /**
108      * Returns the current page classification.
109      * @param isFocusedFromFakebox If the omnibox focus originated from the fakebox.
110      * @return Integer value representing the OmniboxEventProto.PageClassification.
111      */
getPageClassification(boolean isFocusedFromFakebox)112     int getPageClassification(boolean isFocusedFromFakebox);
113 
114     /**
115      * Returns the resource ID of the icon that should be displayed or 0 if no icon should be shown.
116      * @param isTablet Whether or not the display context of the icon is a tablet.
117      */
118     @DrawableRes
getSecurityIconResource(boolean isTablet)119     int getSecurityIconResource(boolean isTablet);
120 
121     /** Returns The {@link ColorStateList} to use to tint the security state icon. */
122     @ColorRes
getSecurityIconColorStateList()123     int getSecurityIconColorStateList();
124 
125     /** Returns the resource ID of the content description for the security icon. */
126     @StringRes
getSecurityIconContentDescriptionResourceId()127     int getSecurityIconContentDescriptionResourceId();
128 }
129