WebInspectorServerQt.cpp [plain text]
#include "config.h"
#include "WebInspectorServer.h"
#include "WebInspectorProxy.h"
#include "WebPageProxy.h"
#include <WebCore/MIMETypeRegistry.h>
#include <wtf/text/CString.h>
#include <wtf/text/StringBuilder.h>
namespace WebKit {
bool WebInspectorServer::platformResourceForPath(const String& path, Vector<char>& data, String& contentType)
{
if (path == "/pagelist.json") {
buildPageList(data, contentType);
return true;
}
String localPath = (path == "/") ? "/webkit/resources/inspectorPageIndex.html" : path;
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;
}
void WebInspectorServer::buildPageList(Vector<char>& data, String& contentType)
{
StringBuilder builder;
builder.append("[ ");
ClientMap::iterator end = m_clientMap.end();
for (ClientMap::iterator it = m_clientMap.begin(); it != end; ++it) {
WebPageProxy* webPage = it->second->page();
if (it != m_clientMap.begin())
builder.append(", ");
builder.append("{ \"id\": " + String::number(it->first));
builder.append(", \"title\": \"");
builder.append(webPage->pageTitle());
builder.append("\", \"url\": \"");
builder.append(webPage->activeURL());
builder.append("\", \"inspectorUrl\": \"");
builder.append("/webkit/inspector/inspector.html?page=" + String::number(it->first));
builder.append("\" }");
}
builder.append(" ]");
CString cstr = builder.toString().utf8();
data.append(cstr.data(), cstr.length());
contentType = "application/json; charset=utf-8";
}
}