1/* 2 * Copyright (C) 2012 Samsung Electronics Ltd. 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 THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 17 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 20 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 */ 24 25#include "config.h" 26 27#if ENABLE(INSPECTOR_SERVER) 28#include "WebInspectorServer.h" 29 30#include "WebInspectorProxy.h" 31#include "WebPageProxy.h" 32#include <WebCore/FileSystem.h> 33#include <WebCore/MIMETypeRegistry.h> 34#include <gio/gio.h> 35#include <glib.h> 36#include <wtf/gobject/GOwnPtr.h> 37#include <wtf/text/CString.h> 38#include <wtf/text/StringBuilder.h> 39 40namespace WebKit { 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 CString localPath = WebCore::fileSystemRepresentation(inspectorServerFilesPath() + ((path == "/") ? "/webinspector/inspectorPageIndex.html" : path)); 52 if (localPath.isNull()) 53 return false; 54 55 GRefPtr<GFile> file = adoptGRef(g_file_new_for_path(localPath.data())); 56 GRefPtr<GFileInfo> fileInfo = adoptGRef(g_file_query_info(file.get(), G_FILE_ATTRIBUTE_STANDARD_SIZE "," G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE, G_FILE_QUERY_INFO_NONE, 0, 0)); 57 if (!fileInfo) 58 return false; 59 60 GRefPtr<GFileInputStream> inputStream = adoptGRef(g_file_read(file.get(), 0, 0)); 61 if (!inputStream) 62 return false; 63 64 data.grow(g_file_info_get_size(fileInfo.get())); 65 if (!g_input_stream_read_all(G_INPUT_STREAM(inputStream.get()), data.data(), data.size(), 0, 0, 0)) 66 return false; 67 68 contentType = GOwnPtr<gchar>(g_file_info_get_attribute_as_string(fileInfo.get(), G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE)).get(); 69 return true; 70} 71 72void WebInspectorServer::buildPageList(Vector<char>& data, String& contentType) 73{ 74 StringBuilder builder; 75 builder.appendLiteral("[ "); 76 ClientMap::iterator end = m_clientMap.end(); 77 for (ClientMap::iterator it = m_clientMap.begin(); it != end; ++it) { 78 WebPageProxy* webPage = it->value->page(); 79 if (it != m_clientMap.begin()) 80 builder.appendLiteral(", "); 81 builder.appendLiteral("{ \"id\": "); 82 builder.appendNumber(it->key); 83 builder.appendLiteral(", \"title\": \""); 84 builder.append(webPage->pageTitle()); 85 builder.appendLiteral("\", \"url\": \""); 86 builder.append(webPage->activeURL()); 87 builder.appendLiteral("\", \"inspectorUrl\": \""); 88 builder.appendLiteral("/webinspector/inspector.html?page="); 89 builder.appendNumber(it->key); 90 builder.appendLiteral("\" }"); 91 } 92 builder.appendLiteral(" ]"); 93 CString cstr = builder.toString().utf8(); 94 data.append(cstr.data(), cstr.length()); 95 contentType = "application/json; charset=utf-8"; 96} 97 98String WebInspectorServer::inspectorServerFilesPath() 99{ 100 if (!m_inspectorServerFilesPath.isNull()) 101 return m_inspectorServerFilesPath; 102 103 const char* environmentPath = g_getenv("WEBKIT_INSPECTOR_SERVER_PATH"); 104 if (environmentPath && g_file_test(environmentPath, G_FILE_TEST_IS_DIR)) 105 m_inspectorServerFilesPath = String(environmentPath); 106 else 107 m_inspectorServerFilesPath = String(WebCore::sharedResourcesPath().data()); 108 109 return m_inspectorServerFilesPath; 110} 111 112} 113#endif 114