Revert of Remove OneClickSigninHelper since it is no longer used. (patchset #5 id...
[chromium-blink-merge.git] / chrome / browser / ui / views / extensions / extension_dialog.cc
blobcc8f31a8094c61e584f67157da3fa817619dff8b
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"
23 #include "url/gurl.h"
25 using content::BrowserContext;
26 using content::WebContents;
28 namespace {
30 ExtensionViewViews* GetExtensionView(extensions::ExtensionViewHost* host) {
31 return static_cast<ExtensionViewViews*>(host->view());
34 } // namespace
36 ExtensionDialog::ExtensionDialog(extensions::ExtensionViewHost* host,
37 ExtensionDialogObserver* observer)
38 : host_(host),
39 observer_(observer) {
40 AddRef(); // Balanced in DeleteDelegate();
42 registrar_.Add(this,
43 extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING,
44 content::Source<BrowserContext>(host->browser_context()));
45 // Listen for the containing view calling window.close();
46 registrar_.Add(this,
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.
50 registrar_.Add(this,
51 extensions::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
52 content::Source<BrowserContext>(host->browser_context()));
55 ExtensionDialog::~ExtensionDialog() {
58 // static
59 ExtensionDialog* ExtensionDialog::Show(
60 const GURL& url,
61 gfx::NativeWindow parent_window,
62 Profile* profile,
63 WebContents* web_contents,
64 int width,
65 int height,
66 int min_width,
67 int min_height,
68 const base::string16& title,
69 ExtensionDialogObserver* observer) {
70 extensions::ExtensionViewHost* host =
71 extensions::ExtensionViewHostFactory::CreateDialogHost(url, profile);
72 if (!host)
73 return NULL;
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.
88 view->set_background(
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();
94 return dialog;
97 void ExtensionDialog::InitWindow(gfx::NativeWindow parent,
98 int width,
99 int height) {
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);
118 window->Show();
119 // TODO(jamescook): Remove redundant call to Activate()?
120 window->Activate();
123 void ExtensionDialog::ObserverDestroyed() {
124 observer_ = NULL;
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())
133 return;
135 content::RenderWidgetHostView* view = host()->render_view_host()->GetView();
136 if (!view)
137 return;
139 view->Focus();
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 GetExtensionView(host_.get())->GetPreferredSize() != gfx::Size();
155 void ExtensionDialog::SetMinimumContentsSize(int width, int height) {
156 GetExtensionView(host_.get())->SetPreferredSize(gfx::Size(width, height));
159 ui::ModalType ExtensionDialog::GetModalType() const {
160 return ui::MODAL_TYPE_WINDOW;
163 bool ExtensionDialog::ShouldShowWindowTitle() const {
164 return !window_title_.empty();
167 base::string16 ExtensionDialog::GetWindowTitle() const {
168 return window_title_;
171 void ExtensionDialog::WindowClosing() {
172 if (observer_)
173 observer_->ExtensionDialogClosing(this);
176 void ExtensionDialog::DeleteDelegate() {
177 // The window has finished closing. Allow ourself to be deleted.
178 Release();
181 views::Widget* ExtensionDialog::GetWidget() {
182 return GetExtensionView(host_.get())->GetWidget();
185 const views::Widget* ExtensionDialog::GetWidget() const {
186 return GetExtensionView(host_.get())->GetWidget();
189 views::View* ExtensionDialog::GetContentsView() {
190 return GetExtensionView(host_.get());
193 bool ExtensionDialog::UseNewStyleForThisDialog() const {
194 return false;
197 /////////////////////////////////////////////////////////////////////////////
198 // content::NotificationObserver overrides.
200 void ExtensionDialog::Observe(int type,
201 const content::NotificationSource& source,
202 const content::NotificationDetails& details) {
203 switch (type) {
204 case extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING:
205 // Avoid potential overdraw by removing the temporary background after
206 // the extension finishes loading.
207 GetExtensionView(host_.get())->set_background(NULL);
208 // The render view is created during the LoadURL(), so we should
209 // set the focus to the view if nobody else takes the focus.
210 if (content::Details<extensions::ExtensionHost>(host()) == details)
211 MaybeFocusRenderView();
212 break;
213 case extensions::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE:
214 // If we aren't the host of the popup, then disregard the notification.
215 if (content::Details<extensions::ExtensionHost>(host()) != details)
216 return;
217 GetWidget()->Close();
218 break;
219 case extensions::NOTIFICATION_EXTENSION_PROCESS_TERMINATED:
220 if (content::Details<extensions::ExtensionHost>(host()) != details)
221 return;
222 if (observer_)
223 observer_->ExtensionTerminated(this);
224 break;
225 default:
226 NOTREACHED() << L"Received unexpected notification";
227 break;