Upstream version 7.35.139.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / file_manager / background / js / device_handler.js
index a6197bb..4906ff2 100644 (file)
@@ -50,7 +50,21 @@ DeviceHandler.Notification = function(prefix, title, message) {
    */
   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);
 };
 
 /**
@@ -112,17 +126,32 @@ DeviceHandler.Notification.FORMAT_FAIL = new DeviceHandler.Notification(
  * 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);
 };
 
 /**
@@ -130,7 +159,10 @@ DeviceHandler.Notification.prototype.show = function(devicePath, opt_message) {
  * @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));
 };
 
 /**
@@ -144,6 +176,17 @@ DeviceHandler.Notification.prototype.makeId_ = function(devicePath) {
 };
 
 /**
+ * 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
@@ -151,7 +194,7 @@ DeviceHandler.Notification.prototype.makeId_ = function(devicePath) {
 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':