wk_navigation_util.h 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2017 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. //
  5. // This file contains utility functions for creating and managing magic URLs
  6. // used to implement NavigationManagerImpl.
  7. //
  8. // A restore session URL is a specific local file that is used to inject history
  9. // into a new web view. See ios/web/navigation/resources/restore_session.html.
  10. //
  11. // A placeholder navigation is an "about:blank" page loaded into the WKWebView
  12. // that corresponds to Native View or WebUI URL. This navigation is inserted to
  13. // generate a WKBackForwardListItem for the Native View or WebUI URL in the
  14. // WebView so that the WKBackForwardList contains the full list of user-visible
  15. // navigations. See "Handling App-specific URLs" section in
  16. // go/bling-navigation-experiment for more details.
  17. #ifndef IOS_WEB_NAVIGATION_WK_NAVIGATION_UTIL_H_
  18. #define IOS_WEB_NAVIGATION_WK_NAVIGATION_UTIL_H_
  19. #import <Foundation/Foundation.h>
  20. #include <memory>
  21. #include <vector>
  22. #include "url/gurl.h"
  23. namespace web {
  24. class NavigationItem;
  25. namespace wk_navigation_util {
  26. // Session restoration algorithm has this limitation on maximum session size.
  27. extern const int kMaxSessionSize;
  28. // URL fragment prefix used to encode the session history to inject in a
  29. // restore_session.html URL.
  30. extern const char kRestoreSessionSessionHashPrefix[];
  31. // URL fragment prefix used to encode target URL in a restore_session.html URL.
  32. extern const char kRestoreSessionTargetUrlHashPrefix[];
  33. // The "Referer" [sic] HTTP header.
  34. extern NSString* const kReferrerHeaderName;
  35. // Sets (offset, size) and returns an updated last committed index, so the final
  36. // size is less or equal to kMaxSessionSize. If item_count is greater than
  37. // kMaxSessionSize, then this function will trim navigation items, which are the
  38. // furthest to |last_committed_item_index|.
  39. int GetSafeItemRange(int last_committed_item_index,
  40. int item_count,
  41. int* offset,
  42. int* size);
  43. // Returns true if |url| is a placeholder URL or restore_session.html URL.
  44. bool IsWKInternalUrl(const GURL& url);
  45. bool IsWKInternalUrl(NSURL* url);
  46. // Returns true if |url| is an app specific url or an about:// scheme
  47. // non-placeholder url.
  48. bool URLNeedsUserAgentType(const GURL& url);
  49. // Returns a file:// URL that points to the magic restore_session.html file.
  50. // This is used in unit tests.
  51. GURL GetRestoreSessionBaseUrl();
  52. // Creates a restore_session.html |url| with the provided session
  53. // history encoded in the URL fragment, such that when this URL is loaded in the
  54. // web view, recreates all the history entries in |items| and the current loaded
  55. // item is the entry at |last_committed_item_index|. Sets |first_index| to the
  56. // new beginning of items.
  57. void CreateRestoreSessionUrl(
  58. int last_committed_item_index,
  59. const std::vector<std::unique_ptr<NavigationItem>>& items,
  60. GURL* url,
  61. int* first_index);
  62. // Returns true if the base URL of |url| is restore_session.html.
  63. bool IsRestoreSessionUrl(const GURL& url);
  64. bool IsRestoreSessionUrl(NSURL* url);
  65. // Creates a restore_session.html URL that encodes the specified |target_url| in
  66. // the URL fragment with a "targetUrl=" prefix. When this URL is loaded in the
  67. // web view, it executes a client-side redirect to |target_url|. This results in
  68. // a new navigation entry and prunes forward navigation history. This URL is
  69. // used by NavigationManagerImpl to reload a page with user agent override,
  70. // as reloading |target_url| directly doesn't create a new navigation entry.
  71. GURL CreateRedirectUrl(const GURL& target_url);
  72. // Extracts the URL encoded in the URL fragment of |restore_session_url| to
  73. // |target_url| and returns true. If the URL fragment does not have a
  74. // "targetUrl=" prefix, returns false.
  75. bool ExtractTargetURL(const GURL& restore_session_url, GURL* target_url);
  76. } // namespace wk_navigation_util
  77. } // namespace web
  78. #endif // IOS_WEB_NAVIGATION_WK_NAVIGATION_UTIL_H_