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