Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(162)

Side by Side Diff: ash/system/chromeos/network/network_state_notifier.cc

Issue 22796014: Eliminate c/b/chromeos/options/network_connect.cc (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
11 #include "base/strings/string16.h" 11 #include "base/strings/string16.h"
12 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
14 #include "chromeos/network/network_configuration_handler.h"
14 #include "chromeos/network/network_connection_handler.h" 15 #include "chromeos/network/network_connection_handler.h"
15 #include "chromeos/network/network_event_log.h" 16 #include "chromeos/network/network_event_log.h"
16 #include "chromeos/network/network_state.h" 17 #include "chromeos/network/network_state.h"
17 #include "chromeos/network/network_state_handler.h" 18 #include "chromeos/network/network_state_handler.h"
18 #include "grit/ash_strings.h" 19 #include "grit/ash_strings.h"
19 #include "third_party/cros_system_api/dbus/service_constants.h" 20 #include "third_party/cros_system_api/dbus/service_constants.h"
20 #include "ui/base/l10n/l10n_util.h" 21 #include "ui/base/l10n/l10n_util.h"
21 22
22 using chromeos::NetworkConnectionHandler; 23 using chromeos::NetworkConnectionHandler;
23 using chromeos::NetworkHandler; 24 using chromeos::NetworkHandler;
(...skipping 15 matching lines...) Expand all
39 return l10n_util::GetStringUTF16( 40 return l10n_util::GetStringUTF16(
40 IDS_CHROMEOS_NETWORK_ERROR_ACTIVATION_FAILED); 41 IDS_CHROMEOS_NETWORK_ERROR_ACTIVATION_FAILED);
41 return string16(); 42 return string16();
42 } 43 }
43 44
44 } // namespace 45 } // namespace
45 46
46 namespace ash { 47 namespace ash {
47 48
48 NetworkStateNotifier::NetworkStateNotifier() 49 NetworkStateNotifier::NetworkStateNotifier()
49 : cellular_out_of_credits_(false) { 50 : cellular_out_of_credits_(false),
51 weak_ptr_factory_(this) {
50 if (!NetworkHandler::IsInitialized()) 52 if (!NetworkHandler::IsInitialized())
51 return; 53 return;
52 NetworkHandler::Get()->network_state_handler()->AddObserver(this, FROM_HERE); 54 NetworkHandler::Get()->network_state_handler()->AddObserver(this, FROM_HERE);
53 55
54 // Initialize |last_active_network_|. 56 // Initialize |last_active_network_|.
55 const NetworkState* default_network = 57 const NetworkState* default_network =
56 NetworkHandler::Get()->network_state_handler()->DefaultNetwork(); 58 NetworkHandler::Get()->network_state_handler()->DefaultNetwork();
57 if (default_network && default_network->IsConnectedState()) 59 if (default_network && default_network->IsConnectedState())
58 last_active_network_ = default_network->path(); 60 last_active_network_ = default_network->path();
59 } 61 }
60 62
61 NetworkStateNotifier::~NetworkStateNotifier() { 63 NetworkStateNotifier::~NetworkStateNotifier() {
62 if (!NetworkHandler::IsInitialized()) 64 if (!NetworkHandler::IsInitialized())
63 return; 65 return;
64 NetworkHandler::Get()->network_state_handler()->RemoveObserver( 66 NetworkHandler::Get()->network_state_handler()->RemoveObserver(
65 this, FROM_HERE); 67 this, FROM_HERE);
66 } 68 }
67 69
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_);
75 }
76 }
77
78 void NetworkStateNotifier::DefaultNetworkChanged(const NetworkState* network) { 70 void NetworkStateNotifier::DefaultNetworkChanged(const NetworkState* network) {
79 if (!network || !network->IsConnectedState()) 71 if (!network || !network->IsConnectedState())
80 return; 72 return;
81 if (network->path() != last_active_network_) { 73 if (network->path() != last_active_network_) {
82 last_active_network_ = network->path(); 74 last_active_network_ = network->path();
83 // Reset state for new connected network 75 // Reset state for new connected network
84 cellular_out_of_credits_ = false; 76 cellular_out_of_credits_ = false;
77 NetworkPropertiesUpdated(network);
85 } 78 }
86 } 79 }
87 80
88 void NetworkStateNotifier::NetworkPropertiesUpdated( 81 void NetworkStateNotifier::NetworkPropertiesUpdated(
89 const NetworkState* network) { 82 const NetworkState* 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 }
97 // Trigger "Out of credits" notification if the cellular network is the most 83 // Trigger "Out of credits" notification if the cellular network is the most
98 // recent default network (i.e. we have not switched to another network). 84 // recent default network (i.e. we have not switched to another network).
99 if (network->type() == flimflam::kTypeCellular && 85 if (network->type() == flimflam::kTypeCellular &&
100 network->path() == last_active_network_) { 86 network->path() == last_active_network_) {
101 cellular_network_ = network->path(); 87 cellular_network_ = network->path();
102 if (network->cellular_out_of_credits() && 88 if (network->cellular_out_of_credits() &&
103 !cellular_out_of_credits_) { 89 !cellular_out_of_credits_) {
104 cellular_out_of_credits_ = true; 90 cellular_out_of_credits_ = true;
105 base::TimeDelta dtime = base::Time::Now() - out_of_credits_notify_time_; 91 base::TimeDelta dtime = base::Time::Now() - out_of_credits_notify_time_;
106 if (dtime.InSeconds() > kMinTimeBetweenOutOfCreditsNotifySeconds) { 92 if (dtime.InSeconds() > kMinTimeBetweenOutOfCreditsNotifySeconds) {
(...skipping 25 matching lines...) Expand all
132 cellular_network_); 118 cellular_network_);
133 } 119 }
134 ash::Shell::GetInstance()->system_tray_notifier()-> 120 ash::Shell::GetInstance()->system_tray_notifier()->
135 NotifyClearNetworkMessage(message_type); 121 NotifyClearNetworkMessage(message_type);
136 } 122 }
137 } 123 }
138 124
139 void NetworkStateNotifier::ShowNetworkConnectError( 125 void NetworkStateNotifier::ShowNetworkConnectError(
140 const std::string& error_name, 126 const std::string& error_name,
141 const std::string& service_path) { 127 const std::string& service_path) {
142 const NetworkState* network = NetworkHandler::Get()->network_state_handler()-> 128 // Get the up-to-date properties for the network and display the error.
143 GetNetworkState(service_path); 129 NetworkHandler::Get()->network_configuration_handler()->GetProperties(
144 if (error_name == NetworkConnectionHandler::kErrorConnectFailed && 130 service_path,
145 service_path != connect_failed_network_) { 131 base::Bind(&NetworkStateNotifier::ConnectErrorPropertiesSucceeded,
146 // Shill may not have set the Error property yet. First request an update 132 weak_ptr_factory_.GetWeakPtr(), error_name),
147 // and wait for either the update to complete or the network list to be 133 base::Bind(&NetworkStateNotifier::ConnectErrorPropertiesFailed,
148 // updated before displaying the error. 134 weak_ptr_factory_.GetWeakPtr(), error_name, service_path));
149 connect_failed_network_ = service_path; 135 }
150 return; 136
137 void NetworkStateNotifier::ConnectErrorPropertiesSucceeded(
138 const std::string& error_name,
139 const std::string& service_path,
140 const base::DictionaryValue& shill_properties) {
141 ShowConnectErrorNotification(error_name, service_path, shill_properties);
142 }
143
144 void NetworkStateNotifier::ConnectErrorPropertiesFailed(
145 const std::string& error_name,
146 const std::string& service_path,
147 const std::string& shill_error_name,
148 scoped_ptr<base::DictionaryValue> shill_error_data) {
149 base::DictionaryValue shill_properties;
150 ShowConnectErrorNotification(error_name, service_path, shill_properties);
151 }
152
153 void NetworkStateNotifier::ShowConnectErrorNotification(
154 const std::string& error_name,
155 const std::string& service_path,
156 const base::DictionaryValue& shill_properties) {
157 string16 error = GetConnectErrorString(error_name);
158 if (error.empty()) {
159 std::string network_error;
160 shill_properties.GetStringWithoutPathExpansion(
161 flimflam::kErrorProperty, &network_error);
162 error = network_connect::ErrorString(network_error);
163 if (error.empty())
164 error = l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_UNKNOWN);
151 } 165 }
152 connect_failed_network_.clear();
153
154 string16 error = GetConnectErrorString(error_name);
155 if (error.empty() && network)
156 error = network_connect::ErrorString(network->error());
157 if (error.empty())
158 error = l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_UNKNOWN);
159 NET_LOG_ERROR("Connect error notification: " + UTF16ToUTF8(error), 166 NET_LOG_ERROR("Connect error notification: " + UTF16ToUTF8(error),
160 service_path); 167 service_path);
161 168
162 std::string name = network ? network->name() : ""; 169 std::string network_name =
170 NetworkState::GetNameFromProperties(service_path, shill_properties);
171 std::string network_error_details;
172 shill_properties.GetStringWithoutPathExpansion(
173 shill::kErrorDetailsProperty, &network_error_details);
174
163 string16 error_msg; 175 string16 error_msg;
164 if (network && !network->error_details().empty()) { 176 if (!network_error_details.empty()) {
165 error_msg = l10n_util::GetStringFUTF16( 177 error_msg = l10n_util::GetStringFUTF16(
166 IDS_NETWORK_CONNECTION_ERROR_MESSAGE_WITH_SERVER_MESSAGE, 178 IDS_NETWORK_CONNECTION_ERROR_MESSAGE_WITH_SERVER_MESSAGE,
167 UTF8ToUTF16(name), error, UTF8ToUTF16(network->error_details())); 179 UTF8ToUTF16(network_name), error,
180 UTF8ToUTF16(network_error_details));
168 } else { 181 } else {
169 error_msg = l10n_util::GetStringFUTF16( 182 error_msg = l10n_util::GetStringFUTF16(
170 IDS_NETWORK_CONNECTION_ERROR_MESSAGE_WITH_DETAILS, 183 IDS_NETWORK_CONNECTION_ERROR_MESSAGE_WITH_DETAILS,
171 UTF8ToUTF16(name), error); 184 UTF8ToUTF16(network_name), error);
185 }
186
187 std::string network_type;
188 shill_properties.GetStringWithoutPathExpansion(
189 flimflam::kTypeProperty, &network_type);
190
191 NetworkObserver::NetworkType type = NetworkObserver::NETWORK_UNKNOWN;
192 if (network_type == flimflam::kTypeCellular) {
193 std::string network_technology;
194 shill_properties.GetStringWithoutPathExpansion(
195 flimflam::kNetworkTechnologyProperty, &network_technology);
196 if (network_technology == flimflam::kNetworkTechnologyLte ||
197 network_technology == flimflam::kNetworkTechnologyLteAdvanced)
198 type = NetworkObserver::NETWORK_CELLULAR_LTE;
199 else
200 type = NetworkObserver::NETWORK_CELLULAR;
201 } else if (network_type == flimflam::kTypeEthernet) {
202 type = NetworkObserver:: NETWORK_ETHERNET;
203 } else if (network_type == flimflam::kTypeWifi) {
204 type = NetworkObserver:: NETWORK_WIFI;
205 } else {
206 NOTREACHED();
172 } 207 }
173 208
174 std::vector<string16> no_links; 209 std::vector<string16> no_links;
175 ash::Shell::GetInstance()->system_tray_notifier()->NotifySetNetworkMessage( 210 ash::Shell::GetInstance()->system_tray_notifier()->NotifySetNetworkMessage(
176 this, 211 this,
177 NetworkObserver::ERROR_CONNECT_FAILED, 212 NetworkObserver::ERROR_CONNECT_FAILED,
178 NetworkObserver::GetNetworkTypeForNetworkState(network), 213 type,
179 l10n_util::GetStringUTF16(IDS_NETWORK_CONNECTION_ERROR_TITLE), 214 l10n_util::GetStringUTF16(IDS_NETWORK_CONNECTION_ERROR_TITLE),
180 error_msg, 215 error_msg,
181 no_links); 216 no_links);
182 } 217 }
183 218
184 } // namespace ash 219 } // namespace ash
OLDNEW
« no previous file with comments | « ash/system/chromeos/network/network_state_notifier.h ('k') | ash/system/tray/system_tray_delegate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698