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

Side by Side Diff: chrome/browser/chromeos/network_message_observer.cc

Issue 10407080: Add NetworkNotificationView to tray_network. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 8 years, 7 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 "chrome/browser/chromeos/network_message_observer.h" 5 #include "chrome/browser/chromeos/network_message_observer.h"
6 6
7 #include "ash/ash_switches.h"
7 #include "ash/shell.h" 8 #include "ash/shell.h"
8 #include "ash/shell_delegate.h" 9 #include "ash/shell_delegate.h"
10 #include "ash/system/network/network_observer.h"
11 #include "ash/system/tray/system_tray.h"
9 #include "base/bind.h" 12 #include "base/bind.h"
10 #include "base/callback.h" 13 #include "base/callback.h"
14 #include "base/command_line.h"
11 #include "base/stl_util.h" 15 #include "base/stl_util.h"
12 #include "base/string_number_conversions.h" 16 #include "base/string_number_conversions.h"
13 #include "base/utf_string_conversions.h" 17 #include "base/utf_string_conversions.h"
14 #include "chrome/browser/chromeos/cros/cros_library.h" 18 #include "chrome/browser/chromeos/cros/cros_library.h"
15 #include "chrome/browser/chromeos/cros/network_library.h" 19 #include "chrome/browser/chromeos/cros/network_library.h"
16 #include "chrome/browser/chromeos/notifications/balloon_view_host_chromeos.h" 20 #include "chrome/browser/chromeos/notifications/balloon_view_host_chromeos.h"
21 #include "chrome/browser/chromeos/notifications/system_notification.h"
17 #include "chrome/browser/prefs/pref_service.h" 22 #include "chrome/browser/prefs/pref_service.h"
18 #include "chrome/browser/profiles/profile.h" 23 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/profiles/profile_manager.h" 24 #include "chrome/browser/profiles/profile_manager.h"
20 #include "chrome/browser/ui/browser.h" 25 #include "chrome/browser/ui/browser.h"
21 #include "chrome/browser/ui/browser_finder.h" 26 #include "chrome/browser/ui/browser_finder.h"
22 #include "chrome/common/pref_names.h" 27 #include "chrome/common/pref_names.h"
23 #include "chrome/common/time_format.h" 28 #include "chrome/common/time_format.h"
24 #include "grit/generated_resources.h" 29 #include "grit/generated_resources.h"
25 #include "grit/theme_resources.h" 30 #include "grit/theme_resources.h"
26 #include "ui/base/l10n/l10n_util.h" 31 #include "ui/base/l10n/l10n_util.h"
27 32
28 namespace { 33 namespace {
29 34
30 // Returns prefs::kShowPlanNotifications in the profile of the last active 35 // Returns prefs::kShowPlanNotifications in the profile of the last active
31 // browser. If there is no active browser, returns true. 36 // browser. If there is no active browser, returns true.
32 bool ShouldShowMobilePlanNotifications() { 37 bool ShouldShowMobilePlanNotifications() {
33 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); 38 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord();
34 PrefService* prefs = profile->GetPrefs(); 39 PrefService* prefs = profile->GetPrefs();
35 return prefs->GetBoolean(prefs::kShowPlanNotifications); 40 return prefs->GetBoolean(prefs::kShowPlanNotifications);
36 } 41 }
37 42
38 } // namespace 43 } // namespace
39 44
40 namespace chromeos { 45 namespace chromeos {
41 46
42 NetworkMessageObserver::NetworkMessageObserver(Profile* profile) 47 class NetworkMessageNotification : public ash::NetworkTrayDelegate {
43 : notification_connection_error_(profile, "network_connection.chromeos", 48 public:
49 NetworkMessageNotification(Profile* profile,
50 const std::string& id,
Greg Spencer (Chromium) 2012/05/23 18:37:39 Would it be better supply an ash::NetworkObserver:
stevenjb 2012/05/23 18:54:37 Good suggestion, changed to pass error_type_ and s
51 int icon_id,
52 const string16& title) :
53 id_(id),
54 title_(title),
55 error_type_(ash::NetworkObserver::ERROR_CONNECT_FAILED) {
56 if (!CommandLine::ForCurrentProcess()->HasSwitch(
57 ash::switches::kAshNotify)) {
58 system_notification_.reset(
59 new SystemNotification(profile, id, icon_id, title));
60 } else {
61 if (id == "network_connection.chromeos") {
62 error_type_ = ash::NetworkObserver::ERROR_CONNECT_FAILED;
63 } else if (id == "network_low_data.chromeos") {
64 error_type_ = ash::NetworkObserver::ERROR_DATA_LOW;
65 } else if (id == "network_no_data.chromeos") {
66 error_type_ = ash::NetworkObserver::ERROR_DATA_NONE;
67 }
68 }
69 }
70
71 // Overridden from ash::NetworkTrayDelegate:
72 virtual void NotificationLinkClicked() {
73 base::ListValue empty_value;
74 if (!callback_.is_null())
75 callback_.Run(&empty_value);
76 }
77
78 void Hide() {
79 if (system_notification_.get()) {
80 system_notification_->Hide();
81 } else {
82 ash::Shell::GetInstance()->tray()->network_observer()->
83 ClearNetworkError(error_type_);
84 }
85 }
86
87 void SetTitle(const string16& title) {
88 title_ = title;
89 if (system_notification_.get()) {
90 system_notification_->set_title(title);
91 }
92 }
93
94 void Show(const string16& message,
95 const string16& link_text,
96 const BalloonViewHost::MessageCallback& callback,
97 bool urgent, bool sticky) {
98 if (system_notification_.get()) {
99 system_notification_->Show(message, link_text, callback, urgent, sticky);
100 } else {
101 callback_ = callback;
102 ash::Shell::GetInstance()->tray()->network_observer()->
103 SetNetworkError(this, error_type_, title_, message, link_text);
104 }
105 }
106
107 void ShowAlways(const string16& message,
108 const string16& link_text,
109 const BalloonViewHost::MessageCallback& callback,
110 bool urgent, bool sticky) {
111 if (system_notification_.get()) {
112 // Hide if already shown to force show it in case user has closed it.
113 if (system_notification_->visible())
114 system_notification_->Hide();
115 }
116 Show(message, link_text, callback, urgent, sticky);
117 }
118
119 private:
120 std::string id_;
Greg Spencer (Chromium) 2012/05/23 18:37:39 This isn't used anywhere, and can be removed.
stevenjb 2012/05/23 18:54:37 Done.
121 string16 title_;
122 scoped_ptr<SystemNotification> system_notification_;
123 ash::NetworkObserver::ErrorType error_type_;
124 BalloonViewHost::MessageCallback callback_;
125 };
126
127 NetworkMessageObserver::NetworkMessageObserver(Profile* profile) {
128 notification_connection_error_.reset(
129 new NetworkMessageNotification(
130 profile, "network_connection.chromeos",
44 IDR_NOTIFICATION_NETWORK_FAILED, 131 IDR_NOTIFICATION_NETWORK_FAILED,
45 l10n_util::GetStringUTF16(IDS_NETWORK_CONNECTION_ERROR_TITLE)), 132 l10n_util::GetStringUTF16(IDS_NETWORK_CONNECTION_ERROR_TITLE)));
46 notification_low_data_(profile, "network_low_data.chromeos", 133 notification_low_data_.reset(
134 new NetworkMessageNotification(
135 profile, "network_low_data.chromeos",
47 IDR_NOTIFICATION_BARS_CRITICAL, 136 IDR_NOTIFICATION_BARS_CRITICAL,
48 l10n_util::GetStringUTF16(IDS_NETWORK_LOW_DATA_TITLE)), 137 l10n_util::GetStringUTF16(IDS_NETWORK_LOW_DATA_TITLE)));
49 notification_no_data_(profile, "network_no_data.chromeos", 138 notification_no_data_.reset(
139 new NetworkMessageNotification(
140 profile, "network_no_data.chromeos",
50 IDR_NOTIFICATION_BARS_EMPTY, 141 IDR_NOTIFICATION_BARS_EMPTY,
51 l10n_util::GetStringUTF16(IDS_NETWORK_OUT_OF_DATA_TITLE)) { 142 l10n_util::GetStringUTF16(IDS_NETWORK_OUT_OF_DATA_TITLE)));
52 NetworkLibrary* netlib = CrosLibrary::Get()->GetNetworkLibrary(); 143 NetworkLibrary* netlib = CrosLibrary::Get()->GetNetworkLibrary();
53 OnNetworkManagerChanged(netlib); 144 OnNetworkManagerChanged(netlib);
54 // Note that this gets added as a NetworkManagerObserver, 145 // Note that this gets added as a NetworkManagerObserver,
55 // CellularDataPlanObserver, and UserActionObserver in 146 // CellularDataPlanObserver, and UserActionObserver in
56 // startup_browser_creator.cc 147 // startup_browser_creator.cc
57 } 148 }
58 149
59 NetworkMessageObserver::~NetworkMessageObserver() { 150 NetworkMessageObserver::~NetworkMessageObserver() {
60 NetworkLibrary* netlib = CrosLibrary::Get()->GetNetworkLibrary(); 151 NetworkLibrary* netlib = CrosLibrary::Get()->GetNetworkLibrary();
61 netlib->RemoveNetworkManagerObserver(this); 152 netlib->RemoveNetworkManagerObserver(this);
62 netlib->RemoveCellularDataPlanObserver(this); 153 netlib->RemoveCellularDataPlanObserver(this);
63 netlib->RemoveUserActionObserver(this); 154 netlib->RemoveUserActionObserver(this);
64 notification_connection_error_.Hide(); 155 notification_connection_error_->Hide();
65 notification_low_data_.Hide(); 156 notification_low_data_->Hide();
66 notification_no_data_.Hide(); 157 notification_no_data_->Hide();
67 } 158 }
68 159
69 // static 160 // static
70 bool NetworkMessageObserver::IsApplicableBackupPlan( 161 bool NetworkMessageObserver::IsApplicableBackupPlan(
71 const CellularDataPlan* plan, const CellularDataPlan* other_plan) { 162 const CellularDataPlan* plan, const CellularDataPlan* other_plan) {
72 // By applicable plan, we mean that the other plan has data AND the timeframe 163 // By applicable plan, we mean that the other plan has data AND the timeframe
73 // will apply: (unlimited OR used bytes < max bytes) AND 164 // will apply: (unlimited OR used bytes < max bytes) AND
74 // ((start time - 1 sec) <= end time of currently active plan). 165 // ((start time - 1 sec) <= end time of currently active plan).
75 // In other words, there is data available and there is no gap of more than a 166 // In other words, there is data available and there is no gap of more than a
76 // second in time between the old plan and the new plan. 167 // second in time between the old plan and the new plan.
(...skipping 13 matching lines...) Expand all
90 ProfileManager::GetDefaultProfileOrOffTheRecord()); 181 ProfileManager::GetDefaultProfileOrOffTheRecord());
91 chromeos::NetworkLibrary* lib = 182 chromeos::NetworkLibrary* lib =
92 chromeos::CrosLibrary::Get()->GetNetworkLibrary(); 183 chromeos::CrosLibrary::Get()->GetNetworkLibrary();
93 const chromeos::CellularNetwork* cellular = lib->cellular_network(); 184 const chromeos::CellularNetwork* cellular = lib->cellular_network();
94 if (!cellular) 185 if (!cellular)
95 return; 186 return;
96 browser->ShowSingletonTab(GURL(cellular->payment_url())); 187 browser->ShowSingletonTab(GURL(cellular->payment_url()));
97 } 188 }
98 189
99 void NetworkMessageObserver::InitNewPlan(const CellularDataPlan* plan) { 190 void NetworkMessageObserver::InitNewPlan(const CellularDataPlan* plan) {
100 notification_low_data_.Hide(); 191 notification_low_data_->Hide();
101 notification_no_data_.Hide(); 192 notification_no_data_->Hide();
102 if (plan->plan_type == CELLULAR_DATA_PLAN_UNLIMITED) { 193 if (plan->plan_type == CELLULAR_DATA_PLAN_UNLIMITED) {
103 notification_no_data_.set_title( 194 notification_no_data_->SetTitle(
104 l10n_util::GetStringFUTF16(IDS_NETWORK_DATA_EXPIRED_TITLE, 195 l10n_util::GetStringFUTF16(IDS_NETWORK_DATA_EXPIRED_TITLE,
105 ASCIIToUTF16(plan->plan_name))); 196 ASCIIToUTF16(plan->plan_name)));
106 notification_low_data_.set_title( 197 notification_low_data_->SetTitle(
107 l10n_util::GetStringFUTF16(IDS_NETWORK_NEARING_EXPIRATION_TITLE, 198 l10n_util::GetStringFUTF16(IDS_NETWORK_NEARING_EXPIRATION_TITLE,
108 ASCIIToUTF16(plan->plan_name))); 199 ASCIIToUTF16(plan->plan_name)));
109 } else { 200 } else {
110 notification_no_data_.set_title( 201 notification_no_data_->SetTitle(
111 l10n_util::GetStringFUTF16(IDS_NETWORK_OUT_OF_DATA_TITLE, 202 l10n_util::GetStringFUTF16(IDS_NETWORK_OUT_OF_DATA_TITLE,
112 ASCIIToUTF16(plan->plan_name))); 203 ASCIIToUTF16(plan->plan_name)));
113 notification_low_data_.set_title( 204 notification_low_data_->SetTitle(
114 l10n_util::GetStringFUTF16(IDS_NETWORK_LOW_DATA_TITLE, 205 l10n_util::GetStringFUTF16(IDS_NETWORK_LOW_DATA_TITLE,
115 ASCIIToUTF16(plan->plan_name))); 206 ASCIIToUTF16(plan->plan_name)));
116 } 207 }
117 } 208 }
118 209
119 void NetworkMessageObserver::ShowNeedsPlanNotification( 210 void NetworkMessageObserver::ShowNeedsPlanNotification(
120 const CellularNetwork* cellular) { 211 const CellularNetwork* cellular) {
121 notification_no_data_.set_title( 212 notification_no_data_->SetTitle(
122 l10n_util::GetStringFUTF16(IDS_NETWORK_NO_DATA_PLAN_TITLE, 213 l10n_util::GetStringFUTF16(IDS_NETWORK_NO_DATA_PLAN_TITLE,
123 UTF8ToUTF16(cellular->name()))); 214 UTF8ToUTF16(cellular->name())));
124 notification_no_data_.Show( 215 notification_no_data_->Show(
125 l10n_util::GetStringFUTF16( 216 l10n_util::GetStringFUTF16(
126 IDS_NETWORK_NO_DATA_PLAN_MESSAGE, 217 IDS_NETWORK_NO_DATA_PLAN_MESSAGE,
127 UTF8ToUTF16(cellular->name())), 218 UTF8ToUTF16(cellular->name())),
128 l10n_util::GetStringUTF16(IDS_NETWORK_PURCHASE_MORE_MESSAGE), 219 l10n_util::GetStringUTF16(IDS_NETWORK_PURCHASE_MORE_MESSAGE),
129 base::Bind(&NetworkMessageObserver::OpenMobileSetupPage, AsWeakPtr()), 220 base::Bind(&NetworkMessageObserver::OpenMobileSetupPage, AsWeakPtr()),
130 false, false); 221 false, false);
131 } 222 }
132 223
133 void NetworkMessageObserver::ShowNoDataNotification( 224 void NetworkMessageObserver::ShowNoDataNotification(
134 CellularDataPlanType plan_type) { 225 CellularDataPlanType plan_type) {
135 notification_low_data_.Hide(); // Hide previous low data notification. 226 notification_low_data_->Hide(); // Hide previous low data notification.
136 string16 message = plan_type == CELLULAR_DATA_PLAN_UNLIMITED ? 227 string16 message = plan_type == CELLULAR_DATA_PLAN_UNLIMITED ?
137 TimeFormat::TimeRemaining(base::TimeDelta()) : 228 TimeFormat::TimeRemaining(base::TimeDelta()) :
138 l10n_util::GetStringFUTF16(IDS_NETWORK_DATA_REMAINING_MESSAGE, 229 l10n_util::GetStringFUTF16(IDS_NETWORK_DATA_REMAINING_MESSAGE,
139 ASCIIToUTF16("0")); 230 ASCIIToUTF16("0"));
140 notification_no_data_.Show(message, 231 notification_no_data_->Show(message,
141 l10n_util::GetStringUTF16(IDS_NETWORK_PURCHASE_MORE_MESSAGE), 232 l10n_util::GetStringUTF16(IDS_NETWORK_PURCHASE_MORE_MESSAGE),
142 base::Bind(&NetworkMessageObserver::OpenMobileSetupPage, AsWeakPtr()), 233 base::Bind(&NetworkMessageObserver::OpenMobileSetupPage, AsWeakPtr()),
143 false, false); 234 false, false);
144 } 235 }
145 236
146 void NetworkMessageObserver::ShowLowDataNotification( 237 void NetworkMessageObserver::ShowLowDataNotification(
147 const CellularDataPlan* plan) { 238 const CellularDataPlan* plan) {
148 string16 message; 239 string16 message;
149 if (plan->plan_type == CELLULAR_DATA_PLAN_UNLIMITED) { 240 if (plan->plan_type == CELLULAR_DATA_PLAN_UNLIMITED) {
150 message = plan->GetPlanExpiration(); 241 message = plan->GetPlanExpiration();
151 } else { 242 } else {
152 int64 remaining_mbytes = plan->remaining_data() / (1024 * 1024); 243 int64 remaining_mbytes = plan->remaining_data() / (1024 * 1024);
153 message = l10n_util::GetStringFUTF16(IDS_NETWORK_DATA_REMAINING_MESSAGE, 244 message = l10n_util::GetStringFUTF16(IDS_NETWORK_DATA_REMAINING_MESSAGE,
154 UTF8ToUTF16(base::Int64ToString(remaining_mbytes))); 245 UTF8ToUTF16(base::Int64ToString(remaining_mbytes)));
155 } 246 }
156 notification_low_data_.Show(message, 247 notification_low_data_->Show(message,
157 l10n_util::GetStringUTF16(IDS_NETWORK_MORE_INFO_MESSAGE), 248 l10n_util::GetStringUTF16(IDS_NETWORK_MORE_INFO_MESSAGE),
158 base::Bind(&NetworkMessageObserver::OpenMoreInfoPage, AsWeakPtr()), 249 base::Bind(&NetworkMessageObserver::OpenMoreInfoPage, AsWeakPtr()),
159 false, false); 250 false, false);
160 } 251 }
161 252
162 void NetworkMessageObserver::OnNetworkManagerChanged(NetworkLibrary* cros) { 253 void NetworkMessageObserver::OnNetworkManagerChanged(NetworkLibrary* cros) {
163 const Network* new_failed_network = NULL; 254 const Network* new_failed_network = NULL;
164 // Check to see if we have any newly failed networks. 255 // Check to see if we have any newly failed networks.
165 for (WifiNetworkVector::const_iterator it = cros->wifi_networks().begin(); 256 for (WifiNetworkVector::const_iterator it = cros->wifi_networks().begin();
166 it != cros->wifi_networks().end(); it++) { 257 it != cros->wifi_networks().end(); it++) {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 const VirtualNetwork* net = *it; 292 const VirtualNetwork* net = *it;
202 if (net->notify_failure()) { 293 if (net->notify_failure()) {
203 new_failed_network = net; 294 new_failed_network = net;
204 break; // There should only be one failed network. 295 break; // There should only be one failed network.
205 } 296 }
206 } 297 }
207 } 298 }
208 299
209 // Show connection error notification if necessary. 300 // Show connection error notification if necessary.
210 if (new_failed_network) { 301 if (new_failed_network) {
211 // Hide if already shown to force show it in case user has closed it. 302 notification_connection_error_->ShowAlways(
212 if (notification_connection_error_.visible()) 303 l10n_util::GetStringFUTF16(
213 notification_connection_error_.Hide(); 304 IDS_NETWORK_CONNECTION_ERROR_MESSAGE_WITH_DETAILS,
214 notification_connection_error_.Show(l10n_util::GetStringFUTF16( 305 UTF8ToUTF16(new_failed_network->name()),
215 IDS_NETWORK_CONNECTION_ERROR_MESSAGE_WITH_DETAILS, 306 UTF8ToUTF16(new_failed_network->GetErrorString())),
216 UTF8ToUTF16(new_failed_network->name()), 307 string16(), BalloonViewHost::MessageCallback(), false, false);
217 UTF8ToUTF16(new_failed_network->GetErrorString())), false, false);
218 } 308 }
219 } 309 }
220 310
221 void NetworkMessageObserver::OnCellularDataPlanChanged(NetworkLibrary* cros) { 311 void NetworkMessageObserver::OnCellularDataPlanChanged(NetworkLibrary* cros) {
222 if (!ShouldShowMobilePlanNotifications()) 312 if (!ShouldShowMobilePlanNotifications())
223 return; 313 return;
224 const CellularNetwork* cellular = cros->cellular_network(); 314 const CellularNetwork* cellular = cros->cellular_network();
225 if (!cellular || !cellular->SupportsDataPlan()) 315 if (!cellular || !cellular->SupportsDataPlan())
226 return; 316 return;
227 317
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 if (!new_plan && (cellular_data_left_ != CellularNetwork::DATA_VERY_LOW)) 361 if (!new_plan && (cellular_data_left_ != CellularNetwork::DATA_VERY_LOW))
272 ShowLowDataNotification(current_plan); 362 ShowLowDataNotification(current_plan);
273 } 363 }
274 364
275 SaveLastCellularInfo(cellular, current_plan); 365 SaveLastCellularInfo(cellular, current_plan);
276 } 366 }
277 367
278 void NetworkMessageObserver::OnConnectionInitiated(NetworkLibrary* cros, 368 void NetworkMessageObserver::OnConnectionInitiated(NetworkLibrary* cros,
279 const Network* network) { 369 const Network* network) {
280 // If user initiated any network connection, we hide the error notification. 370 // If user initiated any network connection, we hide the error notification.
281 notification_connection_error_.Hide(); 371 notification_connection_error_->Hide();
282 } 372 }
283 373
284 void NetworkMessageObserver::SaveLastCellularInfo( 374 void NetworkMessageObserver::SaveLastCellularInfo(
285 const CellularNetwork* cellular, const CellularDataPlan* plan) { 375 const CellularNetwork* cellular, const CellularDataPlan* plan) {
286 DCHECK(cellular); 376 DCHECK(cellular);
287 cellular_service_path_ = cellular->service_path(); 377 cellular_service_path_ = cellular->service_path();
288 cellular_data_left_ = cellular->data_left(); 378 cellular_data_left_ = cellular->data_left();
289 if (plan) { 379 if (plan) {
290 cellular_data_plan_unique_id_ = plan->GetUniqueIdentifier(); 380 cellular_data_plan_unique_id_ = plan->GetUniqueIdentifier();
291 cellular_data_plan_type_ = plan->plan_type; 381 cellular_data_plan_type_ = plan->plan_type;
292 } else { 382 } else {
293 cellular_data_plan_unique_id_ = std::string(); 383 cellular_data_plan_unique_id_ = std::string();
294 cellular_data_plan_type_ = CELLULAR_DATA_PLAN_UNKNOWN; 384 cellular_data_plan_type_ = CELLULAR_DATA_PLAN_UNKNOWN;
295 } 385 }
296 } 386 }
297 387
298 } // namespace chromeos 388 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/network_message_observer.h ('k') | ui/resources/default_100_percent/aura/status_data_low.png » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698