navigation.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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_H_
  5. #define WEBLAYER_PUBLIC_NAVIGATION_H_
  6. #include <string>
  7. #include <vector>
  8. class GURL;
  9. namespace net {
  10. class HttpResponseHeaders;
  11. }
  12. namespace weblayer {
  13. class Page;
  14. // These types are sent over IPC and across different versions. Never remove
  15. // or change the order.
  16. // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.weblayer_private
  17. // GENERATED_JAVA_CLASS_NAME_OVERRIDE: ImplNavigationState
  18. enum class NavigationState {
  19. // Waiting to receive initial response data.
  20. kWaitingResponse = 0,
  21. // Processing the response.
  22. kReceivingBytes = 1,
  23. // The navigation succeeded. Any NavigationObservers would have had
  24. // NavigationCompleted() called.
  25. kComplete = 2,
  26. // The navigation failed. This could be because of an error (in which case
  27. // IsErrorPage() will return true) or the navigation got turned into a
  28. // download (in which case IsDownload() will return true).
  29. // NavigationObservers would have had NavigationFailed() called.
  30. kFailed = 3,
  31. };
  32. class Navigation {
  33. public:
  34. virtual ~Navigation() {}
  35. // The URL the frame is navigating to. This may change during the navigation
  36. // when encountering a server redirect.
  37. virtual GURL GetURL() = 0;
  38. // Returns the redirects that occurred on the way to the current page. The
  39. // current page is the last one in the list (so even when there's no redirect,
  40. // there will be one entry in the list).
  41. virtual const std::vector<GURL>& GetRedirectChain() = 0;
  42. virtual NavigationState GetState() = 0;
  43. // Returns the status code of the navigation. Returns 0 if the navigation
  44. // hasn't completed yet or if a response wasn't received.
  45. virtual int GetHttpStatusCode() = 0;
  46. // Returns the HTTP response headers. Returns nullptr if the navigation
  47. // hasn't completed yet or if a response wasn't received.
  48. virtual const net::HttpResponseHeaders* GetResponseHeaders() = 0;
  49. // Whether the navigation happened without changing document. Examples of
  50. // same document navigations are:
  51. // * reference fragment navigations
  52. // * pushState/replaceState
  53. // * same page history navigation
  54. virtual bool IsSameDocument() = 0;
  55. // Whether the navigation resulted in an error page (e.g. interstitial). Note
  56. // that if an error page reloads, this will return true even though
  57. // GetNetErrorCode will be kNoError.
  58. virtual bool IsErrorPage() = 0;
  59. // Returns true if this navigation resulted in a download. Returns false if
  60. // this navigation did not result in a download, or if download status is not
  61. // yet known for this navigation. Download status is determined for a
  62. // navigation when processing final (post redirect) HTTP response headers.
  63. // This means the only time the embedder can know if it's a download is in
  64. // NavigationObserver::NavigationFailed.
  65. virtual bool IsDownload() = 0;
  66. // Whether the target URL can be handled by the browser's internal protocol
  67. // handlers, i.e., has a scheme that the browser knows how to process
  68. // internally. Examples of such URLs are http(s) URLs, data URLs, and file
  69. // URLs. A typical example of a URL for which there is no internal protocol
  70. // handler (and for which this method would return false) is an intent:// URL.
  71. // Added in 89.
  72. virtual bool IsKnownProtocol() = 0;
  73. // Returns true if the navigation was stopped before it could complete because
  74. // NavigationController::Stop() was called.
  75. virtual bool WasStopCalled() = 0;
  76. // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.weblayer_private
  77. // GENERATED_JAVA_CLASS_NAME_OVERRIDE: ImplLoadError
  78. enum LoadError {
  79. kNoError = 0, // Navigation completed successfully.
  80. kHttpClientError = 1, // Server responded with 4xx status code.
  81. kHttpServerError = 2, // Server responded with 5xx status code.
  82. kSSLError = 3, // Certificate error.
  83. kConnectivityError = 4, // Problem connecting to server.
  84. kOtherError = 5, // An error not listed above or below occurred.
  85. kSafeBrowsingError = 6, // Safe browsing error.
  86. };
  87. // Return information about the error, if any, that was encountered while
  88. // loading the page.
  89. virtual LoadError GetLoadError() = 0;
  90. // Set a request's header. If the header is already present, its value is
  91. // overwritten. This function can only be called at two times, during start
  92. // and redirect. When called during start, the header applies to both the
  93. // start and redirect. |name| must be rfc 2616 compliant and |value| must
  94. // not contain '\0', '\n' or '\r'.
  95. //
  96. // This function may be used to set the referer. If the referer is set in
  97. // navigation start, it is reset during the redirect. In other words, if you
  98. // need to set a referer that applies to redirects, then this must be called
  99. // from NavigationRedirected().
  100. virtual void SetRequestHeader(const std::string& name,
  101. const std::string& value) = 0;
  102. // Sets the user-agent string used for this navigation. The user-agent is
  103. // not sticky, it applies to this navigation only (and any redirects). This
  104. // function may only be called from NavigationObserver::NavigationStarted().
  105. // Any value specified during start carries through to a redirect. |value|
  106. // must not contain any illegal characters as documented in
  107. // SetRequestHeader(). Setting this to a non empty string will cause the
  108. // User-Agent Client Hint header values and the values returned by
  109. // `navigator.userAgentData` to be empty for requests this override is applied
  110. // to.
  111. virtual void SetUserAgentString(const std::string& value) = 0;
  112. // Disables auto-reload for this navigation if the network is down and comes
  113. // back later. Auto-reload is enabled by default. This function may only be
  114. // called from NavigationObserver::NavigationStarted().
  115. virtual void DisableNetworkErrorAutoReload() = 0;
  116. // Whether the navigation was initiated by the page. Examples of
  117. // page-initiated navigations include:
  118. // * <a> link click
  119. // * changing window.location.href
  120. // * redirect via the <meta http-equiv="refresh"> tag
  121. // * using window.history.pushState
  122. // * window.history.forward() or window.history.back()
  123. //
  124. // This method returns false for navigations initiated by the WebLayer API.
  125. virtual bool IsPageInitiated() = 0;
  126. // Whether the navigation is a reload. Examples of reloads include:
  127. // * embedder-specified through NavigationController::Reload
  128. // * page-initiated reloads, e.g. location.reload()
  129. // * reloads when the network interface is reconnected
  130. virtual bool IsReload() = 0;
  131. // Whether the navigation is restoring a page from back-forward cache (see
  132. // https://web.dev/bfcache/). Since a previously loaded page is being reused,
  133. // there are some things embedders have to keep in mind such as:
  134. // * there will be no NavigationObserver::OnFirstContentfulPaint callbacks
  135. // * if an embedder injects code using Tab::ExecuteScript there is no need
  136. // to reinject scripts
  137. virtual bool IsServedFromBackForwardCache() = 0;
  138. // Returns true if this navigation was initiated by a form submission.
  139. virtual bool IsFormSubmission() = 0;
  140. // Returns the referrer for this request.
  141. virtual GURL GetReferrer() = 0;
  142. // Returns the Page object this navigation is occurring for. This method may
  143. // only be called in or after NavigationObserver::NavigationCompleted() or
  144. // NavigationObserve::NavigationFailed(). It can return null if the navigation
  145. // didn't commit (e.g. 204/205 or download).
  146. virtual Page* GetPage() = 0;
  147. // Returns the offset between the indices of the previous last committed and
  148. // the newly committed navigation entries (e.g. -1 for back navigations, 0
  149. // for reloads, 1 for forward navigations). This may not cover all corner
  150. // cases, and can be incorrect in cases like main frame client redirects.
  151. virtual int GetNavigationEntryOffset() = 0;
  152. // Returns true if the navigation response was fetched from the cache.
  153. virtual bool WasFetchedFromCache() = 0;
  154. };
  155. } // namespace weblayer
  156. #endif // WEBLAYER_PUBLIC_NAVIGATION_H_