Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / status / network_menu.cc
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.
4
5 #include "chrome/browser/chromeos/status/network_menu.h"
6
7 #include <algorithm>
8
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"
37
38 namespace chromeos {
39
40 namespace {
41
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;
46
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);
54   }
55   return str;
56 }
57
58 // Highlight any connected or connecting networks in the UI.
59 bool ShouldHighlightNetwork(const NetworkState* network) {
60   return network->IsConnectedState() || network->IsConnectingState();
61 }
62
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);
67 }
68
69 }  // namespace
70
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) {
78     }
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) {
87     }
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) {
95     }
96
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;
103   };
104   typedef std::vector<MenuItem> MenuItemVector;
105
106   explicit NetworkMenuModel(const base::WeakPtr<NetworkMenu>& owner)
107     : owner_(owner) {}
108   virtual ~NetworkMenuModel() {}
109
110   // Connect or reconnect to the network at |index|.
111   void ConnectToNetworkAt(int index);
112
113   // Called by NetworkMenu::UpdateMenu to initialize menu items.
114   virtual void InitMenuItems(bool should_open_button_options) = 0;
115
116   // Menu item field accessors.
117   const MenuItemVector& menu_items() const { return menu_items_; }
118
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;
142
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,
157   };
158
159   // Our menu items.
160   MenuItemVector menu_items_;
161
162   // Weak pointer to NetworkMenu that owns this MenuModel.
163   base::WeakPtr<NetworkMenu> owner_;
164
165  private:
166   // Open a dialog to set up and connect to a network.
167   void ShowOther(const std::string& type) const;
168
169   DISALLOW_COPY_AND_ASSIGN(NetworkMenuModel);
170 };
171
172 class MoreMenuModel : public NetworkMenuModel {
173  public:
174   explicit MoreMenuModel(const base::WeakPtr<NetworkMenu> owner)
175     : NetworkMenuModel(owner) {}
176   virtual ~MoreMenuModel() {}
177
178   // NetworkMenuModel implementation.
179   virtual void InitMenuItems(bool should_open_button_options) OVERRIDE;
180
181   // ui::MenuModel implementation
182   virtual int GetCommandIdAt(int index) const OVERRIDE;
183
184  private:
185   DISALLOW_COPY_AND_ASSIGN(MoreMenuModel);
186 };
187
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)) {
193   }
194   virtual ~MainMenuModel() {}
195
196   // NetworkMenuModel implementation.
197   virtual void InitMenuItems(bool should_open_button_options) OVERRIDE;
198
199   // ui::MenuModel implementation
200   virtual int GetCommandIdAt(int index) const OVERRIDE;
201
202  private:
203   void AddWirelessNetworkMenuItem(const NetworkState* wifi_network, int flag);
204   void AddMessageItem(const base::string16& msg);
205
206   scoped_ptr<MoreMenuModel> more_menu_model_;
207
208   DISALLOW_COPY_AND_ASSIGN(MainMenuModel);
209 };
210
211 ////////////////////////////////////////////////////////////////////////////////
212 // NetworkMenuModel, public methods:
213
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);
218 }
219
220 ////////////////////////////////////////////////////////////////////////////////
221 // NetworkMenuModel, ui::MenuModel implementation:
222
223 bool NetworkMenuModel::HasIcons() const {
224   return true;
225 }
226
227 int NetworkMenuModel::GetItemCount() const {
228   return static_cast<int>(menu_items_.size());
229 }
230
231 ui::MenuModel::ItemType NetworkMenuModel::GetTypeAt(int index) const {
232   return menu_items_[index].type;
233 }
234
235 ui::MenuSeparatorType NetworkMenuModel::GetSeparatorTypeAt(int index) const {
236   return ui::NORMAL_SEPARATOR;
237 }
238
239 base::string16 NetworkMenuModel::GetLabelAt(int index) const {
240   return menu_items_[index].label;
241 }
242
243 bool NetworkMenuModel::IsItemDynamicAt(int index) const {
244   return false;
245 }
246
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);
252   }
253
254   return font_list;
255 }
256
257 bool NetworkMenuModel::GetAcceleratorAt(int index,
258                                         ui::Accelerator* accelerator) const {
259   return false;
260 }
261
262 bool NetworkMenuModel::IsItemCheckedAt(int index) const {
263   // All ui::MenuModel::TYPE_CHECK menu items are checked.
264   return true;
265 }
266
267 int NetworkMenuModel::GetGroupIdAt(int index) const {
268   return 0;
269 }
270
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;
275   }
276   return false;
277 }
278
279 ui::ButtonMenuItemModel* NetworkMenuModel::GetButtonMenuItemAt(
280     int index) const {
281   return NULL;
282 }
283
284 bool NetworkMenuModel::IsEnabledAt(int index) const {
285   return !(menu_items_[index].flags & FLAG_DISABLED);
286 }
287
288 bool NetworkMenuModel::IsVisibleAt(int index) const {
289   return true;
290 }
291
292 ui::MenuModel* NetworkMenuModel::GetSubmenuModelAt(int index) const {
293   return menu_items_[index].sub_menu_model;
294 }
295
296 void NetworkMenuModel::HighlightChangedTo(int index) {
297 }
298
299 void NetworkMenuModel::ActivatedAt(int index) {
300   // When we are refreshing the menu, ignore menu item activation.
301   if (owner_->refreshing_menu_)
302     return;
303
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);
322   }
323 }
324
325 void NetworkMenuModel::SetMenuModelDelegate(ui::MenuModelDelegate* delegate) {
326 }
327
328 ui::MenuModelDelegate* NetworkMenuModel::GetMenuModelDelegate() const {
329   return NULL;
330 }
331
332 ////////////////////////////////////////////////////////////////////////////////
333 // NetworkMenuModel, private methods:
334
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);
341 }
342
343 ////////////////////////////////////////////////////////////////////////////////
344 // MainMenuModel
345
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);
359   }
360
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.
364
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));
372 }
373
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));
378 }
379
380 void MainMenuModel::InitMenuItems(bool should_open_button_options) {
381   menu_items_.clear();
382
383   NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler();
384
385   // Populate our MenuItems with the current list of networks.
386   base::string16 label;
387
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);
403     }
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));
411   }
412
413   // Get the list of all networks.
414   NetworkStateHandler::NetworkStateList network_list;
415   handler->GetNetworkList(&network_list);
416
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();
426
427       // This is only used in the login screen; do not show unactivated
428       // networks.
429       if (activation_state != shill::kActivationStateActivated)
430         continue;
431
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);
442       }
443
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));
453     }
454
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));
467     }
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));
474     }
475   }
476
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);
486     }
487   }
488
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;
504       }
505       AddWirelessNetworkMenuItem(network, FLAG_WIFI);
506     }
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));
516   }
517
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)));
523   }
524
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;
531
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;
537
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);
543
544   if (show_toggle_wifi || show_toggle_mobile) {
545     menu_items_.push_back(MenuItem());  // Separator
546
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));
557     }
558
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));
574     }
575   }
576
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));
587   }
588 }
589
590 int MainMenuModel::GetCommandIdAt(int index) const {
591   return index + kMainIndexMask;
592 }
593
594 ////////////////////////////////////////////////////////////////////////////////
595 // MoreMenuModel
596
597 void MoreMenuModel::InitMenuItems(bool should_open_button_options) {
598   menu_items_.clear();
599   MenuItemVector link_items;
600   MenuItemVector address_items;
601
602   NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler();
603   const NetworkState* default_network = handler->DefaultNetwork();
604
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));
614   }
615
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));
622     }
623   }
624
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));
633   }
634
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));
643   }
644
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());
650 }
651
652 int MoreMenuModel::GetCommandIdAt(int index) const {
653   return index + kMoreIndexMask;
654 }
655
656 ////////////////////////////////////////////////////////////////////////////////
657 // NetworkMenu
658
659 NetworkMenu::Delegate::~Delegate() {
660 }
661
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()));
667 }
668
669 NetworkMenu::~NetworkMenu() {
670 }
671
672 ui::MenuModel* NetworkMenu::GetMenuModel() {
673   return main_menu_model_.get();
674 }
675
676 void NetworkMenu::UpdateMenu() {
677   refreshing_menu_ = true;
678   main_menu_model_->InitMenuItems(delegate_->ShouldOpenButtonOptions());
679   refreshing_menu_ = false;
680 }
681
682 }  // namespace chromeos