diff options
Diffstat (limited to 'chrome/browser/chromeos')
27 files changed, 823 insertions, 377 deletions
diff --git a/chrome/browser/chromeos/cros/cros_mock.cc b/chrome/browser/chromeos/cros/cros_mock.cc index 25bf9aa..13416c5 100644 --- a/chrome/browser/chromeos/cros/cros_mock.cc +++ b/chrome/browser/chromeos/cros/cros_mock.cc @@ -273,7 +273,7 @@ void CrosMock::SetNetworkLibraryStatusAreaExpectations() { .Times(1) .RetiresOnSaturation(); - // NetworkDropdownButton::NetworkChanged() calls: + // NetworkDropdownButton::OnNetworkManagerChanged() calls: EXPECT_CALL(*mock_network_library_, ethernet_connected()) .Times(1) .WillRepeatedly((Return(false))) diff --git a/chrome/browser/chromeos/cros/mock_network_library.h b/chrome/browser/chromeos/cros/mock_network_library.h index 21465c7..513eca34 100644 --- a/chrome/browser/chromeos/cros/mock_network_library.h +++ b/chrome/browser/chromeos/cros/mock_network_library.h @@ -17,10 +17,12 @@ class MockNetworkLibrary : public NetworkLibrary { public: MockNetworkLibrary() {} virtual ~MockNetworkLibrary() {} - MOCK_METHOD1(AddObserver, void(Observer*)); - MOCK_METHOD1(RemoveObserver, void(Observer*)); - MOCK_METHOD2(AddProperyObserver, void(const char*, PropertyObserver*)); - MOCK_METHOD1(RemoveProperyObserver, void(PropertyObserver*)); + MOCK_METHOD1(AddNetworkManagerObserver, void(NetworkManagerObserver*)); + MOCK_METHOD1(RemoveNetworkManagerObserver, void(NetworkManagerObserver*)); + MOCK_METHOD2(AddNetworkObserver, void(const char*, NetworkObserver*)); + MOCK_METHOD1(RemoveNetworkObserver, void(NetworkObserver*)); + MOCK_METHOD1(AddCellularDataPlanObserver, void(NetworkManagerObserver*)); + MOCK_METHOD1(RemoveCellularDataPlanObserver, void(NetworkManagerObserver*)); MOCK_METHOD0(ethernet_network, EthernetNetwork*(void)); MOCK_CONST_METHOD0(ethernet_connecting, bool(void)); MOCK_CONST_METHOD0(ethernet_connected, bool(void)); diff --git a/chrome/browser/chromeos/cros/network_library.cc b/chrome/browser/chromeos/cros/network_library.cc index feb4d49..dac756c 100644 --- a/chrome/browser/chromeos/cros/network_library.cc +++ b/chrome/browser/chromeos/cros/network_library.cc @@ -12,12 +12,130 @@ #include "base/string_number_conversions.h" #include "base/string_util.h" #include "base/utf_string_conversions.h" +#include "base/values.h" #include "chrome/browser/browser_thread.h" #include "chrome/browser/chromeos/cros/cros_library.h" #include "grit/generated_resources.h" namespace chromeos { +namespace { +// TODO(ers) These string constants and Parse functions are copied +// straight out of libcros:chromeos_network.cc. Fix this by moving +// all handling of properties into libcros. +// Network service properties we are interested in monitoring +static const char* kIsActiveProperty = "IsActive"; +static const char* kStateProperty = "State"; +static const char* kSignalStrengthProperty = "Strength"; +static const char* kActivationStateProperty = "Cellular.ActivationState"; +static const char* kNetworkTechnologyProperty = "Cellular.NetworkTechnology"; +static const char* kPaymentURLProperty = "Cellular.OlpUrl"; +static const char* kRestrictedPoolProperty = "Cellular.RestrictedPool"; +static const char* kRoamingStateProperty = "Cellular.RoamingState"; + +// Connman state options. +static const char* kStateIdle = "idle"; +static const char* kStateCarrier = "carrier"; +static const char* kStateAssociation = "association"; +static const char* kStateConfiguration = "configuration"; +static const char* kStateReady = "ready"; +static const char* kStateDisconnect = "disconnect"; +static const char* kStateFailure = "failure"; +static const char* kStateActivationFailure = "activation-failure"; + +// Connman activation state options +static const char* kActivationStateActivated = "activated"; +static const char* kActivationStateActivating = "activating"; +static const char* kActivationStateNotActivated = "not-activated"; +static const char* kActivationStatePartiallyActivated = "partially-activated"; +static const char* kActivationStateUnknown = "unknown"; + +// Connman network technology options. +static const char* kNetworkTechnology1Xrtt = "1xRTT"; +static const char* kNetworkTechnologyEvdo = "EVDO"; +static const char* kNetworkTechnologyGprs = "GPRS"; +static const char* kNetworkTechnologyEdge = "EDGE"; +static const char* kNetworkTechnologyUmts = "UMTS"; +static const char* kNetworkTechnologyHspa = "HSPA"; +static const char* kNetworkTechnologyHspaPlus = "HSPA+"; +static const char* kNetworkTechnologyLte = "LTE"; +static const char* kNetworkTechnologyLteAdvanced = "LTE Advanced"; + +// Connman roaming state options +static const char* kRoamingStateHome = "home"; +static const char* kRoamingStateRoaming = "roaming"; +static const char* kRoamingStateUnknown = "unknown"; + +static ConnectionState ParseState(const std::string& state) { + if (state == kStateIdle) + return STATE_IDLE; + if (state == kStateCarrier) + return STATE_CARRIER; + if (state == kStateAssociation) + return STATE_ASSOCIATION; + if (state == kStateConfiguration) + return STATE_CONFIGURATION; + if (state == kStateReady) + return STATE_READY; + if (state == kStateDisconnect) + return STATE_DISCONNECT; + if (state == kStateFailure) + return STATE_FAILURE; + if (state == kStateActivationFailure) + return STATE_ACTIVATION_FAILURE; + return STATE_UNKNOWN; +} + +static ActivationState ParseActivationState( + const std::string& activation_state) { + if (activation_state == kActivationStateActivated) + return ACTIVATION_STATE_ACTIVATED; + if (activation_state == kActivationStateActivating) + return ACTIVATION_STATE_ACTIVATING; + if (activation_state == kActivationStateNotActivated) + return ACTIVATION_STATE_NOT_ACTIVATED; + if (activation_state == kActivationStateUnknown) + return ACTIVATION_STATE_UNKNOWN; + if (activation_state == kActivationStatePartiallyActivated) + return ACTIVATION_STATE_PARTIALLY_ACTIVATED; + return ACTIVATION_STATE_UNKNOWN; +} + +static NetworkTechnology ParseNetworkTechnology( + const std::string& technology) { + if (technology == kNetworkTechnology1Xrtt) + return NETWORK_TECHNOLOGY_1XRTT; + if (technology == kNetworkTechnologyEvdo) + return NETWORK_TECHNOLOGY_EVDO; + if (technology == kNetworkTechnologyGprs) + return NETWORK_TECHNOLOGY_GPRS; + if (technology == kNetworkTechnologyEdge) + return NETWORK_TECHNOLOGY_EDGE; + if (technology == kNetworkTechnologyUmts) + return NETWORK_TECHNOLOGY_UMTS; + if (technology == kNetworkTechnologyHspa) + return NETWORK_TECHNOLOGY_HSPA; + if (technology == kNetworkTechnologyHspaPlus) + return NETWORK_TECHNOLOGY_HSPA_PLUS; + if (technology == kNetworkTechnologyLte) + return NETWORK_TECHNOLOGY_LTE; + if (technology == kNetworkTechnologyLteAdvanced) + return NETWORK_TECHNOLOGY_LTE_ADVANCED; + return NETWORK_TECHNOLOGY_UNKNOWN; +} +static NetworkRoamingState ParseRoamingState( + const std::string& roaming_state) { + if (roaming_state == kRoamingStateHome) + return ROAMING_STATE_HOME; + if (roaming_state == kRoamingStateRoaming) + return ROAMING_STATE_ROAMING; + if (roaming_state == kRoamingStateUnknown) + return ROAMING_STATE_UNKNOWN; + return ROAMING_STATE_UNKNOWN; +} + +} + // Helper function to wrap Html with <th> tag. static std::string WrapWithTH(std::string text) { return "<th>" + text + "</th>"; @@ -99,6 +217,7 @@ void Network::Clear() { service_path_.clear(); device_path_.clear(); ip_address_.clear(); + is_active_ = false; } Network::Network(const ServiceInfo* service) { @@ -107,6 +226,7 @@ Network::Network(const ServiceInfo* service) { error_ = service->error; service_path_ = SafeString(service->service_path); device_path_ = SafeString(service->device_path); + is_active_ = service->is_active; ip_address_.clear(); // If connected, get ip config. if (EnsureCrosLoaded() && connected() && service->device_path) { @@ -510,7 +630,7 @@ bool WifiNetwork::IsCertificateLoaded() const { class NetworkLibraryImpl : public NetworkLibrary { public: NetworkLibraryImpl() - : network_status_connection_(NULL), + : network_manager_monitor_(NULL), data_plan_monitor_(NULL), ethernet_(NULL), wifi_(NULL), @@ -521,68 +641,98 @@ class NetworkLibraryImpl : public NetworkLibrary { offline_mode_(false) { if (EnsureCrosLoaded()) { Init(); + network_manager_monitor_ = + MonitorNetworkManager(&NetworkManagerStatusChangedHandler, + this); + data_plan_monitor_ = MonitorCellularDataPlan(&DataPlanUpdateHandler, + this); } else { InitTestData(); } } ~NetworkLibraryImpl() { - if (network_status_connection_) { - DisconnectMonitorNetwork(network_status_connection_); - } - if (data_plan_monitor_) { - DisconnectDataPlanUpdateMonitor(data_plan_monitor_); - } - // DCHECK(!observers_.size()); - DCHECK(!property_observers_.size()); - STLDeleteValues(&property_observers_); + DCHECK(!network_manager_observers_.size()); + DCHECK(network_manager_monitor_); + DisconnectPropertyChangeMonitor(network_manager_monitor_); + DCHECK(!data_plan_observers_.size()); + DCHECK(data_plan_monitor_); + DisconnectDataPlanUpdateMonitor(data_plan_monitor_); + DCHECK(!network_observers_.size()); + STLDeleteValues(&network_observers_); ClearNetworks(); } - void AddObserver(Observer* observer) { - observers_.AddObserver(observer); + virtual void AddNetworkManagerObserver(NetworkManagerObserver* observer) { + network_manager_observers_.AddObserver(observer); } - void RemoveObserver(Observer* observer) { - observers_.RemoveObserver(observer); + virtual void RemoveNetworkManagerObserver(NetworkManagerObserver* observer) { + if (!network_manager_observers_.HasObserver(observer)) + network_manager_observers_.RemoveObserver(observer); } - virtual void AddProperyObserver(const char* service_path, - PropertyObserver* observer) { - DCHECK(service_path); + virtual void AddNetworkObserver(const std::string& service_path, + NetworkObserver* observer) { DCHECK(observer); if (!EnsureCrosLoaded()) return; // First, add the observer to the callback map. - PropertyChangeObserverMap::iterator iter = property_observers_.find( - std::string(service_path)); - if (iter != property_observers_.end()) { - iter->second->AddObserver(observer); + NetworkObserverMap::iterator iter = network_observers_.find(service_path); + NetworkObserverList* oblist; + if (iter != network_observers_.end()) { + oblist = iter->second; } else { - std::pair<PropertyChangeObserverMap::iterator, bool> inserted = - property_observers_.insert( - std::pair<std::string, PropertyObserverList*>( - std::string(service_path), - new PropertyObserverList(this, service_path))); - inserted.first->second->AddObserver(observer); + std::pair<NetworkObserverMap::iterator, bool> inserted = + network_observers_.insert( + std::make_pair<std::string, NetworkObserverList*>( + service_path, + new NetworkObserverList(this, service_path))); + oblist = inserted.first->second; + } + if (!oblist->HasObserver(observer)) + oblist->AddObserver(observer); + } + + virtual void RemoveNetworkObserver(const std::string& service_path, + NetworkObserver* observer) { + DCHECK(observer); + DCHECK(service_path.size()); + NetworkObserverMap::iterator map_iter = network_observers_.find( + service_path); + if (map_iter != network_observers_.end()) { + map_iter->second->RemoveObserver(observer); + if (!map_iter->second->size()) { + delete map_iter->second; + network_observers_.erase(map_iter++); + } } } - virtual void RemoveProperyObserver(PropertyObserver* observer) { + virtual void RemoveObserverForAllNetworks(NetworkObserver* observer) { DCHECK(observer); - PropertyChangeObserverMap::iterator map_iter = - property_observers_.begin(); - while (map_iter != property_observers_.end()) { + NetworkObserverMap::iterator map_iter = network_observers_.begin(); + while (map_iter != network_observers_.end()) { map_iter->second->RemoveObserver(observer); if (!map_iter->second->size()) { delete map_iter->second; - property_observers_.erase(map_iter++); + network_observers_.erase(map_iter++); } else { ++map_iter; } } } + virtual void AddCellularDataPlanObserver(CellularDataPlanObserver* observer) { + if (!data_plan_observers_.HasObserver(observer)) + data_plan_observers_.AddObserver(observer); + } + + virtual void RemoveCellularDataPlanObserver( + CellularDataPlanObserver* observer) { + data_plan_observers_.RemoveObserver(observer); + } + virtual EthernetNetwork* ethernet_network() { return ethernet_; } virtual bool ethernet_connecting() const { return ethernet_ ? ethernet_->connecting() : false; @@ -616,14 +766,14 @@ class NetworkLibraryImpl : public NetworkLibrary { } const std::string& IPAddress() const { - // Returns highest priority IP address. - if (ethernet_connected()) + // Returns IP address for the active network. + const Network* active = active_network(); + if (active != NULL) + return active->ip_address(); + if (ethernet_) return ethernet_->ip_address(); - if (wifi_connected()) - return wifi_->ip_address(); - if (cellular_connected()) - return cellular_->ip_address(); - return ethernet_->ip_address(); + static std::string null_address("0.0.0.0"); + return null_address; } virtual const WifiNetworkVector& wifi_networks() const { @@ -688,6 +838,7 @@ class NetworkLibraryImpl : public NetworkLibrary { DCHECK(network); if (!EnsureCrosLoaded()) return; + // TODO(ers) make wifi the highest priority service type if (ConnectToNetworkWithCertInfo(network->service_path().c_str(), password.empty() ? NULL : password.c_str(), identity.empty() ? NULL : identity.c_str(), @@ -702,7 +853,7 @@ class NetworkLibraryImpl : public NetworkLibrary { wifi->set_connecting(true); wifi_ = wifi; } - NotifyNetworkChanged(); + NotifyNetworkManagerChanged(); } } @@ -721,6 +872,7 @@ class NetworkLibraryImpl : public NetworkLibrary { // Set auto-connect. SetAutoConnect(service->service_path, auto_connect); // Now connect to that service. + // TODO(ers) make wifi the highest priority service type ConnectToNetworkWithCertInfo(service->service_path, password.empty() ? NULL : password.c_str(), identity.empty() ? NULL : identity.c_str(), @@ -738,6 +890,7 @@ class NetworkLibraryImpl : public NetworkLibrary { DCHECK(network); if (!EnsureCrosLoaded()) return; + // TODO(ers) make cellular the highest priority service type if (network && ConnectToNetwork(network->service_path().c_str(), NULL)) { // Update local cache and notify listeners. CellularNetwork* cellular = GetWirelessNetworkByPath( @@ -746,7 +899,7 @@ class NetworkLibraryImpl : public NetworkLibrary { cellular->set_connecting(true); cellular_ = cellular; } - NotifyNetworkChanged(); + NotifyNetworkManagerChanged(); } } @@ -761,6 +914,7 @@ class NetworkLibraryImpl : public NetworkLibrary { DCHECK(network); if (!EnsureCrosLoaded() || !network) return; + // TODO(ers) restore default service type priority ordering? if (DisconnectFromNetwork(network->service_path().c_str())) { // Update local cache and notify listeners. if (network->type() == TYPE_WIFI) { @@ -778,7 +932,7 @@ class NetworkLibraryImpl : public NetworkLibrary { cellular_ = NULL; } } - NotifyNetworkChanged(); + NotifyNetworkManagerChanged(); } } @@ -820,7 +974,7 @@ class NetworkLibraryImpl : public NetworkLibrary { break; } } - NotifyNetworkChanged(); + NotifyNetworkManagerChanged(); } } @@ -846,6 +1000,16 @@ class NetworkLibraryImpl : public NetworkLibrary { virtual bool offline_mode() const { return offline_mode_; } + virtual const Network* active_network() const { + if (ethernet_ && ethernet_->is_active()) + return ethernet_; + if (wifi_ && wifi_->is_active()) + return wifi_; + if (cellular_ && cellular_->is_active()) + return cellular_; + return NULL; + } + virtual void EnableEthernetNetworkDevice(bool enable) { EnableNetworkDeviceType(TYPE_ETHERNET, enable); } @@ -951,52 +1115,53 @@ class NetworkLibraryImpl : public NetworkLibrary { private: - class PropertyObserverList : public ObserverList<PropertyObserver> { + class NetworkObserverList : public ObserverList<NetworkObserver> { public: - PropertyObserverList(NetworkLibraryImpl* library, - const char* service_path) { - DCHECK(service_path); - property_change_monitor_ = MonitorNetworkService(&PropertyChangeHandler, - service_path, - library); + NetworkObserverList(NetworkLibraryImpl* library, + const std::string& service_path) { + network_monitor_ = MonitorNetworkService(&NetworkStatusChangedHandler, + service_path.c_str(), + library); } - virtual ~PropertyObserverList() { - if (property_change_monitor_) - DisconnectPropertyChangeMonitor(property_change_monitor_); + virtual ~NetworkObserverList() { + if (network_monitor_) + DisconnectPropertyChangeMonitor(network_monitor_); } private: - static void PropertyChangeHandler(void* object, - const char* path, - const char* key, - const Value* value) { - NetworkLibraryImpl* network = static_cast<NetworkLibraryImpl*>(object); - DCHECK(network); - network->NotifyPropertyChange(path, key, value); + static void NetworkStatusChangedHandler(void* object, + const char* path, + const char* key, + const Value* value) { + NetworkLibraryImpl* networklib = static_cast<NetworkLibraryImpl*>(object); + DCHECK(networklib); + networklib->UpdateNetworkStatus(path, key, value); } - PropertyChangeMonitor property_change_monitor_; + PropertyChangeMonitor network_monitor_; }; - typedef std::map<std::string, PropertyObserverList*> - PropertyChangeObserverMap; + typedef std::map<std::string, NetworkObserverList*> NetworkObserverMap; - static void NetworkStatusChangedHandler(void* object) { - NetworkLibraryImpl* network = static_cast<NetworkLibraryImpl*>(object); - DCHECK(network); - network->UpdateNetworkStatus(); + static void NetworkManagerStatusChangedHandler(void* object, + const char* path, + const char* key, + const Value* value) { + NetworkLibraryImpl* networklib = static_cast<NetworkLibraryImpl*>(object); + DCHECK(networklib); + networklib->UpdateNetworkManagerStatus(); } static void DataPlanUpdateHandler(void* object, const char* modem_service_path, const CellularDataPlanList* dataplan) { - NetworkLibraryImpl* network = static_cast<NetworkLibraryImpl*>(object); - DCHECK(network && network->cellular_network()); + NetworkLibraryImpl* networklib = static_cast<NetworkLibraryImpl*>(object); + DCHECK(networklib && networklib->cellular_network()); // Store data plan for currently connected cellular network. - if (network->cellular_network()->service_path() + if (networklib->cellular_network()->service_path() .compare(modem_service_path) == 0) { if (dataplan != NULL) { - network->UpdateCellularDataPlan(*dataplan); + networklib->UpdateCellularDataPlan(*dataplan); } } } @@ -1021,6 +1186,7 @@ class NetworkLibraryImpl : public NetworkLibrary { << " str=" << service->strength << " fav=" << service->favorite << " auto=" << service->auto_connect + << " is_active=" << service->is_active << " error=" << service->error; // Once a connected ethernet service is found, disregard other ethernet // services that are also found @@ -1060,17 +1226,10 @@ class NetworkLibraryImpl : public NetworkLibrary { } void Init() { - // First, get the currently available networks. This data is cached + // First, get the currently available networks. This data is cached // on the connman side, so the call should be quick. VLOG(1) << "Getting initial CrOS network info."; UpdateSystemInfo(); - - VLOG(1) << "Registering for network status updates."; - // Now, register to receive updates on network status. - network_status_connection_ = MonitorNetwork(&NetworkStatusChangedHandler, - this); - VLOG(1) << "Registering for cellular data plan updates."; - data_plan_monitor_ = MonitorCellularDataPlan(&DataPlanUpdateHandler, this); } void InitTestData() { @@ -1149,7 +1308,7 @@ class NetworkLibraryImpl : public NetworkLibrary { void UpdateSystemInfo() { if (EnsureCrosLoaded()) { - UpdateNetworkStatus(); + UpdateNetworkManagerStatus(); } } @@ -1198,53 +1357,40 @@ class NetworkLibraryImpl : public NetworkLibrary { EnableNetworkDevice(device, enable); } - void NotifyNetworkChanged() { - FOR_EACH_OBSERVER(Observer, observers_, NetworkChanged(this)); + void NotifyNetworkManagerChanged() { + FOR_EACH_OBSERVER(NetworkManagerObserver, + network_manager_observers_, + OnNetworkManagerChanged(this)); } - void NotifyCellularDataPlanChanged() { - FOR_EACH_OBSERVER(Observer, observers_, CellularDataPlanChanged(this)); - } - - void NotifyPropertyChange(const char* service_path, - const char* key, - const Value* value) { - DCHECK(service_path); - DCHECK(key); - DCHECK(value); - PropertyChangeObserverMap::const_iterator iter = property_observers_.find( - std::string(service_path)); - if (iter != property_observers_.end()) { - FOR_EACH_OBSERVER(PropertyObserver, *(iter->second), - PropertyChanged(service_path, key, value)); + void NotifyNetworkChanged(Network* network) { + DCHECK(network); + NetworkObserverMap::const_iterator iter = network_observers_.find( + network->service_path()); + if (iter != network_observers_.end()) { + FOR_EACH_OBSERVER(NetworkObserver, + *(iter->second), + OnNetworkChanged(this, network)); } else { NOTREACHED() << "There weren't supposed to be any property change observers of " << - service_path; + network->service_path(); } } - void ClearNetworks() { - if (ethernet_) - delete ethernet_; - ethernet_ = NULL; - wifi_ = NULL; - cellular_ = NULL; - STLDeleteElements(&wifi_networks_); - wifi_networks_.clear(); - STLDeleteElements(&cellular_networks_); - cellular_networks_.clear(); - STLDeleteElements(&remembered_wifi_networks_); - remembered_wifi_networks_.clear(); + void NotifyCellularDataPlanChanged() { + FOR_EACH_OBSERVER(CellularDataPlanObserver, + data_plan_observers_, + OnCellularDataPlanChanged(this)); } - void UpdateNetworkStatus() { + void UpdateNetworkManagerStatus() { // Make sure we run on UI thread. if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { BrowserThread::PostTask( BrowserThread::UI, FROM_HERE, NewRunnableMethod(this, - &NetworkLibraryImpl::UpdateNetworkStatus)); + &NetworkLibraryImpl::UpdateNetworkManagerStatus)); return; } @@ -1288,30 +1434,114 @@ class NetworkLibraryImpl : public NetworkLibrary { connected_devices_ = system->connected_technologies; offline_mode_ = system->offline_mode; - NotifyNetworkChanged(); + NotifyNetworkManagerChanged(); FreeSystemInfo(system); } + void UpdateNetworkStatus(const char* path, + const char* key, + const Value* value) { + if (key == NULL || value == NULL) + return; + // Make sure we run on UI thread. + if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { + BrowserThread::PostTask( + BrowserThread::UI, FROM_HERE, + NewRunnableMethod(this, + &NetworkLibraryImpl::UpdateNetworkStatus, + path, key, value)); + return; + } + + bool boolval = false; + int intval = 0; + std::string stringval; + Network* network; + if (ethernet_->service_path() == path) { + network = ethernet_; + } else { + CellularNetwork* cellular = + GetWirelessNetworkByPath(cellular_networks_, path); + WifiNetwork* wifi = + GetWirelessNetworkByPath(wifi_networks_, path); + if (cellular == NULL && wifi == NULL) + return; + + WirelessNetwork* wireless; + if (wifi != NULL) + wireless = static_cast<WirelessNetwork*>(wifi); + else + wireless = static_cast<WirelessNetwork*>(cellular); + + if (strcmp(key, kSignalStrengthProperty) == 0) { + if (value->GetAsInteger(&intval)) + wireless->set_strength(intval); + } else if (cellular != NULL) { + if (strcmp(key, kRestrictedPoolProperty) == 0) { + if (value->GetAsBoolean(&boolval)) + cellular->set_restricted_pool(boolval); + } else if (strcmp(key, kActivationStateProperty) == 0) { + if (value->GetAsString(&stringval)) + cellular->set_activation_state(ParseActivationState(stringval)); + } else if (strcmp(key, kPaymentURLProperty) == 0) { + if (value->GetAsString(&stringval)) + cellular->set_payment_url(stringval); + } else if (strcmp(key, kNetworkTechnologyProperty) == 0) { + if (value->GetAsString(&stringval)) + cellular->set_network_technology( + ParseNetworkTechnology(stringval)); + } else if (strcmp(key, kRoamingStateProperty) == 0) { + if (value->GetAsString(&stringval)) + cellular->set_roaming_state(ParseRoamingState(stringval)); + } + } + network = wireless; + } + if (strcmp(key, kIsActiveProperty) == 0) { + if (value->GetAsBoolean(&boolval)) + network->set_active(boolval); + } else if (strcmp(key, kStateProperty) == 0) { + if (value->GetAsString(&stringval)) + network->set_state(ParseState(stringval)); + } + NotifyNetworkChanged(network); + } + void UpdateCellularDataPlan(const CellularDataPlanList& data_plans) { DCHECK(cellular_); cellular_->SetDataPlans(data_plans); NotifyCellularDataPlanChanged(); } - ObserverList<Observer> observers_; + void ClearNetworks() { + if (ethernet_) + delete ethernet_; + ethernet_ = NULL; + wifi_ = NULL; + cellular_ = NULL; + STLDeleteElements(&wifi_networks_); + wifi_networks_.clear(); + STLDeleteElements(&cellular_networks_); + cellular_networks_.clear(); + STLDeleteElements(&remembered_wifi_networks_); + remembered_wifi_networks_.clear(); + } + + // Network manager observer list + ObserverList<NetworkManagerObserver> network_manager_observers_; - // Property change observer map - PropertyChangeObserverMap property_observers_; + // Cellular data plan observer list + ObserverList<CellularDataPlanObserver> data_plan_observers_; - // The network status connection for monitoring network status changes. - MonitorNetworkConnection network_status_connection_; + // Network observer map + NetworkObserverMap network_observers_; + + // For monitoring network manager status changes. + PropertyChangeMonitor network_manager_monitor_; // For monitoring data plan changes to the connected cellular network. DataPlanUpdateMonitor data_plan_monitor_; - // The property change connection for monitoring service property changes. - std::map<std::string, PropertyChangeMonitor> property_change_monitors_; - // The ethernet network. EthernetNetwork* ethernet_; @@ -1353,11 +1583,17 @@ class NetworkLibraryStubImpl : public NetworkLibrary { cellular_(NULL) { } ~NetworkLibraryStubImpl() { if (ethernet_) delete ethernet_; } - virtual void AddObserver(Observer* observer) {} - virtual void RemoveObserver(Observer* observer) {} - virtual void AddProperyObserver(const char* service_path, - PropertyObserver* observer) {} - virtual void RemoveProperyObserver(PropertyObserver* observer) {} + virtual void AddNetworkManagerObserver(NetworkManagerObserver* observer) {} + virtual void RemoveNetworkManagerObserver(NetworkManagerObserver* observer) {} + virtual void AddNetworkObserver(const std::string& service_path, + NetworkObserver* observer) {} + virtual void RemoveNetworkObserver(const std::string& service_path, + NetworkObserver* observer) {} + virtual void RemoveObserverForAllNetworks(NetworkObserver* observer) {} + virtual void AddCellularDataPlanObserver( + CellularDataPlanObserver* observer) {} + virtual void RemoveCellularDataPlanObserver( + CellularDataPlanObserver* observer) {} virtual EthernetNetwork* ethernet_network() { return ethernet_; } @@ -1422,6 +1658,7 @@ class NetworkLibraryStubImpl : public NetworkLibrary { virtual bool ethernet_enabled() const { return true; } virtual bool wifi_enabled() const { return false; } virtual bool cellular_enabled() const { return false; } + virtual const Network* active_network() const { return NULL; } virtual bool offline_mode() const { return false; } virtual void EnableEthernetNetworkDevice(bool enable) {} virtual void EnableWifiNetworkDevice(bool enable) {} diff --git a/chrome/browser/chromeos/cros/network_library.h b/chrome/browser/chromeos/cros/network_library.h index 65574ed..1e48fc7 100644 --- a/chrome/browser/chromeos/cros/network_library.h +++ b/chrome/browser/chromeos/cros/network_library.h @@ -48,13 +48,10 @@ class Network { state_ == STATE_IDLE; } ConnectionError error() const { return error_; } ConnectionState state() const { return state_; } - - void set_service_path(const std::string& service_path) { - service_path_ = service_path; } - void set_connecting(bool connecting) { state_ = (connecting ? - STATE_ASSOCIATION : STATE_IDLE); } - void set_connected(bool connected) { state_ = (connected ? - STATE_READY : STATE_IDLE); } + // Is this the active network, i.e, the one through which + // network traffic is being routed? A network can be connected, + // but not be carrying traffic. + bool is_active() const { return is_active_; } // Clear the fields. virtual void Clear(); @@ -69,7 +66,8 @@ class Network { Network() : type_(TYPE_UNKNOWN), state_(STATE_UNKNOWN), - error_(ERROR_UNKNOWN) {} + error_(ERROR_UNKNOWN), + is_active_(false) {} explicit Network(const Network& network); explicit Network(const ServiceInfo* service); virtual ~Network() {} @@ -80,6 +78,19 @@ class Network { ConnectionType type_; ConnectionState state_; ConnectionError error_; + bool is_active_; + + private: + void set_service_path(const std::string& service_path) { + service_path_ = service_path; } + void set_connecting(bool connecting) { state_ = (connecting ? + STATE_ASSOCIATION : STATE_IDLE); } + void set_connected(bool connected) { state_ = (connected ? + STATE_READY : STATE_IDLE); } + void set_state(ConnectionState state) { state_ = state; } + void set_active(bool is_active) { is_active_ = is_active; } + + friend class NetworkLibraryImpl; }; class EthernetNetwork : public Network { @@ -119,8 +130,6 @@ class WirelessNetwork : public Network { bool auto_connect() const { return auto_connect_; } bool favorite() const { return favorite_; } - void set_name(const std::string& name) { name_ = name; } - void set_strength(int strength) { strength_ = strength; } void set_auto_connect(bool auto_connect) { auto_connect_ = auto_connect; } void set_favorite(bool favorite) { favorite_ = favorite; } @@ -140,6 +149,12 @@ class WirelessNetwork : public Network { int strength_; bool auto_connect_; bool favorite_; + + private: + void set_name(const std::string& name) { name_ = name; } + void set_strength(int strength) { strength_ = strength; } + + friend class NetworkLibraryImpl; }; class CellularNetwork : public WirelessNetwork { @@ -158,9 +173,6 @@ class CellularNetwork : public WirelessNetwork { // Starts device activation process. Returns false if the device state does // not permit activation. bool StartActivation() const; - void set_activation_state(ActivationState state) { - activation_state_ = state; - } const ActivationState activation_state() const { return activation_state_; } const NetworkTechnology network_technology() const { return network_technology_; @@ -171,9 +183,6 @@ class CellularNetwork : public WirelessNetwork { const std::string& operator_name() const { return operator_name_; } const std::string& operator_code() const { return operator_code_; } const std::string& payment_url() const { return payment_url_; } - void set_payment_url(const std::string& url) { - payment_url_ = url; - } const std::string& meid() const { return meid_; } const std::string& imei() const { return imei_; } const std::string& imsi() const { return imsi_; } @@ -234,6 +243,25 @@ class CellularNetwork : public WirelessNetwork { std::string last_update_; unsigned int prl_version_; CellularDataPlanList data_plans_; + + private: + void set_activation_state(ActivationState state) { + activation_state_ = state; + } + void set_payment_url(const std::string& url) { + payment_url_ = url; + } + void set_network_technology(NetworkTechnology technology) { + network_technology_ = technology; + } + void set_roaming_state(NetworkRoamingState state) { + roaming_state_ = state; + } + void set_restricted_pool(bool restricted_pool) { + restricted_pool_ = restricted_pool; + } + + friend class NetworkLibraryImpl; }; class WifiNetwork : public WirelessNetwork { @@ -339,27 +367,47 @@ typedef std::vector<NetworkIPConfig> NetworkIPConfigVector; // library like this: chromeos::CrosLibrary::Get()->GetNetworkLibrary() class NetworkLibrary { public: - class Observer { + class NetworkManagerObserver { public: - // Called when the network has changed. (wifi networks, and ethernet) - virtual void NetworkChanged(NetworkLibrary* obj) = 0; - // Called when the cellular data plan has changed. - virtual void CellularDataPlanChanged(NetworkLibrary* obj) {} + // Called when the state of the network manager has changed, + // for example, networks have appeared or disappeared. + virtual void OnNetworkManagerChanged(NetworkLibrary* obj) = 0; }; - class PropertyObserver { + class NetworkObserver { public: - virtual void PropertyChanged(const char* service_path, - const char* key, - const Value* value) = 0; + // Called when the state of a single network has changed, + // for example signal strength or connection state. + virtual void OnNetworkChanged(NetworkLibrary* cros, + const Network* network) = 0; + }; + + class CellularDataPlanObserver { + public: + // Called when the cellular data plan has changed. + virtual void OnCellularDataPlanChanged(NetworkLibrary* obj) = 0; }; virtual ~NetworkLibrary() {} - virtual void AddObserver(Observer* observer) = 0; - virtual void RemoveObserver(Observer* observer) = 0; - virtual void AddProperyObserver(const char* service_path, - PropertyObserver* observer) = 0; - virtual void RemoveProperyObserver(PropertyObserver* observer) = 0; + + virtual void AddNetworkManagerObserver(NetworkManagerObserver* observer) = 0; + virtual void RemoveNetworkManagerObserver( + NetworkManagerObserver* observer) = 0; + + // An attempt to add an observer that has already been added for a + // give service path will be ignored. + virtual void AddNetworkObserver(const std::string& service_path, + NetworkObserver* observer) = 0; + // Remove an observer of a single network + virtual void RemoveNetworkObserver(const std::string& service_path, + NetworkObserver* observer) = 0; + // Stop |observer| from observing any networks + virtual void RemoveObserverForAllNetworks(NetworkObserver* observer) = 0; + + virtual void AddCellularDataPlanObserver( + CellularDataPlanObserver* observer) = 0; + virtual void RemoveCellularDataPlanObserver( + CellularDataPlanObserver* observer) = 0; // Return the active Ethernet network (or a default structure if inactive). virtual EthernetNetwork* ethernet_network() = 0; @@ -457,6 +505,8 @@ class NetworkLibrary { virtual bool wifi_enabled() const = 0; virtual bool cellular_enabled() const = 0; + virtual const Network* active_network() const = 0; + virtual bool offline_mode() const = 0; // Enables/disables the ethernet network device. diff --git a/chrome/browser/chromeos/dom_ui/internet_options_handler.cc b/chrome/browser/chromeos/dom_ui/internet_options_handler.cc index 47541a3..a4ab56f 100644 --- a/chrome/browser/chromeos/dom_ui/internet_options_handler.cc +++ b/chrome/browser/chromeos/dom_ui/internet_options_handler.cc @@ -56,11 +56,19 @@ std::string FormatHardwareAddress(const std::string& address) { } // namespace InternetOptionsHandler::InternetOptionsHandler() { - chromeos::CrosLibrary::Get()->GetNetworkLibrary()->AddObserver(this); + chromeos::NetworkLibrary* netlib = + chromeos::CrosLibrary::Get()->GetNetworkLibrary(); + netlib->AddNetworkManagerObserver(this); + netlib->AddCellularDataPlanObserver(this); + MonitorActiveNetwork(netlib); } InternetOptionsHandler::~InternetOptionsHandler() { - chromeos::CrosLibrary::Get()->GetNetworkLibrary()->RemoveObserver(this); + chromeos::NetworkLibrary *netlib = + chromeos::CrosLibrary::Get()->GetNetworkLibrary(); + netlib->RemoveNetworkManagerObserver(this); + netlib->RemoveCellularDataPlanObserver(this); + netlib->RemoveObserverForAllNetworks(this); } void InternetOptionsHandler::GetLocalizedValues( @@ -357,10 +365,8 @@ void InternetOptionsHandler::BuyDataPlanCallback(const ListValue* args) { browser->OpenMobilePlanTabAndActivate(); } -void InternetOptionsHandler::NetworkChanged(chromeos::NetworkLibrary* cros) { - if (!dom_ui_) - return; - +void InternetOptionsHandler::RefreshNetworkData( + chromeos::NetworkLibrary* cros) { DictionaryValue dictionary; dictionary.Set("wiredList", GetWiredList()); dictionary.Set("wirelessList", GetWirelessList()); @@ -373,7 +379,45 @@ void InternetOptionsHandler::NetworkChanged(chromeos::NetworkLibrary* cros) { L"options.InternetOptions.refreshNetworkData", dictionary); } -void InternetOptionsHandler::CellularDataPlanChanged( +void InternetOptionsHandler::OnNetworkManagerChanged( + chromeos::NetworkLibrary* cros) { + if (!dom_ui_) + return; + MonitorActiveNetwork(cros); + RefreshNetworkData(cros); +} + +void InternetOptionsHandler::OnNetworkChanged( + chromeos::NetworkLibrary* cros, + const chromeos::Network* network) { + if (dom_ui_) + RefreshNetworkData(cros); +} + +// Add an observer for the active network, if any, so +// that we can dynamically display the correct icon for +// that network's signal strength. +// TODO(ers) Ideally, on this page we'd monitor all networks for +// signal strength changes, not just the active network. +void InternetOptionsHandler::MonitorActiveNetwork( + chromeos::NetworkLibrary* cros) { + const chromeos::Network* network = cros->active_network(); + if (active_network_.empty() || network == NULL || + active_network_ != network->service_path()) { + if (!active_network_.empty()) { + cros->RemoveNetworkObserver(active_network_, this); + } + if (network != NULL) { + cros->AddNetworkObserver(network->service_path(), this); + } + } + if (network != NULL) + active_network_ = network->service_path(); + else + active_network_ = ""; +} + +void InternetOptionsHandler::OnCellularDataPlanChanged( chromeos::NetworkLibrary* obj) { if (!dom_ui_) return; @@ -950,7 +994,7 @@ ListValue* InternetOptionsHandler::GetWirelessList() { cellular_networks.begin(); it != cellular_networks.end(); ++it) { SkBitmap icon = chromeos::NetworkMenu::IconForNetworkStrength( (*it)->strength(), true); - SkBitmap badge = *rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_3G); + SkBitmap badge = chromeos::NetworkMenu::BadgeForNetworkTechnology(*it); icon = chromeos::NetworkMenu::IconForDisplay(icon, badge); list->Append(GetNetwork( (*it)->service_path(), diff --git a/chrome/browser/chromeos/dom_ui/internet_options_handler.h b/chrome/browser/chromeos/dom_ui/internet_options_handler.h index 99dd522..fcecd82 100644 --- a/chrome/browser/chromeos/dom_ui/internet_options_handler.h +++ b/chrome/browser/chromeos/dom_ui/internet_options_handler.h @@ -16,8 +16,12 @@ class WindowDelegate; } // ChromeOS internet options page UI handler. -class InternetOptionsHandler : public OptionsPageUIHandler, - public chromeos::NetworkLibrary::Observer { +class InternetOptionsHandler + : public OptionsPageUIHandler, + public chromeos::NetworkLibrary::NetworkManagerObserver, + public chromeos::NetworkLibrary::NetworkObserver, + public chromeos::NetworkLibrary::CellularDataPlanObserver { + public: InternetOptionsHandler(); virtual ~InternetOptionsHandler(); @@ -28,9 +32,13 @@ class InternetOptionsHandler : public OptionsPageUIHandler, // DOMMessageHandler implementation. virtual void RegisterMessages(); - // NetworkLibrary::Observer implementation. - virtual void NetworkChanged(chromeos::NetworkLibrary* obj); - virtual void CellularDataPlanChanged(chromeos::NetworkLibrary* obj); + // NetworkLibrary::NetworkManagerObserver implementation. + virtual void OnNetworkManagerChanged(chromeos::NetworkLibrary* network_lib); + // NetworkLibrary::NetworkObserver implementation. + virtual void OnNetworkChanged(chromeos::NetworkLibrary* network_lib, + const chromeos::Network* network); + // NetworkLibrary::CellularDataPlanObserver implementation. + virtual void OnCellularDataPlanChanged(chromeos::NetworkLibrary* network_lib); private: // Passes data needed to show details overlay for network. @@ -81,6 +89,13 @@ class InternetOptionsHandler : public OptionsPageUIHandler, ListValue* GetWirelessList(); // Creates the map of remembered networks ListValue* GetRememberedList(); + // Refresh the display of network information + void RefreshNetworkData(chromeos::NetworkLibrary* cros); + // Monitor the active network, if any + void MonitorActiveNetwork(chromeos::NetworkLibrary* cros); + + // If any network is currently active, this is its service path + std::string active_network_; DISALLOW_COPY_AND_ASSIGN(InternetOptionsHandler); }; diff --git a/chrome/browser/chromeos/dom_ui/mobile_setup_ui.cc b/chrome/browser/chromeos/dom_ui/mobile_setup_ui.cc index 0e2d734..048f053 100644 --- a/chrome/browser/chromeos/dom_ui/mobile_setup_ui.cc +++ b/chrome/browser/chromeos/dom_ui/mobile_setup_ui.cc @@ -124,10 +124,12 @@ class MobileSetupUIHTMLSource : public ChromeURLDataManager::DataSource { }; // The handler for Javascript messages related to the "register" view. -class MobileSetupHandler : public DOMMessageHandler, - public chromeos::NetworkLibrary::Observer, - public chromeos::NetworkLibrary::PropertyObserver, - public base::SupportsWeakPtr<MobileSetupHandler> { +class MobileSetupHandler + : public DOMMessageHandler, + public chromeos::NetworkLibrary::NetworkManagerObserver, + public chromeos::NetworkLibrary::NetworkObserver, + public base::SupportsWeakPtr<MobileSetupHandler> { + public: explicit MobileSetupHandler(const std::string& service_path); virtual ~MobileSetupHandler(); @@ -139,12 +141,11 @@ class MobileSetupHandler : public DOMMessageHandler, virtual DOMMessageHandler* Attach(DOMUI* dom_ui); virtual void RegisterMessages(); - // NetworkLibrary::Observer implementation. - virtual void NetworkChanged(chromeos::NetworkLibrary* obj); - // NetworkLibrary::PropertyObserver implementation. - virtual void PropertyChanged(const char* service_path, - const char* key, - const Value* value); + // NetworkLibrary::NetworkManagerObserver implementation. + virtual void OnNetworkManagerChanged(chromeos::NetworkLibrary* obj); + // NetworkLibrary::NetworkObserver implementation. + virtual void OnNetworkChanged(chromeos::NetworkLibrary* obj, + const chromeos::Network* network); private: typedef enum PlanActivationState { @@ -328,8 +329,8 @@ MobileSetupHandler::MobileSetupHandler(const std::string& service_path) MobileSetupHandler::~MobileSetupHandler() { chromeos::NetworkLibrary* lib = chromeos::CrosLibrary::Get()->GetNetworkLibrary(); - lib->RemoveObserver(this); - lib->RemoveProperyObserver(this); + lib->RemoveNetworkManagerObserver(this); + lib->RemoveObserverForAllNetworks(this); ReEnableOtherConnections(); } @@ -352,34 +353,25 @@ void MobileSetupHandler::RegisterMessages() { NewCallback(this, &MobileSetupHandler::HandleSetTransactionStatus)); } -void MobileSetupHandler::NetworkChanged(chromeos::NetworkLibrary* lib) { +void MobileSetupHandler::OnNetworkManagerChanged( + chromeos::NetworkLibrary* cros) { if (state_ == PLAN_ACTIVATION_PAGE_LOADING) return; + // Note that even though we get here when the service has + // reappeared after disappearing earlier in the activation + // process, there's no need to re-establish the NetworkObserver, + // because the service path remains the same. EvaluateCellularNetwork(GetCellularNetwork(service_path_)); } -void MobileSetupHandler::PropertyChanged(const char* service_path, - const char* key, - const Value* value) { - +void MobileSetupHandler::OnNetworkChanged(chromeos::NetworkLibrary* cros, + const chromeos::Network* network) { if (state_ == PLAN_ACTIVATION_PAGE_LOADING) return; - chromeos::CellularNetwork* network = GetCellularNetwork(service_path_); - if (!network) { - EvaluateCellularNetwork(NULL); - return; - } - if (network->service_path() != service_path) { - NOTREACHED(); - return; - } - std::string value_string; - LOG(INFO) << "Cellular property change: " << key << " = " << - value_string.c_str(); - - // TODO(zelidrag, ers): Remove this once we flip the notification machanism. - chromeos::CrosLibrary::Get()->GetNetworkLibrary()->UpdateSystemInfo(); - EvaluateCellularNetwork(network); + DCHECK(network && network->type() == chromeos::TYPE_CELLULAR); + EvaluateCellularNetwork( + static_cast<chromeos::CellularNetwork*>( + const_cast<chromeos::Network*>(network))); } void MobileSetupHandler::HandleCloseTab(const ListValue* args) { @@ -397,14 +389,12 @@ void MobileSetupHandler::HandleStartActivation(const ListValue* args) { ChangeState(NULL, PLAN_ACTIVATION_ERROR, std::string()); return; } - chromeos::NetworkLibrary* lib = chromeos::CrosLibrary::Get()-> - GetNetworkLibrary(); - lib->RemoveObserver(this); - lib->RemoveProperyObserver(this); - // Start monitoring network and service property changes. - lib->AddObserver(this); - lib->AddProperyObserver(network->service_path().c_str(), - this); + // Start monitoring network property changes. + chromeos::NetworkLibrary* lib = + chromeos::CrosLibrary::Get()->GetNetworkLibrary(); + lib->AddNetworkManagerObserver(this); + lib->RemoveObserverForAllNetworks(this); + lib->AddNetworkObserver(network->service_path(), this); ChangeState(network, PLAN_ACTIVATION_START, std::string()); EvaluateCellularNetwork(network); } @@ -581,8 +571,8 @@ void MobileSetupHandler::CompleteActivation( network->set_auto_connect(true); lib->SaveCellularNetwork(network); } - lib->RemoveObserver(this); - lib->RemoveProperyObserver(this); + lib->RemoveNetworkManagerObserver(this); + lib->RemoveObserverForAllNetworks(this); // Reactivate other types of connections if we have // shut them down previously. ReEnableOtherConnections(); diff --git a/chrome/browser/chromeos/login/login_utils.cc b/chrome/browser/chromeos/login/login_utils.cc index 8136645..724ef85 100644 --- a/chrome/browser/chromeos/login/login_utils.cc +++ b/chrome/browser/chromeos/login/login_utils.cc @@ -296,20 +296,20 @@ bool LoginUtilsImpl::IsBrowserLaunchEnabled() const { // We use a special class for this so that it can be safely leaked if we // never connect. At shutdown the order is not well defined, and it's possible // for the infrastructure needed to unregister might be unstable and crash. -class WarmingObserver : public NetworkLibrary::Observer { +class WarmingObserver : public NetworkLibrary::NetworkManagerObserver { public: WarmingObserver() { - NetworkLibrary *network = CrosLibrary::Get()->GetNetworkLibrary(); - network->AddObserver(this); + NetworkLibrary *netlib = CrosLibrary::Get()->GetNetworkLibrary(); + netlib->AddNetworkManagerObserver(this); } // If we're now connected, prewarm the auth url. - void NetworkChanged(NetworkLibrary* network) { - if (network->Connected()) { + void OnNetworkManagerChanged(NetworkLibrary* netlib) { + if (netlib->Connected()) { chrome_browser_net::Preconnect::PreconnectOnUIThread( GURL(GaiaAuthenticator2::kClientLoginUrl), chrome_browser_net::UrlInfo::EARLY_LOAD_MOTIVATED); - network->RemoveObserver(this); + netlib->RemoveNetworkManagerObserver(this); delete this; } } diff --git a/chrome/browser/chromeos/login/network_screen.cc b/chrome/browser/chromeos/login/network_screen.cc index 76ed69d..d760447 100644 --- a/chrome/browser/chromeos/login/network_screen.cc +++ b/chrome/browser/chromeos/login/network_screen.cc @@ -77,9 +77,9 @@ void NetworkScreen::ButtonPressed(views::Button* sender, } //////////////////////////////////////////////////////////////////////////////// -// NetworkLibrary::Observer implementation: +// NetworkLibrary::NetworkManagerObserver implementation: -void NetworkScreen::NetworkChanged(NetworkLibrary* network_lib) { +void NetworkScreen::OnNetworkManagerChanged(NetworkLibrary* network_lib) { UpdateStatus(network_lib); } @@ -110,7 +110,7 @@ void NetworkScreen::OnHelpLinkActivated() { void NetworkScreen::Refresh() { if (CrosLibrary::Get()->EnsureLoaded()) { SubscribeNetworkNotification(); - NetworkChanged(chromeos::CrosLibrary::Get()->GetNetworkLibrary()); + OnNetworkManagerChanged(chromeos::CrosLibrary::Get()->GetNetworkLibrary()); } } @@ -120,14 +120,16 @@ void NetworkScreen::Refresh() { void NetworkScreen::SubscribeNetworkNotification() { if (!is_network_subscribed_) { is_network_subscribed_ = true; - chromeos::CrosLibrary::Get()->GetNetworkLibrary()->AddObserver(this); + chromeos::CrosLibrary::Get()->GetNetworkLibrary() + ->AddNetworkManagerObserver(this); } } void NetworkScreen::UnsubscribeNetworkNotification() { if (is_network_subscribed_) { is_network_subscribed_ = false; - chromeos::CrosLibrary::Get()->GetNetworkLibrary()->RemoveObserver(this); + chromeos::CrosLibrary::Get()->GetNetworkLibrary() + ->RemoveNetworkManagerObserver(this); } } diff --git a/chrome/browser/chromeos/login/network_screen.h b/chrome/browser/chromeos/login/network_screen.h index 284ebb0..b3aa531 100644 --- a/chrome/browser/chromeos/login/network_screen.h +++ b/chrome/browser/chromeos/login/network_screen.h @@ -47,8 +47,8 @@ class NetworkScreen : public ViewScreen<NetworkSelectionView>, // views::ButtonListener implementation: virtual void ButtonPressed(views::Button* sender, const views::Event& event); - // NetworkLibrary::Observer implementation: - virtual void NetworkChanged(NetworkLibrary* network_lib); + // NetworkLibrary::NetworkManagerObserver implementation: + virtual void OnNetworkManagerChanged(NetworkLibrary* network_lib); protected: // Subscribes NetworkScreen to the network change notification, diff --git a/chrome/browser/chromeos/login/network_screen_browsertest.cc b/chrome/browser/chromeos/login/network_screen_browsertest.cc index ee9ce21..39f3f81 100644 --- a/chrome/browser/chromeos/login/network_screen_browsertest.cc +++ b/chrome/browser/chromeos/login/network_screen_browsertest.cc @@ -88,10 +88,13 @@ class NetworkScreenTest : public WizardInProcessBrowserTest { EXPECT_CALL(*mock_network_library_, Connecting()) .Times(2) .WillRepeatedly(Return(false)); - // Add an AddObserver for prewarming auth url check. EXPECT_CALL(*mock_network_library_, AddObserver(_)) .Times(3); EXPECT_CALL(*mock_network_library_, RemoveObserver(_)) + // Add an Observer for prewarming auth url check. + EXPECT_CALL(*mock_network_library_, AddNetworkManagerObserver(_)) + .Times(3); + EXPECT_CALL(*mock_network_library_, RemoveNetworkManagerObserver(_)) .Times(2); cros_mock_->SetStatusAreaMocksExpectations(); @@ -147,7 +150,7 @@ IN_PROC_BROWSER_TEST_F(NetworkScreenTest, Ethernet) { EXPECT_CALL(*mock_network_library_, ethernet_connecting()) .WillOnce((Return(true))); - network_screen->NetworkChanged(mock_network_library_); + network_screen->OnNetworkManagerChanged(mock_network_library_); EXPECT_FALSE(network_view->IsContinueEnabled()); EXPECT_FALSE(network_view->IsConnecting()); @@ -156,7 +159,7 @@ IN_PROC_BROWSER_TEST_F(NetworkScreenTest, Ethernet) { EXPECT_CALL(*mock_network_library_, Connected()) .WillOnce(Return(true)); - network_screen->NetworkChanged(mock_network_library_); + network_screen->OnNetworkManagerChanged(mock_network_library_); EXPECT_TRUE(network_view->IsContinueEnabled()); EmulateContinueButtonExit(network_screen); @@ -186,7 +189,7 @@ IN_PROC_BROWSER_TEST_F(NetworkScreenTest, Wifi) { EXPECT_CALL(*mock_network_library_, wifi_network()) .WillOnce(Return(wifi.get())); - network_screen->NetworkChanged(mock_network_library_); + network_screen->OnNetworkManagerChanged(mock_network_library_); EXPECT_FALSE(network_view->IsContinueEnabled()); EXPECT_FALSE(network_view->IsConnecting()); @@ -195,7 +198,7 @@ IN_PROC_BROWSER_TEST_F(NetworkScreenTest, Wifi) { EXPECT_CALL(*mock_network_library_, Connected()) .WillOnce(Return(true)); - network_screen->NetworkChanged(mock_network_library_); + network_screen->OnNetworkManagerChanged(mock_network_library_); EXPECT_TRUE(network_view->IsContinueEnabled()); EmulateContinueButtonExit(network_screen); @@ -227,7 +230,7 @@ IN_PROC_BROWSER_TEST_F(NetworkScreenTest, Cellular) { EXPECT_CALL(*mock_network_library_, cellular_network()) .WillOnce(Return(cellular.get())); - network_screen->NetworkChanged(mock_network_library_); + network_screen->OnNetworkManagerChanged(mock_network_library_); EXPECT_FALSE(network_view->IsContinueEnabled()); EXPECT_FALSE(network_view->IsConnecting()); @@ -236,7 +239,7 @@ IN_PROC_BROWSER_TEST_F(NetworkScreenTest, Cellular) { EXPECT_CALL(*mock_network_library_, Connected()) .WillOnce(Return(true)); - network_screen->NetworkChanged(mock_network_library_); + network_screen->OnNetworkManagerChanged(mock_network_library_); EXPECT_TRUE(network_view->IsContinueEnabled()); EmulateContinueButtonExit(network_screen); @@ -268,7 +271,7 @@ IN_PROC_BROWSER_TEST_F(NetworkScreenTest, Timeout) { EXPECT_CALL(*mock_network_library_, Connected()) .WillOnce(Return(false)); - network_screen->NetworkChanged(mock_network_library_); + network_screen->OnNetworkManagerChanged(mock_network_library_); EXPECT_FALSE(network_view->IsContinueEnabled()); EXPECT_FALSE(network_view->IsConnecting()); diff --git a/chrome/browser/chromeos/login/network_screen_delegate.h b/chrome/browser/chromeos/login/network_screen_delegate.h index 3a5978a..e64e380 100644 --- a/chrome/browser/chromeos/login/network_screen_delegate.h +++ b/chrome/browser/chromeos/login/network_screen_delegate.h @@ -22,7 +22,7 @@ class LanguageSwitchMenu; // Interface that NetworkScreen exposes to the NetworkSelectionView. class NetworkScreenDelegate : public views::ButtonListener, - public NetworkLibrary::Observer { + public NetworkLibrary::NetworkManagerObserver { public: // Cleares all error notifications. virtual void ClearErrors() = 0; diff --git a/chrome/browser/chromeos/network_list.cc b/chrome/browser/chromeos/network_list.cc index 26df2de..c22795b 100644 --- a/chrome/browser/chromeos/network_list.cc +++ b/chrome/browser/chromeos/network_list.cc @@ -49,7 +49,8 @@ bool NetworkList::IsNetworkConnecting(NetworkType type, return IsInNetworkList(connecting_networks_, type, id); } -void NetworkList::NetworkChanged(chromeos::NetworkLibrary* network_lib) { +void NetworkList::OnNetworkManagerChanged( + chromeos::NetworkLibrary* network_lib) { networks_.clear(); connected_networks_.clear(); connecting_networks_.clear(); diff --git a/chrome/browser/chromeos/network_list.h b/chrome/browser/chromeos/network_list.h index 21f0e63..47c7426 100644 --- a/chrome/browser/chromeos/network_list.h +++ b/chrome/browser/chromeos/network_list.h @@ -83,7 +83,7 @@ class NetworkList { NetworkList::NetworkItem* GetNetworkAt(int index); // Callback from NetworkLibrary. - void NetworkChanged(chromeos::NetworkLibrary* network_lib); + void OnNetworkManagerChanged(chromeos::NetworkLibrary* network_lib); private: typedef std::vector<NetworkItem> NetworkItemVector; diff --git a/chrome/browser/chromeos/network_message_observer.cc b/chrome/browser/chromeos/network_message_observer.cc index 92bc85a..32545bd 100644 --- a/chrome/browser/chromeos/network_message_observer.cc +++ b/chrome/browser/chromeos/network_message_observer.cc @@ -35,11 +35,19 @@ NetworkMessageObserver::NetworkMessageObserver(Profile* profile) notification_no_data_(profile, "network_no_data.chromeos", IDR_NOTIFICATION_BARS_EMPTY, l10n_util::GetStringUTF16(IDS_NETWORK_OUT_OF_DATA_TITLE)) { - NetworkChanged(CrosLibrary::Get()->GetNetworkLibrary()); + NetworkLibrary* netlib = CrosLibrary::Get()->GetNetworkLibrary(); + OnNetworkManagerChanged(netlib); + // Note that this gets added as a NetworkManagerObserver and a + // CellularDataPlanObserver in browser_init.cc + netlib->AddNetworkManagerObserver(this); + netlib->AddCellularDataPlanObserver(this); initialized_ = true; } NetworkMessageObserver::~NetworkMessageObserver() { + NetworkLibrary* netlib = CrosLibrary::Get()->GetNetworkLibrary(); + netlib->RemoveNetworkManagerObserver(this); + netlib->RemoveCellularDataPlanObserver(this); notification_connection_error_.Hide(); notification_low_data_.Hide(); notification_no_data_.Hide(); @@ -65,7 +73,7 @@ void NetworkMessageObserver::MobileSetup(const ListValue* args) { BrowserList::GetLastActive()->OpenMobilePlanTabAndActivate(); } -void NetworkMessageObserver::NetworkChanged(NetworkLibrary* obj) { +void NetworkMessageObserver::OnNetworkManagerChanged(NetworkLibrary* obj) { const WifiNetworkVector& wifi_networks = obj->wifi_networks(); const CellularNetworkVector& cellular_networks = obj->cellular_networks(); @@ -150,7 +158,7 @@ void NetworkMessageObserver::NetworkChanged(NetworkLibrary* obj) { CreateModalPopup(view); } -void NetworkMessageObserver::CellularDataPlanChanged(NetworkLibrary* obj) { +void NetworkMessageObserver::OnCellularDataPlanChanged(NetworkLibrary* obj) { const CellularNetwork* cellular = obj->cellular_network(); if (!cellular) return; diff --git a/chrome/browser/chromeos/network_message_observer.h b/chrome/browser/chromeos/network_message_observer.h index cdb0a81..db1ba1b 100644 --- a/chrome/browser/chromeos/network_message_observer.h +++ b/chrome/browser/chromeos/network_message_observer.h @@ -23,7 +23,8 @@ namespace chromeos { // The network message observer displays a system notification for network // messages. -class NetworkMessageObserver : public NetworkLibrary::Observer { +class NetworkMessageObserver : public NetworkLibrary::NetworkManagerObserver, + public NetworkLibrary::CellularDataPlanObserver { public: explicit NetworkMessageObserver(Profile* profile); virtual ~NetworkMessageObserver(); @@ -34,9 +35,10 @@ class NetworkMessageObserver : public NetworkLibrary::Observer { virtual void CreateModalPopup(views::WindowDelegate* view); virtual void MobileSetup(const ListValue* args); - // NetworkLibrary::Observer implementation. - virtual void NetworkChanged(NetworkLibrary* obj); - virtual void CellularDataPlanChanged(NetworkLibrary* obj); + // NetworkLibrary::NetworkManagerObserver implementation. + virtual void OnNetworkManagerChanged(NetworkLibrary* obj); + // NetworkLibrary::CellularDataPlanObserver implementation. + virtual void OnCellularDataPlanChanged(NetworkLibrary* obj); bool initialized_; // Wifi networks by service path. @@ -62,4 +64,3 @@ class NetworkMessageObserver : public NetworkLibrary::Observer { } // namespace chromeos #endif // CHROME_BROWSER_CHROMEOS_NETWORK_MESSAGE_OBSERVER_H_ - diff --git a/chrome/browser/chromeos/network_state_notifier.cc b/chrome/browser/chromeos/network_state_notifier.cc index aaaf763..d0552f9 100644 --- a/chrome/browser/chromeos/network_state_notifier.cc +++ b/chrome/browser/chromeos/network_state_notifier.cc @@ -34,9 +34,17 @@ NetworkStateNotifier::NetworkStateNotifier() : ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)), state_(RetrieveState()), offline_start_time_(Time::Now()) { + DCHECK(CrosLibrary::Get()->EnsureLoaded()); + // Note that this gets added as a NetworkManagerObserver + // in browser_init.cc +} + +NetworkStateNotifier::~NetworkStateNotifier() { + DCHECK(CrosLibrary::Get()->EnsureLoaded()); + CrosLibrary::Get()->GetNetworkLibrary()->RemoveNetworkManagerObserver(this); } -void NetworkStateNotifier::NetworkChanged(NetworkLibrary* cros) { +void NetworkStateNotifier::OnNetworkManagerChanged(NetworkLibrary* cros) { DCHECK(CrosLibrary::Get()->EnsureLoaded()); BrowserThread::PostTask( BrowserThread::UI, FROM_HERE, diff --git a/chrome/browser/chromeos/network_state_notifier.h b/chrome/browser/chromeos/network_state_notifier.h index 9d49d94..94e57f4 100644 --- a/chrome/browser/chromeos/network_state_notifier.h +++ b/chrome/browser/chromeos/network_state_notifier.h @@ -42,10 +42,10 @@ class NetworkStateDetails { }; // NetworkStateNotifier sends notification when network state has -// chagned. Notificatio is sent in UI thread. +// chagned. Notification is sent in UI thread. // TODO(oshima): port this to other platform. merge with // NetworkChangeNotifier if possible. -class NetworkStateNotifier : public NetworkLibrary::Observer { +class NetworkStateNotifier : public NetworkLibrary::NetworkManagerObserver { public: // Returns the singleton instance of the network state notifier; static NetworkStateNotifier* Get(); @@ -59,8 +59,8 @@ class NetworkStateNotifier : public NetworkLibrary::Observer { return Get()->state_ == NetworkStateDetails::CONNECTED; } - // NetworkLibrary::Observer implementation. - virtual void NetworkChanged(NetworkLibrary* cros); + // NetworkLibrary::NetworkManagerObserver implementation. + virtual void OnNetworkManagerChanged(NetworkLibrary* cros); private: friend struct DefaultSingletonTraits<NetworkStateNotifier>; @@ -69,7 +69,7 @@ class NetworkStateNotifier : public NetworkLibrary::Observer { static NetworkStateDetails::State RetrieveState(); NetworkStateNotifier(); - virtual ~NetworkStateNotifier() {} + virtual ~NetworkStateNotifier(); // Update the current state and sends notification to observers. // This should be invoked in UI thread. diff --git a/chrome/browser/chromeos/network_state_notifier_browsertest.cc b/chrome/browser/chromeos/network_state_notifier_browsertest.cc index 126ff13b..47ea0de 100644 --- a/chrome/browser/chromeos/network_state_notifier_browsertest.cc +++ b/chrome/browser/chromeos/network_state_notifier_browsertest.cc @@ -70,7 +70,7 @@ IN_PROC_BROWSER_TEST_F(NetworkStateNotifierTest, TestConnected) { .WillRepeatedly((Return(true))) .RetiresOnSaturation(); NetworkStateNotifier* notifier = NetworkStateNotifier::Get(); - notifier->NetworkChanged(mock_network_library_); + notifier->OnNetworkManagerChanged(mock_network_library_); WaitForNotification(); EXPECT_EQ(chromeos::NetworkStateDetails::CONNECTED, state_); } @@ -88,7 +88,7 @@ IN_PROC_BROWSER_TEST_F(NetworkStateNotifierTest, TestConnecting) { .WillOnce((Return(true))) .RetiresOnSaturation(); NetworkStateNotifier* notifier = NetworkStateNotifier::Get(); - notifier->NetworkChanged(mock_network_library_); + notifier->OnNetworkManagerChanged(mock_network_library_); WaitForNotification(); EXPECT_EQ(chromeos::NetworkStateDetails::CONNECTING, state_); } @@ -106,7 +106,7 @@ IN_PROC_BROWSER_TEST_F(NetworkStateNotifierTest, TestDisconnected) { .WillOnce((Return(false))) .RetiresOnSaturation(); NetworkStateNotifier* notifier = NetworkStateNotifier::Get(); - notifier->NetworkChanged(mock_network_library_); + notifier->OnNetworkManagerChanged(mock_network_library_); WaitForNotification(); EXPECT_EQ(chromeos::NetworkStateDetails::DISCONNECTED, state_); } diff --git a/chrome/browser/chromeos/options/internet_page_view.cc b/chrome/browser/chromeos/options/internet_page_view.cc index a9f099f..794814a 100644 --- a/chrome/browser/chromeos/options/internet_page_view.cc +++ b/chrome/browser/chromeos/options/internet_page_view.cc @@ -301,7 +301,7 @@ class WirelessSection : public NetworkSection { bool connected, int connection_type); WifiNetworkVector wifi_networks_; - CellularNetworkVector celluar_networks_; + CellularNetworkVector cellular_networks_; DISALLOW_COPY_AND_ASSIGN(WirelessSection); }; @@ -334,36 +334,34 @@ void WirelessSection::InitSection() { } // Cellular - celluar_networks_ = cros->cellular_networks(); - // Cellular networks ssids. - for (size_t i = 0; i < celluar_networks_.size(); ++i) { - std::wstring name = ASCIIToWide(celluar_networks_[i]->name()); + cellular_networks_ = cros->cellular_networks(); + for (size_t i = 0; i < cellular_networks_.size(); ++i) { + std::wstring name = ASCIIToWide(cellular_networks_[i]->name()); SkBitmap icon = NetworkMenu::IconForNetworkStrength( - celluar_networks_[i]->strength(), true); - // TODO(chocobo): Check cellular network 3g/edge. - SkBitmap badge = *rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_3G); -// SkBitmap badge = *rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_EDGE); + cellular_networks_[i]->strength(), true); + SkBitmap badge = + NetworkMenu::BadgeForNetworkTechnology(cellular_networks_[i]); icon = NetworkMenu::IconForDisplay(icon, badge); - bool connecting = celluar_networks_[i]->connecting(); - bool connected = celluar_networks_[i]->connected(); + bool connecting = cellular_networks_[i]->connecting(); + bool connected = cellular_networks_[i]->connected(); AddWirelessNetwork(i, icon, name, connecting, connected, TYPE_CELLULAR); } } void WirelessSection::ButtonClicked(int button, int connection_type, int id) { if (connection_type == TYPE_CELLULAR) { - if (static_cast<int>(celluar_networks_.size()) > id) { + if (static_cast<int>(cellular_networks_.size()) > id) { if (button == CONNECT_BUTTON) { // Connect to cellular network. CrosLibrary::Get()->GetNetworkLibrary()->ConnectToCellularNetwork( - celluar_networks_[id]); + cellular_networks_[id]); } else if (button == DISCONNECT_BUTTON) { CrosLibrary::Get()->GetNetworkLibrary()->DisconnectFromWirelessNetwork( - celluar_networks_[id]); + cellular_networks_[id]); } else { - CreateModalPopup(new NetworkConfigView(celluar_networks_[id])); + CreateModalPopup(new NetworkConfigView(cellular_networks_[id])); } } } else if (connection_type == TYPE_WIFI) { @@ -428,7 +426,6 @@ class RememberedSection : public NetworkSection { private: WifiNetworkVector wifi_networks_; - CellularNetworkVector celluar_networks_; DISALLOW_COPY_AND_ASSIGN(RememberedSection); }; @@ -570,14 +567,14 @@ InternetPageView::InternetPageView(Profile* profile) scroll_view_(new views::ScrollView) { NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); cros->UpdateSystemInfo(); - cros->AddObserver(this); + cros->AddNetworkManagerObserver(this); } InternetPageView::~InternetPageView() { - CrosLibrary::Get()->GetNetworkLibrary()->RemoveObserver(this); + CrosLibrary::Get()->GetNetworkLibrary()->RemoveNetworkManagerObserver(this); } -void InternetPageView::NetworkChanged(NetworkLibrary* obj) { +void InternetPageView::OnNetworkManagerChanged(NetworkLibrary* obj) { // Refresh wired, wireless, and remembered networks. // Remember the current scroll region, and try to scroll back afterwards. gfx::Rect rect = scroll_view_->GetVisibleRect(); diff --git a/chrome/browser/chromeos/options/internet_page_view.h b/chrome/browser/chromeos/options/internet_page_view.h index 71b7399..c5048df 100644 --- a/chrome/browser/chromeos/options/internet_page_view.h +++ b/chrome/browser/chromeos/options/internet_page_view.h @@ -19,13 +19,13 @@ class InternetPageContentView; // Internet settings page for Chrome OS class InternetPageView : public SettingsPageView, - public NetworkLibrary::Observer { + public NetworkLibrary::NetworkManagerObserver { public: explicit InternetPageView(Profile* profile); virtual ~InternetPageView(); - // NetworkLibrary::Observer implementation. - virtual void NetworkChanged(NetworkLibrary* obj); + // NetworkLibrary::NetworkManagerObserver implementation. + virtual void OnNetworkManagerChanged(NetworkLibrary* obj); // views::View overrides: virtual void Layout(); diff --git a/chrome/browser/chromeos/status/network_dropdown_button.cc b/chrome/browser/chromeos/status/network_dropdown_button.cc index cade402..8deb193 100644 --- a/chrome/browser/chromeos/status/network_dropdown_button.cc +++ b/chrome/browser/chromeos/status/network_dropdown_button.cc @@ -34,12 +34,12 @@ NetworkDropdownButton::NetworkDropdownButton(bool browser_mode, parent_window_(parent_window) { animation_connecting_.SetThrobDuration(kThrobDuration); animation_connecting_.SetTweenType(Tween::LINEAR); - NetworkChanged(CrosLibrary::Get()->GetNetworkLibrary()); - CrosLibrary::Get()->GetNetworkLibrary()->AddObserver(this); + OnNetworkManagerChanged(CrosLibrary::Get()->GetNetworkLibrary()); + CrosLibrary::Get()->GetNetworkLibrary()->AddNetworkManagerObserver(this); } NetworkDropdownButton::~NetworkDropdownButton() { - CrosLibrary::Get()->GetNetworkLibrary()->RemoveObserver(this); + CrosLibrary::Get()->GetNetworkLibrary()->RemoveNetworkManagerObserver(this); } //////////////////////////////////////////////////////////////////////////////// @@ -62,13 +62,13 @@ void NetworkDropdownButton::AnimationProgressed(const Animation* animation) { } void NetworkDropdownButton::Refresh() { - NetworkChanged(CrosLibrary::Get()->GetNetworkLibrary()); + OnNetworkManagerChanged(CrosLibrary::Get()->GetNetworkLibrary()); } //////////////////////////////////////////////////////////////////////////////// -// NetworkDropdownButton, NetworkLibrary::Observer implementation: +// NetworkDropdownButton, NetworkLibrary::NetworkManagerObserver implementation: -void NetworkDropdownButton::NetworkChanged(NetworkLibrary* cros) { +void NetworkDropdownButton::OnNetworkManagerChanged(NetworkLibrary* cros) { // Show network that we will actually use. It could be another network than // user selected. For example user selected WiFi network but we have Ethernet // connection and Chrome OS device will actually use Ethernet. @@ -78,28 +78,27 @@ void NetworkDropdownButton::NetworkChanged(NetworkLibrary* cros) { ResourceBundle& rb = ResourceBundle::GetSharedInstance(); if (CrosLibrary::Get()->EnsureLoaded()) { - // Always show the higher priority connection first. Ethernet then wifi. - if (cros->ethernet_connected()) { + // Always show the active network, if any + const Network* active_network = cros->active_network(); + const WirelessNetwork* wireless; + if (active_network != NULL) { animation_connecting_.Stop(); - SetIcon(*rb.GetBitmapNamed(IDR_STATUSBAR_WIRED)); - SetText(l10n_util::GetString(IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET)); - } else if (cros->wifi_connected()) { - animation_connecting_.Stop(); - SetIcon(IconForNetworkStrength( - cros->wifi_network()->strength(), true)); - SetText(ASCIIToWide(cros->wifi_network()->name())); - } else if (cros->cellular_connected()) { - animation_connecting_.Stop(); - SetIcon(IconForNetworkStrength( - cros->cellular_network()->strength(), false)); - SetText(ASCIIToWide(cros->cellular_network()->name())); + if (active_network->type() == TYPE_ETHERNET) { + SetIcon(*rb.GetBitmapNamed(IDR_STATUSBAR_WIRED)); + SetText(l10n_util::GetString(IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET)); + } else { + DCHECK(active_network->type() == TYPE_WIFI || + active_network->type() == TYPE_CELLULAR); + wireless = static_cast<const WirelessNetwork*>(active_network); + SetIcon(IconForNetworkStrength(wireless->strength(), false)); + SetText(ASCIIToWide(wireless->name())); + } } else if (cros->wifi_connecting() || cros->cellular_connecting()) { if (!animation_connecting_.is_animating()) { animation_connecting_.Reset(); animation_connecting_.StartThrobbing(-1); SetIcon(*rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_BARS1_BLACK)); } - if (cros->wifi_connecting()) SetText(ASCIIToWide(cros->wifi_network()->name())); else if (cros->cellular_connecting()) diff --git a/chrome/browser/chromeos/status/network_dropdown_button.h b/chrome/browser/chromeos/status/network_dropdown_button.h index 47500f6..2968e42 100644 --- a/chrome/browser/chromeos/status/network_dropdown_button.h +++ b/chrome/browser/chromeos/status/network_dropdown_button.h @@ -19,7 +19,7 @@ namespace chromeos { // See NetworkMenu for more details. class NetworkDropdownButton : public views::MenuButton, public NetworkMenu, - public NetworkLibrary::Observer { + public NetworkLibrary::NetworkManagerObserver { public: NetworkDropdownButton(bool browser_mode, gfx::NativeWindow parent_window); virtual ~NetworkDropdownButton(); @@ -27,8 +27,8 @@ class NetworkDropdownButton : public views::MenuButton, // AnimationDelegate implementation. virtual void AnimationProgressed(const Animation* animation); - // NetworkLibrary::Observer implementation. - virtual void NetworkChanged(NetworkLibrary* obj); + // NetworkLibrary::NetworkManagerObserver implementation. + virtual void OnNetworkManagerChanged(NetworkLibrary* obj); // Refreshes button state. Used when language has been changed. void Refresh(); diff --git a/chrome/browser/chromeos/status/network_menu.cc b/chrome/browser/chromeos/status/network_menu.cc index 428865b..159b09c 100644 --- a/chrome/browser/chromeos/status/network_menu.cc +++ b/chrome/browser/chromeos/status/network_menu.cc @@ -389,6 +389,47 @@ SkBitmap NetworkMenu::IconForNetworkStrength(const CellularNetwork* cellular) { } // static +// TODO(ers) update for GSM when we have the necessary images +SkBitmap NetworkMenu::BadgeForNetworkTechnology( + const CellularNetwork* cellular) { + + int id; + if (cellular->network_technology() == NETWORK_TECHNOLOGY_EVDO) { + switch (cellular->data_left()) { + case CellularNetwork::DATA_NONE: + case CellularNetwork::DATA_VERY_LOW: + id = IDR_STATUSBAR_NETWORK_3G_ERROR; + break; + case CellularNetwork::DATA_LOW: + id = IDR_STATUSBAR_NETWORK_3G_WARN; + break; + case CellularNetwork::DATA_NORMAL: + id = IDR_STATUSBAR_NETWORK_3G; + break; + } + } else if (cellular->network_technology() == NETWORK_TECHNOLOGY_1XRTT) { + switch (cellular->data_left()) { + case CellularNetwork::DATA_NONE: + case CellularNetwork::DATA_VERY_LOW: + id = IDR_STATUSBAR_NETWORK_1X_ERROR; + break; + case CellularNetwork::DATA_LOW: + id = IDR_STATUSBAR_NETWORK_1X_WARN; + break; + case CellularNetwork::DATA_NORMAL: + id = IDR_STATUSBAR_NETWORK_1X; + break; + } + } else { + id = -1; + } + if (id == -1) + return SkBitmap(); + else + return *ResourceBundle::GetSharedInstance().GetBitmapNamed(id); +} + +// static SkBitmap NetworkMenu::IconForDisplay(SkBitmap icon, SkBitmap badge) { // Draw badge at (14,14). static const int kBadgeX = 14; @@ -529,9 +570,7 @@ void NetworkMenu::InitMenuItems() { } SkBitmap icon = IconForNetworkStrength(cell_networks[i]->strength(), true); - // TODO(chocobo): Check cellular network 3g/edge. - SkBitmap badge = *rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_3G); - // SkBitmap badge = *rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_EDGE); + SkBitmap badge = BadgeForNetworkTechnology(cell_networks[i]); int flag = FLAG_CELLULAR; if (active_cellular && cell_networks[i]->service_path() == diff --git a/chrome/browser/chromeos/status/network_menu.h b/chrome/browser/chromeos/status/network_menu.h index 8f57c61..a68e928 100644 --- a/chrome/browser/chromeos/status/network_menu.h +++ b/chrome/browser/chromeos/status/network_menu.h @@ -115,11 +115,12 @@ class NetworkMenu : public views::ViewMenuDelegate, // |black| is used to specify whether to return a black icon for display // on a light background or a white icon for display on a dark background. static SkBitmap IconForNetworkStrength(int strength, bool black); - // Returns the Icon for a network strength for CellularNetwork |cellular|. // This returns different colored bars depending on cellular data left. static SkBitmap IconForNetworkStrength(const CellularNetwork* cellular); - + // Returns the Badge for a given network technology. + // This returns different colored symbols depending on cellular data left. + static SkBitmap BadgeForNetworkTechnology(const CellularNetwork* cellular); // This method will convert the |icon| bitmap to the correct size for display. // If the |badge| icon is not empty, it will draw that on top of the icon. static SkBitmap IconForDisplay(SkBitmap icon, SkBitmap badge); diff --git a/chrome/browser/chromeos/status/network_menu_button.cc b/chrome/browser/chromeos/status/network_menu_button.cc index 48b5187..b8cbe00 100644 --- a/chrome/browser/chromeos/status/network_menu_button.cc +++ b/chrome/browser/chromeos/status/network_menu_button.cc @@ -35,12 +35,16 @@ NetworkMenuButton::NetworkMenuButton(StatusAreaHost* host) ALLOW_THIS_IN_INITIALIZER_LIST(animation_connecting_(this)) { animation_connecting_.SetThrobDuration(kThrobDuration); animation_connecting_.SetTweenType(Tween::EASE_IN_OUT); - NetworkChanged(CrosLibrary::Get()->GetNetworkLibrary()); - CrosLibrary::Get()->GetNetworkLibrary()->AddObserver(this); + OnNetworkManagerChanged(CrosLibrary::Get()->GetNetworkLibrary()); + CrosLibrary::Get()->GetNetworkLibrary()->AddNetworkManagerObserver(this); + CrosLibrary::Get()->GetNetworkLibrary()->AddCellularDataPlanObserver(this); } NetworkMenuButton::~NetworkMenuButton() { - CrosLibrary::Get()->GetNetworkLibrary()->RemoveObserver(this); + NetworkLibrary* netlib = CrosLibrary::Get()->GetNetworkLibrary(); + netlib->RemoveNetworkManagerObserver(this); + netlib->RemoveObserverForAllNetworks(this); + netlib->RemoveCellularDataPlanObserver(this); } //////////////////////////////////////////////////////////////////////////////// @@ -73,11 +77,27 @@ void NetworkMenuButton::DrawIcon(gfx::Canvas* canvas) { } //////////////////////////////////////////////////////////////////////////////// -// NetworkMenuButton, NetworkLibrary::Observer implementation: +// NetworkMenuButton, NetworkLibrary::NetworkManagerObserver implementation: -void NetworkMenuButton::NetworkChanged(NetworkLibrary* cros) { +void NetworkMenuButton::OnNetworkManagerChanged(NetworkLibrary* cros) { ResourceBundle& rb = ResourceBundle::GetSharedInstance(); if (CrosLibrary::Get()->EnsureLoaded()) { + // Add an observer for the active network, if any + const Network* network = cros->active_network(); + if (active_network_.empty() || network == NULL || + active_network_ != network->service_path()) { + if (!active_network_.empty()) { + cros->RemoveNetworkObserver(active_network_, this); + } + if (network != NULL) { + cros->AddNetworkObserver(network->service_path(), this); + } + } + if (network) + active_network_ = network->service_path(); + else + active_network_ = ""; + if (cros->wifi_connecting() || cros->cellular_connecting()) { // Start the connecting animation if not running. if (!animation_connecting_.is_animating()) { @@ -98,68 +118,35 @@ void NetworkMenuButton::NetworkChanged(NetworkLibrary* cros) { } else { // Stop connecting animation since we are not connecting. animation_connecting_.Stop(); - - // Always show the higher priority connection first. Ethernet then wifi. - if (cros->ethernet_connected()) { - SetIcon(*rb.GetBitmapNamed(IDR_STATUSBAR_WIRED)); - SetTooltipText( - l10n_util::GetStringF( - IDS_STATUSBAR_NETWORK_CONNECTED_TOOLTIP, - l10n_util::GetString(IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET))); - } else if (cros->wifi_connected()) { - SetIcon(IconForNetworkStrength( - cros->wifi_network()->strength(), false)); - SetTooltipText(l10n_util::GetStringF( - IDS_STATUSBAR_NETWORK_CONNECTED_TOOLTIP, - UTF8ToWide(cros->wifi_network()->name()))); - } else if (cros->cellular_connected()) { - const CellularNetwork* cellular = cros->cellular_network(); - if (cellular->data_left() == CellularNetwork::DATA_NONE) { - // If no data, then we show 0 bars. - SetIcon(*rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_BARS0)); - } else { - SetIcon(IconForNetworkStrength(cellular)); - } - SetTooltipText(l10n_util::GetStringF( - IDS_STATUSBAR_NETWORK_CONNECTED_TOOLTIP, - UTF8ToWide(cellular->name()))); - } else { + if (!cros->Connected()) { SetIcon(*rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_BARS0)); SetTooltipText(l10n_util::GetString( IDS_STATUSBAR_NETWORK_NO_NETWORK_TOOLTIP)); + } else { + SetNetworkIcon(network); } } + SetNetworkBadge(cros, network); + } else { + SetIcon(*rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_BARS0)); + SetBadge(*rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_WARNING)); + SetTooltipText(l10n_util::GetString( + IDS_STATUSBAR_NETWORK_NO_NETWORK_TOOLTIP)); + } - // Figure out whether or not to show a badge. - int id = -1; - if (cros->Connecting()) { - if (cros->cellular_connecting()) { - id = IDR_STATUSBAR_NETWORK_3G; - } - } else if (cros->Connected()) { - if (!cros->ethernet_connected() && !cros->wifi_connected() && - cros->cellular_connected()) { - switch (cros->cellular_network()->data_left()) { - case CellularNetwork::DATA_NONE: - case CellularNetwork::DATA_VERY_LOW: - id = IDR_STATUSBAR_NETWORK_3G_ERROR; - break; - case CellularNetwork::DATA_LOW: - id = IDR_STATUSBAR_NETWORK_3G_WARN; - break; - case CellularNetwork::DATA_NORMAL: - id = IDR_STATUSBAR_NETWORK_3G; - break; - } - } - } else { - id = IDR_STATUSBAR_NETWORK_DISCONNECTED; - } - if (id != -1) { - SetBadge(*rb.GetBitmapNamed(id)); - } else { - SetBadge(SkBitmap()); - } + SchedulePaint(); + UpdateMenu(); +} + +//////////////////////////////////////////////////////////////////////////////// +// NetworkMenuButton, NetworkLibrary::NetworkObserver implementation: +void NetworkMenuButton::OnNetworkChanged(NetworkLibrary* cros, + const Network* network) { + ResourceBundle& rb = ResourceBundle::GetSharedInstance(); + if (CrosLibrary::Get()->EnsureLoaded()) { + // Always show the active network connection, if any. + SetNetworkIcon(network); + SetNetworkBadge(cros, network); } else { SetIcon(*rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_BARS0)); SetBadge(*rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_WARNING)); @@ -171,9 +158,9 @@ void NetworkMenuButton::NetworkChanged(NetworkLibrary* cros) { UpdateMenu(); } -void NetworkMenuButton::CellularDataPlanChanged(NetworkLibrary* cros) { - // Call NetworkChanged which will update the icon. - NetworkChanged(cros); +void NetworkMenuButton::OnCellularDataPlanChanged(NetworkLibrary* cros) { + // Call OnNetworkManagerChanged which will update the icon. + OnNetworkManagerChanged(cros); } //////////////////////////////////////////////////////////////////////////////// @@ -195,4 +182,54 @@ bool NetworkMenuButton::ShouldOpenButtonOptions() const { return host_->ShouldOpenButtonOptions(this); } +//////////////////////////////////////////////////////////////////////////////// +// NetworkMenuButton, private methods + +void NetworkMenuButton::SetNetworkIcon(const Network* network) { + ResourceBundle& rb = ResourceBundle::GetSharedInstance(); + if (network && network->is_active()) { + if (network->type() == TYPE_ETHERNET) { + SetIcon(*rb.GetBitmapNamed(IDR_STATUSBAR_WIRED)); + SetTooltipText( + l10n_util::GetStringF( + IDS_STATUSBAR_NETWORK_CONNECTED_TOOLTIP, + l10n_util::GetString(IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET))); + } else if (network->type() == TYPE_WIFI) { + const WifiNetwork* wifi = static_cast<const WifiNetwork*>(network); + SetIcon(IconForNetworkStrength(wifi->strength(), false)); + SetTooltipText(l10n_util::GetStringF( + IDS_STATUSBAR_NETWORK_CONNECTED_TOOLTIP, + UTF8ToWide(wifi->name()))); + } else if (network->type() == TYPE_CELLULAR) { + const CellularNetwork* cellular = + static_cast<const CellularNetwork*>(network); + if (cellular->data_left() == CellularNetwork::DATA_NONE) { + // If no data, then we show 0 bars. + SetIcon(*rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_BARS0)); + } else { + SetIcon(IconForNetworkStrength(cellular)); + } + SetTooltipText(l10n_util::GetStringF( + IDS_STATUSBAR_NETWORK_CONNECTED_TOOLTIP, + UTF8ToWide(cellular->name()))); + } + } +} + +void NetworkMenuButton::SetNetworkBadge(NetworkLibrary* cros, + const Network* network) { + ResourceBundle& rb = ResourceBundle::GetSharedInstance(); + // Figure out whether or not to show a badge. + if (network && network->type() == TYPE_CELLULAR && + (network->is_active() || network->connecting())) { + const CellularNetwork* cellular + = static_cast<const CellularNetwork*>(network); + SetBadge(BadgeForNetworkTechnology(cellular)); + } else if (!cros->Connected() && !cros->Connecting()) { + SetBadge(*rb.GetBitmapNamed(IDR_STATUSBAR_NETWORK_DISCONNECTED)); + } else { + SetBadge(SkBitmap()); + } +} + } // namespace chromeos diff --git a/chrome/browser/chromeos/status/network_menu_button.h b/chrome/browser/chromeos/status/network_menu_button.h index f8b27db..c2b2e71 100644 --- a/chrome/browser/chromeos/status/network_menu_button.h +++ b/chrome/browser/chromeos/status/network_menu_button.h @@ -46,7 +46,9 @@ class StatusAreaHost; // The label will be BOLD if the network is currently connected. class NetworkMenuButton : public StatusAreaButton, public NetworkMenu, - public NetworkLibrary::Observer { + public NetworkLibrary::NetworkManagerObserver, + public NetworkLibrary::NetworkObserver, + public NetworkLibrary::CellularDataPlanObserver { public: explicit NetworkMenuButton(StatusAreaHost* host); virtual ~NetworkMenuButton(); @@ -54,9 +56,12 @@ class NetworkMenuButton : public StatusAreaButton, // AnimationDelegate implementation. virtual void AnimationProgressed(const Animation* animation); - // NetworkLibrary::Observer implementation. - virtual void NetworkChanged(NetworkLibrary* obj); - virtual void CellularDataPlanChanged(NetworkLibrary* obj); + // NetworkLibrary::NetworkManagerObserver implementation. + virtual void OnNetworkManagerChanged(NetworkLibrary* cros); + // NetworkLibrary::NetworkObserver implementation. + virtual void OnNetworkChanged(NetworkLibrary* cros, const Network* network); + // NetworkLibrary::CellularDataPlanObserver implementation. + virtual void OnCellularDataPlanChanged(NetworkLibrary* cros); // Sets the badge icon. void SetBadge(const SkBitmap& badge) { badge_ = badge; } @@ -73,6 +78,9 @@ class NetworkMenuButton : public StatusAreaButton, virtual bool ShouldOpenButtonOptions() const; private: + void SetNetworkIcon(const Network* network); + void SetNetworkBadge(NetworkLibrary* cros, const Network* network); + // The status area host, StatusAreaHost* host_; @@ -85,6 +93,10 @@ class NetworkMenuButton : public StatusAreaButton, // The duration of the icon throbbing in milliseconds. static const int kThrobDuration; + // If any network is currently active, this is the service path of the one + // whose status is displayed in the network menu button. + std::string active_network_; + DISALLOW_COPY_AND_ASSIGN(NetworkMenuButton); }; |