OLD | NEW |
| (Empty) |
1 // Copyright (c) 2014, 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 app; | |
6 | |
7 class ChromiumTargetLister { | |
8 /// Fetch the list of chromium [NetworkVMTargets]. | |
9 static Future<List<WebSocketVMTarget>> fetch(String networkAddress) { | |
10 if (networkAddress == null) { | |
11 return new Future.error(null); | |
12 } | |
13 var encoded = Uri.encodeComponent(networkAddress); | |
14 var url = '/crdptargets/$encoded'; | |
15 return HttpRequest.getString(url).then((String responseText) { | |
16 var list = JSON.decode(responseText); | |
17 if (list == null) { | |
18 return list; | |
19 } | |
20 for (var i = 0; i < list.length; i++) { | |
21 list[i] = new WebSocketVMTarget.fromMap(list[i]); | |
22 } | |
23 return list; | |
24 }).catchError((e) { | |
25 // An error occured while getting the list of Chrome targets. | |
26 // We eagerly request the list of targets, meaning this error can occur | |
27 // regularly. By catching it and dropping it, we avoid spamming errors | |
28 // on the console. | |
29 }); | |
30 } | |
31 } | |
32 | |
33 class TargetManager extends Observable { | |
34 static const _historyKey = 'history'; | |
35 final SettingsGroup _settings = new SettingsGroup('targetManager'); | |
36 final List history = new ObservableList(); | |
37 WebSocketVMTarget defaultTarget; | |
38 | |
39 String _networkAddressOfDefaultTarget() { | |
40 if (Utils.runningInJavaScript()) { | |
41 // We are running as JavaScript, use the same host that Observatory has | |
42 // been loaded from. | |
43 return 'ws://${window.location.host}/ws'; | |
44 } else { | |
45 // Otherwise, assume we are running from Dart Editor and want to connect | |
46 // to the default host. | |
47 return 'ws://localhost:8181/ws'; | |
48 } | |
49 } | |
50 TargetManager() { | |
51 _restore(); | |
52 // Add a default standalone VM target. | |
53 defaultTarget = findOrMake(_networkAddressOfDefaultTarget()); | |
54 assert(defaultTarget != null); | |
55 add(defaultTarget); | |
56 } | |
57 | |
58 void clearHistory() { | |
59 history.clear(); | |
60 _store(); | |
61 } | |
62 | |
63 WebSocketVMTarget findOrMake(String networkAddress) { | |
64 var target; | |
65 target = _find(networkAddress); | |
66 if (target != null) { | |
67 return target; | |
68 } | |
69 target = new WebSocketVMTarget(networkAddress); | |
70 return target; | |
71 } | |
72 | |
73 /// Find by networkAddress. | |
74 WebSocketVMTarget _find(String networkAddress) { | |
75 var r; | |
76 history.forEach((item) { | |
77 if ((item.networkAddress == networkAddress) && (item.chrome == false)) { | |
78 r = item; | |
79 } | |
80 }); | |
81 return r; | |
82 } | |
83 | |
84 void add(WebSocketVMTarget item) { | |
85 if (item.chrome) { | |
86 // We don't store chrome tabs. | |
87 return; | |
88 } | |
89 if (history.contains(item)) { | |
90 return; | |
91 } | |
92 // Not inserting duplicates. | |
93 assert(_find(item.networkAddress) == null); | |
94 history.add(item); | |
95 _sort(); | |
96 _store(); | |
97 } | |
98 | |
99 void remove(WebSocketVMTarget target) { | |
100 history.remove(target); | |
101 _sort(); | |
102 _store(); | |
103 } | |
104 | |
105 void _sort() { | |
106 this.history.sort((WebSocketVMTarget a, WebSocketVMTarget b) { | |
107 return b.lastConnectionTime.compareTo(a.lastConnectionTime); | |
108 }); | |
109 } | |
110 | |
111 /// After making a change, update settings. | |
112 void _store() { | |
113 _sort(); | |
114 _settings.set(_historyKey, history); | |
115 } | |
116 | |
117 /// Read settings from data store. | |
118 void _restore() { | |
119 this.history.clear(); | |
120 var loaded = _settings.get(_historyKey); | |
121 if (loaded == null) { | |
122 return; | |
123 } | |
124 for (var i = 0; i < loaded.length; i++) { | |
125 loaded[i] = new WebSocketVMTarget.fromMap(loaded[i]); | |
126 } | |
127 this.history.addAll(loaded); | |
128 _sort(); | |
129 } | |
130 } | |
OLD | NEW |