1 // Copyright (c) 2012 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 #ifndef UI_WEB_DIALOGS_WEB_DIALOG_DELEGATE_H_
6 #define UI_WEB_DIALOGS_WEB_DIALOG_DELEGATE_H_
7 
8 #include <string>
9 #include <vector>
10 
11 #include "base/strings/string16.h"
12 #include "content/public/browser/web_contents_delegate.h"
13 #include "third_party/blink/public/mojom/loader/resource_load_info.mojom.h"
14 #include "ui/base/ui_base_types.h"
15 #include "ui/base/window_open_disposition.h"
16 #include "ui/web_dialogs/web_dialogs_export.h"
17 
18 class GURL;
19 
20 namespace content {
21 class RenderFrameHost;
22 class WebContents;
23 class WebUI;
24 class WebUIMessageHandler;
25 struct ContextMenuParams;
26 struct OpenURLParams;
27 }
28 
29 namespace gfx {
30 class Size;
31 }
32 
33 namespace ui {
34 class Accelerator;
35 
36 // Implement this class to receive notifications.
37 class WEB_DIALOGS_EXPORT WebDialogDelegate {
38  public:
39   enum class FrameKind {
40     kDialog,     // Does not include a title bar or frame caption buttons.
41     kNonClient,  // Includes a non client frame view with title & buttons.
42   };
43 
44   // Returns the modal type for this dialog. Only called once, during
45   // WebDialogView creation.
46   virtual ModalType GetDialogModalType() const = 0;
47 
48   // Returns the title of the dialog.
49   virtual base::string16 GetDialogTitle() const = 0;
50 
51   // Returns the title to be read with screen readers.
52   virtual base::string16 GetAccessibleDialogTitle() const;
53 
54   // Returns the dialog's name identifier. Used to identify this dialog for
55   // state restoration.
56   virtual std::string GetDialogName() const;
57 
58   // Get the HTML file path for the content to load in the dialog.
59   virtual GURL GetDialogContentURL() const = 0;
60 
61   // Get WebUIMessageHandler objects to handle messages from the HTML/JS page.
62   // The handlers are used to send and receive messages from the page while it
63   // is still open.  Ownership of each handler is taken over by the WebUI
64   // hosting the page.
65   virtual void GetWebUIMessageHandlers(
66       std::vector<content::WebUIMessageHandler*>* handlers) const = 0;
67 
68   // Get the size of the dialog. Implementations can safely assume |size| is a
69   // valid pointer. Callers should be able to handle the case where
70   // implementations do not write into |size|.
71   virtual void GetDialogSize(gfx::Size* size) const = 0;
72 
73   // Get the minimum size of the dialog. The default implementation just calls
74   // GetDialogSize().
75   virtual void GetMinimumDialogSize(gfx::Size* size) const;
76 
77   // Gets the JSON string input to use when showing the dialog.
78   virtual std::string GetDialogArgs() const = 0;
79 
80   // Returns true if the dialog can ever be resized. Default implementation
81   // returns true.
can_resize()82   bool can_resize() const { return can_resize_; }
set_can_resize(bool can_resize)83   void set_can_resize(bool can_resize) { can_resize_ = can_resize; }
84 
85   // Returns true if the dialog can ever be maximized. Default implementation
86   // returns false.
87   virtual bool CanMaximizeDialog() const;
88 
89   // Returns true if the dialog can ever be minimized. Default implementation
90   // returns false.
can_minimize()91   bool can_minimize() const { return can_minimize_; }
set_can_minimize(bool can_minimize)92   void set_can_minimize(bool can_minimize) { can_minimize_ = can_minimize; }
93 
94   // A callback to notify the delegate that |source|'s loading state has
95   // changed.
OnLoadingStateChanged(content::WebContents * source)96   virtual void OnLoadingStateChanged(content::WebContents* source) {}
97 
98   // A callback to notify the delegate that a web dialog has been shown.
99   // |webui| is the WebUI with which the dialog is associated.
OnDialogShown(content::WebUI * webui)100   virtual void OnDialogShown(content::WebUI* webui) {}
101 
102   // A callback to notify the delegate that the window is requesting to be
103   // closed.  If this returns true, the dialog is closed, otherwise the
104   // dialog remains open. Default implementation returns true.
105   virtual bool OnDialogCloseRequested();
106   // Use `OnDialogCloseRequested()` instead. This one is called too late in the
107   // closing process, so returning false here will leave you a half-broken
108   // dialog. Currently, `AddSupervisionDialog` relies on this to record
109   // histogram correctly.
110   //
111   // TODO(crbug.com/1110759): remove this function.
112   virtual bool DeprecatedOnDialogCloseRequested();
113 
114   // A callback to notify the delegate that the dialog is about to close due to
115   // the user pressing the ESC key.
OnDialogClosingFromKeyEvent()116   virtual void OnDialogClosingFromKeyEvent() {}
117 
118   // A callback to notify the delegate that the dialog closed.
119   // IMPORTANT: Implementations should delete |this| here (unless they've
120   // arranged for the delegate to be deleted in some other way, e.g. by
121   // registering it as a message handler in the WebUI object).
122   virtual void OnDialogClosed(const std::string& json_retval) = 0;
123 
124   // A callback to notify the delegate that the dialog is being closed in
125   // response to a "dialogClose" message from WebUI.
126   virtual void OnDialogCloseFromWebUI(const std::string& json_retval);
127 
128   // A callback to notify the delegate that the contents are requesting
129   // to be closed.  This could be in response to a number of events
130   // that are handled by the WebContents.  If the output parameter
131   // is set to true, then the dialog is closed.  The default is false.
132   // |out_close_dialog| is never NULL.
133   virtual void OnCloseContents(content::WebContents* source,
134                                bool* out_close_dialog) = 0;
135 
136   // Returns true if escape should immediately close the dialog. Default is
137   // true.
138   virtual bool ShouldCloseDialogOnEscape() const;
139 
140   // A callback to allow the delegate to dictate that the window should not
141   // have a title bar.  This is useful when presenting branded interfaces.
142   virtual bool ShouldShowDialogTitle() const = 0;
143 
144   // A callback to allow the delegate to center title text. Default is
145   // false.
146   virtual bool ShouldCenterDialogTitleText() const;
147 
148   // Returns true if the dialog should show a close button in the title bar.
149   // Default implementation returns true.
150   virtual bool ShouldShowCloseButton() const;
151 
152   // A callback to allow the delegate to inhibit context menu or show
153   // customized menu.
154   // Returns true iff you do NOT want the standard context menu to be
155   // shown (because you want to handle it yourself).
156   virtual bool HandleContextMenu(content::RenderFrameHost* render_frame_host,
157                                  const content::ContextMenuParams& params);
158 
159   // A callback to allow the delegate to open a new URL inside |source|.
160   // On return |out_new_contents| should contain the WebContents the URL
161   // is opened in. Return false to use the default handler.
162   virtual bool HandleOpenURLFromTab(content::WebContents* source,
163                                     const content::OpenURLParams& params,
164                                     content::WebContents** out_new_contents);
165 
166   // A callback to control whether a WebContents will be created. Returns
167   // true to disallow the creation. Return false to use the default handler.
168   virtual bool HandleShouldOverrideWebContentsCreation();
169 
170   // Stores the dialog bounds.
StoreDialogSize(const gfx::Size & dialog_size)171   virtual void StoreDialogSize(const gfx::Size& dialog_size) {}
172 
173   // Returns the accelerators handled by the delegate.
174   virtual std::vector<Accelerator> GetAccelerators();
175 
176   // Returns true if |accelerator| is processed, otherwise false.
177   virtual bool AcceleratorPressed(const Accelerator& accelerator);
178 
OnWebContentsFinishedLoad()179   virtual void OnWebContentsFinishedLoad() {}
OnMainFrameResourceLoadComplete(const blink::mojom::ResourceLoadInfo & resource_load_info)180   virtual void OnMainFrameResourceLoadComplete(
181       const blink::mojom::ResourceLoadInfo& resource_load_info) {}
182 
RequestMediaAccessPermission(content::WebContents * web_contents,const content::MediaStreamRequest & request,content::MediaResponseCallback callback)183   virtual void RequestMediaAccessPermission(
184       content::WebContents* web_contents,
185       const content::MediaStreamRequest& request,
186       content::MediaResponseCallback callback) {}
187 
188   virtual bool CheckMediaAccessPermission(
189       content::RenderFrameHost* render_frame_host,
190       const GURL& security_origin,
191       blink::mojom::MediaStreamType type);
192 
193   // Whether to use dialog frame view for non client frame view.
194   virtual FrameKind GetWebDialogFrameKind() const;
195 
196   virtual ~WebDialogDelegate() = default;
197 
198  private:
199   bool can_minimize_ = false;
200   bool can_resize_ = true;
201 };
202 
203 }  // namespace ui
204 
205 #endif  // UI_WEB_DIALOGS_WEB_DIALOG_DELEGATE_H_
206