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

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

Issue 2446043002: Close privet printer notifications when clicked. (Closed)
Patch Set: Add test, fix potential UAF Created 4 years, 1 month 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
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 237 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 message_center::ButtonInfo(l10n_util::GetStringUTF16( 248 message_center::ButtonInfo(l10n_util::GetStringUTF16(
249 IDS_LOCAL_DISCOVERY_NOTIFICATIONS_DISABLE_BUTTON_LABEL))); 249 IDS_LOCAL_DISCOVERY_NOTIFICATIONS_DISABLE_BUTTON_LABEL)));
250 250
251 base::string16 title = l10n_util::GetPluralStringFUTF16( 251 base::string16 title = l10n_util::GetPluralStringFUTF16(
252 IDS_LOCAL_DISCOVERY_NOTIFICATION_TITLE_PRINTER, devices_active); 252 IDS_LOCAL_DISCOVERY_NOTIFICATION_TITLE_PRINTER, devices_active);
253 base::string16 body = l10n_util::GetPluralStringFUTF16( 253 base::string16 body = l10n_util::GetPluralStringFUTF16(
254 IDS_LOCAL_DISCOVERY_NOTIFICATION_CONTENTS_PRINTER, devices_active); 254 IDS_LOCAL_DISCOVERY_NOTIFICATION_CONTENTS_PRINTER, devices_active);
255 base::string16 product_name = 255 base::string16 product_name =
256 l10n_util::GetStringUTF16(IDS_LOCAL_DISCOVERY_SERVICE_NAME_PRINTER); 256 l10n_util::GetStringUTF16(IDS_LOCAL_DISCOVERY_SERVICE_NAME_PRINTER);
257 257
258 Profile* profile = Profile::FromBrowserContext(profile_);
yoshiki 2016/10/26 16:24:05 I think you don't need to convert the profile. Pro
Lei Zhang 2016/10/26 16:52:41 Profile is a BrowserContext, but a BrowserContext
258 Notification notification( 259 Notification notification(
259 message_center::NOTIFICATION_TYPE_SIMPLE, title, body, 260 message_center::NOTIFICATION_TYPE_SIMPLE, title, body,
260 ui::ResourceBundle::GetSharedInstance().GetImageNamed( 261 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
261 IDR_LOCAL_DISCOVERY_CLOUDPRINT_ICON), 262 IDR_LOCAL_DISCOVERY_CLOUDPRINT_ICON),
262 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, 263 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT,
263 kPrivetNotificationID), 264 kPrivetNotificationID),
264 product_name, GURL(kPrivetNotificationOriginUrl), kPrivetNotificationID, 265 product_name, GURL(kPrivetNotificationOriginUrl), kPrivetNotificationID,
265 rich_notification_data, new PrivetNotificationDelegate(profile_)); 266 rich_notification_data, CreateNotificationDelegate(profile));
266 267
267 auto* notification_ui_manager = g_browser_process->notification_ui_manager(); 268 auto* notification_ui_manager = g_browser_process->notification_ui_manager();
268 Profile* profile = Profile::FromBrowserContext(profile_);
269 bool updated = notification_ui_manager->Update(notification, 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 notification_ui_manager->Add(notification, profile); 273 notification_ui_manager->Add(notification, profile);
274 } 274 }
275 } 275 }
276 276
277 void PrivetNotificationService::PrivetRemoveNotification() { 277 void PrivetNotificationService::PrivetRemoveNotification() {
278 ReportPrivetUmaEvent(PRIVET_NOTIFICATION_CANCELED); 278 ReportPrivetUmaEvent(PRIVET_NOTIFICATION_CANCELED);
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 342
343 std::unique_ptr<PrivetHTTPAsynchronousFactory> http_factory( 343 std::unique_ptr<PrivetHTTPAsynchronousFactory> http_factory(
344 PrivetHTTPAsynchronousFactory::CreateInstance( 344 PrivetHTTPAsynchronousFactory::CreateInstance(
345 content::BrowserContext::GetDefaultStoragePartition(profile_)-> 345 content::BrowserContext::GetDefaultStoragePartition(profile_)->
346 GetURLRequestContext())); 346 GetURLRequestContext()));
347 347
348 privet_notifications_listener_.reset( 348 privet_notifications_listener_.reset(
349 new PrivetNotificationsListener(std::move(http_factory), this)); 349 new PrivetNotificationsListener(std::move(http_factory), this));
350 } 350 }
351 351
352 PrivetNotificationDelegate::PrivetNotificationDelegate( 352 PrivetNotificationDelegate*
353 content::BrowserContext* profile) 353 PrivetNotificationService::CreateNotificationDelegate(Profile* profile) {
354 : profile_(profile) { 354 return new PrivetNotificationDelegate(profile);
355 } 355 }
356 356
357 PrivetNotificationDelegate::PrivetNotificationDelegate(Profile* profile)
358 : profile_(profile) {}
359
357 PrivetNotificationDelegate::~PrivetNotificationDelegate() { 360 PrivetNotificationDelegate::~PrivetNotificationDelegate() {
358 } 361 }
359 362
360 std::string PrivetNotificationDelegate::id() const { 363 std::string PrivetNotificationDelegate::id() const {
361 return kPrivetNotificationID; 364 return kPrivetNotificationID;
362 } 365 }
363 366
364 void PrivetNotificationDelegate::ButtonClick(int button_index) { 367 void PrivetNotificationDelegate::ButtonClick(int button_index) {
365 if (button_index == 0) { 368 if (button_index == 0) {
366 ReportPrivetUmaEvent(PRIVET_NOTIFICATION_CLICKED); 369 ReportPrivetUmaEvent(PRIVET_NOTIFICATION_CLICKED);
367 OpenTab(GURL(kPrivetNotificationOriginUrl)); 370 OpenTab(GURL(kPrivetNotificationOriginUrl));
368 return; 371 } else {
372 DCHECK_EQ(1, button_index);
373 ReportPrivetUmaEvent(PRIVET_DISABLE_NOTIFICATIONS_CLICKED);
374 DisableNotifications();
369 } 375 }
370 376 CloseNotification();
Lei Zhang 2016/10/26 00:22:09 If I move this to the top, like it was in patch se
dewittj 2016/10/26 16:04:26 Acknowledged.
371 DCHECK_EQ(1, button_index);
372 ReportPrivetUmaEvent(PRIVET_DISABLE_NOTIFICATIONS_CLICKED);
373 DisableNotifications();
374 } 377 }
375 378
376 void PrivetNotificationDelegate::OpenTab(const GURL& url) { 379 void PrivetNotificationDelegate::OpenTab(const GURL& url) {
377 Profile* profile = Profile::FromBrowserContext(profile_); 380 chrome::NavigateParams params(profile_, url,
378 chrome::NavigateParams params(profile, url,
379 ui::PAGE_TRANSITION_AUTO_TOPLEVEL); 381 ui::PAGE_TRANSITION_AUTO_TOPLEVEL);
380 params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB; 382 params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
381 chrome::Navigate(&params); 383 chrome::Navigate(&params);
382 } 384 }
383 385
384 void PrivetNotificationDelegate::DisableNotifications() { 386 void PrivetNotificationDelegate::DisableNotifications() {
385 Profile* profile = Profile::FromBrowserContext(profile_); 387 profile_->GetPrefs()->SetBoolean(prefs::kLocalDiscoveryNotificationsEnabled,
386 profile->GetPrefs()->SetBoolean(prefs::kLocalDiscoveryNotificationsEnabled, 388 false);
387 false); 389 }
390
391 void PrivetNotificationDelegate::CloseNotification() {
392 g_browser_process->notification_ui_manager()->CancelById(
393 id(), NotificationUIManager::GetProfileID(profile_));
388 } 394 }
389 395
390 } // namespace cloud_print 396 } // namespace cloud_print
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698