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_list.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::FontList
* font_list
= model
->GetLabelFontListAt(i
);
101 item
->SetBoolean("bold", font_list
->GetFontStyle() == gfx::Font::BOLD
);
103 if (type
== ui::MenuModel::TYPE_SUBMENU
)
104 item
->Set("sub", ConvertMenuModel(model
->GetSubmenuModelAt(i
)));
110 // NetworkDropdown -------------------------------------------------------------
112 NetworkDropdown::NetworkDropdown(Actor
* actor
,
113 content::WebUI
* web_ui
,
119 network_menu_
.reset(new NetworkMenuWebUI(this, web_ui
));
120 DCHECK(NetworkHandler::IsInitialized());
121 NetworkStateHandler
* handler
= NetworkHandler::Get()->network_state_handler();
122 handler
->RequestScan();
123 handler
->AddObserver(this, FROM_HERE
);
125 network_scan_timer_
.Start(
127 base::TimeDelta::FromSeconds(kNetworkScanIntervalSecs
),
128 this, &NetworkDropdown::RequestNetworkScan
);
131 NetworkDropdown::~NetworkDropdown() {
132 ash::network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(this);
133 if (NetworkHandler::IsInitialized()) {
134 NetworkHandler::Get()->network_state_handler()->RemoveObserver(
139 void NetworkDropdown::OnItemChosen(int id
) {
140 network_menu_
->OnItemChosen(id
);
143 gfx::NativeWindow
NetworkDropdown::GetNativeWindow() const {
144 return LoginDisplayHostImpl::default_host()->GetNativeWindow();
147 void NetworkDropdown::OpenButtonOptions() {
148 LoginDisplayHostImpl::default_host()->OpenProxySettings();
151 bool NetworkDropdown::ShouldOpenButtonOptions() const {
155 void NetworkDropdown::OnConnectToNetworkRequested(
156 const std::string
& service_path
) {
157 actor_
->OnConnectToNetworkRequested(service_path
);
160 void NetworkDropdown::DefaultNetworkChanged(const NetworkState
* network
) {
164 void NetworkDropdown::NetworkConnectionStateChanged(
165 const NetworkState
* network
) {
169 void NetworkDropdown::NetworkListChanged() {
173 void NetworkDropdown::NetworkIconChanged() {
174 SetNetworkIconAndText();
177 void NetworkDropdown::Refresh() {
178 SetNetworkIconAndText();
179 network_menu_
->UpdateMenu();
182 void NetworkDropdown::SetNetworkIconAndText() {
184 gfx::ImageSkia icon_image
;
185 bool animating
= false;
186 ash::network_icon::GetDefaultNetworkImageAndLabel(
187 ash::network_icon::ICON_TYPE_LIST
, &icon_image
, &text
, &animating
);
189 ash::network_icon::NetworkIconAnimation::GetInstance()->AddObserver(this);
191 ash::network_icon::NetworkIconAnimation::GetInstance()->
192 RemoveObserver(this);
194 SkBitmap icon_bitmap
= icon_image
.GetRepresentation(
195 ui::GetImageScale(web_ui_
->GetDeviceScaleFactor())).sk_bitmap();
196 std::string icon_str
;
197 if (!icon_image
.isNull())
198 icon_str
= webui::GetBitmapDataUrl(icon_bitmap
);
199 base::StringValue
title(text
);
200 base::StringValue
icon(icon_str
);
201 web_ui_
->CallJavascriptFunction("cr.ui.DropDown.updateNetworkTitle",
205 void NetworkDropdown::RequestNetworkScan() {
206 NetworkHandler::Get()->network_state_handler()->RequestScan();
210 } // namespace chromeos