summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/network_state_notifier.cc
blob: 954752b1bbd4b761ffae18478dec0e3796194dab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/network_state_notifier.h"

#include "base/message_loop.h"
#include "base/time.h"
#include "chrome/browser/browser_thread.h"
#include "chrome/browser/chromeos/cros/cros_library.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/notification_type.h"

namespace chromeos {

using base::Time;
using base::TimeDelta;

// static
NetworkStateNotifier* NetworkStateNotifier::Get() {
  return Singleton<NetworkStateNotifier>::get();
}

// static
TimeDelta NetworkStateNotifier::GetOfflineDuration() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  // TODO(oshima): make this instance method so that
  // we can mock this for ui_tests.
  // http://crbug.com/4825 .
  return base::Time::Now() - Get()->offline_start_time_;
}

NetworkStateNotifier::NetworkStateNotifier()
    : ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)),
      state_(RetrieveState()),
      offline_start_time_(Time::Now()) {
  // Note that this gets added as a NetworkManagerObserver
  // in browser_init.cc
}

NetworkStateNotifier::~NetworkStateNotifier() {
  // Let the NetworkManagerObserver leak to avoid a DCHECK
  // failure in CommandLine::ForCurrentProcess.
//  if (CrosLibrary::Get()->EnsureLoaded())
//    CrosLibrary::Get()->GetNetworkLibrary()->
//        RemoveNetworkManagerObserver(this);
}

void NetworkStateNotifier::OnNetworkManagerChanged(NetworkLibrary* cros) {
  DCHECK(CrosLibrary::Get()->EnsureLoaded());
  BrowserThread::PostTask(
      BrowserThread::UI, FROM_HERE,
      task_factory_.NewRunnableMethod(
          &NetworkStateNotifier::UpdateNetworkState,
          RetrieveState()));
}

void NetworkStateNotifier::UpdateNetworkState(
    NetworkStateDetails::State new_state) {
  DLOG(INFO) << "UpdateNetworkState: new="
             << new_state << ", old=" << state_;
  if (state_ == NetworkStateDetails::CONNECTED &&
      new_state != NetworkStateDetails::CONNECTED) {
    offline_start_time_ = Time::Now();
  }

  state_ = new_state;
  NetworkStateDetails details(state_);
  NotificationService::current()->Notify(
      NotificationType::NETWORK_STATE_CHANGED,
      NotificationService::AllSources(),
      Details<NetworkStateDetails>(&details));
};

// static
NetworkStateDetails::State NetworkStateNotifier::RetrieveState() {
  // Running on desktop means always connected, for now.
  if (!CrosLibrary::Get()->EnsureLoaded())
    return NetworkStateDetails::CONNECTED;
  NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary();
  if (cros->Connected()) {
    return NetworkStateDetails::CONNECTED;
  } else if (cros->Connecting()) {
    return NetworkStateDetails::CONNECTING;
  } else {
    return NetworkStateDetails::DISCONNECTED;
  }
}


}  // namespace chromeos