1/*
2 * Copyright (C) 2012 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.VBox}
34 * @param {!WebInspector.FileSystemModel.File} file
35 */
36WebInspector.FileContentView = function(file)
37{
38    WebInspector.VBox.call(this);
39
40    this._innerView = /** @type {?WebInspector.View} */ (null);
41    this._file = file;
42    this._content = null;
43}
44
45WebInspector.FileContentView.prototype = {
46    wasShown: function()
47    {
48        if (!this._innerView) {
49            if (this._file.isTextFile)
50                this._innerView = new WebInspector.EmptyView("");
51            else
52                this._innerView = new WebInspector.EmptyView(WebInspector.UIString("Binary File"));
53            this.refresh();
54        }
55
56        this._innerView.show(this.element);
57    },
58
59    /**
60     * @param {number} errorCode
61     * @param {!FileSystemAgent.Metadata} metadata
62     */
63    _metadataReceived: function(errorCode, metadata)
64    {
65        if (errorCode || !metadata)
66            return;
67
68        if (this._content) {
69            if (!this._content.updateMetadata(metadata))
70                return;
71            var sourceFrame = /** @type {!WebInspector.SourceFrame} */ (this._innerView);
72            this._content.requestContent(sourceFrame.setContent.bind(sourceFrame));
73        } else {
74            this._innerView.detach();
75            this._content = new WebInspector.FileContentView.FileContentProvider(this._file, metadata);
76            var sourceFrame = new WebInspector.SourceFrame(this._content);
77            sourceFrame.setHighlighterType(this._file.resourceType.canonicalMimeType());
78            this._innerView = sourceFrame;
79            this._innerView.show(this.element);
80        }
81    },
82
83    refresh: function()
84    {
85        if (!this._innerView)
86            return;
87
88        if (this._file.isTextFile)
89            this._file.requestMetadata(this._metadataReceived.bind(this));
90    },
91
92    __proto__: WebInspector.VBox.prototype
93}
94
95/**
96 * @constructor
97 * @implements {WebInspector.ContentProvider}
98 * @param {!WebInspector.FileSystemModel.File} file
99 * @param {!FileSystemAgent.Metadata} metadata
100 */
101WebInspector.FileContentView.FileContentProvider = function(file, metadata)
102{
103    this._file = file;
104    this._metadata = metadata;
105}
106
107WebInspector.FileContentView.FileContentProvider.prototype = {
108    /**
109     * @return {string}
110     */
111    contentURL: function()
112    {
113        return this._file.url;
114    },
115
116    /**
117     * @return {!WebInspector.ResourceType}
118     */
119    contentType: function()
120    {
121        return this._file.resourceType;
122    },
123
124    /**
125     * @param {function(?string)} callback
126     */
127    requestContent: function(callback)
128    {
129        var size = /** @type {number} */ (this._metadata.size);
130        this._file.requestFileContent(true, 0, size, this._charset || "", this._fileContentReceived.bind(this, callback));
131    },
132
133    /**
134     * @param {function(?string)} callback
135     * @param {number} errorCode
136     * @param {string=} content
137     * @param {boolean=} base64Encoded
138     * @param {string=} charset
139     */
140    _fileContentReceived: function(callback, errorCode, content, base64Encoded, charset)
141    {
142        if (errorCode || !content) {
143            callback(null);
144            return;
145        }
146
147        this._charset = charset;
148        callback(content);
149    },
150
151    /**
152     * @param {string} query
153     * @param {boolean} caseSensitive
154     * @param {boolean} isRegex
155     * @param {function(!Array.<!WebInspector.ContentProvider.SearchMatch>)} callback
156     */
157    searchInContent: function(query, caseSensitive, isRegex, callback)
158    {
159        setTimeout(callback.bind(null, []), 0);
160    },
161
162    /**
163     * @param {!FileSystemAgent.Metadata} metadata
164     * @return {boolean}
165     */
166    updateMetadata: function(metadata)
167    {
168        if (this._metadata.modificationTime >= metadata.modificationTime)
169            return false;
170        this._metadata = metadata.modificationTime;
171        return true;
172    }
173}
174