Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 23 #include "net/base/escape.h" | 23 #include "net/base/escape.h" |
| 24 #include "ui/base/l10n/l10n_util.h" | 24 #include "ui/base/l10n/l10n_util.h" |
| 25 #include "ui/base/resource/resource_bundle.h" | 25 #include "ui/base/resource/resource_bundle.h" |
| 26 #include "ui/gfx/canvas_skia.h" | 26 #include "ui/gfx/canvas_skia.h" |
| 27 #include "ui/gfx/skbitmap_operations.h" | 27 #include "ui/gfx/skbitmap_operations.h" |
| 28 #include "views/controls/menu/menu_2.h" | 28 #include "views/controls/menu/menu_2.h" |
| 29 #include "views/window/window.h" | 29 #include "views/window/window.h" |
| 30 | 30 |
| 31 namespace chromeos { | 31 namespace chromeos { |
| 32 | 32 |
| 33 class MainMenuModel : public NetworkMenuModel { | 33 class MoreMenuModel : public NetworkMenuModel { |
| 34 public: | 34 public: |
| 35 explicit MainMenuModel(NetworkMenu* owner); | 35 explicit MoreMenuModel(NetworkMenu* owner); |
| 36 virtual ~MainMenuModel() {} | 36 virtual ~MoreMenuModel() {} |
| 37 | 37 |
| 38 // NetworkMenuModel implementation. | 38 // NetworkMenuModel implementation. |
| 39 virtual void InitMenuItems(bool is_browser_mode, | 39 virtual void InitMenuItems(bool is_browser_mode, |
| 40 bool should_open_button_options); | 40 bool should_open_button_options); |
| 41 | 41 |
| 42 private: | 42 private: |
| 43 scoped_ptr<NetworkMenuModel> vpn_menu_model_; | 43 friend class MainMenuModel; |
| 44 | 44 DISALLOW_COPY_AND_ASSIGN(MoreMenuModel); |
| 45 DISALLOW_COPY_AND_ASSIGN(MainMenuModel); | |
| 46 }; | 45 }; |
| 47 | 46 |
| 48 class VPNMenuModel : public NetworkMenuModel { | 47 class VPNMenuModel : public NetworkMenuModel { |
| 49 public: | 48 public: |
| 50 explicit VPNMenuModel(NetworkMenu* owner); | 49 explicit VPNMenuModel(NetworkMenu* owner); |
| 51 virtual ~VPNMenuModel() {} | 50 virtual ~VPNMenuModel() {} |
| 52 | 51 |
| 53 // NetworkMenuModel implementation. | 52 // NetworkMenuModel implementation. |
| 54 virtual void InitMenuItems(bool is_browser_mode, | 53 virtual void InitMenuItems(bool is_browser_mode, |
| 55 bool should_open_button_options); | 54 bool should_open_button_options); |
| 56 | 55 |
| 57 static SkBitmap IconForDisplay(const Network* network); | 56 static SkBitmap IconForDisplay(const Network* network); |
| 58 | 57 |
| 59 private: | 58 private: |
| 60 DISALLOW_COPY_AND_ASSIGN(VPNMenuModel); | 59 DISALLOW_COPY_AND_ASSIGN(VPNMenuModel); |
| 61 }; | 60 }; |
| 62 | 61 |
| 62 class MainMenuModel : public NetworkMenuModel { | |
| 63 public: | |
| 64 explicit MainMenuModel(NetworkMenu* owner); | |
| 65 virtual ~MainMenuModel() {} | |
| 66 | |
| 67 // NetworkMenuModel implementation. | |
| 68 virtual void InitMenuItems(bool is_browser_mode, | |
| 69 bool should_open_button_options); | |
| 70 | |
| 71 private: | |
| 72 scoped_ptr<NetworkMenuModel> vpn_menu_model_; | |
| 73 scoped_ptr<MoreMenuModel> more_menu_model_; | |
| 74 | |
| 75 DISALLOW_COPY_AND_ASSIGN(MainMenuModel); | |
| 76 }; | |
| 77 | |
| 63 //////////////////////////////////////////////////////////////////////////////// | 78 //////////////////////////////////////////////////////////////////////////////// |
| 64 // NetworkMenuModel, public methods: | 79 // NetworkMenuModel, public methods: |
| 65 | 80 |
| 66 bool NetworkMenuModel::ConnectToNetworkAt(int index, | 81 bool NetworkMenuModel::ConnectToNetworkAt(int index, |
| 67 const std::string& passphrase, | 82 const std::string& passphrase, |
| 68 const std::string& ssid, | 83 const std::string& ssid, |
| 69 int auto_connect) const { | 84 int auto_connect) const { |
| 70 int flags = menu_items_[index].flags; | 85 int flags = menu_items_[index].flags; |
| 71 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); | 86 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); |
| 72 const std::string& service_path = menu_items_[index].service_path; | 87 const std::string& service_path = menu_items_[index].service_path; |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 282 } | 297 } |
| 283 | 298 |
| 284 void NetworkMenuModel::ShowOtherCellular() const { | 299 void NetworkMenuModel::ShowOtherCellular() const { |
| 285 ChooseMobileNetworkDialog::ShowDialog(owner_->GetNativeWindow()); | 300 ChooseMobileNetworkDialog::ShowDialog(owner_->GetNativeWindow()); |
| 286 } | 301 } |
| 287 | 302 |
| 288 //////////////////////////////////////////////////////////////////////////////// | 303 //////////////////////////////////////////////////////////////////////////////// |
| 289 // MainMenuModel | 304 // MainMenuModel |
| 290 | 305 |
| 291 MainMenuModel::MainMenuModel(NetworkMenu* owner) | 306 MainMenuModel::MainMenuModel(NetworkMenu* owner) |
| 292 : NetworkMenuModel(owner) { | 307 : NetworkMenuModel(owner), |
| 293 vpn_menu_model_.reset(new VPNMenuModel(owner)); | 308 vpn_menu_model_(new VPNMenuModel(owner)), |
| 309 more_menu_model_(new MoreMenuModel(owner)) { | |
| 294 } | 310 } |
| 295 | 311 |
| 296 void MainMenuModel::InitMenuItems(bool is_browser_mode, | 312 void MainMenuModel::InitMenuItems(bool is_browser_mode, |
| 297 bool should_open_button_options) { | 313 bool should_open_button_options) { |
| 298 // This gets called on initialization, so any changes should be reflected | 314 // This gets called on initialization, so any changes should be reflected |
| 299 // in CrosMock::SetNetworkLibraryStatusAreaExpectations(). | 315 // in CrosMock::SetNetworkLibraryStatusAreaExpectations(). |
| 300 | 316 |
| 301 menu_items_.clear(); | 317 menu_items_.clear(); |
| 302 | 318 |
| 303 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); | 319 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 501 | 517 |
| 502 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnableVPN)) { | 518 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnableVPN)) { |
| 503 // If there's a connected network, add submenu for Private Networks. | 519 // If there's a connected network, add submenu for Private Networks. |
| 504 const Network* connected_network = cros->connected_network(); | 520 const Network* connected_network = cros->connected_network(); |
| 505 if (connected_network) { | 521 if (connected_network) { |
| 506 menu_items_.push_back(MenuItem()); // Separator | 522 menu_items_.push_back(MenuItem()); // Separator |
| 507 menu_items_.push_back(MenuItem( | 523 menu_items_.push_back(MenuItem( |
| 508 ui::MenuModel::TYPE_SUBMENU, | 524 ui::MenuModel::TYPE_SUBMENU, |
| 509 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_PRIVATE_NETWORKS), | 525 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_PRIVATE_NETWORKS), |
| 510 VPNMenuModel::IconForDisplay(connected_network), | 526 VPNMenuModel::IconForDisplay(connected_network), |
| 511 vpn_menu_model_.get(), FLAG_PRIVATE_NETWORKS)); | 527 vpn_menu_model_.get(), FLAG_NONE)); |
| 512 } | 528 } |
| 513 } | 529 } |
| 514 | 530 |
| 515 // Enable / disable wireless. | 531 // Enable / disable wireless. |
| 516 if (wifi_available || cellular_available) { | 532 if (wifi_available || cellular_available) { |
| 517 menu_items_.push_back(MenuItem()); // Separator | 533 menu_items_.push_back(MenuItem()); // Separator |
| 518 | 534 |
| 519 if (wifi_available) { | 535 if (wifi_available) { |
| 520 // Add 'Scanning...' | 536 // Add 'Scanning...' |
| 521 if (cros->wifi_scanning()) { | 537 if (cros->wifi_scanning()) { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 554 } | 570 } |
| 555 } | 571 } |
| 556 | 572 |
| 557 // Offline mode. | 573 // Offline mode. |
| 558 // TODO(chocobo): Uncomment once we figure out how to do offline mode. | 574 // TODO(chocobo): Uncomment once we figure out how to do offline mode. |
| 559 // menu_items_.push_back(MenuItem(cros->offline_mode() ? | 575 // menu_items_.push_back(MenuItem(cros->offline_mode() ? |
| 560 // ui::MenuModel::TYPE_CHECK : ui::MenuModel::TYPE_COMMAND, | 576 // ui::MenuModel::TYPE_CHECK : ui::MenuModel::TYPE_COMMAND, |
| 561 // l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_OFFLINE_MODE), | 577 // l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_OFFLINE_MODE), |
| 562 // SkBitmap(), std::string(), FLAG_TOGGLE_OFFLINE)); | 578 // SkBitmap(), std::string(), FLAG_TOGGLE_OFFLINE)); |
| 563 | 579 |
| 564 bool connected = cros->Connected(); // always call for test expectations. | 580 // Additional links like: |
| 565 bool oobe = !should_open_button_options; // we don't show options for OOBE. | 581 // * Network settings; |
| 566 // Network settings. (And IP Address) | 582 // * IP Address on active interface; |
| 567 if (!oobe) { | 583 // * Hardware addresses for wifi and ethernet. |
| 568 menu_items_.push_back(MenuItem()); // Separator | 584 menu_items_.push_back(MenuItem()); // Separator |
| 569 | 585 more_menu_model_->InitMenuItems( |
| 570 if (connected) { | 586 is_browser_mode, should_open_button_options); |
| 571 std::string ip_address = cros->IPAddress(); | 587 if (is_browser_mode) { |
| 572 if (!ip_address.empty()) { | 588 menu_items_.insert( |
| 573 menu_items_.push_back(MenuItem(ui::MenuModel::TYPE_COMMAND, | 589 menu_items_.end(), |
| 574 ASCIIToUTF16(cros->IPAddress()), SkBitmap(), std::string(), | 590 more_menu_model_->menu_items_.begin(), |
| 575 FLAG_DISABLED)); | 591 more_menu_model_->menu_items_.end()); |
| 576 } | 592 } else { |
| 593 if (!more_menu_model_->menu_items_.empty()) { | |
| 594 menu_items_.push_back(MenuItem( | |
| 595 ui::MenuModel::TYPE_SUBMENU, | |
| 596 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_MORE), | |
| 597 SkBitmap(), more_menu_model_.get(), FLAG_NONE)); | |
| 577 } | 598 } |
| 578 | |
| 579 label = l10n_util::GetStringUTF16(is_browser_mode ? | |
| 580 IDS_STATUSBAR_NETWORK_OPEN_OPTIONS_DIALOG : | |
| 581 IDS_STATUSBAR_NETWORK_OPEN_PROXY_SETTINGS_DIALOG); | |
| 582 menu_items_.push_back(MenuItem(ui::MenuModel::TYPE_COMMAND, label, | |
| 583 SkBitmap(), std::string(), FLAG_OPTIONS)); | |
| 584 } | 599 } |
| 585 | 600 |
| 586 // Recursively call each submenu to populate its own menu items. | 601 // Recursively call each submenu to populate its own menu items. |
| 587 for (size_t i = 0; i < menu_items_.size(); ++i) { | 602 for (size_t i = 0; i < menu_items_.size(); ++i) { |
| 588 if (menu_items_[i].type == ui::MenuModel::TYPE_SUBMENU && | 603 if (menu_items_[i].type == ui::MenuModel::TYPE_SUBMENU) { |
| 589 menu_items_[i].sub_menu_model) { | 604 NetworkMenuModel* model = menu_items_[i].sub_menu_model; |
| 605 if (model == NULL) | |
| 606 continue; | |
| 607 if (model == more_menu_model_.get()) { | |
| 608 // This submenu is already initialized. | |
| 609 continue; | |
| 610 } | |
|
stevenjb
2011/04/26 17:05:08
This was designed to be general purpose, but since
Denis Lagno
2011/04/26 18:04:09
that's reasonable. Done.
| |
| 590 menu_items_[i].sub_menu_model->InitMenuItems(is_browser_mode, | 611 menu_items_[i].sub_menu_model->InitMenuItems(is_browser_mode, |
| 591 should_open_button_options); | 612 should_open_button_options); |
| 592 } | 613 } |
| 593 } | 614 } |
| 594 } | 615 } |
| 595 | 616 |
| 596 //////////////////////////////////////////////////////////////////////////////// | 617 //////////////////////////////////////////////////////////////////////////////// |
| 597 // VPNMenuModel | 618 // VPNMenuModel |
| 598 | 619 |
| 599 VPNMenuModel::VPNMenuModel(NetworkMenu* owner) | 620 VPNMenuModel::VPNMenuModel(NetworkMenu* owner) |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 695 default: | 716 default: |
| 696 LOG(WARNING) << "VPN not handled for connection type " << network->type(); | 717 LOG(WARNING) << "VPN not handled for connection type " << network->type(); |
| 697 return SkBitmap(); | 718 return SkBitmap(); |
| 698 } | 719 } |
| 699 | 720 |
| 700 return NetworkMenu::IconForDisplay(icon, bottom_right_badge, top_left_badge, | 721 return NetworkMenu::IconForDisplay(icon, bottom_right_badge, top_left_badge, |
| 701 bottom_left_badge); | 722 bottom_left_badge); |
| 702 } | 723 } |
| 703 | 724 |
| 704 //////////////////////////////////////////////////////////////////////////////// | 725 //////////////////////////////////////////////////////////////////////////////// |
| 726 // MoreMenuModel | |
| 727 | |
| 728 MoreMenuModel::MoreMenuModel(NetworkMenu* owner) | |
| 729 : NetworkMenuModel(owner) { | |
| 730 } | |
| 731 | |
| 732 void MoreMenuModel::InitMenuItems( | |
| 733 bool is_browser_mode, bool should_open_button_options) { | |
| 734 // This gets called on initialization, so any changes should be reflected | |
| 735 // in CrosMock::SetNetworkLibraryStatusAreaExpectations(). | |
| 736 | |
| 737 menu_items_.clear(); | |
| 738 MenuItemVector link_items; | |
| 739 MenuItemVector address_items; | |
| 740 | |
| 741 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); | |
| 742 bool oobe = !should_open_button_options; // we don't show options for OOBE. | |
| 743 if (!oobe) { | |
| 744 string16 label = l10n_util::GetStringUTF16(is_browser_mode ? | |
| 745 IDS_STATUSBAR_NETWORK_OPEN_OPTIONS_DIALOG : | |
| 746 IDS_STATUSBAR_NETWORK_OPEN_PROXY_SETTINGS_DIALOG); | |
| 747 link_items.push_back(MenuItem(ui::MenuModel::TYPE_COMMAND, label, | |
| 748 SkBitmap(), std::string(), FLAG_OPTIONS)); | |
| 749 } | |
| 750 | |
| 751 bool connected = cros->Connected(); // always call for test expectations. | |
| 752 if (connected) { | |
| 753 std::string ip_address = cros->IPAddress(); | |
| 754 if (!ip_address.empty()) { | |
| 755 address_items.push_back(MenuItem(ui::MenuModel::TYPE_COMMAND, | |
| 756 ASCIIToUTF16(cros->IPAddress()), SkBitmap(), std::string(), | |
| 757 FLAG_DISABLED)); | |
| 758 } | |
| 759 } | |
| 760 | |
| 761 if (!is_browser_mode) { | |
| 762 const NetworkDevice* ether = cros->FindEthernetDevice(); | |
| 763 if (ether) { | |
| 764 std::string hardware_address; | |
| 765 cros->GetIPConfigs(ether->device_path(), &hardware_address, | |
| 766 NetworkLibrary::FORMAT_COLON_SEPARATED_HEX); | |
| 767 if (!hardware_address.empty()) { | |
| 768 std::string label = l10n_util::GetStringUTF8( | |
| 769 IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET) + " " + hardware_address; | |
| 770 address_items.push_back(MenuItem(ui::MenuModel::TYPE_COMMAND, | |
| 771 UTF8ToUTF16(label), SkBitmap(), std::string(), FLAG_DISABLED)); | |
| 772 } | |
| 773 } | |
| 774 | |
| 775 if (cros->wifi_enabled()) { | |
| 776 const NetworkDevice* wifi = cros->FindWifiDevice(); | |
| 777 if (wifi) { | |
| 778 std::string hardware_address; | |
| 779 cros->GetIPConfigs(wifi->device_path(), | |
| 780 &hardware_address, NetworkLibrary::FORMAT_COLON_SEPARATED_HEX); | |
| 781 if (!hardware_address.empty()) { | |
| 782 std::string label = l10n_util::GetStringUTF8( | |
| 783 IDS_STATUSBAR_NETWORK_DEVICE_WIFI) + " " + hardware_address; | |
| 784 address_items.push_back(MenuItem(ui::MenuModel::TYPE_COMMAND, | |
| 785 UTF8ToUTF16(label), SkBitmap(), std::string(), FLAG_DISABLED)); | |
| 786 } | |
| 787 } | |
| 788 } | |
| 789 } | |
| 790 | |
| 791 menu_items_ = link_items; | |
| 792 if (!menu_items_.empty() && address_items.size() > 1) | |
| 793 menu_items_.push_back(MenuItem()); // Separator | |
| 794 menu_items_.insert(menu_items_.end(), | |
| 795 address_items.begin(), address_items.end()); | |
| 796 } | |
| 797 | |
| 798 //////////////////////////////////////////////////////////////////////////////// | |
| 705 // NetworkMenu | 799 // NetworkMenu |
| 706 | 800 |
| 707 // static | 801 // static |
| 708 const int NetworkMenu::kNumBarsImages = 4; | 802 const int NetworkMenu::kNumBarsImages = 4; |
| 709 | 803 |
| 710 // NOTE: Use an array rather than just calculating a resource number to avoid | 804 // NOTE: Use an array rather than just calculating a resource number to avoid |
| 711 // creating implicit ordering dependencies on the resource values. | 805 // creating implicit ordering dependencies on the resource values. |
| 712 // static | 806 // static |
| 713 const int NetworkMenu::kBarsImages[kNumBarsImages] = { | 807 const int NetworkMenu::kBarsImages[kNumBarsImages] = { |
| 714 IDR_STATUSBAR_NETWORK_BARS1, | 808 IDR_STATUSBAR_NETWORK_BARS1, |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 742 | 836 |
| 743 // static | 837 // static |
| 744 const int NetworkMenu::kNumAnimatingImages = 10; | 838 const int NetworkMenu::kNumAnimatingImages = 10; |
| 745 | 839 |
| 746 // static | 840 // static |
| 747 SkBitmap NetworkMenu::kAnimatingImages[kNumAnimatingImages]; | 841 SkBitmap NetworkMenu::kAnimatingImages[kNumAnimatingImages]; |
| 748 | 842 |
| 749 // static | 843 // static |
| 750 SkBitmap NetworkMenu::kAnimatingImagesBlack[kNumAnimatingImages]; | 844 SkBitmap NetworkMenu::kAnimatingImagesBlack[kNumAnimatingImages]; |
| 751 | 845 |
| 752 NetworkMenu::NetworkMenu() | 846 NetworkMenu::NetworkMenu() : min_width_(-1) { |
| 753 : min_width_(-1) { | |
| 754 main_menu_model_.reset(new MainMenuModel(this)); | 847 main_menu_model_.reset(new MainMenuModel(this)); |
| 755 network_menu_.reset(new views::Menu2(main_menu_model_.get())); | 848 network_menu_.reset(new views::Menu2(main_menu_model_.get())); |
| 756 } | 849 } |
| 757 | 850 |
| 758 NetworkMenu::~NetworkMenu() { | 851 NetworkMenu::~NetworkMenu() { |
| 759 } | 852 } |
| 760 | 853 |
| 761 void NetworkMenu::SetFirstLevelMenuWidth(int width) { | 854 void NetworkMenu::SetFirstLevelMenuWidth(int width) { |
| 762 min_width_ = width; | 855 min_width_ = width; |
| 763 // This actually has no effect since menu is rebuilt before showing. | 856 // This actually has no effect since menu is rebuilt before showing. |
| (...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 986 // Restore menu width, if it was set up. | 1079 // Restore menu width, if it was set up. |
| 987 // NOTE: width isn't checked for correctness here since all width-related | 1080 // NOTE: width isn't checked for correctness here since all width-related |
| 988 // logic implemented inside |network_menu_|. | 1081 // logic implemented inside |network_menu_|. |
| 989 if (min_width_ != -1) | 1082 if (min_width_ != -1) |
| 990 network_menu_->SetMinimumWidth(min_width_); | 1083 network_menu_->SetMinimumWidth(min_width_); |
| 991 refreshing_menu_ = false; | 1084 refreshing_menu_ = false; |
| 992 network_menu_->RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT); | 1085 network_menu_->RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT); |
| 993 } | 1086 } |
| 994 | 1087 |
| 995 } // namespace chromeos | 1088 } // namespace chromeos |
| 1089 | |
| 1090 | |
| OLD | NEW |