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

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

Issue 11418123: Some refactoring in file browser notifications: (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 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 | 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 "base/bind.h" 7 #include "base/bind.h"
8 #include "base/message_loop.h" 8 #include "base/message_loop.h"
9 #include "base/stl_util.h" 9 #include "base/stl_util.h"
10 #include "base/string_number_conversions.h" 10 #include "base/string_number_conversions.h"
11 #include "base/utf_string_conversions.h" 11 #include "base/utf_string_conversions.h"
12 #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" 13 #include "chrome/browser/notifications/notification_delegate.h"
14 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/browser.h" 15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/browser_list.h" 16 #include "chrome/browser/ui/browser_list.h"
17 #include "chrome/browser/ui/webui/web_ui_util.h" 17 #include "chrome/browser/ui/webui/web_ui_util.h"
18 #include "content/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread.h"
19 #include "grit/generated_resources.h" 19 #include "grit/generated_resources.h"
20 #include "grit/theme_resources.h" 20 #include "grit/theme_resources.h"
21 #include "ui/base/l10n/l10n_util.h" 21 #include "ui/base/l10n/l10n_util.h"
22 #include "ui/base/resource/resource_bundle.h" 22 #include "ui/base/resource/resource_bundle.h"
23 23
24 namespace { 24 namespace {
25 25
26 struct NotificationTypeInfo {
27 FileBrowserNotifications::NotificationType type;
28 const char* notification_id_prefix;
29 int icon_id;
30 int title_id;
31 int message_id;
32 };
33
34 // Information about notification types.
35 // The order of notification types in the array must match the order of types in
36 // NotificationType enum (i.e. the following MUST be satisfied:
37 // kNotificationTypes[type].type == type).
38 const NotificationTypeInfo kNotificationTypes[] = {
39 {
40 FileBrowserNotifications::DEVICE, // type
41 "Device_", // notification_id_prefix
42 IDR_FILES_APP_ICON, // icon_id
43 IDS_REMOVABLE_DEVICE_DETECTION_TITLE, // title_id
44 IDS_REMOVABLE_DEVICE_SCANNING_MESSAGE // message_id
45 },
46 {
47 FileBrowserNotifications::DEVICE_FAIL, // type
48 "DeviceFail_", // notification_id_prefix
49 IDR_FILES_APP_ICON, // icon_id
50 IDS_REMOVABLE_DEVICE_DETECTION_TITLE, // title_id
51 IDS_DEVICE_UNSUPPORTED_DEFAULT_MESSAGE // message_id
52 },
53 {
54 FileBrowserNotifications::DEVICE_HARD_UNPLUG, // type
55 "HardUnplug_", // notification_id_prefix
56 IDR_PAGEINFO_WARNING_MAJOR, // icon_id
57 IDS_REMOVABLE_DEVICE_HARD_UNPLUG_TITLE, // title_id
58 IDS_EXTERNAL_STORAGE_HARD_UNPLUG_MESSAGE // message_id
59 },
60 {
61 FileBrowserNotifications::DEVICE_EXTERNAL_STORAGE_DISABLED, // type
62 "DeviceFail_", // nottification_id_prefix; same as for DEVICE_FAIL.
63 IDR_FILES_APP_ICON, // icon_id
64 IDS_REMOVABLE_DEVICE_DETECTION_TITLE, // title_id
65 IDS_EXTERNAL_STORAGE_DISABLED_MESSAGE // message_id
66 },
67 {
68 FileBrowserNotifications::FORMAT_SUCCESS, // type
69 "FormatComplete_", // notification_id_prefix
70 IDR_FILES_APP_ICON, // icon_id
71 IDS_REMOVABLE_DEVICE_DETECTION_TITLE, // title_id
72 IDS_FORMATTING_FINISHED_SUCCESS_MESSAGE // message_id
73 },
74 {
75 FileBrowserNotifications::FORMAT_FAIL, // type
76 "FormatComplete_", // notifications_id_prefix
77 IDR_FILES_APP_ICON, // icon_id
78 IDS_FORMATTING_OF_DEVICE_FINISHED_TITLE, // title_id
79 IDS_FORMATTING_FINISHED_FAILURE_MESSAGE // message_id
80 },
81 {
82 FileBrowserNotifications::FORMAT_START, // type
83 "FormatStart_", // notification_id_prefix
84 IDR_FILES_APP_ICON, // icon_id
85 IDS_FORMATTING_OF_DEVICE_FINISHED_TITLE, // title_id
86 IDS_FORMATTING_OF_DEVICE_PENDING_MESSAGE // message_id
87 },
88 {
89 FileBrowserNotifications::FORMAT_START_FAIL, // type
90 "FormatComplete_", // notification_id_prefix
91 IDR_FILES_APP_ICON, // icon_id
92 IDS_FORMATTING_OF_DEVICE_FINISHED_TITLE, // title_id
93 IDS_FORMATTING_STARTED_FAILURE_MESSAGE // message_id
94 },
95 };
96
26 int GetIconId(FileBrowserNotifications::NotificationType type) { 97 int GetIconId(FileBrowserNotifications::NotificationType type) {
27 switch (type) { 98 DCHECK(type < arraysize(kNotificationTypes));
28 case FileBrowserNotifications::DEVICE: 99 DCHECK(kNotificationTypes[type].type == type);
29 case FileBrowserNotifications::DEVICE_FAIL: 100
30 case FileBrowserNotifications::FORMAT_SUCCESS: 101 return kNotificationTypes[type].icon_id;
31 case FileBrowserNotifications::FORMAT_FAIL:
32 case FileBrowserNotifications::FORMAT_START:
33 case FileBrowserNotifications::FORMAT_START_FAIL:
34 case FileBrowserNotifications::GDATA_SYNC:
35 case FileBrowserNotifications::GDATA_SYNC_SUCCESS:
36 case FileBrowserNotifications::GDATA_SYNC_FAIL:
37 return IDR_FILES_APP_ICON;
38 case FileBrowserNotifications::DEVICE_HARD_UNPLUG:
39 return IDR_PAGEINFO_WARNING_MAJOR;
40 default:
41 NOTREACHED();
42 return 0;
43 }
44 } 102 }
45 103
46 string16 GetTitle(FileBrowserNotifications::NotificationType type) { 104 string16 GetTitle(FileBrowserNotifications::NotificationType type) {
47 int id; 105 DCHECK(type < arraysize(kNotificationTypes));
48 switch (type) { 106 DCHECK(kNotificationTypes[type].type == type);
49 case FileBrowserNotifications::DEVICE: 107
50 case FileBrowserNotifications::DEVICE_FAIL: 108 int id = kNotificationTypes[type].title_id;
51 id = IDS_REMOVABLE_DEVICE_DETECTION_TITLE; 109 if (id < 0)
52 break; 110 return string16();
53 case FileBrowserNotifications::DEVICE_HARD_UNPLUG:
54 id = IDS_REMOVABLE_DEVICE_HARD_UNPLUG_TITLE;
55 break;
56 case FileBrowserNotifications::FORMAT_START:
57 id = IDS_FORMATTING_OF_DEVICE_PENDING_TITLE;
58 break;
59 case FileBrowserNotifications::FORMAT_START_FAIL:
60 case FileBrowserNotifications::FORMAT_SUCCESS:
61 case FileBrowserNotifications::FORMAT_FAIL:
62 id = IDS_FORMATTING_OF_DEVICE_FINISHED_TITLE;
63 break;
64 default:
65 NOTREACHED();
66 id = 0;
67 }
68 return l10n_util::GetStringUTF16(id); 111 return l10n_util::GetStringUTF16(id);
69 } 112 }
70 113
71 string16 GetMessage(FileBrowserNotifications::NotificationType type) { 114 string16 GetMessage(FileBrowserNotifications::NotificationType type) {
72 int id; 115 DCHECK(type < arraysize(kNotificationTypes));
73 switch (type) { 116 DCHECK(kNotificationTypes[type].type == type);
74 case FileBrowserNotifications::DEVICE: 117
75 id = IDS_REMOVABLE_DEVICE_SCANNING_MESSAGE; 118 int id = kNotificationTypes[type].message_id;
76 break; 119 if (id < 0)
77 case FileBrowserNotifications::DEVICE_FAIL: 120 return string16();
78 id = IDS_DEVICE_UNSUPPORTED_DEFAULT_MESSAGE;
79 break;
80 case FileBrowserNotifications::DEVICE_HARD_UNPLUG:
81 id = IDS_EXTERNAL_STORAGE_HARD_UNPLUG_MESSAGE;
82 break;
83 case FileBrowserNotifications::FORMAT_FAIL:
84 id = IDS_FORMATTING_FINISHED_FAILURE_MESSAGE;
85 break;
86 case FileBrowserNotifications::FORMAT_SUCCESS:
87 id = IDS_FORMATTING_FINISHED_SUCCESS_MESSAGE;
88 break;
89 case FileBrowserNotifications::FORMAT_START:
90 id = IDS_FORMATTING_OF_DEVICE_PENDING_MESSAGE;
91 break;
92 case FileBrowserNotifications::FORMAT_START_FAIL:
93 id = IDS_FORMATTING_STARTED_FAILURE_MESSAGE;
94 break;
95 default:
96 NOTREACHED();
97 id = 0;
98 }
99 return l10n_util::GetStringUTF16(id); 121 return l10n_util::GetStringUTF16(id);
100 } 122 }
101 123
124 std::string GetNotificationId(FileBrowserNotifications::NotificationType type,
125 const std::string& path) {
126 DCHECK(type < arraysize(kNotificationTypes));
127 DCHECK(kNotificationTypes[type].type == type);
128
129 std::string id_prefix(kNotificationTypes[type].notification_id_prefix);
130 return id_prefix.append(path);
131 }
132
102 } // namespace 133 } // namespace
103 134
104 // Manages file browser notifications. Generates a desktop notification on 135 // Manages file browser notifications. Generates a desktop notification on
105 // construction and removes it from the host when closed. Owned by the host. 136 // construction and removes it from the host when closed. Owned by the host.
106 class FileBrowserNotifications::NotificationMessage { 137 class FileBrowserNotifications::NotificationMessage {
107 public: 138 public:
108 class Delegate : public NotificationDelegate { 139 class Delegate : public NotificationDelegate {
109 public: 140 public:
110 Delegate(const base::WeakPtr<FileBrowserNotifications>& host, 141 Delegate(const base::WeakPtr<FileBrowserNotifications>& host,
111 const std::string& id) 142 const std::string& id)
(...skipping 20 matching lines...) Expand all
132 std::string id_; 163 std::string id_;
133 164
134 DISALLOW_COPY_AND_ASSIGN(Delegate); 165 DISALLOW_COPY_AND_ASSIGN(Delegate);
135 }; 166 };
136 167
137 NotificationMessage(FileBrowserNotifications* host, 168 NotificationMessage(FileBrowserNotifications* host,
138 Profile* profile, 169 Profile* profile,
139 NotificationType type, 170 NotificationType type,
140 const std::string& notification_id, 171 const std::string& notification_id,
141 const string16& message) 172 const string16& message)
142 : profile_(profile), 173 : message_(message) {
143 type_(type),
144 notification_id_(notification_id),
145 message_(message) {
146 const gfx::ImageSkia& icon = 174 const gfx::ImageSkia& icon =
147 *ResourceBundle::GetSharedInstance().GetImageSkiaNamed( 175 *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
148 GetIconId(type_)); 176 GetIconId(type));
149 const string16 replace_id = UTF8ToUTF16(notification_id_); 177 const string16 replace_id = UTF8ToUTF16(notification_id);
150 DesktopNotificationService::AddIconNotification( 178 DesktopNotificationService::AddIconNotification(
151 GURL(), GetTitle(type_), message, icon, replace_id, 179 GURL(), GetTitle(type), message, icon, replace_id,
152 new Delegate(host->AsWeakPtr(), notification_id_), profile_); 180 new Delegate(host->AsWeakPtr(), notification_id), profile);
153 } 181 }
154 182
155 ~NotificationMessage() { 183 ~NotificationMessage() {}
156 } 184
185 // Used in test.
186 string16 message() { return message_; }
157 187
158 private: 188 private:
159 Profile* profile_;
160 NotificationType type_;
161 std::string notification_id_;
162 string16 message_; 189 string16 message_;
163 190
164 DISALLOW_COPY_AND_ASSIGN(NotificationMessage); 191 DISALLOW_COPY_AND_ASSIGN(NotificationMessage);
165 }; 192 };
166 193
167 struct FileBrowserNotifications::MountRequestsInfo { 194 struct FileBrowserNotifications::MountRequestsInfo {
168 bool mount_success_exists; 195 bool mount_success_exists;
169 bool fail_message_finalized; 196 bool fail_message_finalized;
170 bool fail_notification_shown; 197 bool fail_notification_shown;
171 bool non_parent_device_failed; 198 bool non_parent_device_failed;
172 bool device_notification_hidden; 199 bool device_notification_hidden;
173 int fail_notifications_count;
174 200
175 MountRequestsInfo() : mount_success_exists(false), 201 MountRequestsInfo() : mount_success_exists(false),
176 fail_message_finalized(false), 202 fail_message_finalized(false),
177 fail_notification_shown(false), 203 fail_notification_shown(false),
178 non_parent_device_failed(false), 204 non_parent_device_failed(false),
179 device_notification_hidden(false), 205 device_notification_hidden(false) {
180 fail_notifications_count(0) {
181 } 206 }
182 }; 207 };
183 208
184 FileBrowserNotifications::FileBrowserNotifications(Profile* profile) 209 FileBrowserNotifications::FileBrowserNotifications(Profile* profile)
185 : profile_(profile) { 210 : profile_(profile) {
186 } 211 }
187 212
188 FileBrowserNotifications::~FileBrowserNotifications() { 213 FileBrowserNotifications::~FileBrowserNotifications() {
189 STLDeleteContainerPairSecondPointers(notification_map_.begin(), 214 STLDeleteContainerPairSecondPointers(notification_map_.begin(),
190 notification_map_.end()); 215 notification_map_.end());
(...skipping 22 matching lines...) Expand all
213 } 238 }
214 239
215 // Check if there is fail notification for parent device. If so, disregard it. 240 // Check if there is fail notification for parent device. If so, disregard it.
216 // (parent device contains partition table, which is unmountable). 241 // (parent device contains partition table, which is unmountable).
217 if (!is_parent && it->second.fail_notification_shown && 242 if (!is_parent && it->second.fail_notification_shown &&
218 !it->second.non_parent_device_failed) { 243 !it->second.non_parent_device_failed) {
219 HideNotification(DEVICE_FAIL, system_path); 244 HideNotification(DEVICE_FAIL, system_path);
220 it->second.fail_notification_shown = false; 245 it->second.fail_notification_shown = false;
221 } 246 }
222 247
223 // If notificaiton can't change any more, no need to continue. 248 // If notification can't change any more, no need to continue.
224 if (it->second.fail_message_finalized) 249 if (it->second.fail_message_finalized)
225 return; 250 return;
226 251
227 int notification_message_id = 0; 252 int notification_message_id = 0;
228 253
229 // Do we have a multi-partition device for which at least one mount failed. 254 // Do we have a multi-partition device for which at least one mount failed.
230 bool fail_on_multipartition_device = 255 bool fail_on_multipartition_device =
231 success ? it->second.non_parent_device_failed 256 success ? it->second.non_parent_device_failed
232 : it->second.mount_success_exists || 257 : it->second.mount_success_exists ||
233 it->second.non_parent_device_failed; 258 it->second.non_parent_device_failed;
(...skipping 24 matching lines...) Expand all
258 283
259 if (notification_message_id == 0) 284 if (notification_message_id == 0)
260 return; 285 return;
261 286
262 if (it->second.fail_notification_shown) { 287 if (it->second.fail_notification_shown) {
263 HideNotification(DEVICE_FAIL, system_path); 288 HideNotification(DEVICE_FAIL, system_path);
264 } else { 289 } else {
265 it->second.fail_notification_shown = true; 290 it->second.fail_notification_shown = true;
266 } 291 }
267 292
268 it->second.fail_notifications_count++;
269
270 if (!label.empty()) { 293 if (!label.empty()) {
271 ShowNotificationWithMessage(DEVICE_FAIL, system_path, 294 ShowNotificationWithMessage(DEVICE_FAIL, system_path,
272 l10n_util::GetStringFUTF16(notification_message_id, 295 l10n_util::GetStringFUTF16(notification_message_id,
273 ASCIIToUTF16(label))); 296 ASCIIToUTF16(label)));
274 } else { 297 } else {
275 ShowNotificationWithMessage(DEVICE_FAIL, system_path, 298 ShowNotificationWithMessage(DEVICE_FAIL, system_path,
276 l10n_util::GetStringUTF16(notification_message_id)); 299 l10n_util::GetStringUTF16(notification_message_id));
277 } 300 }
278 } 301 }
279 302
280 void FileBrowserNotifications::ShowNotification(NotificationType type, 303 void FileBrowserNotifications::ShowNotification(NotificationType type,
281 const std::string& path) { 304 const std::string& path) {
282 ShowNotificationWithMessage(type, path, GetMessage(type)); 305 ShowNotificationWithMessage(type, path, GetMessage(type));
283 } 306 }
284 307
285 void FileBrowserNotifications::ShowNotificationWithMessage( 308 void FileBrowserNotifications::ShowNotificationWithMessage(
286 NotificationType type, 309 NotificationType type,
287 const std::string& path, 310 const std::string& path,
288 const string16& message) { 311 const string16& message) {
289 std::string notification_id = CreateNotificationId(type, path); 312 std::string notification_id = GetNotificationId(type, path);
290 hidden_notifications_.erase(notification_id); 313 hidden_notifications_.erase(notification_id);
291 ShowNotificationById(type, notification_id, message); 314 ShowNotificationById(type, notification_id, message);
292 } 315 }
293 316
294 void FileBrowserNotifications::ShowNotificationDelayed( 317 void FileBrowserNotifications::ShowNotificationDelayed(
295 NotificationType type, 318 NotificationType type,
296 const std::string& path, 319 const std::string& path,
297 base::TimeDelta delay) { 320 base::TimeDelta delay) {
298 std::string notification_id = CreateNotificationId(type, path); 321 std::string notification_id = GetNotificationId(type, path);
299 hidden_notifications_.erase(notification_id); 322 hidden_notifications_.erase(notification_id);
300 MessageLoop::current()->PostDelayedTask( 323 MessageLoop::current()->PostDelayedTask(
301 FROM_HERE, 324 FROM_HERE,
302 base::Bind(&FileBrowserNotifications::ShowNotificationById, AsWeakPtr(), 325 base::Bind(&FileBrowserNotifications::ShowNotificationById, AsWeakPtr(),
303 type, notification_id, GetMessage(type)), 326 type, notification_id, GetMessage(type)),
304 delay); 327 delay);
305 } 328 }
306 329
307 void FileBrowserNotifications::HideNotification(NotificationType type, 330 void FileBrowserNotifications::HideNotification(NotificationType type,
308 const std::string& path) { 331 const std::string& path) {
309 std::string notification_id = CreateNotificationId(type, path); 332 std::string notification_id = GetNotificationId(type, path);
310 HideNotificationById(notification_id); 333 HideNotificationById(notification_id);
311 } 334 }
312 335
313 void FileBrowserNotifications::HideNotificationDelayed( 336 void FileBrowserNotifications::HideNotificationDelayed(
314 NotificationType type, const std::string& path, base::TimeDelta delay) { 337 NotificationType type, const std::string& path, base::TimeDelta delay) {
315 MessageLoop::current()->PostDelayedTask( 338 MessageLoop::current()->PostDelayedTask(
316 FROM_HERE, 339 FROM_HERE,
317 base::Bind(&FileBrowserNotifications::HideNotification, AsWeakPtr(), 340 base::Bind(&FileBrowserNotifications::HideNotification, AsWeakPtr(),
318 type, path), 341 type, path),
319 delay); 342 delay);
320 } 343 }
321 344
322 std::string FileBrowserNotifications::CreateNotificationId(
323 NotificationType type,
324 const std::string& path) {
325 std::string id;
326 switch (type) {
327 case DEVICE:
328 id = "D";
329 break;
330 case DEVICE_FAIL:
331 id = "DF";
332 break;
333 case FORMAT_START:
334 id = "FS";
335 break;
336 default:
337 id = "FF";
338 }
339
340 if (type == DEVICE_FAIL) {
341 MountRequestsMap::const_iterator it = mount_requests_.find(path);
342 if (it != mount_requests_.end())
343 id.append(base::IntToString(it->second.fail_notifications_count));
344 }
345
346 id.append(path);
347 return id;
348 }
349
350 void FileBrowserNotifications::ShowNotificationById( 345 void FileBrowserNotifications::ShowNotificationById(
351 NotificationType type, 346 NotificationType type,
352 const std::string& notification_id, 347 const std::string& notification_id,
353 const string16& message) { 348 const string16& message) {
354 if (hidden_notifications_.find(notification_id) != 349 if (hidden_notifications_.find(notification_id) !=
355 hidden_notifications_.end()) { 350 hidden_notifications_.end()) {
356 // Notification was hidden after a delayed show was requested. 351 // Notification was hidden after a delayed show was requested.
357 hidden_notifications_.erase(notification_id); 352 hidden_notifications_.erase(notification_id);
358 return; 353 return;
359 } 354 }
(...skipping 23 matching lines...) Expand all
383 378
384 void FileBrowserNotifications::RemoveNotificationById( 379 void FileBrowserNotifications::RemoveNotificationById(
385 const std::string& notification_id) { 380 const std::string& notification_id) {
386 NotificationMap::iterator it = notification_map_.find(notification_id); 381 NotificationMap::iterator it = notification_map_.find(notification_id);
387 if (it != notification_map_.end()) { 382 if (it != notification_map_.end()) {
388 NotificationMessage* notification = it->second; 383 NotificationMessage* notification = it->second;
389 notification_map_.erase(it); 384 notification_map_.erase(it);
390 delete notification; 385 delete notification;
391 } 386 }
392 } 387 }
388
389 string16 FileBrowserNotifications::GetNotificationMessageForTest(
390 const std::string& id) const {
391 NotificationMap::const_iterator it = notification_map_.find(id);
392 if (it == notification_map_.end())
393 return string16();
394 return it->second->message();
395 }
396
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698