summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/extensions/file_browser_notifications.cc
blob: 0f70b2d24b48fcb423eefe73c0d51bedae6531ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/extensions/file_browser_notifications.h"

#include "base/bind.h"
#include "base/message_loop.h"
#include "base/stl_util.h"
#include "base/string_number_conversions.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/notifications/desktop_notification_service.h"
#include "chrome/browser/notifications/notification_delegate.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/webui/web_ui_util.h"

namespace {

struct NotificationTypeInfo {
  FileBrowserNotifications::NotificationType type;
  const char* notification_id_prefix;
  int icon_id;
  int title_id;
  int message_id;
};

// Information about notification types.
// The order of notification types in the array must match the order of types in
// NotificationType enum (i.e. the following MUST be satisfied:
// kNotificationTypes[type].type == type).
const NotificationTypeInfo kNotificationTypes[] = {
    {
      FileBrowserNotifications::DEVICE,  // type
      "Device_",  // notification_id_prefix
      IDR_FILES_APP_ICON,  // icon_id
      IDS_REMOVABLE_DEVICE_DETECTION_TITLE,  // title_id
      IDS_REMOVABLE_DEVICE_SCANNING_MESSAGE  // message_id
    },
    {
      FileBrowserNotifications::DEVICE_FAIL,  // type
      "DeviceFail_",  // notification_id_prefix
      IDR_FILES_APP_ICON,  // icon_id
      IDS_REMOVABLE_DEVICE_DETECTION_TITLE,  // title_id
      IDS_DEVICE_UNSUPPORTED_DEFAULT_MESSAGE  // message_id
    },
    {
      FileBrowserNotifications::DEVICE_HARD_UNPLUG,  // type
      "HardUnplug_",  // notification_id_prefix
      IDR_PAGEINFO_WARNING_MAJOR,  // icon_id
      IDS_REMOVABLE_DEVICE_HARD_UNPLUG_TITLE,  // title_id
      IDS_EXTERNAL_STORAGE_HARD_UNPLUG_MESSAGE  // message_id
    },
    {
      FileBrowserNotifications::DEVICE_EXTERNAL_STORAGE_DISABLED,  // type
      "DeviceFail_",  // nottification_id_prefix; same as for DEVICE_FAIL.
      IDR_FILES_APP_ICON,  // icon_id
      IDS_REMOVABLE_DEVICE_DETECTION_TITLE,  // title_id
      IDS_EXTERNAL_STORAGE_DISABLED_MESSAGE  // message_id
    },
    {
      FileBrowserNotifications::FORMAT_START,  // type
      "FormatStart_",  // notification_id_prefix
      IDR_FILES_APP_ICON,  // icon_id
      IDS_FORMATTING_OF_DEVICE_PENDING_TITLE,  // title_id
      IDS_FORMATTING_OF_DEVICE_PENDING_MESSAGE  // message_id
    },
    {
      FileBrowserNotifications::FORMAT_START_FAIL,  // type
      "FormatComplete_",  // notification_id_prefix
      IDR_FILES_APP_ICON,  // icon_id
      IDS_FORMATTING_OF_DEVICE_FAILED_TITLE,  // title_id
      IDS_FORMATTING_STARTED_FAILURE_MESSAGE  // message_id
    },
    {
      FileBrowserNotifications::FORMAT_SUCCESS,  // type
      "FormatComplete_",  // notification_id_prefix
      IDR_FILES_APP_ICON,  // icon_id
      IDS_FORMATTING_OF_DEVICE_FINISHED_TITLE,  // title_id
      IDS_FORMATTING_FINISHED_SUCCESS_MESSAGE  // message_id
    },
    {
      FileBrowserNotifications::FORMAT_FAIL,  // type
      "FormatComplete_",  // notifications_id_prefix
      IDR_FILES_APP_ICON,  // icon_id
      IDS_FORMATTING_OF_DEVICE_FAILED_TITLE,  // title_id
      IDS_FORMATTING_FINISHED_FAILURE_MESSAGE  // message_id
    },
};

int GetIconId(FileBrowserNotifications::NotificationType type) {
  DCHECK_GE(type, 0);
  DCHECK_LT(static_cast<size_t>(type), arraysize(kNotificationTypes));
  DCHECK(kNotificationTypes[type].type == type);

  return kNotificationTypes[type].icon_id;
}

string16 GetTitle(FileBrowserNotifications::NotificationType type) {
  DCHECK_GE(type, 0);
  DCHECK_LT(static_cast<size_t>(type), arraysize(kNotificationTypes));
  DCHECK(kNotificationTypes[type].type == type);

  int id = kNotificationTypes[type].title_id;
  if (id < 0)
    return string16();
  return l10n_util::GetStringUTF16(id);
}

string16 GetMessage(FileBrowserNotifications::NotificationType type) {
  DCHECK_GE(type, 0);
  DCHECK_LT(static_cast<size_t>(type), arraysize(kNotificationTypes));
  DCHECK(kNotificationTypes[type].type == type);

  int id = kNotificationTypes[type].message_id;
  if (id < 0)
    return string16();
  return l10n_util::GetStringUTF16(id);
}

std::string GetNotificationId(FileBrowserNotifications::NotificationType type,
                               const std::string& path) {
  DCHECK_GE(type, 0);
  DCHECK_LT(static_cast<size_t>(type), arraysize(kNotificationTypes));
  DCHECK(kNotificationTypes[type].type == type);

  std::string id_prefix(kNotificationTypes[type].notification_id_prefix);
  return id_prefix.append(path);
}

}  // namespace

