| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/network_state_notifier.h" |
| 6 |
| 7 #include "base/message_loop.h" |
| 8 #include "base/time.h" |
| 5 #include "chrome/browser/chrome_thread.h" | 9 #include "chrome/browser/chrome_thread.h" |
| 6 #include "chrome/browser/chromeos/cros/cros_library.h" | 10 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 7 #include "chrome/browser/chromeos/network_state_notifier.h" | |
| 8 #include "chrome/common/notification_service.h" | 11 #include "chrome/common/notification_service.h" |
| 9 #include "chrome/common/notification_type.h" | 12 #include "chrome/common/notification_type.h" |
| 10 | 13 |
| 11 #include "base/message_loop.h" | |
| 12 | |
| 13 namespace chromeos { | 14 namespace chromeos { |
| 14 | 15 |
| 16 using base::Time; |
| 17 using base::TimeDelta; |
| 18 |
| 19 // static |
| 15 NetworkStateNotifier* NetworkStateNotifier::Get() { | 20 NetworkStateNotifier* NetworkStateNotifier::Get() { |
| 16 return Singleton<NetworkStateNotifier>::get(); | 21 return Singleton<NetworkStateNotifier>::get(); |
| 17 } | 22 } |
| 18 | 23 |
| 24 // static |
| 25 TimeDelta NetworkStateNotifier::GetOfflineDuration() { |
| 26 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::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() - Get()->offline_start_time_; |
| 31 } |
| 32 |
| 19 NetworkStateNotifier::NetworkStateNotifier() | 33 NetworkStateNotifier::NetworkStateNotifier() |
| 20 : task_factory_(this) { | 34 : ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)), |
| 21 state_ = RetrieveState(); | 35 state_(RetrieveState()), |
| 36 offline_start_time_(Time::Now()) { |
| 22 } | 37 } |
| 23 | 38 |
| 24 void NetworkStateNotifier::NetworkChanged(NetworkLibrary* cros) { | 39 void NetworkStateNotifier::NetworkChanged(NetworkLibrary* cros) { |
| 25 DCHECK(CrosLibrary::Get()->EnsureLoaded()); | 40 DCHECK(CrosLibrary::Get()->EnsureLoaded()); |
| 26 // Update the state 1 seconds later using UI thread. | 41 // Update the state 1 seconds later using UI thread. |
| 27 // See http://crosbug.com/4558 | 42 // See http://crosbug.com/4558 |
| 28 ChromeThread::PostDelayedTask( | 43 ChromeThread::PostDelayedTask( |
| 29 ChromeThread::UI, FROM_HERE, | 44 ChromeThread::UI, FROM_HERE, |
| 30 task_factory_.NewRunnableMethod( | 45 task_factory_.NewRunnableMethod( |
| 31 &NetworkStateNotifier::UpdateNetworkState, | 46 &NetworkStateNotifier::UpdateNetworkState, |
| 32 RetrieveState()), | 47 RetrieveState()), |
| 33 1000); | 48 1000); |
| 34 } | 49 } |
| 35 | 50 |
| 36 void NetworkStateNotifier::UpdateNetworkState( | 51 void NetworkStateNotifier::UpdateNetworkState( |
| 37 NetworkStateDetails::State new_state) { | 52 NetworkStateDetails::State new_state) { |
| 38 DLOG(INFO) << "UpdateNetworkState: new=" | 53 DLOG(INFO) << "UpdateNetworkState: new=" |
| 39 << new_state << ", old=" << state_; | 54 << new_state << ", old=" << state_; |
| 55 if (state_ == NetworkStateDetails::CONNECTED && |
| 56 new_state != NetworkStateDetails::CONNECTED) { |
| 57 offline_start_time_ = Time::Now(); |
| 58 } |
| 59 |
| 40 state_ = new_state; | 60 state_ = new_state; |
| 41 NetworkStateDetails details(state_); | 61 NetworkStateDetails details(state_); |
| 42 NotificationService::current()->Notify( | 62 NotificationService::current()->Notify( |
| 43 NotificationType::NETWORK_STATE_CHANGED, | 63 NotificationType::NETWORK_STATE_CHANGED, |
| 44 NotificationService::AllSources(), | 64 NotificationService::AllSources(), |
| 45 Details<NetworkStateDetails>(&details)); | 65 Details<NetworkStateDetails>(&details)); |
| 46 }; | 66 }; |
| 47 | 67 |
| 48 // static | 68 // static |
| 49 NetworkStateDetails::State NetworkStateNotifier::RetrieveState() { | 69 NetworkStateDetails::State NetworkStateNotifier::RetrieveState() { |
| 50 // Running on desktop means always connected, for now. | 70 // Running on desktop means always connected, for now. |
| 51 if (!CrosLibrary::Get()->EnsureLoaded()) | 71 if (!CrosLibrary::Get()->EnsureLoaded()) |
| 52 return NetworkStateDetails::CONNECTED; | 72 return NetworkStateDetails::CONNECTED; |
| 53 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); | 73 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); |
| 54 if (cros->Connected()) { | 74 if (cros->Connected()) { |
| 55 return NetworkStateDetails::CONNECTED; | 75 return NetworkStateDetails::CONNECTED; |
| 56 } else if (cros->Connecting()) { | 76 } else if (cros->Connecting()) { |
| 57 return NetworkStateDetails::CONNECTING; | 77 return NetworkStateDetails::CONNECTING; |
| 58 } else { | 78 } else { |
| 59 return NetworkStateDetails::DISCONNECTED; | 79 return NetworkStateDetails::DISCONNECTED; |
| 60 } | 80 } |
| 61 } | 81 } |
| 62 | 82 |
| 63 | 83 |
| 64 } // namespace chromeos | 84 } // namespace chromeos |
| OLD | NEW |