Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1476)

Unified Diff: chrome/browser/chromeos/network_menu_button.cc

Issue 251099: Refactor cros library code into central location and have the UI elements obs... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/browser/chromeos/network_menu_button.h ('k') | chrome/browser/chromeos/power_menu_button.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/chromeos/network_menu_button.cc
===================================================================
--- chrome/browser/chromeos/network_menu_button.cc (revision 28124)
+++ chrome/browser/chromeos/network_menu_button.cc (working copy)
@@ -4,8 +4,6 @@
#include "chrome/browser/chromeos/network_menu_button.h"
-#include <algorithm>
-
#include "app/l10n_util.h"
#include "app/resource_bundle.h"
#include "base/string_util.h"
@@ -22,63 +20,52 @@
// static
const int NetworkMenuButton::kNumWifiImages = 8;
-SkBitmap* NetworkMenuButton::wifi_images_[kNumWifiImages];
-SkBitmap* NetworkMenuButton::wired_image_ = NULL;
-SkBitmap* NetworkMenuButton::disconnected_image_ = NULL;
const int NetworkMenuButton::kAnimationDelayMillis = 100;
-NetworkMenuButton::NetworkMenuButton(Browser* browser, bool cros_library_loaded)
+NetworkMenuButton::NetworkMenuButton(Browser* browser)
: MenuButton(NULL, std::wstring(), this, false),
- cros_library_loaded_(cros_library_loaded),
refreshing_menu_(false),
- ethernet_connected_(false),
network_menu_(this),
browser_(browser),
icon_animation_index_(0),
icon_animation_increasing_(true) {
- static bool initialized = false;
- if (!initialized) {
- ResourceBundle& rb = ResourceBundle::GetSharedInstance();
- int image_index = IDR_STATUSBAR_WIFI_1;
- for (int i = 0; i < kNumWifiImages; i++)
- wifi_images_[i] = rb.GetBitmapNamed(image_index + i);
- wired_image_ = rb.GetBitmapNamed(IDR_STATUSBAR_WIRED);
- disconnected_image_ = rb.GetBitmapNamed(IDR_STATUSBAR_DISCONNECTED);
- initialized = true;
- }
- RefreshNetworks();
+ SetShowHighlighted(false);
+ UpdateIcon();
+ CrosNetworkLibrary::Get()->AddObserver(this);
}
+NetworkMenuButton::~NetworkMenuButton() {
+ CrosNetworkLibrary::Get()->RemoveObserver(this);
+}
+
////////////////////////////////////////////////////////////////////////////////
// NetworkMenuButton, views::Menu2Model implementation:
int NetworkMenuButton::GetItemCount() const {
// The menu contains the available wifi networks. If there are none, then it
// only has one item with a message that no networks are available.
- return wifi_networks_in_menu_.empty() ? 1 :
- static_cast<int>(wifi_networks_in_menu_.size());
+ return wifi_networks_.empty() ? 1 : static_cast<int>(wifi_networks_.size());
}
views::Menu2Model::ItemType NetworkMenuButton::GetTypeAt(int index) const {
- return wifi_networks_in_menu_.empty() ? views::Menu2Model::TYPE_COMMAND :
+ return wifi_networks_.empty() ? views::Menu2Model::TYPE_COMMAND :
views::Menu2Model::TYPE_CHECK;
}
string16 NetworkMenuButton::GetLabelAt(int index) const {
- return wifi_networks_in_menu_.empty() ?
+ return wifi_networks_.empty() ?
l10n_util::GetStringUTF16(IDS_STATUSBAR_NO_NETWORKS_MESSAGE) :
- ASCIIToUTF16(wifi_networks_in_menu_[index].ssid);
+ ASCIIToUTF16(wifi_networks_[index].ssid);
}
bool NetworkMenuButton::IsItemCheckedAt(int index) const {
- // Network that we are connected to (or currently connecting to) is checked.
- return wifi_networks_in_menu_.empty() ? false :
- wifi_networks_in_menu_[index].ssid == current_ssid_ ||
- wifi_networks_in_menu_[index].ssid == connecting_ssid_;
+ // WifiNetwork that we are connected to (or connecting to) is checked.
+ return wifi_networks_.empty() ? false :
+ wifi_networks_[index].ssid == CrosNetworkLibrary::Get()->wifi_ssid();
}
bool NetworkMenuButton::IsEnabledAt(int index) const {
- return !wifi_networks_in_menu_.empty();
+ return !wifi_networks_.empty();
}
void NetworkMenuButton::ActivatedAt(int index) {
@@ -86,21 +73,23 @@
if (refreshing_menu_)
return;
- // We need to look up the ssid in ssids_in_menu_.
- std::string ssid = wifi_networks_in_menu_[index].ssid;
+ CrosNetworkLibrary* cros = CrosNetworkLibrary::Get();
// If clicked on a network that we are already connected to or we are
// currently trying to connect to, then do nothing.
- if (ssid == current_ssid_ || ssid == connecting_ssid_)
+ if (wifi_networks_[index].ssid == cros->wifi_ssid())
return;
+ activated_wifi_network_ = wifi_networks_[index];
+
// If wifi network is not encrypted, then directly connect.
// Otherwise, we open password dialog window.
- if (!wifi_networks_in_menu_[index].encrypted) {
- ConnectToWifiNetwork(ssid, string16());
+ if (!wifi_networks_[index].encrypted) {
+ cros->ConnectToWifiNetwork(wifi_networks_[index], string16());
} else {
gfx::NativeWindow parent = browser_->window()->GetNativeHandle();
- PasswordDialogView* dialog = new PasswordDialogView(this, ssid);
+ PasswordDialogView* dialog = new PasswordDialogView(this,
+ wifi_networks_[index].ssid);
views::Window* window = views::Window::CreateChromeWindow(
parent, gfx::Rect(), dialog);
// Draw the password dialog right below this button and right aligned.
@@ -118,7 +107,9 @@
bool NetworkMenuButton::OnPasswordDialogAccept(const std::string& ssid,
const string16& password) {
- return ConnectToWifiNetwork(ssid, password);
+ CrosNetworkLibrary::Get()->ConnectToWifiNetwork(activated_wifi_network_,
+ password);
+ return true;
}
////////////////////////////////////////////////////////////////////////////////
@@ -126,9 +117,7 @@
void NetworkMenuButton::RunMenu(views::View* source, const gfx::Point& pt,
gfx::NativeView hwnd) {
- RefreshNetworks();
- // Make a copy of the wifi networks that we are showing because it may change.
- wifi_networks_in_menu_ = wifi_networks_;
+ wifi_networks_ = CrosNetworkLibrary::Get()->GetWifiNetworks();
refreshing_menu_ = true;
network_menu_.Rebuild();
network_menu_.UpdateStates();
@@ -136,130 +125,18 @@
network_menu_.RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT);
}
-bool NetworkMenuButton::GetWifiNetwork(const WifiNetworkVector& networks,
- const std::string& ssid,
- WifiNetwork* network) {
- WifiNetworkVector::const_iterator it;
- for (it = networks.begin(); it != networks.end(); ++it) {
- if (it->ssid == ssid) {
- *network = *it;
- return true;
- }
- }
- return false;
-}
+////////////////////////////////////////////////////////////////////////////////
+// NetworkMenuButton, CrosNetworkLibrary::Observer implementation:
-void NetworkMenuButton::AddWifiNetwork(const std::string& ssid,
- bool encrypted,
- chromeos::EncryptionType encryption,
- int strength) {
- wifi_networks_.push_back(
- WifiNetwork(ssid, encrypted, encryption, strength));
-}
-
-void NetworkMenuButton::RefreshNetworks() {
- std::string current;
- std::string connecting;
- wifi_networks_.clear();
- ethernet_connected_ = false;
-
- if (cros_library_loaded_) {
- chromeos::ServiceStatus* service_status = chromeos::GetAvailableNetworks();
- for (int i = 0; i < service_status->size; i++) {
- chromeos::ServiceInfo service = service_status->services[i];
- std::string ssid = service.ssid;
- DLOG(WARNING) << "Found network type=" << service.type <<
- " ssid=" << service.ssid <<
- " state=" << service.state <<
- " needs_passphrase=" << service.needs_passphrase <<
- " encryption=" << service.encryption <<
- " signal_strength=" << service.signal_strength;
- if (service.type == chromeos::TYPE_WIFI) {
- AddWifiNetwork(ssid, service.needs_passphrase, service.encryption,
- service.signal_strength);
- // Check connection state.
- switch (service.state) {
- case chromeos::STATE_ASSOCIATION: // connecting to access point
- case chromeos::STATE_CONFIGURATION: // optaining ip address
- connecting = ssid;
- break;
- case chromeos::STATE_READY: // connected and has ip
- current = ssid;
- break;
- case chromeos::STATE_FAILURE: // failed to connect
- // TODO(chocobo): Handle failure. Show it to user.
- DLOG(WARNING) << "Wifi network failed to connect: " << ssid;
- break;
- case chromeos::STATE_IDLE: // no connection
- case chromeos::STATE_DISCONNECT: // disconnected
- case chromeos::STATE_CARRIER: // not used
- case chromeos::STATE_UNKNOWN: // unknown
- default:
- break;
- }
- } else if (service.type == chromeos::TYPE_ETHERNET) {
- if (service.state == chromeos::STATE_READY)
- ethernet_connected_ = true;
- }
- }
- chromeos::FreeServiceStatus(service_status);
+void NetworkMenuButton::NetworkChanged(CrosNetworkLibrary* obj) {
+ if (CrosNetworkLibrary::Get()->wifi_connecting()) {
+ StartConnectingAnimation();
} else {
- // Use test data if ChromeOS shared library is not loaded.
- AddWifiNetwork("Wifi (12)", false, chromeos::NONE, 12);
- AddWifiNetwork("Wifi RSN (70)", true, chromeos::RSN, 70);
- AddWifiNetwork("Wifi (28)", false, chromeos::NONE, 28);
- AddWifiNetwork("Wifi WEP (99)", true, chromeos::WEP, 99);
- current = connecting_ssid_.empty() ? current_ssid_ : connecting_ssid_;
- ethernet_connected_ = true;
- }
-
- // Sort the list of wifi networks by ssid.
- std::sort(wifi_networks_.begin(), wifi_networks_.end());
-
- connecting_ssid_ = connecting;
- current_ssid_ = current;
-
- if (connecting_ssid_.empty()) {
StopConnectingAnimation();
UpdateIcon();
- } else {
- StartConnectingAnimation();
}
}
-static const char* GetEncryptionString(chromeos::EncryptionType encryption) {
- switch (encryption) {
- case chromeos::NONE:
- return "none";
- case chromeos::RSN:
- return "rsn";
- case chromeos::WEP:
- return "wep";
- case chromeos::WPA:
- return "wpa";
- }
- return "none";
-}
-
-bool NetworkMenuButton::ConnectToWifiNetwork(const std::string& ssid,
- const string16& password) {
- bool ok = true;
- if (cros_library_loaded_) {
- chromeos::EncryptionType encryption = chromeos::NONE;
- WifiNetwork network;
- if (GetWifiNetwork(wifi_networks_in_menu_, ssid, &network))
- encryption = network.encryption;
- ok = chromeos::ConnectToWifiNetwork(ssid.c_str(),
- password.empty() ? NULL : UTF16ToUTF8(password).c_str(),
- GetEncryptionString(encryption));
- }
- if (ok) {
- connecting_ssid_ = ssid;
- StartConnectingAnimation();
- }
- return true;
-}
-
void NetworkMenuButton::StartConnectingAnimation() {
if (!timer_.IsRunning()) {
icon_animation_index_ = 0;
@@ -276,7 +153,9 @@
}
void NetworkMenuButton::UpdateIcon() {
- if (!connecting_ssid_.empty()) {
+ CrosNetworkLibrary* cros = CrosNetworkLibrary::Get();
+ int id = IDR_STATUSBAR_DISCONNECTED;
+ if (cros->wifi_connecting()) {
// Get the next frame. Reverse direction if necessary.
if (icon_animation_increasing_) {
icon_animation_index_++;
@@ -291,34 +170,23 @@
icon_animation_increasing_ = true;
}
}
- SetIcon(*wifi_images_[icon_animation_index_]);
-
- // Refresh wifi networks every full animation.
- // And see if we need to stop the animation.
- if (icon_animation_index_ == 0)
- RefreshNetworks();
+ id = IDR_STATUSBAR_WIFI_1 + icon_animation_index_;
} else {
- if (current_ssid_.empty()) {
- if (ethernet_connected_)
- SetIcon(*wired_image_);
+ if (cros->wifi_ssid().empty()) {
+ if (cros->ethernet_connected())
+ id = IDR_STATUSBAR_WIRED;
else
- SetIcon(*disconnected_image_);
+ id = IDR_STATUSBAR_DISCONNECTED;
} else {
- WifiNetwork network;
- if (GetWifiNetwork(wifi_networks_, current_ssid_, &network)) {
- // Gets the wifi image of 1-8 bars depending on signal strength. Signal
- // strength is from 0 to 100, so we need to convert that to 0 to 7.
- int index = floor(network.strength / (100.0 / kNumWifiImages));
- // This can happen if the signal strength is 100.
- if (index == kNumWifiImages)
- index--;
- SetIcon(*wifi_images_[index]);
- } else {
- // We no longer find the current network in the list of networks.
- // So just set the icon to the disconnected image.
- SetIcon(*disconnected_image_);
- }
+ // Gets the wifi image of 1-8 bars depending on signal strength. Signal
+ // strength is from 0 to 100, so we need to convert that to 0 to 7.
+ int index = floor(cros->wifi_strength() / (100.0 / kNumWifiImages));
+ // This can happen if the signal strength is 100.
+ if (index == kNumWifiImages)
+ index--;
+ id = IDR_STATUSBAR_WIFI_1 + index;
}
}
+ SetIcon(*ResourceBundle::GetSharedInstance().GetBitmapNamed(id));
SchedulePaint();
}
« no previous file with comments | « chrome/browser/chromeos/network_menu_button.h ('k') | chrome/browser/chromeos/power_menu_button.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698