| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.tab; | 5 package org.chromium.chrome.browser.tab; |
| 6 | 6 |
| 7 import org.chromium.base.VisibleForTesting; | 7 import org.chromium.base.VisibleForTesting; |
| 8 import org.chromium.base.metrics.RecordHistogram; | 8 import org.chromium.base.metrics.RecordHistogram; |
| 9 import org.chromium.chrome.R; | 9 import org.chromium.chrome.R; |
| 10 import org.chromium.chrome.browser.ChromeApplication; | 10 import org.chromium.chrome.browser.ChromeApplication; |
| 11 import org.chromium.chrome.browser.datausage.DataUseTabUIManager; | 11 import org.chromium.chrome.browser.datausage.DataUseTabUIManager; |
| 12 import org.chromium.chrome.browser.externalnav.ExternalNavigationHandler; | 12 import org.chromium.chrome.browser.externalnav.ExternalNavigationHandler; |
| 13 import org.chromium.chrome.browser.externalnav.ExternalNavigationHandler.Overrid
eUrlLoadingResult; | 13 import org.chromium.chrome.browser.externalnav.ExternalNavigationHandler.Overrid
eUrlLoadingResult; |
| 14 import org.chromium.chrome.browser.externalnav.ExternalNavigationParams; | 14 import org.chromium.chrome.browser.externalnav.ExternalNavigationParams; |
| 15 import org.chromium.chrome.browser.navigation.NavigationHandler; | |
| 16 import org.chromium.chrome.browser.tabmodel.TabModel.TabLaunchType; | 15 import org.chromium.chrome.browser.tabmodel.TabModel.TabLaunchType; |
| 17 import org.chromium.components.navigation_interception.InterceptNavigationDelega
te; | 16 import org.chromium.components.navigation_interception.InterceptNavigationDelega
te; |
| 18 import org.chromium.components.navigation_interception.NavigationParams; | 17 import org.chromium.components.navigation_interception.NavigationParams; |
| 18 import org.chromium.content_public.browser.NavigationController; |
| 19 import org.chromium.content_public.browser.WebContents; |
| 19 import org.chromium.content_public.common.ConsoleMessageLevel; | 20 import org.chromium.content_public.common.ConsoleMessageLevel; |
| 20 | 21 |
| 21 /** | 22 /** |
| 22 * Class that controls navigations and allows to intercept them. It is used on A
ndroid to 'convert' | 23 * Class that controls navigations and allows to intercept them. It is used on A
ndroid to 'convert' |
| 23 * certain navigations to Intents to 3rd party applications and to "pause" navig
ations when data use | 24 * certain navigations to Intents to 3rd party applications and to "pause" navig
ations when data use |
| 24 * tracking has ended. | 25 * tracking has ended. |
| 25 */ | 26 */ |
| 26 public class InterceptNavigationDelegateImpl implements InterceptNavigationDeleg
ate { | 27 public class InterceptNavigationDelegateImpl implements InterceptNavigationDeleg
ate { |
| 27 private final Tab mTab; | 28 private final Tab mTab; |
| 28 private final ExternalNavigationHandler mExternalNavHandler; | 29 private final ExternalNavigationHandler mExternalNavHandler; |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 .setShouldCloseContentsOnOverrideUrlLoadingAndLaunchIntent( | 164 .setShouldCloseContentsOnOverrideUrlLoadingAndLaunchIntent( |
| 164 shouldCloseTab && navigationParams.isMainFrame); | 165 shouldCloseTab && navigationParams.isMainFrame); |
| 165 } | 166 } |
| 166 | 167 |
| 167 /** | 168 /** |
| 168 * Updates navigation history if navigation is canceled due to intent handle
r. We go back to the | 169 * Updates navigation history if navigation is canceled due to intent handle
r. We go back to the |
| 169 * last committed entry index which was saved before the navigation, and rem
ove the empty | 170 * last committed entry index which was saved before the navigation, and rem
ove the empty |
| 170 * entries from the navigation history. See crbug.com/426679 | 171 * entries from the navigation history. See crbug.com/426679 |
| 171 */ | 172 */ |
| 172 public void maybeUpdateNavigationHistory() { | 173 public void maybeUpdateNavigationHistory() { |
| 173 NavigationHandler navigationHandler = mTab.getNavigationHandler(); | 174 WebContents webContents = mTab.getWebContents(); |
| 174 if (mClearAllForwardHistoryRequired && navigationHandler != null) { | 175 if (mClearAllForwardHistoryRequired && webContents != null) { |
| 176 NavigationController navigationController = |
| 177 webContents.getNavigationController(); |
| 175 int lastCommittedEntryIndex = getLastCommittedEntryIndex(); | 178 int lastCommittedEntryIndex = getLastCommittedEntryIndex(); |
| 176 while (navigationHandler.canGoForward()) { | 179 while (navigationController.canGoForward()) { |
| 177 boolean ret = navigationHandler.removeEntryAtIndex( | 180 boolean ret = navigationController.removeEntryAtIndex( |
| 178 lastCommittedEntryIndex + 1); | 181 lastCommittedEntryIndex + 1); |
| 179 assert ret; | 182 assert ret; |
| 180 } | 183 } |
| 181 } else if (mShouldClearRedirectHistoryForTabClobbering && navigationHand
ler != null) { | 184 } else if (mShouldClearRedirectHistoryForTabClobbering |
| 185 && webContents != null) { |
| 182 // http://crbug/479056: Even if we clobber the current tab, we want
to remove | 186 // http://crbug/479056: Even if we clobber the current tab, we want
to remove |
| 183 // redirect history to be consistent. | 187 // redirect history to be consistent. |
| 188 NavigationController navigationController = |
| 189 webContents.getNavigationController(); |
| 184 int indexBeforeRedirection = mTab.getTabRedirectHandler() | 190 int indexBeforeRedirection = mTab.getTabRedirectHandler() |
| 185 .getLastCommittedEntryIndexBeforeStartingNavigation(); | 191 .getLastCommittedEntryIndexBeforeStartingNavigation(); |
| 186 int lastCommittedEntryIndex = getLastCommittedEntryIndex(); | 192 int lastCommittedEntryIndex = getLastCommittedEntryIndex(); |
| 187 for (int i = lastCommittedEntryIndex - 1; i > indexBeforeRedirection
; --i) { | 193 for (int i = lastCommittedEntryIndex - 1; i > indexBeforeRedirection
; --i) { |
| 188 boolean ret = navigationHandler.removeEntryAtIndex(i); | 194 boolean ret = navigationController.removeEntryAtIndex(i); |
| 189 assert ret; | 195 assert ret; |
| 190 } | 196 } |
| 191 } | 197 } |
| 192 mClearAllForwardHistoryRequired = false; | 198 mClearAllForwardHistoryRequired = false; |
| 193 mShouldClearRedirectHistoryForTabClobbering = false; | 199 mShouldClearRedirectHistoryForTabClobbering = false; |
| 194 } | 200 } |
| 195 | 201 |
| 196 AuthenticatorNavigationInterceptor getAuthenticatorNavigationInterceptor() { | 202 AuthenticatorNavigationInterceptor getAuthenticatorNavigationInterceptor() { |
| 197 return mAuthenticatorHelper; | 203 return mAuthenticatorHelper; |
| 198 } | 204 } |
| 199 | 205 |
| 200 private int getLastCommittedEntryIndex() { | 206 private int getLastCommittedEntryIndex() { |
| 201 if (mTab.getNavigationHandler() == null) return -1; | 207 if (mTab.getWebContents() == null) return -1; |
| 202 return mTab.getNavigationHandler().getLastCommittedEntryIndex(); | 208 return mTab.getWebContents().getNavigationController().getLastCommittedE
ntryIndex(); |
| 203 } | 209 } |
| 204 | 210 |
| 205 private boolean shouldCloseContentsOnOverrideUrlLoadingAndLaunchIntent() { | 211 private boolean shouldCloseContentsOnOverrideUrlLoadingAndLaunchIntent() { |
| 206 if (mTab.getNavigationHandler() == null) return false; | 212 if (mTab.getWebContents() == null) return false; |
| 207 if (!mTab.getNavigationHandler().canGoToOffset(0)) return true; | 213 if (!mTab.getWebContents().getNavigationController().canGoToOffset(0)) r
eturn true; |
| 208 | 214 |
| 209 // http://crbug/415948 : if the last committed entry index which was sav
ed before this | 215 // http://crbug/415948 : if the last committed entry index which was sav
ed before this |
| 210 // navigation is invalid, it means that this navigation is the first one
since this tab was | 216 // navigation is invalid, it means that this navigation is the first one
since this tab was |
| 211 // created. | 217 // created. |
| 212 // In such case, we would like to close this tab. | 218 // In such case, we would like to close this tab. |
| 213 if (mTab.getTabRedirectHandler().isOnNavigation()) { | 219 if (mTab.getTabRedirectHandler().isOnNavigation()) { |
| 214 return mTab.getTabRedirectHandler().getLastCommittedEntryIndexBefore
StartingNavigation() | 220 return mTab.getTabRedirectHandler().getLastCommittedEntryIndexBefore
StartingNavigation() |
| 215 == TabRedirectHandler.INVALID_ENTRY_INDEX; | 221 == TabRedirectHandler.INVALID_ENTRY_INDEX; |
| 216 } | 222 } |
| 217 return false; | 223 return false; |
| 218 } | 224 } |
| 219 | 225 |
| 220 /** | 226 /** |
| 221 * Called when Chrome decides to override URL loading and show an intent pic
ker. | 227 * Called when Chrome decides to override URL loading and show an intent pic
ker. |
| 222 */ | 228 */ |
| 223 private void onOverrideUrlLoadingAndLaunchIntent() { | 229 private void onOverrideUrlLoadingAndLaunchIntent() { |
| 224 if (mTab.getNavigationHandler() == null) return; | 230 if (mTab.getWebContents() == null) return; |
| 225 | 231 |
| 226 // Before leaving Chrome, close the empty child tab. | 232 // Before leaving Chrome, close the empty child tab. |
| 227 // If a new tab is created through JavaScript open to load this | 233 // If a new tab is created through JavaScript open to load this |
| 228 // url, we would like to close it as we will load this url in a | 234 // url, we would like to close it as we will load this url in a |
| 229 // different Activity. | 235 // different Activity. |
| 230 if (shouldCloseContentsOnOverrideUrlLoadingAndLaunchIntent()) { | 236 if (shouldCloseContentsOnOverrideUrlLoadingAndLaunchIntent()) { |
| 231 if (mTab.getLaunchType() == TabLaunchType.FROM_EXTERNAL_APP) { | 237 if (mTab.getLaunchType() == TabLaunchType.FROM_EXTERNAL_APP) { |
| 232 // Moving task back before closing the tab allows back button to
function better | 238 // Moving task back before closing the tab allows back button to
function better |
| 233 // when Chrome was an intermediate link redirector between two a
pps. | 239 // when Chrome was an intermediate link redirector between two a
pps. |
| 234 // crbug.com/487938. | 240 // crbug.com/487938. |
| 235 mTab.getActivity().moveTaskToBack(true); | 241 mTab.getActivity().moveTaskToBack(true); |
| 236 } | 242 } |
| 237 mTab.getTabModelSelector().closeTab(mTab); | 243 mTab.getTabModelSelector().closeTab(mTab); |
| 238 } else if (mTab.getTabRedirectHandler().isOnNavigation()) { | 244 } else if (mTab.getTabRedirectHandler().isOnNavigation()) { |
| 239 int lastCommittedEntryIndexBeforeNavigation = mTab.getTabRedirectHan
dler() | 245 int lastCommittedEntryIndexBeforeNavigation = mTab.getTabRedirectHan
dler() |
| 240 .getLastCommittedEntryIndexBeforeStartingNavigation(); | 246 .getLastCommittedEntryIndexBeforeStartingNavigation(); |
| 241 if (getLastCommittedEntryIndex() > lastCommittedEntryIndexBeforeNavi
gation) { | 247 if (getLastCommittedEntryIndex() > lastCommittedEntryIndexBeforeNavi
gation) { |
| 242 // http://crbug/426679 : we want to go back to the last committe
d entry index which | 248 // http://crbug/426679 : we want to go back to the last committe
d entry index which |
| 243 // was saved before this navigation, and remove the empty entrie
s from the | 249 // was saved before this navigation, and remove the empty entrie
s from the |
| 244 // navigation history. | 250 // navigation history. |
| 245 mClearAllForwardHistoryRequired = true; | 251 mClearAllForwardHistoryRequired = true; |
| 246 mTab.getNavigationHandler().goToNavigationIndex( | 252 mTab.getWebContents().getNavigationController().goToNavigationIn
dex( |
| 247 lastCommittedEntryIndexBeforeNavigation); | 253 lastCommittedEntryIndexBeforeNavigation); |
| 248 } | 254 } |
| 249 } | 255 } |
| 250 } | 256 } |
| 251 | 257 |
| 252 private void logBlockedNavigationToDevToolsConsole(String url) { | 258 private void logBlockedNavigationToDevToolsConsole(String url) { |
| 253 int resId = mExternalNavHandler.canExternalAppHandleUrl(url) | 259 int resId = mExternalNavHandler.canExternalAppHandleUrl(url) |
| 254 ? R.string.blocked_navigation_warning | 260 ? R.string.blocked_navigation_warning |
| 255 : R.string.unreachable_navigation_warning; | 261 : R.string.unreachable_navigation_warning; |
| 256 mTab.getWebContents().addMessageToDevToolsConsole( | 262 mTab.getWebContents().addMessageToDevToolsConsole( |
| 257 ConsoleMessageLevel.WARNING, mTab.getApplicationContext().getStr
ing(resId, url)); | 263 ConsoleMessageLevel.WARNING, mTab.getApplicationContext().getStr
ing(resId, url)); |
| 258 } | 264 } |
| 259 } | 265 } |
| OLD | NEW |