OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/local_discovery/privet_notifications.h" | 5 #include "chrome/browser/local_discovery/privet_notifications.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 | 140 |
141 if (!json_value || | 141 if (!json_value || |
142 !json_value->GetInteger(kPrivetInfoKeyUptime, &uptime) || | 142 !json_value->GetInteger(kPrivetInfoKeyUptime, &uptime) || |
143 uptime > kTenMinutesInSeconds) { | 143 uptime > kTenMinutesInSeconds) { |
144 return; | 144 return; |
145 } | 145 } |
146 | 146 |
147 DCHECK(!device->notification_may_be_active); | 147 DCHECK(!device->notification_may_be_active); |
148 device->notification_may_be_active = true; | 148 device->notification_may_be_active = true; |
149 devices_active_++; | 149 devices_active_++; |
150 delegate_->PrivetNotify(devices_active_ > 1, true); | 150 delegate_->PrivetNotify(devices_active_, true); |
151 } | 151 } |
152 | 152 |
153 void PrivetNotificationsListener::DeviceRemoved(const std::string& name) { | 153 void PrivetNotificationsListener::DeviceRemoved(const std::string& name) { |
154 DeviceContextMap::iterator device_iter = devices_seen_.find(name); | 154 DeviceContextMap::iterator device_iter = devices_seen_.find(name); |
155 if (device_iter == devices_seen_.end()) | 155 if (device_iter == devices_seen_.end()) |
156 return; | 156 return; |
157 DeviceContext* device = device_iter->second.get(); | 157 DeviceContext* device = device_iter->second.get(); |
158 | 158 |
159 device->info_operation.reset(); | 159 device->info_operation.reset(); |
160 device->privet_http_resolution.reset(); | 160 device->privet_http_resolution.reset(); |
(...skipping 15 matching lines...) Expand all Loading... |
176 | 176 |
177 devices_active_ = 0; | 177 devices_active_ = 0; |
178 delegate_->PrivetRemoveNotification(); | 178 delegate_->PrivetRemoveNotification(); |
179 } | 179 } |
180 | 180 |
181 void PrivetNotificationsListener::NotifyDeviceRemoved() { | 181 void PrivetNotificationsListener::NotifyDeviceRemoved() { |
182 devices_active_--; | 182 devices_active_--; |
183 if (devices_active_ == 0) { | 183 if (devices_active_ == 0) { |
184 delegate_->PrivetRemoveNotification(); | 184 delegate_->PrivetRemoveNotification(); |
185 } else { | 185 } else { |
186 delegate_->PrivetNotify(devices_active_ > 1, false); | 186 delegate_->PrivetNotify(devices_active_, false); |
187 } | 187 } |
188 } | 188 } |
189 | 189 |
190 PrivetNotificationsListener::DeviceContext::DeviceContext() { | 190 PrivetNotificationsListener::DeviceContext::DeviceContext() { |
191 } | 191 } |
192 | 192 |
193 PrivetNotificationsListener::DeviceContext::~DeviceContext() { | 193 PrivetNotificationsListener::DeviceContext::~DeviceContext() { |
194 } | 194 } |
195 | 195 |
196 PrivetNotificationService::PrivetNotificationService( | 196 PrivetNotificationService::PrivetNotificationService( |
(...skipping 30 matching lines...) Expand all Loading... |
227 return !command_line->HasSwitch( | 227 return !command_line->HasSwitch( |
228 switches::kDisableDeviceDiscoveryNotifications); | 228 switches::kDisableDeviceDiscoveryNotifications); |
229 } | 229 } |
230 | 230 |
231 // static | 231 // static |
232 bool PrivetNotificationService::IsForced() { | 232 bool PrivetNotificationService::IsForced() { |
233 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 233 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
234 return command_line->HasSwitch(switches::kEnableDeviceDiscoveryNotifications); | 234 return command_line->HasSwitch(switches::kEnableDeviceDiscoveryNotifications); |
235 } | 235 } |
236 | 236 |
237 void PrivetNotificationService::PrivetNotify(bool has_multiple, | 237 void PrivetNotificationService::PrivetNotify(int devices_active, |
238 bool added) { | 238 bool added) { |
239 base::string16 product_name = l10n_util::GetStringUTF16( | 239 base::string16 product_name = l10n_util::GetStringUTF16( |
240 IDS_LOCAL_DISOCVERY_SERVICE_NAME_PRINTER); | 240 IDS_LOCAL_DISCOVERY_SERVICE_NAME_PRINTER); |
241 | 241 |
242 int title_resource = has_multiple ? | 242 base::string16 title = l10n_util::GetPluralStringFUTF16( |
243 IDS_LOCAL_DISOCVERY_NOTIFICATION_TITLE_PRINTER_MULTIPLE : | 243 IDS_LOCAL_DISCOVERY_NOTIFICATION_TITLE_PRINTER, devices_active); |
244 IDS_LOCAL_DISOCVERY_NOTIFICATION_TITLE_PRINTER; | 244 base::string16 body = l10n_util::GetPluralStringFUTF16( |
245 | 245 IDS_LOCAL_DISCOVERY_NOTIFICATION_CONTENTS_PRINTER, devices_active); |
246 int body_resource = has_multiple ? | |
247 IDS_LOCAL_DISOCVERY_NOTIFICATION_CONTENTS_PRINTER_MULTIPLE : | |
248 IDS_LOCAL_DISOCVERY_NOTIFICATION_CONTENTS_PRINTER; | |
249 | |
250 base::string16 title = l10n_util::GetStringUTF16(title_resource); | |
251 base::string16 body = l10n_util::GetStringUTF16(body_resource); | |
252 | 246 |
253 Profile* profile_object = Profile::FromBrowserContext(profile_); | 247 Profile* profile_object = Profile::FromBrowserContext(profile_); |
254 message_center::RichNotificationData rich_notification_data; | 248 message_center::RichNotificationData rich_notification_data; |
255 | 249 |
256 rich_notification_data.buttons.push_back( | 250 rich_notification_data.buttons.push_back( |
257 message_center::ButtonInfo(l10n_util::GetStringUTF16( | 251 message_center::ButtonInfo(l10n_util::GetStringUTF16( |
258 IDS_LOCAL_DISOCVERY_NOTIFICATION_BUTTON_PRINTER))); | 252 IDS_LOCAL_DISCOVERY_NOTIFICATION_BUTTON_PRINTER))); |
259 | 253 |
260 rich_notification_data.buttons.push_back( | 254 rich_notification_data.buttons.push_back( |
261 message_center::ButtonInfo(l10n_util::GetStringUTF16( | 255 message_center::ButtonInfo(l10n_util::GetStringUTF16( |
262 IDS_LOCAL_DISCOVERY_NOTIFICATIONS_DISABLE_BUTTON_LABEL))); | 256 IDS_LOCAL_DISCOVERY_NOTIFICATIONS_DISABLE_BUTTON_LABEL))); |
263 | 257 |
264 Notification notification( | 258 Notification notification( |
265 message_center::NOTIFICATION_TYPE_SIMPLE, | 259 message_center::NOTIFICATION_TYPE_SIMPLE, |
266 GURL(kPrivetNotificationOriginUrl), | 260 GURL(kPrivetNotificationOriginUrl), |
267 title, | 261 title, |
268 body, | 262 body, |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 | 384 |
391 void PrivetNotificationDelegate::DisableNotifications() { | 385 void PrivetNotificationDelegate::DisableNotifications() { |
392 Profile* profile_obj = Profile::FromBrowserContext(profile_); | 386 Profile* profile_obj = Profile::FromBrowserContext(profile_); |
393 | 387 |
394 profile_obj->GetPrefs()->SetBoolean( | 388 profile_obj->GetPrefs()->SetBoolean( |
395 prefs::kLocalDiscoveryNotificationsEnabled, | 389 prefs::kLocalDiscoveryNotificationsEnabled, |
396 false); | 390 false); |
397 } | 391 } |
398 | 392 |
399 } // namespace local_discovery | 393 } // namespace local_discovery |
OLD | NEW |