devices.js revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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 5devices = new (function() { 6 7this.backends_ = []; // ['Android', 'Linux'] 8this.devices_ = {}; // 'Android/a1b2' -> {.backend, .name, .id} 9this.selDeviceUri_ = null; 10 11this.onDomReady_ = function() { 12 $('#refresh-devices').on('click', this.refresh.bind(this)); 13 $('#devices').on('change', this.onDeviceSelectionChange_.bind(this)); 14 this.refresh(); 15}; 16 17this.getSelectedURI = function() { 18 return this.selDeviceUri_; 19}; 20 21this.getAllBackends = function() { 22 // Returns a list of the registered backends, e.g., ['Android', 'Linux']. 23 return this.backends_; 24}; 25 26this.getAllDevices = function() { 27 // Returns a list of devices [{backend:'Android', name:'N7', id:'1234'}]. 28 return Object.keys(this.devices_).map(function(k) { 29 return this.devices_[k]; 30 }, this); 31}; 32 33this.refresh = function() { 34 webservice.ajaxRequest('/devices', this.onDevicesAjaxResponse_.bind(this)); 35 webservice.ajaxRequest('/backends', this.onBackendsAjaxResponse_.bind(this)); 36}; 37 38this.onBackendsAjaxResponse_ = function(data) { 39 if (!data || !data.length) 40 { 41 rootUi.showDialog('No backends detected! Memory Inspector looks terribly' + 42 ' broken. Please file a bug'); 43 } 44 this.backends_ = data; 45}; 46 47this.onDevicesAjaxResponse_ = function(data) { 48 var devList = $('#devices'); 49 devList.empty(); 50 this.devices_ = {}; 51 data.forEach(function(device) { 52 var deviceUri = device.backend + '/' + device.id; 53 var deviceFullTime = device.backend + ' : ' + 54 device.name + ' [' + device.id + ']'; 55 devList.append($('<option/>').val(deviceUri).text(deviceFullTime)); 56 this.devices_[deviceUri] = device; 57 }, this); 58 59 if (data.length > 0) { 60 this.onDeviceSelectionChange_(); // Start monitoring the first device. 61 } else { 62 rootUi.showDialog('No devices could be detected. Check the settings tab ' + 63 'to ensure that the adb path is properly configured.'); 64 } 65}; 66 67this.onDeviceSelectionChange_ = function() { 68 this.selDeviceUri_ = $('#devices').val(); 69 if (!this.selDeviceUri_) 70 return; 71 72 // Initialize device and start processes / OS stats. 73 webservice.ajaxRequest('/initialize/' + this.selDeviceUri_, 74 this.onDeviceInitializationComplete_.bind(this)); 75}; 76 77this.onDeviceInitializationComplete_ = function() { 78 processes.startPsTable(); 79 processes.startDeviceStats(); 80}; 81 82$(document).ready(this.onDomReady_.bind(this)); 83 84})();