Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / local_discovery / local_discovery.js
index ab37a66..68fe480 100644 (file)
 cr.define('local_discovery', function() {
   'use strict';
 
-  /**
-   * Prefix for printer management page URLs, relative to base cloud print URL.
-   * @type {string}
-   */
-  var PRINTER_MANAGEMENT_PAGE_PREFIX = '#printers/';
-
   // Histogram buckets for UMA tracking.
   /** @const */ var DEVICES_PAGE_EVENTS = {
     OPENED: 0,
@@ -104,10 +98,11 @@ cr.define('local_discovery', function() {
 
       this.registerButton = fillDeviceDescription(
         this.domElement,
-        this.info.human_readable_name,
+        this.info.display_name,
         this.info.description,
+        this.info.type,
         loadTimeData.getString('serviceRegister'),
-        this.showRegister.bind(this));
+        this.showRegister.bind(this, this.info.type));
 
       this.setRegisterEnabled(this.registerEnabled);
     },
@@ -126,7 +121,8 @@ cr.define('local_discovery', function() {
     register: function() {
       recordUmaEvent(DEVICES_PAGE_EVENTS.REGISTER_CONFIRMED);
       chrome.send('registerDevice', [this.info.service_name]);
-      setRegisterPage('register-page-adding1');
+      setRegisterPage(isPrinter(this.info.type) ?
+          'register-printer-page-adding1' : 'register-device-page-adding1');
     },
     /**
      * Show registrtation UI for device.
@@ -134,8 +130,9 @@ cr.define('local_discovery', function() {
     showRegister: function() {
       recordUmaEvent(DEVICES_PAGE_EVENTS.REGISTER_CLICKED);
       $('register-message').textContent = loadTimeData.getStringF(
-        'registerConfirmMessage',
-        this.info.human_readable_name);
+        isPrinter(this.info.type) ? 'registerPrinterConfirmMessage' :
+                                    'registerDeviceConfirmMessage',
+        this.info.display_name);
       $('register-continue-button').onclick = this.register.bind(this);
       showRegisterOverlay();
     },
@@ -189,17 +186,20 @@ cr.define('local_discovery', function() {
    * @param {HTMLElement} device_dom_element Element to be filled.
    * @param {string} name Name of device.
    * @param {string} description Description of device.
+   * @param {string} type Type of device.
    * @param {string} button_text Text to appear on button.
-   * @param {function()} button_action Action for button.
+   * @param {function()?} button_action Action for button.
    * @return {HTMLElement} The button (for enabling/disabling/rebinding)
    */
   function fillDeviceDescription(device_dom_element,
-                                name,
-                                description,
-                                button_text,
-                                button_action) {
+                                 name,
+                                 description,
+                                 type,
+                                 button_text,
+                                 button_action) {
     device_dom_element.classList.add('device');
-    device_dom_element.classList.add('printer');
+    if (isPrinter(type))
+      device_dom_element.classList.add('printer');
 
     var deviceInfo = document.createElement('div');
     deviceInfo.className = 'device-info';
@@ -215,10 +215,12 @@ cr.define('local_discovery', function() {
     deviceDescription.textContent = description;
     deviceInfo.appendChild(deviceDescription);
 
-    var button = document.createElement('button');
-    button.textContent = button_text;
-    button.addEventListener('click', button_action);
-    device_dom_element.appendChild(button);
+    if (button_action) {
+      var button = document.createElement('button');
+      button.textContent = button_text;
+      button.addEventListener('click', button_action);
+      device_dom_element.appendChild(button);
+    }
 
     return button;
   }
@@ -289,7 +291,16 @@ cr.define('local_discovery', function() {
    * Update UI to reflect that registration has been confirmed on the printer.
    */
   function onRegistrationConfirmedOnPrinter() {
-    setRegisterPage('register-page-adding2');
+    setRegisterPage('register-printer-page-adding2');
+  }
+
+  /**
+   * Shows UI to confirm security code.
+   * @param {string} code The security code to confirm.
+   */
+  function onRegistrationConfirmDeviceCode(code) {
+    setRegisterPage('register-device-page-adding2');
+    $('register-device-page-code').textContent = code;
   }
 
   /**
@@ -331,14 +342,19 @@ cr.define('local_discovery', function() {
 
     var description;
     if (device.description == '') {
-        description = loadTimeData.getString('noDescription');
-      } else {
-        description = device.description;
-      }
+      if (isPrinter(device.type))
+        description = loadTimeData.getString('noDescriptionPrinter');
+      else
+        description = loadTimeData.getString('noDescriptionDevice');
+    } else {
+      description = device.description;
+    }
 
     fillDeviceDescription(devicesDomElement, device.display_name,
-                          description, 'Manage' /*Localize*/,
-                          manageCloudDevice.bind(null, device.id));
+                          description, device.type,
+                          loadTimeData.getString('manageDevice'),
+                          isPrinter(device.type) ?
+                              manageCloudDevice.bind(null, device.id) : null);
     return devicesDomElement;
   }
 
@@ -432,15 +448,15 @@ cr.define('local_discovery', function() {
   }
 
   /**
-   * Request the printer list.
+   * Request the device list.
    */
-  function requestPrinterList() {
+  function requestDeviceList() {
     if (isUserLoggedIn) {
       clearElement($('cloud-devices'));
       $('cloud-devices-loading').hidden = false;
       $('cloud-devices-unavailable').hidden = true;
 
-      chrome.send('requestPrinterList');
+      chrome.send('requestDeviceList');
     }
   }
 
@@ -450,8 +466,7 @@ cr.define('local_discovery', function() {
    */
   function manageCloudDevice(device_id) {
     recordUmaEvent(DEVICES_PAGE_EVENTS.MANAGE_CLICKED);
-    chrome.send('openCloudPrintURL',
-                [PRINTER_MANAGEMENT_PAGE_PREFIX + device_id]);
+    chrome.send('openCloudPrintURL', [device_id]);
   }
 
   /**
@@ -474,10 +489,18 @@ cr.define('local_discovery', function() {
   }
 
   /**
+   * Confirms device code.
+   */
+  function confirmCode() {
+    chrome.send('confirmCode');
+    setRegisterPage('register-device-page-adding1');
+  }
+
+  /**
    * Retry loading the devices from Google Cloud Print.
    */
   function retryLoadCloudDevices() {
-    requestPrinterList();
+    requestDeviceList();
   }
 
   /**
@@ -491,7 +514,7 @@ cr.define('local_discovery', function() {
     $('register-continue-button').disabled = !isUserLoggedIn;
 
     if (isUserLoggedIn) {
-      requestPrinterList();
+      requestDeviceList();
       $('register-login-promo').hidden = true;
     } else {
       $('cloud-devices-loading').hidden = true;
@@ -554,7 +577,7 @@ cr.define('local_discovery', function() {
       } else {
         $('cloudPrintConnectorSetupButton').onclick = function(event) {
           chrome.send('disableCloudPrintConnector');
-          requestPrinterList();
+          requestDeviceList();
         };
       }
     }
@@ -575,16 +598,28 @@ cr.define('local_discovery', function() {
     }
   }
 
+  /**
+   * Returns true of device is printer.
+   * @param {string} type Type of printer.
+   */
+  function isPrinter(type) {
+    return type == 'printer';
+  }
+
   document.addEventListener('DOMContentLoaded', function() {
     cr.ui.overlay.setupOverlay($('overlay'));
     cr.ui.overlay.globalInitialization();
     $('overlay').addEventListener('cancelOverlay', cancelRegistration);
 
-    var cancelButtons = document.querySelectorAll('.register-cancel');
-    var cancelButtonsLength = cancelButtons.length;
-    for (var i = 0; i < cancelButtonsLength; i++) {
-      cancelButtons[i].addEventListener('click', cancelRegistration);
-    }
+    [].forEach.call(
+        document.querySelectorAll('.register-cancel'), function(button) {
+      button.addEventListener('click', cancelRegistration);
+    });
+
+    [].forEach.call(
+        document.querySelectorAll('.confirm-code'), function(button) {
+      button.addEventListener('click', confirmCode);
+    });
 
     $('register-error-exit').addEventListener('click', cancelRegistration);
 
@@ -604,6 +639,13 @@ cr.define('local_discovery', function() {
       'click',
       registerOverlayLoginButtonClicked);
 
+    if (loadTimeData.valueExists('backButtonURL')) {
+      $('back-button').hidden = false;
+      $('back-button').addEventListener('click', function() {
+        window.location.href = loadTimeData.getString('backButtonURL');
+      });
+    }
+
     updateVisibility();
     document.addEventListener('visibilitychange', updateVisibility, false);
 
@@ -619,6 +661,7 @@ cr.define('local_discovery', function() {
     onRegistrationFailed: onRegistrationFailed,
     onUnregisteredDeviceUpdate: onUnregisteredDeviceUpdate,
     onRegistrationConfirmedOnPrinter: onRegistrationConfirmedOnPrinter,
+    onRegistrationConfirmDeviceCode: onRegistrationConfirmDeviceCode,
     onCloudDeviceListAvailable: onCloudDeviceListAvailable,
     onCloudDeviceListUnavailable: onCloudDeviceListUnavailable,
     onDeviceCacheFlushed: onDeviceCacheFlushed,