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

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: Fix NOTREACHED 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
« no previous file with comments | « chrome/browser/chromeos/network_message_observer.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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:
44 IDR_NOTIFICATION_NETWORK_FAILED, 49 NetworkMessageNotification(Profile* profile,
45 l10n_util::GetStringUTF16(IDS_NETWORK_CONNECTION_ERROR_TITLE)), 50 ash::NetworkObserver::ErrorType error_type)
46 notification_low_data_(profile, "network_low_data.chromeos", 51 : error_type_(error_type) {
47 IDR_NOTIFICATION_BARS_CRITICAL, 52 std::string id;
48 l10n_util::GetStringUTF16(IDS_NETWORK_LOW_DATA_TITLE)), 53 int icon_id = 0;
49 notification_no_data_(profile, "network_no_data.chromeos", 54 switch (error_type) {
50 IDR_NOTIFICATION_BARS_EMPTY, 55 case ash::NetworkObserver::ERROR_CONNECT_FAILED:
51 l10n_util::GetStringUTF16(IDS_NETWORK_OUT_OF_DATA_TITLE)) { 56 id = "network_connection.chromeos";
57 icon_id = IDR_NOTIFICATION_NETWORK_FAILED;
58 title_ = l10n_util::GetStringUTF16(IDS_NETWORK_CONNECTION_ERROR_TITLE);
59 break;
60 case ash::NetworkObserver::ERROR_DATA_LOW:
61 id = "network_low_data.chromeos";
62 icon_id = IDR_NOTIFICATION_BARS_CRITICAL;
63 title_ = l10n_util::GetStringUTF16(IDS_NETWORK_LOW_DATA_TITLE);
64 break;
65 case ash::NetworkObserver::ERROR_DATA_NONE:
66 id = "network_no_data.chromeos";
67 icon_id = IDR_NOTIFICATION_BARS_EMPTY;
68 title_ = l10n_util::GetStringUTF16(IDS_NETWORK_OUT_OF_DATA_TITLE);
69 break;
70 }
71 LOG_IF(ERROR, id.empty()) << "Unexpected error type: " << error_type;
72 if (!CommandLine::ForCurrentProcess()->HasSwitch(
73 ash::switches::kAshNotify) && !id.empty()) {
74 system_notification_.reset(
75 new SystemNotification(profile, id, icon_id, title_));
76 }
77 }
78
79 // Overridden from ash::NetworkTrayDelegate:
80 virtual void NotificationLinkClicked() {
81 base::ListValue empty_value;
82 if (!callback_.is_null())
83 callback_.Run(&empty_value);
84 }
85
86 void Hide() {
87 if (system_notification_.get()) {
88 system_notification_->Hide();
89 } else {
90 ash::Shell::GetInstance()->tray()->network_observer()->
91 ClearNetworkError(error_type_);
92 }
93 }
94
95 void SetTitle(const string16& title) {
96 title_ = title;
97 if (system_notification_.get()) {
98 system_notification_->set_title(title);
99 }
100 }
101
102 void Show(const string16& message,
103 const string16& link_text,
104 const BalloonViewHost::MessageCallback& callback,
105 bool urgent, bool sticky) {
106 if (system_notification_.get()) {
107 system_notification_->Show(message, link_text, callback, urgent, sticky);
108 } else {
109 callback_ = callback;
110 ash::Shell::GetInstance()->tray()->network_observer()->
111 SetNetworkError(this, error_type_, title_, message, link_text);
112 }
113 }
114
115 void ShowAlways(const string16& message,
116 const string16& link_text,
117 const BalloonViewHost::MessageCallback& callback,
118 bool urgent, bool sticky) {
119 if (system_notification_.get()) {
120 // Hide if already shown to force show it in case user has closed it.
121 if (system_notification_->visible())
122 system_notification_->Hide();
123 }
124 Show(message, link_text, callback, urgent, sticky);
125 }
126
127 private:
128 string16 title_;
129 scoped_ptr<SystemNotification> system_notification_;
130 ash::NetworkObserver::ErrorType error_type_;
131 BalloonViewHost::MessageCallback callback_;
132 };
133
134 NetworkMessageObserver::NetworkMessageObserver(Profile* profile) {
135 notification_connection_error_.reset(
136 new NetworkMessageNotification(
137 profile, ash::NetworkObserver::ERROR_CONNECT_FAILED));
138 notification_low_data_.reset(
139 new NetworkMessageNotification(
140 profile,
141 ash::NetworkObserver::ERROR_DATA_LOW));
142 notification_no_data_.reset(
143 new NetworkMessageNotification(
144 profile,
145 ash::NetworkObserver::ERROR_DATA_NONE));
52 NetworkLibrary* netlib = CrosLibrary::Get()->GetNetworkLibrary(); 146 NetworkLibrary* netlib = CrosLibrary::Get()->GetNetworkLibrary();
53 OnNetworkManagerChanged(netlib); 147 OnNetworkManagerChanged(netlib);
54 // Note that this gets added as a NetworkManagerObserver, 148 // Note that this gets added as a NetworkManagerObserver,
55 // CellularDataPlanObserver, and UserActionObserver in 149 // CellularDataPlanObserver, and UserActionObserver in
56 // startup_browser_creator.cc 150 // startup_browser_creator.cc
57 } 151 }
58 152
59 NetworkMessageObserver::~NetworkMessageObserver() { 153 NetworkMessageObserver::~NetworkMessageObserver() {
60 NetworkLibrary* netlib = CrosLibrary::Get()->GetNetworkLibrary(); 154 NetworkLibrary* netlib = CrosLibrary::Get()->GetNetworkLibrary();
61 netlib->RemoveNetworkManagerObserver(this); 155 netlib->RemoveNetworkManagerObserver(this);
62 netlib->RemoveCellularDataPlanObserver(this); 156 netlib->RemoveCellularDataPlanObserver(this);
63 netlib->RemoveUserActionObserver(this); 157 netlib->RemoveUserActionObserver(this);
64 notification_connection_error_.Hide(); 158 notification_connection_error_->Hide();
65 notification_low_data_.Hide(); 159 notification_low_data_->Hide();
66 notification_no_data_.Hide(); 160 notification_no_data_->Hide();
67 } 161 }
68 162
69 // static 163 // static
70 bool NetworkMessageObserver::IsApplicableBackupPlan( 164 bool NetworkMessageObserver::IsApplicableBackupPlan(
71 const CellularDataPlan* plan, const CellularDataPlan* other_plan) { 165 const CellularDataPlan* plan, const CellularDataPlan* other_plan) {
72 // By applicable plan, we mean that the other plan has data AND the timeframe 166 // By applicable plan, we mean that the other plan has data AND the timeframe
73 // will apply: (unlimited OR used bytes < max bytes) AND 167 // will apply: (unlimited OR used bytes < max bytes) AND
74 // ((start time - 1 sec) <= end time of currently active plan). 168 // ((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 169 // 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. 170 // second in time between the old plan and the new plan.
(...skipping 13 matching lines...) Expand all
90 ProfileManager::GetDefaultProfileOrOffTheRecord()); 184 ProfileManager::GetDefaultProfileOrOffTheRecord());
91 chromeos::NetworkLibrary* lib = 185 chromeos::NetworkLibrary* lib =
92 chromeos::CrosLibrary::Get()->GetNetworkLibrary(); 186 chromeos::CrosLibrary::Get()->GetNetworkLibrary();
93 const chromeos::CellularNetwork* cellular = lib->cellular_network(); 187 const chromeos::CellularNetwork* cellular = lib->cellular_network();
94 if (!cellular) 188 if (!cellular)
95 return; 189 return;
96 browser->ShowSingletonTab(GURL(cellular->payment_url())); 190 browser->ShowSingletonTab(GURL(cellular->payment_url()));
97 } 191 }
98 192
99 void NetworkMessageObserver::InitNewPlan(const CellularDataPlan* plan) { 193 void NetworkMessageObserver::InitNewPlan(const CellularDataPlan* plan) {
100 notification_low_data_.Hide(); 194 notification_low_data_->Hide();
101 notification_no_data_.Hide(); 195 notification_no_data_->Hide();
102 if (plan->plan_type == CELLULAR_DATA_PLAN_UNLIMITED) { 196 if (plan->plan_type == CELLULAR_DATA_PLAN_UNLIMITED) {
103 notification_no_data_.set_title( 197 notification_no_data_->SetTitle(
104 l10n_util::GetStringFUTF16(IDS_NETWORK_DATA_EXPIRED_TITLE, 198 l10n_util::GetStringFUTF16(IDS_NETWORK_DATA_EXPIRED_TITLE,
105 ASCIIToUTF16(plan->plan_name))); 199 ASCIIToUTF16(plan->plan_name)));
106 notification_low_data_.set_title( 200 notification_low_data_->SetTitle(
107 l10n_util::GetStringFUTF16(IDS_NETWORK_NEARING_EXPIRATION_TITLE, 201 l10n_util::GetStringFUTF16(IDS_NETWORK_NEARING_EXPIRATION_TITLE,
108 ASCIIToUTF16(plan->plan_name))); 202 ASCIIToUTF16(plan->plan_name)));
109 } else { 203 } else {
110 notification_no_data_.set_title( 204 notification_no_data_->SetTitle(
111 l10n_util::GetStringFUTF16(IDS_NETWORK_OUT_OF_DATA_TITLE, 205 l10n_util::GetStringFUTF16(IDS_NETWORK_OUT_OF_DATA_TITLE,
112 ASCIIToUTF16(plan->plan_name))); 206 ASCIIToUTF16(plan->plan_name)));
113 notification_low_data_.set_title( 207 notification_low_data_->SetTitle(
114 l10n_util::GetStringFUTF16(IDS_NETWORK_LOW_DATA_TITLE, 208 l10n_util::GetStringFUTF16(IDS_NETWORK_LOW_DATA_TITLE,
115 ASCIIToUTF16(plan->plan_name))); 209 ASCIIToUTF16(plan->plan_name)));
116 } 210 }
117 } 211 }
118 212
119 void NetworkMessageObserver::ShowNeedsPlanNotification( 213 void NetworkMessageObserver::ShowNeedsPlanNotification(
120 const CellularNetwork* cellular) { 214 const CellularNetwork* cellular) {
121 notification_no_data_.set_title( 215 notification_no_data_->SetTitle(
122 l10n_util::GetStringFUTF16(IDS_NETWORK_NO_DATA_PLAN_TITLE, 216 l10n_util::GetStringFUTF16(IDS_NETWORK_NO_DATA_PLAN_TITLE,
123 UTF8ToUTF16(cellular->name()))); 217 UTF8ToUTF16(cellular->name())));
124 notification_no_data_.Show( 218 notification_no_data_->Show(
125 l10n_util::GetStringFUTF16( 219 l10n_util::GetStringFUTF16(
126 IDS_NETWORK_NO_DATA_PLAN_MESSAGE, 220 IDS_NETWORK_NO_DATA_PLAN_MESSAGE,
127 UTF8ToUTF16(cellular->name())), 221 UTF8ToUTF16(cellular->name())),
128 l10n_util::GetStringUTF16(IDS_NETWORK_PURCHASE_MORE_MESSAGE), 222 l10n_util::GetStringUTF16(IDS_NETWORK_PURCHASE_MORE_MESSAGE),
129 base::Bind(&NetworkMessageObserver::OpenMobileSetupPage, AsWeakPtr()), 223 base::Bind(&NetworkMessageObserver::OpenMobileSetupPage, AsWeakPtr()),
130 false, false); 224 false, false);
131 } 225 }
132 226
133 void NetworkMessageObserver::ShowNoDataNotification( 227 void NetworkMessageObserver::ShowNoDataNotification(
134 CellularDataPlanType plan_type) { 228 CellularDataPlanType plan_type) {
135 notification_low_data_.Hide(); // Hide previous low data notification. 229 notification_low_data_->Hide(); // Hide previous low data notification.
136 string16 message = plan_type == CELLULAR_DATA_PLAN_UNLIMITED ? 230 string16 message = plan_type == CELLULAR_DATA_PLAN_UNLIMITED ?
137 TimeFormat::TimeRemaining(base::TimeDelta()) : 231 TimeFormat::TimeRemaining(base::TimeDelta()) :
138 l10n_util::GetStringFUTF16(IDS_NETWORK_DATA_REMAINING_MESSAGE, 232 l10n_util::GetStringFUTF16(IDS_NETWORK_DATA_REMAINING_MESSAGE,
139 ASCIIToUTF16("0")); 233 ASCIIToUTF16("0"));
140 notification_no_data_.Show(message, 234 notification_no_data_->Show(message,
141 l10n_util::GetStringUTF16(IDS_NETWORK_PURCHASE_MORE_MESSAGE), 235 l10n_util::GetStringUTF16(IDS_NETWORK_PURCHASE_MORE_MESSAGE),
142 base::Bind(&NetworkMessageObserver::OpenMobileSetupPage, AsWeakPtr()), 236 base::Bind(&NetworkMessageObserver::OpenMobileSetupPage, AsWeakPtr()),
143 false, false); 237 false, false);
144 } 238 }
145 239
146 void NetworkMessageObserver::ShowLowDataNotification( 240 void NetworkMessageObserver::ShowLowDataNotification(
147 const CellularDataPlan* plan) { 241 const CellularDataPlan* plan) {
148 string16 message; 242 string16 message;
149 if (plan->plan_type == CELLULAR_DATA_PLAN_UNLIMITED) { 243 if (plan->plan_type == CELLULAR_DATA_PLAN_UNLIMITED) {
150 message = plan->GetPlanExpiration(); 244 message = plan->GetPlanExpiration();
151 } else { 245 } else {
152 int64 remaining_mbytes = plan->remaining_data() / (1024 * 1024); 246 int64 remaining_mbytes = plan->remaining_data() / (1024 * 1024);
153 message = l10n_util::GetStringFUTF16(IDS_NETWORK_DATA_REMAINING_MESSAGE, 247 message = l10n_util::GetStringFUTF16(IDS_NETWORK_DATA_REMAINING_MESSAGE,
154 UTF8ToUTF16(base::Int64ToString(remaining_mbytes))); 248 UTF8ToUTF16(base::Int64ToString(remaining_mbytes)));
155 } 249 }
156 notification_low_data_.Show(message, 250 notification_low_data_->Show(message,
157 l10n_util::GetStringUTF16(IDS_NETWORK_MORE_INFO_MESSAGE), 251 l10n_util::GetStringUTF16(IDS_NETWORK_MORE_INFO_MESSAGE),
158 base::Bind(&NetworkMessageObserver::OpenMoreInfoPage, AsWeakPtr()), 252 base::Bind(&NetworkMessageObserver::OpenMoreInfoPage, AsWeakPtr()),
159 false, false); 253 false, false);
160 } 254 }
161 255
162 void NetworkMessageObserver::OnNetworkManagerChanged(NetworkLibrary* cros) { 256 void NetworkMessageObserver::OnNetworkManagerChanged(NetworkLibrary* cros) {
163 const Network* new_failed_network = NULL; 257 const Network* new_failed_network = NULL;
164 // Check to see if we have any newly failed networks. 258 // Check to see if we have any newly failed networks.
165 for (WifiNetworkVector::const_iterator it = cros->wifi_networks().begin(); 259 for (WifiNetworkVector::const_iterator it = cros->wifi_networks().begin();
166 it != cros->wifi_networks().end(); it++) { 260 it != cros->wifi_networks().end(); it++) {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 const VirtualNetwork* net = *it; 295 const VirtualNetwork* net = *it;
202 if (net->notify_failure()) { 296 if (net->notify_failure()) {
203 new_failed_network = net; 297 new_failed_network = net;
204 break; // There should only be one failed network. 298 break; // There should only be one failed network.
205 } 299 }
206 } 300 }
207 } 301 }
208 302
209 // Show connection error notification if necessary. 303 // Show connection error notification if necessary.
210 if (new_failed_network) { 304 if (new_failed_network) {
211 // Hide if already shown to force show it in case user has closed it. 305 notification_connection_error_->ShowAlways(
212 if (notification_connection_error_.visible()) 306 l10n_util::GetStringFUTF16(
213 notification_connection_error_.Hide(); 307 IDS_NETWORK_CONNECTION_ERROR_MESSAGE_WITH_DETAILS,
214 notification_connection_error_.Show(l10n_util::GetStringFUTF16( 308 UTF8ToUTF16(new_failed_network->name()),
215 IDS_NETWORK_CONNECTION_ERROR_MESSAGE_WITH_DETAILS, 309 UTF8ToUTF16(new_failed_network->GetErrorString())),
216 UTF8ToUTF16(new_failed_network->name()), 310 string16(), BalloonViewHost::MessageCallback(), false, false);
217 UTF8ToUTF16(new_failed_network->GetErrorString())), false, false);
218 } 311 }
219 } 312 }
220 313
221 void NetworkMessageObserver::OnCellularDataPlanChanged(NetworkLibrary* cros) { 314 void NetworkMessageObserver::OnCellularDataPlanChanged(NetworkLibrary* cros) {
222 if (!ShouldShowMobilePlanNotifications()) 315 if (!ShouldShowMobilePlanNotifications())
223 return; 316 return;
224 const CellularNetwork* cellular = cros->cellular_network(); 317 const CellularNetwork* cellular = cros->cellular_network();
225 if (!cellular || !cellular->SupportsDataPlan()) 318 if (!cellular || !cellular->SupportsDataPlan())
226 return; 319 return;
227 320
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 if (!new_plan && (cellular_data_left_ != CellularNetwork::DATA_VERY_LOW)) 364 if (!new_plan && (cellular_data_left_ != CellularNetwork::DATA_VERY_LOW))
272 ShowLowDataNotification(current_plan); 365 ShowLowDataNotification(current_plan);
273 } 366 }
274 367
275 SaveLastCellularInfo(cellular, current_plan); 368 SaveLastCellularInfo(cellular, current_plan);
276 } 369 }
277 370
278 void NetworkMessageObserver::OnConnectionInitiated(NetworkLibrary* cros, 371 void NetworkMessageObserver::OnConnectionInitiated(NetworkLibrary* cros,
279 const Network* network) { 372 const Network* network) {
280 // If user initiated any network connection, we hide the error notification. 373 // If user initiated any network connection, we hide the error notification.
281 notification_connection_error_.Hide(); 374 notification_connection_error_->Hide();
282 } 375 }
283 376
284 void NetworkMessageObserver::SaveLastCellularInfo( 377 void NetworkMessageObserver::SaveLastCellularInfo(
285 const CellularNetwork* cellular, const CellularDataPlan* plan) { 378 const CellularNetwork* cellular, const CellularDataPlan* plan) {
286 DCHECK(cellular); 379 DCHECK(cellular);
287 cellular_service_path_ = cellular->service_path(); 380 cellular_service_path_ = cellular->service_path();
288 cellular_data_left_ = cellular->data_left(); 381 cellular_data_left_ = cellular->data_left();
289 if (plan) { 382 if (plan) {
290 cellular_data_plan_unique_id_ = plan->GetUniqueIdentifier(); 383 cellular_data_plan_unique_id_ = plan->GetUniqueIdentifier();
291 cellular_data_plan_type_ = plan->plan_type; 384 cellular_data_plan_type_ = plan->plan_type;
292 } else { 385 } else {
293 cellular_data_plan_unique_id_ = std::string(); 386 cellular_data_plan_unique_id_ = std::string();
294 cellular_data_plan_type_ = CELLULAR_DATA_PLAN_UNKNOWN; 387 cellular_data_plan_type_ = CELLULAR_DATA_PLAN_UNKNOWN;
295 } 388 }
296 } 389 }
297 390
298 } // namespace chromeos 391 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/network_message_observer.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698