Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(773)

Side by Side Diff: runtime/bin/vmservice/client/lib/src/app/target_manager.dart

Issue 335463008: Allow Observatory to run as a hosted web service (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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<NetworkVMTarget>> 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 NetworkVMTarget.fromMap(list[i]);
22 }
23 return list;
24 }).catchError((e) {
25 //
turnidge 2014/07/01 00:27:47 Fill in this comment?
Cutch 2014/07/01 17:14:51 Done.
26 });
27 }
28 }
29
30 class TargetManager extends Observable {
31 static const _historyKey = 'history';
32 final SettingsGroup _settings = new SettingsGroup('targetManager');
33 final List history = new ObservableList();
34 NetworkVMTarget defaultTarget;
35
36 String _networkAddressOfDefaultTarget() {
37 if (Utils.runningInJavaScript()) {
38 // We are running as JavaScript, use the same host that Observatory has
39 // been loaded from.
40 return 'ws://${window.location.host}/ws';
41 } else {
42 // Otherwise, assume we are running from Dart Editor and want to connect
43 // to the default host.
44 return 'ws://localhost:8181/ws';
turnidge 2014/07/01 00:27:47 This code seems familiar. Is it cloned elsewhere?
Cutch 2014/07/01 17:14:51 It moved here.
45 }
46 }
47 TargetManager() {
48 _restore();
49 // Add a default standalone VM target.
50 defaultTarget = findOrMake(_networkAddressOfDefaultTarget());
51 assert(defaultTarget != null);
52 add(defaultTarget);
53 }
54
55 void clearHistory() {
56 history.clear();
57 _store();
58 }
59
60 NetworkVMTarget findOrMake(String networkAddress) {
61 var target;
62 target = _find(networkAddress);
63 if (target != null) {
64 return target;
65 }
66 target = new NetworkVMTarget(networkAddress);
67 return target;
68 }
69
70 /// Find by networkAddress.
71 NetworkVMTarget _find(String networkAddress) {
72 var r;
73 history.forEach((item) {
74 if ((item.networkAddress == networkAddress) && (item.chrome == false)) {
75 r = item;
76 }
77 });
78 return r;
79 }
80
81 void add(NetworkVMTarget item) {
82 if (item.chrome) {
83 // We don't store chrome tabs.
84 return;
85 }
86 if (history.contains(item)) {
87 return;
88 }
89 // Not inserting duplicates.
90 assert(_find(item.networkAddress) == null);
91 history.add(item);
92 _sort();
93 _store();
94 }
95
96 void remove(NetworkVMTarget target) {
97 history.remove(target);
98 _sort();
99 _store();
100 }
101
102 void _sort() {
103 this.history.sort((NetworkVMTarget a, NetworkVMTarget b) {
104 return b.lastConnectionTime.compareTo(a.lastConnectionTime);
105 });
106 }
107
108 /// After making a change, update settings.
109 void _store() {
110 _sort();
111 _settings.set(_historyKey, history);
112 }
113
114 /// Read settings from data store.
115 void _restore() {
116 this.history.clear();
117 var loaded = _settings.get(_historyKey);
118 if (loaded == null) {
119 return;
120 }
121 for (var i = 0; i < loaded.length; i++) {
122 loaded[i] = new NetworkVMTarget.fromMap(loaded[i]);
123 }
124 this.history.addAll(loaded);
125 _sort();
126 }
127 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698