1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
|
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview Tools is a main class that wires all components of the
* DevTools frontend together. It is also responsible for overriding existing
* WebInspector functionality while it is getting upstreamed into WebCore.
*/
goog.provide('devtools.Tools');
goog.require('devtools.DebuggerAgent');
goog.require('devtools.DomAgent');
goog.require('devtools.NetAgent');
devtools.ToolsAgent = function() {
RemoteToolsAgent.DidEvaluateJavaScript = devtools.Callback.processCallback;
RemoteToolsAgent.DidExecuteUtilityFunction =
devtools.Callback.processCallback;
RemoteToolsAgent.UpdateFocusedNode =
goog.bind(this.updateFocusedNode, this);
RemoteToolsAgent.FrameNavigate =
goog.bind(this.frameNavigate, this);
RemoteToolsAgent.AddMessageToConsole =
goog.bind(this.addMessageToConsole, this);
this.debuggerAgent_ = new devtools.DebuggerAgent();
this.domAgent_ = new devtools.DomAgent();
this.netAgent_ = new devtools.NetAgent();
};
/**
* Resets tools agent to its initial state.
*/
devtools.ToolsAgent.prototype.reset = function() {
this.domAgent_.reset();
this.netAgent_.reset();
this.debuggerAgent_.reset();
this.domAgent_.getDocumentElementAsync();
this.debuggerAgent_.requestScripts();
};
/**
* @param {string} script Script exression to be evaluated in the context of the
* inspected page.
* @param {function(string):undefined} callback Function to call with the
* result.
*/
devtools.ToolsAgent.prototype.evaluateJavaScript = function(script, callback) {
var callbackId = devtools.Callback.wrap(callback);
RemoteToolsAgent.EvaluateJavaScript(callbackId, script);
};
/**
* Returns all properties of the given node.
* @param {devtools.DomNode} node Node to get properties for.
* @param {Array.<string>} path Path to the object.
* @param {number} protoDepth Depth to the exact proto level.
* @param {function(string):undefined} callback Function to call with the
* result.
*/
devtools.ToolsAgent.prototype.getNodePropertiesAsync = function(nodeId,
path, protoDepth, callback) {
var callbackId = devtools.Callback.wrap(callback);
RemoteToolsAgent.ExecuteUtilityFunction(callbackId,
'devtools$$getProperties', nodeId,
goog.json.serialize([path, protoDepth]));
};
/**
* Returns prototype chain for a given node.
* @param {devtools.DomNode} node Node to get prototypes for.
* @param {Function} callback.
*/
devtools.ToolsAgent.prototype.getNodePrototypesAsync = function(nodeId,
callback) {
var callbackId = devtools.Callback.wrap(callback);
RemoteToolsAgent.ExecuteUtilityFunction(callbackId,
'devtools$$getPrototypes', nodeId, '');
};
/**
* @return {devtools.DebuggerAgent} Debugger agent instance.
*/
devtools.ToolsAgent.prototype.getDebuggerAgent = function() {
return this.debuggerAgent_;
};
/**
* DomAgent accessor.
* @return {devtools.DomAgent} Dom agent instance.
*/
devtools.ToolsAgent.prototype.getDomAgent = function() {
return this.domAgent_;
};
/**
* NetAgent accessor.
* @return {devtools.NetAgent} Net agent instance.
*/
devtools.ToolsAgent.prototype.getNetAgent = function() {
return this.netAgent_;
};
/**
* @see tools_agent.h
*/
devtools.ToolsAgent.prototype.updateFocusedNode = function(nodeId) {
var node = this.domAgent_.getNodeForId(nodeId);
WebInspector.updateFocusedNode(node);
};
/**
* @param {string} url Url frame navigated to.
* @param {bool} topLevel True iff top level navigation occurred.
* @see tools_agent.h
*/
devtools.ToolsAgent.prototype.frameNavigate = function(url, topLevel) {
if (topLevel) {
this.reset();
WebInspector.reset();
}
};
/**
* @param {string} message Message to add.
* @param {string} source Source url.
* @param {number} line Line number in source.
* @see tools_agent.h
*/
devtools.ToolsAgent.prototype.addMessageToConsole = function(message, source,
line) {
var console = WebInspector.console;
if (console) {
console.addMessage(new WebInspector.ConsoleMessage(
"", undefined, line, source, undefined, 1, message));
}
};
/**
* Evaluates js expression.
* @param {string} expr
*/
devtools.ToolsAgent.prototype.evaluate = function(expr) {
RemoteToolsAgent.evaluate(expr);
};
/**
* Prints string to the inspector console or shows alert if the console doesn't
* exist.
* @param {string} text
*/
function debugPrint(text) {
var console = WebInspector.console;
if (console) {
console.addMessage(new WebInspector.ConsoleMessage(
"", undefined, 1, "", undefined, 1, text));
} else {
alert(text);
}
}
/**
* Global instance of the tools agent.
* @type {devtools.ToolsAgent}
*/
devtools.tools = null;
var context = {}; // Used by WebCore's inspector routines.
///////////////////////////////////////////////////////////////////////////////
// Here and below are overrides to existing WebInspector methods only.
// TODO(pfeldman): Patch WebCore and upstream changes.
var oldLoaded = WebInspector.loaded;
WebInspector.loaded = function() {
devtools.tools = new devtools.ToolsAgent();
devtools.tools.reset();
Preferences.ignoreWhitespace = false;
oldLoaded.call(this);
DevToolsHost.loaded();
};
var webkitUpdateChildren =
WebInspector.ElementsTreeElement.prototype.updateChildren;
/**
* @override
*/
WebInspector.ElementsTreeElement.prototype.updateChildren = function() {
var self = this;
devtools.tools.getDomAgent().getChildNodesAsync(this.representedObject,
function() {
webkitUpdateChildren.call(self);
});
};
/**
* @override
*/
WebInspector.ElementsPanel.prototype.performSearch = function(query) {
this.searchCanceled();
var self = this;
devtools.tools.getDomAgent().performSearch(query, function(node) {
var treeElement = self.treeOutline.findTreeElement(node);
if (treeElement)
treeElement.highlighted = true;
});
};
/**
* @override
*/
WebInspector.ElementsPanel.prototype.searchCanceled = function() {
var self = this;
devtools.tools.getDomAgent().searchCanceled(function(node) {
var treeElement = self.treeOutline.findTreeElement(node);
if (treeElement)
treeElement.highlighted = false;
});
};
/**
* @override
*/
WebInspector.ElementsPanel.prototype.jumpToNextSearchResult = function() {
};
/**
* @override
*/
WebInspector.ElementsPanel.prototype.jumpToPreviousSearchResult = function() {
};
/**
* @override
*/
WebInspector.Console.prototype._evalInInspectedWindow = function(expr) {
return devtools.tools.evaluate(expr);
};
/**
* @override
*/
WebInspector.ElementsPanel.prototype.updateStyles = function(forceUpdate) {
var stylesSidebarPane = this.sidebarPanes.styles;
if (!stylesSidebarPane.expanded || !stylesSidebarPane.needsUpdate)
return;
var node = this.focusedDOMNode;
if (node && node.nodeType === Node.TEXT_NODE && node.parentNode)
node = node.parentNode;
if (node && node.nodeType == Node.ELEMENT_NODE) {
var callback = function() {
stylesSidebarPane.update(node, null, forceUpdate);
stylesSidebarPane.needsUpdate = false;
};
devtools.tools.getDomAgent().getNodeStylesAsync(node,
!Preferences.showUserAgentStyles, callback);
} else {
stylesSidebarPane.update(null, null, forceUpdate);
stylesSidebarPane.needsUpdate = false;
}
};
/**
* @override
*/
WebInspector.PropertiesSidebarPane.prototype.update = function(object) {
var body = this.bodyElement;
body.removeChildren();
this.sections = [];
if (!object) {
return;
}
var self = this;
devtools.tools.getNodePrototypesAsync(object.id_, function(json) {
// Get array of prototype user-friendly names.
var prototypes = goog.json.parse(json);
for (var i = 0; i < prototypes.length; ++i) {
var prototype = {};
prototype.id_ = object.id_;
prototype.protoDepth_ = i;
var section = new WebInspector.SidebarObjectPropertiesSection(prototype,
prototypes[i]);
self.sections.push(section);
body.appendChild(section.element);
}
});
};
/**
* Our implementation of ObjectPropertiesSection for Elements tab.
* @constructor
*/
WebInspector.SidebarObjectPropertiesSection = function(object, title) {
WebInspector.ObjectPropertiesSection.call(this, object, title,
null /* subtitle */, null /* emptyPlaceholder */,
null /* ignoreHasOwnProperty */, null /* extraProperties */,
WebInspector.SidebarObjectPropertyTreeElement /* treeElementConstructor */
);
};
goog.inherits(WebInspector.SidebarObjectPropertiesSection,
WebInspector.ObjectPropertiesSection);
/**
* @override
*/
WebInspector.SidebarObjectPropertiesSection.prototype.onpopulate = function() {
var nodeId = this.object.id_;
var protoDepth = this.object.protoDepth_;
var path = [];
devtools.tools.getNodePropertiesAsync(nodeId, path, protoDepth,
goog.partial(WebInspector.didGetNodePropertiesAsync_,
this.propertiesTreeOutline,
this.treeElementConstructor,
nodeId,
path));
};
/**
* Our implementation of ObjectPropertyTreeElement for Elements tab.
* @constructor
*/
WebInspector.SidebarObjectPropertyTreeElement = function(parentObject,
propertyName) {
WebInspector.ObjectPropertyTreeElement.call(this, parentObject, propertyName);
};
goog.inherits(WebInspector.SidebarObjectPropertyTreeElement,
WebInspector.ObjectPropertyTreeElement);
/**
* @override
*/
WebInspector.SidebarObjectPropertyTreeElement.prototype.onpopulate =
function() {
var nodeId = this.parentObject.devtools$$nodeId_;
var path = this.parentObject.devtools$$path_.slice(0);
path.push(this.propertyName);
devtools.tools.getNodePropertiesAsync(nodeId, path, -1, goog.partial(
WebInspector.didGetNodePropertiesAsync_,
this,
this.treeOutline.section.treeElementConstructor,
nodeId, path));
};
/**
* This override is necessary for starting highlighting after the resource
* was added into the frame.
* @override
*/
WebInspector.SourceView.prototype.setupSourceFrameIfNeeded = function() {
if (!this._frameNeedsSetup) {
return;
}
this.attach();
var self = this;
var identifier = this.resource.identifier;
var element = this.sourceFrame.element;
var netAgent = devtools.tools.getNetAgent();
netAgent.getResourceContentAsync(identifier, function(source) {
var resource = netAgent.getResource(identifier);
if (InspectorController.addSourceToFrame(resource.mimeType, source,
element)) {
delete self._frameNeedsSetup;
if (resource.type === WebInspector.Resource.Type.Script) {
self.sourceFrame.addEventListener("syntax highlighting complete",
self._syntaxHighlightingComplete, self);
self.sourceFrame.syntaxHighlightJavascript();
}
} else {
self._sourceFrameSetupFinished();
}
});
return true;
};
/**
* Dummy object used during properties inspection.
* @see WebInspector.didGetNodePropertiesAsync_
*/
WebInspector.dummyObject_ = { 'foo' : 'bar' };
/**
* Dummy function used during properties inspection.
* @see WebInspector.didGetNodePropertiesAsync_
*/
WebInspector.dummyFunction_ = function() {};
/**
* Callback function used with the getNodeProperties.
*/
WebInspector.didGetNodePropertiesAsync_ = function(treeOutline, constructor,
nodeId, path, json) {
var props = goog.json.parse(json);
var properties = [];
var obj = {};
obj.devtools$$nodeId_ = nodeId;
obj.devtools$$path_ = path;
for (var i = 0; i < props.length; i += 3) {
var type = props[i];
var name = props[i + 1];
var value = props[i + 2];
properties.push(name);
if (type == 'object') {
// fake object is going to be replaced on expand.
obj[name] = WebInspector.dummyObject_;
} else if (type == 'function') {
// fake function is going to be replaced on expand.
obj[name] = WebInspector.dummyFunction_;
} else {
obj[name] = value;
}
}
properties.sort();
treeOutline.removeChildren();
for (var i = 0; i < properties.length; ++i) {
var propertyName = properties[i];
treeOutline.appendChild(new constructor(obj, propertyName));
}
};
/**
* Replace WebKit method with our own implementation to use our call stack
* representation. Original method uses Object.prototype.toString.call to
* learn if scope object is a JSActivation which doesn't work in Chrome.
*/
WebInspector.ScopeChainSidebarPane.prototype.update = function(callFrame) {
this.bodyElement.removeChildren();
this.sections = [];
this.callFrame = callFrame;
if (!callFrame) {
var infoElement = document.createElement("div");
infoElement.className = "info";
infoElement.textContent = WebInspector.UIString("Not Paused");
this.bodyElement.appendChild(infoElement);
return;
}
if (!callFrame._expandedProperties) {
callFrame._expandedProperties = {};
}
var scopeObject = callFrame.localScope;
var title = WebInspector.UIString("Local");
var subtitle = Object.describe(scopeObject, true);
var emptyPlaceholder = null;
var extraProperties = null;
var section = new WebInspector.ObjectPropertiesSection(scopeObject, title,
subtitle, emptyPlaceholder, true, extraProperties,
WebInspector.ScopeVariableTreeElement);
section.editInSelectedCallFrameWhenPaused = true;
section.pane = this;
section.expanded = true;
this.sections.push(section);
this.bodyElement.appendChild(section.element);
};
|