1 // Copyright (c) 2012 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.
5 #include "chrome/browser/ui/webui/chromeos/login/network_dropdown.h"
9 #include "ash/system/chromeos/network/network_icon.h"
10 #include "ash/system/chromeos/network/network_icon_animation.h"
11 #include "base/time/time.h"
12 #include "base/values.h"
13 #include "chrome/browser/chromeos/login/login_display_host.h"
14 #include "chrome/browser/chromeos/login/login_display_host_impl.h"
15 #include "chromeos/network/network_state_handler.h"
16 #include "content/public/browser/web_ui.h"
17 #include "ui/base/models/menu_model.h"
18 #include "ui/base/webui/web_ui_util.h"
19 #include "ui/gfx/font.h"
20 #include "ui/gfx/image/image.h"
21 #include "ui/gfx/image/image_skia.h"
25 // Timeout between consecutive requests to network library for network
27 const int kNetworkScanIntervalSecs = 60;
33 // WebUI specific implementation of the NetworkMenu class.
34 class NetworkMenuWebUI : public NetworkMenu {
36 NetworkMenuWebUI(NetworkMenu::Delegate* delegate, content::WebUI* web_ui);
38 // NetworkMenu override:
39 virtual void UpdateMenu() OVERRIDE;
41 // Called when item with command |id| is chosen.
42 void OnItemChosen(int id);
45 // Converts menu model into the ListValue, ready for passing to WebUI.
46 base::ListValue* ConvertMenuModel(ui::MenuModel* model);
48 // WebUI where network menu is located.
49 content::WebUI* web_ui_;
51 DISALLOW_COPY_AND_ASSIGN(NetworkMenuWebUI);
54 // NetworkMenuWebUI ------------------------------------------------------------
56 NetworkMenuWebUI::NetworkMenuWebUI(NetworkMenu::Delegate* delegate,
57 content::WebUI* web_ui)
58 : NetworkMenu(delegate),
62 void NetworkMenuWebUI::UpdateMenu() {
63 NetworkMenu::UpdateMenu();
65 scoped_ptr<base::ListValue> list(ConvertMenuModel(GetMenuModel()));
66 web_ui_->CallJavascriptFunction("cr.ui.DropDown.updateNetworks", *list);
70 void NetworkMenuWebUI::OnItemChosen(int id) {
72 ui::MenuModel* model = GetMenuModel();
73 if (!ui::MenuModel::GetModelAndIndexForCommandId(id, &model, &index))
75 model->ActivatedAt(index);
78 base::ListValue* NetworkMenuWebUI::ConvertMenuModel(ui::MenuModel* model) {
79 base::ListValue* list = new base::ListValue();
80 for (int i = 0; i < model->GetItemCount(); ++i) {
81 ui::MenuModel::ItemType type = model->GetTypeAt(i);
83 if (type == ui::MenuModel::TYPE_SEPARATOR)
86 id = model->GetCommandIdAt(i);
87 base::DictionaryValue* item = new base::DictionaryValue();
88 item->SetInteger("id", id);
89 item->SetString("label", model->GetLabelAt(i));
91 if (model->GetIconAt(i, &icon)) {
92 SkBitmap icon_bitmap = icon.ToImageSkia()->GetRepresentation(
94 web_ui_->GetDeviceScaleFactor())).sk_bitmap();
95 item->SetString("icon", webui::GetBitmapDataUrl(icon_bitmap));
98 item->SetBoolean("enabled", model->IsEnabledAt(i));
99 const gfx::Font* font = model->GetLabelFontAt(i);
101 item->SetBoolean("bold", font->GetStyle() == gfx::Font::BOLD);
104 if (type == ui::MenuModel::TYPE_SUBMENU)
105 item->Set("sub", ConvertMenuModel(model->GetSubmenuModelAt(i)));
111 // NetworkDropdown -------------------------------------------------------------
113 NetworkDropdown::NetworkDropdown(Actor* actor,
114 content::WebUI* web_ui,
120 network_menu_.reset(new NetworkMenuWebUI(this, web_ui));
121 DCHECK(NetworkHandler::IsInitialized());
122 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler();
123 handler->RequestScan();
124 handler->AddObserver(this, FROM_HERE);
126 network_scan_timer_.Start(
128 base::TimeDelta::FromSeconds(kNetworkScanIntervalSecs),
129 this, &NetworkDropdown::RequestNetworkScan);
132 NetworkDropdown::~NetworkDropdown() {
133 ash::network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(this);
134 if (NetworkHandler::IsInitialized()) {
135 NetworkHandler::Get()->network_state_handler()->RemoveObserver(
140 void NetworkDropdown::OnItemChosen(int id) {
141 network_menu_->OnItemChosen(id);
144 gfx::NativeWindow NetworkDropdown::GetNativeWindow() const {
145 return LoginDisplayHostImpl::default_host()->GetNativeWindow();
148 void NetworkDropdown::OpenButtonOptions() {
149 LoginDisplayHostImpl::default_host()->OpenProxySettings();
152 bool NetworkDropdown::ShouldOpenButtonOptions() const {
156 void NetworkDropdown::OnConnectToNetworkRequested(
157 const std::string& service_path) {
158 actor_->OnConnectToNetworkRequested(service_path);
161 void NetworkDropdown::DefaultNetworkChanged(const NetworkState* network) {
165 void NetworkDropdown::NetworkConnectionStateChanged(
166 const NetworkState* network) {
170 void NetworkDropdown::NetworkListChanged() {
174 void NetworkDropdown::NetworkIconChanged() {
175 SetNetworkIconAndText();
178 void NetworkDropdown::Refresh() {
179 SetNetworkIconAndText();
180 network_menu_->UpdateMenu();
183 void NetworkDropdown::SetNetworkIconAndText() {
185 gfx::ImageSkia icon_image;
186 bool animating = false;
187 ash::network_icon::GetDefaultNetworkImageAndLabel(
188 ash::network_icon::ICON_TYPE_LIST, &icon_image, &text, &animating);
190 ash::network_icon::NetworkIconAnimation::GetInstance()->AddObserver(this);
192 ash::network_icon::NetworkIconAnimation::GetInstance()->
193 RemoveObserver(this);
195 SkBitmap icon_bitmap = icon_image.GetRepresentation(
196 ui::GetImageScale(web_ui_->GetDeviceScaleFactor())).sk_bitmap();
197 std::string icon_str;
198 if (!icon_image.isNull())
199 icon_str = webui::GetBitmapDataUrl(icon_bitmap);
200 base::StringValue title(text);
201 base::StringValue icon(icon_str);
202 web_ui_->CallJavascriptFunction("cr.ui.DropDown.updateNetworkTitle",
206 void NetworkDropdown::RequestNetworkScan() {
207 NetworkHandler::Get()->network_state_handler()->RequestScan();
211 } // namespace chromeos