web_state_delegate.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2016 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_STATE_DELEGATE_H_
  5. #define IOS_WEB_PUBLIC_WEB_STATE_DELEGATE_H_
  6. #include <set>
  7. #import <Foundation/Foundation.h>
  8. #import <UIKit/UIKit.h>
  9. #include "base/callback.h"
  10. #import "ios/web/public/web_state.h"
  11. @protocol CRWResponderInputView;
  12. @class UIViewController;
  13. namespace web {
  14. struct ContextMenuParams;
  15. class JavaScriptDialogPresenter;
  16. // Objects implement this interface to get notified about changes in the
  17. // WebState and to provide necessary functionality.
  18. class WebStateDelegate {
  19. public:
  20. WebStateDelegate();
  21. // Called when |source| wants to open a new window. |url| is the URL of
  22. // the new window; |opener_url| is the URL of the page which requested a
  23. // window to be open; |initiated_by_user| is true if action was caused by the
  24. // user. |source| will not open a window if this method returns nil. This
  25. // method can not return |source|.
  26. virtual WebState* CreateNewWebState(WebState* source,
  27. const GURL& url,
  28. const GURL& opener_url,
  29. bool initiated_by_user);
  30. // Called when the page calls wants to close self by calling window.close()
  31. // JavaScript API.
  32. virtual void CloseWebState(WebState* source);
  33. // Returns the WebState the URL is opened in, or nullptr if the URL wasn't
  34. // opened immediately.
  35. virtual WebState* OpenURLFromWebState(WebState* source,
  36. const WebState::OpenURLParams& params);
  37. // Requests the repost form confirmation dialog. Clients must call |callback|
  38. // with true to allow repost and with false to cancel the repost. If this
  39. // method is not implemented then WebState will repost the form.
  40. virtual void ShowRepostFormWarningDialog(
  41. WebState* source,
  42. base::OnceCallback<void(bool)> callback);
  43. // Returns a pointer to a service to manage dialogs. May return nullptr in
  44. // which case dialogs aren't shown.
  45. // TODO(crbug.com/622084): Find better place for this method.
  46. virtual JavaScriptDialogPresenter* GetJavaScriptDialogPresenter(
  47. WebState* source);
  48. // Called when a request receives an authentication challenge specified by
  49. // |protection_space|, and is unable to respond using cached credentials.
  50. // Clients must call |callback| even if they want to cancel authentication
  51. // (in which case |username| or |password| should be nil).
  52. typedef base::OnceCallback<void(NSString* username, NSString* password)>
  53. AuthCallback;
  54. virtual void OnAuthRequired(WebState* source,
  55. NSURLProtectionSpace* protection_space,
  56. NSURLCredential* proposed_credential,
  57. AuthCallback callback) = 0;
  58. // Returns the UIView used to contain the WebView for sizing purposes. Can be
  59. // nil.
  60. virtual UIView* GetWebViewContainer(WebState* source);
  61. // Called when the context menu is triggered and now it is required to
  62. // provide a UIContextMenuConfiguration to |completion_handler| to generate
  63. // the context menu.
  64. virtual void ContextMenuConfiguration(
  65. WebState* source,
  66. const ContextMenuParams& params,
  67. void (^completion_handler)(UIContextMenuConfiguration*));
  68. // Called when the context menu will commit with animator.
  69. virtual void ContextMenuWillCommitWithAnimator(
  70. WebState* source,
  71. id<UIContextMenuInteractionCommitAnimating> animator);
  72. // UIResponder Form Input APIs, consult Apple's UIResponder documentation for
  73. // more info.
  74. virtual id<CRWResponderInputView> GetResponderInputView(WebState* source);
  75. protected:
  76. virtual ~WebStateDelegate();
  77. private:
  78. friend class WebStateImpl;
  79. // Called when |this| becomes the WebStateDelegate for |source|.
  80. void Attach(WebState* source);
  81. // Called when |this| is no longer the WebStateDelegate for |source|.
  82. void Detach(WebState* source);
  83. // The WebStates for which |this| is currently a delegate.
  84. std::set<WebState*> attached_states_;
  85. };
  86. } // namespace web
  87. #endif // IOS_WEB_PUBLIC_WEB_STATE_DELEGATE_H_