cc787efddfd152991a52b482ceaeecb6dcbf8ce4
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / signin / profile_signin_confirmation_dialog.cc
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.
4
5 #include "chrome/browser/ui/webui/signin/profile_signin_confirmation_dialog.h"
6
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"
23
24 // ProfileSigninConfirmationHandler --------------------------------------------
25
26 namespace {
27
28 class ProfileSigninConfirmationHandler : public content::WebUIMessageHandler {
29  public:
30   ProfileSigninConfirmationHandler(
31       const ProfileSigninConfirmationDialog* dialog,
32       ui::ProfileSigninConfirmationDelegate* delegate_);
33   virtual ~ProfileSigninConfirmationHandler();
34   virtual void RegisterMessages() OVERRIDE;
35
36  private:
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);
41
42   // Weak ptr to parent dialog.
43   const ProfileSigninConfirmationDialog* dialog_;
44
45   // Dialog button handling.
46   ui::ProfileSigninConfirmationDelegate* delegate_;
47 };
48
49 ProfileSigninConfirmationHandler::ProfileSigninConfirmationHandler(
50     const ProfileSigninConfirmationDialog* dialog,
51     ui::ProfileSigninConfirmationDelegate* delegate)
52   : dialog_(dialog), delegate_(delegate) {
53 }
54
55 ProfileSigninConfirmationHandler::~ProfileSigninConfirmationHandler() {
56 }
57
58 void ProfileSigninConfirmationHandler::RegisterMessages() {
59   web_ui()->RegisterMessageCallback(
60       "cancel",
61       base::Bind(&ProfileSigninConfirmationHandler::OnCancelButtonClicked,
62                  base::Unretained(this)));
63   web_ui()->RegisterMessageCallback(
64       "createNewProfile",
65       base::Bind(&ProfileSigninConfirmationHandler::OnCreateProfileClicked,
66                  base::Unretained(this)));
67   web_ui()->RegisterMessageCallback(
68       "continue",
69       base::Bind(&ProfileSigninConfirmationHandler::OnContinueButtonClicked,
70                  base::Unretained(this)));
71 }
72
73 void ProfileSigninConfirmationHandler::OnCancelButtonClicked(
74     const base::ListValue* args) {
75   // TODO(dconnelly): redirect back to NTP?
76   delegate_->OnCancelSignin();
77   dialog_->Close();
78 }
79
80 void ProfileSigninConfirmationHandler::OnCreateProfileClicked(
81     const base::ListValue* args) {
82   delegate_->OnSigninWithNewProfile();
83   dialog_->Close();
84 }
85
86 void ProfileSigninConfirmationHandler::OnContinueButtonClicked(
87     const base::ListValue* args) {
88   delegate_->OnContinueSignin();
89   dialog_->Close();
90 }
91
92 }  // namespace
93
94 #if !defined(TOOLKIT_VIEWS) && !defined(OS_MACOSX)
95 namespace chrome {
96 // static
97 // Declared in browser_dialogs.h
98 void ShowProfileSigninConfirmationDialog(
99     Browser* browser,
100     content::WebContents* web_contents,
101     Profile* profile,
102     const std::string& username,
103     ui::ProfileSigninConfirmationDelegate* delegate) {
104   ProfileSigninConfirmationDialog::ShowDialog(web_contents,
105                                               profile,
106                                               username,
107                                               delegate);
108 }
109 }  // namespace chrome
110 #endif
111
112 // ProfileSigninConfirmationDialog ---------------------------------------------
113
114 ProfileSigninConfirmationDialog::ProfileSigninConfirmationDialog(
115     content::WebContents* web_contents,
116     Profile* profile,
117     const std::string& username,
118     ui::ProfileSigninConfirmationDelegate* delegate)
119   : web_contents_(web_contents),
120     profile_(profile),
121     username_(username),
122     signin_delegate_(delegate),
123     dialog_delegate_(NULL),
124     prompt_for_new_profile_(true) {
125 }
126
127 ProfileSigninConfirmationDialog::~ProfileSigninConfirmationDialog() {
128 }
129
130 // static
131 void ProfileSigninConfirmationDialog::ShowDialog(
132   content::WebContents* web_contents,
133   Profile* profile,
134   const std::string& username,
135   ui::ProfileSigninConfirmationDelegate* delegate) {
136   ProfileSigninConfirmationDialog* dialog =
137       new ProfileSigninConfirmationDialog(web_contents,
138                                           profile,
139                                           username,
140                                           delegate);
141   ui::CheckShouldPromptForNewProfile(
142       profile,
143       // This callback is guaranteed to be invoked, and once it is, the dialog
144       // owns itself.
145       base::Bind(&ProfileSigninConfirmationDialog::Show,
146                  base::Unretained(dialog)));
147 }
148
149 void ProfileSigninConfirmationDialog::Close() const {
150   closed_by_handler_ = true;
151   dialog_delegate_->OnDialogCloseFromWebUI();
152 }
153
154 void ProfileSigninConfirmationDialog::Show(bool prompt) {
155   prompt_for_new_profile_ = prompt;
156   dialog_delegate_ =
157       CreateConstrainedWebDialog(profile_, this, NULL, web_contents_);
158 }
159
160 ui::ModalType ProfileSigninConfirmationDialog::GetDialogModalType() const {
161   return ui::MODAL_TYPE_WINDOW;
162 }
163
164 base::string16 ProfileSigninConfirmationDialog::GetDialogTitle() const {
165   return l10n_util::GetStringUTF16(IDS_ENTERPRISE_SIGNIN_TITLE);
166 }
167
168 GURL ProfileSigninConfirmationDialog::GetDialogContentURL() const {
169   return GURL(chrome::kChromeUIProfileSigninConfirmationURL);
170 }
171
172 void ProfileSigninConfirmationDialog::GetWebUIMessageHandlers(
173     std::vector<content::WebUIMessageHandler*>* handlers) const {
174   handlers->push_back(
175       new ProfileSigninConfirmationHandler(this, signin_delegate_));
176 }
177
178 void ProfileSigninConfirmationDialog::GetDialogSize(gfx::Size* size) const {
179   const int kMinimumDialogWidth = 480;
180 #if defined(OS_WIN)
181   const int kMinimumDialogHeight = 180;
182 #else
183   const int kMinimumDialogHeight = 210;
184 #endif
185   const int kProfileCreationMessageHeight = prompt_for_new_profile_ ? 50 : 0;
186   size->SetSize(kMinimumDialogWidth,
187                 kMinimumDialogHeight + kProfileCreationMessageHeight);
188 }
189
190 std::string ProfileSigninConfirmationDialog::GetDialogArgs() const {
191   std::string data;
192   base::DictionaryValue dict;
193   dict.SetString("username", username_);
194   dict.SetBoolean("promptForNewProfile", prompt_for_new_profile_);
195 #if defined(OS_WIN)
196   dict.SetBoolean("hideTitle", true);
197 #endif
198   base::JSONWriter::Write(&dict, &data);
199   return data;
200 }
201
202 void ProfileSigninConfirmationDialog::OnDialogClosed(
203     const std::string& json_retval) {
204   if (!closed_by_handler_)
205     signin_delegate_->OnCancelSignin();
206 }
207
208 void ProfileSigninConfirmationDialog::OnCloseContents(
209     content::WebContents* source,
210     bool* out_close_dialog) {
211   if (out_close_dialog)
212     *out_close_dialog = true;
213 }
214
215 bool ProfileSigninConfirmationDialog::ShouldShowDialogTitle() const {
216   return true;
217 }