1/*
2 * Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies)
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this program; see the file COPYING.LIB.  If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 *
19 */
20
21
22#include "config.h"
23
24#if ENABLE(INSPECTOR_SERVER)
25#include "WebInspectorServer.h"
26
27#include "WebInspectorProxy.h"
28#include "WebPageProxy.h"
29#include <QFile>
30#include <WebCore/MIMETypeRegistry.h>
31#include <wtf/text/CString.h>
32#include <wtf/text/StringBuilder.h>
33
34namespace WebKit {
35
36static String remoteInspectorPagePath()
37{
38    DEFINE_STATIC_LOCAL(String, pagePath, (ASCIILiteral("/webkit/inspector/inspector.html?page=")));
39    return pagePath;
40}
41
42bool WebInspectorServer::platformResourceForPath(const String& path, Vector<char>& data, String& contentType)
43{
44    // The page list contains an unformated list of pages that can be inspected with a link to open a session.
45    if (path == "/pagelist.json") {
46        buildPageList(data, contentType);
47        return true;
48    }
49
50    // Point the default path to a formatted page that queries the page list and display them.
51    String localPath = (path == "/") ? "/webkit/resources/inspectorPageIndex.html" : path;
52    // All other paths are mapped directly to a resource, if possible.
53    QFile file(QString::fromLatin1(":%1").arg(localPath));
54    if (file.exists()) {
55        file.open(QIODevice::ReadOnly);
56        data.grow(file.size());
57        file.read(data.data(), data.size());
58
59        size_t extStart = localPath.reverseFind('.');
60        String ext = localPath.substring(extStart != notFound ? extStart + 1 : 0);
61        contentType = WebCore::MIMETypeRegistry::getMIMETypeForExtension(ext);
62        return true;
63    }
64    return false;
65}
66
67String WebInspectorServer::inspectorUrlForPageID(int pageId)
68{
69    if (pageId <= 0 || serverState() == Closed)
70        return String();
71    StringBuilder builder;
72    builder.appendLiteral("http://");
73    builder.append(bindAddress());
74    builder.append(':');
75    builder.appendNumber(port());
76    builder.append(remoteInspectorPagePath());
77    builder.appendNumber(pageId);
78    return builder.toString();
79}
80
81void WebInspectorServer::buildPageList(Vector<char>& data, String& contentType)
82{
83    StringBuilder builder;
84    builder.appendLiteral("[ ");
85    ClientMap::iterator end = m_clientMap.end();
86    for (ClientMap::iterator it = m_clientMap.begin(); it != end; ++it) {
87        WebPageProxy* webPage = it->value->page();
88        if (it != m_clientMap.begin())
89            builder.appendLiteral(", ");
90        builder.appendLiteral("{ \"id\": ");
91        builder.appendNumber(it->key);
92        builder.appendLiteral(", \"title\": \"");
93        builder.append(webPage->pageTitle());
94        builder.appendLiteral("\", \"url\": \"");
95        builder.append(webPage->activeURL());
96        builder.appendLiteral("\", \"inspectorUrl\": \"");
97        builder.append(remoteInspectorPagePath());
98        builder.appendNumber(it->key);
99        builder.appendLiteral("\" }");
100    }
101    builder.appendLiteral(" ]");
102    CString cstr = builder.toString().utf8();
103    data.append(cstr.data(), cstr.length());
104    contentType = "application/json; charset=utf-8";
105}
106
107}
108#endif
109