Home | History | Annotate | Download | only in web_dialogs
      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 "ui/base/ui_base_types.h"
     13 #include "ui/base/window_open_disposition.h"
     14 #include "ui/web_dialogs/web_dialogs_export.h"
     15 
     16 class GURL;
     17 
     18 namespace content {
     19 class RenderViewHost;
     20 class WebContents;
     21 class WebUI;
     22 class WebUIMessageHandler;
     23 struct ContextMenuParams;
     24 struct OpenURLParams;
     25 }
     26 
     27 namespace gfx {
     28 class Rect;
     29 class Size;
     30 }
     31 
     32 namespace ui {
     33 
     34 // Implement this class to receive notifications.
     35 class WEB_DIALOGS_EXPORT WebDialogDelegate {
     36  public:
     37   // Returns true if the contents needs to be run in a modal dialog.
     38   virtual ModalType GetDialogModalType() const = 0;
     39 
     40   // Returns the title of the dialog.
     41   virtual base::string16 GetDialogTitle() const = 0;
     42 
     43   // Returns the dialog's name identifier. Used to identify this dialog for
     44   // state restoration.
     45   virtual std::string GetDialogName() const;
     46 
     47   // Get the HTML file path for the content to load in the dialog.
     48   virtual GURL GetDialogContentURL() const = 0;
     49 
     50   // Get WebUIMessageHandler objects to handle messages from the HTML/JS page.
     51   // The handlers are used to send and receive messages from the page while it
     52   // is still open.  Ownership of each handler is taken over by the WebUI
     53   // hosting the page.
     54   virtual void GetWebUIMessageHandlers(
     55       std::vector<content::WebUIMessageHandler*>* handlers) const = 0;
     56 
     57   // Get the size of the dialog.
     58   virtual void GetDialogSize(gfx::Size* size) const = 0;
     59 
     60   // Get the size of the dialog.
     61   virtual void GetMinimumDialogSize(gfx::Size* size) const;
     62 
     63   // Gets the JSON string input to use when showing the dialog.
     64   virtual std::string GetDialogArgs() const = 0;
     65 
     66   // Returns true to signal that the dialog can be closed. Specialized
     67   // WebDialogDelegate subclasses can override this default behavior to allow
     68   // the close to be blocked until the user corrects mistakes, accepts an
     69   // agreement, etc.
     70   virtual bool CanCloseDialog() const;
     71 
     72   // Returns true if the dialog can ever be resized. Default implementation
     73   // returns |true|.
     74   virtual bool CanResizeDialog() const;
     75 
     76   // A callback to notify the delegate that |source|'s loading state has
     77   // changed.
     78   virtual void OnLoadingStateChanged(content::WebContents* source) {}
     79 
     80   // A callback to notify the delegate that a web dialog has been shown.
     81   // |webui| is the WebUI with which the dialog is associated.
     82   // |render_view_host| is the RenderViewHost for the shown dialog.
     83   virtual void OnDialogShown(content::WebUI* webui,
     84                              content::RenderViewHost* render_view_host) {}
     85 
     86   // A callback to notify the delegate that the dialog closed.
     87   // IMPORTANT: Implementations should delete |this| here (unless they've
     88   // arranged for the delegate to be deleted in some other way, e.g. by
     89   // registering it as a message handler in the WebUI object).
     90   virtual void OnDialogClosed(const std::string& json_retval) = 0;
     91 
     92   // A callback to notify the delegate that the dialog is being closed in
     93   // response to a "dialogClose" message from WebUI.
     94   virtual void OnDialogCloseFromWebUI(const std::string& json_retval);
     95 
     96   // A callback to notify the delegate that the contents have gone
     97   // away. Only relevant if your dialog hosts code that calls
     98   // windows.close() and you've allowed that.  If the output parameter
     99   // is set to true, then the dialog is closed.  The default is false.
    100   virtual void OnCloseContents(content::WebContents* source,
    101                                bool* out_close_dialog) = 0;
    102 
    103   // A callback to allow the delegate to dictate that the window should not
    104   // have a title bar.  This is useful when presenting branded interfaces.
    105   virtual bool ShouldShowDialogTitle() const = 0;
    106 
    107   // A callback to allow the delegate to inhibit context menu or show
    108   // customized menu.
    109   // Returns true iff you do NOT want the standard context menu to be
    110   // shown (because you want to handle it yourself).
    111   virtual bool HandleContextMenu(const content::ContextMenuParams& params);
    112 
    113   // A callback to allow the delegate to open a new URL inside |source|.
    114   // On return |out_new_contents| should contain the WebContents the URL
    115   // is opened in. Return false to use the default handler.
    116   virtual bool HandleOpenURLFromTab(content::WebContents* source,
    117                                     const content::OpenURLParams& params,
    118                                     content::WebContents** out_new_contents);
    119 
    120   // A callback to create a new tab with |new_contents|. |source| is the
    121   // WebContent where the operation originated. |disposition| controls how the
    122   // new tab should be opened. |initial_pos| is the position of the window if a
    123   // new window is created. |user_gesture| is true if the operation was started
    124   // by a user gesture. Return false to use the default handler.
    125   virtual bool HandleAddNewContents(content::WebContents* source,
    126                                     content::WebContents* new_contents,
    127                                     WindowOpenDisposition disposition,
    128                                     const gfx::Rect& initial_pos,
    129                                     bool user_gesture);
    130 
    131   // Stores the dialog bounds.
    132   virtual void StoreDialogSize(const gfx::Size& dialog_size) {}
    133 
    134   virtual ~WebDialogDelegate() {}
    135 };
    136 
    137 }  // namespace ui
    138 
    139 #endif  // UI_WEB_DIALOGS_WEB_DIALOG_DELEGATE_H_
    140