1 | /* |
2 | * This file is part of the WebKit open source project. |
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 "WebKitDOMBlob.h" |
22 | |
23 | #include <WebCore/CSSImportRule.h> |
24 | #include "DOMObjectCache.h" |
25 | #include <WebCore/Document.h> |
26 | #include <WebCore/JSExecState.h> |
27 | #include "WebKitDOMBlobPrivate.h" |
28 | #include "WebKitDOMPrivate.h" |
29 | #include "ConvertToUTF8String.h" |
30 | #include <wtf/GetPtr.h> |
31 | #include <wtf/RefPtr.h> |
32 | |
33 | #define WEBKIT_DOM_BLOB_GET_PRIVATE(obj) G_TYPE_INSTANCE_GET_PRIVATE(obj, WEBKIT_DOM_TYPE_BLOB, WebKitDOMBlobPrivate) |
34 | |
35 | typedef struct _WebKitDOMBlobPrivate { |
36 | RefPtr<WebCore::Blob> coreObject; |
37 | } WebKitDOMBlobPrivate; |
38 | |
39 | G_GNUC_BEGIN_IGNORE_DEPRECATIONS; |
40 | |
41 | namespace WebKit { |
42 | |
43 | WebKitDOMBlob* kit(WebCore::Blob* obj) |
44 | { |
45 | if (!obj) |
46 | return 0; |
47 | |
48 | if (gpointer ret = DOMObjectCache::get(obj)) |
49 | return WEBKIT_DOM_BLOB(ret); |
50 | |
51 | return wrap(obj); |
52 | } |
53 | |
54 | WebCore::Blob* core(WebKitDOMBlob* request) |
55 | { |
56 | return request ? static_cast<WebCore::Blob*>(WEBKIT_DOM_OBJECT(request)->coreObject) : 0; |
57 | } |
58 | |
59 | WebKitDOMBlob* wrapBlob(WebCore::Blob* coreObject) |
60 | { |
61 | ASSERT(coreObject); |
62 | return WEBKIT_DOM_BLOB(g_object_new(WEBKIT_DOM_TYPE_BLOB, "core-object" , coreObject, nullptr)); |
63 | } |
64 | |
65 | } // namespace WebKit |
66 | |
67 | G_DEFINE_TYPE(WebKitDOMBlob, webkit_dom_blob, WEBKIT_DOM_TYPE_OBJECT) |
68 | |
69 | enum { |
70 | PROP_0, |
71 | PROP_SIZE, |
72 | }; |
73 | |
74 | static void webkit_dom_blob_finalize(GObject* object) |
75 | { |
76 | WebKitDOMBlobPrivate* priv = WEBKIT_DOM_BLOB_GET_PRIVATE(object); |
77 | |
78 | WebKit::DOMObjectCache::forget(priv->coreObject.get()); |
79 | |
80 | priv->~WebKitDOMBlobPrivate(); |
81 | G_OBJECT_CLASS(webkit_dom_blob_parent_class)->finalize(object); |
82 | } |
83 | |
84 | static void webkit_dom_blob_get_property(GObject* object, guint propertyId, GValue* value, GParamSpec* pspec) |
85 | { |
86 | WebKitDOMBlob* self = WEBKIT_DOM_BLOB(object); |
87 | |
88 | switch (propertyId) { |
89 | case PROP_SIZE: |
90 | g_value_set_uint64(value, webkit_dom_blob_get_size(self)); |
91 | break; |
92 | default: |
93 | G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec); |
94 | break; |
95 | } |
96 | } |
97 | |
98 | static GObject* webkit_dom_blob_constructor(GType type, guint constructPropertiesCount, GObjectConstructParam* constructProperties) |
99 | { |
100 | GObject* object = G_OBJECT_CLASS(webkit_dom_blob_parent_class)->constructor(type, constructPropertiesCount, constructProperties); |
101 | |
102 | WebKitDOMBlobPrivate* priv = WEBKIT_DOM_BLOB_GET_PRIVATE(object); |
103 | priv->coreObject = static_cast<WebCore::Blob*>(WEBKIT_DOM_OBJECT(object)->coreObject); |
104 | WebKit::DOMObjectCache::put(priv->coreObject.get(), object); |
105 | |
106 | return object; |
107 | } |
108 | |
109 | static void webkit_dom_blob_class_init(WebKitDOMBlobClass* requestClass) |
110 | { |
111 | GObjectClass* gobjectClass = G_OBJECT_CLASS(requestClass); |
112 | g_type_class_add_private(gobjectClass, sizeof(WebKitDOMBlobPrivate)); |
113 | gobjectClass->constructor = webkit_dom_blob_constructor; |
114 | gobjectClass->finalize = webkit_dom_blob_finalize; |
115 | gobjectClass->get_property = webkit_dom_blob_get_property; |
116 | |
117 | g_object_class_install_property( |
118 | gobjectClass, |
119 | PROP_SIZE, |
120 | g_param_spec_uint64( |
121 | "size" , |
122 | "Blob:size" , |
123 | "read-only guint64 Blob:size" , |
124 | 0, G_MAXUINT64, 0, |
125 | WEBKIT_PARAM_READABLE)); |
126 | } |
127 | |
128 | static void webkit_dom_blob_init(WebKitDOMBlob* request) |
129 | { |
130 | WebKitDOMBlobPrivate* priv = WEBKIT_DOM_BLOB_GET_PRIVATE(request); |
131 | new (priv) WebKitDOMBlobPrivate(); |
132 | } |
133 | |
134 | guint64 webkit_dom_blob_get_size(WebKitDOMBlob* self) |
135 | { |
136 | WebCore::JSMainThreadNullState state; |
137 | g_return_val_if_fail(WEBKIT_DOM_IS_BLOB(self), 0); |
138 | WebCore::Blob* item = WebKit::core(self); |
139 | guint64 result = item->size(); |
140 | return result; |
141 | } |
142 | G_GNUC_END_IGNORE_DEPRECATIONS; |
143 | |