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 "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/time/time.h"
12 #include "base/values.h"
13 #include "chrome/browser/chromeos/login/ui/login_display_host.h"
14 #include "chrome/browser/chromeos/login/ui/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/chromeos/network/network_icon.h"
20 #include "ui/chromeos/network/network_icon_animation.h"
21 #include "ui/gfx/font_list.h"
22 #include "ui/gfx/image/image.h"
23 #include "ui/gfx/image/image_skia.h"
27 // Timeout between consecutive requests to network library for network
29 const int kNetworkScanIntervalSecs
= 60;
35 // WebUI specific implementation of the NetworkMenu class.
36 class NetworkMenuWebUI
: public NetworkMenu
{
38 NetworkMenuWebUI(NetworkMenu::Delegate
* delegate
, content::WebUI
* web_ui
);
40 // NetworkMenu override:
41 void UpdateMenu() override
;
43 // Called when item with command |id| is chosen.
44 void OnItemChosen(int id
);
47 // Converts menu model into the ListValue, ready for passing to WebUI.
48 base::ListValue
* ConvertMenuModel(ui::MenuModel
* model
);
50 // WebUI where network menu is located.
51 content::WebUI
* web_ui_
;
53 DISALLOW_COPY_AND_ASSIGN(NetworkMenuWebUI
);
56 // NetworkMenuWebUI ------------------------------------------------------------
58 NetworkMenuWebUI::NetworkMenuWebUI(NetworkMenu::Delegate
* delegate
,
59 content::WebUI
* web_ui
)
60 : NetworkMenu(delegate
),
64 void NetworkMenuWebUI::UpdateMenu() {
65 NetworkMenu::UpdateMenu();
67 scoped_ptr
<base::ListValue
> list(ConvertMenuModel(GetMenuModel()));
68 web_ui_
->CallJavascriptFunction("cr.ui.DropDown.updateNetworks", *list
);
72 void NetworkMenuWebUI::OnItemChosen(int id
) {
74 ui::MenuModel
* model
= GetMenuModel();
75 if (!ui::MenuModel::GetModelAndIndexForCommandId(id
, &model
, &index
))
77 model
->ActivatedAt(index
);
80 base::ListValue
* NetworkMenuWebUI::ConvertMenuModel(ui::MenuModel
* model
) {
81 base::ListValue
* list
= new base::ListValue();
82 for (int i
= 0; i
< model
->GetItemCount(); ++i
) {
83 ui::MenuModel::ItemType type
= model
->GetTypeAt(i
);
85 if (type
== ui::MenuModel::TYPE_SEPARATOR
)
88 id
= model
->GetCommandIdAt(i
);
89 base::DictionaryValue
* item
= new base::DictionaryValue();
90 item
->SetInteger("id", id
);
91 base::string16 label
= model
->GetLabelAt(i
);
92 base::ReplaceSubstringsAfterOffset(&label
, 0, base::ASCIIToUTF16("&&"),
93 base::ASCIIToUTF16("&"));
94 item
->SetString("label", label
);
96 if (model
->GetIconAt(i
, &icon
)) {
97 SkBitmap icon_bitmap
= icon
.ToImageSkia()->GetRepresentation(
98 web_ui_
->GetDeviceScaleFactor()).sk_bitmap();
99 item
->SetString("icon", webui::GetBitmapDataUrl(icon_bitmap
));
102 item
->SetBoolean("enabled", model
->IsEnabledAt(i
));
103 const gfx::FontList
* font_list
= model
->GetLabelFontListAt(i
);
105 item
->SetBoolean("bold", font_list
->GetFontStyle() == gfx::Font::BOLD
);
107 if (type
== ui::MenuModel::TYPE_SUBMENU
)
108 item
->Set("sub", ConvertMenuModel(model
->GetSubmenuModelAt(i
)));
114 // NetworkDropdown -------------------------------------------------------------
116 NetworkDropdown::NetworkDropdown(Actor
* actor
,
117 content::WebUI
* web_ui
,
123 network_menu_
.reset(new NetworkMenuWebUI(this, web_ui
));
124 DCHECK(NetworkHandler::IsInitialized());
125 NetworkStateHandler
* handler
= NetworkHandler::Get()->network_state_handler();
126 handler
->RequestScan();
127 handler
->AddObserver(this, FROM_HERE
);
129 network_scan_timer_
.Start(
131 base::TimeDelta::FromSeconds(kNetworkScanIntervalSecs
),
132 this, &NetworkDropdown::RequestNetworkScan
);
135 NetworkDropdown::~NetworkDropdown() {
136 ui::network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(this);
137 if (NetworkHandler::IsInitialized()) {
138 NetworkHandler::Get()->network_state_handler()->RemoveObserver(
143 void NetworkDropdown::OnItemChosen(int id
) {
144 network_menu_
->OnItemChosen(id
);
147 gfx::NativeWindow
NetworkDropdown::GetNativeWindow() const {
148 return LoginDisplayHostImpl::default_host()->GetNativeWindow();
151 void NetworkDropdown::OpenButtonOptions() {
152 LoginDisplayHostImpl::default_host()->OpenProxySettings();
155 bool NetworkDropdown::ShouldOpenButtonOptions() const {
159 void NetworkDropdown::OnConnectToNetworkRequested() {
160 actor_
->OnConnectToNetworkRequested();
163 void NetworkDropdown::DefaultNetworkChanged(const NetworkState
* network
) {
167 void NetworkDropdown::NetworkConnectionStateChanged(
168 const NetworkState
* network
) {
172 void NetworkDropdown::NetworkListChanged() {
176 void NetworkDropdown::NetworkIconChanged() {
177 SetNetworkIconAndText();
180 void NetworkDropdown::Refresh() {
181 SetNetworkIconAndText();
182 network_menu_
->UpdateMenu();
185 void NetworkDropdown::SetNetworkIconAndText() {
187 gfx::ImageSkia icon_image
;
188 bool animating
= false;
189 ui::network_icon::GetDefaultNetworkImageAndLabel(
190 ui::network_icon::ICON_TYPE_LIST
, &icon_image
, &text
, &animating
);
192 ui::network_icon::NetworkIconAnimation::GetInstance()->AddObserver(this);
194 ui::network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(this);
196 SkBitmap icon_bitmap
= icon_image
.GetRepresentation(
197 web_ui_
->GetDeviceScaleFactor()).sk_bitmap();
198 std::string icon_str
;
199 if (!icon_image
.isNull())
200 icon_str
= webui::GetBitmapDataUrl(icon_bitmap
);
201 base::StringValue
title(text
);
202 base::StringValue
icon(icon_str
);
203 web_ui_
->CallJavascriptFunction("cr.ui.DropDown.updateNetworkTitle",
207 void NetworkDropdown::RequestNetworkScan() {
208 NetworkHandler::Get()->network_state_handler()->RequestScan();
212 } // namespace chromeos