1// Copyright (c) 2012 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 "ash/system/chromeos/network/tray_network_state_observer.h"
6
7#include <set>
8#include <string>
9
10#include "base/location.h"
11#include "chromeos/network/network_state.h"
12#include "chromeos/network/network_state_handler.h"
13#include "third_party/cros_system_api/dbus/service_constants.h"
14#include "ui/chromeos/network/network_icon.h"
15
16using chromeos::NetworkHandler;
17
18namespace ash {
19
20TrayNetworkStateObserver::TrayNetworkStateObserver(Delegate* delegate)
21    : delegate_(delegate) {
22  if (NetworkHandler::IsInitialized()) {
23    NetworkHandler::Get()->network_state_handler()->AddObserver(
24        this, FROM_HERE);
25  }
26}
27
28TrayNetworkStateObserver::~TrayNetworkStateObserver() {
29  if (NetworkHandler::IsInitialized()) {
30    NetworkHandler::Get()->network_state_handler()->RemoveObserver(
31        this, FROM_HERE);
32  }
33}
34
35void TrayNetworkStateObserver::NetworkListChanged() {
36  delegate_->NetworkStateChanged(true);
37  ui::network_icon::PurgeNetworkIconCache();
38}
39
40void TrayNetworkStateObserver::DeviceListChanged() {
41  delegate_->NetworkStateChanged(false);
42}
43
44// Any change to the Default (primary connected) network, including Strength
45// changes, should trigger a NetworkStateChanged update.
46void TrayNetworkStateObserver::DefaultNetworkChanged(
47    const chromeos::NetworkState* network) {
48  delegate_->NetworkStateChanged(true);
49}
50
51// Any change to the Connection State should trigger a NetworkStateChanged
52// update. This is important when both a VPN and a physical network are
53// connected.
54void TrayNetworkStateObserver::NetworkConnectionStateChanged(
55    const chromeos::NetworkState* network) {
56  delegate_->NetworkStateChanged(true);
57}
58
59// This tracks Strength and other property changes for all networks. It will
60// be called in addition to NetworkConnectionStateChanged for connection state
61// changes.
62void TrayNetworkStateObserver::NetworkPropertiesUpdated(
63    const chromeos::NetworkState* network) {
64  if (network ==
65      NetworkHandler::Get()->network_state_handler()->DefaultNetwork()) {
66    // Trigger NetworkStateChanged in case the Strength property of the
67    // Default network changed.
68    delegate_->NetworkStateChanged(true);
69  }
70  delegate_->NetworkServiceChanged(network);
71}
72
73}  // namespace ash
74