Chromium Code Reviews| 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 observatory; | 5 part of observatory; |
| 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; | 11 static const int InvalidIsolateId = 0; |
| 12 static const String defaultHash = '#/isolates/'; | 12 static const String defaultHash = '#/isolates/'; |
| 13 static final RegExp currentIsolateMatcher = new RegExp(r"#/isolates/\d+/"); | 13 static final RegExp currentIsolateMatcher = new RegExp(r"#/isolates/\d+"); |
| 14 | 14 |
| 15 ObservatoryApplication _application; | 15 ObservatoryApplication _application; |
| 16 ObservatoryApplication get application => _application; | 16 ObservatoryApplication get application => _application; |
| 17 | 17 |
| 18 @observable String currentHash = ''; | 18 @observable String currentHash = ''; |
| 19 @observable Uri currentHashUri; | 19 @observable Uri currentHashUri; |
| 20 void init() { | 20 void init() { |
| 21 window.onHashChange.listen((event) { | 21 window.onHashChange.listen((event) { |
| 22 if (setDefaultHash()) { | 22 if (setDefaultHash()) { |
| 23 // We just triggered another onHashChange event. | 23 // We just triggered another onHashChange event. |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 41 return null; | 41 return null; |
| 42 } | 42 } |
| 43 return m.input.substring(m.start, m.end); | 43 return m.input.substring(m.start, m.end); |
| 44 } | 44 } |
| 45 | 45 |
| 46 /// Predicate, does the current URL have a current isolate ID in it? | 46 /// Predicate, does the current URL have a current isolate ID in it? |
| 47 bool get hasCurrentIsolate { | 47 bool get hasCurrentIsolate { |
| 48 return currentIsolateAnchorPrefix() != null; | 48 return currentIsolateAnchorPrefix() != null; |
| 49 } | 49 } |
| 50 | 50 |
| 51 bool get isScriptLink { | |
| 52 String type = currentHashUri.queryParameters['type']; | |
| 53 return type == 'Script'; | |
| 54 } | |
| 55 | |
| 56 String get scriptName { | |
| 57 return Uri.decodeQueryComponent(currentHashUri.queryParameters['name']); | |
| 58 } | |
| 59 | |
| 60 /// Extract the current isolate id as an integer. Returns [InvalidIsolateId] | 51 /// Extract the current isolate id as an integer. Returns [InvalidIsolateId] |
| 61 /// if none is present in window.location. | 52 /// if none is present in window.location. |
| 62 int currentIsolateId() { | 53 String currentIsolateId() { |
| 63 String isolatePrefix = currentIsolateAnchorPrefix(); | 54 var prefix = currentIsolateAnchorPrefix(); |
| 64 if (isolatePrefix == null) { | 55 if (prefix == null) { |
| 65 return InvalidIsolateId; | 56 return ''; |
| 66 } | 57 } |
|
turnidge
2013/12/19 20:39:29
Comment for why we are skipping these first 2 char
Cutch
2013/12/19 21:53:58
Done.
| |
| 67 String id = isolatePrefix.split("/")[2]; | 58 return prefix.substring(2); |
| 68 return int.parse(id); | |
| 69 } | 59 } |
| 70 | 60 |
| 71 /// 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. |
| 72 /// Return false otherwise. | 62 /// Return false otherwise. |
| 73 bool setDefaultHash() { | 63 bool setDefaultHash() { |
| 74 currentHash = window.location.hash; | 64 currentHash = window.location.hash; |
| 75 if (currentHash == '' || currentHash == '#') { | 65 if (currentHash == '' || currentHash == '#') { |
| 76 window.location.hash = defaultHash; | 66 window.location.hash = defaultHash; |
| 77 return true; | 67 return true; |
| 78 } | 68 } |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 92 /// Create a request for [l] on the current isolate. | 82 /// Create a request for [l] on the current isolate. |
| 93 @observable | 83 @observable |
| 94 String currentIsolateRelativeLink(String l) { | 84 String currentIsolateRelativeLink(String l) { |
| 95 var isolateId = currentIsolateId(); | 85 var isolateId = currentIsolateId(); |
| 96 if (isolateId == LocationManager.InvalidIsolateId) { | 86 if (isolateId == LocationManager.InvalidIsolateId) { |
| 97 return defaultHash; | 87 return defaultHash; |
| 98 } | 88 } |
| 99 return relativeLink(isolateId, l); | 89 return relativeLink(isolateId, l); |
| 100 } | 90 } |
| 101 | 91 |
| 102 /// Create a request for [objectId] on the current isolate. | 92 /// Create a request for [scriptURL] on the current isolate. |
| 103 @observable | 93 @observable |
| 104 String currentIsolateObjectLink(int objectId) { | 94 String currentIsolateScriptLink(String scriptURL) { |
| 105 var isolateId = currentIsolateId(); | 95 var encoded = Uri.encodeComponent(scriptURL); |
| 106 if (isolateId == LocationManager.InvalidIsolateId) { | 96 return currentIsolateRelativeLink('scripts/$encoded'); |
| 107 return defaultHash; | |
| 108 } | |
| 109 return objectLink(isolateId, objectId); | |
| 110 } | |
| 111 | |
| 112 /// Create a request for [cid] on the current isolate. | |
| 113 @observable | |
| 114 String currentIsolateClassLink(int cid) { | |
| 115 var isolateId = currentIsolateId(); | |
| 116 if (isolateId == LocationManager.InvalidIsolateId) { | |
| 117 return defaultHash; | |
| 118 } | |
| 119 return classLink(isolateId, cid); | |
| 120 } | |
| 121 | |
| 122 /// Create a request for the script [objectId] with script [name]. | |
| 123 @observable | |
| 124 String currentIsolateScriptLink(int objectId, String name) { | |
| 125 var isolateId = currentIsolateId(); | |
| 126 if (isolateId == LocationManager.InvalidIsolateId) { | |
| 127 return defaultHash; | |
| 128 } | |
| 129 return scriptLink(isolateId, objectId, name); | |
| 130 } | 97 } |
| 131 | 98 |
| 132 /// Create a request for [l] on [isolateId]. | 99 /// Create a request for [l] on [isolateId]. |
| 133 @observable | 100 @observable |
| 134 String relativeLink(int isolateId, String l) { | 101 String relativeLink(String isolateId, String l) { |
| 135 return '#/isolates/$isolateId/$l'; | 102 return '#/$isolateId/$l'; |
|
turnidge
2013/12/19 20:39:29
Things are simpler now.
| |
| 136 } | |
| 137 | |
| 138 /// Create a request for [objectId] on [isolateId]. | |
| 139 @observable | |
| 140 String objectLink(int isolateId, int objectId) { | |
| 141 return '#/isolates/$isolateId/objects/$objectId'; | |
| 142 } | |
| 143 | |
| 144 /// Create a request for [cid] on [isolateId]. | |
| 145 @observable | |
| 146 String classLink(int isolateId, int cid) { | |
| 147 return '#/isolates/$isolateId/classes/$cid'; | |
| 148 } | |
| 149 | |
| 150 @observable | |
| 151 /// Create a request for the script [objectId] with script [url]. | |
| 152 String scriptLink(int isolateId, int objectId, String name) { | |
| 153 String encodedName = Uri.encodeQueryComponent(name); | |
| 154 return '#/isolates/$isolateId/objects/$objectId' | |
| 155 '?type=Script&name=$encodedName'; | |
| 156 } | 103 } |
| 157 } | 104 } |
| OLD | NEW |