network_state_notifier.cc revision dc0f95d653279beabeb9817299e2902918ba123e
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/network_state_notifier.h" 6 7#include "base/message_loop.h" 8#include "base/time.h" 9#include "chrome/browser/chromeos/cros/cros_library.h" 10#include "content/browser/browser_thread.h" 11#include "content/common/notification_service.h" 12#include "content/common/notification_type.h" 13 14namespace chromeos { 15 16using base::Time; 17using base::TimeDelta; 18 19// static 20NetworkStateNotifier* NetworkStateNotifier::GetInstance() { 21 return Singleton<NetworkStateNotifier>::get(); 22} 23 24// static 25TimeDelta NetworkStateNotifier::GetOfflineDuration() { 26 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 27 // TODO(oshima): make this instance method so that 28 // we can mock this for ui_tests. 29 // http://crbug.com/4825 . 30 return base::Time::Now() - GetInstance()->offline_start_time_; 31} 32 33NetworkStateNotifier::NetworkStateNotifier() 34 : ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)), 35 state_(RetrieveState()), 36 offline_start_time_(Time::Now()) { 37 // Note that this gets added as a NetworkManagerObserver 38 // in browser_init.cc 39} 40 41NetworkStateNotifier::~NetworkStateNotifier() { 42 // Let the NetworkManagerObserver leak to avoid a DCHECK 43 // failure in CommandLine::ForCurrentProcess. 44// if (CrosLibrary::Get()->EnsureLoaded()) 45// CrosLibrary::Get()->GetNetworkLibrary()-> 46// RemoveNetworkManagerObserver(this); 47} 48 49void NetworkStateNotifier::OnNetworkManagerChanged(NetworkLibrary* cros) { 50 DCHECK(CrosLibrary::Get()->EnsureLoaded()); 51 BrowserThread::PostTask( 52 BrowserThread::UI, FROM_HERE, 53 task_factory_.NewRunnableMethod( 54 &NetworkStateNotifier::UpdateNetworkState, 55 RetrieveState())); 56} 57 58void NetworkStateNotifier::UpdateNetworkState( 59 NetworkStateDetails::State new_state) { 60 DVLOG(1) << "UpdateNetworkState: new=" << new_state << ", old=" << state_; 61 if (state_ == NetworkStateDetails::CONNECTED && 62 new_state != NetworkStateDetails::CONNECTED) { 63 offline_start_time_ = Time::Now(); 64 } 65 66 state_ = new_state; 67 NetworkStateDetails details(state_); 68 NotificationService::current()->Notify( 69 NotificationType::NETWORK_STATE_CHANGED, 70 NotificationService::AllSources(), 71 Details<NetworkStateDetails>(&details)); 72}; 73 74// static 75NetworkStateDetails::State NetworkStateNotifier::RetrieveState() { 76 // Running on desktop means always connected, for now. 77 if (!CrosLibrary::Get()->EnsureLoaded()) 78 return NetworkStateDetails::CONNECTED; 79 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); 80 if (cros->Connected()) { 81 return NetworkStateDetails::CONNECTED; 82 } else if (cros->Connecting()) { 83 return NetworkStateDetails::CONNECTING; 84 } else { 85 return NetworkStateDetails::DISCONNECTED; 86 } 87} 88 89 90} // namespace chromeos 91