1/*
2 * Copyright (C) 2010 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/**
32 * @constructor
33 * @extends {WebInspector.TabbedPane}
34 * @param {WebInspector.NetworkRequest} request
35 */
36WebInspector.NetworkItemView = function(request)
37{
38    WebInspector.TabbedPane.call(this);
39    this.element.addStyleClass("network-item-view");
40
41    var headersView = new WebInspector.RequestHeadersView(request);
42    this.appendTab("headers", WebInspector.UIString("Headers"), headersView);
43
44    this.addEventListener(WebInspector.TabbedPane.EventTypes.TabSelected, this._tabSelected, this);
45
46    if (request.type === WebInspector.resourceTypes.WebSocket) {
47        var frameView = new WebInspector.ResourceWebSocketFrameView(request);
48        this.appendTab("webSocketFrames", WebInspector.UIString("Frames"), frameView);
49    } else {
50        var responseView = new WebInspector.RequestResponseView(request);
51        var previewView = new WebInspector.RequestPreviewView(request, responseView);
52        this.appendTab("preview", WebInspector.UIString("Preview"), previewView);
53        this.appendTab("response", WebInspector.UIString("Response"), responseView);
54    }
55
56    if (request.requestCookies || request.responseCookies) {
57        this._cookiesView = new WebInspector.RequestCookiesView(request);
58        this.appendTab("cookies", WebInspector.UIString("Cookies"), this._cookiesView);
59    }
60
61    if (request.timing) {
62        var timingView = new WebInspector.RequestTimingView(request);
63        this.appendTab("timing", WebInspector.UIString("Timing"), timingView);
64    }
65    this._request = request;
66}
67
68WebInspector.NetworkItemView.prototype = {
69    wasShown: function()
70    {
71        WebInspector.TabbedPane.prototype.wasShown.call(this);
72        this._selectTab();
73    },
74
75    /**
76     * @param {string=} tabId
77     */
78    _selectTab: function(tabId)
79    {
80        if (!tabId)
81            tabId = WebInspector.settings.resourceViewTab.get();
82
83        if (!this.selectTab(tabId)) {
84            this._isInFallbackSelection = true;
85            this.selectTab("headers");
86            delete this._isInFallbackSelection;
87        }
88    },
89
90    _tabSelected: function(event)
91    {
92        if (!event.data.isUserGesture)
93            return;
94
95        WebInspector.settings.resourceViewTab.set(event.data.tabId);
96
97        WebInspector.notifications.dispatchEventToListeners(WebInspector.UserMetrics.UserAction, {
98            action: WebInspector.UserMetrics.UserActionNames.NetworkRequestTabSelected,
99            tab: event.data.tabId,
100            url: this._request.url
101        });
102    },
103
104    /**
105      * @return {WebInspector.NetworkRequest}
106      */
107    request: function()
108    {
109        return this._request;
110    },
111
112    __proto__: WebInspector.TabbedPane.prototype
113}
114
115/**
116 * @constructor
117 * @extends {WebInspector.RequestView}
118 * @param {WebInspector.NetworkRequest} request
119 */
120WebInspector.RequestContentView = function(request)
121{
122    WebInspector.RequestView.call(this, request);
123}
124
125WebInspector.RequestContentView.prototype = {
126    hasContent: function()
127    {
128        return true;
129    },
130
131    get innerView()
132    {
133        return this._innerView;
134    },
135
136    set innerView(innerView)
137    {
138        this._innerView = innerView;
139    },
140
141    wasShown: function()
142    {
143        this._ensureInnerViewShown();
144    },
145
146    _ensureInnerViewShown: function()
147    {
148        if (this._innerViewShowRequested)
149            return;
150        this._innerViewShowRequested = true;
151
152        /**
153         * @param {?string} content
154         * @param {boolean} contentEncoded
155         * @param {string} mimeType
156         */
157        function callback(content, contentEncoded, mimeType)
158        {
159            this._innerViewShowRequested = false;
160            this.contentLoaded();
161        }
162
163        this.request.requestContent(callback.bind(this));
164    },
165
166    contentLoaded: function()
167    {
168        // Should be implemented by subclasses.
169    },
170
171    canHighlightLine: function()
172    {
173        return this._innerView && this._innerView.canHighlightLine();
174    },
175
176    highlightLine: function(line)
177    {
178        if (this.canHighlightLine())
179            this._innerView.highlightLine(line);
180    },
181
182    __proto__: WebInspector.RequestView.prototype
183}
184