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   // Returns true if the contents needs to be run in a modal dialog.
40   virtual ModalType GetDialogModalType() const = 0;
41 
42   // Returns the title of the dialog.
43   virtual base::string16 GetDialogTitle() const = 0;
44 
45   // Returns the title to be read with screen readers.
46   virtual base::string16 GetAccessibleDialogTitle() const;
47 
48   // Returns the dialog's name identifier. Used to identify this dialog for
49   // state restoration.
50   virtual std::string GetDialogName() const;
51 
52   // Get the HTML file path for the content to load in the dialog.
53   virtual GURL GetDialogContentURL() const = 0;
54 
55   // Get WebUIMessageHandler objects to handle messages from the HTML/JS page.
56   // The handlers are used to send and receive messages from the page while it
57   // is still open.  Ownership of each handler is taken over by the WebUI
58   // hosting the page.
59   virtual void GetWebUIMessageHandlers(
60       std::vector<content::WebUIMessageHandler*>* handlers) const = 0;
61 
62   // Get the size of the dialog. Implementations can safely assume |size| is a
63   // valid pointer. Callers should be able to handle the case where
64   // implementations do not write into |size|.
65   virtual void GetDialogSize(gfx::Size* size) const = 0;
66 
67   // Get the minimum size of the dialog. The default implementation just calls
68   // GetDialogSize().
69   virtual void GetMinimumDialogSize(gfx::Size* size) const;
70 
71   // Gets the JSON string input to use when showing the dialog.
72   virtual std::string GetDialogArgs() const = 0;
73 
74   // Returns true to signal that the dialog can be closed. Specialized
75   // WebDialogDelegate subclasses can override this default behavior to allow
76   // the close to be blocked until the user corrects mistakes, accepts an
77   // agreement, etc.
78   virtual bool CanCloseDialog() const;
79 
80   // Returns true if the dialog can ever be resized. Default implementation
81   // returns true.
82   virtual bool CanResizeDialog() const;
83 
84   // A callback to notify the delegate that |source|'s loading state has
85   // changed.
OnLoadingStateChanged(content::WebContents * source)86   virtual void OnLoadingStateChanged(content::WebContents* source) {}
87 
88   // A callback to notify the delegate that a web dialog has been shown.
89   // |webui| is the WebUI with which the dialog is associated.
OnDialogShown(content::WebUI * webui)90   virtual void OnDialogShown(content::WebUI* webui) {}
91 
92   // A callback to notify the delegate that the window is requesting to be
93   // closed.  If this returns true, the dialog is closed, otherwise the
94   // dialog remains open. Default implementation returns true.
95   virtual bool OnDialogCloseRequested();
96 
97   // A callback to notify the delegate that the dialog is about to close due to
98   // the user pressing the ESC key.
OnDialogClosingFromKeyEvent()99   virtual void OnDialogClosingFromKeyEvent() {}
100 
101   // A callback to notify the delegate that the dialog closed.
102   // IMPORTANT: Implementations should delete |this| here (unless they've
103   // arranged for the delegate to be deleted in some other way, e.g. by
104   // registering it as a message handler in the WebUI object).
105   virtual void OnDialogClosed(const std::string& json_retval) = 0;
106 
107   // A callback to notify the delegate that the dialog is being closed in
108   // response to a "dialogClose" message from WebUI.
109   virtual void OnDialogCloseFromWebUI(const std::string& json_retval);
110 
111   // A callback to notify the delegate that the contents are requesting
112   // to be closed.  This could be in response to a number of events
113   // that are handled by the WebContents.  If the output parameter
114   // is set to true, then the dialog is closed.  The default is false.
115   // |out_close_dialog| is never NULL.
116   virtual void OnCloseContents(content::WebContents* source,
117                                bool* out_close_dialog) = 0;
118 
119   // Returns true if escape should immediately close the dialog. Default is
120   // true.
121   virtual bool ShouldCloseDialogOnEscape() const;
122 
123   // A callback to allow the delegate to dictate that the window should not
124   // have a title bar.  This is useful when presenting branded interfaces.
125   virtual bool ShouldShowDialogTitle() const = 0;
126 
127   // A callback to allow the delegate to center title text. Default is
128   // false.
129   virtual bool ShouldCenterDialogTitleText() const;
130 
131   // Returns true if the dialog should show a close button in the title bar.
132   // Default implementation returns true.
133   virtual bool ShouldShowCloseButton() const;
134 
135   // A callback to allow the delegate to inhibit context menu or show
136   // customized menu.
137   // Returns true iff you do NOT want the standard context menu to be
138   // shown (because you want to handle it yourself).
139   virtual bool HandleContextMenu(content::RenderFrameHost* render_frame_host,
140                                  const content::ContextMenuParams& params);
141 
142   // A callback to allow the delegate to open a new URL inside |source|.
143   // On return |out_new_contents| should contain the WebContents the URL
144   // is opened in. Return false to use the default handler.
145   virtual bool HandleOpenURLFromTab(content::WebContents* source,
146                                     const content::OpenURLParams& params,
147                                     content::WebContents** out_new_contents);
148 
149   // A callback to control whether a WebContents will be created. Returns
150   // true to disallow the creation. Return false to use the default handler.
151   virtual bool HandleShouldOverrideWebContentsCreation();
152 
153   // Stores the dialog bounds.
StoreDialogSize(const gfx::Size & dialog_size)154   virtual void StoreDialogSize(const gfx::Size& dialog_size) {}
155 
156   // Returns the accelerators handled by the delegate.
157   virtual std::vector<Accelerator> GetAccelerators();
158 
159   // Returns true if |accelerator| is processed, otherwise false.
160   virtual bool AcceleratorPressed(const Accelerator& accelerator);
161 
OnWebContentsFinishedLoad()162   virtual void OnWebContentsFinishedLoad() {}
OnMainFrameResourceLoadComplete(const blink::mojom::ResourceLoadInfo & resource_load_info)163   virtual void OnMainFrameResourceLoadComplete(
164       const blink::mojom::ResourceLoadInfo& resource_load_info) {}
165 
~WebDialogDelegate()166   virtual ~WebDialogDelegate() {}
167 };
168 
169 }  // namespace ui
170 
171 #endif  // UI_WEB_DIALOGS_WEB_DIALOG_DELEGATE_H_
172