1 module webkit2.WebResource; 2 3 private import gio.AsyncResultIF; 4 private import gio.Cancellable; 5 private import gio.TlsCertificate; 6 private import glib.ErrorG; 7 private import glib.GException; 8 private import glib.Str; 9 private import gobject.ObjectG; 10 private import gobject.Signals; 11 private import std.algorithm; 12 private import webkit2.URIRequest; 13 private import webkit2.URIResponse; 14 private import webkit2.c.functions; 15 public import webkit2.c.types; 16 17 18 /** 19 * A #WebKitWebResource encapsulates content for each resource at the 20 * end of a particular URI. For example, one #WebKitWebResource will 21 * be created for each separate image and stylesheet when a page is 22 * loaded. 23 * 24 * You can access the response and the URI for a given 25 * #WebKitWebResource, using webkit_web_resource_get_uri() and 26 * webkit_web_resource_get_response(), as well as the raw data, using 27 * webkit_web_resource_get_data(). 28 */ 29 public class WebResource : ObjectG 30 { 31 /** the main Gtk struct */ 32 protected WebKitWebResource* webKitWebResource; 33 34 /** Get the main Gtk struct */ 35 public WebKitWebResource* getWebResourceStruct(bool transferOwnership = false) 36 { 37 if (transferOwnership) 38 ownedRef = false; 39 return webKitWebResource; 40 } 41 42 /** the main Gtk struct as a void* */ 43 protected override void* getStruct() 44 { 45 return cast(void*)webKitWebResource; 46 } 47 48 /** 49 * Sets our main struct and passes it to the parent class. 50 */ 51 public this (WebKitWebResource* webKitWebResource, bool ownedRef = false) 52 { 53 this.webKitWebResource = webKitWebResource; 54 super(cast(GObject*)webKitWebResource, ownedRef); 55 } 56 57 58 /** */ 59 public static GType getType() 60 { 61 return webkit_web_resource_get_type(); 62 } 63 64 /** 65 * Asynchronously get the raw data for @resource. 66 * 67 * When the operation is finished, @callback will be called. You can then call 68 * webkit_web_resource_get_data_finish() to get the result of the operation. 69 * 70 * Params: 71 * cancellable = a #GCancellable or %NULL to ignore 72 * callback = a #GAsyncReadyCallback to call when the request is satisfied 73 * userData = the data to pass to callback function 74 */ 75 public void getData(Cancellable cancellable, GAsyncReadyCallback callback, void* userData) 76 { 77 webkit_web_resource_get_data(webKitWebResource, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData); 78 } 79 80 /** 81 * Finish an asynchronous operation started with webkit_web_resource_get_data(). 82 * 83 * Params: 84 * result = a #GAsyncResult 85 * 86 * Returns: a 87 * string with the data of @resource, or %NULL in case of error. if @length 88 * is not %NULL, the size of the data will be assigned to it. 89 * 90 * Throws: GException on failure. 91 */ 92 public char[] getDataFinish(AsyncResultIF result) 93 { 94 size_t length; 95 GError* err = null; 96 97 auto __p = webkit_web_resource_get_data_finish(webKitWebResource, (result is null) ? null : result.getAsyncResultStruct(), &length, &err); 98 99 if (err !is null) 100 { 101 throw new GException( new ErrorG(err) ); 102 } 103 104 return cast(char[])__p[0 .. length]; 105 } 106 107 /** 108 * Retrieves the #WebKitURIResponse of the resource load operation. 109 * This method returns %NULL if called before the response 110 * is received from the server. You can connect to notify::response 111 * signal to be notified when the response is received. 112 * 113 * Returns: the #WebKitURIResponse, or %NULL if 114 * the response hasn't been received yet. 115 */ 116 public URIResponse getResponse() 117 { 118 auto __p = webkit_web_resource_get_response(webKitWebResource); 119 120 if(__p is null) 121 { 122 return null; 123 } 124 125 return ObjectG.getDObject!(URIResponse)(cast(WebKitURIResponse*) __p); 126 } 127 128 /** 129 * Returns the current active URI of @resource. The active URI might change during 130 * a load operation: 131 * 132 * <orderedlist> 133 * <listitem><para> 134 * When the resource load starts, the active URI is the requested URI 135 * </para></listitem> 136 * <listitem><para> 137 * When the initial request is sent to the server, #WebKitWebResource::sent-request 138 * signal is emitted without a redirected response, the active URI is the URI of 139 * the request sent to the server. 140 * </para></listitem> 141 * <listitem><para> 142 * In case of a server redirection, #WebKitWebResource::sent-request signal 143 * is emitted again with a redirected response, the active URI is the URI the request 144 * was redirected to. 145 * </para></listitem> 146 * <listitem><para> 147 * When the response is received from the server, the active URI is the final 148 * one and it will not change again. 149 * </para></listitem> 150 * </orderedlist> 151 * 152 * You can monitor the active URI by connecting to the notify::uri 153 * signal of @resource. 154 * 155 * Returns: the current active URI of @resource 156 */ 157 public string getUri() 158 { 159 return Str.toString(webkit_web_resource_get_uri(webKitWebResource)); 160 } 161 162 /** 163 * This signal is emitted when an error occurs during the resource 164 * load operation. 165 * 166 * Params: 167 * error = the #GError that was triggered 168 */ 169 gulong addOnFailed(void delegate(ErrorG, WebResource) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 170 { 171 return Signals.connect(this, "failed", dlg, connectFlags ^ ConnectFlags.SWAPPED); 172 } 173 174 /** 175 * This signal is emitted when a TLS error occurs during the resource load operation. 176 * 177 * Params: 178 * certificate = a #GTlsCertificate 179 * errors = a #GTlsCertificateFlags with the verification status of @certificate 180 * 181 * Since: 2.8 182 */ 183 gulong addOnFailedWithTlsErrors(void delegate(TlsCertificate, GTlsCertificateFlags, WebResource) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 184 { 185 return Signals.connect(this, "failed-with-tls-errors", dlg, connectFlags ^ ConnectFlags.SWAPPED); 186 } 187 188 /** 189 * This signal is emitted when the resource load finishes successfully 190 * or due to an error. In case of errors #WebKitWebResource::failed signal 191 * is emitted before this one. 192 */ 193 gulong addOnFinished(void delegate(WebResource) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 194 { 195 return Signals.connect(this, "finished", dlg, connectFlags ^ ConnectFlags.SWAPPED); 196 } 197 198 /** 199 * This signal is emitted after response is received, 200 * every time new data has been received. It's 201 * useful to know the progress of the resource load operation. 202 * 203 * Params: 204 * dataLength = the length of data received in bytes 205 */ 206 gulong addOnReceivedData(void delegate(ulong, WebResource) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 207 { 208 return Signals.connect(this, "received-data", dlg, connectFlags ^ ConnectFlags.SWAPPED); 209 } 210 211 /** 212 * This signal is emitted when @request has been sent to the 213 * server. In case of a server redirection this signal is 214 * emitted again with the @request argument containing the new 215 * request sent to the server due to the redirection and the 216 * @redirected_response parameter containing the response 217 * received by the server for the initial request. 218 * 219 * Params: 220 * request = a #WebKitURIRequest 221 * redirectedResponse = a #WebKitURIResponse, or %NULL 222 */ 223 gulong addOnSentRequest(void delegate(URIRequest, URIResponse, WebResource) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 224 { 225 return Signals.connect(this, "sent-request", dlg, connectFlags ^ ConnectFlags.SWAPPED); 226 } 227 }