Home | History | Annotate | Download | only in ui
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 'use strict';
      6 
      7 tvcm.require('ui.spy');
      8 tvcm.require('tvcm.ui');
      9 tvcm.require('tvcm.ui.dom_helpers');
     10 tvcm.requireTemplate('ui.spy_shell');
     11 
     12 tvcm.exportTo('ui', function() {
     13   /**
     14    * @constructor
     15    */
     16   var SpyShell = tvcm.ui.define('x-spy-shell');
     17 
     18   SpyShell.prototype = {
     19     __proto__: HTMLUnknownElement.prototype,
     20 
     21     decorate: function(socketURL) {
     22       var node = tvcm.instantiateTemplate('#x-spy-shell-template');
     23       this.appendChild(node);
     24 
     25       this.socketURL_ = socketURL;
     26       this.conn_ = undefined;
     27 
     28       this.statusEl_ = this.querySelector('#status');
     29       this.statusEl_.textContent = 'Not connected';
     30 
     31       this.spy_ = this.querySelector('x-spy');
     32       tvcm.ui.decorate(this.spy_, ui.Spy);
     33 
     34       this.openConnection_();
     35     },
     36 
     37     get socketURL() {
     38       return this.socketURL_;
     39     },
     40 
     41     openConnection_: function() {
     42       if (!(this.conn_ == undefined ||
     43             this.conn_.readyState === undefined ||
     44             conn.readyState > 1)) {
     45         return;
     46       }
     47 
     48       this.conn_ = new WebSocket(this.socketURL_);
     49       this.conn_.onopen = function() {
     50         this.statusEl_.textContent = 'connected at ' + this.socketURL_;
     51         this.spy_.connection = this.conn_;
     52       }.bind(this);
     53 
     54       this.conn_.onclose = function(event) {
     55         this.statusEl_.textContent = 'connection closed';
     56         this.spy_.connection = undefined;
     57       }.bind(this);
     58       this.conn_.onerror = function(event) {
     59         this.statusEl_.innerHTML = 'got error';
     60       }.bind(this);
     61     }
     62 
     63   };
     64 
     65   return {
     66     SpyShell: SpyShell
     67   };
     68 });
     69