2009-09-12 Chris Toshok <toshok@ximian.com>
[moon.git] / src / downloader.h
blob66f62d3cc200969e0b6655032c59815908022b40
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * downloader.h: Downloader class.
5 * Contact:
6 * Moonlight List (moonligt-list@lists.ximian.com)
8 * Copyright 2008 Novell, Inc. (http://www.novell.com)
10 * See the LICENSE file included with the distribution for details.
14 #ifndef __DOWNLOADER_H__
15 #define __DOWNLOADER_H__
17 #include <glib.h>
19 #include "dependencyobject.h"
20 #include "internal-downloader.h"
22 class FileDownloader;
23 class Downloader;
24 class DownloaderResponse;
26 /* @CBindingRequisite */
27 typedef void (* DownloaderResponseHeaderCallback) (gpointer context, const char *header, const char *value);
29 /* @CBindingRequisite */
30 typedef void (* DownloaderWriteFunc) (void *buf, gint32 offset, gint32 n, gpointer cb_data);
31 /* @CBindingRequisite */
32 typedef void (* DownloaderNotifySizeFunc) (gint64 size, gpointer cb_data);
34 /* @CBindingRequisite */
35 typedef gpointer (* DownloaderCreateStateFunc) (Downloader *dl);
36 /* @CBindingRequisite */
37 typedef void (* DownloaderDestroyStateFunc) (gpointer state);
38 /* @CBindingRequisite */
40 * custom_header_support:
41 * must be set to true if HeaderFunc or BodyFunc is called later
42 * disable_cache:
43 * must be set to true if there are multiple simultaneous requests to the same uri
44 * when a browser is the
46 typedef void (* DownloaderOpenFunc) (gpointer state, const char *verb, const char *uri, bool custom_header_support, bool disable_cache);
47 /* @CBindingRequisite */
48 typedef void (* DownloaderSendFunc) (gpointer state);
49 /* @CBindingRequisite */
50 typedef void (* DownloaderAbortFunc) (gpointer state);
51 /* @CBindingRequisite */
52 typedef void (* DownloaderHeaderFunc) (gpointer state, const char *header, const char *value);
53 /* @CBindingRequisite */
54 typedef void (* DownloaderBodyFunc) (gpointer state, void *body, guint32 length);
55 /* @CBindingRequisite */
56 typedef gpointer (* DownloaderCreateWebRequestFunc) (const char *method, const char *uri, gpointer context);
57 /* @CBindingRequisite */
58 typedef void (* DownloaderSetResponseHeaderCallbackFunc) (gpointer state, DownloaderResponseHeaderCallback callback, gpointer context);
59 /* @CBindingRequisite */
60 typedef DownloaderResponse * (* DownloaderGetResponseFunc) (gpointer state);
62 // Reference: URL Access Restrictions in Silverlight 2
63 // http://msdn.microsoft.com/en-us/library/cc189008(VS.95).aspx
64 enum DownloaderAccessPolicy {
65 DownloaderPolicy,
66 MediaPolicy,
67 XamlPolicy,
68 FontPolicy,
69 StreamingPolicy,
70 MsiPolicy,
71 NoPolicy
74 /* @Namespace=None */
75 /* @ManagedDependencyProperties=None */
76 class Downloader : public DependencyObject {
77 static DownloaderCreateStateFunc create_state;
78 static DownloaderDestroyStateFunc destroy_state;
79 static DownloaderOpenFunc open_func;
80 static DownloaderSendFunc send_func;
81 static DownloaderAbortFunc abort_func;
82 static DownloaderHeaderFunc header_func;
83 static DownloaderBodyFunc body_func;
84 static DownloaderCreateWebRequestFunc request_func;
85 static DownloaderSetResponseHeaderCallbackFunc set_response_header_callback_func;
86 static DownloaderGetResponseFunc get_response_func;
88 // Set by the consumer
89 DownloaderNotifySizeFunc notify_size;
90 DownloaderWriteFunc writer;
91 gpointer user_data;
93 // Set by the supplier.
94 gpointer downloader_state;
96 gpointer context;
98 gint64 file_size;
99 gint64 total;
101 char *filename;
102 char *buffer;
104 char *failed_msg;
106 int send_queued:1;
107 int completed:1;
108 int started:1;
109 int aborted:1;
110 int custom_header_support:1;
111 int disable_cache:1;
113 InternalDownloader *internal_dl;
115 DownloaderAccessPolicy access_policy;
117 protected:
118 virtual ~Downloader ();
120 void SetStatusText (const char *text);
121 void SetStatus (int status);
123 public:
124 /* @PropertyType=double,DefaultValue=0.0,GenerateAccessors */
125 const static int DownloadProgressProperty;
126 /* @PropertyType=string */
127 const static int ResponseTextProperty;
128 /* @PropertyType=gint32,DefaultValue=0,GenerateAccessors */
129 const static int StatusProperty;
130 /* @PropertyType=string,DefaultValue=\"\",GenerateAccessors */
131 const static int StatusTextProperty;
132 /* @PropertyType=Uri,GenerateAccessors */
133 const static int UriProperty;
135 // Events you can AddHandler to
136 const static int CompletedEvent;
137 const static int DownloadProgressChangedEvent;
138 const static int DownloadFailedEvent;
140 /* @GenerateCBinding,GeneratePInvoke */
141 Downloader ();
143 void Abort ();
144 char *GetResponseText (const char *Partname, gint64 *size);
145 char *GetDownloadedFilename (const char *partname);
146 void Open (const char *verb, const char *uri, DownloaderAccessPolicy policy);
147 void Open (const char *verb, Uri *uri, DownloaderAccessPolicy policy);
148 void SendInternal ();
149 void Send ();
150 void SendNow ();
152 // the following is stuff not exposed by C#/js, but is useful
153 // when writing unmanaged code for downloader implementations
154 // or data sinks.
156 void OpenInitialize ();
157 void InternalAbort ();
158 void InternalWrite (void *buf, gint32 offset, gint32 n);
159 void InternalOpen (const char *verb, const char *uri);
160 void InternalSetHeader (const char *header, const char *value);
161 void InternalSetHeaderFormatted (const char *header, char *value); // calls g_free on the value
162 void InternalSetBody (void *body, guint32 length);
164 /* @GenerateCBinding,GeneratePInvoke */
165 void Write (void *buf, gint32 offset, gint32 n);
167 /* @GenerateCBinding,GeneratePInvoke */
168 void NotifyFinished (const char *final_uri);
170 /* @GenerateCBinding,GeneratePInvoke */
171 void NotifyFailed (const char *msg);
173 /* @GenerateCBinding,GeneratePInvoke */
174 void NotifySize (gint64 size);
176 bool CheckRedirectionPolicy (const char *url);
178 void SetFilename (const char *fname);
179 char *GetBuffer () { return buffer; }
180 gint64 GetSize () { return total; }
182 InternalDownloader *GetInternalDownloader () { return internal_dl; }
184 // This is called by the consumer of the downloaded data (the
185 // Image class for instance)
186 void SetStreamFunctions (DownloaderWriteFunc writer,
187 DownloaderNotifySizeFunc notify_size,
188 gpointer user_data);
190 // This is called by the supplier of the downloaded data (the
191 // managed framework, the browser plugin, the demo test)
193 /* @GenerateCBinding,GeneratePInvoke */
194 static void SetFunctions (DownloaderCreateStateFunc create_state,
195 DownloaderDestroyStateFunc destroy_state,
196 DownloaderOpenFunc open,
197 DownloaderSendFunc send,
198 DownloaderAbortFunc abort,
199 DownloaderHeaderFunc header,
200 DownloaderBodyFunc body,
201 DownloaderCreateWebRequestFunc request,
202 DownloaderSetResponseHeaderCallbackFunc response_header_callback,
203 DownloaderGetResponseFunc get_response
206 bool Started ();
207 bool Completed ();
208 bool IsAborted () { return aborted; }
209 const char *GetFailedMessage () { return failed_msg; }
211 void SetRequireCustomHeaderSupport (bool value) { custom_header_support = value; }
212 bool GetRequireCustomHeaderSupport () { return custom_header_support; }
213 void SetDisableCache (bool value) { disable_cache = value; }
214 bool GetDisableCache () { return disable_cache; }
216 void SetContext (gpointer context) { this->context = context;}
217 gpointer GetContext () { return context; }
218 gpointer GetDownloaderState () { return downloader_state; }
220 DownloaderCreateWebRequestFunc GetRequestFunc () {return request_func; }
221 /* @GenerateCBinding,GeneratePInvoke */
222 void *CreateWebRequest (const char *method, const char *uri);
223 void SetResponseHeaderCallback (DownloaderResponseHeaderCallback callback, gpointer context);
225 DownloaderResponse * GetResponse ();
227 // Property Accessors
229 void SetDownloadProgress (double progress);
230 double GetDownloadProgress ();
232 const char *GetStatusText ();
233 int GetStatus ();
235 void SetUri (Uri *uri);
236 Uri *GetUri ();
239 class DownloaderResponse;
240 class DownloaderRequest;
242 /* @CBindingRequisite */
243 typedef guint32 (* DownloaderResponseStartedHandler) (DownloaderResponse *response, gpointer context);
244 /* @CBindingRequisite */
245 typedef guint32 (* DownloaderResponseDataAvailableHandler) (DownloaderResponse *response, gpointer context, char *buffer, guint32 length);
246 /* @CBindingRequisite */
247 typedef guint32 (* DownloaderResponseFinishedHandler) (DownloaderResponse *response, gpointer context, bool success, gpointer data, const char *uri);
249 class IDownloader {
250 private:
251 Deployment *deployment;
253 public:
254 virtual ~IDownloader () {};
256 virtual void Abort () = 0;
257 virtual const bool IsAborted () = 0;
258 Deployment *GetDeployment () { return deployment; }
259 void SetDeployment (Deployment *deployment) { this->deployment = deployment; }
262 class DownloaderResponse : public IDownloader {
263 protected:
264 DownloaderResponseStartedHandler started;
265 DownloaderResponseDataAvailableHandler available;
266 DownloaderResponseFinishedHandler finished;
267 gpointer context;
268 DownloaderRequest *request;
269 bool aborted;
271 public:
272 DownloaderResponse ();
273 DownloaderResponse (DownloaderResponseStartedHandler started, DownloaderResponseDataAvailableHandler available, DownloaderResponseFinishedHandler finished, gpointer context);
274 /* @GenerateCBinding,GeneratePInvoke */
275 virtual ~DownloaderResponse ();
277 /* @GenerateCBinding,GeneratePInvoke */
278 virtual void Abort () = 0;
279 virtual const bool IsAborted () { return this->aborted; }
280 /* @GenerateCBinding,GeneratePInvoke */
281 virtual void SetHeaderVisitor (DownloaderResponseHeaderCallback visitor, gpointer context) = 0;
282 /* @GenerateCBinding,GeneratePInvoke */
283 virtual int GetResponseStatus () = 0;
284 /* @GenerateCBinding,GeneratePInvoke */
285 virtual const char * GetResponseStatusText () = 0;
286 DownloaderRequest *GetDownloaderRequest () { return request; }
287 void SetDownloaderRequest (DownloaderRequest *value) { request = value; }
289 virtual void ref () = 0;
290 virtual void unref () = 0;
293 class DownloaderRequest : public IDownloader {
294 protected:
295 DownloaderResponse *response;
296 char *uri;
297 char *method;
299 bool aborted;
301 public:
302 DownloaderRequest (const char *method, const char *uri);
303 /* @GenerateCBinding,GeneratePInvoke */
304 virtual ~DownloaderRequest ();
306 /* @GenerateCBinding,GeneratePInvoke */
307 virtual void Abort () = 0;
308 /* @GenerateCBinding,GeneratePInvoke */
309 virtual bool GetResponse (DownloaderResponseStartedHandler started, DownloaderResponseDataAvailableHandler available, DownloaderResponseFinishedHandler finished, gpointer context) = 0;
310 /* @GenerateCBinding,GeneratePInvoke */
311 virtual const bool IsAborted () { return this->aborted; }
312 /* @GenerateCBinding,GeneratePInvoke */
313 virtual void SetHttpHeader (const char *name, const char *value) = 0;
314 /* @GenerateCBinding,GeneratePInvoke */
315 virtual void SetBody (/* @MarshalAs=byte[] */ void *body, int size) = 0;
316 /* @GenerateCBinding,GeneratePInvoke */
317 DownloaderResponse *GetDownloaderResponse () { return response; }
318 void SetDownloaderResponse (DownloaderResponse *value) { response = value; }
321 G_BEGIN_DECLS
323 void downloader_init (void);
325 G_END_DECLS
327 #endif