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 "content/browser/webui/web_ui.h" |
| 10 #include "chrome/browser/chromeos/login/proxy_settings_dialog.h" |
| 11 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 12 #include "chrome/browser/ui/webui/web_ui_util.h" |
| 13 #include "ui/base/models/menu_model.h" |
| 14 #include "ui/gfx/font.h" |
| 15 |
| 16 namespace chromeos { |
| 17 |
| 18 // WebUI specific implementation of the NetworkMenu class. |
| 19 class NetworkMenuWebUI : public NetworkMenu { |
| 20 public: |
| 21 NetworkMenuWebUI(NetworkMenu::Delegate* delegate, WebUI* web_ui); |
| 22 |
| 23 // NetworkMenu override: |
| 24 virtual void UpdateMenu() OVERRIDE; |
| 25 |
| 26 // Called when item with command |id| is chosen. |
| 27 void OnItemChosen(int id); |
| 28 |
| 29 private: |
| 30 // Converts menu model into the ListValue, ready for passing to WebUI. |
| 31 base::ListValue* ConvertMenuModel(ui::MenuModel* model); |
| 32 |
| 33 // WebUI where network menu is located. |
| 34 WebUI* web_ui_; |
| 35 |
| 36 DISALLOW_COPY_AND_ASSIGN(NetworkMenuWebUI); |
| 37 }; |
| 38 |
| 39 // NetworkMenuWebUI ------------------------------------------------------------ |
| 40 |
| 41 NetworkMenuWebUI::NetworkMenuWebUI(NetworkMenu::Delegate* delegate, |
| 42 WebUI* web_ui) |
| 43 : NetworkMenu(delegate, false), |
| 44 web_ui_(web_ui) { |
| 45 } |
| 46 |
| 47 void NetworkMenuWebUI::UpdateMenu() { |
| 48 NetworkMenu::UpdateMenu(); |
| 49 if (web_ui_) { |
| 50 scoped_ptr<base::ListValue> list(ConvertMenuModel(GetMenuModel())); |
| 51 web_ui_->CallJavascriptFunction("oobe.NetworkScreen.updateNetworks", *list); |
| 52 } |
| 53 } |
| 54 |
| 55 void NetworkMenuWebUI::OnItemChosen(int id) { |
| 56 int index; |
| 57 ui::MenuModel* model = GetMenuModel(); |
| 58 if (!ui::MenuModel::GetModelAndIndexForCommandId(id, &model, &index)) |
| 59 return; |
| 60 model->ActivatedAt(index); |
| 61 } |
| 62 |
| 63 base::ListValue* NetworkMenuWebUI::ConvertMenuModel(ui::MenuModel* model) { |
| 64 base::ListValue* list = new base::ListValue(); |
| 65 for (int i = 0; i < model->GetItemCount(); ++i) { |
| 66 ui::MenuModel::ItemType type = model->GetTypeAt(i); |
| 67 int id; |
| 68 if (type == ui::MenuModel::TYPE_SEPARATOR) |
| 69 id = -2; |
| 70 else |
| 71 id = model->GetCommandIdAt(i); |
| 72 base::DictionaryValue* item = new base::DictionaryValue(); |
| 73 item->SetInteger("id", id); |
| 74 item->SetString("label", model->GetLabelAt(i)); |
| 75 SkBitmap icon; |
| 76 if (model->GetIconAt(i, &icon)) |
| 77 item->SetString("icon", web_ui_util::GetImageDataUrl(icon)); |
| 78 if (id >= 0) { |
| 79 item->SetBoolean("enabled", model->IsEnabledAt(i)); |
| 80 const gfx::Font* font = model->GetLabelFontAt(i); |
| 81 if (font) { |
| 82 item->SetBoolean("bold", font->GetStyle() == gfx::Font::BOLD); |
| 83 } |
| 84 } |
| 85 if (type == ui::MenuModel::TYPE_SUBMENU) |
| 86 item->Set("sub", ConvertMenuModel(model->GetSubmenuModelAt(i))); |
| 87 list->Append(item); |
| 88 } |
| 89 return list; |
| 90 } |
| 91 |
| 92 // NetworkDropdown ------------------------------------------------------------- |
| 93 |
| 94 NetworkDropdown::NetworkDropdown(WebUI *web_ui, gfx::NativeWindow parent_window) |
| 95 : parent_window_(parent_window), |
| 96 web_ui_(web_ui) { |
| 97 network_menu_.reset(new NetworkMenuWebUI(this, web_ui)); |
| 98 network_icon_.reset( |
| 99 new NetworkMenuIcon(this, NetworkMenuIcon::DROPDOWN_MODE)); |
| 100 CrosLibrary::Get()->GetNetworkLibrary()->AddNetworkManagerObserver(this); |
| 101 SetNetworkIconAndText(); |
| 102 network_menu_->UpdateMenu(); |
| 103 } |
| 104 |
| 105 NetworkDropdown::~NetworkDropdown() { |
| 106 CrosLibrary::Get()->GetNetworkLibrary()->RemoveNetworkManagerObserver(this); |
| 107 } |
| 108 |
| 109 void NetworkDropdown::OnItemChosen(int id) { |
| 110 network_menu_->OnItemChosen(id); |
| 111 } |
| 112 |
| 113 views::MenuButton* NetworkDropdown::GetMenuButton() { |
| 114 NOTREACHED(); |
| 115 return NULL; |
| 116 } |
| 117 |
| 118 gfx::NativeWindow NetworkDropdown::GetNativeWindow() const { |
| 119 return parent_window_; |
| 120 } |
| 121 |
| 122 void NetworkDropdown::OpenButtonOptions() { |
| 123 if (proxy_settings_dialog_.get() == NULL) { |
| 124 proxy_settings_dialog_.reset( |
| 125 new ProxySettingsDialog(this, GetNativeWindow())); |
| 126 } |
| 127 proxy_settings_dialog_->Show(); |
| 128 } |
| 129 |
| 130 bool NetworkDropdown::ShouldOpenButtonOptions() const { |
| 131 return true; |
| 132 } |
| 133 |
| 134 void NetworkDropdown::OnNetworkManagerChanged(NetworkLibrary* cros) { |
| 135 SetNetworkIconAndText(); |
| 136 network_menu_->UpdateMenu(); |
| 137 } |
| 138 |
| 139 void NetworkDropdown::OnDialogClosed() { |
| 140 } |
| 141 |
| 142 void NetworkDropdown::NetworkMenuIconChanged() { |
| 143 SetNetworkIconAndText(); |
| 144 } |
| 145 |
| 146 void NetworkDropdown::SetNetworkIconAndText() { |
| 147 string16 text; |
| 148 const SkBitmap* icon_bitmap = network_icon_->GetIconAndText(&text); |
| 149 std::string icon_str = |
| 150 icon_bitmap ? web_ui_util::GetImageDataUrl(*icon_bitmap) : std::string(); |
| 151 base::StringValue title(text); |
| 152 base::StringValue icon(icon_str); |
| 153 web_ui_->CallJavascriptFunction("oobe.NetworkScreen.updateNetworkTitle", |
| 154 title, icon); |
| 155 } |
| 156 |
| 157 } // namespace chromeos |
OLD | NEW |