// Manages file browser notifications. Generates a desktop notification on
// construction and removes it from the host when closed. Owned by the host.
class FileBrowserNotifications::NotificationMessage {
 public:
  class Delegate : public NotificationDelegate {
   public:
    Delegate(const base::WeakPtr<FileBrowserNotifications>& host,
             const std::string& id)
        : host_(host),
          id_(id) {}
    virtual void Display() OVERRIDE {}
    virtual void Error() OVERRIDE {}
    virtual void Close(bool by_user) OVERRIDE {
      if (host_)
        host_->RemoveNotificationById(id_);
    }
    virtual void Click() OVERRIDE {
      // TODO(tbarzic): Show more info page once we have one.
    }
    virtual std::string id() const OVERRIDE { return id_; }
    virtual content::RenderViewHost* GetRenderViewHost() const OVERRIDE {
      return NULL;
    }

   private:
    virtual ~Delegate() {}

    base::WeakPtr<FileBrowserNotifications> host_;
    std::string id_;

    DISALLOW_COPY_AND_ASSIGN(Delegate);
  };

  NotificationMessage(FileBrowserNotifications* host,
                      Profile* profile,
                      NotificationType type,
                      const std::string& notification_id,
                      const string16& message)
      : message_(message) {
    const gfx::ImageSkia& icon =
        *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
            GetIconId(type));
    const string16 replace_id = UTF8ToUTF16(notification_id);
    DesktopNotificationService::AddIconNotification(
        GURL(), GetTitle(type), message, icon, replace_id,
        new Delegate(host->AsWeakPtr(), notification_id), profile);
  }

  ~NotificationMessage() {}

  // Used in test.
  string16 message() { return message_; }

 private:
  string16 message_;

  DISALLOW_COPY_AND_ASSIGN(NotificationMessage);
};

struct FileBrowserNotifications::MountRequestsInfo {
  bool mount_success_exists;
  bool fail_message_finalized;
  bool fail_notification_shown;
  bool non_parent_device_failed;
  bool device_notification_hidden;

  MountRequestsInfo() : mount_success_exists(false),
                        fail_message_finalized(false),
                        fail_notification_shown(false),
                        non_parent_device_failed(false),
                        device_notification_hidden(false) {
  }
};

FileBrowserNotifications::FileBrowserNotifications(Profile* profile)
    : profile_(profile) {
}

FileBrowserNotifications::~FileBrowserNotifications() {
  STLDeleteContainerPairSecondPointers(notification_map_.begin(),
                                       notification_map_.end());
}

void FileBrowserNotifications::RegisterDevice(const std::string& path) {
  mount_requests_.insert(MountRequestsMap::value_type(path,
                                                      MountRequestsInfo()));
}

void FileBrowserNotifications::UnregisterDevice(const std::string& path) {
  mount_requests_.erase(path);
}

