| Index: chrome/browser/extensions/api/web_navigation/web_navigation_api.cc
|
| diff --git a/chrome/browser/extensions/api/web_navigation/web_navigation_api.cc b/chrome/browser/extensions/api/web_navigation/web_navigation_api.cc
|
| index 6d45ec5856295bb365085ebcbf3cdc14a43bc81d..652994bd6511db87becf1201f744dd75d3d69272 100644
|
| --- a/chrome/browser/extensions/api/web_navigation/web_navigation_api.cc
|
| +++ b/chrome/browser/extensions/api/web_navigation/web_navigation_api.cc
|
| @@ -271,11 +271,27 @@ void WebNavigationTabObserver::DidStartNavigation(
|
| return;
|
| }
|
|
|
| - helpers::DispatchOnBeforeNavigate(navigation_handle);
|
| + pending_on_before_navigate_event_ =
|
| + helpers::CreateOnBeforeNavigateEvent(navigation_handle);
|
| +
|
| + // Only dispatch the onBeforeNavigate event if the associated WebContents
|
| + // is already added to the tab strip. Otherwise the event should be delayed
|
| + // and sent after the addition, to preserve the ordering of events.
|
| + if (ExtensionTabUtil::GetTabById(ExtensionTabUtil::GetTabId(web_contents()),
|
| + web_contents()->GetBrowserContext(), false,
|
| + nullptr, nullptr, nullptr, nullptr)) {
|
| + DispatchCachedOnBeforeNavigate();
|
| + }
|
| }
|
|
|
| void WebNavigationTabObserver::DidFinishNavigation(
|
| content::NavigationHandle* navigation_handle) {
|
| + // If there has been a DidStartNavigation call before the tab was ready to
|
| + // dispatch events, ensure that it is sent before processing the
|
| + // DidFinishNavigation.
|
| + // Note: This is exercised by WebNavigationApiTest.TargetBlankIncognito.
|
| + DispatchCachedOnBeforeNavigate();
|
| +
|
| if (navigation_handle->HasCommitted() && !navigation_handle->IsErrorPage()) {
|
| HandleCommit(navigation_handle);
|
| return;
|
| @@ -391,6 +407,14 @@ void WebNavigationTabObserver::WebContentsDestroyed() {
|
| registrar_.RemoveAll();
|
| }
|
|
|
| +void WebNavigationTabObserver::DispatchCachedOnBeforeNavigate() {
|
| + if (!pending_on_before_navigate_event_.get())
|
| + return;
|
| +
|
| + EventRouter::Get(web_contents()->GetBrowserContext())
|
| + ->BroadcastEvent(std::move(pending_on_before_navigate_event_));
|
| +}
|
| +
|
| void WebNavigationTabObserver::HandleCommit(
|
| content::NavigationHandle* navigation_handle) {
|
| bool is_reference_fragment_navigation = IsReferenceFragmentNavigation(
|
|
|