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