[email protected] | 707d6be6 | 2012-01-12 03:56:15 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 4acc19a6 | 2009-04-03 03:05:11 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 265a149 | 2011-02-27 05:58:07 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_UI_WEBUI_HTML_DIALOG_UI_H_ |
| 6 | #define CHROME_BROWSER_UI_WEBUI_HTML_DIALOG_UI_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | 4acc19a6 | 2009-04-03 03:05:11 | [diff] [blame] | 8 | |
[email protected] | 038d52e1 | 2009-10-14 16:53:41 | [diff] [blame] | 9 | #include <string> |
[email protected] | 2bc2de6 | 2009-06-29 23:37:42 | [diff] [blame] | 10 | #include <vector> |
| 11 | |
[email protected] | c63cedf2 | 2012-01-17 18:42:22 | [diff] [blame] | 12 | #include "base/compiler_specific.h" |
[email protected] | bdae5c1 | 2011-08-05 21:49:06 | [diff] [blame] | 13 | #include "base/string16.h" |
[email protected] | 6936ace | 2012-01-31 02:48:12 | [diff] [blame^] | 14 | #include "content/public/browser/web_contents_delegate.h" |
[email protected] | e14c95912 | 2012-01-13 16:58:25 | [diff] [blame] | 15 | #include "content/public/browser/web_ui_controller.h" |
[email protected] | 4acc19a6 | 2009-04-03 03:05:11 | [diff] [blame] | 16 | #include "googleurl/src/gurl.h" |
[email protected] | 707d6be6 | 2012-01-12 03:56:15 | [diff] [blame] | 17 | #include "ui/base/ui_base_types.h" |
[email protected] | 4acc19a6 | 2009-04-03 03:05:11 | [diff] [blame] | 18 | |
[email protected] | 9a80e22 | 2010-11-22 11:27:26 | [diff] [blame] | 19 | struct ContextMenuParams; |
| 20 | |
[email protected] | f3a1c64 | 2011-07-12 19:15:03 | [diff] [blame] | 21 | namespace base { |
| 22 | class ListValue; |
[email protected] | 45644f6 | 2011-11-23 00:58:23 | [diff] [blame] | 23 | template<class T> class PropertyAccessor; |
| 24 | } |
| 25 | |
[email protected] | a81343d23 | 2011-12-27 07:39:20 | [diff] [blame] | 26 | namespace content { |
| 27 | class WebContents; |
[email protected] | c63cedf2 | 2012-01-17 18:42:22 | [diff] [blame] | 28 | class WebUIMessageHandler; |
[email protected] | a81343d23 | 2011-12-27 07:39:20 | [diff] [blame] | 29 | } |
| 30 | |
[email protected] | 45644f6 | 2011-11-23 00:58:23 | [diff] [blame] | 31 | namespace gfx { |
| 32 | class Size; |
[email protected] | f3a1c64 | 2011-07-12 19:15:03 | [diff] [blame] | 33 | } |
| 34 | |
[email protected] | 4acc19a6 | 2009-04-03 03:05:11 | [diff] [blame] | 35 | // Implement this class to receive notifications. |
| 36 | class HtmlDialogUIDelegate { |
| 37 | public: |
[email protected] | 038d52e1 | 2009-10-14 16:53:41 | [diff] [blame] | 38 | // Returns true if the contents needs to be run in a modal dialog. |
[email protected] | 707d6be6 | 2012-01-12 03:56:15 | [diff] [blame] | 39 | virtual ui::ModalType GetDialogModalType() const = 0; |
[email protected] | 4acc19a6 | 2009-04-03 03:05:11 | [diff] [blame] | 40 | |
[email protected] | 038d52e1 | 2009-10-14 16:53:41 | [diff] [blame] | 41 | // Returns the title of the dialog. |
[email protected] | bdae5c1 | 2011-08-05 21:49:06 | [diff] [blame] | 42 | virtual string16 GetDialogTitle() const = 0; |
[email protected] | 4acc19a6 | 2009-04-03 03:05:11 | [diff] [blame] | 43 | |
[email protected] | 038d52e1 | 2009-10-14 16:53:41 | [diff] [blame] | 44 | // Get the HTML file path for the content to load in the dialog. |
| 45 | virtual GURL GetDialogContentURL() const = 0; |
[email protected] | 4acc19a6 | 2009-04-03 03:05:11 | [diff] [blame] | 46 | |
[email protected] | 36e1217 | 2011-02-08 23:46:02 | [diff] [blame] | 47 | // Get WebUIMessageHandler objects to handle messages from the HTML/JS page. |
[email protected] | 038d52e1 | 2009-10-14 16:53:41 | [diff] [blame] | 48 | // The handlers are used to send and receive messages from the page while it |
[email protected] | d21cdb1 | 2011-02-10 01:22:32 | [diff] [blame] | 49 | // is still open. Ownership of each handler is taken over by the WebUI |
[email protected] | 038d52e1 | 2009-10-14 16:53:41 | [diff] [blame] | 50 | // hosting the page. |
[email protected] | 36e1217 | 2011-02-08 23:46:02 | [diff] [blame] | 51 | virtual void GetWebUIMessageHandlers( |
[email protected] | 26e2632a | 2011-12-31 04:02:55 | [diff] [blame] | 52 | std::vector<content::WebUIMessageHandler*>* handlers) const = 0; |
[email protected] | 2bc2de6 | 2009-06-29 23:37:42 | [diff] [blame] | 53 | |
[email protected] | 038d52e1 | 2009-10-14 16:53:41 | [diff] [blame] | 54 | // Get the size of the dialog. |
| 55 | virtual void GetDialogSize(gfx::Size* size) const = 0; |
[email protected] | 4acc19a6 | 2009-04-03 03:05:11 | [diff] [blame] | 56 | |
[email protected] | 038d52e1 | 2009-10-14 16:53:41 | [diff] [blame] | 57 | // Gets the JSON string input to use when showing the dialog. |
| 58 | virtual std::string GetDialogArgs() const = 0; |
[email protected] | 4acc19a6 | 2009-04-03 03:05:11 | [diff] [blame] | 59 | |
[email protected] | 038d52e1 | 2009-10-14 16:53:41 | [diff] [blame] | 60 | // A callback to notify the delegate that the dialog closed. |
[email protected] | 0a0298403 | 2011-09-02 20:35:46 | [diff] [blame] | 61 | // IMPORTANT: Implementations should delete |this| here (unless they've |
| 62 | // arranged for the delegate to be deleted in some other way, e.g. by |
| 63 | // registering it as a message handler in the WebUI object). |
[email protected] | 038d52e1 | 2009-10-14 16:53:41 | [diff] [blame] | 64 | virtual void OnDialogClosed(const std::string& json_retval) = 0; |
[email protected] | 4acc19a6 | 2009-04-03 03:05:11 | [diff] [blame] | 65 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 66 | // A callback to notify the delegate that the contents have gone |
| 67 | // away. Only relevant if your dialog hosts code that calls |
| 68 | // windows.close() and you've allowed that. If the output parameter |
| 69 | // is set to true, then the dialog is closed. The default is false. |
[email protected] | a81343d23 | 2011-12-27 07:39:20 | [diff] [blame] | 70 | virtual void OnCloseContents(content::WebContents* source, |
| 71 | bool* out_close_dialog) = 0; |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 72 | |
[email protected] | ea161da | 2010-11-02 21:57:35 | [diff] [blame] | 73 | // A callback to allow the delegate to dictate that the window should not |
| 74 | // have a title bar. This is useful when presenting branded interfaces. |
| 75 | virtual bool ShouldShowDialogTitle() const = 0; |
| 76 | |
[email protected] | 9a80e22 | 2010-11-22 11:27:26 | [diff] [blame] | 77 | // A callback to allow the delegate to inhibit context menu or show |
| 78 | // customized menu. |
[email protected] | eb929d6 | 2011-12-23 21:58:26 | [diff] [blame] | 79 | // Returns true iff you do NOT want the standard context menu to be |
| 80 | // shown (because you want to handle it yourself). |
[email protected] | 9a80e22 | 2010-11-22 11:27:26 | [diff] [blame] | 81 | virtual bool HandleContextMenu(const ContextMenuParams& params); |
| 82 | |
[email protected] | 6936ace | 2012-01-31 02:48:12 | [diff] [blame^] | 83 | // A callback to allow the delegate to open a new URL inside |source|. |
| 84 | // On return |out_new_contents| should contain the WebContents the URL |
| 85 | // is opened in. Return false to use the default handler. |
| 86 | virtual bool HandleOpenURLFromTab(content::WebContents* source, |
| 87 | const content::OpenURLParams& params, |
| 88 | content::WebContents** out_new_contents); |
| 89 | |
| 90 | // A callback to create a new tab with |new_contents|. |source| is the |
| 91 | // WebContent where the operation originated. |disposition| controls how the |
| 92 | // new tab should be opened. |initial_pos| is the position of the window if a |
| 93 | // new window is created. |user_gesture| is true if the operation was started |
| 94 | // by a user gesture. Return false to use the default handler. |
| 95 | virtual bool HandleAddNewContents(content::WebContents* source, |
| 96 | content::WebContents* new_contents, |
| 97 | WindowOpenDisposition disposition, |
| 98 | const gfx::Rect& initial_pos, |
| 99 | bool user_gesture); |
| 100 | |
[email protected] | ef1c7df | 2011-12-06 13:49:49 | [diff] [blame] | 101 | // Stores the dialog bounds. |
[email protected] | 1cd78a07 | 2012-01-27 08:59:13 | [diff] [blame] | 102 | virtual void StoreDialogSize(const gfx::Size& dialog_size) {} |
[email protected] | ef1c7df | 2011-12-06 13:49:49 | [diff] [blame] | 103 | |
[email protected] | 038d52e1 | 2009-10-14 16:53:41 | [diff] [blame] | 104 | protected: |
[email protected] | 1bc6c55 | 2010-07-30 20:39:31 | [diff] [blame] | 105 | virtual ~HtmlDialogUIDelegate() {} |
[email protected] | 4acc19a6 | 2009-04-03 03:05:11 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | // Displays file URL contents inside a modal HTML dialog. |
| 109 | // |
[email protected] | 0ec4898e | 2011-12-30 21:09:24 | [diff] [blame] | 110 | // This application really should not use WebContents + WebUI. It should instead |
[email protected] | 4acc19a6 | 2009-04-03 03:05:11 | [diff] [blame] | 111 | // just embed a RenderView in a dialog and be done with it. |
| 112 | // |
[email protected] | d21cdb1 | 2011-02-10 01:22:32 | [diff] [blame] | 113 | // Before loading a URL corresponding to this WebUI, the caller should set its |
[email protected] | 0ec4898e | 2011-12-30 21:09:24 | [diff] [blame] | 114 | // delegate as a property on the WebContents. This WebUI will pick it up from |
[email protected] | 4acc19a6 | 2009-04-03 03:05:11 | [diff] [blame] | 115 | // there and call it back. This is a bit of a hack to allow the dialog to pass |
[email protected] | 0ec4898e | 2011-12-30 21:09:24 | [diff] [blame] | 116 | // its delegate to the Web UI without having nasty accessors on the WebContents. |
[email protected] | 4acc19a6 | 2009-04-03 03:05:11 | [diff] [blame] | 117 | // The correct design using RVH directly would avoid all of this. |
[email protected] | c63cedf2 | 2012-01-17 18:42:22 | [diff] [blame] | 118 | class HtmlDialogUI : public content::WebUIController { |
[email protected] | 4acc19a6 | 2009-04-03 03:05:11 | [diff] [blame] | 119 | public: |
| 120 | struct HtmlDialogParams { |
| 121 | // The URL for the content that will be loaded in the dialog. |
| 122 | GURL url; |
| 123 | // Width of the dialog. |
| 124 | int width; |
| 125 | // Height of the dialog. |
| 126 | int height; |
| 127 | // The JSON input to pass to the dialog when showing it. |
| 128 | std::string json_input; |
| 129 | }; |
| 130 | |
[email protected] | 0ec4898e | 2011-12-30 21:09:24 | [diff] [blame] | 131 | // When created, the property should already be set on the WebContents. |
[email protected] | 01ec4ec | 2012-01-18 04:13:47 | [diff] [blame] | 132 | explicit HtmlDialogUI(content::WebUI* web_ui); |
[email protected] | 4acc19a6 | 2009-04-03 03:05:11 | [diff] [blame] | 133 | virtual ~HtmlDialogUI(); |
| 134 | |
[email protected] | 8befa1a | 2011-09-21 02:03:38 | [diff] [blame] | 135 | // Close the dialog, passing the specified arguments to the close handler. |
| 136 | void CloseDialog(const base::ListValue* args); |
| 137 | |
[email protected] | 4acc19a6 | 2009-04-03 03:05:11 | [diff] [blame] | 138 | // Returns the PropertyBag accessor object used to write the delegate pointer |
[email protected] | 0ec4898e | 2011-12-30 21:09:24 | [diff] [blame] | 139 | // into the WebContents (see class-level comment above). |
[email protected] | 45644f6 | 2011-11-23 00:58:23 | [diff] [blame] | 140 | static base::PropertyAccessor<HtmlDialogUIDelegate*>& GetPropertyAccessor(); |
[email protected] | 4acc19a6 | 2009-04-03 03:05:11 | [diff] [blame] | 141 | |
| 142 | private: |
[email protected] | e14c95912 | 2012-01-13 16:58:25 | [diff] [blame] | 143 | // WebUIController |
[email protected] | 3abbed00 | 2011-08-01 16:02:20 | [diff] [blame] | 144 | virtual void RenderViewCreated(RenderViewHost* render_view_host) OVERRIDE; |
[email protected] | 4acc19a6 | 2009-04-03 03:05:11 | [diff] [blame] | 145 | |
| 146 | // JS message handler. |
[email protected] | f3a1c64 | 2011-07-12 19:15:03 | [diff] [blame] | 147 | void OnDialogClosed(const base::ListValue* args); |
[email protected] | 4acc19a6 | 2009-04-03 03:05:11 | [diff] [blame] | 148 | |
| 149 | DISALLOW_COPY_AND_ASSIGN(HtmlDialogUI); |
| 150 | }; |
| 151 | |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 152 | // Displays external URL contents inside a modal HTML dialog. |
| 153 | // |
| 154 | // Intended to be the place to collect the settings and lockdowns |
[email protected] | 78637b2 | 2011-12-02 20:51:52 | [diff] [blame] | 155 | // necessary for running external UI components securely (e.g., the |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 156 | // cloud print dialog). |
| 157 | class ExternalHtmlDialogUI : public HtmlDialogUI { |
| 158 | public: |
[email protected] | 01ec4ec | 2012-01-18 04:13:47 | [diff] [blame] | 159 | explicit ExternalHtmlDialogUI(content::WebUI* web_ui); |
[email protected] | 73852b8f | 2010-05-14 00:38:12 | [diff] [blame] | 160 | virtual ~ExternalHtmlDialogUI(); |
| 161 | }; |
| 162 | |
[email protected] | 265a149 | 2011-02-27 05:58:07 | [diff] [blame] | 163 | #endif // CHROME_BROWSER_UI_WEBUI_HTML_DIALOG_UI_H_ |