web_client.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Copyright 2014 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 IOS_WEB_PUBLIC_WEB_CLIENT_H_
  5. #define IOS_WEB_PUBLIC_WEB_CLIENT_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/callback.h"
  11. #include "base/strings/string_piece.h"
  12. #include "base/task/thread_pool/thread_pool_instance.h"
  13. #include "ios/web/common/user_agent.h"
  14. #include "mojo/public/cpp/bindings/generic_pending_receiver.h"
  15. #include "third_party/abseil-cpp/absl/types/optional.h"
  16. #include "ui/base/layout.h"
  17. namespace base {
  18. class RefCountedMemory;
  19. }
  20. class GURL;
  21. @protocol UITraitEnvironment;
  22. @class UIWebView;
  23. @class NSString;
  24. namespace net {
  25. class SSLInfo;
  26. }
  27. namespace web {
  28. class BrowserState;
  29. class BrowserURLRewriter;
  30. class JavaScriptFeature;
  31. class WebClient;
  32. class WebMainParts;
  33. class WebState;
  34. // Setter and getter for the client. The client should be set early, before any
  35. // web code is called.
  36. void SetWebClient(WebClient* client);
  37. WebClient* GetWebClient();
  38. // Interface that the embedder of the web layer implements.
  39. class WebClient {
  40. public:
  41. WebClient();
  42. virtual ~WebClient();
  43. // Allows the embedder to set a custom WebMainParts implementation for the
  44. // browser startup code.
  45. virtual std::unique_ptr<WebMainParts> CreateWebMainParts();
  46. // Gives the embedder a chance to perform tasks before a web view is created.
  47. virtual void PreWebViewCreation() const {}
  48. // An embedder may support schemes that are otherwise unknown to lower-level
  49. // components. To control how /net/url and other components interpret urls of
  50. // such schemes, the embedder overrides |AddAdditionalSchemes| and adds to the
  51. // vectors inside the |Schemes| structure.
  52. struct Schemes {
  53. Schemes();
  54. ~Schemes();
  55. // "Standard" (RFC3986 syntax) schemes.
  56. std::vector<std::string> standard_schemes;
  57. // See https://www.w3.org/TR/powerful-features/#is-origin-trustworthy.
  58. std::vector<std::string> secure_schemes;
  59. };
  60. // Gives the embedder a chance to register its own standard and secure url
  61. // schemes early on in the startup sequence.
  62. virtual void AddAdditionalSchemes(Schemes* schemes) const {}
  63. // Returns the embedding application locale string.
  64. virtual std::string GetApplicationLocale() const;
  65. // Returns true if URL has application specific schema. Embedder must return
  66. // true for every custom app specific schema it supports. For example Chromium
  67. // browser would return true for "chrome://about" URL.
  68. virtual bool IsAppSpecificURL(const GURL& url) const;
  69. // Returns text to be displayed for an unsupported plugin.
  70. virtual std::u16string GetPluginNotSupportedText() const;
  71. // Returns the user agent string for the specified type.
  72. virtual std::string GetUserAgent(UserAgentType type) const;
  73. // Returns a string resource given its id.
  74. virtual std::u16string GetLocalizedString(int message_id) const;
  75. // Returns the contents of a resource in a StringPiece given the resource id.
  76. virtual base::StringPiece GetDataResource(
  77. int resource_id,
  78. ui::ResourceScaleFactor scale_factor) const;
  79. // Returns the raw bytes of a scale independent data resource.
  80. virtual base::RefCountedMemory* GetDataResourceBytes(int resource_id) const;
  81. // Returns a list of additional WebUI schemes, if any. These additional
  82. // schemes act as aliases to the about: scheme. The additional schemes may or
  83. // may not serve specific WebUI pages depending on the particular
  84. // URLDataSourceIOS and its override of
  85. // URLDataSourceIOS::ShouldServiceRequest. For all schemes returned here,
  86. // view-source is allowed.
  87. virtual void GetAdditionalWebUISchemes(
  88. std::vector<std::string>* additional_schemes) {}
  89. // Gives the embedder a chance to add url rewriters to the BrowserURLRewriter
  90. // singleton.
  91. virtual void PostBrowserURLRewriterCreation(BrowserURLRewriter* rewriter) {}
  92. // Gives the embedder a chance to provide custom JavaScriptFeatures.
  93. virtual std::vector<JavaScriptFeature*> GetJavaScriptFeatures(
  94. BrowserState* browser_state) const;
  95. // Gives the embedder a chance to provide the JavaScript to be injected into
  96. // the web view as early as possible. Result must not be nil.
  97. // The script returned will be injected in all frames (main and subframes).
  98. //
  99. // TODO(crbug.com/703964): Change the return value to NSArray<NSString*> to
  100. // improve performance.
  101. virtual NSString* GetDocumentStartScriptForAllFrames(
  102. BrowserState* browser_state) const;
  103. // Gives the embedder a chance to provide the JavaScript to be injected into
  104. // the web view as early as possible. Result must not be nil.
  105. // The script returned will only be injected in the main frame.
  106. //
  107. // TODO(crbug.com/703964): Change the return value to NSArray<NSString*> to
  108. // improve performance.
  109. virtual NSString* GetDocumentStartScriptForMainFrame(
  110. BrowserState* browser_state) const;
  111. // Allows the embedder to bind an interface request for a WebState-scoped
  112. // interface that originated from the main frame of |web_state|. Called if
  113. // |web_state| could not bind the receiver itself.
  114. virtual void BindInterfaceReceiverFromMainFrame(
  115. WebState* web_state,
  116. mojo::GenericPendingReceiver receiver) {}
  117. // Calls the given |callback| with the contents of an error page to display
  118. // when a navigation error occurs. |error| is always a valid pointer. The
  119. // string passed to |callback| will be nil if no error page should be
  120. // displayed. Otherwise, this string will contain the details of the error
  121. // and maybe links to more info. |info| will have a value for SSL cert errors
  122. // and otherwise be nullopt. |navigation_id| is passed into this method so
  123. // that in the case of an SSL cert error, the blocking page can be associated
  124. // with the tab.
  125. virtual void PrepareErrorPage(WebState* web_state,
  126. const GURL& url,
  127. NSError* error,
  128. bool is_post,
  129. bool is_off_the_record,
  130. const absl::optional<net::SSLInfo>& info,
  131. int64_t navigation_id,
  132. base::OnceCallback<void(NSString*)> callback);
  133. // Instructs the embedder to return a container that is attached to a window.
  134. virtual UIView* GetWindowedContainer();
  135. // Enables the logic to handle long press context menu with UIContextMenu.
  136. virtual bool EnableLongPressUIContextMenu() const;
  137. // Returns the UserAgentType that should be used by default for the web
  138. // content, based on the |web_state|.
  139. virtual UserAgentType GetDefaultUserAgent(web::WebState* web_state,
  140. const GURL& url) const;
  141. // Logs the default mode used (Mobile or Desktop). This is supposed to be
  142. // called only if the user didn't force the mode.
  143. virtual void LogDefaultUserAgent(web::WebState* web_state,
  144. const GURL& url) const;
  145. // Returns true if URL was restored via session restoration cache.
  146. virtual bool RestoreSessionFromCache(web::WebState* web_state) const;
  147. // Correct missing NTP and reading list virtualURLs and titles. Native session
  148. // restoration may not properly restore these items.
  149. virtual void CleanupNativeRestoreURLs(web::WebState* web_state) const;
  150. // Notify the embedder that |web_state| will display a prompt for the user.
  151. virtual void WillDisplayMediaCapturePermissionPrompt(
  152. web::WebState* web_state) const;
  153. // Returns whether `url1` and `url2` are actually pointing to the same page.
  154. virtual bool IsPointingToSameDocument(const GURL& url1,
  155. const GURL& url2) const;
  156. };
  157. } // namespace web
  158. #endif // IOS_WEB_PUBLIC_WEB_CLIENT_H_