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

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

Issue 192443004: Complete the switch to ServiceObject (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 9 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 service;
6
7 /// A [ServiceObject] is an object known to the VM service and is tied
8 /// to an owning [Isolate].
9 abstract class ServiceObject extends Observable {
10 Isolate _isolate;
turnidge 2014/03/10 21:03:28 consider blank lines between decls.
Cutch 2014/03/11 03:17:48 Done.
11 /// Owning isolate.
12 Isolate get isolate => _isolate;
13 /// Owning vm.
14 VM get vm => _isolate.vm;
15 /// The complete service url of this object.
16 String get link => isolate.relativeLink(_id);
17 // The complete service url of this object with a '#/' prefix.
18 String get hashLink => isolate.relativeHashLink(_id);
19 String _id;
20 /// The id of this object.
21 String get id => _id;
22 String _serviceType;
23 /// The service type of this object.
24 String get serviceType => _serviceType;
25 bool _ref;
26
27 @observable String name;
28 @observable String vmName;
29
30 /// Refresh [this]. Returns a future which completes to [this] or
31 /// a [ServiceError].
32 Future<ServiceObject> refresh() {
33 assert(isolate != null);
34 if (id == '') {
35 // Errors don't have ids.
36 assert(serviceType == 'Error');
turnidge 2014/03/10 21:03:28 We want to consider whether we want to assert on b
Cutch 2014/03/11 03:17:48 I'm convinced that it's a bad idea. But, we need a
37 return new Future.value(this);
38 }
39 return isolate.vm.fetchMap(link).then(update);
40 }
41
42 /// Update [this] using [m] as a source. [m] can be a reference.
43 ServiceObject update(ObservableMap m) {
44 // Assert that m is a service map.
45 assert(ServiceObject.isServiceMap(m));
46 if ((m['type'] == 'Error') && (_serviceType != 'Error')) {
47 // Got an unexpected error. Don't update the object.
48 return _upgradeToServiceObject(vm, isolate, m);
49 }
50 // Assert that the id hasn't changed.
51 assert(m['id'] == _id);
52 // Assert that the type hasn't changed.
53 assert(ServiceObject.unreffedType(m['type']) == _serviceType);
54 _update(m);
55 return this;
56 }
57
58 // update internal state from [map]. [map] can be a reference.
59 void _update(ObservableMap map);
60
61 /// Returns true if [this] has only been partially initialized via
62 /// a reference. See [deref].
63 bool isRef() => _ref;
64
65 /// If [this] was created from a reference, request the full object
66 /// from the service by calling [refresh]. Else, return [this].
67 Future<ServiceObject> deref() {
turnidge 2014/03/10 21:03:28 I suggested this elsewhere but will write it here
Cutch 2014/03/11 03:17:48 Done.
68 if (!_ref) {
69 // Not a reference.
70 return new Future.value(this);
71 }
72 // Call refresh which will fill in the entire object.
73 return new Future(refresh);
turnidge 2014/03/10 21:03:28 Can this be just return refresh();
Cutch 2014/03/11 03:17:48 Done.
74 }
75
76 void _created() {
77 var refNotice = _ref ? ' Created from reference.' : '';
78 Logger.root.info('Created ServiceObject for \'${_id}\' with type '
79 '\'${_serviceType}\'.' + refNotice);
80 }
81
82 ServiceObject(this._isolate, this._id, this._serviceType) {
83 _ref = isRefType(_serviceType);
84 _serviceType = unreffedType(_serviceType);
85 _created();
86 }
87
88 ServiceObject.fromMap(this._isolate, ObservableMap m) {
89 assert(isServiceMap(m));
90 _id = m['id'];
91 _ref = isRefType(m['type']);
92 _serviceType = unreffedType(m['type']);
93 _created();
94 update(m);
95 }
96
97 /// Returns true if [map] is a service map. i.e. it has the following keys:
98 /// 'id' and a 'type'.
99 static bool isServiceMap(ObservableMap m) {
100 return (m != null) && (m['id'] != null) && (m['type'] != null);
101 }
102
103 /// Returns true if [type] is a reference type. i.e. it begins with an
104 /// '@' character.
105 static bool isRefType(String type) {
turnidge 2014/03/10 21:03:28 Could change to "isRef" instead of "isRefType". N
Cutch 2014/03/11 03:17:48 Disagree. "isRef" is an instance method on the Ser
106 return type.startsWith('@');
107 }
108
109 /// Returns the unreffed version of [type].
110 static String unreffedType(String type) {
turnidge 2014/03/10 21:03:28 Consider renaming to "stripRef"
Cutch 2014/03/11 03:17:48 Done.
111 if (!isRefType(type)) {
112 return type;
113 }
114 // Strip off the '@' character.
115 return type.substring(1);
116 }
117 }
118
119
120 /// Upgrades response ([m]) from [vm] and [isolate] to a [ServiceObject].
121 ServiceObject _upgradeToServiceObject(VM vm, Isolate isolate, ObservableMap m) {
122 assert(ServiceObject.isServiceMap(m));
123 var type = ServiceObject.unreffedType(m['type']);
124 if (type == 'Error') {
125 return new ServiceError.fromMap(isolate, m);
126 }
127 if (isolate == null) {
128 // Only an isolate list should have a null isolate.
129 assert(type == 'IsolateList');
130 return new IsolateList.fromMap(vm, m);
131 }
132 if (isolate.scripts.cachesType(type)) {
turnidge 2014/03/10 21:03:28 The data-driven thing here doesn't buy much. I wo
Cutch 2014/03/11 03:17:48 I've reworked this.
133 return isolate.scripts.cachedOrUpgradeAndAdd(m);
134 }
135 if (isolate.codes.cachesType(type)) {
136 return isolate.codes.cachedOrUpgradeAndAdd(m);
137 }
138 if (isolate.classes.cachesType(type)) {
139 return isolate.classes.cachedOrUpgradeAndAdd(m);
140 }
141 return new ServiceMap.fromMap(isolate, m);
142 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698