1/*
2 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
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 library 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 library; 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#include "config.h"
21#include "WorkQueueItem.h"
22
23#include "DumpRenderTree.h"
24
25#include <GOwnPtr.h>
26#include <JavaScriptCore/JSStringRef.h>
27#include <webkit/webkit.h>
28#include <string.h>
29
30// Returns a newly allocated UTF-8 character buffer which must be freed with g_free()
31gchar* JSStringCopyUTF8CString(JSStringRef jsString)
32{
33    size_t dataSize = JSStringGetMaximumUTF8CStringSize(jsString);
34    gchar* utf8 = (gchar*)g_malloc(dataSize);
35    JSStringGetUTF8CString(jsString, utf8, dataSize);
36
37    return utf8;
38}
39
40bool LoadItem::invoke() const
41{
42    gchar* targetString = JSStringCopyUTF8CString(m_target.get());
43
44    WebKitWebFrame* targetFrame;
45    if (!strlen(targetString))
46        targetFrame = mainFrame;
47    else
48        targetFrame = webkit_web_frame_find_frame(mainFrame, targetString);
49    g_free(targetString);
50
51    gchar* urlString = JSStringCopyUTF8CString(m_url.get());
52    WebKitNetworkRequest* request = webkit_network_request_new(urlString);
53    g_free(urlString);
54    webkit_web_frame_load_request(targetFrame, request);
55    g_object_unref(request);
56
57    return true;
58}
59
60bool LoadHTMLStringItem::invoke() const
61{
62    GOwnPtr<gchar> content(JSStringCopyUTF8CString(m_content.get()));
63    GOwnPtr<gchar> baseURL(JSStringCopyUTF8CString(m_baseURL.get()));
64
65    if (m_unreachableURL) {
66        GOwnPtr<gchar> unreachableURL(JSStringCopyUTF8CString(m_unreachableURL.get()));
67        webkit_web_frame_load_alternate_string(mainFrame, content.get(), baseURL.get(), unreachableURL.get());
68        return true;
69    }
70    webkit_web_frame_load_string(mainFrame, content.get(), 0, 0, baseURL.get());
71    return true;
72}
73
74bool ReloadItem::invoke() const
75{
76    webkit_web_frame_reload(mainFrame);
77    return true;
78}
79
80bool ScriptItem::invoke() const
81{
82    WebKitWebView* webView = webkit_web_frame_get_web_view(mainFrame);
83    gchar* scriptString = JSStringCopyUTF8CString(m_script.get());
84    webkit_web_view_execute_script(webView, scriptString);
85    g_free(scriptString);
86    return true;
87}
88
89bool BackForwardItem::invoke() const
90{
91    WebKitWebView* webView = webkit_web_frame_get_web_view(mainFrame);
92    if (m_howFar == 1)
93        webkit_web_view_go_forward(webView);
94    else if (m_howFar == -1)
95        webkit_web_view_go_back(webView);
96    else {
97        WebKitWebBackForwardList* webBackForwardList = webkit_web_view_get_back_forward_list(webView);
98        WebKitWebHistoryItem* item = webkit_web_back_forward_list_get_nth_item(webBackForwardList, m_howFar);
99        webkit_web_view_go_to_back_forward_item(webView, item);
100    }
101    return true;
102}
103