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