1 // Copyright (c) 2013 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/webui/signin/profile_signin_confirmation_dialog.h"
7 #include "base/basictypes.h"
8 #include "base/json/json_writer.h"
9 #include "base/logging.h"
10 #include "base/values.h"
11 #include "chrome/browser/profiles/profile_manager.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/browser_dialogs.h"
14 #include "chrome/browser/ui/browser_window.h"
15 #include "chrome/browser/ui/webui/constrained_web_dialog_ui.h"
16 #include "chrome/common/url_constants.h"
17 #include "content/public/browser/web_ui.h"
18 #include "content/public/browser/web_ui_message_handler.h"
19 #include "grit/browser_resources.h"
20 #include "grit/chromium_strings.h"
21 #include "grit/generated_resources.h"
22 #include "ui/base/l10n/l10n_util.h"
24 // ProfileSigninConfirmationHandler --------------------------------------------
28 class ProfileSigninConfirmationHandler
: public content::WebUIMessageHandler
{
30 ProfileSigninConfirmationHandler(
31 const ProfileSigninConfirmationDialog
* dialog
,
32 ui::ProfileSigninConfirmationDelegate
* delegate_
);
33 virtual ~ProfileSigninConfirmationHandler();
34 virtual void RegisterMessages() OVERRIDE
;
37 // content::WebUIMessageHandler implementation.
38 void OnCancelButtonClicked(const base::ListValue
* args
);
39 void OnCreateProfileClicked(const base::ListValue
* args
);
40 void OnContinueButtonClicked(const base::ListValue
* args
);
42 // Weak ptr to parent dialog.
43 const ProfileSigninConfirmationDialog
* dialog_
;
45 // Dialog button handling.
46 ui::ProfileSigninConfirmationDelegate
* delegate_
;
49 ProfileSigninConfirmationHandler::ProfileSigninConfirmationHandler(
50 const ProfileSigninConfirmationDialog
* dialog
,
51 ui::ProfileSigninConfirmationDelegate
* delegate
)
52 : dialog_(dialog
), delegate_(delegate
) {
55 ProfileSigninConfirmationHandler::~ProfileSigninConfirmationHandler() {
58 void ProfileSigninConfirmationHandler::RegisterMessages() {
59 web_ui()->RegisterMessageCallback(
61 base::Bind(&ProfileSigninConfirmationHandler::OnCancelButtonClicked
,
62 base::Unretained(this)));
63 web_ui()->RegisterMessageCallback(
65 base::Bind(&ProfileSigninConfirmationHandler::OnCreateProfileClicked
,
66 base::Unretained(this)));
67 web_ui()->RegisterMessageCallback(
69 base::Bind(&ProfileSigninConfirmationHandler::OnContinueButtonClicked
,
70 base::Unretained(this)));
73 void ProfileSigninConfirmationHandler::OnCancelButtonClicked(
74 const base::ListValue
* args
) {
75 // TODO(dconnelly): redirect back to NTP?
76 delegate_
->OnCancelSignin();
80 void ProfileSigninConfirmationHandler::OnCreateProfileClicked(
81 const base::ListValue
* args
) {
82 delegate_
->OnSigninWithNewProfile();
86 void ProfileSigninConfirmationHandler::OnContinueButtonClicked(
87 const base::ListValue
* args
) {
88 delegate_
->OnContinueSignin();
94 #if !defined(TOOLKIT_VIEWS) && !defined(OS_MACOSX)
97 // Declared in browser_dialogs.h
98 void ShowProfileSigninConfirmationDialog(
100 content::WebContents
* web_contents
,
102 const std::string
& username
,
103 ui::ProfileSigninConfirmationDelegate
* delegate
) {
104 ProfileSigninConfirmationDialog::ShowDialog(web_contents
,
109 } // namespace chrome
112 // ProfileSigninConfirmationDialog ---------------------------------------------
114 ProfileSigninConfirmationDialog::ProfileSigninConfirmationDialog(
115 content::WebContents
* web_contents
,
117 const std::string
& username
,
118 ui::ProfileSigninConfirmationDelegate
* delegate
)
119 : web_contents_(web_contents
),
122 signin_delegate_(delegate
),
123 dialog_delegate_(NULL
),
124 prompt_for_new_profile_(true) {
127 ProfileSigninConfirmationDialog::~ProfileSigninConfirmationDialog() {
131 void ProfileSigninConfirmationDialog::ShowDialog(
132 content::WebContents
* web_contents
,
134 const std::string
& username
,
135 ui::ProfileSigninConfirmationDelegate
* delegate
) {
136 ProfileSigninConfirmationDialog
* dialog
=
137 new ProfileSigninConfirmationDialog(web_contents
,
141 ui::CheckShouldPromptForNewProfile(
143 // This callback is guaranteed to be invoked, and once it is, the dialog
145 base::Bind(&ProfileSigninConfirmationDialog::Show
,
146 base::Unretained(dialog
)));
149 void ProfileSigninConfirmationDialog::Close() const {
150 closed_by_handler_
= true;
151 dialog_delegate_
->OnDialogCloseFromWebUI();
154 void ProfileSigninConfirmationDialog::Show(bool prompt
) {
155 prompt_for_new_profile_
= prompt
;
157 CreateConstrainedWebDialog(profile_
, this, NULL
, web_contents_
);
160 ui::ModalType
ProfileSigninConfirmationDialog::GetDialogModalType() const {
161 return ui::MODAL_TYPE_WINDOW
;
164 base::string16
ProfileSigninConfirmationDialog::GetDialogTitle() const {
165 return l10n_util::GetStringUTF16(IDS_ENTERPRISE_SIGNIN_TITLE
);
168 GURL
ProfileSigninConfirmationDialog::GetDialogContentURL() const {
169 return GURL(chrome::kChromeUIProfileSigninConfirmationURL
);
172 void ProfileSigninConfirmationDialog::GetWebUIMessageHandlers(
173 std::vector
<content::WebUIMessageHandler
*>* handlers
) const {
175 new ProfileSigninConfirmationHandler(this, signin_delegate_
));
178 void ProfileSigninConfirmationDialog::GetDialogSize(gfx::Size
* size
) const {
179 const int kMinimumDialogWidth
= 480;
181 const int kMinimumDialogHeight
= 180;
183 const int kMinimumDialogHeight
= 210;
185 const int kProfileCreationMessageHeight
= prompt_for_new_profile_
? 50 : 0;
186 size
->SetSize(kMinimumDialogWidth
,
187 kMinimumDialogHeight
+ kProfileCreationMessageHeight
);
190 std::string
ProfileSigninConfirmationDialog::GetDialogArgs() const {
192 base::DictionaryValue dict
;
193 dict
.SetString("username", username_
);
194 dict
.SetBoolean("promptForNewProfile", prompt_for_new_profile_
);
196 dict
.SetBoolean("hideTitle", true);
198 base::JSONWriter::Write(&dict
, &data
);
202 void ProfileSigninConfirmationDialog::OnDialogClosed(
203 const std::string
& json_retval
) {
204 if (!closed_by_handler_
)
205 signin_delegate_
->OnCancelSignin();
208 void ProfileSigninConfirmationDialog::OnCloseContents(
209 content::WebContents
* source
,
210 bool* out_close_dialog
) {
211 if (out_close_dialog
)
212 *out_close_dialog
= true;
215 bool ProfileSigninConfirmationDialog::ShouldShowDialogTitle() const {