1/*
2 * Copyright (C) 2012 Igalia S.L.
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 "WebKitJavascriptResult.h"
22
23#include "WebKitJavascriptResultPrivate.h"
24#include "WebSerializedScriptValue.h"
25#include <wtf/gobject/GRefPtr.h>
26
27using namespace WebKit;
28
29struct _WebKitJavascriptResult {
30    _WebKitJavascriptResult(WebKitWebView* view, WebSerializedScriptValue* serializedScriptValue)
31        : webView(view)
32        , referenceCount(1)
33    {
34        value = serializedScriptValue->deserialize(webkit_web_view_get_javascript_global_context(view), 0);
35    }
36
37    GRefPtr<WebKitWebView> webView;
38    JSValueRef value;
39
40    int referenceCount;
41};
42
43G_DEFINE_BOXED_TYPE(WebKitJavascriptResult, webkit_javascript_result, webkit_javascript_result_ref, webkit_javascript_result_unref)
44
45WebKitJavascriptResult* webkitJavascriptResultCreate(WebKitWebView* webView, WebSerializedScriptValue* serializedScriptValue)
46{
47    WebKitJavascriptResult* result = g_slice_new(WebKitJavascriptResult);
48    new (result) WebKitJavascriptResult(webView, serializedScriptValue);
49    return result;
50}
51
52/**
53 * webkit_javascript_result_ref:
54 * @js_result: a #WebKitJavascriptResult
55 *
56 * Atomically increments the reference count of @js_result by one. This
57 * function is MT-safe and may be called from any thread.
58 *
59 * Returns: The passed in #WebKitJavascriptResult
60 */
61WebKitJavascriptResult* webkit_javascript_result_ref(WebKitJavascriptResult* javascriptResult)
62{
63    g_atomic_int_inc(&javascriptResult->referenceCount);
64    return javascriptResult;
65}
66
67/**
68 * webkit_javascript_result_unref:
69 * @js_result: a #WebKitJavascriptResult
70 *
71 * Atomically decrements the reference count of @js_result by one. If the
72 * reference count drops to 0, all memory allocated by the #WebKitJavascriptResult is
73 * released. This function is MT-safe and may be called from any
74 * thread.
75 */
76void webkit_javascript_result_unref(WebKitJavascriptResult* javascriptResult)
77{
78    if (g_atomic_int_dec_and_test(&javascriptResult->referenceCount)) {
79        javascriptResult->~WebKitJavascriptResult();
80        g_slice_free(WebKitJavascriptResult, javascriptResult);
81    }
82}
83
84/**
85 * webkit_javascript_result_get_global_context:
86 * @js_result: a #WebKitJavascriptResult
87 *
88 * Get the global Javascript context that should be used with the
89 * <function>JSValueRef</function> returned by webkit_javascript_result_get_value().
90 *
91 * Returns: the <function>JSGlobalContextRef</function> for the #WebKitJavascriptResult
92 */
93JSGlobalContextRef webkit_javascript_result_get_global_context(WebKitJavascriptResult* javascriptResult)
94{
95    return webkit_web_view_get_javascript_global_context(javascriptResult->webView.get());
96}
97
98/**
99 * webkit_javascript_result_get_value:
100 * @js_result: a #WebKitJavascriptResult
101 *
102 * Get the value of @js_result. You should use the <function>JSGlobalContextRef</function>
103 * returned by webkit_javascript_result_get_global_context() to use the <function>JSValueRef</function>.
104 *
105 * Returns: the <function>JSValueRef</function> of the #WebKitJavascriptResult
106 */
107JSValueRef webkit_javascript_result_get_value(WebKitJavascriptResult* javascriptResult)
108{
109    return javascriptResult->value;
110}
111