Update V8 to version 4.6.62.
[chromium-blink-merge.git] / components / constrained_window / constrained_window_views.cc
blobd00551385504274b22bd5666e1d1d8496dd99168
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 #include "components/constrained_window/constrained_window_views.h"
7 #include <algorithm>
9 #include "components/constrained_window/constrained_window_views_client.h"
10 #include "components/guest_view/browser/guest_view_base.h"
11 #include "components/web_modal/web_contents_modal_dialog_host.h"
12 #include "components/web_modal/web_contents_modal_dialog_manager.h"
13 #include "components/web_modal/web_contents_modal_dialog_manager_delegate.h"
14 #include "ui/views/border.h"
15 #include "ui/views/widget/widget.h"
16 #include "ui/views/widget/widget_observer.h"
17 #include "ui/views/window/dialog_delegate.h"
19 using web_modal::ModalDialogHost;
20 using web_modal::ModalDialogHostObserver;
22 namespace constrained_window {
23 namespace {
25 ConstrainedWindowViewsClient* constrained_window_views_client = NULL;
27 // The name of a key to store on the window handle to associate
28 // WidgetModalDialogHostObserverViews with the Widget.
29 const char* const kWidgetModalDialogHostObserverViewsKey =
30 "__WIDGET_MODAL_DIALOG_HOST_OBSERVER_VIEWS__";
32 // Applies positioning changes from the ModalDialogHost to the Widget.
33 class WidgetModalDialogHostObserverViews
34 : public views::WidgetObserver,
35 public ModalDialogHostObserver {
36 public:
37 WidgetModalDialogHostObserverViews(ModalDialogHost* host,
38 views::Widget* target_widget,
39 const char *const native_window_property)
40 : host_(host),
41 target_widget_(target_widget),
42 native_window_property_(native_window_property) {
43 DCHECK(host_);
44 DCHECK(target_widget_);
45 host_->AddObserver(this);
46 target_widget_->AddObserver(this);
49 ~WidgetModalDialogHostObserverViews() override {
50 if (host_)
51 host_->RemoveObserver(this);
52 target_widget_->RemoveObserver(this);
53 target_widget_->SetNativeWindowProperty(native_window_property_, NULL);
56 // WidgetObserver overrides
57 void OnWidgetClosing(views::Widget* widget) override { delete this; }
59 // WebContentsModalDialogHostObserver overrides
60 void OnPositionRequiresUpdate() override {
61 UpdateWidgetModalDialogPosition(target_widget_, host_);
64 void OnHostDestroying() override {
65 host_->RemoveObserver(this);
66 host_ = NULL;
69 private:
70 ModalDialogHost* host_;
71 views::Widget* target_widget_;
72 const char* const native_window_property_;
74 DISALLOW_COPY_AND_ASSIGN(WidgetModalDialogHostObserverViews);
77 void UpdateModalDialogPosition(views::Widget* widget,
78 web_modal::ModalDialogHost* dialog_host,
79 const gfx::Size& size) {
80 // Do not forcibly update the dialog widget position if it is being dragged.
81 if (widget->HasCapture())
82 return;
84 gfx::Point position = dialog_host->GetDialogPosition(size);
85 views::Border* border = widget->non_client_view()->frame_view()->border();
86 // Border may be null during widget initialization.
87 if (border) {
88 // Align the first row of pixels inside the border. This is the apparent
89 // top of the dialog.
90 position.set_y(position.y() - border->GetInsets().top());
93 if (widget->is_top_level()) {
94 position +=
95 views::Widget::GetWidgetForNativeView(dialog_host->GetHostView())->
96 GetClientAreaBoundsInScreen().OffsetFromOrigin();
99 widget->SetBounds(gfx::Rect(position, size));
102 } // namespace
104 // static
105 void SetConstrainedWindowViewsClient(
106 scoped_ptr<ConstrainedWindowViewsClient> new_client) {
107 delete constrained_window_views_client;
108 constrained_window_views_client = new_client.release();
111 void UpdateWebContentsModalDialogPosition(
112 views::Widget* widget,
113 web_modal::WebContentsModalDialogHost* dialog_host) {
114 gfx::Size size = widget->GetRootView()->GetPreferredSize();
115 gfx::Size max_size = dialog_host->GetMaximumDialogSize();
116 // Enlarge the max size by the top border, as the dialog will be shifted
117 // outside the area specified by the dialog host by this amount later.
118 views::Border* border =
119 widget->non_client_view()->frame_view()->border();
120 // Border may be null during widget initialization.
121 if (border)
122 max_size.Enlarge(0, border->GetInsets().top());
123 size.SetToMin(max_size);
124 UpdateModalDialogPosition(widget, dialog_host, size);
127 void UpdateWidgetModalDialogPosition(views::Widget* widget,
128 web_modal::ModalDialogHost* dialog_host) {
129 UpdateModalDialogPosition(widget, dialog_host,
130 widget->GetRootView()->GetPreferredSize());
133 views::Widget* ShowWebModalDialogViews(
134 views::WidgetDelegate* dialog,
135 content::WebContents* initiator_web_contents) {
136 DCHECK(constrained_window_views_client);
137 // For embedded WebContents, use the embedder's WebContents for constrained
138 // window.
139 content::WebContents* web_contents =
140 guest_view::GuestViewBase::GetTopLevelWebContents(initiator_web_contents);
141 views::Widget* widget = CreateWebModalDialogViews(dialog, web_contents);
142 web_modal::WebContentsModalDialogManager::FromWebContents(web_contents)
143 ->ShowModalDialog(widget->GetNativeWindow());
144 return widget;
147 views::Widget* CreateWebModalDialogViews(views::WidgetDelegate* dialog,
148 content::WebContents* web_contents) {
149 DCHECK_EQ(ui::MODAL_TYPE_CHILD, dialog->GetModalType());
150 return views::DialogDelegate::CreateDialogWidget(
151 dialog, nullptr,
152 web_modal::WebContentsModalDialogManager::FromWebContents(web_contents)
153 ->delegate()
154 ->GetWebContentsModalDialogHost()
155 ->GetHostView());
158 views::Widget* CreateBrowserModalDialogViews(views::DialogDelegate* dialog,
159 gfx::NativeWindow parent) {
160 DCHECK_NE(ui::MODAL_TYPE_CHILD, dialog->GetModalType());
161 DCHECK_NE(ui::MODAL_TYPE_NONE, dialog->GetModalType());
163 DCHECK(constrained_window_views_client);
164 gfx::NativeView parent_view =
165 parent ? constrained_window_views_client->GetDialogHostView(parent)
166 : nullptr;
167 views::Widget* widget =
168 views::DialogDelegate::CreateDialogWidget(dialog, NULL, parent_view);
169 if (!dialog->UseNewStyleForThisDialog())
170 return widget;
171 ModalDialogHost* host = constrained_window_views_client->
172 GetModalDialogHost(parent);
173 if (host) {
174 DCHECK_EQ(parent_view, host->GetHostView());
175 ModalDialogHostObserver* dialog_host_observer =
176 new WidgetModalDialogHostObserverViews(
177 host, widget, kWidgetModalDialogHostObserverViewsKey);
178 dialog_host_observer->OnPositionRequiresUpdate();
180 return widget;
183 } // namespace constrained window