OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "ash/system/chromeos/network/network_state_notifier.h" | 5 #include "ash/system/chromeos/network/network_state_notifier.h" |
6 | 6 |
7 #include "ash/shell.h" | 7 #include "ash/shell.h" |
8 #include "ash/system/chromeos/network/network_connect.h" | 8 #include "ash/system/chromeos/network/network_connect.h" |
9 #include "ash/system/chromeos/network/network_observer.h" | 9 #include "ash/system/chromeos/network/network_observer.h" |
10 #include "ash/system/tray/system_tray_notifier.h" | 10 #include "ash/system/tray/system_tray_notifier.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 using chromeos::NetworkStateHandler; | 25 using chromeos::NetworkStateHandler; |
26 | 26 |
27 namespace { | 27 namespace { |
28 | 28 |
29 const int kMinTimeBetweenOutOfCreditsNotifySeconds = 10 * 60; | 29 const int kMinTimeBetweenOutOfCreditsNotifySeconds = 10 * 60; |
30 | 30 |
31 // Error messages based on |error_name|, not network_state->error(). | 31 // Error messages based on |error_name|, not network_state->error(). |
32 string16 GetConnectErrorString(const std::string& error_name) { | 32 string16 GetConnectErrorString(const std::string& error_name) { |
33 if (error_name == NetworkConnectionHandler::kErrorNotFound) | 33 if (error_name == NetworkConnectionHandler::kErrorNotFound) |
34 return l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_CONNECT_FAILED); | 34 return l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_CONNECT_FAILED); |
| 35 if (error_name == NetworkConnectionHandler::kErrorConfigureFailed) |
| 36 return l10n_util::GetStringUTF16( |
| 37 IDS_CHROMEOS_NETWORK_ERROR_CONFIGURE_FAILED); |
| 38 if (error_name == NetworkConnectionHandler::kErrorActivateFailed) |
| 39 return l10n_util::GetStringUTF16( |
| 40 IDS_CHROMEOS_NETWORK_ERROR_ACTIVATION_FAILED); |
35 return string16(); | 41 return string16(); |
36 } | 42 } |
37 | 43 |
38 } // namespace | 44 } // namespace |
39 | 45 |
40 namespace ash { | 46 namespace ash { |
41 | 47 |
42 NetworkStateNotifier::NetworkStateNotifier() | 48 NetworkStateNotifier::NetworkStateNotifier() |
43 : cellular_out_of_credits_(false) { | 49 : cellular_out_of_credits_(false) { |
44 if (NetworkHandler::IsInitialized()) { | 50 if (!NetworkHandler::IsInitialized()) |
45 NetworkHandler::Get()->network_state_handler()->AddObserver( | 51 return; |
46 this, FROM_HERE); | 52 NetworkHandler::Get()->network_state_handler()->AddObserver(this, FROM_HERE); |
47 InitializeNetworks(); | 53 |
| 54 // Initialize |last_active_network_|. |
| 55 const NetworkState* default_network = |
| 56 NetworkHandler::Get()->network_state_handler()->DefaultNetwork(); |
| 57 if (default_network && default_network->IsConnectedState()) |
| 58 last_active_network_ = default_network->path(); |
| 59 } |
| 60 |
| 61 NetworkStateNotifier::~NetworkStateNotifier() { |
| 62 if (!NetworkHandler::IsInitialized()) |
| 63 return; |
| 64 NetworkHandler::Get()->network_state_handler()->RemoveObserver( |
| 65 this, FROM_HERE); |
| 66 } |
| 67 |
| 68 void NetworkStateNotifier::NetworkListChanged() { |
| 69 // Trigger any pending connect failed error if the network list changes |
| 70 // (which indicates all NetworkState entries are up to date). This is in |
| 71 // case a connect attempt fails because a network is no longer visible. |
| 72 if (!connect_failed_network_.empty()) { |
| 73 ShowNetworkConnectError( |
| 74 NetworkConnectionHandler::kErrorConnectFailed, connect_failed_network_); |
48 } | 75 } |
49 } | 76 } |
50 | 77 |
51 NetworkStateNotifier::~NetworkStateNotifier() { | |
52 if (NetworkHandler::IsInitialized()) { | |
53 NetworkHandler::Get()->network_state_handler()->RemoveObserver( | |
54 this, FROM_HERE); | |
55 } | |
56 } | |
57 | |
58 void NetworkStateNotifier::DefaultNetworkChanged(const NetworkState* network) { | 78 void NetworkStateNotifier::DefaultNetworkChanged(const NetworkState* network) { |
59 if (!network || !network->IsConnectedState()) | 79 if (!network || !network->IsConnectedState()) |
60 return; | 80 return; |
61 if (network->path() != last_active_network_) { | 81 if (network->path() != last_active_network_) { |
62 last_active_network_ = network->path(); | 82 last_active_network_ = network->path(); |
63 // Reset state for new connected network | 83 // Reset state for new connected network |
64 cellular_out_of_credits_ = false; | 84 cellular_out_of_credits_ = false; |
65 } | 85 } |
66 } | 86 } |
67 | 87 |
68 void NetworkStateNotifier::NetworkConnectionStateChanged( | |
69 const NetworkState* network) { | |
70 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler(); | |
71 std::string prev_state; | |
72 std::string new_state = network->connection_state(); | |
73 CachedStateMap::iterator iter = cached_state_.find(network->path()); | |
74 if (iter != cached_state_.end()) { | |
75 prev_state = iter->second; | |
76 if (prev_state == new_state) | |
77 return; // No state change | |
78 VLOG(1) << "NetworkStateNotifier: State: " << prev_state | |
79 << " ->: " << new_state; | |
80 iter->second = new_state; | |
81 } else { | |
82 VLOG(1) << "NetworkStateNotifier: New Service: " << network->path() | |
83 << " State: " << new_state; | |
84 cached_state_[network->path()] = new_state; | |
85 return; // New network, no state change | |
86 } | |
87 | |
88 if (new_state != flimflam::kStateFailure) | |
89 return; | |
90 | |
91 if (network->path() != handler->connecting_network()) | |
92 return; // Only show notifications for explicitly connected networks | |
93 | |
94 NET_LOG_EVENT("ConnectionFailure", network->path()); | |
95 | |
96 ShowConnectError("", network); | |
97 } | |
98 | |
99 void NetworkStateNotifier::NetworkPropertiesUpdated( | 88 void NetworkStateNotifier::NetworkPropertiesUpdated( |
100 const NetworkState* network) { | 89 const NetworkState* network) { |
101 DCHECK(network); | 90 DCHECK(network); |
| 91 // Trigger a pending connect failed error for |network| when the Error |
| 92 // property has been set. |
| 93 if (network->path() == connect_failed_network_ && !network->error().empty()) { |
| 94 ShowNetworkConnectError( |
| 95 NetworkConnectionHandler::kErrorConnectFailed, connect_failed_network_); |
| 96 } |
102 // Trigger "Out of credits" notification if the cellular network is the most | 97 // Trigger "Out of credits" notification if the cellular network is the most |
103 // recent default network (i.e. we have not switched to another network). | 98 // recent default network (i.e. we have not switched to another network). |
104 if (network->type() == flimflam::kTypeCellular && | 99 if (network->type() == flimflam::kTypeCellular && |
105 network->path() == last_active_network_) { | 100 network->path() == last_active_network_) { |
106 cellular_network_ = network->path(); | 101 cellular_network_ = network->path(); |
107 if (network->cellular_out_of_credits() && | 102 if (network->cellular_out_of_credits() && |
108 !cellular_out_of_credits_) { | 103 !cellular_out_of_credits_) { |
109 cellular_out_of_credits_ = true; | 104 cellular_out_of_credits_ = true; |
110 base::TimeDelta dtime = base::Time::Now() - out_of_credits_notify_time_; | 105 base::TimeDelta dtime = base::Time::Now() - out_of_credits_notify_time_; |
111 if (dtime.InSeconds() > kMinTimeBetweenOutOfCreditsNotifySeconds) { | 106 if (dtime.InSeconds() > kMinTimeBetweenOutOfCreditsNotifySeconds) { |
(...skipping 27 matching lines...) Expand all Loading... |
139 ash::Shell::GetInstance()->system_tray_notifier()-> | 134 ash::Shell::GetInstance()->system_tray_notifier()-> |
140 NotifyClearNetworkMessage(message_type); | 135 NotifyClearNetworkMessage(message_type); |
141 } | 136 } |
142 } | 137 } |
143 | 138 |
144 void NetworkStateNotifier::ShowNetworkConnectError( | 139 void NetworkStateNotifier::ShowNetworkConnectError( |
145 const std::string& error_name, | 140 const std::string& error_name, |
146 const std::string& service_path) { | 141 const std::string& service_path) { |
147 const NetworkState* network = NetworkHandler::Get()->network_state_handler()-> | 142 const NetworkState* network = NetworkHandler::Get()->network_state_handler()-> |
148 GetNetworkState(service_path); | 143 GetNetworkState(service_path); |
149 ShowConnectError(error_name, network); | 144 if (error_name == NetworkConnectionHandler::kErrorConnectFailed && |
150 } | 145 service_path != connect_failed_network_) { |
| 146 // Shill may not have set the Error property yet. First request an update |
| 147 // and wait for either the update to complete or the network list to be |
| 148 // updated before displaying the error. |
| 149 connect_failed_network_ = service_path; |
| 150 return; |
| 151 } |
| 152 connect_failed_network_.clear(); |
151 | 153 |
152 void NetworkStateNotifier::ShowConnectError(const std::string& error_name, | |
153 const NetworkState* network) { | |
154 std::vector<string16> no_links; | |
155 string16 error = GetConnectErrorString(error_name); | 154 string16 error = GetConnectErrorString(error_name); |
156 if (error.empty() && network) | 155 if (error.empty() && network) |
157 error = network_connect::ErrorString(network->error()); | 156 error = network_connect::ErrorString(network->error()); |
158 if (error.empty()) | 157 if (error.empty()) |
159 error = l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_UNKNOWN); | 158 error = l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_UNKNOWN); |
| 159 NET_LOG_ERROR("Connect error notification: " + UTF16ToUTF8(error), |
| 160 service_path); |
| 161 |
160 std::string name = network ? network->name() : ""; | 162 std::string name = network ? network->name() : ""; |
161 string16 error_msg; | 163 string16 error_msg; |
162 if (network && !network->error_details().empty()) { | 164 if (network && !network->error_details().empty()) { |
163 error_msg = l10n_util::GetStringFUTF16( | 165 error_msg = l10n_util::GetStringFUTF16( |
164 IDS_NETWORK_CONNECTION_ERROR_MESSAGE_WITH_SERVER_MESSAGE, | 166 IDS_NETWORK_CONNECTION_ERROR_MESSAGE_WITH_SERVER_MESSAGE, |
165 UTF8ToUTF16(name), error, UTF8ToUTF16(network->error_details())); | 167 UTF8ToUTF16(name), error, UTF8ToUTF16(network->error_details())); |
166 } else { | 168 } else { |
167 error_msg = l10n_util::GetStringFUTF16( | 169 error_msg = l10n_util::GetStringFUTF16( |
168 IDS_NETWORK_CONNECTION_ERROR_MESSAGE_WITH_DETAILS, | 170 IDS_NETWORK_CONNECTION_ERROR_MESSAGE_WITH_DETAILS, |
169 UTF8ToUTF16(name), error); | 171 UTF8ToUTF16(name), error); |
170 } | 172 } |
| 173 |
| 174 std::vector<string16> no_links; |
171 ash::Shell::GetInstance()->system_tray_notifier()->NotifySetNetworkMessage( | 175 ash::Shell::GetInstance()->system_tray_notifier()->NotifySetNetworkMessage( |
172 this, | 176 this, |
173 NetworkObserver::ERROR_CONNECT_FAILED, | 177 NetworkObserver::ERROR_CONNECT_FAILED, |
174 NetworkObserver::GetNetworkTypeForNetworkState(network), | 178 NetworkObserver::GetNetworkTypeForNetworkState(network), |
175 l10n_util::GetStringUTF16(IDS_NETWORK_CONNECTION_ERROR_TITLE), | 179 l10n_util::GetStringUTF16(IDS_NETWORK_CONNECTION_ERROR_TITLE), |
176 error_msg, | 180 error_msg, |
177 no_links); | 181 no_links); |
178 } | 182 } |
179 | 183 |
180 void NetworkStateNotifier::InitializeNetworks() { | |
181 NetworkStateHandler::NetworkStateList network_list; | |
182 NetworkHandler::Get()->network_state_handler()->GetNetworkList(&network_list); | |
183 VLOG(1) << "NetworkStateNotifier:InitializeNetworks: " | |
184 << network_list.size(); | |
185 for (NetworkStateHandler::NetworkStateList::iterator iter = | |
186 network_list.begin(); iter != network_list.end(); ++iter) { | |
187 const NetworkState* network = *iter; | |
188 VLOG(2) << " Network: " << network->path(); | |
189 cached_state_[network->path()] = network->connection_state(); | |
190 } | |
191 const NetworkState* default_network = | |
192 NetworkHandler::Get()->network_state_handler()->DefaultNetwork(); | |
193 if (default_network && default_network->IsConnectedState()) | |
194 last_active_network_ = default_network->path(); | |
195 } | |
196 | |
197 } // namespace ash | 184 } // namespace ash |
OLD | NEW |