1 // Copyright 2014 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/chromeos/login/ui/login_web_dialog.h"
9 #include "base/lazy_instance.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/chromeos/login/helper.h"
12 #include "chrome/browser/ui/browser_dialogs.h"
13 #include "chrome/browser/ui/browser_finder.h"
14 #include "content/public/browser/browser_context.h"
15 #include "content/public/browser/notification_source.h"
16 #include "content/public/browser/notification_types.h"
17 #include "content/public/browser/web_contents.h"
18 #include "ui/gfx/geometry/rect.h"
19 #include "ui/gfx/geometry/size.h"
20 #include "ui/gfx/native_widget_types.h"
21 #include "ui/views/widget/widget.h"
23 using content::WebContents
;
24 using content::WebUIMessageHandler
;
30 // Default width/height ratio of screen size.
31 const double kDefaultWidthRatio
= 0.6;
32 const double kDefaultHeightRatio
= 0.6;
34 // Default width/height ratio of minimal dialog size.
35 const double kMinimumWidthRatio
= 0.25;
36 const double kMinimumHeightRatio
= 0.25;
38 static base::LazyInstance
<std::deque
<content::WebContents
*> >
39 g_web_contents_stack
= LAZY_INSTANCE_INITIALIZER
;
43 ///////////////////////////////////////////////////////////////////////////////
44 // LoginWebDialog, public:
46 void LoginWebDialog::Delegate::OnDialogClosed() {
49 LoginWebDialog::LoginWebDialog(content::BrowserContext
* browser_context
,
51 gfx::NativeWindow parent_window
,
52 const base::string16
& title
,
54 : browser_context_(browser_context
),
55 parent_window_(parent_window
),
60 gfx::Rect
screen_bounds(chromeos::CalculateScreenBounds(gfx::Size()));
61 width_
= static_cast<int>(kDefaultWidthRatio
* screen_bounds
.width());
62 height_
= static_cast<int>(kDefaultHeightRatio
* screen_bounds
.height());
65 LoginWebDialog::~LoginWebDialog() {
69 void LoginWebDialog::Show() {
70 chrome::ShowWebDialog(parent_window_
, browser_context_
, this);
74 void LoginWebDialog::SetDialogSize(int width
, int height
) {
75 DCHECK(width
>= 0 && height
>= 0);
80 void LoginWebDialog::SetDialogTitle(const base::string16
& title
) {
84 ///////////////////////////////////////////////////////////////////////////////
85 // LoginWebDialog, protected:
87 ui::ModalType
LoginWebDialog::GetDialogModalType() const {
88 return ui::MODAL_TYPE_SYSTEM
;
91 base::string16
LoginWebDialog::GetDialogTitle() const {
95 GURL
LoginWebDialog::GetDialogContentURL() const {
99 void LoginWebDialog::GetWebUIMessageHandlers(
100 std::vector
<WebUIMessageHandler
*>* handlers
) const {
103 void LoginWebDialog::GetDialogSize(gfx::Size
* size
) const {
104 size
->SetSize(width_
, height_
);
107 void LoginWebDialog::GetMinimumDialogSize(gfx::Size
* size
) const {
108 gfx::Rect
screen_bounds(chromeos::CalculateScreenBounds(gfx::Size()));
109 size
->SetSize(kMinimumWidthRatio
* screen_bounds
.width(),
110 kMinimumHeightRatio
* screen_bounds
.height());
113 std::string
LoginWebDialog::GetDialogArgs() const {
114 return std::string();
118 content::WebContents
* LoginWebDialog::GetCurrentWebContents() {
119 if (!g_web_contents_stack
.Pointer()->size())
122 return g_web_contents_stack
.Pointer()->front();
125 void LoginWebDialog::OnDialogShown(content::WebUI
* webui
,
126 content::RenderViewHost
* render_view_host
) {
127 g_web_contents_stack
.Pointer()->push_front(webui
->GetWebContents());
130 void LoginWebDialog::OnDialogClosed(const std::string
& json_retval
) {
132 notification_registrar_
.RemoveAll();
134 delegate_
->OnDialogClosed();
138 void LoginWebDialog::OnCloseContents(WebContents
* source
,
139 bool* out_close_dialog
) {
140 *out_close_dialog
= true;
142 if (g_web_contents_stack
.Pointer()->size() &&
143 source
== g_web_contents_stack
.Pointer()->front()) {
144 g_web_contents_stack
.Pointer()->pop_front();
146 // Else: TODO(pkotwicz): Investigate if the else case should ever be hit.
147 // http://crbug.com/419837
150 bool LoginWebDialog::ShouldShowDialogTitle() const {
154 bool LoginWebDialog::HandleContextMenu(
155 const content::ContextMenuParams
& params
) {
156 // Disable context menu.
160 bool LoginWebDialog::HandleOpenURLFromTab(
161 content::WebContents
* source
,
162 const content::OpenURLParams
& params
,
163 content::WebContents
** out_new_contents
) {
164 // On a login screen, if a missing extension is trying to show in a web
165 // dialog, a NetErrorHelper is displayed instead (hence we have a |source|),
166 // but there is no browser window associated with it. A helper screen will
167 // fire an auto-reload, which in turn leads to opening a new browser window,
168 // so we must suppress it.
169 // http://crbug.com/443096
170 return (source
&& !chrome::FindBrowserWithWebContents(source
));
173 bool LoginWebDialog::HandleShouldCreateWebContents() {
177 void LoginWebDialog::Observe(int type
,
178 const content::NotificationSource
& source
,
179 const content::NotificationDetails
& details
) {
180 DCHECK(type
== content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME
);
181 // TODO(saintlou): Do we need a throbber for Aura?
185 } // namespace chromeos