initial import
[vuplus_webkit] / Source / WebCore / inspector / front-end / JavaScriptContextManager.js
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
31 WebInspector.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
40 WebInspector.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         if (!context)
63             return;
64         this._consoleView.removeContext(context);
65         delete this._frameIdToContext[frameId];
66     },
67 }
68
69 WebInspector.JavaScriptContextManager.prototype.__proto__ = WebInspector.Object.prototype;
70
71 WebInspector.FrameEvaluationContext = function(frame)
72 {
73     this._frame = frame;
74 }
75
76 WebInspector.FrameEvaluationContext.EventTypes = {
77     Updated: "updated"
78 }
79
80 WebInspector.FrameEvaluationContext.prototype =
81 {
82     _frameNavigated: function(frame)
83     {
84         this._frame = frame;
85         this.dispatchEventToListeners(WebInspector.FrameEvaluationContext.EventTypes.Updated, this);
86     },
87
88     get frameId()
89     {
90         return this._frame.id
91     },
92
93     get url()
94     {
95         return this._frame.url;
96     },
97
98     get displayName()
99     {
100         if (!this._frame.parentId)
101             return "<top frame>";
102         var name = this._frame.name || "";
103         var subtitle = new WebInspector.Resource(null, this._frame.url).displayName;
104         if (subtitle) {
105             if (!name)
106                 return subtitle;
107             return name + "( " + subtitle + " )";
108         }
109         return "<iframe>";
110     }
111 }
112
113 WebInspector.FrameEvaluationContext.prototype.__proto__ = WebInspector.Object.prototype;