9c662e80039114708b7ab65bb32a4bf8d95e47be
[WebKit-https.git] / Source / WebCore / inspector / InjectedScriptSource.js
1 /*
2  * Copyright (C) 2007 Apple Inc.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 (function (InjectedScriptHost, inspectedWindow, injectedScriptId) {
30
31 function bind(thisObject, memberFunction)
32 {
33     var func = memberFunction;
34     var args = Array.prototype.slice.call(arguments, 2);
35     function bound()
36     {
37         return func.apply(thisObject, args.concat(Array.prototype.slice.call(arguments, 0)));
38     }
39     bound.toString = function() {
40         return "bound: " + func;
41     };
42     return bound;
43 }
44
45 var InjectedScript = function()
46 {
47     this._lastBoundObjectId = 1;
48     this._idToWrappedObject = {};
49     this._objectGroups = {};
50 }
51
52 InjectedScript.prototype = {
53     wrapObject: function(object, groupName, canAccessInspectedWindow)
54     {
55         if (canAccessInspectedWindow)
56             return this._wrapObject(object, groupName);
57         var result = {};
58         result.type = typeof object;
59         result.description = this._toString(object);
60         return result;
61     },
62
63     inspectNode: function(object)
64     {
65         this._inspect(object);
66     },
67
68     _inspect: function(object)
69     {
70         if (arguments.length === 0)
71             return;
72
73         var objectId = this._wrapObject(object, "", false);
74         var hints = {};
75
76         switch (injectedScript._describe(object)) {
77             case "Database":
78                 var databaseId = InjectedScriptHost.databaseId(object)
79                 if (databaseId)
80                     hints.databaseId = databaseId;
81                 break;
82             case "Storage":
83                 var storageId = InjectedScriptHost.storageId(object)
84                 if (storageId)
85                     hints.domStorageId = storageId;
86                 break;
87         }
88         InjectedScriptHost.inspect(objectId, hints);
89         return object;
90     },
91
92     _wrapObject: function(object, objectGroupName, abbreviate)
93     {
94         try {
95             if (typeof object === "object" || typeof object === "function" || this._isHTMLAllCollection(object)) {
96                 var id = this._lastBoundObjectId++;
97                 this._idToWrappedObject[id] = object;
98                 var objectId = { injectedScriptId: injectedScriptId, id: id };
99     
100                 if (objectGroupName) {
101                     var group = this._objectGroups[objectGroupName];
102                     if (!group) {
103                         group = [];
104                         this._objectGroups[objectGroupName] = group;
105                     }
106                     group.push(id);
107                     objectId.groupName = objectGroupName;
108                 }
109             }
110             return InjectedScript.RemoteObject.fromObject(object, objectId, abbreviate);
111         } catch (e) {
112             return InjectedScript.RemoteObject.fromObject("[ Exception: " + e.toString() + " ]");
113         }
114     },
115
116     _parseObjectId: function(objectId)
117     {
118         return eval("(" + objectId + ")");
119     },
120
121     releaseObjectGroup: function(objectGroupName)
122     {
123         var group = this._objectGroups[objectGroupName];
124         if (!group)
125             return;
126         for (var i = 0; i < group.length; i++)
127             delete this._idToWrappedObject[group[i]];
128         delete this._objectGroups[objectGroupName];
129     },
130
131     dispatch: function(methodName, args)
132     {
133         var argsArray = eval("(" + args + ")");
134         var result = this[methodName].apply(this, argsArray);
135         if (typeof result === "undefined") {
136             inspectedWindow.console.error("Web Inspector error: InjectedScript.%s returns undefined", methodName);
137             result = null;
138         }
139         return result;
140     },
141
142     getProperties: function(objectId, ignoreHasOwnProperty, abbreviate)
143     {
144         var parsedObjectId = this._parseObjectId(objectId);
145         var object = this._objectForId(parsedObjectId);
146
147         if (!this._isDefined(object))
148             return false;
149         var properties = [];
150
151         var propertyNames = ignoreHasOwnProperty ? this._getPropertyNames(object) : Object.getOwnPropertyNames(object);
152         if (!ignoreHasOwnProperty && object.__proto__)
153             propertyNames.push("__proto__");
154     
155         // Go over properties, prepare results.
156         for (var i = 0; i < propertyNames.length; ++i) {
157             var propertyName = propertyNames[i];
158     
159             var property = {};
160             property.name = propertyName + "";
161             var isGetter = object["__lookupGetter__"] && object.__lookupGetter__(propertyName);
162             if (!isGetter) {
163                 try {
164                     property.value = this._wrapObject(object[propertyName], parsedObjectId.groupName, abbreviate);
165                 } catch(e) {
166                     property.value = new InjectedScript.RemoteObject.fromException(e);
167                 }
168             } else {
169                 // FIXME: this should show something like "getter" (bug 16734).
170                 property.value = new InjectedScript.RemoteObject.fromObject("\u2014"); // em dash
171                 property.isGetter = true;
172             }
173             properties.push(property);
174         }
175         return properties;
176     },
177
178     setPropertyValue: function(objectId, propertyName, expression)
179     {
180         var parsedObjectId = this._parseObjectId(objectId);
181         var object = this._objectForId(parsedObjectId);
182         if (!this._isDefined(object))
183             return false;
184     
185         var expressionLength = expression.length;
186         if (!expressionLength) {
187             delete object[propertyName];
188             return !(propertyName in object);
189         }
190     
191         try {
192             // Surround the expression in parenthesis so the result of the eval is the result
193             // of the whole expression not the last potential sub-expression.
194     
195             // There is a regression introduced here: eval is now happening against global object,
196             // not call frame while on a breakpoint.
197             // TODO: bring evaluation against call frame back.
198             var result = inspectedWindow.eval("(" + expression + ")");
199             // Store the result in the property.
200             object[propertyName] = result;
201             return true;
202         } catch(e) {
203             try {
204                 var result = inspectedWindow.eval("\"" + expression.replace(/"/g, "\\\"") + "\"");
205                 object[propertyName] = result;
206                 return true;
207             } catch(e) {
208                 return false;
209             }
210         }
211     },
212
213     releaseObject: function(objectId)
214     {
215         var parsedObjectId = this._parseObjectId(objectId);
216         delete this._idToWrappedObject[parsedObjectId.id];
217     },
218
219     _populatePropertyNames: function(object, resultSet)
220     {
221         for (var o = object; o; o = o.__proto__) {
222             try {
223                 var names = Object.getOwnPropertyNames(o);
224                 for (var i = 0; i < names.length; ++i)
225                     resultSet[names[i]] = true;
226             } catch (e) {
227             }
228         }
229     },
230
231     _getPropertyNames: function(object, resultSet)
232     {
233         var propertyNameSet = {};
234         this._populatePropertyNames(object, propertyNameSet);
235         return Object.keys(propertyNameSet);
236     },
237
238     evaluate: function(expression, objectGroup, injectCommandLineAPI)
239     {
240         return this._evaluateAndWrap(inspectedWindow.eval, inspectedWindow, expression, objectGroup, false, injectCommandLineAPI);
241     },
242
243     evaluateOn: function(objectId, expression)
244     {
245         var parsedObjectId = this._parseObjectId(objectId);
246         var object = this._objectForId(parsedObjectId);
247         if (!object)
248             return false;
249         try {
250             inspectedWindow.console._objectToEvaluateOn = object;
251             return this._evaluateAndWrap(inspectedWindow.eval, inspectedWindow, "(function() {" + expression + "}).call(window.console._objectToEvaluateOn)", parsedObjectId.objectGroup, false, false);
252         } finally {
253             delete inspectedWindow.console._objectToEvaluateOn;
254         }
255     },
256
257     _evaluateAndWrap: function(evalFunction, object, expression, objectGroup, isEvalOnCallFrame, injectCommandLineAPI)
258     {
259         try {
260             return this._wrapObject(this._evaluateOn(evalFunction, object, expression, isEvalOnCallFrame, injectCommandLineAPI), objectGroup);
261         } catch (e) {
262             return InjectedScript.RemoteObject.fromException(e);
263         }
264     },
265
266     _evaluateOn: function(evalFunction, object, expression, isEvalOnCallFrame, injectCommandLineAPI)
267     {
268         // Only install command line api object for the time of evaluation.
269         // Surround the expression in with statements to inject our command line API so that
270         // the window object properties still take more precedent than our API functions.
271
272         try {
273             if (injectCommandLineAPI && inspectedWindow.console) {
274                 inspectedWindow.console._commandLineAPI = new CommandLineAPI(this._commandLineAPIImpl, isEvalOnCallFrame ? object : null);
275                 expression = "with ((window && window.console && window.console._commandLineAPI) || {}) {\n" + expression + "\n}";
276             }
277
278             var value = evalFunction.call(object, expression);
279
280             // When evaluating on call frame error is not thrown, but returned as a value.
281             if (this._type(value) === "error")
282                 throw value.toString();
283
284             return value;
285         } finally {
286             if (injectCommandLineAPI && inspectedWindow.console)
287                 delete inspectedWindow.console._commandLineAPI;
288         }
289     },
290
291     callFrames: function()
292     {
293         var callFrame = InjectedScriptHost.currentCallFrame();
294         if (!callFrame)
295             return false;
296     
297         injectedScript.releaseObjectGroup("backtrace");
298         var result = [];
299         var depth = 0;
300         do {
301             result.push(new InjectedScript.CallFrameProxy(depth++, callFrame));
302             callFrame = callFrame.caller;
303         } while (callFrame);
304         return result;
305     },
306
307     evaluateOnCallFrame: function(callFrameId, expression, objectGroup, injectCommandLineAPI)
308     {
309         var callFrame = this._callFrameForId(callFrameId);
310         if (!callFrame)
311             return false;
312         return this._evaluateAndWrap(callFrame.evaluate, callFrame, expression, objectGroup, true, injectCommandLineAPI);
313     },
314
315     _callFrameForId: function(callFrameId)
316     {
317         var parsedCallFrameId = eval("(" + callFrameId + ")");
318         var ordinal = parsedCallFrameId.ordinal;
319         var callFrame = InjectedScriptHost.currentCallFrame();
320         while (--ordinal >= 0 && callFrame)
321             callFrame = callFrame.caller;
322         return callFrame;
323     },
324
325     _objectForId: function(objectId)
326     {
327         return this._idToWrappedObject[objectId.id];
328     },
329
330     nodeForObjectId: function(objectId)
331     {
332         var parsedObjectId = this._parseObjectId(objectId);
333         var object = this._objectForId(parsedObjectId);
334         if (!object || this._type(object) !== "node")
335             return null;
336         return object;
337     },
338
339     resolveNode: function(node)
340     {
341         return this._wrapObject(node);
342     },
343
344     _isDefined: function(object)
345     {
346         return object || this._isHTMLAllCollection(object);
347     },
348
349     _isHTMLAllCollection: function(object)
350     {
351         // document.all is reported as undefined, but we still want to process it.
352         return (typeof object === "undefined") && inspectedWindow.HTMLAllCollection && object instanceof inspectedWindow.HTMLAllCollection;
353     },
354
355     _type: function(obj)
356     {
357         if (obj === null)
358             return "null";
359
360         var type = typeof obj;
361         if (type !== "object" && type !== "function") {
362             // FIXME(33716): typeof document.all is always 'undefined'.
363             if (this._isHTMLAllCollection(obj))
364                 return "array";
365             return type;
366         }
367
368         // If owning frame has navigated to somewhere else window properties will be undefined.
369         // In this case just return result of the typeof.
370         if (!inspectedWindow.document)
371             return type;
372
373         if (obj instanceof inspectedWindow.Node)
374             return (obj.nodeType === undefined ? type : "node");
375         if (obj instanceof inspectedWindow.String)
376             return "string";
377         if (obj instanceof inspectedWindow.Array)
378             return "array";
379         if (obj instanceof inspectedWindow.Boolean)
380             return "boolean";
381         if (obj instanceof inspectedWindow.Number)
382             return "number";
383         if (obj instanceof inspectedWindow.Date)
384             return "date";
385         if (obj instanceof inspectedWindow.RegExp)
386             return "regexp";
387         // FireBug's array detection.
388         if (isFinite(obj.length) && typeof obj.splice === "function")
389             return "array";
390         if (isFinite(obj.length) && typeof obj.callee === "function") // arguments.
391             return "array";
392         if (obj instanceof inspectedWindow.NodeList)
393             return "array";
394         if (obj instanceof inspectedWindow.HTMLCollection)
395             return "array";
396         if (obj instanceof inspectedWindow.Error)
397             return "error";
398         return type;
399     },
400
401     _describe: function(obj, abbreviated)
402     {
403         var type = this._type(obj);
404
405         switch (type) {
406         case "object":
407         case "node":
408             var result = InjectedScriptHost.internalConstructorName(obj);
409             if (result === "Object") {
410                 // In Chromium DOM wrapper prototypes will have Object as their constructor name,
411                 // get the real DOM wrapper name from the constructor property.
412                 var constructorName = obj.constructor && obj.constructor.name;
413                 if (constructorName)
414                     return constructorName;
415             }
416             return result;
417         case "array":
418             var className = InjectedScriptHost.internalConstructorName(obj);
419             if (typeof obj.length === "number")
420                 className += "[" + obj.length + "]";
421             return className;
422         case "string":
423             if (!abbreviated)
424                 return obj;
425             if (obj.length > 100)
426                 return "\"" + obj.substring(0, 100) + "\u2026\"";
427             return "\"" + obj + "\"";
428         case "function":
429             var objectText = this._toString(obj);
430             if (abbreviated)
431                 objectText = /.*/.exec(objectText)[0].replace(/ +$/g, "");
432             return objectText;
433         default:
434             return this._toString(obj);
435         }
436     },
437
438     _toString: function(obj)
439     {
440         // We don't use String(obj) because inspectedWindow.String is undefined if owning frame navigated to another page.
441         return "" + obj;
442     }
443 }
444
445 var injectedScript = new InjectedScript();
446
447 InjectedScript.RemoteObject = function(objectId, type, description, hasChildren)
448 {
449     this.objectId = objectId;
450     this.type = type;
451     this.description = description;
452     this.hasChildren = hasChildren;
453 }
454
455 InjectedScript.RemoteObject.fromException = function(e)
456 {
457     return new InjectedScript.RemoteObject(null, "error", e.toString());
458 }
459
460 InjectedScript.RemoteObject.fromObject = function(object, objectId, abbreviate)
461 {
462     var type = injectedScript._type(object);
463     var rawType = typeof object;
464     var hasChildren = (rawType === "object" && object !== null && (!!Object.getOwnPropertyNames(object).length || !!object.__proto__)) || rawType === "function";
465     var description = "";
466     try {
467         var description = injectedScript._describe(object, abbreviate);
468         return new InjectedScript.RemoteObject(objectId, type, description, hasChildren);
469     } catch (e) {
470         return InjectedScript.RemoteObject.fromException(e);
471     }
472 }
473
474 InjectedScript.CallFrameProxy = function(ordinal, callFrame)
475 {
476     this.id = { ordinal: ordinal, injectedScriptId: injectedScriptId };
477     this.type = callFrame.type;
478     this.functionName = (this.type === "function" ? callFrame.functionName : "");
479     this.sourceID = callFrame.sourceID;
480     this.line = callFrame.line;
481     this.column = callFrame.column;
482     this.scopeChain = this._wrapScopeChain(callFrame);
483 }
484
485 InjectedScript.CallFrameProxy.prototype = {
486     _wrapScopeChain: function(callFrame)
487     {
488         const GLOBAL_SCOPE = 0;
489         const LOCAL_SCOPE = 1;
490         const WITH_SCOPE = 2;
491         const CLOSURE_SCOPE = 3;
492         const CATCH_SCOPE = 4;
493     
494         var scopeChain = callFrame.scopeChain;
495         var scopeChainProxy = [];
496         var foundLocalScope = false;
497         for (var i = 0; i < scopeChain.length; i++) {
498             var scopeType = callFrame.scopeType(i);
499             var scopeObject = scopeChain[i];
500             var scopeObjectProxy = injectedScript._wrapObject(scopeObject, "backtrace", true);
501
502             switch(scopeType) {
503                 case LOCAL_SCOPE: {
504                     foundLocalScope = true;
505                     scopeObjectProxy.isLocal = true;
506                     scopeObjectProxy.thisObject = injectedScript._wrapObject(callFrame.thisObject, "backtrace", true);
507                     break;
508                 }
509                 case CLOSURE_SCOPE: {
510                     scopeObjectProxy.isClosure = true;
511                     break;
512                 }
513                 case WITH_SCOPE:
514                 case CATCH_SCOPE: {
515                     if (foundLocalScope && scopeObject instanceof inspectedWindow.Element)
516                         scopeObjectProxy.isElement = true;
517                     else if (foundLocalScope && scopeObject instanceof inspectedWindow.Document)
518                         scopeObjectProxy.isDocument = true;
519                     else
520                         scopeObjectProxy.isWithBlock = true;
521                     break;
522                 }
523             }
524             scopeChainProxy.push(scopeObjectProxy);
525         }
526         return scopeChainProxy;
527     }
528 }
529
530 function CommandLineAPI(commandLineAPIImpl, callFrame)
531 {
532     function inScopeVariables(member)
533     {
534         if (!callFrame)
535             return false;
536
537         var scopeChain = callFrame.scopeChain;
538         for (var i = 0; i < scopeChain.length; ++i) {
539             if (member in scopeChain[i])
540                 return true;
541         }
542         return false;
543     }
544
545     for (var i = 0; i < CommandLineAPI.members_.length; ++i) {
546         var member = CommandLineAPI.members_[i];
547         if (member in inspectedWindow || inScopeVariables(member))
548             continue;
549
550         this[member] = bind(commandLineAPIImpl, commandLineAPIImpl[member]);
551     }
552
553     for (var i = 0; i < 5; ++i) {
554         var member = "$" + i;
555         if (member in inspectedWindow || inScopeVariables(member))
556             continue;
557
558         this.__defineGetter__("$" + i, bind(commandLineAPIImpl, commandLineAPIImpl._inspectedNode, i));
559     }
560 }
561
562 CommandLineAPI.members_ = [
563     "$", "$$", "$x", "dir", "dirxml", "keys", "values", "profile", "profileEnd",
564     "monitorEvents", "unmonitorEvents", "inspect", "copy", "clear"
565 ];
566
567 function CommandLineAPIImpl()
568 {
569 }
570
571 CommandLineAPIImpl.prototype = {
572     $: function()
573     {
574         return document.getElementById.apply(document, arguments)
575     },
576
577     $$: function()
578     {
579         return document.querySelectorAll.apply(document, arguments)
580     },
581
582     $x: function(xpath, context)
583     {
584         var nodes = [];
585         try {
586             var doc = (context && context.ownerDocument) || inspectedWindow.document;
587             var results = doc.evaluate(xpath, context || doc, null, XPathResult.ANY_TYPE, null);
588             var node;
589             while (node = results.iterateNext())
590                 nodes.push(node);
591         } catch (e) {
592         }
593         return nodes;
594     },
595
596     dir: function()
597     {
598         return console.dir.apply(console, arguments)
599     },
600
601     dirxml: function()
602     {
603         return console.dirxml.apply(console, arguments)
604     },
605
606     keys: function(object)
607     {
608         return Object.keys(object);
609     },
610
611     values: function(object)
612     {
613         var result = [];
614         for (var key in object)
615             result.push(object[key]);
616         return result;
617     },
618
619     profile: function()
620     {
621         return console.profile.apply(console, arguments)
622     },
623
624     profileEnd: function()
625     {
626         return console.profileEnd.apply(console, arguments)
627     },
628
629     monitorEvents: function(object, types)
630     {
631         if (!object || !object.addEventListener || !object.removeEventListener)
632             return;
633         types = this._normalizeEventTypes(types);
634         for (var i = 0; i < types.length; ++i) {
635             object.removeEventListener(types[i], this._logEvent, false);
636             object.addEventListener(types[i], this._logEvent, false);
637         }
638     },
639
640     unmonitorEvents: function(object, types)
641     {
642         if (!object || !object.addEventListener || !object.removeEventListener)
643             return;
644         types = this._normalizeEventTypes(types);
645         for (var i = 0; i < types.length; ++i)
646             object.removeEventListener(types[i], this._logEvent, false);
647     },
648
649     inspect: function(object)
650     {
651         return injectedScript._inspect(object);
652     },
653
654     copy: function(object)
655     {
656         if (injectedScript._type(object) === "node")
657             object = object.outerHTML;
658         InjectedScriptHost.copyText(object);
659     },
660
661     clear: function()
662     {
663         InjectedScriptHost.clearConsoleMessages();
664     },
665
666     _inspectedNode: function(num)
667     {
668         return InjectedScriptHost.inspectedNode(num);
669     },
670
671     _normalizeEventTypes: function(types)
672     {
673         if (typeof types === "undefined")
674             types = [ "mouse", "key", "load", "unload", "abort", "error", "select", "change", "submit", "reset", "focus", "blur", "resize", "scroll" ];
675         else if (typeof types === "string")
676             types = [ types ];
677
678         var result = [];
679         for (var i = 0; i < types.length; i++) {
680             if (types[i] === "mouse")
681                 result.splice(0, 0, "mousedown", "mouseup", "click", "dblclick", "mousemove", "mouseover", "mouseout");
682             else if (types[i] === "key")
683                 result.splice(0, 0, "keydown", "keyup", "keypress");
684             else
685                 result.push(types[i]);
686         }
687         return result;
688     },
689
690     _logEvent: function(event)
691     {
692         console.log(event.type, event);
693     }
694 }
695
696 injectedScript._commandLineAPIImpl = new CommandLineAPIImpl();
697 return injectedScript;
698 })