OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/status/network_menu.h" | 5 #include "chrome/browser/chromeos/status/network_menu.h" |
6 | 6 |
7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
8 #include "app/resource_bundle.h" | 8 #include "app/resource_bundle.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
12 #include "chrome/browser/browser.h" | 12 #include "chrome/browser/browser.h" |
13 #include "chrome/browser/browser_list.h" | 13 #include "chrome/browser/browser_list.h" |
14 #include "chrome/browser/chromeos/cros/cros_library.h" | 14 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 15 #include "chrome/browser/chromeos/dom_ui/network_menu_ui.h" |
15 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
16 #include "chrome/common/url_constants.h" | 17 #include "chrome/common/url_constants.h" |
17 #include "chrome/browser/views/window.h" | 18 #include "chrome/browser/views/window.h" |
18 #include "gfx/canvas_skia.h" | 19 #include "gfx/canvas_skia.h" |
19 #include "gfx/skbitmap_operations.h" | 20 #include "gfx/skbitmap_operations.h" |
20 #include "grit/generated_resources.h" | 21 #include "grit/generated_resources.h" |
21 #include "grit/theme_resources.h" | 22 #include "grit/theme_resources.h" |
22 #include "net/base/escape.h" | 23 #include "net/base/escape.h" |
| 24 #include "views/controls/menu/menu_2.h" |
23 #include "views/window/window.h" | 25 #include "views/window/window.h" |
24 | 26 |
| 27 namespace { |
| 28 // Constants passed to Javascript: |
| 29 static const char* kNetworkTypeEthernet = "ethernet"; |
| 30 static const char* kNetworkTypeWifi = "wifi"; |
| 31 static const char* kNetworkTypeCellular = "cellular"; |
| 32 static const char* kNetworkTypeOther = "other"; |
| 33 |
| 34 static const char* kNetworkStatusConnected = "connected"; |
| 35 static const char* kNetworkStatusConnecting = "connecting"; |
| 36 static const char* kNetworkStatusDisconnected = "disconnected"; |
| 37 static const char* kNetworkStatusError = "error"; |
| 38 } |
| 39 |
25 namespace chromeos { | 40 namespace chromeos { |
26 | 41 |
27 //////////////////////////////////////////////////////////////////////////////// | 42 //////////////////////////////////////////////////////////////////////////////// |
28 // NetworkMenu | 43 // NetworkMenu |
29 | 44 |
30 // static | 45 // static |
31 const int NetworkMenu::kNumWifiImages = 9; | 46 const int NetworkMenu::kNumWifiImages = 9; |
32 | 47 |
33 NetworkMenu::NetworkMenu() | 48 NetworkMenu::NetworkMenu() |
34 : ALLOW_THIS_IN_INITIALIZER_LIST(network_menu_(this)), | 49 : min_width_(-1) { |
35 min_width_(-1) { | 50 network_menu_.reset(NetworkMenuUI::CreateMenu2(this)); |
36 } | 51 } |
37 | 52 |
38 NetworkMenu::~NetworkMenu() { | 53 NetworkMenu::~NetworkMenu() { |
39 } | 54 } |
40 | 55 |
| 56 bool NetworkMenu::GetNetworkAt(int index, NetworkInfo* info) const { |
| 57 DCHECK(info); |
| 58 bool res = true; // True unless a network doesn't exist. |
| 59 int flags = menu_items_[index].flags; |
| 60 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); |
| 61 if (flags & FLAG_ETHERNET) { |
| 62 info->network_type = kNetworkTypeEthernet; |
| 63 if (cros->ethernet_connected()) { |
| 64 info->status = kNetworkStatusConnected; |
| 65 info->ip_address = cros->ethernet_network().ip_address(); |
| 66 } |
| 67 info->need_passphrase = false; |
| 68 info->remembered = true; |
| 69 } else if (flags & FLAG_WIFI) { |
| 70 WifiNetwork wifi; |
| 71 bool found = cros->FindWifiNetworkByPath( |
| 72 menu_items_[index].wireless_path, &wifi); |
| 73 if (found) { |
| 74 info->network_type = kNetworkTypeWifi; |
| 75 if (wifi.name() == cros->wifi_name()) { |
| 76 if (cros->wifi_connected()) { |
| 77 info->status = kNetworkStatusConnected; |
| 78 info->message = l10n_util::GetStringUTF8( |
| 79 IDS_STATUSBAR_NETWORK_DEVICE_CONNECTED); |
| 80 } else if (cros->wifi_connecting()) { |
| 81 info->status = kNetworkStatusConnecting; |
| 82 // TODO(stevenjb): Eliminate status message, or localize properly. |
| 83 info->message = l10n_util::GetStringUTF8( |
| 84 IDS_STATUSBAR_NETWORK_DEVICE_CONNECTING) |
| 85 + ": " + wifi.GetStateString(); |
| 86 } else if (wifi.state() == STATE_FAILURE) { |
| 87 info->status = kNetworkStatusError; |
| 88 info->message = wifi.GetErrorString(); |
| 89 } else { |
| 90 info->status = kNetworkStatusDisconnected; |
| 91 info->message = l10n_util::GetStringUTF8( |
| 92 IDS_STATUSBAR_NETWORK_DEVICE_DISCONNECTED); |
| 93 } |
| 94 } |
| 95 info->ip_address = wifi.ip_address(); |
| 96 info->need_passphrase = wifi.encrypted(); |
| 97 info->remembered = wifi.favorite(); |
| 98 } else { |
| 99 res = false; // Network not found, hide entry. |
| 100 } |
| 101 } else if (flags & FLAG_CELLULAR) { |
| 102 CellularNetwork cellular; |
| 103 bool found = cros->FindCellularNetworkByPath( |
| 104 menu_items_[index].wireless_path, &cellular); |
| 105 if (found) { |
| 106 info->network_type = kNetworkTypeCellular; |
| 107 if (cellular.name() == cros->cellular_name()) { |
| 108 if (cros->cellular_connected()) { |
| 109 info->status = kNetworkStatusConnected; |
| 110 info->message = l10n_util::GetStringUTF8( |
| 111 IDS_STATUSBAR_NETWORK_DEVICE_CONNECTED); |
| 112 } else if (cros->cellular_connecting()) { |
| 113 // TODO(stevenjb): Eliminate status message, or localize properly. |
| 114 info->status = kNetworkStatusConnecting; |
| 115 info->message = l10n_util::GetStringUTF8( |
| 116 IDS_STATUSBAR_NETWORK_DEVICE_CONNECTING) |
| 117 + ": " + cellular.GetStateString(); |
| 118 } else if (cellular.state() == STATE_FAILURE) { |
| 119 info->status = kNetworkStatusError; |
| 120 info->message = cellular.GetErrorString(); |
| 121 } else { |
| 122 info->status = kNetworkStatusDisconnected; |
| 123 info->message = l10n_util::GetStringUTF8( |
| 124 IDS_STATUSBAR_NETWORK_DEVICE_DISCONNECTED); |
| 125 } |
| 126 } |
| 127 info->ip_address = cellular.ip_address(); |
| 128 info->need_passphrase = false; |
| 129 info->remembered = true; |
| 130 } else { |
| 131 res = false; // Network not found, hide entry. |
| 132 } |
| 133 } else if (flags & FLAG_OTHER_NETWORK) { |
| 134 info->network_type = kNetworkTypeOther; |
| 135 info->need_passphrase = true; |
| 136 info->remembered = true; |
| 137 } else { |
| 138 // Not a network, e.g options, separator. |
| 139 } |
| 140 return res; |
| 141 } |
| 142 |
41 //////////////////////////////////////////////////////////////////////////////// | 143 //////////////////////////////////////////////////////////////////////////////// |
42 // NetworkMenu, menus::MenuModel implementation: | 144 // NetworkMenu, menus::MenuModel implementation: |
43 | 145 |
44 int NetworkMenu::GetItemCount() const { | 146 int NetworkMenu::GetItemCount() const { |
45 return static_cast<int>(menu_items_.size()); | 147 return static_cast<int>(menu_items_.size()); |
46 } | 148 } |
47 | 149 |
48 menus::MenuModel::ItemType NetworkMenu::GetTypeAt(int index) const { | 150 menus::MenuModel::ItemType NetworkMenu::GetTypeAt(int index) const { |
49 return menu_items_[index].type; | 151 return menu_items_[index].type; |
50 } | 152 } |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 } | 300 } |
199 } else { | 301 } else { |
200 cros->ConnectToCellularNetwork(cellular); | 302 cros->ConnectToCellularNetwork(cellular); |
201 } | 303 } |
202 } | 304 } |
203 } | 305 } |
204 | 306 |
205 void NetworkMenu::SetFirstLevelMenuWidth(int width) { | 307 void NetworkMenu::SetFirstLevelMenuWidth(int width) { |
206 min_width_ = width; | 308 min_width_ = width; |
207 // This actually has no effect since menu is rebuilt before showing. | 309 // This actually has no effect since menu is rebuilt before showing. |
208 network_menu_.SetMinimumWidth(width); | 310 network_menu_->SetMinimumWidth(width); |
209 } | 311 } |
210 | 312 |
211 void NetworkMenu::CancelMenu() { | 313 void NetworkMenu::CancelMenu() { |
212 network_menu_.CancelMenu(); | 314 network_menu_->CancelMenu(); |
213 } | 315 } |
214 | 316 |
215 // static | 317 // static |
216 SkBitmap NetworkMenu::IconForNetworkStrength(int strength, bool black) { | 318 SkBitmap NetworkMenu::IconForNetworkStrength(int strength, bool black) { |
217 // Compose wifi icon by superimposing various icons. | 319 // Compose wifi icon by superimposing various icons. |
218 // NOTE: Use an array rather than just calculating a resource number to avoid | 320 // NOTE: Use an array rather than just calculating a resource number to avoid |
219 // creating implicit ordering dependencies on the resource values. | 321 // creating implicit ordering dependencies on the resource values. |
220 static const int kBarsImages[kNumWifiImages] = { | 322 static const int kBarsImages[kNumWifiImages] = { |
221 IDR_STATUSBAR_NETWORK_BARS1, | 323 IDR_STATUSBAR_NETWORK_BARS1, |
222 IDR_STATUSBAR_NETWORK_BARS2, | 324 IDR_STATUSBAR_NETWORK_BARS2, |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 | 370 |
269 //////////////////////////////////////////////////////////////////////////////// | 371 //////////////////////////////////////////////////////////////////////////////// |
270 // NetworkMenu, views::ViewMenuDelegate implementation: | 372 // NetworkMenu, views::ViewMenuDelegate implementation: |
271 | 373 |
272 void NetworkMenu::RunMenu(views::View* source, const gfx::Point& pt) { | 374 void NetworkMenu::RunMenu(views::View* source, const gfx::Point& pt) { |
273 refreshing_menu_ = true; | 375 refreshing_menu_ = true; |
274 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); | 376 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); |
275 cros->RequestWifiScan(); | 377 cros->RequestWifiScan(); |
276 cros->UpdateSystemInfo(); | 378 cros->UpdateSystemInfo(); |
277 InitMenuItems(); | 379 InitMenuItems(); |
278 network_menu_.Rebuild(); | 380 network_menu_->Rebuild(); |
279 network_menu_.UpdateStates(); | 381 network_menu_->UpdateStates(); |
280 // Restore menu width, if it was set up. | 382 // Restore menu width, if it was set up. |
281 // NOTE: width isn't checked for correctness here since all width-related | 383 // NOTE: width isn't checked for correctness here since all width-related |
282 // logic implemented inside |network_menu_|. | 384 // logic implemented inside |network_menu_|. |
283 if (min_width_ != -1) | 385 if (min_width_ != -1) |
284 network_menu_.SetMinimumWidth(min_width_); | 386 network_menu_->SetMinimumWidth(min_width_); |
285 refreshing_menu_ = false; | 387 refreshing_menu_ = false; |
286 network_menu_.RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT); | 388 network_menu_->RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT); |
287 } | 389 } |
288 | 390 |
289 void NetworkMenu::InitMenuItems() { | 391 void NetworkMenu::InitMenuItems() { |
290 menu_items_.clear(); | 392 menu_items_.clear(); |
291 // Populate our MenuItems with the current list of wifi networks. | 393 // Populate our MenuItems with the current list of wifi networks. |
292 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); | 394 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); |
293 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 395 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
294 | 396 |
295 // Ethernet | 397 // Ethernet |
296 string16 label = l10n_util::GetStringUTF16( | 398 string16 label = l10n_util::GetStringUTF16( |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 // Offline mode. | 478 // Offline mode. |
377 // menu_items_.push_back(MenuItem(cros->offline_mode() ? | 479 // menu_items_.push_back(MenuItem(cros->offline_mode() ? |
378 // menus::MenuModel::TYPE_CHECK : menus::MenuModel::TYPE_COMMAND, | 480 // menus::MenuModel::TYPE_CHECK : menus::MenuModel::TYPE_COMMAND, |
379 // l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_OFFLINE_MODE), | 481 // l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_OFFLINE_MODE), |
380 // SkBitmap(), std::string(), FLAG_TOGGLE_OFFLINE)); | 482 // SkBitmap(), std::string(), FLAG_TOGGLE_OFFLINE)); |
381 | 483 |
382 if (cros->Connected() || ShouldOpenButtonOptions()) { | 484 if (cros->Connected() || ShouldOpenButtonOptions()) { |
383 // Separator. | 485 // Separator. |
384 menu_items_.push_back(MenuItem()); | 486 menu_items_.push_back(MenuItem()); |
385 | 487 |
386 // IP address | |
387 if (cros->Connected()) { | |
388 menu_items_.push_back(MenuItem(menus::MenuModel::TYPE_COMMAND, | |
389 ASCIIToUTF16(cros->IPAddress()), SkBitmap(), | |
390 std::string(), FLAG_DISABLED)); | |
391 } | |
392 | |
393 // Network settings. | 488 // Network settings. |
394 if (ShouldOpenButtonOptions()) { | 489 if (ShouldOpenButtonOptions()) { |
395 label = | 490 label = |
396 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_OPEN_OPTIONS_DIALOG); | 491 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_OPEN_OPTIONS_DIALOG); |
397 menu_items_.push_back(MenuItem(menus::MenuModel::TYPE_COMMAND, label, | 492 menu_items_.push_back(MenuItem(menus::MenuModel::TYPE_COMMAND, label, |
398 SkBitmap(), std::string(), FLAG_OPTIONS)); | 493 SkBitmap(), std::string(), FLAG_OPTIONS)); |
399 } | 494 } |
400 } | 495 } |
401 } | 496 } |
402 | 497 |
403 } // namespace chromeos | 498 } // namespace chromeos |
OLD | NEW |