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 "chrome/browser/ui/views/extensions/extension_dialog.h"
7 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/extensions/extension_view_host.h"
9 #include "chrome/browser/extensions/extension_view_host_factory.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/views/extensions/extension_dialog_observer.h"
12 #include "chrome/browser/ui/views/extensions/extension_view_views.h"
13 #include "components/constrained_window/constrained_window_views.h"
14 #include "content/public/browser/notification_details.h"
15 #include "content/public/browser/notification_source.h"
16 #include "content/public/browser/render_view_host.h"
17 #include "content/public/browser/render_widget_host_view.h"
18 #include "content/public/browser/web_contents.h"
19 #include "ui/base/base_window.h"
20 #include "ui/gfx/screen.h"
21 #include "ui/views/background.h"
22 #include "ui/views/widget/widget.h"
25 using content::BrowserContext
;
26 using content::WebContents
;
30 ExtensionViewViews
* GetExtensionView(extensions::ExtensionViewHost
* host
) {
31 return static_cast<ExtensionViewViews
*>(host
->view());
36 ExtensionDialog::ExtensionDialog(extensions::ExtensionViewHost
* host
,
37 ExtensionDialogObserver
* observer
)
40 AddRef(); // Balanced in DeleteDelegate();
43 extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_FIRST_LOAD
,
44 content::Source
<BrowserContext
>(host
->browser_context()));
45 // Listen for the containing view calling window.close();
47 extensions::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE
,
48 content::Source
<BrowserContext
>(host
->browser_context()));
49 // Listen for a crash or other termination of the extension process.
51 extensions::NOTIFICATION_EXTENSION_PROCESS_TERMINATED
,
52 content::Source
<BrowserContext
>(host
->browser_context()));
55 ExtensionDialog::~ExtensionDialog() {
59 ExtensionDialog
* ExtensionDialog::Show(
61 gfx::NativeWindow parent_window
,
63 WebContents
* web_contents
,
68 const base::string16
& title
,
69 ExtensionDialogObserver
* observer
) {
70 extensions::ExtensionViewHost
* host
=
71 extensions::ExtensionViewHostFactory::CreateDialogHost(url
, profile
);
74 // Preferred size must be set before views::Widget::CreateWindowWithParent
75 // is called because CreateWindowWithParent refers the result of CanResize().
76 ExtensionViewViews
* view
= GetExtensionView(host
);
77 view
->SetPreferredSize(gfx::Size(width
, height
));
78 view
->set_minimum_size(gfx::Size(min_width
, min_height
));
79 host
->SetAssociatedWebContents(web_contents
);
81 DCHECK(parent_window
);
82 ExtensionDialog
* dialog
= new ExtensionDialog(host
, observer
);
83 dialog
->set_title(title
);
84 dialog
->InitWindow(parent_window
, width
, height
);
86 // Show a white background while the extension loads. This is prettier than
87 // flashing a black unfilled window frame.
89 views::Background::CreateSolidBackground(0xFF, 0xFF, 0xFF));
90 view
->SetVisible(true);
92 // Ensure the DOM JavaScript can respond immediately to keyboard shortcuts.
93 host
->host_contents()->Focus();
97 void ExtensionDialog::InitWindow(gfx::NativeWindow parent
,
100 views::Widget
* window
=
101 constrained_window::CreateBrowserModalDialogViews(this, parent
);
103 // Center the window over the browser.
104 views::Widget
* parent_widget
=
105 views::Widget::GetWidgetForNativeWindow(parent
);
106 gfx::Point center
= parent_widget
->GetWindowBoundsInScreen().CenterPoint();
107 int x
= center
.x() - width
/ 2;
108 int y
= center
.y() - height
/ 2;
109 // Ensure the top left and top right of the window are on screen, with
110 // priority given to the top left.
111 gfx::Rect screen_rect
=
112 gfx::Screen::GetScreenFor(parent_widget
->GetNativeView())
113 ->GetDisplayNearestPoint(center
).bounds();
114 gfx::Rect bounds_rect
= gfx::Rect(x
, y
, width
, height
);
115 bounds_rect
.AdjustToFit(screen_rect
);
116 window
->SetBounds(bounds_rect
);
119 // TODO(jamescook): Remove redundant call to Activate()?
123 void ExtensionDialog::ObserverDestroyed() {
127 void ExtensionDialog::MaybeFocusRenderView() {
128 views::FocusManager
* focus_manager
= GetWidget()->GetFocusManager();
129 DCHECK(focus_manager
!= NULL
);
131 // Already there's a focused view, so no need to switch the focus.
132 if (focus_manager
->GetFocusedView())
135 content::RenderWidgetHostView
* view
= host()->render_view_host()->GetView();
142 /////////////////////////////////////////////////////////////////////////////
143 // views::DialogDelegate overrides.
145 int ExtensionDialog::GetDialogButtons() const {
146 // The only user, SelectFileDialogExtension, provides its own buttons.
147 return ui::DIALOG_BUTTON_NONE
;
150 bool ExtensionDialog::CanResize() const {
151 // Can resize only if minimum contents size set.
152 return static_cast<views::View
*>(GetExtensionView(host_
.get()))->
153 GetPreferredSize() != gfx::Size();
156 void ExtensionDialog::SetMinimumContentsSize(int width
, int height
) {
157 GetExtensionView(host_
.get())->SetPreferredSize(gfx::Size(width
, height
));
160 ui::ModalType
ExtensionDialog::GetModalType() const {
161 return ui::MODAL_TYPE_WINDOW
;
164 bool ExtensionDialog::ShouldShowWindowTitle() const {
165 return !window_title_
.empty();
168 base::string16
ExtensionDialog::GetWindowTitle() const {
169 return window_title_
;
172 void ExtensionDialog::WindowClosing() {
174 observer_
->ExtensionDialogClosing(this);
177 void ExtensionDialog::DeleteDelegate() {
178 // The window has finished closing. Allow ourself to be deleted.
182 views::Widget
* ExtensionDialog::GetWidget() {
183 return GetExtensionView(host_
.get())->GetWidget();
186 const views::Widget
* ExtensionDialog::GetWidget() const {
187 return GetExtensionView(host_
.get())->GetWidget();
190 views::View
* ExtensionDialog::GetContentsView() {
191 return GetExtensionView(host_
.get());
194 bool ExtensionDialog::UseNewStyleForThisDialog() const {
198 /////////////////////////////////////////////////////////////////////////////
199 // content::NotificationObserver overrides.
201 void ExtensionDialog::Observe(int type
,
202 const content::NotificationSource
& source
,
203 const content::NotificationDetails
& details
) {
205 case extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_FIRST_LOAD
:
206 // Avoid potential overdraw by removing the temporary background after
207 // the extension finishes loading.
208 GetExtensionView(host_
.get())->set_background(NULL
);
209 // The render view is created during the LoadURL(), so we should
210 // set the focus to the view if nobody else takes the focus.
211 if (content::Details
<extensions::ExtensionHost
>(host()) == details
)
212 MaybeFocusRenderView();
214 case extensions::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE
:
215 // If we aren't the host of the popup, then disregard the notification.
216 if (content::Details
<extensions::ExtensionHost
>(host()) != details
)
218 GetWidget()->Close();
220 case extensions::NOTIFICATION_EXTENSION_PROCESS_TERMINATED
:
221 if (content::Details
<extensions::ExtensionHost
>(host()) != details
)
224 observer_
->ExtensionTerminated(this);
227 NOTREACHED() << L
"Received unexpected notification";