| 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/ash_switches.h" |
| 7 #include "ash/shell.h" | 8 #include "ash/shell.h" |
| 8 #include "ash/system/chromeos/network/network_observer.h" | 9 #include "ash/system/chromeos/network/network_observer.h" |
| 9 #include "ash/system/tray/system_tray_notifier.h" | 10 #include "ash/system/tray/system_tray_notifier.h" |
| 11 #include "base/command_line.h" |
| 10 #include "base/string16.h" | 12 #include "base/string16.h" |
| 11 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 12 #include "chromeos/network/network_event_log.h" | 14 #include "chromeos/network/network_event_log.h" |
| 13 #include "chromeos/network/network_state.h" | 15 #include "chromeos/network/network_state.h" |
| 14 #include "chromeos/network/network_state_handler.h" | 16 #include "chromeos/network/network_state_handler.h" |
| 15 #include "grit/ash_strings.h" | 17 #include "grit/ash_strings.h" |
| 16 #include "third_party/cros_system_api/dbus/service_constants.h" | 18 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 17 #include "ui/base/l10n/l10n_util.h" | 19 #include "ui/base/l10n/l10n_util.h" |
| 18 | 20 |
| 21 using chromeos::NetworkState; |
| 22 using chromeos::NetworkStateHandler; |
| 23 |
| 19 namespace { | 24 namespace { |
| 25 |
| 20 const char kLogModule[] = "NetworkStateNotifier"; | 26 const char kLogModule[] = "NetworkStateNotifier"; |
| 21 | 27 |
| 22 ash::NetworkObserver::NetworkType GetAshNetworkType(const std::string& type) { | 28 const int kMinTimeBetweenOutOfCreditsNotifySeconds = 10 * 60; |
| 23 if (type == flimflam::kTypeCellular) | 29 |
| 24 return ash::NetworkObserver::NETWORK_CELLULAR; | 30 ash::NetworkObserver::NetworkType GetAshNetworkType( |
| 31 const NetworkState* network) { |
| 32 const std::string& type = network->type(); |
| 33 if (type == flimflam::kTypeCellular) { |
| 34 if (network->technology() == flimflam::kNetworkTechnologyLte || |
| 35 network->technology() == flimflam::kNetworkTechnologyLteAdvanced) |
| 36 return ash::NetworkObserver::NETWORK_CELLULAR_LTE; |
| 37 else |
| 38 return ash::NetworkObserver::NETWORK_CELLULAR; |
| 39 } |
| 25 if (type == flimflam::kTypeEthernet) | 40 if (type == flimflam::kTypeEthernet) |
| 26 return ash::NetworkObserver::NETWORK_ETHERNET; | 41 return ash::NetworkObserver::NETWORK_ETHERNET; |
| 27 if (type == flimflam::kTypeWifi) | 42 if (type == flimflam::kTypeWifi) |
| 28 return ash::NetworkObserver::NETWORK_WIFI; | 43 return ash::NetworkObserver::NETWORK_WIFI; |
| 29 if (type == flimflam::kTypeBluetooth) | 44 if (type == flimflam::kTypeBluetooth) |
| 30 return ash::NetworkObserver::NETWORK_BLUETOOTH; | 45 return ash::NetworkObserver::NETWORK_BLUETOOTH; |
| 31 return ash::NetworkObserver::NETWORK_UNKNOWN; | 46 return ash::NetworkObserver::NETWORK_UNKNOWN; |
| 32 } | 47 } |
| 33 | 48 |
| 34 string16 GetErrorString(const std::string& error) { | 49 string16 GetErrorString(const std::string& error) { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 return l10n_util::GetStringUTF16( | 95 return l10n_util::GetStringUTF16( |
| 81 IDS_CHROMEOS_NETWORK_ERROR_PPP_AUTH_FAILED); | 96 IDS_CHROMEOS_NETWORK_ERROR_PPP_AUTH_FAILED); |
| 82 } | 97 } |
| 83 if (error == flimflam::kUnknownString) | 98 if (error == flimflam::kUnknownString) |
| 84 return l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_UNKNOWN); | 99 return l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_UNKNOWN); |
| 85 return l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_STATE_UNRECOGNIZED); | 100 return l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_STATE_UNRECOGNIZED); |
| 86 } | 101 } |
| 87 | 102 |
| 88 } // namespace | 103 } // namespace |
| 89 | 104 |
| 90 using chromeos::NetworkState; | |
| 91 using chromeos::NetworkStateHandler; | |
| 92 | |
| 93 namespace ash { | 105 namespace ash { |
| 94 namespace internal { | 106 namespace internal { |
| 95 | 107 |
| 96 NetworkStateNotifier::NetworkStateNotifier() { | 108 NetworkStateNotifier::NetworkStateNotifier() |
| 97 if (NetworkStateHandler::Get()) { | 109 : cellular_out_of_credits_(false) { |
| 98 NetworkStateHandler::Get()->AddObserver(this); | 110 if (!NetworkStateHandler::Get()) |
| 99 InitializeNetworks(); | 111 return; |
| 100 } | 112 NetworkStateHandler::Get()->AddObserver(this); |
| 113 InitializeNetworks(); |
| 101 } | 114 } |
| 102 | 115 |
| 103 NetworkStateNotifier::~NetworkStateNotifier() { | 116 NetworkStateNotifier::~NetworkStateNotifier() { |
| 104 if (NetworkStateHandler::Get()) | 117 if (NetworkStateHandler::Get()) |
| 105 NetworkStateHandler::Get()->RemoveObserver(this); | 118 NetworkStateHandler::Get()->RemoveObserver(this); |
| 106 } | 119 } |
| 107 | 120 |
| 121 void NetworkStateNotifier::DefaultNetworkChanged(const NetworkState* network) { |
| 122 if (network) |
| 123 last_default_network_ = network->path(); |
| 124 } |
| 125 |
| 108 void NetworkStateNotifier::NetworkConnectionStateChanged( | 126 void NetworkStateNotifier::NetworkConnectionStateChanged( |
| 109 const NetworkState* network) { | 127 const NetworkState* network) { |
| 110 NetworkStateHandler* handler = NetworkStateHandler::Get(); | 128 NetworkStateHandler* handler = NetworkStateHandler::Get(); |
| 111 std::string prev_state; | 129 std::string prev_state; |
| 112 std::string new_state = network->connection_state(); | 130 std::string new_state = network->connection_state(); |
| 113 CachedStateMap::iterator iter = cached_state_.find(network->path()); | 131 CachedStateMap::iterator iter = cached_state_.find(network->path()); |
| 114 if (iter != cached_state_.end()) { | 132 if (iter != cached_state_.end()) { |
| 115 prev_state = iter->second; | 133 prev_state = iter->second; |
| 116 if (prev_state == new_state) | 134 if (prev_state == new_state) |
| 117 return; // No state change | 135 return; // No state change |
| (...skipping 18 matching lines...) Expand all Loading... |
| 136 // Connecting -> Idle without an error shouldn't happen but sometimes does. | 154 // Connecting -> Idle without an error shouldn't happen but sometimes does. |
| 137 notify_failure = true; | 155 notify_failure = true; |
| 138 } | 156 } |
| 139 if (!notify_failure) | 157 if (!notify_failure) |
| 140 return; | 158 return; |
| 141 | 159 |
| 142 chromeos::network_event_log::AddEntry( | 160 chromeos::network_event_log::AddEntry( |
| 143 kLogModule, "ConnectionFailure", network->path()); | 161 kLogModule, "ConnectionFailure", network->path()); |
| 144 | 162 |
| 145 std::vector<string16> no_links; | 163 std::vector<string16> no_links; |
| 146 ash::NetworkObserver::NetworkType network_type = | 164 ash::NetworkObserver::NetworkType network_type = GetAshNetworkType(network); |
| 147 GetAshNetworkType(network->type()); | |
| 148 string16 error = GetErrorString(network->error()); | 165 string16 error = GetErrorString(network->error()); |
| 149 ash::Shell::GetInstance()->system_tray_notifier()->NotifySetNetworkMessage( | 166 ash::Shell::GetInstance()->system_tray_notifier()->NotifySetNetworkMessage( |
| 150 NULL, ash::NetworkObserver::ERROR_CONNECT_FAILED, network_type, | 167 this, ash::NetworkObserver::ERROR_CONNECT_FAILED, network_type, |
| 151 l10n_util::GetStringUTF16(IDS_NETWORK_CONNECTION_ERROR_TITLE), | 168 l10n_util::GetStringUTF16(IDS_NETWORK_CONNECTION_ERROR_TITLE), |
| 152 l10n_util::GetStringFUTF16( | 169 l10n_util::GetStringFUTF16( |
| 153 IDS_NETWORK_CONNECTION_ERROR_MESSAGE_WITH_DETAILS, | 170 IDS_NETWORK_CONNECTION_ERROR_MESSAGE_WITH_DETAILS, |
| 154 UTF8ToUTF16(network->name()), error), | 171 UTF8ToUTF16(network->name()), error), |
| 155 no_links); | 172 no_links); |
| 156 } | 173 } |
| 157 | 174 |
| 175 void NetworkStateNotifier::NetworkPropertiesUpdated( |
| 176 const NetworkState* network) { |
| 177 DCHECK(network); |
| 178 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 179 ash::switches::kAshDisableNewNetworkStatusArea)) { |
| 180 return; |
| 181 } |
| 182 // Trigger "Out of credits" notification if the cellular network is the most |
| 183 // recent default network (i.e. we have not switched to another network). |
| 184 if (network->type() == flimflam::kTypeCellular && |
| 185 network->path() == last_default_network_) { |
| 186 cellular_network_ = network->path(); |
| 187 if (network->cellular_out_of_credits() && |
| 188 !cellular_out_of_credits_) { |
| 189 cellular_out_of_credits_ = true; |
| 190 base::TimeDelta dtime = base::Time::Now() - out_of_credits_notify_time_; |
| 191 if (dtime.InSeconds() > kMinTimeBetweenOutOfCreditsNotifySeconds) { |
| 192 out_of_credits_notify_time_ = base::Time::Now(); |
| 193 ash::NetworkObserver::NetworkType network_type = |
| 194 GetAshNetworkType(network); |
| 195 std::vector<string16> links; |
| 196 links.push_back( |
| 197 l10n_util::GetStringFUTF16(IDS_NETWORK_OUT_OF_CREDITS_LINK, |
| 198 UTF8ToUTF16(network->name()))); |
| 199 ash::Shell::GetInstance()->system_tray_notifier()-> |
| 200 NotifySetNetworkMessage( |
| 201 this, ash::NetworkObserver::ERROR_OUT_OF_CREDITS, network_type, |
| 202 l10n_util::GetStringUTF16(IDS_NETWORK_OUT_OF_CREDITS_TITLE), |
| 203 l10n_util::GetStringUTF16(IDS_NETWORK_OUT_OF_CREDITS_BODY), |
| 204 links); |
| 205 } |
| 206 } else if (!network->cellular_out_of_credits() && |
| 207 cellular_out_of_credits_) { |
| 208 cellular_out_of_credits_ = false; |
| 209 } |
| 210 } |
| 211 } |
| 212 |
| 213 void NetworkStateNotifier::NotificationLinkClicked( |
| 214 NetworkObserver::MessageType message_type, |
| 215 size_t link_index) { |
| 216 if (message_type == ash::NetworkObserver::ERROR_OUT_OF_CREDITS) { |
| 217 if (!cellular_network_.empty()) { |
| 218 // This will trigger the activation / portal code. |
| 219 Shell::GetInstance()->system_tray_delegate()->ConnectToNetwork( |
| 220 cellular_network_); |
| 221 } |
| 222 ash::Shell::GetInstance()->system_tray_notifier()-> |
| 223 NotifyClearNetworkMessage(message_type); |
| 224 } |
| 225 } |
| 226 |
| 158 void NetworkStateNotifier::InitializeNetworks() { | 227 void NetworkStateNotifier::InitializeNetworks() { |
| 159 NetworkStateList network_list; | 228 NetworkStateList network_list; |
| 160 NetworkStateHandler::Get()->GetNetworkList(&network_list); | 229 NetworkStateHandler::Get()->GetNetworkList(&network_list); |
| 161 VLOG(1) << "NetworkStateNotifier:InitializeNetworks: " | 230 VLOG(1) << "NetworkStateNotifier:InitializeNetworks: " |
| 162 << network_list.size(); | 231 << network_list.size(); |
| 163 for (NetworkStateList::iterator iter = network_list.begin(); | 232 for (NetworkStateList::iterator iter = network_list.begin(); |
| 164 iter != network_list.end(); ++iter) { | 233 iter != network_list.end(); ++iter) { |
| 165 const NetworkState* network = *iter; | 234 const NetworkState* network = *iter; |
| 166 VLOG(2) << " Network: " << network->path(); | 235 VLOG(2) << " Network: " << network->path(); |
| 167 cached_state_[network->path()] = network->connection_state(); | 236 cached_state_[network->path()] = network->connection_state(); |
| 168 } | 237 } |
| 238 const NetworkState* default_network = |
| 239 NetworkStateHandler::Get()->DefaultNetwork(); |
| 240 if (default_network) |
| 241 last_default_network_ = default_network->path(); |
| 169 } | 242 } |
| 170 | 243 |
| 171 } // namespace internal | 244 } // namespace internal |
| 172 } // namespace ash | 245 } // namespace ash |
| OLD | NEW |