navigation_observer.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef WEBLAYER_PUBLIC_NAVIGATION_OBSERVER_H_
  5. #define WEBLAYER_PUBLIC_NAVIGATION_OBSERVER_H_
  6. class GURL;
  7. namespace base {
  8. class TimeDelta;
  9. class TimeTicks;
  10. } // namespace base
  11. namespace weblayer {
  12. class Navigation;
  13. class Page;
  14. // An interface for a WebLayer embedder to get notified about navigations. For
  15. // now this only notifies for the main frame.
  16. //
  17. // The lifecycle of a navigation:
  18. // 1) A navigation is initiated, such as by NavigationController::Navigate()
  19. // or within the page
  20. // 2) LoadStateChanged() first invoked
  21. // 3) NavigationStarted
  22. // 4) 0 or more NavigationRedirected
  23. // 5) NavigationCompleted or NavigationFailed
  24. // 6) Main frame completes loading, LoadStateChanged() last invoked
  25. class NavigationObserver {
  26. public:
  27. virtual ~NavigationObserver() {}
  28. // Called when a navigation started in the Tab. |navigation| is
  29. // unique to a specific navigation. The same |navigation| will be provided on
  30. // subsequent calls to NavigationRedirected, NavigationCommitted,
  31. // NavigationCompleted and NavigationFailed when related to this navigation.
  32. // Observers should clear any references to |navigation| in
  33. // NavigationCompleted or NavigationFailed, just before it is destroyed.
  34. //
  35. // Note that this is only fired by navigations in the main frame.
  36. //
  37. // Note that this is fired by same-document navigations, such as fragment
  38. // navigations or pushState/replaceState, which will not result in a document
  39. // change. To filter these out, use Navigation::IsSameDocument.
  40. //
  41. // Note that more than one navigation can be ongoing in the Tab
  42. // at the same time. Each will get its own Navigation object.
  43. //
  44. // Note that there is no guarantee that NavigationCompleted/NavigationFailed
  45. // will be called for any particular navigation before NavigationStarted is
  46. // called on the next.
  47. virtual void NavigationStarted(Navigation* navigation) {}
  48. // Called when a navigation encountered a server redirect.
  49. virtual void NavigationRedirected(Navigation* navigation) {}
  50. // Called when a navigation completes successfully in the Tab.
  51. //
  52. // The document load will still be ongoing in the Tab. Use the
  53. // document loads events such as OnFirstContentfulPaint and related methods to
  54. // listen for continued events from this Tab.
  55. //
  56. // Note that this is fired by same-document navigations, such as fragment
  57. // navigations or pushState/replaceState, which will not result in a document
  58. // change. To filter these out, use NavigationHandle::IsSameDocument.
  59. //
  60. // Note that |navigation| will be destroyed at the end of this call, so do not
  61. // keep a reference to it afterward.
  62. virtual void NavigationCompleted(Navigation* navigation) {}
  63. // Called when a navigation aborts in the Tab.
  64. //
  65. // Note that |navigation| will be destroyed at the end of this call, so do not
  66. // keep a reference to it afterward.
  67. virtual void NavigationFailed(Navigation* navigation) {}
  68. // Indicates that loading has started (|is_loading| is true) or is done
  69. // (|is_loading| is false). |should_show_loading_ui| will be true unless the
  70. // load is a fragment navigation, or triggered by
  71. // history.pushState/replaceState.
  72. virtual void LoadStateChanged(bool is_loading, bool should_show_loading_ui) {}
  73. // Indicates that the load progress of the page has changed. |progress|
  74. // ranges from 0.0 to 1.0.
  75. virtual void LoadProgressChanged(double progress) {}
  76. // This is fired after each navigation has completed to indicate that the
  77. // first paint after a non-empty layout has finished.
  78. virtual void OnFirstContentfulPaint() {}
  79. // Similar to OnFirstContentfulPaint but contains timing information from the
  80. // renderer process to better align with the Navigation Timing API.
  81. // |navigation_start| is the navigation start time.
  82. // |first_contentful_paint| is the duration to first contentful paint from
  83. // navigation start.
  84. virtual void OnFirstContentfulPaint(
  85. const base::TimeTicks& navigation_start,
  86. const base::TimeDelta& first_contentful_paint) {}
  87. // This is fired when the largest contentful paint page load metric is
  88. // available. |navigation_start| is the navigation start time.
  89. // |largest_contentful_paint| is the duration to largest contentful paint from
  90. // navigation start.
  91. virtual void OnLargestContentfulPaint(
  92. const base::TimeTicks& navigation_start,
  93. const base::TimeDelta& largest_contentful_paint) {}
  94. // Called after each navigation to indicate that the old page is no longer
  95. // being rendered. Note this is not ordered with respect to
  96. // OnFirstContentfulPaint.
  97. virtual void OnOldPageNoLongerRendered(const GURL& url) {}
  98. // Called when a Page is destroyed. For the common case, this is called when
  99. // the user navigates away from a page to a new one or when the Tab is
  100. // destroyed. However there are situations when a page is alive when it's not
  101. // visible, e.g. when it goes into the back-forward cache. In that case this
  102. // method will either be called when the back-forward cache entry is evicted
  103. // or if it is used then this cycle repeats.
  104. virtual void OnPageDestroyed(Page* page) {}
  105. // Called when the source language for |page| has been determined to be
  106. // |language|.
  107. // Note: |language| is an ISO 639 language code (two letters, except for
  108. // Chinese where a localization is necessary).
  109. virtual void OnPageLanguageDetermined(Page* page,
  110. const std::string& language) {}
  111. };
  112. } // namespace weblayer
  113. #endif // WEBLAYER_PUBLIC_NAVIGATION_OBSERVER_H_