| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 part of app; | |
| 6 | |
| 7 /// The observatory application. Instances of this are created and owned | |
| 8 /// by the observatory_application custom element. | |
| 9 class ObservatoryApplication extends Observable { | |
| 10 static ObservatoryApplication app; | |
| 11 final _pageRegistry = new List<Page>(); | |
| 12 @observable Page currentPage; | |
| 13 @observable final LocationManager locationManager; | |
| 14 VM _vm; | |
| 15 VM get vm => _vm; | |
| 16 set vm(VM vm) { | |
| 17 if (_vm == vm) { | |
| 18 // Do nothing. | |
| 19 return; | |
| 20 } | |
| 21 if (_vm != null) { | |
| 22 // Disconnect from current VM. | |
| 23 notifications.clear(); | |
| 24 _vm.disconnect(); | |
| 25 } | |
| 26 if (vm != null) { | |
| 27 Logger.root.info('Registering new VM callbacks'); | |
| 28 vm.onConnect.then(_vmConnected); | |
| 29 vm.onDisconnect.then(_vmDisconnected); | |
| 30 vm.errors.stream.listen(_onError); | |
| 31 vm.events.stream.listen(_onEvent); | |
| 32 vm.exceptions.stream.listen(_onException); | |
| 33 } | |
| 34 _vm = vm; | |
| 35 } | |
| 36 final TargetManager targets; | |
| 37 @observable Isolate isolate; | |
| 38 @reflectable final ObservatoryApplicationElement rootElement; | |
| 39 | |
| 40 @reflectable ServiceObject lastErrorOrException; | |
| 41 @observable ObservableList<ServiceEvent> notifications = | |
| 42 new ObservableList<ServiceEvent>(); | |
| 43 | |
| 44 void _initOnce(bool chromium) { | |
| 45 assert(app == null); | |
| 46 app = this; | |
| 47 _registerPages(); | |
| 48 locationManager._init(this); | |
| 49 } | |
| 50 | |
| 51 void removePauseEvents(Isolate isolate) { | |
| 52 bool isPauseEvent(var event) { | |
| 53 return (event.eventType == 'IsolateInterrupted' || | |
| 54 event.eventType == 'BreakpointReached' || | |
| 55 event.eventType == 'ExceptionThrown'); | |
| 56 } | |
| 57 | |
| 58 notifications.removeWhere((oldEvent) { | |
| 59 return (oldEvent.isolate == isolate && | |
| 60 isPauseEvent(oldEvent)); | |
| 61 }); | |
| 62 } | |
| 63 | |
| 64 void _onEvent(ServiceEvent event) { | |
| 65 switch(event.eventType) { | |
| 66 case 'IsolateCreated': | |
| 67 // vm.reload(); | |
| 68 break; | |
| 69 | |
| 70 case 'IsolateShutdown': | |
| 71 // TODO(turnidge): Should we show the user isolate shutdown events? | |
| 72 // What if there are hundreds of them? Coalesce multiple | |
| 73 // shutdown events into one notification? | |
| 74 removePauseEvents(event.isolate); | |
| 75 // vm.reload(); | |
| 76 break; | |
| 77 | |
| 78 case 'BreakpointResolved': | |
| 79 event.isolate.reloadBreakpoints(); | |
| 80 break; | |
| 81 | |
| 82 case 'BreakpointReached': | |
| 83 case 'IsolateInterrupted': | |
| 84 case 'ExceptionThrown': | |
| 85 removePauseEvents(event.isolate); | |
| 86 notifications.add(event); | |
| 87 break; | |
| 88 | |
| 89 default: | |
| 90 // Ignore unrecognized events. | |
| 91 Logger.root.severe('Unrecognized event type: ${event.eventType}'); | |
| 92 break; | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 void _registerPages() { | |
| 97 // Register ClassTreePage. | |
| 98 _pageRegistry.add(new ClassTreePage(this)); | |
| 99 _pageRegistry.add(new VMConnectPage(this)); | |
| 100 _pageRegistry.add(new ErrorViewPage(this)); | |
| 101 // Note that ServiceObjectPage must be the last entry in the list as it is | |
| 102 // the catch all. | |
| 103 _pageRegistry.add(new ServiceObjectPage(this)); | |
| 104 } | |
| 105 | |
| 106 void _onError(ServiceError error) { | |
| 107 lastErrorOrException = error; | |
| 108 _visit('error/', null); | |
| 109 } | |
| 110 | |
| 111 void _onException(ServiceException exception) { | |
| 112 lastErrorOrException = exception; | |
| 113 if (exception.kind == 'NetworkException') { | |
| 114 // Got a network exception, visit the vm-connect page. | |
| 115 locationManager.go(locationManager.makeLink('/vm-connect/')); | |
| 116 } else { | |
| 117 _visit('error/', null); | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 void _visit(String url, String args) { | |
| 122 var argsMap; | |
| 123 if (args == null) { | |
| 124 argsMap = {}; | |
| 125 } else { | |
| 126 argsMap = Uri.splitQueryString(args); | |
| 127 } | |
| 128 for (var i = 0; i < _pageRegistry.length; i++) { | |
| 129 var page = _pageRegistry[i]; | |
| 130 if (page.canVisit(url)) { | |
| 131 _installPage(page); | |
| 132 page.visit(url, argsMap); | |
| 133 return; | |
| 134 } | |
| 135 } | |
| 136 throw new FallThroughError(); | |
| 137 } | |
| 138 | |
| 139 /// Set the Observatory application page. | |
| 140 void _installPage(Page page) { | |
| 141 assert(page != null); | |
| 142 if (currentPage == page) { | |
| 143 // Already isntalled. | |
| 144 return; | |
| 145 } | |
| 146 if (currentPage != null) { | |
| 147 Logger.root.info('Uninstalling page: $currentPage'); | |
| 148 currentPage.onUninstall(); | |
| 149 // Clear children. | |
| 150 rootElement.children.clear(); | |
| 151 } | |
| 152 Logger.root.info('Installing page: $page'); | |
| 153 try { | |
| 154 page.onInstall(); | |
| 155 } catch (e) { | |
| 156 Logger.root.severe('Failed to install page: $e'); | |
| 157 } | |
| 158 // Add new page. | |
| 159 rootElement.children.add(page.element); | |
| 160 // Remember page. | |
| 161 currentPage = page; | |
| 162 } | |
| 163 | |
| 164 ObservatoryApplication.devtools(this.rootElement) : | |
| 165 locationManager = new HashLocationManager(), | |
| 166 targets = null { | |
| 167 vm = new PostMessageVM(); | |
| 168 _initOnce(true); | |
| 169 } | |
| 170 | |
| 171 ObservatoryApplication(this.rootElement) : | |
| 172 locationManager = new HashLocationManager(), | |
| 173 targets = new TargetManager() { | |
| 174 vm = new WebSocketVM(targets.defaultTarget); | |
| 175 _initOnce(false); | |
| 176 } | |
| 177 | |
| 178 _vmConnected(VM vm) { | |
| 179 if (vm is WebSocketVM) { | |
| 180 targets.add(vm.target); | |
| 181 } | |
| 182 } | |
| 183 | |
| 184 _vmDisconnected(VM vm) { | |
| 185 if (this.vm != vm) { | |
| 186 // This disconnect event occured *after* a new VM was installed. | |
| 187 return; | |
| 188 } | |
| 189 this.vm = null; | |
| 190 locationManager.go(locationManager.makeLink('/vm-connect/')); | |
| 191 } | |
| 192 } | |
| OLD | NEW |