Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / ui / webui / chromeos / login / network_dropdown.cc
blobdd2aa8215074971cc7667f7fde16f9500d7f4dde
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"
7 #include <string>
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"
25 namespace {
27 // Timeout between consecutive requests to network library for network
28 // scan.
29 const int kNetworkScanIntervalSecs = 60;
31 } // namespace
33 namespace chromeos {
35 // WebUI specific implementation of the NetworkMenu class.
36 class NetworkMenuWebUI : public NetworkMenu {
37 public:
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);
46 private:
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),
61 web_ui_(web_ui) {
64 void NetworkMenuWebUI::UpdateMenu() {
65 NetworkMenu::UpdateMenu();
66 if (web_ui_) {
67 scoped_ptr<base::ListValue> list(ConvertMenuModel(GetMenuModel()));
68 web_ui_->CallJavascriptFunction("cr.ui.DropDown.updateNetworks", *list);
72 void NetworkMenuWebUI::OnItemChosen(int id) {
73 int index;
74 ui::MenuModel* model = GetMenuModel();
75 if (!ui::MenuModel::GetModelAndIndexForCommandId(id, &model, &index))
76 return;
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);
84 int id;
85 if (type == ui::MenuModel::TYPE_SEPARATOR)
86 id = -2;
87 else
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);
95 gfx::Image icon;
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));
101 if (id >= 0) {
102 item->SetBoolean("enabled", model->IsEnabledAt(i));
103 const gfx::FontList* font_list = model->GetLabelFontListAt(i);
104 if (font_list)
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)));
109 list->Append(item);
111 return list;
114 // NetworkDropdown -------------------------------------------------------------
116 NetworkDropdown::NetworkDropdown(Actor* actor,
117 content::WebUI* web_ui,
118 bool oobe)
119 : actor_(actor),
120 web_ui_(web_ui),
121 oobe_(oobe) {
122 DCHECK(actor_);
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);
128 Refresh();
129 network_scan_timer_.Start(
130 FROM_HERE,
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(
139 this, FROM_HERE);
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 {
156 return !oobe_;
159 void NetworkDropdown::OnConnectToNetworkRequested() {
160 actor_->OnConnectToNetworkRequested();
163 void NetworkDropdown::DefaultNetworkChanged(const NetworkState* network) {
164 Refresh();
167 void NetworkDropdown::NetworkConnectionStateChanged(
168 const NetworkState* network) {
169 Refresh();
172 void NetworkDropdown::NetworkListChanged() {
173 Refresh();
176 void NetworkDropdown::NetworkIconChanged() {
177 SetNetworkIconAndText();
180 void NetworkDropdown::Refresh() {
181 SetNetworkIconAndText();
182 network_menu_->UpdateMenu();
185 void NetworkDropdown::SetNetworkIconAndText() {
186 base::string16 text;
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);
191 if (animating) {
192 ui::network_icon::NetworkIconAnimation::GetInstance()->AddObserver(this);
193 } else {
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",
204 title, icon);
207 void NetworkDropdown::RequestNetworkScan() {
208 NetworkHandler::Get()->network_state_handler()->RequestScan();
209 Refresh();
212 } // namespace chromeos