Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / chromeos / status / network_menu.cc
blob6575cbe7cda2ffd21b0f7bb34e462e5ae7f23478
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/chromeos/status/network_menu.h"
7 #include <algorithm>
9 #include "ash/shell.h"
10 #include "ash/shell_delegate.h"
11 #include "ash/system/chromeos/network/network_connect.h"
12 #include "ash/system/chromeos/network/network_icon.h"
13 #include "base/bind.h"
14 #include "base/logging.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "chrome/browser/chromeos/login/user_manager.h"
18 #include "chrome/browser/chromeos/mobile_config.h"
19 #include "chrome/browser/chromeos/options/network_config_view.h"
20 #include "chrome/browser/chromeos/sim_dialog_delegate.h"
21 #include "chrome/browser/chromeos/ui/choose_mobile_network_dialog.h"
22 #include "chrome/browser/defaults.h"
23 #include "chrome/browser/profiles/profile_manager.h"
24 #include "chrome/common/url_constants.h"
25 #include "chromeos/network/device_state.h"
26 #include "chromeos/network/network_state.h"
27 #include "chromeos/network/network_state_handler.h"
28 #include "chromeos/network/shill_property_util.h"
29 #include "grit/ash_resources.h"
30 #include "grit/ash_strings.h"
31 #include "grit/generated_resources.h"
32 #include "third_party/cros_system_api/dbus/service_constants.h"
33 #include "ui/base/l10n/l10n_util.h"
34 #include "ui/base/models/menu_model.h"
35 #include "ui/base/resource/resource_bundle.h"
36 #include "ui/gfx/image/image_skia.h"
38 namespace chromeos {
40 namespace {
42 // Offsets for views menu ids (main menu and submenu ids use the same
43 // namespace).
44 const int kMainIndexMask = 0x1000;
45 const int kMoreIndexMask = 0x4000;
47 // Replace '&' in a string with "&&" to allow it to be a menu item label.
48 std::string EscapeAmpersands(const std::string& input) {
49 std::string str = input;
50 size_t found = str.find('&');
51 while (found != std::string::npos) {
52 str.replace(found, 1, "&&");
53 found = str.find('&', found + 2);
55 return str;
58 // Highlight any connected or connecting networks in the UI.
59 bool ShouldHighlightNetwork(const NetworkState* network) {
60 return network->IsConnectedState() || network->IsConnectingState();
63 void ToggleTechnology(const NetworkTypePattern& technology) {
64 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler();
65 bool is_enabled = handler->IsTechnologyEnabled(technology);
66 ash::network_connect::SetTechnologyEnabled(technology, !is_enabled);
69 } // namespace
71 class NetworkMenuModel : public ui::MenuModel {
72 public:
73 struct MenuItem {
74 MenuItem()
75 : type(ui::MenuModel::TYPE_SEPARATOR),
76 sub_menu_model(NULL),
77 flags(0) {
79 MenuItem(ui::MenuModel::ItemType type, base::string16 label, gfx::ImageSkia icon,
80 const std::string& service_path, int flags)
81 : type(type),
82 label(label),
83 icon(icon),
84 service_path(service_path),
85 sub_menu_model(NULL),
86 flags(flags) {
88 MenuItem(ui::MenuModel::ItemType type, base::string16 label, gfx::ImageSkia icon,
89 NetworkMenuModel* sub_menu_model, int flags)
90 : type(type),
91 label(label),
92 icon(icon),
93 sub_menu_model(sub_menu_model),
94 flags(flags) {
97 ui::MenuModel::ItemType type;
98 base::string16 label;
99 gfx::ImageSkia icon;
100 std::string service_path;
101 NetworkMenuModel* sub_menu_model; // Weak ptr.
102 int flags;
104 typedef std::vector<MenuItem> MenuItemVector;
106 explicit NetworkMenuModel(const base::WeakPtr<NetworkMenu>& owner)
107 : owner_(owner) {}
108 virtual ~NetworkMenuModel() {}
110 // Connect or reconnect to the network at |index|.
111 void ConnectToNetworkAt(int index);
113 // Called by NetworkMenu::UpdateMenu to initialize menu items.
114 virtual void InitMenuItems(bool should_open_button_options) = 0;
116 // Menu item field accessors.
117 const MenuItemVector& menu_items() const { return menu_items_; }
119 // ui::MenuModel implementation
120 // GetCommandIdAt() must be implemented by subclasses.
121 virtual bool HasIcons() const OVERRIDE;
122 virtual int GetItemCount() const OVERRIDE;
123 virtual ui::MenuModel::ItemType GetTypeAt(int index) const OVERRIDE;
124 virtual ui::MenuSeparatorType GetSeparatorTypeAt(int index) const OVERRIDE;
125 virtual base::string16 GetLabelAt(int index) const OVERRIDE;
126 virtual bool IsItemDynamicAt(int index) const OVERRIDE;
127 virtual const gfx::FontList* GetLabelFontListAt(int index) const OVERRIDE;
128 virtual bool GetAcceleratorAt(int index,
129 ui::Accelerator* accelerator) const OVERRIDE;
130 virtual bool IsItemCheckedAt(int index) const OVERRIDE;
131 virtual int GetGroupIdAt(int index) const OVERRIDE;
132 virtual bool GetIconAt(int index, gfx::Image* icon) OVERRIDE;
133 virtual ui::ButtonMenuItemModel* GetButtonMenuItemAt(
134 int index) const OVERRIDE;
135 virtual bool IsEnabledAt(int index) const OVERRIDE;
136 virtual bool IsVisibleAt(int index) const OVERRIDE;
137 virtual ui::MenuModel* GetSubmenuModelAt(int index) const OVERRIDE;
138 virtual void HighlightChangedTo(int index) OVERRIDE;
139 virtual void ActivatedAt(int index) OVERRIDE;
140 virtual void SetMenuModelDelegate(ui::MenuModelDelegate* delegate) OVERRIDE;
141 virtual ui::MenuModelDelegate* GetMenuModelDelegate() const OVERRIDE;
143 protected:
144 enum MenuItemFlags {
145 FLAG_NONE = 0,
146 FLAG_DISABLED = 1 << 0,
147 FLAG_TOGGLE_WIFI = 1 << 2,
148 FLAG_TOGGLE_MOBILE = 1 << 3,
149 FLAG_ASSOCIATED = 1 << 5,
150 FLAG_ETHERNET = 1 << 6,
151 FLAG_WIFI = 1 << 7,
152 FLAG_WIMAX = 1 << 8,
153 FLAG_CELLULAR = 1 << 9,
154 FLAG_OPTIONS = 1 << 10,
155 FLAG_ADD_WIFI = 1 << 11,
156 FLAG_ADD_CELLULAR = 1 << 12,
159 // Our menu items.
160 MenuItemVector menu_items_;
162 // Weak pointer to NetworkMenu that owns this MenuModel.
163 base::WeakPtr<NetworkMenu> owner_;
165 private:
166 // Open a dialog to set up and connect to a network.
167 void ShowOther(const std::string& type) const;
169 DISALLOW_COPY_AND_ASSIGN(NetworkMenuModel);
172 class MoreMenuModel : public NetworkMenuModel {
173 public:
174 explicit MoreMenuModel(const base::WeakPtr<NetworkMenu> owner)
175 : NetworkMenuModel(owner) {}
176 virtual ~MoreMenuModel() {}
178 // NetworkMenuModel implementation.
179 virtual void InitMenuItems(bool should_open_button_options) OVERRIDE;
181 // ui::MenuModel implementation
182 virtual int GetCommandIdAt(int index) const OVERRIDE;
184 private:
185 DISALLOW_COPY_AND_ASSIGN(MoreMenuModel);
188 class MainMenuModel : public NetworkMenuModel {
189 public:
190 explicit MainMenuModel(const base::WeakPtr<NetworkMenu>& owner)
191 : NetworkMenuModel(owner),
192 more_menu_model_(new MoreMenuModel(owner)) {
194 virtual ~MainMenuModel() {}
196 // NetworkMenuModel implementation.
197 virtual void InitMenuItems(bool should_open_button_options) OVERRIDE;
199 // ui::MenuModel implementation
200 virtual int GetCommandIdAt(int index) const OVERRIDE;
202 private:
203 void AddWirelessNetworkMenuItem(const NetworkState* wifi_network, int flag);
204 void AddMessageItem(const base::string16& msg);
206 scoped_ptr<MoreMenuModel> more_menu_model_;
208 DISALLOW_COPY_AND_ASSIGN(MainMenuModel);
211 ////////////////////////////////////////////////////////////////////////////////
212 // NetworkMenuModel, public methods:
214 void NetworkMenuModel::ConnectToNetworkAt(int index) {
215 const std::string& service_path = menu_items_[index].service_path;
216 gfx::NativeWindow native_window = owner_->delegate()->GetNativeWindow();
217 ash::network_connect::ConnectToNetwork(service_path, native_window);
220 ////////////////////////////////////////////////////////////////////////////////
221 // NetworkMenuModel, ui::MenuModel implementation:
223 bool NetworkMenuModel::HasIcons() const {
224 return true;
227 int NetworkMenuModel::GetItemCount() const {
228 return static_cast<int>(menu_items_.size());
231 ui::MenuModel::ItemType NetworkMenuModel::GetTypeAt(int index) const {
232 return menu_items_[index].type;
235 ui::MenuSeparatorType NetworkMenuModel::GetSeparatorTypeAt(int index) const {
236 return ui::NORMAL_SEPARATOR;
239 base::string16 NetworkMenuModel::GetLabelAt(int index) const {
240 return menu_items_[index].label;
243 bool NetworkMenuModel::IsItemDynamicAt(int index) const {
244 return false;
247 const gfx::FontList* NetworkMenuModel::GetLabelFontListAt(int index) const {
248 const gfx::FontList* font_list = NULL;
249 if (menu_items_[index].flags & FLAG_ASSOCIATED) {
250 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
251 font_list = &rb.GetFontList(browser_defaults::kAssociatedNetworkFontStyle);
254 return font_list;
257 bool NetworkMenuModel::GetAcceleratorAt(int index,
258 ui::Accelerator* accelerator) const {
259 return false;
262 bool NetworkMenuModel::IsItemCheckedAt(int index) const {
263 // All ui::MenuModel::TYPE_CHECK menu items are checked.
264 return true;
267 int NetworkMenuModel::GetGroupIdAt(int index) const {
268 return 0;
271 bool NetworkMenuModel::GetIconAt(int index, gfx::Image* icon) {
272 if (!menu_items_[index].icon.isNull()) {
273 *icon = gfx::Image(menu_items_[index].icon);
274 return true;
276 return false;
279 ui::ButtonMenuItemModel* NetworkMenuModel::GetButtonMenuItemAt(
280 int index) const {
281 return NULL;
284 bool NetworkMenuModel::IsEnabledAt(int index) const {
285 return !(menu_items_[index].flags & FLAG_DISABLED);
288 bool NetworkMenuModel::IsVisibleAt(int index) const {
289 return true;
292 ui::MenuModel* NetworkMenuModel::GetSubmenuModelAt(int index) const {
293 return menu_items_[index].sub_menu_model;
296 void NetworkMenuModel::HighlightChangedTo(int index) {
299 void NetworkMenuModel::ActivatedAt(int index) {
300 // When we are refreshing the menu, ignore menu item activation.
301 if (owner_->refreshing_menu_)
302 return;
304 int flags = menu_items_[index].flags;
305 if (flags & FLAG_OPTIONS) {
306 owner_->delegate()->OpenButtonOptions();
307 } else if (flags & FLAG_TOGGLE_WIFI) {
308 ToggleTechnology(NetworkTypePattern::WiFi());
309 } else if (flags & FLAG_TOGGLE_MOBILE) {
310 ToggleTechnology(NetworkTypePattern::Mobile());
311 } else if (flags & FLAG_ETHERNET) {
312 owner_->delegate()->OnConnectToNetworkRequested(
313 menu_items_[index].service_path);
314 } else if (flags & (FLAG_WIFI | FLAG_WIMAX | FLAG_CELLULAR)) {
315 ConnectToNetworkAt(index);
316 owner_->delegate()->OnConnectToNetworkRequested(
317 menu_items_[index].service_path);
318 } else if (flags & FLAG_ADD_WIFI) {
319 ShowOther(shill::kTypeWifi);
320 } else if (flags & FLAG_ADD_CELLULAR) {
321 ShowOther(shill::kTypeCellular);
325 void NetworkMenuModel::SetMenuModelDelegate(ui::MenuModelDelegate* delegate) {
328 ui::MenuModelDelegate* NetworkMenuModel::GetMenuModelDelegate() const {
329 return NULL;
332 ////////////////////////////////////////////////////////////////////////////////
333 // NetworkMenuModel, private methods:
335 void NetworkMenuModel::ShowOther(const std::string& type) const {
336 gfx::NativeWindow native_window = owner_->delegate()->GetNativeWindow();
337 if (type == shill::kTypeCellular)
338 ChooseMobileNetworkDialog::ShowDialog(native_window);
339 else
340 NetworkConfigView::ShowForType(shill::kTypeWifi, native_window);
343 ////////////////////////////////////////////////////////////////////////////////
344 // MainMenuModel
346 void MainMenuModel::AddWirelessNetworkMenuItem(const NetworkState* network,
347 int flag) {
348 base::string16 label;
349 // Ampersand is a valid character in an SSID, but menu2 uses it to mark
350 // "mnemonics" for keyboard shortcuts.
351 std::string wifi_name = EscapeAmpersands(network->name());
352 if (network->IsConnectingState()) {
353 label = l10n_util::GetStringFUTF16(
354 IDS_STATUSBAR_NETWORK_DEVICE_STATUS,
355 base::UTF8ToUTF16(wifi_name),
356 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_DEVICE_CONNECTING));
357 } else {
358 label = base::UTF8ToUTF16(wifi_name);
361 // We do not have convenient access to whether or not it might be possible
362 // to connect to a wireless network (e.g. whether certs are required), so all
363 // entries are enabled.
365 if (ShouldHighlightNetwork(network))
366 flag |= FLAG_ASSOCIATED;
367 const gfx::ImageSkia icon = ash::network_icon::GetImageForNetwork(
368 network, ash::network_icon::ICON_TYPE_LIST);
369 menu_items_.push_back(
370 MenuItem(ui::MenuModel::TYPE_COMMAND,
371 label, icon, network->path(), flag));
374 void MainMenuModel::AddMessageItem(const base::string16& msg) {
375 menu_items_.push_back(MenuItem(
376 ui::MenuModel::TYPE_COMMAND, msg,
377 gfx::ImageSkia(), std::string(), FLAG_DISABLED));
380 void MainMenuModel::InitMenuItems(bool should_open_button_options) {
381 menu_items_.clear();
383 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler();
385 // Populate our MenuItems with the current list of networks.
386 base::string16 label;
388 // Ethernet
389 // Only display an ethernet icon if enabled, and an ethernet network exists.
390 bool ethernet_enabled =
391 handler->IsTechnologyEnabled(NetworkTypePattern::Ethernet());
392 const NetworkState* ethernet_network =
393 handler->FirstNetworkByType(NetworkTypePattern::Ethernet());
394 if (ethernet_enabled && ethernet_network) {
395 bool ethernet_connecting = ethernet_network->IsConnectingState();
396 if (ethernet_connecting) {
397 label = l10n_util::GetStringFUTF16(
398 IDS_STATUSBAR_NETWORK_DEVICE_STATUS,
399 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET),
400 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_DEVICE_CONNECTING));
401 } else {
402 label = l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET);
404 int flag = FLAG_ETHERNET;
405 if (ShouldHighlightNetwork(ethernet_network))
406 flag |= FLAG_ASSOCIATED;
407 const gfx::ImageSkia icon = ash::network_icon::GetImageForNetwork(
408 ethernet_network, ash::network_icon::ICON_TYPE_LIST);
409 menu_items_.push_back(MenuItem(ui::MenuModel::TYPE_COMMAND,
410 label, icon, std::string(), flag));
413 // Get the list of all networks.
414 NetworkStateHandler::NetworkStateList network_list;
415 handler->GetNetworkList(&network_list);
417 // Cellular Networks
418 if (handler->IsTechnologyEnabled(NetworkTypePattern::Cellular())) {
419 // List Cellular networks.
420 for (NetworkStateHandler::NetworkStateList::const_iterator iter =
421 network_list.begin(); iter != network_list.end(); ++iter) {
422 const NetworkState* network = *iter;
423 if (network->type() != shill::kTypeCellular)
424 continue;
425 std::string activation_state = network->activation_state();
427 // This is only used in the login screen; do not show unactivated
428 // networks.
429 if (activation_state != shill::kActivationStateActivated)
430 continue;
432 // Ampersand is a valid character in a network name, but menu2 uses it
433 // to mark "mnemonics" for keyboard shortcuts. http://crosbug.com/14697
434 std::string network_name = EscapeAmpersands(network->name());
435 if (network->IsConnectingState()) {
436 label = l10n_util::GetStringFUTF16(
437 IDS_STATUSBAR_NETWORK_DEVICE_STATUS,
438 base::UTF8ToUTF16(network_name),
439 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_DEVICE_CONNECTING));
440 } else {
441 label = base::UTF8ToUTF16(network_name);
444 int flag = FLAG_CELLULAR;
445 bool isActive = ShouldHighlightNetwork(network);
446 if (isActive)
447 flag |= FLAG_ASSOCIATED;
448 const gfx::ImageSkia icon = ash::network_icon::GetImageForNetwork(
449 network, ash::network_icon::ICON_TYPE_LIST);
450 menu_items_.push_back(
451 MenuItem(ui::MenuModel::TYPE_COMMAND,
452 label, icon, network->path(), flag));
455 // For GSM add cellular network scan.
456 const DeviceState* cellular_device =
457 handler->GetDeviceStateByType(NetworkTypePattern::Cellular());
458 if (cellular_device && cellular_device->support_network_scan()) {
459 const gfx::ImageSkia icon =
460 ash::network_icon::GetImageForDisconnectedNetwork(
461 ash::network_icon::ICON_TYPE_LIST, shill::kTypeCellular);
462 menu_items_.push_back(MenuItem(
463 ui::MenuModel::TYPE_COMMAND,
464 l10n_util::GetStringUTF16(
465 IDS_OPTIONS_SETTINGS_OTHER_CELLULAR_NETWORKS),
466 icon, std::string(), FLAG_ADD_CELLULAR));
468 } else {
469 int initializing_message_id =
470 ash::network_icon::GetCellularUninitializedMsg();
471 if (initializing_message_id) {
472 // Initializing cellular modem...
473 AddMessageItem(l10n_util::GetStringUTF16(initializing_message_id));
477 // Wimax Networks
478 if (handler->IsTechnologyEnabled(NetworkTypePattern::Wimax())) {
479 // List Wimax networks.
480 for (NetworkStateHandler::NetworkStateList::const_iterator iter =
481 network_list.begin(); iter != network_list.end(); ++iter) {
482 const NetworkState* network = *iter;
483 if (network->type() != shill::kTypeWimax)
484 continue;
485 AddWirelessNetworkMenuItem(network, FLAG_WIMAX);
489 // Wifi Networks
490 if (handler->IsTechnologyEnabled(NetworkTypePattern::WiFi())) {
491 // List Wifi networks.
492 int scanning_msg = handler->GetScanningByType(NetworkTypePattern::WiFi())
493 ? IDS_ASH_STATUS_TRAY_WIFI_SCANNING_MESSAGE
494 : 0;
495 for (NetworkStateHandler::NetworkStateList::const_iterator iter =
496 network_list.begin(); iter != network_list.end(); ++iter) {
497 const NetworkState* network = *iter;
498 if (network->type() != shill::kTypeWifi)
499 continue;
500 // Add 'Searching for Wi-Fi networks...' after connected networks.
501 if (scanning_msg && !network->IsConnectedState()) {
502 AddMessageItem(l10n_util::GetStringUTF16(scanning_msg));
503 scanning_msg = 0;
505 AddWirelessNetworkMenuItem(network, FLAG_WIFI);
507 if (scanning_msg)
508 AddMessageItem(l10n_util::GetStringUTF16(scanning_msg));
509 const gfx::ImageSkia icon =
510 ash::network_icon::GetImageForConnectedNetwork(
511 ash::network_icon::ICON_TYPE_LIST, shill::kTypeWifi);
512 menu_items_.push_back(MenuItem(
513 ui::MenuModel::TYPE_COMMAND,
514 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_OTHER_WIFI_NETWORKS),
515 icon, std::string(), FLAG_ADD_WIFI));
518 if (menu_items_.empty()) {
519 // No networks available (and not initializing cellular or wifi scanning)
520 AddMessageItem(l10n_util::GetStringFUTF16(
521 IDS_STATUSBAR_NETWORK_MENU_ITEM_INDENT,
522 l10n_util::GetStringUTF16(IDS_STATUSBAR_NO_NETWORKS_MESSAGE)));
525 // Enable / Disable Technology
526 NetworkStateHandler::TechnologyState wifi_state =
527 handler->GetTechnologyState(NetworkTypePattern::WiFi());
528 bool wifi_available =
529 wifi_state != NetworkStateHandler::TECHNOLOGY_UNAVAILABLE;
530 bool wifi_enabled = wifi_state == NetworkStateHandler::TECHNOLOGY_ENABLED;
532 NetworkStateHandler::TechnologyState mobile_state =
533 handler->GetTechnologyState(NetworkTypePattern::Mobile());
534 bool mobile_available =
535 mobile_state != NetworkStateHandler::TECHNOLOGY_UNAVAILABLE;
536 bool mobile_enabled = mobile_state == NetworkStateHandler::TECHNOLOGY_ENABLED;
538 // Do not show disable wifi or cellular during oobe.
539 bool show_toggle_wifi = wifi_available &&
540 (should_open_button_options || !wifi_enabled);
541 bool show_toggle_mobile = mobile_available &&
542 (should_open_button_options || !mobile_enabled);
544 if (show_toggle_wifi || show_toggle_mobile) {
545 menu_items_.push_back(MenuItem()); // Separator
547 if (show_toggle_wifi) {
548 int id = wifi_enabled ? IDS_STATUSBAR_NETWORK_DEVICE_DISABLE :
549 IDS_STATUSBAR_NETWORK_DEVICE_ENABLE;
550 label = l10n_util::GetStringFUTF16(id,
551 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_DEVICE_WIFI));
552 int flag = FLAG_TOGGLE_WIFI;
553 if (wifi_state == NetworkStateHandler::TECHNOLOGY_ENABLING)
554 flag |= FLAG_DISABLED;
555 menu_items_.push_back(MenuItem(ui::MenuModel::TYPE_COMMAND, label,
556 gfx::ImageSkia(), std::string(), flag));
559 if (show_toggle_mobile) {
560 const DeviceState* mobile_device =
561 handler->GetDeviceStateByType(NetworkTypePattern::Mobile());
562 bool is_locked = mobile_device && !mobile_device->sim_lock_type().empty();
563 int id = (mobile_enabled && !is_locked)
564 ? IDS_STATUSBAR_NETWORK_DEVICE_DISABLE
565 : IDS_STATUSBAR_NETWORK_DEVICE_ENABLE;
566 label = l10n_util::GetStringFUTF16(id,
567 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_DEVICE_CELLULAR));
568 gfx::ImageSkia icon;
569 int flag = FLAG_TOGGLE_MOBILE;
570 if (mobile_state == NetworkStateHandler::TECHNOLOGY_ENABLING)
571 flag |= FLAG_DISABLED;
572 menu_items_.push_back(MenuItem(ui::MenuModel::TYPE_COMMAND, label,
573 icon, std::string(), flag));
577 // Additional links like:
578 // * IP Address on active interface;
579 // * Hardware addresses for wifi and ethernet.
580 more_menu_model_->InitMenuItems(should_open_button_options);
581 if (!more_menu_model_->menu_items().empty()) {
582 menu_items_.push_back(MenuItem()); // Separator
583 menu_items_.push_back(MenuItem(
584 ui::MenuModel::TYPE_SUBMENU,
585 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_MORE),
586 gfx::ImageSkia(), more_menu_model_.get(), FLAG_NONE));
590 int MainMenuModel::GetCommandIdAt(int index) const {
591 return index + kMainIndexMask;
594 ////////////////////////////////////////////////////////////////////////////////
595 // MoreMenuModel
597 void MoreMenuModel::InitMenuItems(bool should_open_button_options) {
598 menu_items_.clear();
599 MenuItemVector link_items;
600 MenuItemVector address_items;
602 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler();
603 const NetworkState* default_network = handler->DefaultNetwork();
605 int message_id = -1;
606 if (default_network)
607 message_id = IDS_STATUSBAR_NETWORK_OPEN_PROXY_SETTINGS_DIALOG;
608 if (message_id != -1) {
609 link_items.push_back(MenuItem(ui::MenuModel::TYPE_COMMAND,
610 l10n_util::GetStringUTF16(message_id),
611 gfx::ImageSkia(),
612 std::string(),
613 FLAG_OPTIONS));
616 if (default_network) {
617 std::string ip_address = default_network->ip_address();
618 if (!ip_address.empty()) {
619 address_items.push_back(MenuItem(
620 ui::MenuModel::TYPE_COMMAND, base::ASCIIToUTF16(ip_address),
621 gfx::ImageSkia(), std::string(), FLAG_DISABLED));
625 std::string ethernet_address =
626 handler->FormattedHardwareAddressForType(NetworkTypePattern::Ethernet());
627 if (!ethernet_address.empty()) {
628 std::string label = l10n_util::GetStringUTF8(
629 IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET) + " " + ethernet_address;
630 address_items.push_back(MenuItem(
631 ui::MenuModel::TYPE_COMMAND, base::UTF8ToUTF16(label),
632 gfx::ImageSkia(), std::string(), FLAG_DISABLED));
635 std::string wifi_address =
636 handler->FormattedHardwareAddressForType(NetworkTypePattern::WiFi());
637 if (!wifi_address.empty()) {
638 std::string label = l10n_util::GetStringUTF8(
639 IDS_STATUSBAR_NETWORK_DEVICE_WIFI) + " " + wifi_address;
640 address_items.push_back(MenuItem(
641 ui::MenuModel::TYPE_COMMAND, base::UTF8ToUTF16(label),
642 gfx::ImageSkia(), std::string(), FLAG_DISABLED));
645 menu_items_ = link_items;
646 if (!menu_items_.empty() && address_items.size() > 1)
647 menu_items_.push_back(MenuItem()); // Separator
648 menu_items_.insert(menu_items_.end(),
649 address_items.begin(), address_items.end());
652 int MoreMenuModel::GetCommandIdAt(int index) const {
653 return index + kMoreIndexMask;
656 ////////////////////////////////////////////////////////////////////////////////
657 // NetworkMenu
659 NetworkMenu::Delegate::~Delegate() {
662 NetworkMenu::NetworkMenu(Delegate* delegate)
663 : delegate_(delegate),
664 refreshing_menu_(false),
665 weak_pointer_factory_(this) {
666 main_menu_model_.reset(new MainMenuModel(weak_pointer_factory_.GetWeakPtr()));
669 NetworkMenu::~NetworkMenu() {
672 ui::MenuModel* NetworkMenu::GetMenuModel() {
673 return main_menu_model_.get();
676 void NetworkMenu::UpdateMenu() {
677 refreshing_menu_ = true;
678 main_menu_model_->InitMenuItems(delegate_->ShouldOpenButtonOptions());
679 refreshing_menu_ = false;
682 } // namespace chromeos