Commit a372055d authored by Jinsuk Kim's avatar Jinsuk Kim Committed by Commit Bot

EphemeralTab: Disable contextual search for ephemeral tab

When ephemeral tab and contextual search both rely on overlay panel,
and it's best for only one of them to work at a time. This CL disables
contextual search while ephemeral tab is showing. The other case
is already handled as cs gets dismissed when ephemeral tab is
brought up.

Bug: 864619
Change-Id: I7b0149fd028e8a6940321436ddac9c7901dfd8e8
Reviewed-on: https://chromium-review.googlesource.com/c/1349151
Commit-Queue: Jinsuk Kim <jinsukkim@chromium.org>
Reviewed-by: default avatarDonn Denman <donnd@chromium.org>
Reviewed-by: default avatarMatthew Jones <mdjones@chromium.org>
Cr-Commit-Position: refs/heads/master@{#612064}
parent aea686c3
......@@ -354,7 +354,7 @@ public class ContextualSearchManager
}
/** @return Whether the {@code mSearchPanel} is not {@code null} and is showing. */
private boolean isSearchPanelShowing() {
boolean isSearchPanelShowing() {
return mSearchPanel != null && mSearchPanel.isShowing();
}
......
......@@ -5,11 +5,15 @@
package org.chromium.chrome.browser.contextualsearch;
import android.app.Activity;
import android.os.Handler;
import android.view.ContextMenu;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.chrome.browser.ChromeActivity;
import org.chromium.chrome.browser.compositor.CompositorViewHolder;
import org.chromium.chrome.browser.compositor.bottombar.OverlayPanel.StateChangeReason;
import org.chromium.chrome.browser.compositor.bottombar.OverlayPanelManager.OverlayPanelManagerObserver;
import org.chromium.chrome.browser.compositor.layouts.LayoutManager;
import org.chromium.chrome.browser.firstrun.FirstRunStatus;
import org.chromium.chrome.browser.fullscreen.FullscreenOptions;
import org.chromium.chrome.browser.locale.LocaleManager;
......@@ -19,6 +23,8 @@ import org.chromium.chrome.browser.search_engines.TemplateUrlService;
import org.chromium.chrome.browser.search_engines.TemplateUrlService.TemplateUrlServiceObserver;
import org.chromium.chrome.browser.tab.EmptyTabObserver;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.tab.Tab.TabHidingType;
import org.chromium.chrome.browser.tabmodel.TabModel.TabSelectionType;
import org.chromium.content_public.browser.GestureListenerManager;
import org.chromium.content_public.browser.GestureStateListener;
import org.chromium.content_public.browser.SelectionPopupController;
......@@ -58,6 +64,15 @@ public class ContextualSearchTabHelper
private long mNativeHelper;
/** {@code true} while observing other overlay panel via {@link OverlayPanelManagerObserver} */
private boolean mIsObservingPanel;
/**
* A Tab that has had Contextual Search unhooked from itself because another overlay is
* showing on it, or {@code null}.
*/
private Tab mUnhookedTab;
/**
* Creates a contextual search tab helper for the given tab.
* @param tab The tab whose contextual search actions will be handled by this helper.
......@@ -84,6 +99,74 @@ public class ContextualSearchTabHelper
mPxToDp = scaleFactor;
}
/**
* Used to disable contextual search (remove contextual search hooks) when other overlay
* panel comes into action.
*/
private OverlayPanelManagerObserver mPanelObserver = new OverlayPanelManagerObserver() {
@Override
public void onOverlayPanelShown() {
// This leaves the handling of the hooks to the responsibility of the activity tab.
// Restoring them will be then done by the tab that was the activity tab when
// the panel was shown.
Tab activityTab = mTab.getActivity().getActivityTabProvider().getActivityTab();
if (activityTab != mTab) return;
// Removes the hooks if the panel other than contextual search panel just got shown.
// Post the task on handler since |isSearchPanelShowing| is not reliable right after
// this event is invoked because it is based on the panel height which might be
// changing for animation effect.
new Handler().post(() -> {
if (!getContextualSearchManager(mTab).isSearchPanelShowing()) {
mUnhookedTab = activityTab;
updateContextualSearchHooks(mUnhookedTab.getWebContents());
}
});
}
@Override
public void onOverlayPanelHidden() {
if (mUnhookedTab != null) {
WebContents webContents = mUnhookedTab.getWebContents();
mUnhookedTab = null;
updateContextualSearchHooks(webContents);
}
}
};
/**
* Starts observing other panel using {@link OverlayPanelManagerObserver} if we're not
* already doing it.
* @param tab {@link Tab} to get the overlay panel manager to add the observer to.
*/
private void addPanelObserver(Tab tab) {
if (mIsObservingPanel || tab.isNativePage()) return;
LayoutManager manager = getLayoutManager(tab);
if (manager != null) {
manager.getOverlayPanelManager().addObserver(mPanelObserver);
mIsObservingPanel = true;
}
}
/**
* Stops observing other panel if we haven't stopped it already.
* @param tab {@link Tab} to get the overlay panel manager to remove the observer from.
*/
private void removePanelObserver(Tab tab) {
if (!mIsObservingPanel || tab.isNativePage()) return;
LayoutManager manager = getLayoutManager(tab);
if (manager != null) {
manager.getOverlayPanelManager().removeObserver(mPanelObserver);
mIsObservingPanel = false;
}
}
private static LayoutManager getLayoutManager(Tab tab) {
if (tab.getActivity() == null) return null;
CompositorViewHolder cvh = tab.getActivity().getCompositorViewHolder();
return cvh != null ? cvh.getLayoutManager() : null;
}
// ============================================================================================
// EmptyTabObserver overrides.
// ============================================================================================
......@@ -95,6 +178,23 @@ public class ContextualSearchTabHelper
if (manager != null) manager.onBasePageLoadStarted();
}
@Override
public void onPageLoadFinished(Tab tab, String url) {
// Makes sure the observer is added. Doing this in |onShown| doesn't cover all
// situations as it can be invoked before OverlayPanelManager is ready.
addPanelObserver(tab);
}
@Override
public void onShown(Tab tab, @TabSelectionType int type) {
addPanelObserver(tab);
}
@Override
public void onHidden(Tab tab, @TabHidingType int type) {
removePanelObserver(tab);
}
@Override
public void onContentChanged(Tab tab) {
// Native initialization happens after a page loads or content is changed to ensure profile
......@@ -131,6 +231,7 @@ public class ContextualSearchTabHelper
if (NetworkChangeNotifier.isInitialized()) {
NetworkChangeNotifier.removeConnectionTypeObserver(this);
}
removePanelObserver(tab);
removeContextualSearchHooks(mWebContents);
mWebContents = null;
mContextualSearchManager = null;
......@@ -158,8 +259,10 @@ public class ContextualSearchTabHelper
public void onActivityAttachmentChanged(Tab tab, boolean isAttached) {
if (isAttached) {
updateHooksForTab(tab);
addPanelObserver(tab);
} else {
removeContextualSearchHooks(mWebContents);
removePanelObserver(tab);
mContextualSearchManager = null;
}
}
......@@ -282,7 +385,7 @@ public class ContextualSearchTabHelper
// http://crbug.com/396934).
&& !manager.isRunningInCompatibilityMode()
&& !(mTab.isShowingErrorPage() || mTab.isShowingInterstitialPage())
&& isDeviceOnline(manager);
&& isDeviceOnline(manager) && mUnhookedTab == null;
}
/** @return Whether the device is online, or we have disabled online-detection. */
......
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