Chromium Code Reviews| Index: runtime/bin/vmservice/client/observatory/packages/observatory/src/observatory/anchor.dart |
| diff --git a/runtime/bin/vmservice/client/observatory/packages/observatory/src/observatory/anchor.dart b/runtime/bin/vmservice/client/observatory/packages/observatory/src/observatory/anchor.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..28613df7caf88f88058fbbca74e967d0f93c2b70 |
| --- /dev/null |
| +++ b/runtime/bin/vmservice/client/observatory/packages/observatory/src/observatory/anchor.dart |
| @@ -0,0 +1,119 @@ |
| +part of observatory; |
| + |
| +/// The Anchor class observes and parses the hash ('#') portion of the URL |
| +/// in window.location. The text after the '#' is used as the request string |
| +/// for the VM service. |
| +class Anchor extends Object with ObservableMixin { |
|
justinfagnani
2013/09/26 01:46:24
I'd rename this to something like Links or Uris
Cutch
2013/09/26 20:34:21
Done.
|
| + static const int InvalidIsolateId = 0; |
| + static const String defaultAnchor = '#/isolates/'; |
| + static final RegExp currentIsolateMatcher = new RegExp(r"#/isolates/\d+/"); |
| + |
| + ObservatoryApplication _application; |
| + ObservatoryApplication get application => _application; |
| + |
| + @observable String currentAnchor = ''; |
| + |
| + void init() { |
| + window.onHashChange.listen((event) { |
| + if (setDefaultAnchor()) { |
| + // We just triggered another onHashChange event. |
| + return; |
| + } |
| + // Request the current anchor. |
| + requestCurrentAnchor(); |
| + }); |
| + |
| + if (!setDefaultAnchor()) { |
| + // An anchor was already present, trigger a request. |
| + requestCurrentAnchor(); |
| + } |
| + } |
| + |
| + /// Returns the current isolate prefix, i.e. '#/isolates/XX/' if one |
| + /// is present and null otherwise. |
| + String currentIsolateAnchorPrefix() { |
| + Match m = currentIsolateMatcher.matchAsPrefix(currentAnchor); |
| + if (m == null) { |
| + return null; |
| + } |
| + return m.input.substring(m.start, m.end); |
| + } |
| + |
| + /// Predicate, does the current URL have a current isolate ID in it? |
| + bool get hasCurrentIsolate { |
| + return currentIsolateAnchorPrefix() != null; |
| + } |
| + |
| + /// Extract the current isolate id as an integer. Returns [InvalidIsolateId] |
| + /// if none is present in window.location. |
| + int currentIsolateId() { |
| + String isolatePrefix = currentIsolateAnchorPrefix(); |
| + if (isolatePrefix == null) { |
| + return InvalidIsolateId; |
| + } |
| + String id = isolatePrefix.split("/")[2]; |
| + return int.parse(id); |
| + } |
| + |
| + /// If no anchor is set, set the default anchor and return true. |
| + /// Return false otherwise. |
| + bool setDefaultAnchor() { |
| + currentAnchor = window.location.hash; |
| + if (currentAnchor == '' || currentAnchor == '#') { |
| + window.location.hash = defaultAnchor; |
| + return true; |
| + } |
| + return false; |
| + } |
| + |
| + /// Take the current request string from window.location and submit the |
| + /// request to the request manager. |
| + void requestCurrentAnchor() { |
| + currentAnchor = window.location.hash; |
| + // Chomp off the # |
| + String requestUrl = currentAnchor.substring(1); |
| + application.requestManager.get(requestUrl); |
| + } |
| + |
| + /// Create a request for [l] on the current isolate. |
| + String currentIsolateRelativeLink(String l) { |
| + var isolateId = currentIsolateId(); |
| + if (isolateId == Anchor.InvalidIsolateId) { |
| + return defaultAnchor; |
| + } |
| + return relativeLink(isolateId, l); |
| + } |
| + |
| + /// Create a request for [objectId] on the current isolate. |
| + String currentIsolateObjectLink(int objectId) { |
| + var isolateId = currentIsolateId(); |
| + if (isolateId == Anchor.InvalidIsolateId) { |
| + return defaultAnchor; |
| + } |
| + return objectLink(isolateId, objectId); |
| + } |
| + |
| + /// Create a request for [cid] on the current isolate. |
| + String currentIsolateClassLink(int cid) { |
| + var isolateId = currentIsolateId(); |
| + if (isolateId == Anchor.InvalidIsolateId) { |
| + return defaultAnchor; |
| + } |
| + return classLink(isolateId, cid); |
| + } |
| + |
| + /// Create a request for [l] on [isolateId]. |
| + String relativeLink(int isolateId, String l) { |
| + return '#/isolates/$isolateId/$l'; |
| + } |
| + |
| + /// Create a request for [objectId] on [isolateId]. |
| + String objectLink(int isolateId, int objectId) { |
| + return '#/isolates/$isolateId/objects/$objectId'; |
| + } |
| + |
| + /// Create a request for [cid] on [isolateId]. |
| + String classLink(int isolateId, int cid) { |
| + return '#/isolates/$isolateId/classes/$cid'; |
| + } |
| +} |