void FileBrowserNotifications::ManageNotificationsOnMountCompleted(
    const std::string& system_path, const std::string& label, bool is_parent,
    bool success, bool is_unsupported) {
  MountRequestsMap::iterator it = mount_requests_.find(system_path);
  if (it == mount_requests_.end())
    return;

  // We have to hide device scanning notification if we haven't done it already.
  if (!it->second.device_notification_hidden) {
    HideNotification(DEVICE, system_path);
    it->second.device_notification_hidden = true;
  }

  // Check if there is fail notification for parent device. If so, disregard it.
  // (parent device contains partition table, which is unmountable).
  if (!is_parent && it->second.fail_notification_shown &&
      !it->second.non_parent_device_failed) {
    HideNotification(DEVICE_FAIL, system_path);
    it->second.fail_notification_shown = false;
  }

  // If notification can't change any more, no need to continue.
  if (it->second.fail_message_finalized)
    return;

  int notification_message_id = 0;

  // Do we have a multi-partition device for which at least one mount failed.
  bool fail_on_multipartition_device =
      success ? it->second.non_parent_device_failed
              : it->second.mount_success_exists ||
                it->second.non_parent_device_failed;

  if (fail_on_multipartition_device) {
    it->second.fail_message_finalized = true;
    notification_message_id =
        label.empty() ? IDS_MULTIPART_DEVICE_UNSUPPORTED_DEFAULT_MESSAGE
                      : IDS_MULTIPART_DEVICE_UNSUPPORTED_MESSAGE;
  } else if (!success) {
    // First device failed.
    if (!is_unsupported) {
      notification_message_id =
          label.empty() ? IDS_DEVICE_UNKNOWN_DEFAULT_MESSAGE
                        : IDS_DEVICE_UNKNOWN_MESSAGE;
    } else {
      notification_message_id =
          label.empty() ? IDS_DEVICE_UNSUPPORTED_DEFAULT_MESSAGE
                        : IDS_DEVICE_UNSUPPORTED_MESSAGE;
    }
  }

  if (success) {
    it->second.mount_success_exists = true;
  } else {
    it->second.non_parent_device_failed |= !is_parent;
  }

  if (notification_message_id == 0)
    return;

  if (it->second.fail_notification_shown) {
    HideNotification(DEVICE_FAIL, system_path);
  } else {
    it->second.fail_notification_shown = true;
  }

  if (!label.empty()) {
    ShowNotificationWithMessage(DEVICE_FAIL, system_path,
        l10n_util::GetStringFUTF16(notification_message_id,
                                   ASCIIToUTF16(label)));
  } else {
    ShowNotificationWithMessage(DEVICE_FAIL, system_path,
        l10n_util::GetStringUTF16(notification_message_id));
  }
}

void FileBrowserNotifications::ShowNotification(NotificationType type,
                                                const std::string& path) {
  ShowNotificationWithMessage(type, path, GetMessage(type));
}

void FileBrowserNotifications::ShowNotificationWithMessage(
    NotificationType type,
    const std::string& path,
    const string16& message) {
  std::string notification_id = GetNotificationId(type, path);
  hidden_notifications_.erase(notification_id);
  ShowNotificationById(type, notification_id, message);
}

void FileBrowserNotifications::ShowNotificationDelayed(
    NotificationType type,
    const std::string& path,
    base::TimeDelta delay) {
  std::string notification_id = GetNotificationId(type, path);
  hidden_notifications_.erase(notification_id);
  MessageLoop::current()->PostDelayedTask(
      FROM_HERE,
      base::Bind(&FileBrowserNotifications::ShowNotificationById, AsWeakPtr(),
                 type, notification_id, GetMessage(type)),
      delay);
}

void FileBrowserNotifications::HideNotification(NotificationType type,
                                                const std::string& path) {
  std::string notification_id = GetNotificationId(type, path);
  HideNotificationById(notification_id);
}

void FileBrowserNotifications::HideNotificationDelayed(
    NotificationType type, const std::string& path, base::TimeDelta delay) {
  MessageLoop::current()->PostDelayedTask(
      FROM_HERE,
      base::Bind(&FileBrowserNotifications::HideNotification, AsWeakPtr(),
                 type, path),
      delay);
}

void FileBrowserNotifications::ShowNotificationById(
    NotificationType type,
    const std::string& notification_id,
    const string16& message) {
  if (hidden_notifications_.find(notification_id) !=
      hidden_notifications_.end()) {
    // Notification was hidden after a delayed show was requested.
    hidden_notifications_.erase(notification_id);
    return;
  }
  if (notification_map_.find(notification_id) != notification_map_.end()) {
    // Remove any existing notification with |notification_id|.
    // Will trigger Delegate::Close which will call RemoveNotificationById.
    DesktopNotificationService::RemoveNotification(notification_id);
    DCHECK(notification_map_.find(notification_id) == notification_map_.end());
  }
  // Create a new notification with |notification_id|.
  NotificationMessage* new_message =
      new NotificationMessage(this, profile_, type, notification_id, message);
  notification_map_[notification_id] = new_message;
}

void FileBrowserNotifications::HideNotificationById(
    const std::string& notification_id) {
  NotificationMap::iterator it = notification_map_.find(notification_id);
  if (it != notification_map_.end()) {
    // Will trigger Delegate::Close which will call RemoveNotificationById.
    DesktopNotificationService::RemoveNotification(notification_id);
  } else {
    // Mark as hidden so it does not get shown from a delayed task.
    hidden_notifications_.insert(notification_id);
  }
}

void FileBrowserNotifications::RemoveNotificationById(
    const std::string& notification_id) {
  NotificationMap::iterator it = notification_map_.find(notification_id);
  if (it != notification_map_.end()) {
    NotificationMessage* notification = it->second;
    notification_map_.erase(it);
    delete notification;
  }
}

string16 FileBrowserNotifications::GetNotificationMessageForTest(
    const std::string& id) const {
  NotificationMap::const_iterator it = notification_map_.find(id);
  if (it == notification_map_.end())
    return string16();
  return it->second->message();
}