| 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 observatory; | |
| 6 | |
| 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 | |
| 9 /// string for the VM service. | |
| 10 class LocationManager extends Observable { | |
| 11 static const int InvalidIsolateId = 0; | |
| 12 static const String defaultHash = '#/isolates/'; | |
| 13 static final RegExp currentIsolateMatcher = new RegExp(r"#/isolates/\d+"); | |
| 14 static final RegExp currentIsolateProfileMatcher = | |
| 15 new RegExp(r'#/isolates/\d+/profile'); | |
| 16 | |
| 17 ObservatoryApplication _application; | |
| 18 ObservatoryApplication get application => _application; | |
| 19 | |
| 20 @observable bool profile = false; | |
| 21 @observable String currentHash = ''; | |
| 22 @observable Uri currentHashUri; | |
| 23 void init() { | |
| 24 window.onHashChange.listen((event) { | |
| 25 if (setDefaultHash()) { | |
| 26 // We just triggered another onHashChange event. | |
| 27 return; | |
| 28 } | |
| 29 notifyPropertyChange(#hasCurrentIsolate, !hasCurrentIsolate, | |
| 30 hasCurrentIsolate); | |
| 31 // Request the current anchor. | |
| 32 requestCurrentHash(); | |
| 33 }); | |
| 34 | |
| 35 if (!setDefaultHash()) { | |
| 36 // An anchor was already present, trigger a request. | |
| 37 requestCurrentHash(); | |
| 38 } | |
| 39 } | |
| 40 | |
| 41 | |
| 42 /// Returns the current isolate prefix, i.e. '#/isolates/XX/' if one | |
| 43 /// is present and null otherwise. | |
| 44 String currentIsolateAnchorPrefix() { | |
| 45 Match m = currentIsolateMatcher.matchAsPrefix(currentHash); | |
| 46 if (m == null) { | |
| 47 return null; | |
| 48 } | |
| 49 return m.input.substring(m.start, m.end); | |
| 50 } | |
| 51 | |
| 52 /// Predicate, does the current URL have a current isolate ID in it? | |
| 53 @observable bool get hasCurrentIsolate { | |
| 54 return currentIsolateAnchorPrefix() != null; | |
| 55 } | |
| 56 | |
| 57 /// Extract the current isolate id as an integer. Returns [InvalidIsolateId] | |
| 58 /// if none is present in window.location. | |
| 59 String currentIsolateId() { | |
| 60 var prefix = currentIsolateAnchorPrefix(); | |
| 61 if (prefix == null) { | |
| 62 return ''; | |
| 63 } | |
| 64 // Chop off the '/#'. | |
| 65 return prefix.substring(2); | |
| 66 } | |
| 67 | |
| 68 @observable Isolate currentIsolate() { | |
| 69 var id = currentIsolateId(); | |
| 70 if (id == '') { | |
| 71 return null; | |
| 72 } | |
| 73 return _application.isolateManager.getIsolate(id); | |
| 74 } | |
| 75 | |
| 76 /// If no anchor is set, set the default anchor and return true. | |
| 77 /// Return false otherwise. | |
| 78 bool setDefaultHash() { | |
| 79 currentHash = window.location.hash; | |
| 80 if (currentHash == '' || currentHash == '#') { | |
| 81 window.location.hash = defaultHash; | |
| 82 return true; | |
| 83 } | |
| 84 return false; | |
| 85 } | |
| 86 | |
| 87 /// Take the current request string from window.location and submit the | |
| 88 /// request to the request manager. | |
| 89 void requestCurrentHash() { | |
| 90 currentHash = window.location.hash; | |
| 91 // Chomp off the # | |
| 92 String requestUrl = currentHash.substring(1); | |
| 93 currentHashUri = Uri.parse(requestUrl); | |
| 94 if (currentIsolateProfileMatcher.hasMatch(currentHash)) { | |
| 95 // We do not automatically fetch profile data. | |
| 96 profile = true; | |
| 97 } else { | |
| 98 application.requestManager.get(requestUrl); | |
| 99 profile = false; | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 /// Create a request for [l] on the current isolate. | |
| 104 @observable | |
| 105 String currentIsolateRelativeLink(String l) { | |
| 106 var isolateId = currentIsolateId(); | |
| 107 if (isolateId == LocationManager.InvalidIsolateId) { | |
| 108 return defaultHash; | |
| 109 } | |
| 110 return relativeLink(isolateId, l); | |
| 111 } | |
| 112 | |
| 113 /// Create a request for [scriptURL] on the current isolate. | |
| 114 @observable | |
| 115 String currentIsolateScriptLink(String scriptURL) { | |
| 116 var encoded = Uri.encodeComponent(scriptURL); | |
| 117 return currentIsolateRelativeLink('scripts/$encoded'); | |
| 118 } | |
| 119 | |
| 120 /// Create a request for [l] on [isolateId]. | |
| 121 @observable | |
| 122 String relativeLink(String isolateId, String l) { | |
| 123 return '#/$isolateId/$l'; | |
| 124 } | |
| 125 | |
| 126 /// Create a request for [l] on [isolateId]. | |
| 127 @observable | |
| 128 String absoluteLink(String l) { | |
| 129 return '#/$l'; | |
| 130 } | |
| 131 } | |
| OLD | NEW |