1/*
2 * Copyright (C) 2013 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 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26WebInspector.ApplicationCacheDetailsSidebarPanel = function() {
27    WebInspector.DetailsSidebarPanel.call(this, "application-cache-details", WebInspector.UIString("Storage"), WebInspector.UIString("Storage"), "Images/NavigationItemStorage.svg");
28
29    this.element.classList.add(WebInspector.ApplicationCacheDetailsSidebarPanel.StyleClassName);
30
31    this._applicationCacheFrame = null;
32
33    this._locationManifestURLRow = new WebInspector.DetailsSectionSimpleRow(WebInspector.UIString("Manifest URL"));
34    this._locationFrameURLRow = new WebInspector.DetailsSectionSimpleRow(WebInspector.UIString("Frame URL"));
35
36    this._locationGroup = new WebInspector.DetailsSectionGroup([this._locationManifestURLRow, this._locationFrameURLRow]);
37
38    this._locationSection = new WebInspector.DetailsSection("application-cache-location", WebInspector.UIString("Location"), [this._locationGroup]);
39
40    this._onlineRow = new WebInspector.DetailsSectionSimpleRow(WebInspector.UIString("Online"));
41    this._statusRow = new WebInspector.DetailsSectionSimpleRow(WebInspector.UIString("Status"));
42
43    this._statusGroup = new WebInspector.DetailsSectionGroup([this._onlineRow, this._statusRow]);
44
45    this._statusSection = new WebInspector.DetailsSection("application-cache-status", WebInspector.UIString("Status"), [this._statusGroup]);
46
47    this.element.appendChild(this._locationSection.element);
48    this.element.appendChild(this._statusSection.element);
49
50    WebInspector.applicationCacheManager.addEventListener(WebInspector.ApplicationCacheManager.Event.NetworkStateUpdated, this._networkStateUpdated, this);
51    WebInspector.applicationCacheManager.addEventListener(WebInspector.ApplicationCacheManager.Event.FrameManifestStatusChanged, this._frameManifestStatusChanged, this);
52};
53
54WebInspector.ApplicationCacheDetailsSidebarPanel.StyleClassName = "application-cache";
55
56// This needs to be kept in sync with ApplicationCacheManager.js.
57WebInspector.ApplicationCacheDetailsSidebarPanel.Status = {
58    0: "Uncached",
59    1: "Idle",
60    2: "Checking",
61    3: "Downloading",
62    4: "UpdateReady",
63    5: "Obsolete"
64};
65
66WebInspector.ApplicationCacheDetailsSidebarPanel.prototype = {
67    constructor: WebInspector.ApplicationCacheDetailsSidebarPanel,
68
69    // Public
70
71    inspect: function(objects)
72    {
73        // Convert to a single item array if needed.
74        if (!(objects instanceof Array))
75            objects = [objects];
76
77        var applicationCacheFrameToInspect = null;
78
79        // Iterate over the objects to find a WebInspector.ApplicationCacheFrame to inspect.
80        for (var i = 0; i < objects.length; ++i) {
81            if (objects[i] instanceof WebInspector.ApplicationCacheFrame) {
82                applicationCacheFrameToInspect = objects[i];
83                break;
84            }
85        }
86
87        this.applicationCacheFrame = applicationCacheFrameToInspect;
88
89        return !!this.applicationCacheFrame;
90    },
91
92    get applicationCacheFrame()
93    {
94        return this._applicationCacheFrame;
95    },
96
97    set applicationCacheFrame(applicationCacheFrame)
98    {
99        if (this._applicationCacheFrame === applicationCacheFrame)
100            return;
101
102        this._applicationCacheFrame = applicationCacheFrame;
103
104        this.needsRefresh();
105    },
106
107    refresh: function()
108    {
109        if (!this.applicationCacheFrame)
110            return;
111
112        this._locationFrameURLRow.value = this.applicationCacheFrame.frame.url;
113        this._locationManifestURLRow.value = this.applicationCacheFrame.manifest.manifestURL;
114
115        this._refreshOnlineRow();
116        this._refreshStatusRow();
117    },
118
119    // Private
120
121    _networkStateUpdated: function(event)
122    {
123        if (!this.applicationCacheFrame)
124            return;
125
126        this._refreshOnlineRow();
127    },
128
129    _frameManifestStatusChanged: function(event)
130    {
131        if (!this.applicationCacheFrame)
132            return;
133
134        console.assert(event.data.frameManifest instanceof WebInspector.ApplicationCacheFrame);
135        if (event.data.frameManifest !== this.applicationCacheFrame)
136            return;
137
138        this._refreshStatusRow();
139    },
140
141    _refreshOnlineRow: function()
142    {
143        this._onlineRow.value = WebInspector.applicationCacheManager.online ? WebInspector.UIString("Yes") : WebInspector.UIString("No");
144    },
145
146    _refreshStatusRow: function()
147    {
148        this._statusRow.value = WebInspector.ApplicationCacheDetailsSidebarPanel.Status[this.applicationCacheFrame.status];
149    }
150};
151
152WebInspector.ApplicationCacheDetailsSidebarPanel.prototype.__proto__ = WebInspector.DetailsSidebarPanel.prototype;
153