Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: chrome/browser/resources/inspect/inspect.js

Issue 2248133002: [DevTools] Add UI for adding remote targets (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/resources/inspect/inspect.html ('k') | chrome/browser/ui/webui/inspect_ui.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 var MIN_VERSION_TAB_CLOSE = 25; 5 var MIN_VERSION_TAB_CLOSE = 25;
6 var MIN_VERSION_TARGET_ID = 26; 6 var MIN_VERSION_TARGET_ID = 26;
7 var MIN_VERSION_NEW_TAB = 29; 7 var MIN_VERSION_NEW_TAB = 29;
8 var MIN_VERSION_TAB_ACTIVATE = 30; 8 var MIN_VERSION_TAB_ACTIVATE = 30;
9 var WEBRTC_SERIAL = 'WEBRTC'; 9 var WEBRTC_SERIAL = 'WEBRTC';
10 10
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 var hash = window.location.hash.slice(1).toLowerCase(); 67 var hash = window.location.hash.slice(1).toLowerCase();
68 if (!selectTab(hash)) 68 if (!selectTab(hash))
69 selectTab('devices'); 69 selectTab('devices');
70 } 70 }
71 71
72 /** 72 /**
73 * @param {string} id Tab id. 73 * @param {string} id Tab id.
74 * @return {boolean} True if successful. 74 * @return {boolean} True if successful.
75 */ 75 */
76 function selectTab(id) { 76 function selectTab(id) {
77 closePortForwardingConfig(); 77 closeConfigDialog();
78 78
79 var tabContents = document.querySelectorAll('#content > div'); 79 var tabContents = document.querySelectorAll('#content > div');
80 var tabHeaders = $('navigation').querySelectorAll('.tab-header'); 80 var tabHeaders = $('navigation').querySelectorAll('.tab-header');
81 var found = false; 81 var found = false;
82 for (var i = 0; i != tabContents.length; i++) { 82 for (var i = 0; i != tabContents.length; i++) {
83 var tabContent = tabContents[i]; 83 var tabContent = tabContents[i];
84 var tabHeader = tabHeaders[i]; 84 var tabHeader = tabHeaders[i];
85 if (tabContent.id == id) { 85 if (tabContent.id == id) {
86 tabContent.classList.add('selected'); 86 tabContent.classList.add('selected');
87 tabHeader.classList.add('selected'); 87 tabHeader.classList.add('selected');
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 var browserUser = browserSection.querySelector('.browser-user'); 167 var browserUser = browserSection.querySelector('.browser-user');
168 if (browserUser) 168 if (browserUser)
169 browserUser.hidden = hasSingleUser; 169 browserUser.hidden = hasSingleUser;
170 }); 170 });
171 } 171 }
172 172
173 function populateRemoteTargets(devices) { 173 function populateRemoteTargets(devices) {
174 if (!devices) 174 if (!devices)
175 return; 175 return;
176 176
177 if ($('port-forwarding-config').open) { 177 if ($('config-dialog').open) {
178 window.holdDevices = devices; 178 window.holdDevices = devices;
179 return; 179 return;
180 } 180 }
181 181
182 function browserCompare(a, b) { 182 function browserCompare(a, b) {
183 if (a.adbBrowserName != b.adbBrowserName) 183 if (a.adbBrowserName != b.adbBrowserName)
184 return a.adbBrowserName < b.adbBrowserName; 184 return a.adbBrowserName < b.adbBrowserName;
185 if (a.adbBrowserVersion != b.adbBrowserVersion) 185 if (a.adbBrowserVersion != b.adbBrowserVersion)
186 return a.adbBrowserVersion < b.adbBrowserVersion; 186 return a.adbBrowserVersion < b.adbBrowserVersion;
187 return a.id < b.id; 187 return a.id < b.id;
(...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after
600 } 600 }
601 601
602 602
603 function initSettings() { 603 function initSettings() {
604 $('discover-usb-devices-enable').addEventListener('change', 604 $('discover-usb-devices-enable').addEventListener('change',
605 enableDiscoverUsbDevices); 605 enableDiscoverUsbDevices);
606 606
607 $('port-forwarding-enable').addEventListener('change', enablePortForwarding); 607 $('port-forwarding-enable').addEventListener('change', enablePortForwarding);
608 $('port-forwarding-config-open').addEventListener( 608 $('port-forwarding-config-open').addEventListener(
609 'click', openPortForwardingConfig); 609 'click', openPortForwardingConfig);
610 $('port-forwarding-config-close').addEventListener( 610 $('target-discovery-config-open').addEventListener(
611 'click', closePortForwardingConfig); 611 'click', openTargetsConfig);
612 $('port-forwarding-config-done').addEventListener( 612 $('config-dialog-close').addEventListener('click', function() {
613 'click', commitPortForwardingConfig.bind(null, true)); 613 $('config-dialog').commit(true);
eostroukhov 2016/08/17 17:32:46 I changed the behavior for consistency with the di
614 });
614 } 615 }
615 616
616 function enableDiscoverUsbDevices(event) { 617 function enableDiscoverUsbDevices(event) {
617 sendCommand('set-discover-usb-devices-enabled', event.target.checked); 618 sendCommand('set-discover-usb-devices-enabled', event.target.checked);
618 } 619 }
619 620
620 function enablePortForwarding(event) { 621 function enablePortForwarding(event) {
621 sendCommand('set-port-forwarding-enabled', event.target.checked); 622 sendCommand('set-port-forwarding-enabled', event.target.checked);
622 } 623 }
623 624
624 function handleKey(event) { 625 function handleKey(event) {
625 switch (event.keyCode) { 626 switch (event.keyCode) {
626 case 13: // Enter 627 case 13: // Enter
628 var dialog = $('config-dialog');
627 if (event.target.nodeName == 'INPUT') { 629 if (event.target.nodeName == 'INPUT') {
628 var line = event.target.parentNode; 630 var line = event.target.parentNode;
629 if (!line.classList.contains('fresh') || 631 if (!line.classList.contains('fresh') ||
630 line.classList.contains('empty')) { 632 line.classList.contains('empty')) {
631 commitPortForwardingConfig(true); 633 dialog.commit(true);
632 } else { 634 } else {
633 commitFreshLineIfValid(true /* select new line */); 635 commitFreshLineIfValid(true /* select new line */);
634 commitPortForwardingConfig(false); 636 dialog.commit(false);
635 } 637 }
636 } else { 638 } else {
637 commitPortForwardingConfig(true); 639 dialog.commit(true);
638 } 640 }
639 break; 641 break;
640 } 642 }
641 } 643 }
642 644
643 function openPortForwardingConfig() { 645 function openConfigDialog(dialogClass, commitHandler, lineFactory, data) {
644 loadPortForwardingConfig(window.portForwardingConfig); 646 function commitDialog(shouldClose) {
647 if (shouldClose)
648 closeConfigDialog();
649 commitFreshLineIfValid();
650 commitHandler();
651 }
645 652
646 $('port-forwarding-config').showModal(); 653 var dialog = $('config-dialog');
654 if (dialog.open)
655 return;
656
657 dialog.className = dialogClass;
658 dialog.classList.add('config');
659
647 document.addEventListener('keyup', handleKey); 660 document.addEventListener('keyup', handleKey);
648 $('port-forwarding-config').onclose = function() { 661 dialog.commit = commitDialog;
649 commitPortForwardingConfig(true); 662 dialog.onclose = commitDialog.bind(null, true);
650 }; 663 $('button-done').onclick = commitDialog.bind(null, true);
651 664
652 var freshPort = document.querySelector('.fresh .port'); 665 var list = $('config-dialog').querySelector('.list');
653 if (freshPort) 666 list.textContent = '';
654 freshPort.focus(); 667
668 list.createRow = appendRow.bind(null, list, lineFactory);
669 for (var key in data)
670 list.createRow(key, data[key]);
671 list.createRow(null, null);
672
673 dialog.showModal();
674 var defaultFocus = dialog.querySelector('.fresh .preselected');
675 if (defaultFocus)
676 defaultFocus.focus();
655 else 677 else
656 $('port-forwarding-config-done').focus(); 678 doneButton.focus();
679
657 } 680 }
658 681
659 function closePortForwardingConfig() { 682 function openPortForwardingConfig() {
660 if (!$('port-forwarding-config').open) 683 function createPortForwardingConfigLine(port, location) {
684 var line = document.createElement('div');
685 line.className = 'port-forwarding-pair config-list-row';
686
687 var portInput = createConfigField(port, 'port preselected',
688 'Port', validatePort);
689 line.appendChild(portInput);
690
691 var locationInput = createConfigField(
692 location, 'location', 'IP address and port', validateLocation);
693 locationInput.classList.add('primary');
694 line.appendChild(locationInput);
695 return line;
696 }
697
698 function commitPortForwardingConfig(closeConfig) {
lushnikov 2016/08/18 20:42:14 what's this closeConfig?
eostroukhov 2016/08/18 21:40:41 Not used anymore. Thanks!
699 var config = {};
700 filterList(['.port', '.location'], function(port, location) {
701 config[port] = location;
702 });
703 sendCommand('set-port-forwarding-config', config);
704 }
705
706 openConfigDialog('port-forwarding',
707 commitPortForwardingConfig,
708 createPortForwardingConfigLine,
709 window.portForwardingConfig);
710 }
711
712 function openTargetsConfig() {
713 function createTargetDiscoveryConfigLine(index, targetDiscovery) {
714 var line = document.createElement('div');
715 line.className = 'target-discovery-line config-list-row';
716
717 var locationInput = createConfigField(
718 targetDiscovery, 'location preselected', 'IP address and port',
719 validateLocation);
720 locationInput.classList.add('primary');
721 line.appendChild(locationInput);
722 return line;
723 }
724
725 function commitTargetDiscoveryConfig(closeConfig) {
726 var entries = [];
727 filterList(['.location'], function(location) {
728 entries.push(location);
729 });
730 sendCommand('set-target-discovery-config', entries);
731 }
732
733 openConfigDialog('target-discovery',
734 commitTargetDiscoveryConfig,
735 createTargetDiscoveryConfigLine,
736 window.targetDiscoveryConfig);
737 }
738
739 function closeConfigDialog() {
740 var element = $('config-dialog');
741 if (!element.open)
661 return; 742 return;
662 743 element.onclose = null;
lushnikov 2016/08/18 20:42:14 don't you want to save dialog here as well? (like
eostroukhov 2016/08/18 21:40:41 Actually, selectTab can no longer be invoked since
663 $('port-forwarding-config').onclose = null; 744 element.close();
664 $('port-forwarding-config').close();
665 document.removeEventListener('keyup', handleKey); 745 document.removeEventListener('keyup', handleKey);
666 746
667 if (window.holdDevices) { 747 if (window.holdDevices) {
668 populateRemoteTargets(window.holdDevices); 748 populateRemoteTargets(window.holdDevices);
669 delete window.holdDevices; 749 delete window.holdDevices;
670 } 750 }
671 } 751 }
672 752
673 function loadPortForwardingConfig(config) { 753 function filterList(fieldSelectors, callback) {
674 var list = $('port-forwarding-config-list'); 754 var lines = $('config-dialog').querySelectorAll('.config-list-row');
675 list.textContent = '';
676 for (var port in config)
677 list.appendChild(createConfigLine(port, config[port]));
678 list.appendChild(createEmptyConfigLine());
679 }
680
681 function commitPortForwardingConfig(closeConfig) {
682 if (closeConfig)
683 closePortForwardingConfig();
684
685 commitFreshLineIfValid();
686 var lines = document.querySelectorAll('.port-forwarding-pair');
687 var config = {}; 755 var config = {};
lushnikov 2016/08/18 20:42:14 unused
eostroukhov 2016/08/18 21:40:41 Thanks.
688 for (var i = 0; i != lines.length; i++) { 756 for (var i = 0; i != lines.length; i++) {
689 var line = lines[i]; 757 var line = lines[i];
690 var portInput = line.querySelector('.port'); 758 var values = [];
691 var locationInput = line.querySelector('.location'); 759 for (var selector of fieldSelectors) {
692 760 var input = line.querySelector(selector);
693 var port = portInput.classList.contains('invalid') ? 761 var value = input.classList.contains('invalid') ?
694 portInput.lastValidValue : 762 input.lastValidValue :
695 portInput.value; 763 input.value;
696 764 if (!value)
697 var location = locationInput.classList.contains('invalid') ? 765 break;
698 locationInput.lastValidValue : 766 values.push(value);
699 locationInput.value; 767 }
700 768 if (values.length == fieldSelectors.length)
701 if (port && location) 769 callback.apply(null, values);
702 config[port] = location;
703 } 770 }
704 sendCommand('set-port-forwarding-config', config);
705 } 771 }
706 772
707 function updateDiscoverUsbDevicesEnabled(enabled) { 773 function updateDiscoverUsbDevicesEnabled(enabled) {
708 var checkbox = $('discover-usb-devices-enable'); 774 var checkbox = $('discover-usb-devices-enable');
709 checkbox.checked = !!enabled; 775 checkbox.checked = !!enabled;
710 checkbox.disabled = false; 776 checkbox.disabled = false;
711 } 777 }
712 778
713 function updatePortForwardingEnabled(enabled) { 779 function updatePortForwardingEnabled(enabled) {
714 var checkbox = $('port-forwarding-enable'); 780 var checkbox = $('port-forwarding-enable');
715 checkbox.checked = !!enabled; 781 checkbox.checked = !!enabled;
716 checkbox.disabled = false; 782 checkbox.disabled = false;
717 } 783 }
718 784
719 function updatePortForwardingConfig(config) { 785 function updatePortForwardingConfig(config) {
720 window.portForwardingConfig = config; 786 window.portForwardingConfig = config;
721 $('port-forwarding-config-open').disabled = !config; 787 $('port-forwarding-config-open').disabled = !config;
722 } 788 }
723 789
724 function createConfigLine(port, location) { 790 function updateTargetDiscoveryConfig(config) {
725 var line = document.createElement('div'); 791 window.targetDiscoveryConfig = config;
726 line.className = 'port-forwarding-pair'; 792 $('target-discovery-config-open').disabled = !config;
793 }
727 794
728 var portInput = createConfigField(port, 'port', 'Port', validatePort); 795 function appendRow(list, lineFactory, key, value) {
729 line.appendChild(portInput); 796 var line = lineFactory(key, value);
730 797 line.lastElementChild.addEventListener('keydown', function(e) {
731 var locationInput = createConfigField(
732 location, 'location', 'IP address and port', validateLocation);
733 line.appendChild(locationInput);
734 locationInput.addEventListener('keydown', function(e) {
735 if (e.key == 'Tab' && 798 if (e.key == 'Tab' &&
736 !e.ctrlKey && !e.altKey && !e.shiftKey && !e.metaKey && 799 !e.ctrlKey && !e.altKey && !e.shiftKey && !e.metaKey &&
737 line.classList.contains('fresh') && 800 line.classList.contains('fresh') &&
738 !line.classList.contains('empty')) { 801 !line.classList.contains('empty')) {
739 // Tabbing forward on the fresh line, try create a new empty one. 802 // Tabbing forward on the fresh line, try create a new empty one.
740 if (commitFreshLineIfValid(true)) 803 if (commitFreshLineIfValid(true))
741 e.preventDefault(); 804 e.preventDefault();
742 } 805 }
743 }); 806 });
744 807
745 var lineDelete = document.createElement('div'); 808 var lineDelete = document.createElement('div');
746 lineDelete.className = 'close-button'; 809 lineDelete.className = 'close-button';
747 lineDelete.addEventListener('click', function() { 810 lineDelete.addEventListener('click', function() {
748 var newSelection = line.nextElementSibling; 811 var newSelection = line.nextElementSibling || line.previousElementSimpling;
lushnikov 2016/08/18 20:42:14 typo: line.previousElementSibling
eostroukhov 2016/08/18 21:40:41 Done.
812 selectLine(newSelection, true);
749 line.parentNode.removeChild(line); 813 line.parentNode.removeChild(line);
750 selectLine(newSelection); 814 $('config-dialog').commit(false);
751 commitPortForwardingConfig(false);
752 }); 815 });
753 line.appendChild(lineDelete); 816 line.appendChild(lineDelete);
754 817
755 line.addEventListener('click', selectLine.bind(null, line)); 818 line.addEventListener(
756 line.addEventListener('focus', selectLine.bind(null, line)); 819 'click', selectLine.bind(null, line, true));
757 820 line.addEventListener(
821 'focus', selectLine.bind(null, line, true));
758 checkEmptyLine(line); 822 checkEmptyLine(line);
759 823
760 return line; 824 if (!key && !value)
825 line.classList.add('fresh');
826
827 return list.appendChild(line);
761 } 828 }
762 829
763 function validatePort(input) { 830 function validatePort(input) {
764 var match = input.value.match(/^(\d+)$/); 831 var match = input.value.match(/^(\d+)$/);
765 if (!match) 832 if (!match)
766 return false; 833 return false;
767 var port = parseInt(match[1]); 834 var port = parseInt(match[1]);
768 if (port < 1024 || 65535 < port) 835 if (port < 1024 || 65535 < port)
769 return false; 836 return false;
770 837
771 var inputs = document.querySelectorAll('input.port:not(.invalid)'); 838 var inputs = document.querySelectorAll('input.port:not(.invalid)');
772 for (var i = 0; i != inputs.length; ++i) { 839 for (var i = 0; i != inputs.length; ++i) {
773 if (inputs[i] == input) 840 if (inputs[i] == input)
774 break; 841 break;
775 if (parseInt(inputs[i].value) == port) 842 if (parseInt(inputs[i].value) == port)
776 return false; 843 return false;
777 } 844 }
778 return true; 845 return true;
779 } 846 }
780 847
781 function validateLocation(input) { 848 function validateLocation(input) {
782 var match = input.value.match(/^([a-zA-Z0-9\.\-_]+):(\d+)$/); 849 var match = input.value.match(/^([a-zA-Z0-9\.\-_]+):(\d+)$/);
783 if (!match) 850 if (!match)
784 return false; 851 return false;
785 var port = parseInt(match[2]); 852 var port = parseInt(match[2]);
786 return port <= 65535; 853 return port <= 65535;
787 } 854 }
788 855
789 function createEmptyConfigLine() {
790 var line = createConfigLine('', '');
791 line.classList.add('fresh');
792 return line;
793 }
794
795 function createConfigField(value, className, hint, validate) { 856 function createConfigField(value, className, hint, validate) {
796 var input = document.createElement('input'); 857 var input = document.createElement('input');
797 input.className = className; 858 input.className = className;
798 input.type = 'text'; 859 input.type = 'text';
799 input.placeholder = hint; 860 input.placeholder = hint;
800 input.value = value; 861 input.value = value || '';
801 input.lastValidValue = value; 862 input.lastValidValue = value || '';
802 863
803 function checkInput() { 864 function checkInput() {
804 if (validate(input)) 865 if (validate(input))
805 input.classList.remove('invalid'); 866 input.classList.remove('invalid');
806 else 867 else
807 input.classList.add('invalid'); 868 input.classList.add('invalid');
808 if (input.parentNode) 869 if (input.parentNode)
809 checkEmptyLine(input.parentNode); 870 checkEmptyLine(input.parentNode);
810 } 871 }
811 checkInput(); 872 checkInput();
(...skipping 17 matching lines...) Expand all
829 for (var i = 0; i != inputs.length; i++) { 890 for (var i = 0; i != inputs.length; i++) {
830 if (inputs[i].value != '') 891 if (inputs[i].value != '')
831 empty = false; 892 empty = false;
832 } 893 }
833 if (empty) 894 if (empty)
834 line.classList.add('empty'); 895 line.classList.add('empty');
835 else 896 else
836 line.classList.remove('empty'); 897 line.classList.remove('empty');
837 } 898 }
838 899
839 function selectLine(line) { 900 function selectLine(line, opt_focusInput) {
840 if (line.classList.contains('selected')) 901 if (line.classList.contains('selected'))
841 return; 902 return;
842 unselectLine(); 903 var selected =
904 line.parentElement && line.parentElement.querySelector('.selected');
905 if (selected)
906 selected.classList.remove('selected');
843 line.classList.add('selected'); 907 line.classList.add('selected');
844 } 908 if (opt_focusInput) {
845 909 var el = line.querySelector('.preselected');
846 function unselectLine() { 910 if (el) {
847 var line = document.querySelector('.port-forwarding-pair.selected'); 911 line.firstChild.select();
848 if (!line) 912 line.firstChild.focus();
849 return; 913 }
850 line.classList.remove('selected'); 914 }
851 commitFreshLineIfValid();
852 } 915 }
853 916
854 function commitFreshLineIfValid(opt_selectNew) { 917 function commitFreshLineIfValid(opt_selectNew) {
855 var line = document.querySelector('.port-forwarding-pair.fresh'); 918 var line = $('config-dialog').querySelector('.config-list-row.fresh');
856 if (line.querySelector('.invalid')) 919 if (line.querySelector('.invalid'))
857 return false; 920 return false;
858 line.classList.remove('fresh'); 921 line.classList.remove('fresh');
859 var freshLine = createEmptyConfigLine(); 922 var freshLine = line.parentElement.createRow();
860 line.parentNode.appendChild(freshLine);
861 if (opt_selectNew) 923 if (opt_selectNew)
862 freshLine.querySelector('.port').focus(); 924 freshLine.querySelector('.preselected').focus();
863 return true; 925 return true;
864 } 926 }
865 927
866 function populatePortStatus(devicesStatusMap) { 928 function populatePortStatus(devicesStatusMap) {
867 for (var deviceId in devicesStatusMap) { 929 for (var deviceId in devicesStatusMap) {
868 if (!devicesStatusMap.hasOwnProperty(deviceId)) 930 if (!devicesStatusMap.hasOwnProperty(deviceId))
869 continue; 931 continue;
870 var deviceStatus = devicesStatusMap[deviceId]; 932 var deviceStatus = devicesStatusMap[deviceId];
871 var deviceStatusMap = deviceStatus.ports; 933 var deviceStatusMap = deviceStatus.ports;
872 934
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
930 deviceSection.querySelectorAll('.browser'), clearBrowserPorts); 992 deviceSection.querySelectorAll('.browser'), clearBrowserPorts);
931 } 993 }
932 994
933 Array.prototype.forEach.call( 995 Array.prototype.forEach.call(
934 document.querySelectorAll('.device'), clearPorts); 996 document.querySelectorAll('.device'), clearPorts);
935 } 997 }
936 998
937 document.addEventListener('DOMContentLoaded', onload); 999 document.addEventListener('DOMContentLoaded', onload);
938 1000
939 window.addEventListener('hashchange', onHashChange); 1001 window.addEventListener('hashchange', onHashChange);
OLDNEW
« no previous file with comments | « chrome/browser/resources/inspect/inspect.html ('k') | chrome/browser/ui/webui/inspect_ui.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698