Chromium Code Reviews| 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/chromeos/login/network_dropdown.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/base64.h" | |
| 10 #include "base/utf_string_conversions.h" | |
| 11 #include "content/browser/webui/web_ui.h" | |
| 12 #include "chrome/browser/chromeos/login/proxy_settings_dialog.h" | |
| 13 #include "chrome/browser/chromeos/cros/cros_library.h" | |
| 14 #include "chrome/browser/ui/webui/web_ui_util.h" | |
| 15 #include "views/controls/menu/menu_delegate.h" | |
| 16 #include "views/controls/menu/menu_item_view.h" | |
| 17 #include "ui/gfx/font.h" | |
| 18 | |
| 19 namespace chromeos { | |
| 20 | |
| 21 // WebUI specific implementation of the views::MenuItemInterface class. | |
| 22 class WebUIMenuItem : public views::MenuItemInterface { | |
| 23 public: | |
| 24 WebUIMenuItem(views::MenuDelegate* delegate, WebUI* web_ui); | |
| 25 | |
| 26 virtual ~WebUIMenuItem(); | |
| 27 | |
| 28 base::ListValue* CreateValue(); | |
| 29 | |
| 30 // views::MenuItemInterface implementation: | |
| 31 virtual MenuItemInterface* AppendSubMenu(int item_id, | |
| 32 const std::wstring& label); | |
| 33 virtual MenuItemInterface* AppendSubMenuWithIcon(int item_id, | |
| 34 const std::wstring& label, | |
| 35 const SkBitmap& icon); | |
| 36 virtual MenuItemInterface* AppendMenuItemWithLabel(int item_id, | |
| 37 const std::wstring& label); | |
| 38 virtual MenuItemInterface* AppendMenuItemWithIcon(int item_id, | |
| 39 const std::wstring& label, | |
| 40 const SkBitmap& icon); | |
| 41 virtual void AppendSeparator(); | |
| 42 virtual void ClearSubmenu(); | |
| 43 virtual void ChildrenChanged(); | |
| 44 virtual void Cancel(); | |
| 45 virtual void set_margins(int top_margin, int bottom_margin); | |
| 46 | |
| 47 private: | |
| 48 WebUIMenuItem(const std::wstring& label, | |
| 49 int id, | |
| 50 const std::string& icon, | |
| 51 WebUIMenuItem* previous, | |
| 52 views::MenuDelegate *delegate); | |
| 53 | |
| 54 views::MenuDelegate* delegate_; | |
| 55 | |
| 56 // Item content. Icon is base64 encoded data URL. | |
| 57 int id_; | |
| 58 std::string icon_; | |
| 59 std::string label_; | |
| 60 | |
| 61 // Points to the previous item in the same menu level or NULL. | |
| 62 WebUIMenuItem* previous_; | |
| 63 // Points to the last item in the sub-menu or NULL. | |
| 64 WebUIMenuItem* last_child_; | |
| 65 | |
| 66 WebUI* web_ui_; | |
| 67 }; | |
| 68 | |
| 69 // WebUI specific implementation of the NetworkMenu class. | |
| 70 class NetworkMenuWebUI : public NetworkMenu { | |
| 71 public: | |
| 72 NetworkMenuWebUI(NetworkMenu::Delegate* delegate, WebUI* web_ui) | |
| 73 : NetworkMenu(delegate, false, NULL) { | |
| 74 menu_item_.reset(new WebUIMenuItem(GetMenuDelegate(), web_ui)); | |
| 75 } | |
| 76 | |
| 77 void OnItemChosen(int id) { | |
| 78 GetMenuDelegate()->ExecuteCommand(id); | |
| 79 } | |
| 80 }; | |
| 81 | |
| 82 // WebUIMenuItem --------------------------------------------------------------- | |
| 83 | |
| 84 WebUIMenuItem::WebUIMenuItem(views::MenuDelegate* delegate, WebUI* web_ui) | |
| 85 : delegate_(delegate), | |
| 86 id_(-1), | |
| 87 previous_(NULL), | |
| 88 last_child_(NULL), | |
| 89 web_ui_(web_ui) { | |
| 90 } | |
| 91 | |
| 92 WebUIMenuItem::~WebUIMenuItem() { | |
| 93 if (last_child_) | |
| 94 delete last_child_; | |
|
xiyuan
2011/07/29 21:39:33
why not use scoped_ptr?
Nikita (slow)
2011/08/05 23:40:26
Done.
| |
| 95 if (previous_) | |
| 96 delete previous_; | |
|
xiyuan
2011/07/29 21:39:33
ditto
Nikita (slow)
2011/08/05 23:40:26
Done.
| |
| 97 } | |
| 98 | |
| 99 base::ListValue* WebUIMenuItem::CreateValue() { | |
| 100 base::DictionaryValue* item = new base::DictionaryValue(); | |
| 101 item->SetInteger("id", id_); | |
| 102 item->SetString("label", label_); | |
| 103 item->SetString("icon", icon_); | |
| 104 if (id_ >= 0) { | |
| 105 item->SetBoolean("enabled", delegate_->IsCommandEnabled(id_)); | |
| 106 item->SetBoolean( | |
| 107 "bold", delegate_->GetLabelFont(id_).GetStyle() == gfx::Font::BOLD); | |
| 108 } | |
| 109 if (last_child_) { | |
| 110 item->Set("sub", last_child_->CreateValue()); | |
| 111 } | |
| 112 | |
| 113 base::ListValue* list; | |
| 114 if (previous_) { | |
| 115 list = previous_->CreateValue(); | |
| 116 } else { | |
| 117 list = new base::ListValue(); | |
| 118 } | |
| 119 | |
| 120 list->Append(item); | |
| 121 | |
| 122 return list; | |
| 123 } | |
| 124 | |
| 125 views::MenuItemInterface* WebUIMenuItem::AppendSubMenu( | |
| 126 int item_id, const std::wstring& label) { | |
| 127 return last_child_ = | |
| 128 new WebUIMenuItem(label, item_id, std::string(), last_child_, delegate_); | |
| 129 } | |
| 130 | |
| 131 views::MenuItemInterface* WebUIMenuItem::AppendSubMenuWithIcon( | |
| 132 int item_id, const std::wstring& label, const SkBitmap& icon) { | |
| 133 return last_child_ = new WebUIMenuItem(label, | |
| 134 item_id, | |
| 135 web_ui_util::GetImageDataUrl(icon), | |
| 136 last_child_, | |
| 137 delegate_); | |
| 138 } | |
| 139 | |
| 140 views::MenuItemInterface* WebUIMenuItem::AppendMenuItemWithLabel( | |
| 141 int item_id, const std::wstring& label) { | |
| 142 return last_child_ = | |
| 143 new WebUIMenuItem(label, item_id, std::string(), last_child_, delegate_); | |
| 144 } | |
| 145 | |
| 146 views::MenuItemInterface* WebUIMenuItem::AppendMenuItemWithIcon( | |
| 147 int item_id, const std::wstring& label, const SkBitmap& icon) { | |
| 148 return last_child_ = new WebUIMenuItem(label, | |
| 149 item_id, | |
| 150 web_ui_util::GetImageDataUrl(icon), | |
| 151 last_child_, | |
| 152 delegate_); | |
| 153 } | |
| 154 | |
| 155 void WebUIMenuItem::AppendSeparator() { | |
| 156 last_child_ = new WebUIMenuItem(UTF8ToWide("-----"), -2, | |
| 157 std::string(), last_child_, | |
| 158 delegate_); | |
| 159 } | |
| 160 | |
| 161 void WebUIMenuItem::ClearSubmenu() { | |
| 162 if (last_child_) | |
| 163 delete last_child_; | |
| 164 last_child_ = NULL; | |
| 165 } | |
| 166 | |
| 167 void WebUIMenuItem::ChildrenChanged() { | |
| 168 if (web_ui_) { | |
| 169 scoped_ptr<base::ListValue> data(CreateValue()); | |
| 170 web_ui_->CallJavascriptFunction("oobe.NetworkScreen.updateNetworks", *data); | |
| 171 } | |
| 172 } | |
| 173 | |
| 174 void WebUIMenuItem::Cancel() { | |
| 175 } | |
| 176 | |
| 177 void WebUIMenuItem::set_margins(int top_margin, int bottom_margin) { | |
| 178 } | |
| 179 | |
| 180 WebUIMenuItem::WebUIMenuItem(const std::wstring& label, | |
| 181 int id, | |
| 182 const std::string& icon, | |
| 183 WebUIMenuItem* previous, | |
| 184 views::MenuDelegate *delegate) | |
| 185 : delegate_(delegate), | |
| 186 id_(id), | |
| 187 icon_(icon), | |
| 188 label_(WideToUTF8(label)), | |
| 189 previous_(previous), | |
| 190 last_child_(NULL), | |
| 191 web_ui_(NULL) { | |
| 192 } | |
| 193 | |
| 194 // NetworkDropdown ------------------------------------------------------------- | |
| 195 | |
| 196 NetworkDropdown::NetworkDropdown(WebUI *web_ui, gfx::NativeWindow parent_window) | |
| 197 : parent_window_(parent_window), | |
| 198 web_ui_(web_ui) { | |
| 199 network_menu_.reset(new NetworkMenuWebUI(this, web_ui)); | |
| 200 network_icon_.reset( | |
| 201 new NetworkMenuIcon(this, NetworkMenuIcon::DROPDOWN_MODE)); | |
| 202 CrosLibrary::Get()->GetNetworkLibrary()->AddNetworkManagerObserver(this); | |
| 203 | |
| 204 SetNetworkIconAndText(); | |
| 205 network_menu_->UpdateMenu(); | |
| 206 } | |
| 207 | |
| 208 NetworkDropdown::~NetworkDropdown() { | |
| 209 CrosLibrary::Get()->GetNetworkLibrary()->RemoveNetworkManagerObserver(this); | |
| 210 } | |
| 211 | |
| 212 void NetworkDropdown::OnItemChosen(int id) { | |
| 213 network_menu_->OnItemChosen(id); | |
| 214 } | |
| 215 | |
| 216 views::MenuButton *NetworkDropdown::GetMenuButton() { | |
| 217 NOTREACHED(); | |
| 218 return NULL; | |
| 219 } | |
| 220 | |
| 221 gfx::NativeWindow NetworkDropdown::GetNativeWindow() const { | |
| 222 return parent_window_; | |
| 223 } | |
| 224 | |
| 225 void NetworkDropdown::OpenButtonOptions() { | |
| 226 if (proxy_settings_dialog_.get() == NULL) { | |
| 227 proxy_settings_dialog_.reset( | |
| 228 new ProxySettingsDialog(this, GetNativeWindow())); | |
| 229 } | |
| 230 proxy_settings_dialog_->Show(); | |
| 231 } | |
| 232 | |
| 233 bool NetworkDropdown::ShouldOpenButtonOptions() const { | |
| 234 return true; | |
| 235 } | |
| 236 | |
| 237 void NetworkDropdown::OnNetworkManagerChanged(NetworkLibrary* cros) { | |
| 238 SetNetworkIconAndText(); | |
| 239 network_menu_->UpdateMenu(); | |
| 240 } | |
| 241 | |
| 242 void NetworkDropdown::OnDialogClosed() { | |
| 243 } | |
| 244 | |
| 245 void NetworkDropdown::NetworkMenuIconChanged() { | |
| 246 // TODO(altimofeev): update icon. | |
| 247 } | |
| 248 | |
| 249 void NetworkDropdown::SetNetworkIconAndText() { | |
| 250 string16 text; | |
| 251 const SkBitmap* icon_bitmap = network_icon_->GetIconAndText(&text); | |
| 252 std::string icon_str = | |
| 253 icon_bitmap ? web_ui_util::GetImageDataUrl(*icon_bitmap) : std::string(); | |
| 254 base::StringValue title(text); | |
| 255 base::StringValue icon(icon_str); | |
| 256 web_ui_->CallJavascriptFunction("oobe.NetworkScreen.updateNetworkTitle", | |
| 257 title, icon); | |
| 258 // TODO(altimofeev): update icon. | |
| 259 } | |
| 260 | |
| 261 } // namespace chromeos | |
| OLD | NEW |