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/chromeos/login/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/chromeos/profiles/profile_helper.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/browser_dialogs.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/native_widget_types.h"
19 #include "ui/gfx/rect.h"
20 #include "ui/gfx/size.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(Profile
* profile
,
51 gfx::NativeWindow parent_window
,
52 const base::string16
& title
,
56 parent_window_(parent_window
),
62 gfx::Rect
screen_bounds(chromeos::CalculateScreenBounds(gfx::Size()));
63 width_
= static_cast<int>(kDefaultWidthRatio
* screen_bounds
.width());
64 height_
= static_cast<int>(kDefaultHeightRatio
* screen_bounds
.height());
67 LoginWebDialog::~LoginWebDialog() {
71 void LoginWebDialog::Show() {
72 chrome::ShowWebDialog(parent_window_
,
78 void LoginWebDialog::SetDialogSize(int width
, int height
) {
79 DCHECK(width
>= 0 && height
>= 0);
84 void LoginWebDialog::SetDialogTitle(const base::string16
& title
) {
88 ///////////////////////////////////////////////////////////////////////////////
89 // LoginWebDialog, protected:
91 ui::ModalType
LoginWebDialog::GetDialogModalType() const {
92 return ui::MODAL_TYPE_SYSTEM
;
95 base::string16
LoginWebDialog::GetDialogTitle() const {
99 GURL
LoginWebDialog::GetDialogContentURL() const {
103 void LoginWebDialog::GetWebUIMessageHandlers(
104 std::vector
<WebUIMessageHandler
*>* handlers
) const {
107 void LoginWebDialog::GetDialogSize(gfx::Size
* size
) const {
108 size
->SetSize(width_
, height_
);
111 void LoginWebDialog::GetMinimumDialogSize(gfx::Size
* size
) const {
112 gfx::Rect
screen_bounds(chromeos::CalculateScreenBounds(gfx::Size()));
113 size
->SetSize(kMinimumWidthRatio
* screen_bounds
.width(),
114 kMinimumHeightRatio
* screen_bounds
.height());
117 std::string
LoginWebDialog::GetDialogArgs() const {
118 return std::string();
122 content::WebContents
* LoginWebDialog::GetCurrentWebContents() {
123 if (!g_web_contents_stack
.Pointer()->size())
126 return g_web_contents_stack
.Pointer()->front();
129 void LoginWebDialog::OnDialogShown(content::WebUI
* webui
,
130 content::RenderViewHost
* render_view_host
) {
131 g_web_contents_stack
.Pointer()->push_front(webui
->GetWebContents());
134 void LoginWebDialog::OnDialogClosed(const std::string
& json_retval
) {
136 notification_registrar_
.RemoveAll();
138 delegate_
->OnDialogClosed();
142 void LoginWebDialog::OnCloseContents(WebContents
* source
,
143 bool* out_close_dialog
) {
144 if (out_close_dialog
)
145 *out_close_dialog
= true;
147 if (g_web_contents_stack
.Pointer()->size() &&
148 source
== g_web_contents_stack
.Pointer()->front()) {
149 g_web_contents_stack
.Pointer()->pop_front();
155 bool LoginWebDialog::ShouldShowDialogTitle() const {
159 bool LoginWebDialog::HandleContextMenu(
160 const content::ContextMenuParams
& params
) {
161 // Disable context menu.
165 void LoginWebDialog::Observe(int type
,
166 const content::NotificationSource
& source
,
167 const content::NotificationDetails
& details
) {
168 DCHECK(type
== content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME
);
169 // TODO(saintlou): Do we need a throbber for Aura?
173 } // namespace chromeos