Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(300)

Side by Side Diff: chrome/browser/ui/webui/input_window_dialog_webui.cc

Issue 8438037: Change 'Add Page' to show a simple input dialog with --use-more-webui. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 19
20 namespace { 20 namespace {
21 21
22 const int kInputWindowDialogWidth = 240; 22 const int kInputWindowDialogWidth = 300;
23 const int kInputWindowDialogHeight = 120; 23 const int kInputWindowDialogBaseHeight = 90;
24 const int kInputWindowDialogContentsHeight = 20;
24 25
25 } 26 }
26 27
27 //////////////////////////////////////////////////////////////////////////////// 28 ////////////////////////////////////////////////////////////////////////////////
28 // InputWindowWebUIDailog private methods 29 // InputWindowWebUIDailog private methods
29 30
30 InputWindowDialogWebUI::InputWindowDialogWebUI( 31 InputWindowDialogWebUI::InputWindowDialogWebUI(
31 const string16& window_title, 32 const string16& window_title,
32 const string16& label, 33 const LabelContentsPairs& label_contents_pairs,
33 const string16& contents,
34 InputWindowDialog::Delegate* delegate) 34 InputWindowDialog::Delegate* delegate)
35 : handler_(new InputWindowDialogHandler(delegate)), 35 : handler_(new InputWindowDialogHandler(delegate)),
36 window_title_(window_title), 36 window_title_(window_title),
37 label_(label), 37 label_contents_pairs_(label_contents_pairs),
38 contents_(contents),
39 closed_(true), 38 closed_(true),
40 delegate_(delegate) { 39 delegate_(delegate) {
41 } 40 }
42 41
43 InputWindowDialogWebUI::~InputWindowDialogWebUI() { 42 InputWindowDialogWebUI::~InputWindowDialogWebUI() {
44 } 43 }
45 44
46 void InputWindowDialogWebUI::Show() { 45 void InputWindowDialogWebUI::Show() {
47 Browser* browser = BrowserList::GetLastActive(); 46 Browser* browser = BrowserList::GetLastActive();
48 DCHECK(browser); 47 DCHECK(browser);
(...skipping 19 matching lines...) Expand all
68 GURL InputWindowDialogWebUI::GetDialogContentURL() const { 67 GURL InputWindowDialogWebUI::GetDialogContentURL() const {
69 return GURL(chrome::kChromeUIInputWindowDialogURL); 68 return GURL(chrome::kChromeUIInputWindowDialogURL);
70 } 69 }
71 70
72 void InputWindowDialogWebUI::GetWebUIMessageHandlers( 71 void InputWindowDialogWebUI::GetWebUIMessageHandlers(
73 std::vector<WebUIMessageHandler*>* handlers) const { 72 std::vector<WebUIMessageHandler*>* handlers) const {
74 handlers->push_back(handler_); 73 handlers->push_back(handler_);
75 } 74 }
76 75
77 void InputWindowDialogWebUI::GetDialogSize(gfx::Size* size) const { 76 void InputWindowDialogWebUI::GetDialogSize(gfx::Size* size) const {
78 size->SetSize(kInputWindowDialogWidth, kInputWindowDialogHeight); 77 const int height = kInputWindowDialogBaseHeight +
78 kInputWindowDialogContentsHeight * label_contents_pairs_.size();
79 size->SetSize(kInputWindowDialogWidth, height);
79 } 80 }
80 81
81 std::string InputWindowDialogWebUI::GetDialogArgs() const { 82 std::string InputWindowDialogWebUI::GetDialogArgs() const {
82 DictionaryValue value; 83 DictionaryValue value;
83 value.SetString("label", label_); 84 value.SetString("nameLabel", label_contents_pairs_[0].first);
84 value.SetString("contents", contents_); 85 value.SetString("name", label_contents_pairs_[0].second);
86 if (label_contents_pairs_.size() == 2) {
87 value.SetString("urlLabel", label_contents_pairs_[1].first);
88 value.SetString("url", label_contents_pairs_[1].second);
89 }
85 std::string json; 90 std::string json;
86 base::JSONWriter::Write(&value, false, &json); 91 base::JSONWriter::Write(&value, false, &json);
87 return json; 92 return json;
88 } 93 }
89 94
90 void InputWindowDialogWebUI::OnDialogClosed(const std::string& json_retval) { 95 void InputWindowDialogWebUI::OnDialogClosed(const std::string& json_retval) {
91 scoped_ptr<Value> root(base::JSONReader::Read(json_retval, false)); 96 scoped_ptr<Value> root(base::JSONReader::Read(json_retval, false));
92 bool response = false; 97 bool response = false;
93 ListValue* list = NULL; 98 ListValue* list = NULL;
94 bool accepted = false; 99 bool accepted = false;
95 // If the dialog closes because of a button click then the json is a list 100 // If the dialog closes because of a button click then the json is a list
96 // containing a bool value. When OK button is clicked, a string in the text 101 // containing a bool value. When OK button is clicked, a string in the text
97 // field is added to the list. 102 // field is added to the list.
98 if (root.get() && root->GetAsList(&list) && list && 103 if (root.get() && root->GetAsList(&list) && list &&
99 list->GetBoolean(0, &response) && response) { 104 list->GetBoolean(0, &response) && response) {
100 DCHECK_EQ(2U, list->GetSize()); 105 DCHECK(list->GetSize() == 2 || list->GetSize() == 3);
101 string16 text; 106 InputTexts texts;
102 if (list->GetString(1, &text)) { 107 string16 name;
103 delegate_->InputAccepted(text); 108 if (list->GetString(1, &name)) {
109 texts.push_back(name);
110 }
111 string16 url;
112 if (list->GetSize() == 3 && list->GetString(2, &url)) {
113 texts.push_back(url);
114 }
115 if (delegate_->IsValid(texts)) {
116 delegate_->InputAccepted(texts);
104 accepted = true; 117 accepted = true;
105 } 118 }
106 } 119 }
107 if (!accepted) { 120 if (!accepted) {
108 delegate_->InputCanceled(); 121 delegate_->InputCanceled();
109 } 122 }
110 closed_ = true; 123 closed_ = true;
111 } 124 }
112 125
113 void InputWindowDialogWebUI::OnCloseContents(TabContents* source, 126 void InputWindowDialogWebUI::OnCloseContents(TabContents* source,
(...skipping 17 matching lines...) Expand all
131 static_cast<HtmlDialogUI*>(web_ui_)->CloseDialog(NULL); 144 static_cast<HtmlDialogUI*>(web_ui_)->CloseDialog(NULL);
132 } 145 }
133 146
134 void InputWindowDialogHandler::RegisterMessages() { 147 void InputWindowDialogHandler::RegisterMessages() {
135 web_ui_->RegisterMessageCallback("validate", 148 web_ui_->RegisterMessageCallback("validate",
136 base::Bind(&InputWindowDialogHandler::Validate, 149 base::Bind(&InputWindowDialogHandler::Validate,
137 base::Unretained(this))); 150 base::Unretained(this)));
138 } 151 }
139 152
140 void InputWindowDialogHandler::Validate(const base::ListValue* args) { 153 void InputWindowDialogHandler::Validate(const base::ListValue* args) {
141 DCHECK_EQ(1U, args->GetSize()); 154 DCHECK(args->GetSize() == 1U || args->GetSize() == 2U);
142 bool valid = false; 155 InputWindowDialog::InputTexts texts;
143 string16 text; 156 string16 name;
144 if (args->GetString(0, &text)) { 157 if (args->GetString(0, &name)) {
145 valid = delegate_->IsValid(text); 158 texts.push_back(name);
146 } 159 }
160 string16 url;
161 if (args->GetSize() == 2 && args->GetString(0, &url)) {
162 texts.push_back(url);
163 }
164 const bool valid = delegate_->IsValid(texts);
147 scoped_ptr<Value> result(Value::CreateBooleanValue(valid)); 165 scoped_ptr<Value> result(Value::CreateBooleanValue(valid));
148 web_ui_->CallJavascriptFunction("inputWindowDialog.ackValidation", 166 web_ui_->CallJavascriptFunction("inputWindowDialog.ackValidation",
149 *result); 167 *result);
150 } 168 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698