Elim cr-checkbox
[chromium-blink-merge.git] / chrome / browser / chromeos / login / ui / login_web_dialog.cc
blobc46692fae00464eb0fd481e1377fcec6e0b6104d
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"
7 #include <deque>
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;
26 namespace chromeos {
28 namespace {
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;
41 } // namespace
43 ///////////////////////////////////////////////////////////////////////////////
44 // LoginWebDialog, public:
46 void LoginWebDialog::Delegate::OnDialogClosed() {
49 LoginWebDialog::LoginWebDialog(content::BrowserContext* browser_context,
50 Delegate* delegate,
51 gfx::NativeWindow parent_window,
52 const base::string16& title,
53 const GURL& url)
54 : browser_context_(browser_context),
55 parent_window_(parent_window),
56 delegate_(delegate),
57 title_(title),
58 url_(url),
59 is_open_(false) {
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() {
66 delegate_ = NULL;
69 void LoginWebDialog::Show() {
70 chrome::ShowWebDialog(parent_window_, browser_context_, this);
71 is_open_ = true;
74 void LoginWebDialog::SetDialogSize(int width, int height) {
75 DCHECK(width >= 0 && height >= 0);
76 width_ = width;
77 height_ = height;
80 void LoginWebDialog::SetDialogTitle(const base::string16& title) {
81 title_ = title;
84 ///////////////////////////////////////////////////////////////////////////////
85 // LoginWebDialog, protected:
87 ui::ModalType LoginWebDialog::GetDialogModalType() const {
88 return ui::MODAL_TYPE_SYSTEM;
91 base::string16 LoginWebDialog::GetDialogTitle() const {
92 return title_;
95 GURL LoginWebDialog::GetDialogContentURL() const {
96 return url_;
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();
117 // static.
118 content::WebContents* LoginWebDialog::GetCurrentWebContents() {
119 if (!g_web_contents_stack.Pointer()->size())
120 return NULL;
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) {
131 is_open_ = false;
132 notification_registrar_.RemoveAll();
133 if (delegate_)
134 delegate_->OnDialogClosed();
135 delete this;
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 {
151 return true;
154 bool LoginWebDialog::HandleContextMenu(
155 const content::ContextMenuParams& params) {
156 // Disable context menu.
157 return true;
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() {
174 return false;
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?
182 NOTIMPLEMENTED();
185 } // namespace chromeos