| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // Implements the Chrome Extensions WebNavigation API. | 5 // Implements the Chrome Extensions WebNavigation API. |
| 6 | 6 |
| 7 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api.h" | 7 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api.h" |
| 8 | 8 |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "chrome/browser/chrome_notification_types.h" | 10 #include "chrome/browser/chrome_notification_types.h" |
| (...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 navigation_state_.FrameHostCreated(new_host); | 264 navigation_state_.FrameHostCreated(new_host); |
| 265 } | 265 } |
| 266 | 266 |
| 267 void WebNavigationTabObserver::DidStartNavigation( | 267 void WebNavigationTabObserver::DidStartNavigation( |
| 268 content::NavigationHandle* navigation_handle) { | 268 content::NavigationHandle* navigation_handle) { |
| 269 if (navigation_handle->IsSamePage() || | 269 if (navigation_handle->IsSamePage() || |
| 270 !FrameNavigationState::IsValidUrl(navigation_handle->GetURL())) { | 270 !FrameNavigationState::IsValidUrl(navigation_handle->GetURL())) { |
| 271 return; | 271 return; |
| 272 } | 272 } |
| 273 | 273 |
| 274 helpers::DispatchOnBeforeNavigate(navigation_handle); | 274 pending_on_before_navigate_event_ = |
| 275 helpers::CreateOnBeforeNavigateEvent(navigation_handle); |
| 276 |
| 277 // Only dispatch the onBeforeNavigate event if the associated WebContents |
| 278 // is already added to the tab strip. Otherwise the event should be delayed |
| 279 // and sent after the addition, to preserve the ordering of events. |
| 280 // |
| 281 // TODO(nasko|devlin): This check is necessary because chrome::Navigate() |
| 282 // begins the navigation before the sending the TAB_ADDED notification, and it |
| 283 // is used an indication of that. It would be best if instead it was known |
| 284 // when the tab was created and immediately sent the created event instead of |
| 285 // waiting for the later TAB_ADDED notification, but this appears to work for |
| 286 // now. |
| 287 if (ExtensionTabUtil::GetTabById(ExtensionTabUtil::GetTabId(web_contents()), |
| 288 web_contents()->GetBrowserContext(), false, |
| 289 nullptr, nullptr, nullptr, nullptr)) { |
| 290 DispatchCachedOnBeforeNavigate(); |
| 291 } |
| 275 } | 292 } |
| 276 | 293 |
| 277 void WebNavigationTabObserver::DidFinishNavigation( | 294 void WebNavigationTabObserver::DidFinishNavigation( |
| 278 content::NavigationHandle* navigation_handle) { | 295 content::NavigationHandle* navigation_handle) { |
| 296 // If there has been a DidStartNavigation call before the tab was ready to |
| 297 // dispatch events, ensure that it is sent before processing the |
| 298 // DidFinishNavigation. |
| 299 // Note: This is exercised by WebNavigationApiTest.TargetBlankIncognito. |
| 300 DispatchCachedOnBeforeNavigate(); |
| 301 |
| 279 if (navigation_handle->HasCommitted() && !navigation_handle->IsErrorPage()) { | 302 if (navigation_handle->HasCommitted() && !navigation_handle->IsErrorPage()) { |
| 280 HandleCommit(navigation_handle); | 303 HandleCommit(navigation_handle); |
| 281 return; | 304 return; |
| 282 } | 305 } |
| 283 | 306 |
| 284 HandleError(navigation_handle); | 307 HandleError(navigation_handle); |
| 285 } | 308 } |
| 286 | 309 |
| 287 void WebNavigationTabObserver::DocumentLoadedInFrame( | 310 void WebNavigationTabObserver::DocumentLoadedInFrame( |
| 288 content::RenderFrameHost* render_frame_host) { | 311 content::RenderFrameHost* render_frame_host) { |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 source_render_frame_host, | 407 source_render_frame_host, |
| 385 new_contents, | 408 new_contents, |
| 386 url); | 409 url); |
| 387 } | 410 } |
| 388 | 411 |
| 389 void WebNavigationTabObserver::WebContentsDestroyed() { | 412 void WebNavigationTabObserver::WebContentsDestroyed() { |
| 390 g_tab_observer.Get().erase(web_contents()); | 413 g_tab_observer.Get().erase(web_contents()); |
| 391 registrar_.RemoveAll(); | 414 registrar_.RemoveAll(); |
| 392 } | 415 } |
| 393 | 416 |
| 417 void WebNavigationTabObserver::DispatchCachedOnBeforeNavigate() { |
| 418 if (!pending_on_before_navigate_event_) |
| 419 return; |
| 420 |
| 421 // EventRouter can be null in unit tests. |
| 422 EventRouter* event_router = |
| 423 EventRouter::Get(web_contents()->GetBrowserContext()); |
| 424 if (event_router) |
| 425 event_router->BroadcastEvent(std::move(pending_on_before_navigate_event_)); |
| 426 } |
| 427 |
| 394 void WebNavigationTabObserver::HandleCommit( | 428 void WebNavigationTabObserver::HandleCommit( |
| 395 content::NavigationHandle* navigation_handle) { | 429 content::NavigationHandle* navigation_handle) { |
| 396 bool is_reference_fragment_navigation = IsReferenceFragmentNavigation( | 430 bool is_reference_fragment_navigation = IsReferenceFragmentNavigation( |
| 397 navigation_handle->GetRenderFrameHost(), navigation_handle->GetURL()); | 431 navigation_handle->GetRenderFrameHost(), navigation_handle->GetURL()); |
| 398 | 432 |
| 399 navigation_state_.StartTrackingDocumentLoad( | 433 navigation_state_.StartTrackingDocumentLoad( |
| 400 navigation_handle->GetRenderFrameHost(), navigation_handle->GetURL(), | 434 navigation_handle->GetRenderFrameHost(), navigation_handle->GetURL(), |
| 401 navigation_handle->IsSamePage(), | 435 navigation_handle->IsSamePage(), |
| 402 false, // is_error_page | 436 false, // is_error_page |
| 403 navigation_handle->IsSrcdoc()); | 437 navigation_handle->IsSrcdoc()); |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 560 return g_factory.Pointer(); | 594 return g_factory.Pointer(); |
| 561 } | 595 } |
| 562 | 596 |
| 563 void WebNavigationAPI::OnListenerAdded(const EventListenerInfo& details) { | 597 void WebNavigationAPI::OnListenerAdded(const EventListenerInfo& details) { |
| 564 web_navigation_event_router_.reset(new WebNavigationEventRouter( | 598 web_navigation_event_router_.reset(new WebNavigationEventRouter( |
| 565 Profile::FromBrowserContext(browser_context_))); | 599 Profile::FromBrowserContext(browser_context_))); |
| 566 EventRouter::Get(browser_context_)->UnregisterObserver(this); | 600 EventRouter::Get(browser_context_)->UnregisterObserver(this); |
| 567 } | 601 } |
| 568 | 602 |
| 569 } // namespace extensions | 603 } // namespace extensions |
| OLD | NEW |