summaryrefslogtreecommitdiffstats
path: root/Source/WebKit2/UIProcess/InspectorServer/qt/WebInspectorServerQt.cpp
blob: 73a0c266461e56b9a83c422fc2377eb68d199619 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this program; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */


#include "config.h"

#if ENABLE(INSPECTOR_SERVER)
#include "WebInspectorServer.h"

#include "WebInspectorProxy.h"
#include "WebPageProxy.h"
#include <QFile>
#include <WebCore/MIMETypeRegistry.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/text/CString.h>
#include <wtf/text/StringBuilder.h>

namespace WebKit {

static String remoteInspectorPagePath()
{
    static NeverDestroyed<WTF::String> pagePath(ASCIILiteral("/webkit/inspector/UserInterface/Main.html?page="));
    return pagePath;
}

bool WebInspectorServer::platformResourceForPath(const String& path, Vector<char>& data, String& contentType)
{
    // The page list contains an unformated list of pages that can be inspected with a link to open a session.
    if (path == "/pagelist.json") {
        buildPageList(data, contentType);
        return true;
    }

    // Point the default path to a formatted page that queries the page list and display them.
    String localPath = (path == "/") ? "/webkit/resources/inspectorPageIndex.html" : path;
    // All other paths are mapped directly to a resource, if possible.
    QFile file(QString::fromLatin1(":%1").arg(localPath));
    if (file.exists()) {
        file.open(QIODevice::ReadOnly);
        data.grow(file.size());
        file.read(data.data(), data.size());

        size_t extStart = localPath.reverseFind('.');
        String ext = localPath.substring(extStart != notFound ? extStart + 1 : 0);
        contentType = WebCore::MIMETypeRegistry::getMIMETypeForExtension(ext);
        return true;
    }
    return false;
}

String WebInspectorServer::inspectorUrlForPageID(int pageId)
{
    if (pageId <= 0 || serverState() == Closed)
        return String();
    StringBuilder builder;
    builder.appendLiteral("http://");
    builder.append(bindAddress());
    builder.append(':');
    builder.appendNumber(port());
    builder.append(remoteInspectorPagePath());
    builder.appendNumber(pageId);
    return builder.toString();
}

void WebInspectorServer::buildPageList(Vector<char>& data, String& contentType)
{
    StringBuilder builder;
    builder.appendLiteral("[ ");
    ClientMap::iterator end = m_clientMap.end();
    for (ClientMap::iterator it = m_clientMap.begin(); it != end; ++it) {
        WebPageProxy* webPage = it->value->inspectedPage();
        if (it != m_clientMap.begin())
            builder.appendLiteral(", ");
        builder.appendLiteral("{ \"id\": ");
        builder.appendNumber(it->key);
        builder.appendLiteral(", \"title\": \"");
        builder.append(webPage->pageLoadState().title());
        builder.appendLiteral("\", \"url\": \"");
        builder.append(webPage->pageLoadState().activeURL());
        builder.appendLiteral("\", \"inspectorUrl\": \"");
        builder.append(remoteInspectorPagePath());
        builder.appendNumber(it->key);
        builder.appendLiteral("\" }");
    }
    builder.appendLiteral(" ]");
    CString cstr = builder.toString().utf8();
    data.append(cstr.data(), cstr.length());
    contentType = "application/json; charset=utf-8";
}

}
#endif