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

Side by Side Diff: chrome/browser/notifications/message_center_notification_manager.cc

Issue 580243004: Remove unnecessary image downloading handler of notifications. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: re-upload Created 6 years, 3 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
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/notifications/message_center_notification_manager.h" 5 #include "chrome/browser/notifications/message_center_notification_manager.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/prefs/pref_registry_simple.h" 9 #include "base/prefs/pref_registry_simple.h"
10 #include "base/prefs/pref_service.h" 10 #include "base/prefs/pref_service.h"
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 // default message center (extension_id.empty()). 139 // default message center (extension_id.empty()).
140 140
141 // If there exist apps/extensions that have notificationProvider permission, 141 // If there exist apps/extensions that have notificationProvider permission,
142 // route notifications to one of the apps/extensions. 142 // route notifications to one of the apps/extensions.
143 std::string extension_id = GetExtensionTakingOverNotifications(profile); 143 std::string extension_id = GetExtensionTakingOverNotifications(profile);
144 if (!extension_id.empty()) 144 if (!extension_id.empty())
145 profile_notification->AddToAlternateProvider(extension_id); 145 profile_notification->AddToAlternateProvider(extension_id);
146 146
147 message_center_->AddNotification(make_scoped_ptr( 147 message_center_->AddNotification(make_scoped_ptr(
148 new message_center::Notification(profile_notification->notification()))); 148 new message_center::Notification(profile_notification->notification())));
149 profile_notification->StartDownloads();
150 } 149 }
151 150
152 bool MessageCenterNotificationManager::Update(const Notification& notification, 151 bool MessageCenterNotificationManager::Update(const Notification& notification,
153 Profile* profile) { 152 Profile* profile) {
154 const base::string16& replace_id = notification.replace_id(); 153 const base::string16& replace_id = notification.replace_id();
155 if (replace_id.empty()) 154 if (replace_id.empty())
156 return false; 155 return false;
157 156
158 const GURL origin_url = notification.origin_url(); 157 const GURL origin_url = notification.origin_url();
159 DCHECK(origin_url.is_valid()); 158 DCHECK(origin_url.is_valid());
(...skipping 24 matching lines...) Expand all
184 // providers. 183 // providers.
185 184
186 // WARNING: You MUST use AddProfileNotification or update the message 185 // WARNING: You MUST use AddProfileNotification or update the message
187 // center via the notification within a ProfileNotification object or the 186 // center via the notification within a ProfileNotification object or the
188 // profile ID will not be correctly set for ChromeOS. 187 // profile ID will not be correctly set for ChromeOS.
189 message_center_->UpdateNotification( 188 message_center_->UpdateNotification(
190 old_id, 189 old_id,
191 make_scoped_ptr(new message_center::Notification( 190 make_scoped_ptr(new message_center::Notification(
192 new_notification->notification()))); 191 new_notification->notification())));
193 192
194 new_notification->StartDownloads();
195 return true; 193 return true;
196 } 194 }
197 } 195 }
198 return false; 196 return false;
199 } 197 }
200 198
201 const Notification* MessageCenterNotificationManager::FindById( 199 const Notification* MessageCenterNotificationManager::FindById(
202 const std::string& id) const { 200 const std::string& id) const {
203 NotificationMap::const_iterator iter = profile_notifications_.find(id); 201 NotificationMap::const_iterator iter = profile_notifications_.find(id);
204 if (iter == profile_notifications_.end()) 202 if (iter == profile_notifications_.end())
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
329 const content::NotificationDetails& details) { 327 const content::NotificationDetails& details) {
330 if (type == chrome::NOTIFICATION_FULLSCREEN_CHANGED) { 328 if (type == chrome::NOTIFICATION_FULLSCREEN_CHANGED) {
331 const bool is_fullscreen = *content::Details<bool>(details).ptr(); 329 const bool is_fullscreen = *content::Details<bool>(details).ptr();
332 330
333 if (is_fullscreen && tray_.get() && tray_->GetMessageCenterTray()) 331 if (is_fullscreen && tray_.get() && tray_->GetMessageCenterTray())
334 tray_->GetMessageCenterTray()->HidePopupBubble(); 332 tray_->GetMessageCenterTray()->HidePopupBubble();
335 } 333 }
336 } 334 }
337 335
338 //////////////////////////////////////////////////////////////////////////////// 336 ////////////////////////////////////////////////////////////////////////////////
339 // ImageDownloads
340
341 MessageCenterNotificationManager::ImageDownloads::ImageDownloads(
342 message_center::MessageCenter* message_center,
343 ImageDownloadsObserver* observer)
344 : message_center_(message_center),
345 pending_downloads_(0),
346 observer_(observer) {
347 }
348
349 MessageCenterNotificationManager::ImageDownloads::~ImageDownloads() { }
350
351 void MessageCenterNotificationManager::ImageDownloads::StartDownloads(
352 const Notification& notification) {
353 // In case all downloads are synchronous, assume a pending download.
354 AddPendingDownload();
355
356 // Notification image.
357 StartDownloadWithImage(
358 notification,
359 NULL,
360 notification.image_url(),
361 base::Bind(&message_center::MessageCenter::SetNotificationImage,
362 base::Unretained(message_center_),
363 notification.delegate_id()));
364
365 // Notification button icons.
366 StartDownloadWithImage(
367 notification,
368 NULL,
369 notification.button_one_icon_url(),
370 base::Bind(&message_center::MessageCenter::SetNotificationButtonIcon,
371 base::Unretained(message_center_),
372 notification.delegate_id(),
373 0));
374 StartDownloadWithImage(
375 notification,
376 NULL,
377 notification.button_two_icon_url(),
378 base::Bind(&message_center::MessageCenter::SetNotificationButtonIcon,
379 base::Unretained(message_center_),
380 notification.delegate_id(),
381 1));
382
383 // This should tell the observer we're done if everything was synchronous.
384 PendingDownloadCompleted();
385 }
386
387 void MessageCenterNotificationManager::ImageDownloads::StartDownloadWithImage(
388 const Notification& notification,
389 const gfx::Image* image,
390 const GURL& url,
391 const SetImageCallback& callback) {
392 // Set the image directly if we have it.
393 if (image && !image->IsEmpty()) {
394 callback.Run(*image);
395 return;
396 }
397
398 // Leave the image null if there's no URL.
399 if (url.is_empty())
400 return;
401
402 content::WebContents* contents = notification.GetWebContents();
403 if (!contents) {
404 LOG(WARNING) << "Notification needs an image but has no WebContents";
405 return;
406 }
407
408 AddPendingDownload();
409
410 contents->DownloadImage(
411 url,
412 false, // Not a favicon
413 0, // No maximum size
414 base::Bind(
415 &MessageCenterNotificationManager::ImageDownloads::DownloadComplete,
416 AsWeakPtr(),
417 callback));
418 }
419
420 void MessageCenterNotificationManager::ImageDownloads::DownloadComplete(
421 const SetImageCallback& callback,
422 int download_id,
423 int http_status_code,
424 const GURL& image_url,
425 const std::vector<SkBitmap>& bitmaps,
426 const std::vector<gfx::Size>& original_bitmap_sizes) {
427 PendingDownloadCompleted();
428
429 if (bitmaps.empty())
430 return;
431 gfx::Image image = gfx::Image::CreateFrom1xBitmap(bitmaps[0]);
432 callback.Run(image);
433 }
434
435 // Private methods.
436
437 void MessageCenterNotificationManager::ImageDownloads::AddPendingDownload() {
438 ++pending_downloads_;
439 }
440
441 void
442 MessageCenterNotificationManager::ImageDownloads::PendingDownloadCompleted() {
443 DCHECK(pending_downloads_ > 0);
444 if (--pending_downloads_ == 0 && observer_)
445 observer_->OnDownloadsCompleted();
446 }
447
448 ////////////////////////////////////////////////////////////////////////////////
449 // ProfileNotification 337 // ProfileNotification
450 338
451 MessageCenterNotificationManager::ProfileNotification::ProfileNotification( 339 MessageCenterNotificationManager::ProfileNotification::ProfileNotification(
452 Profile* profile, 340 Profile* profile,
453 const Notification& notification, 341 const Notification& notification,
454 message_center::MessageCenter* message_center) 342 message_center::MessageCenter* message_center)
455 : profile_(profile), 343 : profile_(profile),
456 notification_(notification), 344 notification_(notification) {
457 downloads_(new ImageDownloads(message_center, this)) {
458 DCHECK(profile); 345 DCHECK(profile);
459 #if defined(OS_CHROMEOS) 346 #if defined(OS_CHROMEOS)
460 notification_.set_profile_id(multi_user_util::GetUserIDFromProfile(profile)); 347 notification_.set_profile_id(multi_user_util::GetUserIDFromProfile(profile));
461 #endif 348 #endif
462 } 349 }
463 350
464 MessageCenterNotificationManager::ProfileNotification::~ProfileNotification() { 351 MessageCenterNotificationManager::ProfileNotification::~ProfileNotification() {
465 } 352 }
466 353
467 void MessageCenterNotificationManager::ProfileNotification::StartDownloads() {
468 downloads_->StartDownloads(notification_);
469 }
470
471 void
472 MessageCenterNotificationManager::ProfileNotification::OnDownloadsCompleted() {
473 notification_.DoneRendering();
474 }
475
476 void 354 void
477 MessageCenterNotificationManager::ProfileNotification::AddToAlternateProvider( 355 MessageCenterNotificationManager::ProfileNotification::AddToAlternateProvider(
478 const std::string extension_id) { 356 const std::string extension_id) {
479 // Convert data from Notification type to NotificationOptions type. 357 // Convert data from Notification type to NotificationOptions type.
480 extensions::api::notifications::NotificationOptions options; 358 extensions::api::notifications::NotificationOptions options;
481 NotificationConversionHelper::NotificationToNotificationOptions(notification_, 359 NotificationConversionHelper::NotificationToNotificationOptions(notification_,
482 &options); 360 &options);
483 361
484 // Send the notification to the alternate provider extension/app. 362 // Send the notification to the alternate provider extension/app.
485 extensions::NotificationProviderEventRouter event_router(profile_); 363 extensions::NotificationProviderEventRouter event_router(profile_);
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
531 it != registry->enabled_extensions().end(); 409 it != registry->enabled_extensions().end();
532 ++it) { 410 ++it) {
533 if ((*it->get()).permissions_data()->HasAPIPermission( 411 if ((*it->get()).permissions_data()->HasAPIPermission(
534 extensions::APIPermission::ID::kNotificationProvider)) { 412 extensions::APIPermission::ID::kNotificationProvider)) {
535 extension_id = (*it->get()).id(); 413 extension_id = (*it->get()).id();
536 return extension_id; 414 return extension_id;
537 } 415 }
538 } 416 }
539 return extension_id; 417 return extension_id;
540 } 418 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698