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

Side by Side Diff: chrome/browser/chromeos/extensions/file_browser_notifications.cc

Issue 10947046: Eliminate kAshNotifyDisabled and SystemNotification (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix nit Created 8 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 | Annotate | Revision Log
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/extensions/file_browser_notifications.h" 5 #include "chrome/browser/chromeos/extensions/file_browser_notifications.h"
6 6
7 #include "ash/ash_switches.h"
8 #include "base/bind.h" 7 #include "base/bind.h"
9 #include "base/command_line.h"
10 #include "base/message_loop.h" 8 #include "base/message_loop.h"
11 #include "base/stl_util.h" 9 #include "base/stl_util.h"
12 #include "base/string_number_conversions.h" 10 #include "base/string_number_conversions.h"
13 #include "base/utf_string_conversions.h" 11 #include "base/utf_string_conversions.h"
14 #include "chrome/browser/chromeos/notifications/system_notification.h"
15 #include "chrome/browser/notifications/desktop_notification_service.h" 12 #include "chrome/browser/notifications/desktop_notification_service.h"
13 #include "chrome/browser/notifications/notification_delegate.h"
16 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/ui/browser.h" 15 #include "chrome/browser/ui/browser.h"
18 #include "chrome/browser/ui/browser_list.h" 16 #include "chrome/browser/ui/browser_list.h"
19 #include "chrome/browser/ui/webui/web_ui_util.h" 17 #include "chrome/browser/ui/webui/web_ui_util.h"
20 #include "content/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread.h"
21 #include "grit/generated_resources.h" 19 #include "grit/generated_resources.h"
22 #include "grit/theme_resources.h" 20 #include "grit/theme_resources.h"
23 #include "ui/base/l10n/l10n_util.h" 21 #include "ui/base/l10n/l10n_util.h"
24 #include "ui/base/resource/resource_bundle.h" 22 #include "ui/base/resource/resource_bundle.h"
25 23
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 break; 84 break;
87 default: 85 default:
88 NOTREACHED(); 86 NOTREACHED();
89 id = 0; 87 id = 0;
90 } 88 }
91 return l10n_util::GetStringUTF16(id); 89 return l10n_util::GetStringUTF16(id);
92 } 90 }
93 91
94 } // namespace 92 } // namespace
95 93
96 // Simple wrapper class to support ether old chromeos SystemNotifications or 94 // Manages file browser notifications. Generates a desktop notification on
97 // new ash notifications. 95 // construction and removes it from the host when closed. Owned by the host.
98 class FileBrowserNotifications::NotificationMessage { 96 class FileBrowserNotifications::NotificationMessage {
99 public: 97 public:
100 class Delegate : public NotificationDelegate { 98 class Delegate : public NotificationDelegate {
101 public: 99 public:
102 Delegate(const base::WeakPtr<FileBrowserNotifications>& host, 100 Delegate(const base::WeakPtr<FileBrowserNotifications>& host,
103 const std::string& id) 101 const std::string& id)
104 : host_(host), 102 : host_(host),
105 id_(id) {} 103 id_(id) {}
106 virtual void Display() OVERRIDE {} 104 virtual void Display() OVERRIDE {}
107 virtual void Error() OVERRIDE {} 105 virtual void Error() OVERRIDE {}
(...skipping 20 matching lines...) Expand all
128 126
129 NotificationMessage(FileBrowserNotifications* host, 127 NotificationMessage(FileBrowserNotifications* host,
130 Profile* profile, 128 Profile* profile,
131 NotificationType type, 129 NotificationType type,
132 const std::string& notification_id, 130 const std::string& notification_id,
133 const string16& message) 131 const string16& message)
134 : profile_(profile), 132 : profile_(profile),
135 type_(type), 133 type_(type),
136 notification_id_(notification_id), 134 notification_id_(notification_id),
137 message_(message) { 135 message_(message) {
138 if (!CommandLine::ForCurrentProcess()->HasSwitch( 136 const gfx::ImageSkia& icon =
139 ash::switches::kAshNotifyDisabled)) { 137 *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
140 const gfx::ImageSkia& icon = 138 IDR_FILES_APP_ICON);
141 *ResourceBundle::GetSharedInstance().GetImageSkiaNamed( 139 const string16 replace_id = UTF8ToUTF16(notification_id_);
142 IDR_FILES_APP_ICON); 140 DesktopNotificationService::AddIconNotification(
143 string16 replace_id = UTF8ToUTF16(notification_id_); 141 GURL(), GetTitle(type_), message, icon, replace_id,
144 notification_id_ = DesktopNotificationService::AddIconNotification( 142 new Delegate(host->AsWeakPtr(), notification_id_), profile_);
145 GURL(), GetTitle(type_), message, icon, replace_id,
146 new Delegate(host->AsWeakPtr(), notification_id_), profile_);
147 } else {
148 system_notification_.reset(
149 new chromeos::SystemNotification(profile_,
150 notification_id_,
151 GetIconId(type_),
152 GetTitle(type_)));
153 }
154 } 143 }
155 144
156 ~NotificationMessage() { 145 ~NotificationMessage() {
157 } 146 }
158 147
159 void Show() {
160 if (system_notification_.get())
161 system_notification_->Show(message_, false, false);
162 // Ash notifications are shown automatically (only) when created.
163 }
164
165 void Hide() {
166 if (system_notification_.get() && system_notification_->visible())
167 system_notification_->Hide();
168 // Ash notifications are hidden automatically.
169 }
170
171 void set_message(const string16& message) { message_ = message; }
172
173 private: 148 private:
174 Profile* profile_; 149 Profile* profile_;
175 NotificationType type_; 150 NotificationType type_;
176 std::string notification_id_; 151 std::string notification_id_;
177 string16 message_; 152 string16 message_;
178 scoped_ptr<chromeos::SystemNotification> system_notification_;
179 153
180 DISALLOW_COPY_AND_ASSIGN(NotificationMessage); 154 DISALLOW_COPY_AND_ASSIGN(NotificationMessage);
181 }; 155 };
182 156
183 struct FileBrowserNotifications::MountRequestsInfo { 157 struct FileBrowserNotifications::MountRequestsInfo {
184 bool mount_success_exists; 158 bool mount_success_exists;
185 bool fail_message_finalized; 159 bool fail_message_finalized;
186 bool fail_notification_shown; 160 bool fail_notification_shown;
187 bool non_parent_device_failed; 161 bool non_parent_device_failed;
188 bool device_notification_hidden; 162 bool device_notification_hidden;
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
356 if (type == DEVICE_FAIL) { 330 if (type == DEVICE_FAIL) {
357 MountRequestsMap::const_iterator it = mount_requests_.find(path); 331 MountRequestsMap::const_iterator it = mount_requests_.find(path);
358 if (it != mount_requests_.end()) 332 if (it != mount_requests_.end())
359 id.append(base::IntToString(it->second.fail_notifications_count)); 333 id.append(base::IntToString(it->second.fail_notifications_count));
360 } 334 }
361 335
362 id.append(path); 336 id.append(path);
363 return id; 337 return id;
364 } 338 }
365 339
366 FileBrowserNotifications::NotificationMessage*
367 FileBrowserNotifications::GetNotification(NotificationType type,
368 const std::string& notification_id,
369 const string16& message) {
370 NotificationMap::iterator iter = notification_map_.find(notification_id);
371 if (iter != notification_map_.end()) {
372 iter->second->set_message(message);
373 return iter->second;
374 }
375 NotificationMessage* new_message =
376 new NotificationMessage(this, profile_, type, notification_id, message);
377 notification_map_[notification_id] = new_message;
378 return new_message;
379 }
380
381 void FileBrowserNotifications::ShowNotificationById( 340 void FileBrowserNotifications::ShowNotificationById(
382 NotificationType type, 341 NotificationType type,
383 const std::string& notification_id, 342 const std::string& notification_id,
384 const string16& message) { 343 const string16& message) {
385 if (hidden_notifications_.find(notification_id) != 344 if (hidden_notifications_.find(notification_id) !=
386 hidden_notifications_.end()) { 345 hidden_notifications_.end()) {
387 // Notification was hidden after a delayed show was requested. 346 // Notification was hidden after a delayed show was requested.
388 hidden_notifications_.erase(notification_id); 347 hidden_notifications_.erase(notification_id);
389 return; 348 return;
390 } 349 }
391 NotificationMessage* notification = 350 if (notification_map_.find(notification_id) != notification_map_.end()) {
392 GetNotification(type, notification_id, message); 351 // Remove any existing notification with |notification_id|.
393 notification->Show(); 352 // Will trigger Delegate::Close which will call RemoveNotificationById.
353 DesktopNotificationService::RemoveNotification(notification_id);
354 DCHECK(notification_map_.find(notification_id) == notification_map_.end());
355 }
356 // Create a new notification with |notification_id|.
357 NotificationMessage* new_message =
358 new NotificationMessage(this, profile_, type, notification_id, message);
359 notification_map_[notification_id] = new_message;
394 } 360 }
395 361
396 void FileBrowserNotifications::HideNotificationById( 362 void FileBrowserNotifications::HideNotificationById(
397 const std::string& notification_id) { 363 const std::string& notification_id) {
398 NotificationMap::iterator it = notification_map_.find(notification_id); 364 NotificationMap::iterator it = notification_map_.find(notification_id);
399 if (it != notification_map_.end()) { 365 if (it != notification_map_.end()) {
400 if (!CommandLine::ForCurrentProcess()->HasSwitch( 366 // Will trigger Delegate::Close which will call RemoveNotificationById.
401 ash::switches::kAshNotifyDisabled)) { 367 DesktopNotificationService::RemoveNotification(notification_id);
402 // Will trigger Delegate::Close which will call RemoveNotificationById.
403 DesktopNotificationService::RemoveNotification(notification_id);
404 } else {
405 NotificationMessage* notification = it->second;
406 notification_map_.erase(it);
407 notification->Hide();
408 delete notification;
409 }
410 } else { 368 } else {
411 // Mark as hidden so it does not get shown from a delayed task. 369 // Mark as hidden so it does not get shown from a delayed task.
412 hidden_notifications_.insert(notification_id); 370 hidden_notifications_.insert(notification_id);
413 } 371 }
414 } 372 }
415 373
416 void FileBrowserNotifications::RemoveNotificationById( 374 void FileBrowserNotifications::RemoveNotificationById(
417 const std::string& notification_id) { 375 const std::string& notification_id) {
418 NotificationMap::iterator it = notification_map_.find(notification_id); 376 NotificationMap::iterator it = notification_map_.find(notification_id);
419 if (it != notification_map_.end()) { 377 if (it != notification_map_.end()) {
420 NotificationMessage* notification = it->second; 378 NotificationMessage* notification = it->second;
421 notification_map_.erase(it); 379 notification_map_.erase(it);
422 notification->Hide();
423 delete notification; 380 delete notification;
424 } 381 }
425 } 382 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698