| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of app; | 5 part of app; |
| 6 | 6 |
| 7 /// The LocationManager class observes and parses the hash ('#') portion of the | 7 /// The LocationManager class observes and parses the hash ('#') portion of the |
| 8 /// URL in window.location. The text after the '#' is used as the request | 8 /// URL in window.location. The text after the '#' is used as the request |
| 9 /// string for the VM service. | 9 /// string for the VM service. |
| 10 class LocationManager extends Observable { | 10 class LocationManager extends Observable { |
| 11 static const int InvalidIsolateId = 0; | |
| 12 static const String defaultHash = '#/isolates/'; | 11 static const String defaultHash = '#/isolates/'; |
| 13 static final RegExp currentIsolateMatcher = new RegExp(r"#/isolates/\d+"); | 12 static final RegExp _currentIsolateMatcher = new RegExp(r'#/isolates/\d+'); |
| 13 static final RegExp _currentObjectMatcher = new RegExp(r'#/isolates/\d+/'); |
| 14 ObservatoryApplication _app; |
| 15 @observable String currentHash = ''; |
| 14 | 16 |
| 15 ObservatoryApplication _app; | |
| 16 ObservatoryApplication get app => _app; | |
| 17 | |
| 18 @observable bool profile = false; | |
| 19 @observable String currentHash = ''; | |
| 20 @observable Uri currentHashUri; | |
| 21 void init() { | 17 void init() { |
| 22 window.onHashChange.listen((event) { | 18 window.onHashChange.listen((event) { |
| 23 if (setDefaultHash()) { | 19 if (setDefaultHash()) { |
| 24 // We just triggered another onHashChange event. | 20 // We just triggered another onHashChange event. |
| 25 return; | 21 return; |
| 26 } | 22 } |
| 27 notifyPropertyChange(#hasCurrentIsolate, !hasCurrentIsolate, | |
| 28 hasCurrentIsolate); | |
| 29 // Request the current anchor. | 23 // Request the current anchor. |
| 30 requestCurrentHash(); | 24 requestCurrentHash(); |
| 31 }); | 25 }); |
| 32 | 26 |
| 33 if (!setDefaultHash()) { | 27 if (!setDefaultHash()) { |
| 34 // An anchor was already present, trigger a request. | 28 // An anchor was already present, trigger a request. |
| 35 requestCurrentHash(); | 29 requestCurrentHash(); |
| 36 } | 30 } |
| 37 } | 31 } |
| 38 | 32 |
| 39 | 33 /// Parses the location entry and extracts the id for the object |
| 40 /// Returns the current isolate prefix, i.e. '#/isolates/XX/' if one | 34 /// inside the current isolate. |
| 41 /// is present and null otherwise. | 35 String currentIsolateObjectId() { |
| 42 String currentIsolateAnchorPrefix() { | 36 Match m = _currentObjectMatcher.matchAsPrefix(currentHash); |
| 43 Match m = currentIsolateMatcher.matchAsPrefix(currentHash); | |
| 44 if (m == null) { | |
| 45 return null; | |
| 46 } | |
| 47 return m.input.substring(m.start, m.end); | |
| 48 } | |
| 49 | |
| 50 /// Returns the current object id. | |
| 51 String currentObjectId() { | |
| 52 Match m = currentIsolateMatcher.matchAsPrefix(currentHash); | |
| 53 if (m == null) { | 37 if (m == null) { |
| 54 return null; | 38 return null; |
| 55 } | 39 } |
| 56 return m.input.substring(m.end); | 40 return m.input.substring(m.end); |
| 57 } | 41 } |
| 58 | 42 |
| 59 /// Predicate, does the current URL have a current isolate ID in it? | 43 /// Parses the location entry and extracts the id for the current isolate. |
| 60 @observable bool get hasCurrentIsolate { | |
| 61 return currentIsolateAnchorPrefix() != null; | |
| 62 } | |
| 63 | |
| 64 /// Extract the current isolate id as an integer. Returns [InvalidIsolateId] | |
| 65 /// if none is present in window.location. | |
| 66 String currentIsolateId() { | 44 String currentIsolateId() { |
| 67 var prefix = currentIsolateAnchorPrefix(); | 45 Match m = _currentIsolateMatcher.matchAsPrefix(currentHash); |
| 68 if (prefix == null) { | 46 if (m == null) { |
| 69 return ''; | 47 return ''; |
| 70 } | 48 } |
| 71 // Chop off the '/#'. | 49 return m.input.substring(2, m.end); |
| 72 return prefix.substring(2); | |
| 73 } | 50 } |
| 74 | 51 |
| 75 /// Returns the current isolate. | 52 /// Returns the current isolate. |
| 76 @observable Isolate currentIsolate() { | 53 @observable Isolate currentIsolate() { |
| 77 var id = currentIsolateId(); | 54 var id = currentIsolateId(); |
| 78 if (id == '') { | 55 if (id == '') { |
| 79 return null; | 56 return null; |
| 80 } | 57 } |
| 81 return app.vm.getIsolate(id); | 58 return _app.vm.isolates.getIsolate(id); |
| 82 } | 59 } |
| 83 | 60 |
| 84 /// If no anchor is set, set the default anchor and return true. | 61 /// If no anchor is set, set the default anchor and return true. |
| 85 /// Return false otherwise. | 62 /// Return false otherwise. |
| 86 bool setDefaultHash() { | 63 bool setDefaultHash() { |
| 87 currentHash = window.location.hash; | 64 currentHash = window.location.hash; |
| 88 if (currentHash == '' || currentHash == '#') { | 65 if (currentHash == '' || currentHash == '#') { |
| 89 window.location.hash = defaultHash; | 66 window.location.hash = defaultHash; |
| 90 return true; | 67 return true; |
| 91 } | 68 } |
| 92 return false; | 69 return false; |
| 93 } | 70 } |
| 94 | 71 |
| 95 /// Take the current request string from window.location and submit the | 72 void _setResponse(ServiceObject serviceObject) { |
| 96 /// request to the request manager. | 73 _app.response = serviceObject; |
| 74 } |
| 75 |
| 76 /// Refresh the service object reference in the location entry. |
| 97 void requestCurrentHash() { | 77 void requestCurrentHash() { |
| 98 currentHash = window.location.hash; | 78 currentHash = window.location.hash; |
| 99 // Chomp off the # | 79 _app.isolate = currentIsolate(); |
| 100 String requestUrl = currentHash.substring(1); | 80 if (_app.isolate == null) { |
| 101 app.isolate = currentIsolate(); | 81 // No current isolate, refresh the isolate list. |
| 102 currentHashUri = Uri.parse(requestUrl); | 82 _app.vm.isolates.reload().then(_setResponse); |
| 103 if (app.isolate == null) { | |
| 104 Logger.root.warning('Refreshing isolates.'); | |
| 105 app.vm.refreshIsolates(); | |
| 106 return; | 83 return; |
| 107 } | 84 } |
| 108 var objectId = currentObjectId(); | 85 // Have a current isolate, request object. |
| 109 Logger.root.info('Asking ${app.isolate.id} for ${objectId}'); | 86 var objectId = currentIsolateObjectId(); |
| 110 app.isolate.get(objectId).then((so) { | 87 _app.isolate.get(objectId).then(_setResponse); |
| 111 app.response = so; | |
| 112 }); | |
| 113 } | 88 } |
| 114 } | 89 } |
| OLD | NEW |