Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / ui / webui / signin / profile_signin_confirmation_dialog.cc
blob5ad9d877ced5fea7e5576778a31c3328ded5ca62
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_window.h"
14 #include "chrome/browser/ui/webui/constrained_web_dialog_ui.h"
15 #include "chrome/common/url_constants.h"
16 #include "chrome/grit/chromium_strings.h"
17 #include "content/public/browser/web_ui.h"
18 #include "content/public/browser/web_ui_message_handler.h"
19 #include "ui/base/l10n/l10n_util.h"
21 // ProfileSigninConfirmationHandler --------------------------------------------
23 namespace {
25 class ProfileSigninConfirmationHandler : public content::WebUIMessageHandler {
26 public:
27 ProfileSigninConfirmationHandler(
28 const ProfileSigninConfirmationDialog* dialog,
29 ui::ProfileSigninConfirmationDelegate* delegate_);
30 ~ProfileSigninConfirmationHandler() override;
31 void RegisterMessages() override;
33 private:
34 // content::WebUIMessageHandler implementation.
35 void OnCancelButtonClicked(const base::ListValue* args);
36 void OnCreateProfileClicked(const base::ListValue* args);
37 void OnContinueButtonClicked(const base::ListValue* args);
39 // Weak ptr to parent dialog.
40 const ProfileSigninConfirmationDialog* dialog_;
42 // Dialog button handling.
43 ui::ProfileSigninConfirmationDelegate* delegate_;
46 ProfileSigninConfirmationHandler::ProfileSigninConfirmationHandler(
47 const ProfileSigninConfirmationDialog* dialog,
48 ui::ProfileSigninConfirmationDelegate* delegate)
49 : dialog_(dialog), delegate_(delegate) {
52 ProfileSigninConfirmationHandler::~ProfileSigninConfirmationHandler() {
55 void ProfileSigninConfirmationHandler::RegisterMessages() {
56 web_ui()->RegisterMessageCallback(
57 "cancel",
58 base::Bind(&ProfileSigninConfirmationHandler::OnCancelButtonClicked,
59 base::Unretained(this)));
60 web_ui()->RegisterMessageCallback(
61 "createNewProfile",
62 base::Bind(&ProfileSigninConfirmationHandler::OnCreateProfileClicked,
63 base::Unretained(this)));
64 web_ui()->RegisterMessageCallback(
65 "continue",
66 base::Bind(&ProfileSigninConfirmationHandler::OnContinueButtonClicked,
67 base::Unretained(this)));
70 void ProfileSigninConfirmationHandler::OnCancelButtonClicked(
71 const base::ListValue* args) {
72 // TODO(dconnelly): redirect back to NTP?
73 delegate_->OnCancelSignin();
74 dialog_->Close();
77 void ProfileSigninConfirmationHandler::OnCreateProfileClicked(
78 const base::ListValue* args) {
79 delegate_->OnSigninWithNewProfile();
80 dialog_->Close();
83 void ProfileSigninConfirmationHandler::OnContinueButtonClicked(
84 const base::ListValue* args) {
85 delegate_->OnContinueSignin();
86 dialog_->Close();
89 } // namespace
91 // ProfileSigninConfirmationDialog ---------------------------------------------
93 ProfileSigninConfirmationDialog::ProfileSigninConfirmationDialog(
94 content::WebContents* web_contents,
95 Profile* profile,
96 const std::string& username,
97 ui::ProfileSigninConfirmationDelegate* delegate)
98 : web_contents_(web_contents),
99 profile_(profile),
100 username_(username),
101 signin_delegate_(delegate),
102 dialog_delegate_(NULL),
103 prompt_for_new_profile_(true) {
106 ProfileSigninConfirmationDialog::~ProfileSigninConfirmationDialog() {
109 // static
110 void ProfileSigninConfirmationDialog::ShowDialog(
111 content::WebContents* web_contents,
112 Profile* profile,
113 const std::string& username,
114 ui::ProfileSigninConfirmationDelegate* delegate) {
115 ProfileSigninConfirmationDialog* dialog =
116 new ProfileSigninConfirmationDialog(web_contents,
117 profile,
118 username,
119 delegate);
120 ui::CheckShouldPromptForNewProfile(
121 profile,
122 // This callback is guaranteed to be invoked, and once it is, the dialog
123 // owns itself.
124 base::Bind(&ProfileSigninConfirmationDialog::Show,
125 base::Unretained(dialog)));
128 void ProfileSigninConfirmationDialog::Close() const {
129 closed_by_handler_ = true;
130 dialog_delegate_->OnDialogCloseFromWebUI();
133 void ProfileSigninConfirmationDialog::Show(bool prompt) {
134 prompt_for_new_profile_ = prompt;
135 dialog_delegate_ = ShowConstrainedWebDialog(profile_, this, web_contents_);
138 ui::ModalType ProfileSigninConfirmationDialog::GetDialogModalType() const {
139 return ui::MODAL_TYPE_WINDOW;
142 base::string16 ProfileSigninConfirmationDialog::GetDialogTitle() const {
143 return l10n_util::GetStringUTF16(IDS_ENTERPRISE_SIGNIN_TITLE);
146 GURL ProfileSigninConfirmationDialog::GetDialogContentURL() const {
147 return GURL(chrome::kChromeUIProfileSigninConfirmationURL);
150 void ProfileSigninConfirmationDialog::GetWebUIMessageHandlers(
151 std::vector<content::WebUIMessageHandler*>* handlers) const {
152 handlers->push_back(
153 new ProfileSigninConfirmationHandler(this, signin_delegate_));
156 void ProfileSigninConfirmationDialog::GetDialogSize(gfx::Size* size) const {
157 const int kMinimumDialogWidth = 480;
158 #if defined(OS_WIN)
159 const int kMinimumDialogHeight = 180;
160 #else
161 const int kMinimumDialogHeight = 210;
162 #endif
163 const int kProfileCreationMessageHeight = prompt_for_new_profile_ ? 50 : 0;
164 size->SetSize(kMinimumDialogWidth,
165 kMinimumDialogHeight + kProfileCreationMessageHeight);
168 std::string ProfileSigninConfirmationDialog::GetDialogArgs() const {
169 std::string data;
170 base::DictionaryValue dict;
171 dict.SetString("username", username_);
172 dict.SetBoolean("promptForNewProfile", prompt_for_new_profile_);
173 #if defined(OS_WIN)
174 dict.SetBoolean("hideTitle", true);
175 #endif
176 base::JSONWriter::Write(dict, &data);
177 return data;
180 void ProfileSigninConfirmationDialog::OnDialogClosed(
181 const std::string& json_retval) {
182 if (!closed_by_handler_)
183 signin_delegate_->OnCancelSignin();
186 void ProfileSigninConfirmationDialog::OnCloseContents(
187 content::WebContents* source,
188 bool* out_close_dialog) {
189 *out_close_dialog = true;
192 bool ProfileSigninConfirmationDialog::ShouldShowDialogTitle() const {
193 return true;