Commit f2b4b596 authored by Patrick Noland's avatar Patrick Noland Committed by Commit Bot

[ToolbarMVC] Set a profileSupplier on LocationBar

Rather than setting the profile each each time directly, we inject a supplier that can be observed. This reduces the complexity of the interface and will let us constructor inject the supplier more easily in the future.

Change-Id: Ibd587e1a5496e5bca3d27eb7120aacbedb2ff138
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2419635
Commit-Queue: Patrick Noland <pnoland@chromium.org>
Reviewed-by: default avatarTed Choc <tedchoc@chromium.org>
Reviewed-by: default avatarBrandon Wylie <wylieb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#810011}
parent e4c40aae
...@@ -11,6 +11,7 @@ import android.view.Window; ...@@ -11,6 +11,7 @@ import android.view.Window;
import androidx.annotation.IntDef; import androidx.annotation.IntDef;
import org.chromium.base.supplier.ObservableSupplier;
import org.chromium.base.supplier.Supplier; import org.chromium.base.supplier.Supplier;
import org.chromium.chrome.browser.ActivityTabProvider; import org.chromium.chrome.browser.ActivityTabProvider;
import org.chromium.chrome.browser.WindowDelegate; import org.chromium.chrome.browser.WindowDelegate;
...@@ -83,17 +84,6 @@ public interface LocationBar extends UrlBarDelegate, FakeboxDelegate { ...@@ -83,17 +84,6 @@ public interface LocationBar extends UrlBarDelegate, FakeboxDelegate {
*/ */
void onTabLoadingNTP(NewTabPage ntp); void onTabLoadingNTP(NewTabPage ntp);
/**
* Called to set the autocomplete profile to a new profile.
*/
void setAutocompleteProfile(Profile profile);
/**
* Specify whether location bar should present icons when focused.
* @param showIcon True if we should show the icons when the url is focused.
*/
void setShowIconsWhenUrlFocused(boolean showIcon);
/** /**
* Call to force the UI to update the state of various buttons based on whether or not the * Call to force the UI to update the state of various buttons based on whether or not the
* current tab is incognito. * current tab is incognito.
...@@ -225,4 +215,11 @@ public interface LocationBar extends UrlBarDelegate, FakeboxDelegate { ...@@ -225,4 +215,11 @@ public interface LocationBar extends UrlBarDelegate, FakeboxDelegate {
*/ */
void updateSearchEngineStatusIcon(boolean shouldShowSearchEngineLogo, void updateSearchEngineStatusIcon(boolean shouldShowSearchEngineLogo,
boolean isSearchEngineGoogle, String searchEngineUrl); boolean isSearchEngineGoogle, String searchEngineUrl);
/**
* Sets the (observable) supplier of the active profile. This supplier will notify observers of
* changes to the active profile, e.g. when selecting an incognito tab model.
* @param profileSupplier The supplier of the active profile.
*/
void setProfileSupplier(ObservableSupplier<Profile> profileSupplier);
} }
...@@ -30,10 +30,13 @@ import androidx.core.view.MarginLayoutParamsCompat; ...@@ -30,10 +30,13 @@ import androidx.core.view.MarginLayoutParamsCompat;
import androidx.core.view.ViewCompat; import androidx.core.view.ViewCompat;
import org.chromium.base.ApiCompatibilityUtils; import org.chromium.base.ApiCompatibilityUtils;
import org.chromium.base.Callback;
import org.chromium.base.CallbackController;
import org.chromium.base.CommandLine; import org.chromium.base.CommandLine;
import org.chromium.base.ObserverList; import org.chromium.base.ObserverList;
import org.chromium.base.metrics.RecordHistogram; import org.chromium.base.metrics.RecordHistogram;
import org.chromium.base.metrics.RecordUserAction; import org.chromium.base.metrics.RecordUserAction;
import org.chromium.base.supplier.ObservableSupplier;
import org.chromium.base.supplier.Supplier; import org.chromium.base.supplier.Supplier;
import org.chromium.chrome.R; import org.chromium.chrome.R;
import org.chromium.chrome.browser.ActivityTabProvider; import org.chromium.chrome.browser.ActivityTabProvider;
...@@ -136,6 +139,9 @@ public class LocationBarLayout extends FrameLayout ...@@ -136,6 +139,9 @@ public class LocationBarLayout extends FrameLayout
private Runnable mKeyboardResizeModeTask; private Runnable mKeyboardResizeModeTask;
private Runnable mKeyboardHideTask; private Runnable mKeyboardHideTask;
private boolean mKeyboardShouldShow; private boolean mKeyboardShouldShow;
private ObservableSupplier<Profile> mProfileSupplier;
private Callback<Profile> mProfileSupplierObserver;
private CallbackController mCallbackController = new CallbackController();
/** /**
* Class to handle input from a hardware keyboard when the focus is on the URL bar. In * Class to handle input from a hardware keyboard when the focus is on the URL bar. In
...@@ -242,6 +248,17 @@ public class LocationBarLayout extends FrameLayout ...@@ -242,6 +248,17 @@ public class LocationBarLayout extends FrameLayout
mAssistantVoiceSearchService.destroy(); mAssistantVoiceSearchService.destroy();
mAssistantVoiceSearchService = null; mAssistantVoiceSearchService = null;
} }
if (mCallbackController != null) {
mCallbackController.destroy();
mCallbackController = null;
}
if (mProfileSupplier != null) {
mProfileSupplier.removeObserver(mProfileSupplierObserver);
mProfileSupplier = null;
mProfileSupplierObserver = null;
}
} }
@Override @Override
...@@ -362,6 +379,7 @@ public class LocationBarLayout extends FrameLayout ...@@ -362,6 +379,7 @@ public class LocationBarLayout extends FrameLayout
TemplateUrlServiceFactory.get(), GSAState.getInstance(getContext()), this); TemplateUrlServiceFactory.get(), GSAState.getInstance(getContext()), this);
mVoiceRecognitionHandler.setAssistantVoiceSearchService(mAssistantVoiceSearchService); mVoiceRecognitionHandler.setAssistantVoiceSearchService(mAssistantVoiceSearchService);
onAssistantVoiceSearchServiceChanged(); onAssistantVoiceSearchServiceChanged();
setProfile(mProfileSupplier.get());
} }
/** /**
...@@ -382,22 +400,28 @@ public class LocationBarLayout extends FrameLayout ...@@ -382,22 +400,28 @@ public class LocationBarLayout extends FrameLayout
mStatusCoordinator.setShouldAnimateIconChanges(shouldAnimate); mStatusCoordinator.setShouldAnimateIconChanges(shouldAnimate);
} }
@Override
public void setProfileSupplier(ObservableSupplier<Profile> profileSupplier) {
assert profileSupplier != null;
assert mProfileSupplier == null;
mProfileSupplier = profileSupplier;
mProfileSupplierObserver = mCallbackController.makeCancelable(this::setProfile);
mProfileSupplier.addObserver(mProfileSupplierObserver);
}
/** /**
* Updates the profile used for generating autocomplete suggestions. * Updates the profile used by this LocationBar, for, e.g. determining incognito status or
* generating autocomplete suggestions..
* @param profile The profile to be used. * @param profile The profile to be used.
*/ */
@Override private void setProfile(Profile profile) {
public void setAutocompleteProfile(Profile profile) { if (profile == null || !mNativeInitialized) return;
// This will only be called once at least one tab exists, and the tab model is told to
// update its state. During Chrome initialization the tab model update happens after the
// call to onNativeLibraryReady, so this assert will not fire.
assert mNativeInitialized : "Setting Autocomplete Profile before native side initialized";
mAutocompleteCoordinator.setAutocompleteProfile(profile); mAutocompleteCoordinator.setAutocompleteProfile(profile);
mOmniboxPrerender.initializeForProfile(profile); mOmniboxPrerender.initializeForProfile(profile);
}
@Override setShowIconsWhenUrlFocused(
public void setShowIconsWhenUrlFocused(boolean showIcon) {} SearchEngineLogoUtils.shouldShowSearchEngineLogo(profile.isOffTheRecord()));
}
/** Focuses the current page. */ /** Focuses the current page. */
private void focusCurrentTab() { private void focusCurrentTab() {
...@@ -441,6 +465,12 @@ public class LocationBarLayout extends FrameLayout ...@@ -441,6 +465,12 @@ public class LocationBarLayout extends FrameLayout
return mUrlFocusChangeInProgress; return mUrlFocusChangeInProgress;
} }
/**
* Specify whether location bar should present icons when focused.
* @param showIcon True if we should show the icons when the url is focused.
*/
protected void setShowIconsWhenUrlFocused(boolean showIcon) {}
/** /**
* @param inProgress Whether a URL focus change is taking place. * @param inProgress Whether a URL focus change is taking place.
*/ */
......
...@@ -22,6 +22,7 @@ import org.chromium.base.Callback; ...@@ -22,6 +22,7 @@ import org.chromium.base.Callback;
import org.chromium.base.IntentUtils; import org.chromium.base.IntentUtils;
import org.chromium.base.Log; import org.chromium.base.Log;
import org.chromium.base.metrics.RecordUserAction; import org.chromium.base.metrics.RecordUserAction;
import org.chromium.base.supplier.ObservableSupplierImpl;
import org.chromium.chrome.R; import org.chromium.chrome.R;
import org.chromium.chrome.browser.IntentHandler; import org.chromium.chrome.browser.IntentHandler;
import org.chromium.chrome.browser.WebContentsFactory; import org.chromium.chrome.browser.WebContentsFactory;
...@@ -32,6 +33,7 @@ import org.chromium.chrome.browser.document.ChromeLauncherActivity; ...@@ -32,6 +33,7 @@ import org.chromium.chrome.browser.document.ChromeLauncherActivity;
import org.chromium.chrome.browser.init.AsyncInitializationActivity; import org.chromium.chrome.browser.init.AsyncInitializationActivity;
import org.chromium.chrome.browser.init.SingleWindowKeyboardVisibilityDelegate; import org.chromium.chrome.browser.init.SingleWindowKeyboardVisibilityDelegate;
import org.chromium.chrome.browser.locale.LocaleManager; import org.chromium.chrome.browser.locale.LocaleManager;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.tab.Tab; import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.tab.TabBuilder; import org.chromium.chrome.browser.tab.TabBuilder;
import org.chromium.chrome.browser.tab.TabDelegateFactory; import org.chromium.chrome.browser.tab.TabDelegateFactory;
...@@ -112,6 +114,7 @@ public class SearchActivity extends AsyncInitializationActivity ...@@ -112,6 +114,7 @@ public class SearchActivity extends AsyncInitializationActivity
private SnackbarManager mSnackbarManager; private SnackbarManager mSnackbarManager;
private SearchBoxDataProvider mSearchBoxDataProvider; private SearchBoxDataProvider mSearchBoxDataProvider;
private Tab mTab; private Tab mTab;
private ObservableSupplierImpl<Profile> mProfileSupplier = new ObservableSupplierImpl<>();
@Override @Override
protected boolean isStartedUpCorrectly(Intent intent) { protected boolean isStartedUpCorrectly(Intent intent) {
...@@ -164,6 +167,7 @@ public class SearchActivity extends AsyncInitializationActivity ...@@ -164,6 +167,7 @@ public class SearchActivity extends AsyncInitializationActivity
mSearchBox.setToolbarDataProvider(mSearchBoxDataProvider); mSearchBox.setToolbarDataProvider(mSearchBoxDataProvider);
mSearchBox.initializeControls( mSearchBox.initializeControls(
new WindowDelegate(getWindow()), getWindowAndroid(), null, null, null, null); new WindowDelegate(getWindow()), getWindowAndroid(), null, null, null, null);
mSearchBox.setProfileSupplier(mProfileSupplier);
// Kick off everything needed for the user to type into the box. // Kick off everything needed for the user to type into the box.
beginQuery(); beginQuery();
...@@ -237,16 +241,19 @@ public class SearchActivity extends AsyncInitializationActivity ...@@ -237,16 +241,19 @@ public class SearchActivity extends AsyncInitializationActivity
return null; return null;
} }
}; };
WebContents webContents = WebContentsFactory.createWebContents(false, false);
mTab = new TabBuilder() mTab = new TabBuilder()
.setWindow(getWindowAndroid()) .setWindow(getWindowAndroid())
.setLaunchType(TabLaunchType.FROM_EXTERNAL_APP) .setLaunchType(TabLaunchType.FROM_EXTERNAL_APP)
.setWebContents(WebContentsFactory.createWebContents(false, false)) .setWebContents(webContents)
.setDelegateFactory(factory) .setDelegateFactory(factory)
.build(); .build();
mTab.loadUrl(new LoadUrlParams(ContentUrlConstants.ABOUT_BLANK_DISPLAY_URL)); mTab.loadUrl(new LoadUrlParams(ContentUrlConstants.ABOUT_BLANK_DISPLAY_URL));
mSearchBoxDataProvider.onNativeLibraryReady(mTab); mSearchBoxDataProvider.onNativeLibraryReady(mTab);
mSearchBox.onNativeLibraryReady(); mSearchBox.onNativeLibraryReady();
mProfileSupplier.set(Profile.fromWebContents(webContents));
// Force the user to choose a search engine if they have to. // Force the user to choose a search engine if they have to.
final Callback<Boolean> onSearchEngineFinalizedCallback = new Callback<Boolean>() { final Callback<Boolean> onSearchEngineFinalizedCallback = new Callback<Boolean>() {
......
...@@ -9,10 +9,8 @@ import android.os.Handler; ...@@ -9,10 +9,8 @@ import android.os.Handler;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.View; import android.view.View;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
import org.chromium.chrome.R; import org.chromium.chrome.R;
import org.chromium.chrome.browser.locale.LocaleManager; import org.chromium.chrome.browser.locale.LocaleManager;
import org.chromium.chrome.browser.omnibox.LocationBarLayout; import org.chromium.chrome.browser.omnibox.LocationBarLayout;
...@@ -20,7 +18,6 @@ import org.chromium.chrome.browser.omnibox.UrlBar; ...@@ -20,7 +18,6 @@ import org.chromium.chrome.browser.omnibox.UrlBar;
import org.chromium.chrome.browser.omnibox.UrlBarCoordinator.SelectionState; import org.chromium.chrome.browser.omnibox.UrlBarCoordinator.SelectionState;
import org.chromium.chrome.browser.omnibox.UrlBarData; import org.chromium.chrome.browser.omnibox.UrlBarData;
import org.chromium.chrome.browser.omnibox.voice.VoiceRecognitionHandler; import org.chromium.chrome.browser.omnibox.voice.VoiceRecognitionHandler;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.toolbar.top.ToolbarPhone; import org.chromium.chrome.browser.toolbar.top.ToolbarPhone;
/** Implementation of the {@link LocationBarLayout} that is displayed for widget searches. */ /** Implementation of the {@link LocationBarLayout} that is displayed for widget searches. */
...@@ -79,8 +76,6 @@ public class SearchActivityLocationBarLayout extends LocationBarLayout { ...@@ -79,8 +76,6 @@ public class SearchActivityLocationBarLayout extends LocationBarLayout {
super.onNativeLibraryReady(); super.onNativeLibraryReady();
mNativeLibraryReady = true; mNativeLibraryReady = true;
setAutocompleteProfile(Profile.getLastUsedRegularProfile());
mPendingSearchPromoDecision = LocaleManager.getInstance().needToCheckForSearchEnginePromo(); mPendingSearchPromoDecision = LocaleManager.getInstance().needToCheckForSearchEnginePromo();
getAutocompleteCoordinator().setShouldPreventOmniboxAutocomplete( getAutocompleteCoordinator().setShouldPreventOmniboxAutocomplete(
mPendingSearchPromoDecision); mPendingSearchPromoDecision);
......
...@@ -130,9 +130,7 @@ public class ToolbarManager implements UrlFocusChangeListener, ThemeColorObserve ...@@ -130,9 +130,7 @@ public class ToolbarManager implements UrlFocusChangeListener, ThemeColorObserve
private final ActivityTabProvider mActivityTabProvider; private final ActivityTabProvider mActivityTabProvider;
private final LocationBarModel mLocationBarModel; private final LocationBarModel mLocationBarModel;
private ObservableSupplier<BookmarkBridge> mBookmarkBridgeSupplier; private ObservableSupplier<BookmarkBridge> mBookmarkBridgeSupplier;
private ObservableSupplier<Profile> mProfileSupplier;
private final Callback<BookmarkBridge> mBookmarkBridgeSupplierObserver; private final Callback<BookmarkBridge> mBookmarkBridgeSupplierObserver;
private final Callback<Profile> mProfileSupplierObserver;
private TemplateUrlServiceObserver mTemplateUrlObserver; private TemplateUrlServiceObserver mTemplateUrlObserver;
private LocationBar mLocationBar; private LocationBar mLocationBar;
private FindToolbarManager mFindToolbarManager; private FindToolbarManager mFindToolbarManager;
...@@ -250,9 +248,6 @@ public class ToolbarManager implements UrlFocusChangeListener, ThemeColorObserve ...@@ -250,9 +248,6 @@ public class ToolbarManager implements UrlFocusChangeListener, ThemeColorObserve
// reference the same object. // reference the same object.
mBookmarkBridgeSupplierObserver = this::setBookmarkBridge; mBookmarkBridgeSupplierObserver = this::setBookmarkBridge;
mBookmarkBridgeSupplier.addObserver(mBookmarkBridgeSupplierObserver); mBookmarkBridgeSupplier.addObserver(mBookmarkBridgeSupplierObserver);
mProfileSupplier = profileSupplier;
mProfileSupplierObserver = this::setCurrentProfile;
mProfileSupplier.addObserver(mProfileSupplierObserver);
mOverviewModeBehaviorSupplier = overviewModeBehaviorSupplier; mOverviewModeBehaviorSupplier = overviewModeBehaviorSupplier;
mOverviewModeBehaviorSupplier.onAvailable( mOverviewModeBehaviorSupplier.onAvailable(
...@@ -286,7 +281,7 @@ public class ToolbarManager implements UrlFocusChangeListener, ThemeColorObserve ...@@ -286,7 +281,7 @@ public class ToolbarManager implements UrlFocusChangeListener, ThemeColorObserve
mToolbarTabController = new ToolbarTabControllerImpl(mLocationBarModel::getTab, mToolbarTabController = new ToolbarTabControllerImpl(mLocationBarModel::getTab,
// clang-format off // clang-format off
() -> mShowStartSurfaceSupplier != null && mShowStartSurfaceSupplier.get(), () -> mShowStartSurfaceSupplier != null && mShowStartSurfaceSupplier.get(),
mProfileSupplier, () -> mBottomControlsCoordinator, this::updateButtonStatus); profileSupplier, () -> mBottomControlsCoordinator, this::updateButtonStatus);
// clang-format on // clang-format on
BrowserStateBrowserControlsVisibilityDelegate controlsVisibilityDelegate = BrowserStateBrowserControlsVisibilityDelegate controlsVisibilityDelegate =
...@@ -327,6 +322,7 @@ public class ToolbarManager implements UrlFocusChangeListener, ThemeColorObserve ...@@ -327,6 +322,7 @@ public class ToolbarManager implements UrlFocusChangeListener, ThemeColorObserve
mToolbar.setPaintInvalidator(invalidator); mToolbar.setPaintInvalidator(invalidator);
mActionModeController.setTabStripHeight(mToolbar.getTabStripHeight()); mActionModeController.setTabStripHeight(mToolbar.getTabStripHeight());
mLocationBar = mToolbar.getLocationBar(); mLocationBar = mToolbar.getLocationBar();
mLocationBar.setProfileSupplier(profileSupplier);
mLocationBar.setToolbarDataProvider(mLocationBarModel); mLocationBar.setToolbarDataProvider(mLocationBarModel);
mLocationBar.addUrlFocusChangeListener(this); mLocationBar.addUrlFocusChangeListener(this);
mLocationBar.setDefaultTextEditActionModeCallback( mLocationBar.setDefaultTextEditActionModeCallback(
...@@ -760,7 +756,6 @@ public class ToolbarManager implements UrlFocusChangeListener, ThemeColorObserve ...@@ -760,7 +756,6 @@ public class ToolbarManager implements UrlFocusChangeListener, ThemeColorObserve
mControlContainer.setReadyForBitmapCapture(true); mControlContainer.setReadyForBitmapCapture(true);
} }
setCurrentProfile(mProfileSupplier.get());
TraceEvent.end("ToolbarManager.initializeWithNative"); TraceEvent.end("ToolbarManager.initializeWithNative");
} }
...@@ -895,11 +890,6 @@ public class ToolbarManager implements UrlFocusChangeListener, ThemeColorObserve ...@@ -895,11 +890,6 @@ public class ToolbarManager implements UrlFocusChangeListener, ThemeColorObserve
mFindToolbarManager = null; mFindToolbarManager = null;
} }
if (mProfileSupplier != null) {
mProfileSupplier.removeObserver(mProfileSupplierObserver);
mProfileSupplier = null;
}
if (mMenuButtonCoordinator != null) { if (mMenuButtonCoordinator != null) {
mMenuButtonCoordinator.destroy(); mMenuButtonCoordinator.destroy();
mMenuButtonCoordinator = null; mMenuButtonCoordinator = null;
...@@ -1236,17 +1226,6 @@ public class ToolbarManager implements UrlFocusChangeListener, ThemeColorObserve ...@@ -1236,17 +1226,6 @@ public class ToolbarManager implements UrlFocusChangeListener, ThemeColorObserve
updateButtonStatus(); updateButtonStatus();
} }
// TODO(https://crbug.com/865801): Inject a profile supplier directly to mLocationBar and remove
// setCurrentProfile.
private void setCurrentProfile(Profile profile) {
if (profile != null && mInitializedWithNative) {
mLocationBar.setAutocompleteProfile(profile);
mLocationBar.setShowIconsWhenUrlFocused(
SearchEngineLogoUtils.shouldShowSearchEngineLogo(
mLocationBarModel.isIncognito()));
}
}
private void setBookmarkBridge(BookmarkBridge bookmarkBridge) { private void setBookmarkBridge(BookmarkBridge bookmarkBridge) {
if (bookmarkBridge == null) return; if (bookmarkBridge == null) return;
bookmarkBridge.addObserver(mBookmarksObserver); bookmarkBridge.addObserver(mBookmarksObserver);
......
...@@ -41,6 +41,7 @@ import org.chromium.base.ApiCompatibilityUtils; ...@@ -41,6 +41,7 @@ import org.chromium.base.ApiCompatibilityUtils;
import org.chromium.base.ThreadUtils; import org.chromium.base.ThreadUtils;
import org.chromium.base.library_loader.LibraryLoader; import org.chromium.base.library_loader.LibraryLoader;
import org.chromium.base.metrics.RecordUserAction; import org.chromium.base.metrics.RecordUserAction;
import org.chromium.base.supplier.ObservableSupplier;
import org.chromium.base.supplier.Supplier; import org.chromium.base.supplier.Supplier;
import org.chromium.base.task.PostTask; import org.chromium.base.task.PostTask;
import org.chromium.chrome.R; import org.chromium.chrome.R;
...@@ -817,10 +818,7 @@ public class CustomTabToolbar extends ToolbarLayout implements View.OnLongClickL ...@@ -817,10 +818,7 @@ public class CustomTabToolbar extends ToolbarLayout implements View.OnLongClickL
public void onTabLoadingNTP(NewTabPage ntp) {} public void onTabLoadingNTP(NewTabPage ntp) {}
@Override @Override
public void setAutocompleteProfile(Profile profile) {} public void setProfileSupplier(ObservableSupplier<Profile> profileSupplier) {}
@Override
public void setShowIconsWhenUrlFocused(boolean showIcon) {}
@Override @Override
public void setUnfocusedWidth(int unfocusedWidth) {} public void setUnfocusedWidth(int unfocusedWidth) {}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment