| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/input_window_dialog_webui.h" | 5 #include "chrome/browser/ui/webui/input_window_dialog_webui.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
| 10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/browser_list.h" | 14 #include "chrome/browser/ui/browser_list.h" |
| 15 #include "chrome/browser/ui/browser_dialogs.h" | 15 #include "chrome/browser/ui/browser_dialogs.h" |
| 16 #include "chrome/browser/ui/webui/html_dialog_ui.h" | 16 #include "chrome/browser/ui/webui/html_dialog_ui.h" |
| 17 #include "chrome/common/url_constants.h" | 17 #include "chrome/common/url_constants.h" |
| 18 #include "content/browser/tab_contents/tab_contents.h" | 18 #include "content/browser/tab_contents/tab_contents.h" |
| 19 #include "grit/generated_resources.h" | 19 #include "grit/generated_resources.h" |
| 20 #include "ui/base/l10n/l10n_util.h" | 20 #include "ui/base/l10n/l10n_util.h" |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 const int kInputWindowDialogWidth = 240; | 24 const int kInputWindowDialogWidth = 300; |
| 25 const int kInputWindowDialogHeight = 120; | 25 const int kInputWindowDialogBaseHeight = 90; |
| 26 const int kInputWindowDialogContentsHeight = 20; |
| 26 | 27 |
| 27 } // namespace | 28 } // namespace |
| 28 | 29 |
| 29 //////////////////////////////////////////////////////////////////////////////// | 30 //////////////////////////////////////////////////////////////////////////////// |
| 30 // InputWindowWebUIDailog private methods | 31 // InputWindowWebUIDailog private methods |
| 31 | 32 |
| 32 InputWindowDialogWebUI::InputWindowDialogWebUI(const string16& window_title, | 33 InputWindowDialogWebUI::InputWindowDialogWebUI( |
| 33 const string16& label, | 34 const string16& window_title, |
| 34 const string16& contents, | 35 const LabelContentsPairs& label_contents_pairs, |
| 35 Delegate* delegate, | 36 InputWindowDialog::Delegate* delegate, |
| 36 ButtonType type) | 37 ButtonType type) |
| 37 : handler_(new InputWindowDialogHandler(delegate)), | 38 : handler_(new InputWindowDialogHandler(delegate)), |
| 38 window_title_(window_title), | 39 window_title_(window_title), |
| 39 label_(label), | 40 label_contents_pairs_(label_contents_pairs), |
| 40 contents_(contents), | |
| 41 closed_(true), | 41 closed_(true), |
| 42 delegate_(delegate), | 42 delegate_(delegate), |
| 43 type_(type) { | 43 type_(type) { |
| 44 } | 44 } |
| 45 | 45 |
| 46 InputWindowDialogWebUI::~InputWindowDialogWebUI() { | 46 InputWindowDialogWebUI::~InputWindowDialogWebUI() { |
| 47 } | 47 } |
| 48 | 48 |
| 49 void InputWindowDialogWebUI::Show() { | 49 void InputWindowDialogWebUI::Show() { |
| 50 Browser* browser = BrowserList::GetLastActive(); | 50 Browser* browser = BrowserList::GetLastActive(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 71 GURL InputWindowDialogWebUI::GetDialogContentURL() const { | 71 GURL InputWindowDialogWebUI::GetDialogContentURL() const { |
| 72 return GURL(chrome::kChromeUIInputWindowDialogURL); | 72 return GURL(chrome::kChromeUIInputWindowDialogURL); |
| 73 } | 73 } |
| 74 | 74 |
| 75 void InputWindowDialogWebUI::GetWebUIMessageHandlers( | 75 void InputWindowDialogWebUI::GetWebUIMessageHandlers( |
| 76 std::vector<WebUIMessageHandler*>* handlers) const { | 76 std::vector<WebUIMessageHandler*>* handlers) const { |
| 77 handlers->push_back(handler_); | 77 handlers->push_back(handler_); |
| 78 } | 78 } |
| 79 | 79 |
| 80 void InputWindowDialogWebUI::GetDialogSize(gfx::Size* size) const { | 80 void InputWindowDialogWebUI::GetDialogSize(gfx::Size* size) const { |
| 81 size->SetSize(kInputWindowDialogWidth, kInputWindowDialogHeight); | 81 const int height = kInputWindowDialogBaseHeight + |
| 82 kInputWindowDialogContentsHeight * label_contents_pairs_.size(); |
| 83 size->SetSize(kInputWindowDialogWidth, height); |
| 82 } | 84 } |
| 83 | 85 |
| 84 std::string InputWindowDialogWebUI::GetDialogArgs() const { | 86 std::string InputWindowDialogWebUI::GetDialogArgs() const { |
| 85 DictionaryValue value; | 87 DictionaryValue value; |
| 86 value.SetString("label", label_); | 88 value.SetString("nameLabel", label_contents_pairs_[0].first); |
| 87 value.SetString("contents", contents_); | 89 value.SetString("name", label_contents_pairs_[0].second); |
| 90 if (label_contents_pairs_.size() == 2) { |
| 91 value.SetString("urlLabel", label_contents_pairs_[1].first); |
| 92 value.SetString("url", label_contents_pairs_[1].second); |
| 93 } |
| 88 string16 ok_button_title = l10n_util::GetStringUTF16( | 94 string16 ok_button_title = l10n_util::GetStringUTF16( |
| 89 type_ == BUTTON_TYPE_ADD ? IDS_ADD : IDS_SAVE); | 95 type_ == BUTTON_TYPE_ADD ? IDS_ADD : IDS_SAVE); |
| 90 value.SetString("ok_button_title", ok_button_title); | 96 value.SetString("ok_button_title", ok_button_title); |
| 91 std::string json; | 97 std::string json; |
| 92 base::JSONWriter::Write(&value, false, &json); | 98 base::JSONWriter::Write(&value, false, &json); |
| 93 return json; | 99 return json; |
| 94 } | 100 } |
| 95 | 101 |
| 96 void InputWindowDialogWebUI::OnDialogClosed(const std::string& json_retval) { | 102 void InputWindowDialogWebUI::OnDialogClosed(const std::string& json_retval) { |
| 97 scoped_ptr<Value> root(base::JSONReader::Read(json_retval, false)); | 103 scoped_ptr<Value> root(base::JSONReader::Read(json_retval, false)); |
| 98 bool response = false; | 104 bool response = false; |
| 99 ListValue* list = NULL; | 105 ListValue* list = NULL; |
| 100 bool accepted = false; | 106 bool accepted = false; |
| 101 // If the dialog closes because of a button click then the json is a list | 107 // If the dialog closes because of a button click then the json is a list |
| 102 // containing a bool value. When OK button is clicked, a string in the text | 108 // containing a bool value. When OK button is clicked, a string in the text |
| 103 // field is added to the list. | 109 // field is added to the list. |
| 104 if (root.get() && root->GetAsList(&list) && list && | 110 if (root.get() && root->GetAsList(&list) && list && |
| 105 list->GetBoolean(0, &response) && response) { | 111 list->GetBoolean(0, &response) && response) { |
| 106 DCHECK_EQ(2U, list->GetSize()); | 112 DCHECK(list->GetSize() == 2 || list->GetSize() == 3); |
| 107 string16 text; | 113 InputTexts texts; |
| 108 if (list->GetString(1, &text)) { | 114 string16 name; |
| 109 delegate_->InputAccepted(text); | 115 if (list->GetString(1, &name)) { |
| 116 texts.push_back(name); |
| 117 } |
| 118 string16 url; |
| 119 if (list->GetSize() == 3 && list->GetString(2, &url)) { |
| 120 texts.push_back(url); |
| 121 } |
| 122 if (delegate_->IsValid(texts)) { |
| 123 delegate_->InputAccepted(texts); |
| 110 accepted = true; | 124 accepted = true; |
| 111 } | 125 } |
| 112 } | 126 } |
| 113 if (!accepted) { | 127 if (!accepted) { |
| 114 delegate_->InputCanceled(); | 128 delegate_->InputCanceled(); |
| 115 } | 129 } |
| 116 closed_ = true; | 130 closed_ = true; |
| 117 } | 131 } |
| 118 | 132 |
| 119 void InputWindowDialogWebUI::OnCloseContents(TabContents* source, | 133 void InputWindowDialogWebUI::OnCloseContents(TabContents* source, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 137 static_cast<HtmlDialogUI*>(web_ui_)->CloseDialog(NULL); | 151 static_cast<HtmlDialogUI*>(web_ui_)->CloseDialog(NULL); |
| 138 } | 152 } |
| 139 | 153 |
| 140 void InputWindowDialogHandler::RegisterMessages() { | 154 void InputWindowDialogHandler::RegisterMessages() { |
| 141 web_ui_->RegisterMessageCallback("validate", | 155 web_ui_->RegisterMessageCallback("validate", |
| 142 base::Bind(&InputWindowDialogHandler::Validate, | 156 base::Bind(&InputWindowDialogHandler::Validate, |
| 143 base::Unretained(this))); | 157 base::Unretained(this))); |
| 144 } | 158 } |
| 145 | 159 |
| 146 void InputWindowDialogHandler::Validate(const base::ListValue* args) { | 160 void InputWindowDialogHandler::Validate(const base::ListValue* args) { |
| 147 DCHECK_EQ(1U, args->GetSize()); | 161 DCHECK(args->GetSize() == 1U || args->GetSize() == 2U); |
| 148 bool valid = false; | 162 InputWindowDialog::InputTexts texts; |
| 149 string16 text; | 163 string16 name; |
| 150 if (args->GetString(0, &text)) { | 164 if (args->GetString(0, &name)) { |
| 151 valid = delegate_->IsValid(text); | 165 texts.push_back(name); |
| 152 } | 166 } |
| 167 string16 url; |
| 168 if (args->GetSize() == 2 && args->GetString(0, &url)) { |
| 169 texts.push_back(url); |
| 170 } |
| 171 const bool valid = delegate_->IsValid(texts); |
| 153 scoped_ptr<Value> result(Value::CreateBooleanValue(valid)); | 172 scoped_ptr<Value> result(Value::CreateBooleanValue(valid)); |
| 154 web_ui_->CallJavascriptFunction("inputWindowDialog.ackValidation", | 173 web_ui_->CallJavascriptFunction("inputWindowDialog.ackValidation", |
| 155 *result); | 174 *result); |
| 156 } | 175 } |
| OLD | NEW |