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 "chrome/grit/chromium_strings.h"
18 #include "content/public/browser/web_ui.h"
19 #include "content/public/browser/web_ui_message_handler.h"
20 #include "ui/base/l10n/l10n_util.h"
22 // ProfileSigninConfirmationHandler --------------------------------------------
26 class ProfileSigninConfirmationHandler : public content::WebUIMessageHandler {
28 ProfileSigninConfirmationHandler(
29 const ProfileSigninConfirmationDialog* dialog,
30 ui::ProfileSigninConfirmationDelegate* delegate_);
31 virtual ~ProfileSigninConfirmationHandler();
32 virtual void RegisterMessages() OVERRIDE;
35 // content::WebUIMessageHandler implementation.
36 void OnCancelButtonClicked(const base::ListValue* args);
37 void OnCreateProfileClicked(const base::ListValue* args);
38 void OnContinueButtonClicked(const base::ListValue* args);
40 // Weak ptr to parent dialog.
41 const ProfileSigninConfirmationDialog* dialog_;
43 // Dialog button handling.
44 ui::ProfileSigninConfirmationDelegate* delegate_;
47 ProfileSigninConfirmationHandler::ProfileSigninConfirmationHandler(
48 const ProfileSigninConfirmationDialog* dialog,
49 ui::ProfileSigninConfirmationDelegate* delegate)
50 : dialog_(dialog), delegate_(delegate) {
53 ProfileSigninConfirmationHandler::~ProfileSigninConfirmationHandler() {
56 void ProfileSigninConfirmationHandler::RegisterMessages() {
57 web_ui()->RegisterMessageCallback(
59 base::Bind(&ProfileSigninConfirmationHandler::OnCancelButtonClicked,
60 base::Unretained(this)));
61 web_ui()->RegisterMessageCallback(
63 base::Bind(&ProfileSigninConfirmationHandler::OnCreateProfileClicked,
64 base::Unretained(this)));
65 web_ui()->RegisterMessageCallback(
67 base::Bind(&ProfileSigninConfirmationHandler::OnContinueButtonClicked,
68 base::Unretained(this)));
71 void ProfileSigninConfirmationHandler::OnCancelButtonClicked(
72 const base::ListValue* args) {
73 // TODO(dconnelly): redirect back to NTP?
74 delegate_->OnCancelSignin();
78 void ProfileSigninConfirmationHandler::OnCreateProfileClicked(
79 const base::ListValue* args) {
80 delegate_->OnSigninWithNewProfile();
84 void ProfileSigninConfirmationHandler::OnContinueButtonClicked(
85 const base::ListValue* args) {
86 delegate_->OnContinueSignin();
92 #if !defined(TOOLKIT_VIEWS) && !defined(OS_MACOSX)
95 // Declared in browser_dialogs.h
96 void ShowProfileSigninConfirmationDialog(
98 content::WebContents* web_contents,
100 const std::string& username,
101 ui::ProfileSigninConfirmationDelegate* delegate) {
102 ProfileSigninConfirmationDialog::ShowDialog(web_contents,
107 } // namespace chrome
110 // ProfileSigninConfirmationDialog ---------------------------------------------
112 ProfileSigninConfirmationDialog::ProfileSigninConfirmationDialog(
113 content::WebContents* web_contents,
115 const std::string& username,
116 ui::ProfileSigninConfirmationDelegate* delegate)
117 : web_contents_(web_contents),
120 signin_delegate_(delegate),
121 dialog_delegate_(NULL),
122 prompt_for_new_profile_(true) {
125 ProfileSigninConfirmationDialog::~ProfileSigninConfirmationDialog() {
129 void ProfileSigninConfirmationDialog::ShowDialog(
130 content::WebContents* web_contents,
132 const std::string& username,
133 ui::ProfileSigninConfirmationDelegate* delegate) {
134 ProfileSigninConfirmationDialog* dialog =
135 new ProfileSigninConfirmationDialog(web_contents,
139 ui::CheckShouldPromptForNewProfile(
141 // This callback is guaranteed to be invoked, and once it is, the dialog
143 base::Bind(&ProfileSigninConfirmationDialog::Show,
144 base::Unretained(dialog)));
147 void ProfileSigninConfirmationDialog::Close() const {
148 closed_by_handler_ = true;
149 dialog_delegate_->OnDialogCloseFromWebUI();
152 void ProfileSigninConfirmationDialog::Show(bool prompt) {
153 prompt_for_new_profile_ = prompt;
154 dialog_delegate_ = CreateConstrainedWebDialog(profile_, this, web_contents_);
157 ui::ModalType ProfileSigninConfirmationDialog::GetDialogModalType() const {
158 return ui::MODAL_TYPE_WINDOW;
161 base::string16 ProfileSigninConfirmationDialog::GetDialogTitle() const {
162 return l10n_util::GetStringUTF16(IDS_ENTERPRISE_SIGNIN_TITLE);
165 GURL ProfileSigninConfirmationDialog::GetDialogContentURL() const {
166 return GURL(chrome::kChromeUIProfileSigninConfirmationURL);
169 void ProfileSigninConfirmationDialog::GetWebUIMessageHandlers(
170 std::vector<content::WebUIMessageHandler*>* handlers) const {
172 new ProfileSigninConfirmationHandler(this, signin_delegate_));
175 void ProfileSigninConfirmationDialog::GetDialogSize(gfx::Size* size) const {
176 const int kMinimumDialogWidth = 480;
178 const int kMinimumDialogHeight = 180;
180 const int kMinimumDialogHeight = 210;
182 const int kProfileCreationMessageHeight = prompt_for_new_profile_ ? 50 : 0;
183 size->SetSize(kMinimumDialogWidth,
184 kMinimumDialogHeight + kProfileCreationMessageHeight);
187 std::string ProfileSigninConfirmationDialog::GetDialogArgs() const {
189 base::DictionaryValue dict;
190 dict.SetString("username", username_);
191 dict.SetBoolean("promptForNewProfile", prompt_for_new_profile_);
193 dict.SetBoolean("hideTitle", true);
195 base::JSONWriter::Write(&dict, &data);
199 void ProfileSigninConfirmationDialog::OnDialogClosed(
200 const std::string& json_retval) {
201 if (!closed_by_handler_)
202 signin_delegate_->OnCancelSignin();
205 void ProfileSigninConfirmationDialog::OnCloseContents(
206 content::WebContents* source,
207 bool* out_close_dialog) {
208 if (out_close_dialog)
209 *out_close_dialog = true;
212 bool ProfileSigninConfirmationDialog::ShouldShowDialogTitle() const {