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

Side by Side Diff: chrome/browser/printing/cloud_print/privet_notifications.cc

Issue 2027883002: Fix PrivetNotificationService::PrivetNotify() silliness. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: nits Created 4 years, 6 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
« no previous file with comments | « no previous file | chrome/browser/printing/cloud_print/privet_notifications_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/printing/cloud_print/privet_notifications.h" 5 #include "chrome/browser/printing/cloud_print/privet_notifications.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 privet_http_factory_.swap(privet_http_factory); 84 privet_http_factory_.swap(privet_http_factory);
85 } 85 }
86 86
87 PrivetNotificationsListener::~PrivetNotificationsListener() { 87 PrivetNotificationsListener::~PrivetNotificationsListener() {
88 } 88 }
89 89
90 void PrivetNotificationsListener::DeviceChanged( 90 void PrivetNotificationsListener::DeviceChanged(
91 const std::string& name, 91 const std::string& name,
92 const DeviceDescription& description) { 92 const DeviceDescription& description) {
93 ReportPrivetUmaEvent(PRIVET_DEVICE_CHANGED); 93 ReportPrivetUmaEvent(PRIVET_DEVICE_CHANGED);
94 DeviceContextMap::iterator found = devices_seen_.find(name); 94 DeviceContextMap::iterator it = devices_seen_.find(name);
95 if (found != devices_seen_.end()) { 95 if (it != devices_seen_.end()) {
96 if (!description.id.empty() && // Device is registered 96 if (!description.id.empty() && // Device is registered
97 found->second->notification_may_be_active) { 97 it->second->notification_may_be_active) {
98 found->second->notification_may_be_active = false; 98 it->second->notification_may_be_active = false;
99 devices_active_--;
99 NotifyDeviceRemoved(); 100 NotifyDeviceRemoved();
100 } 101 }
101 return; // Already saw this device. 102 return; // Already saw this device.
102 } 103 }
103 104
104 std::unique_ptr<DeviceContext>& device_context = devices_seen_[name]; 105 std::unique_ptr<DeviceContext>& device_context = devices_seen_[name];
105 device_context.reset(new DeviceContext); 106 device_context.reset(new DeviceContext);
106 device_context->notification_may_be_active = false; 107 device_context->notification_may_be_active = false;
107 device_context->registered = !description.id.empty(); 108 device_context->registered = !description.id.empty();
108 109
109 if (!device_context->registered) { 110 if (device_context->registered)
110 device_context->privet_http_resolution = 111 return;
111 privet_http_factory_->CreatePrivetHTTP(name); 112
112 device_context->privet_http_resolution->Start( 113 device_context->privet_http_resolution =
113 description.address, 114 privet_http_factory_->CreatePrivetHTTP(name);
114 base::Bind(&PrivetNotificationsListener::CreateInfoOperation, 115 device_context->privet_http_resolution->Start(
115 base::Unretained(this))); 116 description.address,
116 } 117 base::Bind(&PrivetNotificationsListener::CreateInfoOperation,
118 base::Unretained(this)));
117 } 119 }
118 120
119 void PrivetNotificationsListener::CreateInfoOperation( 121 void PrivetNotificationsListener::CreateInfoOperation(
120 std::unique_ptr<PrivetHTTPClient> http_client) { 122 std::unique_ptr<PrivetHTTPClient> http_client) {
121 if (!http_client) { 123 // Do nothing if resolution fails.
122 // Do nothing if resolution fails. 124 if (!http_client)
123 return; 125 return;
124 }
125 126
126 std::string name = http_client->GetName(); 127 std::string name = http_client->GetName();
127 DeviceContextMap::iterator device_iter = devices_seen_.find(name); 128 DeviceContextMap::iterator it = devices_seen_.find(name);
128 if (device_iter == devices_seen_.end()) 129 if (it == devices_seen_.end())
129 return; 130 return;
130 DeviceContext* device = device_iter->second.get(); 131
132 DeviceContext* device = it->second.get();
131 device->privet_http.swap(http_client); 133 device->privet_http.swap(http_client);
132 device->info_operation = device->privet_http->CreateInfoOperation( 134 device->info_operation = device->privet_http->CreateInfoOperation(
133 base::Bind(&PrivetNotificationsListener::OnPrivetInfoDone, 135 base::Bind(&PrivetNotificationsListener::OnPrivetInfoDone,
134 base::Unretained(this), 136 base::Unretained(this),
135 device)); 137 device));
136 device->info_operation->Start(); 138 device->info_operation->Start();
137 } 139 }
138 140
139 void PrivetNotificationsListener::OnPrivetInfoDone( 141 void PrivetNotificationsListener::OnPrivetInfoDone(
140 DeviceContext* device, 142 DeviceContext* device,
141 const base::DictionaryValue* json_value) { 143 const base::DictionaryValue* json_value) {
142 int uptime; 144 int uptime;
143 145
144 if (!json_value || 146 if (!json_value ||
145 !json_value->GetInteger(kPrivetInfoKeyUptime, &uptime) || 147 !json_value->GetInteger(kPrivetInfoKeyUptime, &uptime) ||
146 uptime > kTenMinutesInSeconds) { 148 uptime > kTenMinutesInSeconds) {
147 return; 149 return;
148 } 150 }
149 151
150 DCHECK(!device->notification_may_be_active); 152 DCHECK(!device->notification_may_be_active);
151 device->notification_may_be_active = true; 153 device->notification_may_be_active = true;
152 devices_active_++; 154 devices_active_++;
153 delegate_->PrivetNotify(devices_active_, true); 155 delegate_->PrivetNotify(devices_active_, true);
154 } 156 }
155 157
156 void PrivetNotificationsListener::DeviceRemoved(const std::string& name) { 158 void PrivetNotificationsListener::DeviceRemoved(const std::string& name) {
157 DeviceContextMap::iterator device_iter = devices_seen_.find(name); 159 DeviceContextMap::iterator it = devices_seen_.find(name);
158 if (device_iter == devices_seen_.end()) 160 if (it == devices_seen_.end())
159 return; 161 return;
160 DeviceContext* device = device_iter->second.get();
161 162
163 DeviceContext* device = it->second.get();
162 device->info_operation.reset(); 164 device->info_operation.reset();
163 device->privet_http_resolution.reset(); 165 device->privet_http_resolution.reset();
166 if (!device->notification_may_be_active)
167 return;
168
164 device->notification_may_be_active = false; 169 device->notification_may_be_active = false;
170 devices_active_--;
165 NotifyDeviceRemoved(); 171 NotifyDeviceRemoved();
166 } 172 }
167 173
168 void PrivetNotificationsListener::DeviceCacheFlushed() { 174 void PrivetNotificationsListener::DeviceCacheFlushed() {
169 for (DeviceContextMap::iterator i = devices_seen_.begin(); 175 for (const auto& it : devices_seen_) {
170 i != devices_seen_.end(); ++i) { 176 DeviceContext* device = it.second.get();
171 DeviceContext* device = i->second.get();
172
173 device->info_operation.reset(); 177 device->info_operation.reset();
174 device->privet_http_resolution.reset(); 178 device->privet_http_resolution.reset();
175 if (device->notification_may_be_active) { 179 device->notification_may_be_active = false;
176 device->notification_may_be_active = false;
177 }
178 } 180 }
179 181
180 devices_active_ = 0; 182 devices_active_ = 0;
181 delegate_->PrivetRemoveNotification(); 183 NotifyDeviceRemoved();
182 } 184 }
183 185
184 void PrivetNotificationsListener::NotifyDeviceRemoved() { 186 void PrivetNotificationsListener::NotifyDeviceRemoved() {
185 devices_active_--;
186 if (devices_active_ == 0) { 187 if (devices_active_ == 0) {
187 delegate_->PrivetRemoveNotification(); 188 delegate_->PrivetRemoveNotification();
188 } else { 189 } else {
189 delegate_->PrivetNotify(devices_active_, false); 190 delegate_->PrivetNotify(devices_active_, false);
190 } 191 }
191 } 192 }
192 193
193 PrivetNotificationsListener::DeviceContext::DeviceContext() { 194 PrivetNotificationsListener::DeviceContext::DeviceContext() {
194 } 195 }
195 196
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 } 231 }
231 232
232 // static 233 // static
233 bool PrivetNotificationService::IsForced() { 234 bool PrivetNotificationService::IsForced() {
234 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); 235 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
235 return command_line->HasSwitch(switches::kEnableDeviceDiscoveryNotifications); 236 return command_line->HasSwitch(switches::kEnableDeviceDiscoveryNotifications);
236 } 237 }
237 238
238 void PrivetNotificationService::PrivetNotify(int devices_active, 239 void PrivetNotificationService::PrivetNotify(int devices_active,
239 bool added) { 240 bool added) {
240 base::string16 product_name = l10n_util::GetStringUTF16( 241 DCHECK_GT(devices_active, 0);
241 IDS_LOCAL_DISCOVERY_SERVICE_NAME_PRINTER); 242
243 message_center::RichNotificationData rich_notification_data;
244 rich_notification_data.buttons.push_back(
245 message_center::ButtonInfo(l10n_util::GetStringUTF16(
246 IDS_LOCAL_DISCOVERY_NOTIFICATION_BUTTON_PRINTER)));
247 rich_notification_data.buttons.push_back(
248 message_center::ButtonInfo(l10n_util::GetStringUTF16(
249 IDS_LOCAL_DISCOVERY_NOTIFICATIONS_DISABLE_BUTTON_LABEL)));
242 250
243 base::string16 title = l10n_util::GetPluralStringFUTF16( 251 base::string16 title = l10n_util::GetPluralStringFUTF16(
244 IDS_LOCAL_DISCOVERY_NOTIFICATION_TITLE_PRINTER, devices_active); 252 IDS_LOCAL_DISCOVERY_NOTIFICATION_TITLE_PRINTER, devices_active);
245 base::string16 body = l10n_util::GetPluralStringFUTF16( 253 base::string16 body = l10n_util::GetPluralStringFUTF16(
246 IDS_LOCAL_DISCOVERY_NOTIFICATION_CONTENTS_PRINTER, devices_active); 254 IDS_LOCAL_DISCOVERY_NOTIFICATION_CONTENTS_PRINTER, devices_active);
247 255 base::string16 product_name =
248 Profile* profile_object = Profile::FromBrowserContext(profile_); 256 l10n_util::GetStringUTF16(IDS_LOCAL_DISCOVERY_SERVICE_NAME_PRINTER);
249 message_center::RichNotificationData rich_notification_data;
250
251 rich_notification_data.buttons.push_back(
252 message_center::ButtonInfo(l10n_util::GetStringUTF16(
253 IDS_LOCAL_DISCOVERY_NOTIFICATION_BUTTON_PRINTER)));
254
255 rich_notification_data.buttons.push_back(
256 message_center::ButtonInfo(l10n_util::GetStringUTF16(
257 IDS_LOCAL_DISCOVERY_NOTIFICATIONS_DISABLE_BUTTON_LABEL)));
258 257
259 Notification notification( 258 Notification notification(
260 message_center::NOTIFICATION_TYPE_SIMPLE, title, body, 259 message_center::NOTIFICATION_TYPE_SIMPLE, title, body,
261 ui::ResourceBundle::GetSharedInstance().GetImageNamed( 260 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
262 IDR_LOCAL_DISCOVERY_CLOUDPRINT_ICON), 261 IDR_LOCAL_DISCOVERY_CLOUDPRINT_ICON),
263 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, 262 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT,
264 kPrivetNotificationID), 263 kPrivetNotificationID),
265 product_name, GURL(kPrivetNotificationOriginUrl), kPrivetNotificationID, 264 product_name, GURL(kPrivetNotificationOriginUrl), kPrivetNotificationID,
266 rich_notification_data, new PrivetNotificationDelegate(profile_)); 265 rich_notification_data, new PrivetNotificationDelegate(profile_));
267 266
268 bool updated = g_browser_process->notification_ui_manager()->Update( 267 auto* notification_ui_manager = g_browser_process->notification_ui_manager();
269 notification, profile_object); 268 Profile* profile = Profile::FromBrowserContext(profile_);
269 bool updated = notification_ui_manager->Update(notification, profile);
270 if (!updated && added && 270 if (!updated && added &&
271 !local_discovery::LocalDiscoveryUIHandler::GetHasVisible()) { 271 !local_discovery::LocalDiscoveryUIHandler::GetHasVisible()) {
272 ReportPrivetUmaEvent(PRIVET_NOTIFICATION_SHOWN); 272 ReportPrivetUmaEvent(PRIVET_NOTIFICATION_SHOWN);
273 g_browser_process->notification_ui_manager()->Add(notification, 273 notification_ui_manager->Add(notification, profile);
274 profile_object);
275 } 274 }
276 } 275 }
277 276
278 void PrivetNotificationService::PrivetRemoveNotification() { 277 void PrivetNotificationService::PrivetRemoveNotification() {
279 ReportPrivetUmaEvent(PRIVET_NOTIFICATION_CANCELED); 278 ReportPrivetUmaEvent(PRIVET_NOTIFICATION_CANCELED);
280 Profile* profile_object = Profile::FromBrowserContext(profile_); 279 Profile* profile_object = Profile::FromBrowserContext(profile_);
281 g_browser_process->notification_ui_manager()->CancelById( 280 g_browser_process->notification_ui_manager()->CancelById(
282 kPrivetNotificationID, 281 kPrivetNotificationID,
283 NotificationUIManager::GetProfileID(profile_object)); 282 NotificationUIManager::GetProfileID(profile_object));
284 } 283 }
(...skipping 21 matching lines...) Expand all
306 if (IsForced()) { 305 if (IsForced()) {
307 StartLister(); 306 StartLister();
308 } else if (*enable_privet_notification_member_) { 307 } else if (*enable_privet_notification_member_) {
309 ReportPrivetUmaEvent(PRIVET_SERVICE_STARTED); 308 ReportPrivetUmaEvent(PRIVET_SERVICE_STARTED);
310 traffic_detector_ = 309 traffic_detector_ =
311 new PrivetTrafficDetector( 310 new PrivetTrafficDetector(
312 net::ADDRESS_FAMILY_IPV4, 311 net::ADDRESS_FAMILY_IPV4,
313 base::Bind(&PrivetNotificationService::StartLister, AsWeakPtr())); 312 base::Bind(&PrivetNotificationService::StartLister, AsWeakPtr()));
314 traffic_detector_->Start(); 313 traffic_detector_->Start();
315 } else { 314 } else {
316 traffic_detector_ = NULL; 315 traffic_detector_ = nullptr;
317 device_lister_.reset(); 316 device_lister_.reset();
318 service_discovery_client_ = NULL; 317 service_discovery_client_ = nullptr;
319 privet_notifications_listener_.reset(); 318 privet_notifications_listener_.reset();
320 } 319 }
321 #else 320 #else
322 if (IsForced() || *enable_privet_notification_member_) { 321 if (IsForced() || *enable_privet_notification_member_) {
323 StartLister(); 322 StartLister();
324 } else { 323 } else {
325 device_lister_.reset(); 324 device_lister_.reset();
326 service_discovery_client_ = NULL; 325 service_discovery_client_ = nullptr;
327 privet_notifications_listener_.reset(); 326 privet_notifications_listener_.reset();
328 } 327 }
329 #endif 328 #endif
330 } 329 }
331 330
332 void PrivetNotificationService::StartLister() { 331 void PrivetNotificationService::StartLister() {
333 ReportPrivetUmaEvent(PRIVET_LISTER_STARTED); 332 ReportPrivetUmaEvent(PRIVET_LISTER_STARTED);
334 #if defined(ENABLE_MDNS) 333 #if defined(ENABLE_MDNS)
335 traffic_detector_ = NULL; 334 traffic_detector_ = nullptr;
336 #endif // ENABLE_MDNS 335 #endif // ENABLE_MDNS
337 service_discovery_client_ = 336 service_discovery_client_ =
338 local_discovery::ServiceDiscoverySharedClient::GetInstance(); 337 local_discovery::ServiceDiscoverySharedClient::GetInstance();
339 device_lister_.reset( 338 device_lister_.reset(
340 new PrivetDeviceListerImpl(service_discovery_client_.get(), this)); 339 new PrivetDeviceListerImpl(service_discovery_client_.get(), this));
341 device_lister_->Start(); 340 device_lister_->Start();
342 device_lister_->DiscoverNewDevices(false); 341 device_lister_->DiscoverNewDevices(false);
343 342
344 std::unique_ptr<PrivetHTTPAsynchronousFactory> http_factory( 343 std::unique_ptr<PrivetHTTPAsynchronousFactory> http_factory(
345 PrivetHTTPAsynchronousFactory::CreateInstance( 344 PrivetHTTPAsynchronousFactory::CreateInstance(
(...skipping 13 matching lines...) Expand all
359 } 358 }
360 359
361 std::string PrivetNotificationDelegate::id() const { 360 std::string PrivetNotificationDelegate::id() const {
362 return kPrivetNotificationID; 361 return kPrivetNotificationID;
363 } 362 }
364 363
365 void PrivetNotificationDelegate::ButtonClick(int button_index) { 364 void PrivetNotificationDelegate::ButtonClick(int button_index) {
366 if (button_index == 0) { 365 if (button_index == 0) {
367 ReportPrivetUmaEvent(PRIVET_NOTIFICATION_CLICKED); 366 ReportPrivetUmaEvent(PRIVET_NOTIFICATION_CLICKED);
368 OpenTab(GURL(kPrivetNotificationOriginUrl)); 367 OpenTab(GURL(kPrivetNotificationOriginUrl));
369 } else if (button_index == 1) { 368 return;
370 ReportPrivetUmaEvent(PRIVET_DISABLE_NOTIFICATIONS_CLICKED);
371 DisableNotifications();
372 } 369 }
370
371 DCHECK_EQ(1, button_index);
372 ReportPrivetUmaEvent(PRIVET_DISABLE_NOTIFICATIONS_CLICKED);
373 DisableNotifications();
373 } 374 }
374 375
375 void PrivetNotificationDelegate::OpenTab(const GURL& url) { 376 void PrivetNotificationDelegate::OpenTab(const GURL& url) {
376 Profile* profile_obj = Profile::FromBrowserContext(profile_); 377 Profile* profile = Profile::FromBrowserContext(profile_);
377 378 chrome::NavigateParams params(profile, url,
378 chrome::NavigateParams params(profile_obj, 379 ui::PAGE_TRANSITION_AUTO_TOPLEVEL);
379 url,
380 ui::PAGE_TRANSITION_AUTO_TOPLEVEL);
381 params.disposition = NEW_FOREGROUND_TAB; 380 params.disposition = NEW_FOREGROUND_TAB;
382 chrome::Navigate(&params); 381 chrome::Navigate(&params);
383 } 382 }
384 383
385 void PrivetNotificationDelegate::DisableNotifications() { 384 void PrivetNotificationDelegate::DisableNotifications() {
386 Profile* profile_obj = Profile::FromBrowserContext(profile_); 385 Profile* profile = Profile::FromBrowserContext(profile_);
387 386 profile->GetPrefs()->SetBoolean(prefs::kLocalDiscoveryNotificationsEnabled,
388 profile_obj->GetPrefs()->SetBoolean( 387 false);
389 prefs::kLocalDiscoveryNotificationsEnabled,
390 false);
391 } 388 }
392 389
393 } // namespace cloud_print 390 } // namespace cloud_print
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/printing/cloud_print/privet_notifications_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698