OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/ui/webui/input_window_dialog_webui.h" | |
6 | |
7 #include <string> | |
8 #include <vector> | |
xiyuan
2011/10/18 16:11:32
you don't need them again since they are included
mazda
2011/10/18 17:32:02
Done.
| |
9 | |
10 #include "base/bind.h" | |
11 #include "base/bind_helpers.h" | |
12 #include "base/json/json_reader.h" | |
13 #include "base/json/json_writer.h" | |
14 #include "base/utf_string_conversions.h" | |
15 #include "base/values.h" | |
16 #include "chrome/browser/ui/browser.h" | |
17 #include "chrome/browser/ui/browser_list.h" | |
18 #include "chrome/browser/ui/browser_dialogs.h" | |
19 #include "chrome/browser/ui/webui/html_dialog_ui.h" | |
20 #include "chrome/common/url_constants.h" | |
21 #include "content/browser/tab_contents/tab_contents.h" | |
22 | |
23 namespace { | |
xiyuan
2011/10/18 16:11:32
nit: insert an empty line after this line and befo
mazda
2011/10/18 17:32:02
Done.
| |
24 const int kInputWindowDialogWidth = 240; | |
25 const int kInputWindowDialogHeight = 120; | |
26 } | |
27 | |
28 //////////////////////////////////////////////////////////////////////////////// | |
29 // InputWindowWebUIDailog private methods | |
30 | |
31 InputWindowDialogWebUI::InputWindowDialogWebUI( | |
32 const string16& window_title, | |
33 const string16& label, | |
34 const string16& contents, | |
35 InputWindowDialog::Delegate* delegate) | |
36 : handler_(new InputWindowDialogHandler(delegate)), | |
37 window_title_(window_title), | |
38 label_(label), | |
39 contents_(contents), | |
40 closed_(true), | |
41 delegate_(delegate) { | |
42 } | |
43 | |
44 InputWindowDialogWebUI::~InputWindowDialogWebUI() { | |
45 } | |
46 | |
47 void InputWindowDialogWebUI::Show() { | |
48 Browser* browser = BrowserList::GetLastActive(); | |
49 DCHECK(browser); | |
50 browser->BrowserShowHtmlDialog(this, NULL); | |
51 closed_ = false; | |
52 } | |
53 | |
54 void InputWindowDialogWebUI::Close() { | |
55 if (!closed_) { | |
56 DCHECK(handler_); | |
57 handler_->CloseDialog(); | |
58 } | |
59 } | |
60 | |
61 bool InputWindowDialogWebUI::IsDialogModal() const { | |
62 return true; | |
63 } | |
64 | |
65 string16 InputWindowDialogWebUI::GetDialogTitle() const { | |
66 return window_title_; | |
67 } | |
68 | |
69 GURL InputWindowDialogWebUI::GetDialogContentURL() const { | |
70 return GURL(chrome::kChromeUIInputWindowDialogURL); | |
71 } | |
72 | |
73 void InputWindowDialogWebUI::GetWebUIMessageHandlers( | |
74 std::vector<WebUIMessageHandler*>* handlers) const { | |
75 handlers->push_back(handler_); | |
76 } | |
77 | |
78 void InputWindowDialogWebUI::GetDialogSize(gfx::Size* size) const { | |
79 size->SetSize(kInputWindowDialogWidth, kInputWindowDialogHeight); | |
80 } | |
81 | |
82 std::string InputWindowDialogWebUI::GetDialogArgs() const { | |
83 DictionaryValue value; | |
84 value.SetString("label", label_); | |
85 value.SetString("contents", contents_); | |
86 std::string json; | |
87 base::JSONWriter::Write(&value, false, &json); | |
88 return json; | |
89 } | |
90 | |
91 void InputWindowDialogWebUI::OnDialogClosed(const std::string& json_retval) { | |
92 scoped_ptr<Value> root(base::JSONReader::Read(json_retval, false)); | |
93 bool response = false; | |
94 ListValue* list = NULL; | |
95 bool accepted = false; | |
96 // If the dialog closes because of a button click then the json is a list | |
97 // containing a bool value. When OK button is clicked, a string in the text | |
98 // field is added to the list. | |
99 if (root.get() && root->GetAsList(&list) && list && | |
100 list->GetBoolean(0, &response) && response) { | |
101 DCHECK_EQ(2U, list->GetSize()); | |
102 string16 text; | |
103 if (list->GetString(1, &text)) { | |
104 delegate_->InputAccepted(text); | |
105 accepted = true; | |
106 } | |
107 } | |
108 if (!accepted) { | |
109 delegate_->InputCanceled(); | |
110 } | |
111 closed_ = true; | |
112 } | |
113 | |
114 void InputWindowDialogWebUI::OnCloseContents(TabContents* source, | |
115 bool* out_close_dialog) { | |
116 } | |
117 | |
118 bool InputWindowDialogWebUI::ShouldShowDialogTitle() const { | |
119 return true; | |
120 } | |
121 | |
122 //////////////////////////////////////////////////////////////////////////////// | |
123 // InputWindowDialogHandler methods | |
124 | |
125 InputWindowDialogHandler::InputWindowDialogHandler( | |
126 InputWindowDialog::Delegate* delegate) | |
127 : delegate_(delegate) { | |
128 } | |
129 | |
130 void InputWindowDialogHandler::CloseDialog() { | |
131 DCHECK(web_ui_); | |
132 static_cast<HtmlDialogUI*>(web_ui_)->CloseDialog(NULL); | |
133 } | |
134 | |
135 void InputWindowDialogHandler::RegisterMessages() { | |
136 web_ui_->RegisterMessageCallback("validate", | |
137 base::Bind(&InputWindowDialogHandler::Validate, | |
138 base::Unretained(this))); | |
139 } | |
140 | |
141 void InputWindowDialogHandler::Validate(const base::ListValue* args) { | |
142 DCHECK_EQ(1U, args->GetSize()); | |
143 bool valid = false; | |
144 string16 text; | |
145 if (args->GetString(0, &text)) { | |
146 valid = delegate_->IsValid(text); | |
147 } | |
148 scoped_ptr<Value> result(Value::CreateBooleanValue(valid)); | |
149 web_ui_->CallJavascriptFunction("inputWindowDialog.ackValidation", | |
150 *result); | |
151 } | |
OLD | NEW |