Home | History | Annotate | Download | only in gtk
      1 /*
      2  * Copyright (C) 2007 Alp Toker <alp (at) 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 <JavaScriptCore/JSStringRef.h>
     26 #include <webkit/webkit.h>
     27 #include <string.h>
     28 
     29 // Returns a newly allocated UTF-8 character buffer which must be freed with g_free()
     30 gchar* JSStringCopyUTF8CString(JSStringRef jsString)
     31 {
     32     size_t dataSize = JSStringGetMaximumUTF8CStringSize(jsString);
     33     gchar* utf8 = (gchar*)g_malloc(dataSize);
     34     JSStringGetUTF8CString(jsString, utf8, dataSize);
     35 
     36     return utf8;
     37 }
     38 
     39 bool LoadItem::invoke() const
     40 {
     41     gchar* targetString = JSStringCopyUTF8CString(m_target.get());
     42 
     43     WebKitWebFrame* targetFrame;
     44     if (!strlen(targetString))
     45         targetFrame = mainFrame;
     46     else
     47         targetFrame = webkit_web_frame_find_frame(mainFrame, targetString);
     48     g_free(targetString);
     49 
     50     gchar* urlString = JSStringCopyUTF8CString(m_url.get());
     51     WebKitNetworkRequest* request = webkit_network_request_new(urlString);
     52     g_free(urlString);
     53     webkit_web_frame_load_request(targetFrame, request);
     54     g_object_unref(request);
     55 
     56     return true;
     57 }
     58 
     59 bool ReloadItem::invoke() const
     60 {
     61     webkit_web_frame_reload(mainFrame);
     62     return true;
     63 }
     64 
     65 bool ScriptItem::invoke() const
     66 {
     67     WebKitWebView* webView = webkit_web_frame_get_web_view(mainFrame);
     68     gchar* scriptString = JSStringCopyUTF8CString(m_script.get());
     69     webkit_web_view_execute_script(webView, scriptString);
     70     g_free(scriptString);
     71     return true;
     72 }
     73 
     74 bool BackForwardItem::invoke() const
     75 {
     76     WebKitWebView* webView = webkit_web_frame_get_web_view(mainFrame);
     77     if (m_howFar == 1)
     78         webkit_web_view_go_forward(webView);
     79     else if (m_howFar == -1)
     80         webkit_web_view_go_back(webView);
     81     else {
     82         WebKitWebBackForwardList* webBackForwardList = webkit_web_view_get_back_forward_list(webView);
     83         WebKitWebHistoryItem* item = webkit_web_back_forward_list_get_nth_item(webBackForwardList, m_howFar);
     84         webkit_web_view_go_to_back_forward_item(webView, item);
     85     }
     86     return true;
     87 }
     88