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 String defaultHash = '#/isolates/'; | 11 static const String defaultHash = '#/vm'; |
12 static final RegExp _currentIsolateMatcher = new RegExp(r'#/isolates/\d+'); | 12 |
13 static final RegExp _currentObjectMatcher = new RegExp(r'#/isolates/\d+(/|$)')
; | |
14 ObservatoryApplication _app; | 13 ObservatoryApplication _app; |
15 @observable String currentHash = ''; | 14 @observable String currentHash = ''; |
16 | 15 |
17 void init() { | 16 void init() { |
18 window.onHashChange.listen((event) { | 17 window.onHashChange.listen((event) { |
19 if (setDefaultHash()) { | 18 if (setDefaultHash()) { |
20 // We just triggered another onHashChange event. | 19 // We just triggered another onHashChange event. |
21 return; | 20 return; |
22 } | 21 } |
23 // Request the current anchor. | 22 // Request the current anchor. |
24 requestCurrentHash(); | 23 requestCurrentHash(); |
25 }); | 24 }); |
26 | 25 |
27 if (!setDefaultHash()) { | 26 if (!setDefaultHash()) { |
28 // An anchor was already present, trigger a request. | 27 // An anchor was already present, trigger a request. |
29 requestCurrentHash(); | 28 requestCurrentHash(); |
30 } | 29 } |
31 } | 30 } |
32 | 31 |
33 /// Parses the location entry and extracts the id for the object | |
34 /// inside the current isolate. | |
35 String currentIsolateObjectId() { | |
36 Match m = _currentObjectMatcher.matchAsPrefix(currentHash); | |
37 if (m == null) { | |
38 return null; | |
39 } | |
40 return m.input.substring(m.end); | |
41 } | |
42 | |
43 /// Parses the location entry and extracts the id for the current isolate. | |
44 String currentIsolateId() { | |
45 Match m = _currentIsolateMatcher.matchAsPrefix(currentHash); | |
46 if (m == null) { | |
47 return ''; | |
48 } | |
49 return m.input.substring(2, m.end); | |
50 } | |
51 | |
52 /// Returns the current isolate. | |
53 @observable Isolate currentIsolate() { | |
54 var id = currentIsolateId(); | |
55 if (id == '') { | |
56 return null; | |
57 } | |
58 return _app.vm.isolates.getIsolate(id); | |
59 } | |
60 | |
61 /// If no anchor is set, set the default anchor and return true. | 32 /// If no anchor is set, set the default anchor and return true. |
62 /// Return false otherwise. | 33 /// Return false otherwise. |
63 bool setDefaultHash() { | 34 bool setDefaultHash() { |
64 currentHash = window.location.hash; | 35 currentHash = window.location.hash; |
65 if (currentHash == '' || currentHash == '#') { | 36 if (currentHash == '' || currentHash == '#') { |
66 window.location.hash = defaultHash; | 37 window.location.hash = defaultHash; |
67 return true; | 38 return true; |
68 } | 39 } |
69 return false; | 40 return false; |
70 } | 41 } |
71 | 42 |
72 void _setResponse(ServiceObject serviceObject) { | |
73 _app.response = serviceObject; | |
74 } | |
75 | |
76 /// Refresh the service object reference in the location entry. | 43 /// Refresh the service object reference in the location entry. |
77 void requestCurrentHash() { | 44 void requestCurrentHash() { |
78 currentHash = window.location.hash; | 45 currentHash = window.location.hash; |
79 _app.isolate = currentIsolate(); | 46 assert(currentHash.startsWith('#/')); |
80 if (_app.isolate == null) { | 47 |
81 // No current isolate, refresh the isolate list. | 48 var parts = currentHash.substring(2).split('#'); |
82 _app.vm.isolates.reload().then(_setResponse); | 49 var location = parts[0]; |
83 return; | 50 var args = (parts.length > 1 ? parts[1] : ''); |
| 51 if (parts.length > 2) { |
| 52 Logger.root.warning('Found more than 2 #-characters in $currentHash'); |
84 } | 53 } |
85 // Have a current isolate, request object. | 54 _app.vm.get(currentHash.substring(2)).then((obj) { |
86 var objectId = currentIsolateObjectId(); | 55 _app.response = obj; |
87 _app.isolate.get(objectId).then(_setResponse); | 56 _app.args = args; |
| 57 }); |
88 } | 58 } |
89 } | 59 } |
OLD | NEW |