2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * @extends {WebInspector.Object}
34 * @param {!WebInspector.ResourceTreeModel} resourceTreeModel
36 WebInspector.RuntimeModel = function(resourceTreeModel)
38 resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.FrameAdded, this._frameAdded, this);
39 resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.FrameNavigated, this._frameNavigated, this);
40 resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.FrameDetached, this._frameDetached, this);
41 resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.CachedResourcesLoaded, this._didLoadCachedResources, this);
42 this._frameIdToContextList = {};
45 WebInspector.RuntimeModel.Events = {
46 FrameExecutionContextListAdded: "FrameExecutionContextListAdded",
47 FrameExecutionContextListRemoved: "FrameExecutionContextListRemoved",
50 WebInspector.RuntimeModel.prototype = {
52 * @param {?WebInspector.ExecutionContext} executionContext
54 setCurrentExecutionContext: function(executionContext)
56 this._currentExecutionContext = executionContext;
60 * @return {?WebInspector.ExecutionContext}
62 currentExecutionContext: function()
64 return this._currentExecutionContext;
68 * @return {!Array.<!WebInspector.FrameExecutionContextList>}
70 contextLists: function()
72 return Object.values(this._frameIdToContextList);
76 * @param {!WebInspector.ResourceTreeFrame} frame
77 * @return {!WebInspector.FrameExecutionContextList}
79 contextListByFrame: function(frame)
81 return this._frameIdToContextList[frame.id];
85 * @param {!WebInspector.Event} event
87 _frameAdded: function(event)
89 var frame = /** @type {!WebInspector.ResourceTreeFrame} */ (event.data);
90 var context = new WebInspector.FrameExecutionContextList(frame);
91 this._frameIdToContextList[frame.id] = context;
92 this.dispatchEventToListeners(WebInspector.RuntimeModel.Events.FrameExecutionContextListAdded, context);
96 * @param {!WebInspector.Event} event
98 _frameNavigated: function(event)
100 var frame = /** @type {!WebInspector.ResourceTreeFrame} */ (event.data);
101 var context = this._frameIdToContextList[frame.id];
103 context._frameNavigated(frame);
107 * @param {!WebInspector.Event} event
109 _frameDetached: function(event)
111 var frame = /** @type {!WebInspector.ResourceTreeFrame} */ (event.data);
112 var context = this._frameIdToContextList[frame.id];
115 this.dispatchEventToListeners(WebInspector.RuntimeModel.Events.FrameExecutionContextListRemoved, context);
116 delete this._frameIdToContextList[frame.id];
119 _didLoadCachedResources: function()
121 InspectorBackend.registerRuntimeDispatcher(new WebInspector.RuntimeDispatcher(this));
122 RuntimeAgent.enable();
125 _executionContextCreated: function(context)
127 var contextList = this._frameIdToContextList[context.frameId];
128 console.assert(contextList);
129 contextList._addExecutionContext(new WebInspector.ExecutionContext(context.id, context.name, context.isPageContext));
133 * @param {string} expression
134 * @param {string} objectGroup
135 * @param {boolean} includeCommandLineAPI
136 * @param {boolean} doNotPauseOnExceptionsAndMuteConsole
137 * @param {boolean} returnByValue
138 * @param {boolean} generatePreview
139 * @param {function(?WebInspector.RemoteObject, boolean, ?RuntimeAgent.RemoteObject=)} callback
141 evaluate: function(expression, objectGroup, includeCommandLineAPI, doNotPauseOnExceptionsAndMuteConsole, returnByValue, generatePreview, callback)
143 if (WebInspector.debuggerModel.selectedCallFrame()) {
144 WebInspector.debuggerModel.evaluateOnSelectedCallFrame(expression, objectGroup, includeCommandLineAPI, doNotPauseOnExceptionsAndMuteConsole, returnByValue, generatePreview, callback);
149 // There is no expression, so the completion should happen against global properties.
154 * @param {?Protocol.Error} error
155 * @param {!RuntimeAgent.RemoteObject} result
156 * @param {boolean=} wasThrown
158 function evalCallback(error, result, wasThrown)
161 callback(null, false);
166 callback(null, !!wasThrown, wasThrown ? null : result);
168 callback(WebInspector.RemoteObject.fromPayload(result), !!wasThrown);
170 RuntimeAgent.evaluate(expression, objectGroup, includeCommandLineAPI, doNotPauseOnExceptionsAndMuteConsole, this._currentExecutionContext ? this._currentExecutionContext.id : undefined, returnByValue, generatePreview, evalCallback);
174 * @param {!Element} proxyElement
175 * @param {!Range} wordRange
176 * @param {boolean} force
177 * @param {function(!Array.<string>, number=)} completionsReadyCallback
179 completionsForTextPrompt: function(proxyElement, wordRange, force, completionsReadyCallback)
181 // Pass less stop characters to rangeOfWord so the range will be a more complete expression.
182 var expressionRange = wordRange.startContainer.rangeOfWord(wordRange.startOffset, " =:[({;,!+-*/&|^<>", proxyElement, "backward");
183 var expressionString = expressionRange.toString();
184 var prefix = wordRange.toString();
185 this._completionsForExpression(expressionString, prefix, force, completionsReadyCallback);
189 * @param {string} expressionString
190 * @param {string} prefix
191 * @param {boolean} force
192 * @param {function(!Array.<string>, number=)} completionsReadyCallback
194 _completionsForExpression: function(expressionString, prefix, force, completionsReadyCallback)
196 var lastIndex = expressionString.length - 1;
198 var dotNotation = (expressionString[lastIndex] === ".");
199 var bracketNotation = (expressionString[lastIndex] === "[");
201 if (dotNotation || bracketNotation)
202 expressionString = expressionString.substr(0, lastIndex);
204 if (expressionString && parseInt(expressionString, 10) == expressionString) {
205 // User is entering float value, do not suggest anything.
206 completionsReadyCallback([]);
210 if (!prefix && !expressionString && !force) {
211 completionsReadyCallback([]);
215 if (!expressionString && WebInspector.debuggerModel.selectedCallFrame())
216 WebInspector.debuggerModel.getSelectedCallFrameVariables(receivedPropertyNames.bind(this));
218 this.evaluate(expressionString, "completion", true, true, false, false, evaluated.bind(this));
221 * @this {WebInspector.RuntimeModel}
223 function evaluated(result, wasThrown)
225 if (!result || wasThrown) {
226 completionsReadyCallback([]);
231 * @param {string} primitiveType
232 * @this {WebInspector.RuntimeModel}
234 function getCompletions(primitiveType)
237 if (primitiveType === "string")
238 object = new String("");
239 else if (primitiveType === "number")
240 object = new Number(0);
241 else if (primitiveType === "boolean")
242 object = new Boolean(false);
247 for (var o = object; o; o = o.__proto__) {
249 var names = Object.getOwnPropertyNames(o);
250 for (var i = 0; i < names.length; ++i)
251 resultSet[names[i]] = true;
258 if (result.type === "object" || result.type === "function")
259 result.callFunctionJSON(getCompletions, undefined, receivedPropertyNames.bind(this));
260 else if (result.type === "string" || result.type === "number" || result.type === "boolean")
261 this.evaluate("(" + getCompletions + ")(\"" + result.type + "\")", "completion", false, true, true, false, receivedPropertyNamesFromEval.bind(this));
265 * @param {?WebInspector.RemoteObject} notRelevant
266 * @param {boolean} wasThrown
267 * @param {?RuntimeAgent.RemoteObject=} result
268 * @this {WebInspector.RuntimeModel}
270 function receivedPropertyNamesFromEval(notRelevant, wasThrown, result)
272 if (result && !wasThrown)
273 receivedPropertyNames.call(this, result.value);
275 completionsReadyCallback([]);
279 * @this {WebInspector.RuntimeModel}
281 function receivedPropertyNames(propertyNames)
283 RuntimeAgent.releaseObjectGroup("completion");
284 if (!propertyNames) {
285 completionsReadyCallback([]);
288 var includeCommandLineAPI = (!dotNotation && !bracketNotation);
289 if (includeCommandLineAPI) {
290 const commandLineAPI = ["dir", "dirxml", "keys", "values", "profile", "profileEnd", "monitorEvents", "unmonitorEvents", "inspect", "copy", "clear",
291 "getEventListeners", "debug", "undebug", "monitor", "unmonitor", "table", "$", "$$", "$x"];
292 for (var i = 0; i < commandLineAPI.length; ++i)
293 propertyNames[commandLineAPI[i]] = true;
295 this._reportCompletions(completionsReadyCallback, dotNotation, bracketNotation, expressionString, prefix, Object.keys(propertyNames));
300 * @param {function(!Array.<string>, number=)} completionsReadyCallback
301 * @param {boolean} dotNotation
302 * @param {boolean} bracketNotation
303 * @param {string} expressionString
304 * @param {string} prefix
305 * @param {!Array.<string>} properties
307 _reportCompletions: function(completionsReadyCallback, dotNotation, bracketNotation, expressionString, prefix, properties) {
308 if (bracketNotation) {
309 if (prefix.length && prefix[0] === "'")
312 var quoteUsed = "\"";
317 if (!expressionString) {
318 const keywords = ["break", "case", "catch", "continue", "default", "delete", "do", "else", "finally", "for", "function", "if", "in",
319 "instanceof", "new", "return", "switch", "this", "throw", "try", "typeof", "var", "void", "while", "with"];
320 properties = properties.concat(keywords);
325 for (var i = 0; i < properties.length; ++i) {
326 var property = properties[i];
328 // Assume that all non-ASCII characters are letters and thus can be used as part of identifier.
329 if (dotNotation && !/^[a-zA-Z_$\u008F-\uFFFF][a-zA-Z0-9_$\u008F-\uFFFF]*$/.test(property))
332 if (bracketNotation) {
333 if (!/^[0-9]+$/.test(property))
334 property = quoteUsed + property.escapeCharacters(quoteUsed + "\\") + quoteUsed;
338 if (property.length < prefix.length)
340 if (prefix.length && !property.startsWith(prefix))
343 results.push(property);
345 completionsReadyCallback(results);
348 __proto__: WebInspector.Object.prototype
352 * @type {!WebInspector.RuntimeModel}
354 WebInspector.runtimeModel;
358 * @implements {RuntimeAgent.Dispatcher}
359 * @param {!WebInspector.RuntimeModel} runtimeModel
361 WebInspector.RuntimeDispatcher = function(runtimeModel)
363 this._runtimeModel = runtimeModel;
366 WebInspector.RuntimeDispatcher.prototype = {
367 executionContextCreated: function(context)
369 this._runtimeModel._executionContextCreated(context);
376 WebInspector.ExecutionContext = function(id, name, isPageContext)
379 this.name = (isPageContext && !name) ? "<page context>" : name;
380 this.isMainWorldContext = isPageContext;
384 * @param {!WebInspector.ExecutionContext} a
385 * @param {!WebInspector.ExecutionContext} b
388 WebInspector.ExecutionContext.comparator = function(a, b)
390 // Main world context should always go first.
391 if (a.isMainWorldContext)
393 if (b.isMainWorldContext)
395 return a.name.localeCompare(b.name);
400 * @extends {WebInspector.Object}
401 * @param {!WebInspector.ResourceTreeFrame} frame
403 WebInspector.FrameExecutionContextList = function(frame)
406 this._executionContexts = [];
409 WebInspector.FrameExecutionContextList.EventTypes = {
410 ContextsUpdated: "ContextsUpdated",
411 ContextAdded: "ContextAdded"
414 WebInspector.FrameExecutionContextList.prototype =
417 * @param {!WebInspector.ResourceTreeFrame} frame
419 _frameNavigated: function(frame)
422 this._executionContexts = [];
423 this.dispatchEventToListeners(WebInspector.FrameExecutionContextList.EventTypes.ContextsUpdated, this);
427 * @param {!WebInspector.ExecutionContext} context
429 _addExecutionContext: function(context)
431 var insertAt = insertionIndexForObjectInListSortedByFunction(context, this._executionContexts, WebInspector.ExecutionContext.comparator);
432 this._executionContexts.splice(insertAt, 0, context);
433 this.dispatchEventToListeners(WebInspector.FrameExecutionContextList.EventTypes.ContextAdded, this);
437 * @return {!Array.<!WebInspector.ExecutionContext>}
439 executionContexts: function()
441 return this._executionContexts;
445 * @return {!WebInspector.ExecutionContext}
447 mainWorldContext: function()
449 return this._executionContexts[0];
453 * @param {string} securityOrigin
454 * @return {?WebInspector.ExecutionContext}
456 contextBySecurityOrigin: function(securityOrigin)
458 for (var i = 0; i < this._executionContexts.length; ++i) {
459 var context = this._executionContexts[i];
460 if (!context.isMainWorldContext && context.name === securityOrigin)
471 return this._frame.id;
479 return this._frame.url;
487 return this._frame.displayName();
490 __proto__: WebInspector.Object.prototype