Home | History | Annotate | Download | only in net
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/chromeos/net/network_change_notifier_chromeos.h"
      6 
      7 #include "base/task.h"
      8 #include "chrome/browser/chromeos/cros/cros_library.h"
      9 #include "content/browser/browser_thread.h"
     10 
     11 namespace chromeos {
     12 
     13 NetworkChangeNotifierChromeos::NetworkChangeNotifierChromeos()
     14     : has_active_network_(false),
     15       connectivity_state_(chromeos::CONN_STATE_UNKNOWN),
     16       ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {
     17 
     18   chromeos::NetworkLibrary* lib =
     19       chromeos::CrosLibrary::Get()->GetNetworkLibrary();
     20   lib->AddNetworkManagerObserver(this);
     21 }
     22 
     23 NetworkChangeNotifierChromeos::~NetworkChangeNotifierChromeos() {
     24   chromeos::NetworkLibrary* lib =
     25       chromeos::CrosLibrary::Get()->GetNetworkLibrary();
     26   lib->RemoveNetworkManagerObserver(this);
     27   lib->RemoveObserverForAllNetworks(this);
     28 }
     29 
     30 void NetworkChangeNotifierChromeos::OnNetworkManagerChanged(
     31     chromeos::NetworkLibrary* cros) {
     32   UpdateNetworkState(cros);
     33 }
     34 
     35 bool NetworkChangeNotifierChromeos::IsCurrentlyOffline() const {
     36   return connectivity_state_ != chromeos::CONN_STATE_UNRESTRICTED;
     37 }
     38 
     39 void NetworkChangeNotifierChromeos::UpdateNetworkState(
     40     chromeos::NetworkLibrary* lib) {
     41   const chromeos::Network* network = lib->active_network();
     42 
     43   // Check if active network was added, removed or changed.
     44   if ((!network && has_active_network_) ||
     45       (network && (!has_active_network_ ||
     46                    network->service_path() != service_path_ ||
     47                    network->ip_address() != ip_address_))) {
     48     if (has_active_network_)
     49       lib->RemoveObserverForAllNetworks(this);
     50     if (!network) {
     51       has_active_network_ = false;
     52       service_path_.clear();
     53       ip_address_.clear();
     54     } else {
     55       has_active_network_ = true;
     56       service_path_ = network->service_path();
     57       lib->AddNetworkObserver(network->service_path(), this);
     58       ip_address_ = network->ip_address();
     59     }
     60     BrowserThread::PostTask(
     61         BrowserThread::IO, FROM_HERE,
     62         NewRunnableFunction(
     63             &NetworkChangeNotifierChromeos::NotifyObserversOfIPAddressChange));
     64   }
     65 }
     66 
     67 void NetworkChangeNotifierChromeos::OnNetworkChanged(
     68     chromeos::NetworkLibrary* cros,
     69     const chromeos::Network* network) {
     70   if (!network) {
     71     NOTREACHED();
     72     return;
     73   }
     74   // Active network changed?
     75   if (network->service_path() != service_path_) {
     76     UpdateNetworkState(cros);
     77     return;
     78   }
     79   if (network->connectivity_state() != connectivity_state_) {
     80     connectivity_state_ = network->connectivity_state();
     81     BrowserThread::PostTask(
     82         BrowserThread::IO, FROM_HERE,
     83         method_factory_.NewRunnableMethod(
     84           &NetworkChangeNotifierChromeos::NotifyObserversOfOnlineStateChange));
     85   }
     86 }
     87 
     88 }  // namespace net
     89