1/*
2 * Copyright (C) 2011 Google 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 are
6 * met:
7 *
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
13 * distribution.
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.
17 *
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.
29 */
30
31WebInspector.JavaScriptContextManager = function(resourceTreeModel, consoleView)
32{
33 resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.FrameAdded, this._frameAdded, this);
34 resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.FrameNavigated, this._frameNavigated, this);
35 resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.FrameDetached, this._frameDetached, this);
36 this._consoleView = consoleView;
37 this._frameIdToContext = {};
38}
39
40WebInspector.JavaScriptContextManager.prototype = {
41 _frameAdded: function(event)
42 {
43 var frame = event.data;
44 var parentFrameId = frame.parentId;
45 var context = new WebInspector.FrameEvaluationContext(frame);
46 this._frameIdToContext[frame.id] = context;
47 this._consoleView.addContext(context);
48 },
49
50 _frameNavigated: function(event)
51 {
52 var frame = event.data.frame;
53 var context = this._frameIdToContext[frame.id];
54 if (context)
55 context._frameNavigated(frame);
56 },
57
58 _frameDetached: function(event)
59 {
60 var frameId = event.data;
61 var context = this._frameIdToContext[frameId];
62 this._consoleView.removeContext(context);
63 delete this._frameIdToContext[frameId];
64 },
65}
66
67WebInspector.JavaScriptContextManager.prototype.__proto__ = WebInspector.Object.prototype;
68
69WebInspector.FrameEvaluationContext = function(frame)
70{
71 this._frame = frame;
72}
73
74WebInspector.FrameEvaluationContext.EventTypes = {
75 Updated: "updated"
76}
77
78WebInspector.FrameEvaluationContext.prototype =
79{
80 _frameNavigated: function(frame)
81 {
82 this._frame = frame;
83 this.dispatchEventToListeners(WebInspector.FrameEvaluationContext.EventTypes.Updated, this);
84 },
85
86 get frameId()
87 {
88 return this._frame.id
89 },
90
91 get url()
92 {
93 return this._frame.url;
94 },
95
96 get displayName()
97 {
98 if (!this._frame.parentId)
99 return "<top frame>";
100 var name = this._frame.name || "";
101 var subtitle = new WebInspector.Resource(null, this._frame.url).displayName;
102 if (subtitle) {
103 if (!name)
104 return subtitle;
105 return name + "( " + subtitle + " )";
106 }
107 return "<iframe>";
108 }
109}
110
111WebInspector.FrameEvaluationContext.prototype.__proto__ = WebInspector.Object.prototype;