*/
this.message = message;
- Object.freeze(this);
+ /**
+ * Queue of API call.
+ * @type {AsyncUtil.Queue}
+ * @private
+ */
+ this.queue_ = new AsyncUtil.Queue();
+
+ /**
+ * Timeout ID.
+ * @type {number}
+ * @private
+ */
+ this.pendingShowTimerId_ = 0;
+
+ Object.seal(this);
};
/**
* Shows the notification for the device path.
* @param {string} devicePath Device path.
* @param {string=} opt_message Message overrides the default message.
+ * @return {string} Notification ID.
*/
DeviceHandler.Notification.prototype.show = function(devicePath, opt_message) {
- chrome.notifications.create(
- this.makeId_(devicePath),
- {
- type: 'basic',
- title: str(this.title),
- message: opt_message || str(this.message),
- iconUrl: chrome.runtime.getURL('/common/images/icon96.png')
- },
- function() {});
+ this.clearTimeout_();
+ var notificationId = this.makeId_(devicePath);
+ this.queue_.run(function(callback) {
+ chrome.notifications.create(
+ notificationId,
+ {
+ type: 'basic',
+ title: str(this.title),
+ message: opt_message || str(this.message),
+ iconUrl: chrome.runtime.getURL('/common/images/icon96.png')
+ },
+ callback);
+ }.bind(this));
+ return notificationId;
+};
+
+/**
+ * Shows the notificaiton after 5 seconds.
+ * @param {string} devicePath Device path.
+ */
+DeviceHandler.Notification.prototype.showLater = function(devicePath) {
+ this.clearTimeout_();
+ this.pendingShowTimerId_ = setTimeout(this.show.bind(this, devicePath), 5000);
};
/**
* @param {string} devicePath Device path.
*/
DeviceHandler.Notification.prototype.hide = function(devicePath) {
- chrome.notifications.clear(this.makeId_(devicePath), function() {});
+ this.clearTimeout_();
+ this.queue_.run(function(callback) {
+ chrome.notifications.clear(this.makeId_(devicePath), callback);
+ }.bind(this));
};
/**
};
/**
+ * Cancels the timeout request.
+ * @private
+ */
+DeviceHandler.Notification.prototype.clearTimeout_ = function() {
+ if (this.pendingShowTimerId_) {
+ clearTimeout(this.pendingShowTimerId_);
+ this.pendingShowTimerId_ = 0;
+ }
+};
+
+/**
* Handles notifications from C++ sides.
* @param {DeviceEvent} event Device event.
* @private
DeviceHandler.prototype.onDeviceChanged_ = function(event) {
switch (event.type) {
case 'added':
- DeviceHandler.Notification.DEVICE.show(event.devicePath);
+ DeviceHandler.Notification.DEVICE.showLater(event.devicePath);
this.mountStatus_[event.devicePath] = DeviceHandler.MountStatus.NO_RESULT;
break;
case 'disabled':