Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / views / controls / webview / web_dialog_view.h
blobdc6598d8d4cefcff84f05f6fda1500a870d8933e
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.
5 #ifndef UI_VIEWS_CONTROLS_WEBVIEW_WEB_DIALOG_VIEW_H_
6 #define UI_VIEWS_CONTROLS_WEBVIEW_WEB_DIALOG_VIEW_H_
8 #include <string>
9 #include <vector>
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "ui/gfx/geometry/size.h"
14 #include "ui/views/controls/webview/webview_export.h"
15 #include "ui/views/widget/widget_delegate.h"
16 #include "ui/views/window/client_view.h"
17 #include "ui/web_dialogs/web_dialog_delegate.h"
18 #include "ui/web_dialogs/web_dialog_web_contents_delegate.h"
20 namespace content {
21 class BrowserContext;
24 namespace views {
25 class WebView;
27 ////////////////////////////////////////////////////////////////////////////////
29 // WebDialogView is a view used to display an web dialog to the user. The
30 // content of the dialogs is determined by the delegate
31 // (ui::WebDialogDelegate), but is basically a file URL along with a
32 // JSON input string. The HTML is supposed to show a UI to the user and is
33 // expected to send back a JSON file as a return value.
35 ////////////////////////////////////////////////////////////////////////////////
37 // TODO(akalin): Make WebDialogView contain an WebDialogWebContentsDelegate
38 // instead of inheriting from it to avoid violating the "no multiple
39 // inheritance" rule.
40 class WEBVIEW_EXPORT WebDialogView : public views::ClientView,
41 public ui::WebDialogWebContentsDelegate,
42 public ui::WebDialogDelegate,
43 public views::WidgetDelegate {
44 public:
45 // |handler| must not be NULL and this class takes the ownership.
46 WebDialogView(content::BrowserContext* context,
47 ui::WebDialogDelegate* delegate,
48 WebContentsHandler* handler);
49 ~WebDialogView() override;
51 // For testing.
52 content::WebContents* web_contents();
54 // Overridden from views::ClientView:
55 gfx::Size GetPreferredSize() const override;
56 gfx::Size GetMinimumSize() const override;
57 bool AcceleratorPressed(const ui::Accelerator& accelerator) override;
58 void ViewHierarchyChanged(
59 const ViewHierarchyChangedDetails& details) override;
60 bool CanClose() override;
62 // Overridden from views::WidgetDelegate:
63 bool CanResize() const override;
64 ui::ModalType GetModalType() const override;
65 base::string16 GetWindowTitle() const override;
66 std::string GetWindowName() const override;
67 void WindowClosing() override;
68 views::View* GetContentsView() override;
69 ClientView* CreateClientView(views::Widget* widget) override;
70 views::View* GetInitiallyFocusedView() override;
71 bool ShouldShowWindowTitle() const override;
72 views::Widget* GetWidget() override;
73 const views::Widget* GetWidget() const override;
75 // Overridden from ui::WebDialogDelegate:
76 ui::ModalType GetDialogModalType() const override;
77 base::string16 GetDialogTitle() const override;
78 GURL GetDialogContentURL() const override;
79 void GetWebUIMessageHandlers(
80 std::vector<content::WebUIMessageHandler*>* handlers) const override;
81 void GetDialogSize(gfx::Size* size) const override;
82 void GetMinimumDialogSize(gfx::Size* size) const override;
83 std::string GetDialogArgs() const override;
84 void OnDialogShown(content::WebUI* webui,
85 content::RenderViewHost* render_view_host) override;
86 void OnDialogClosed(const std::string& json_retval) override;
87 void OnDialogCloseFromWebUI(const std::string& json_retval) override;
88 void OnCloseContents(content::WebContents* source,
89 bool* out_close_dialog) override;
90 bool ShouldShowDialogTitle() const override;
91 bool HandleContextMenu(const content::ContextMenuParams& params) override;
93 // Overridden from content::WebContentsDelegate:
94 void MoveContents(content::WebContents* source,
95 const gfx::Rect& pos) override;
96 void HandleKeyboardEvent(
97 content::WebContents* source,
98 const content::NativeWebKeyboardEvent& event) override;
99 void CloseContents(content::WebContents* source) override;
100 content::WebContents* OpenURLFromTab(
101 content::WebContents* source,
102 const content::OpenURLParams& params) override;
103 void AddNewContents(content::WebContents* source,
104 content::WebContents* new_contents,
105 WindowOpenDisposition disposition,
106 const gfx::Rect& initial_rect,
107 bool user_gesture,
108 bool* was_blocked) override;
109 void LoadingStateChanged(content::WebContents* source,
110 bool to_different_document) override;
111 void BeforeUnloadFired(content::WebContents* tab,
112 bool proceed,
113 bool* proceed_to_fire_unload) override;
114 bool ShouldCreateWebContents(
115 content::WebContents* web_contents,
116 int route_id,
117 int main_frame_route_id,
118 WindowContainerType window_container_type,
119 const std::string& frame_name,
120 const GURL& target_url,
121 const std::string& partition_id,
122 content::SessionStorageNamespace* session_storage_namespace) override;
124 private:
125 FRIEND_TEST_ALL_PREFIXES(WebDialogBrowserTest, WebContentRendered);
127 // Initializes the contents of the dialog.
128 void InitDialog();
130 // This view is a delegate to the HTML content since it needs to get notified
131 // about when the dialog is closing. For all other actions (besides dialog
132 // closing) we delegate to the creator of this view, which we keep track of
133 // using this variable.
134 ui::WebDialogDelegate* delegate_;
136 views::WebView* web_view_;
138 // Whether user is attempting to close the dialog and we are processing
139 // beforeunload event.
140 bool is_attempting_close_dialog_;
142 // Whether beforeunload event has been fired and we have finished processing
143 // beforeunload event.
144 bool before_unload_fired_;
146 // Whether the dialog is closed from WebUI in response to a "dialogClose"
147 // message.
148 bool closed_via_webui_;
150 // A json string returned to WebUI from a "dialogClose" message.
151 std::string dialog_close_retval_;
153 // Whether CloseContents() has been called.
154 bool close_contents_called_;
156 DISALLOW_COPY_AND_ASSIGN(WebDialogView);
159 } // namespace views
161 #endif // UI_VIEWS_CONTROLS_WEBVIEW_WEB_DIALOG_VIEW_H_