2009-08-26 Chris Toshok <toshok@ximian.com>
[moon.git] / src / downloader.h
blob2aaaea8acbcdd100f82642f85b18561782869c99
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 NoPolicy
73 /* @Namespace=None */
74 /* @ManagedDependencyProperties=None */
75 class Downloader : public DependencyObject {
76 static DownloaderCreateStateFunc create_state;
77 static DownloaderDestroyStateFunc destroy_state;
78 static DownloaderOpenFunc open_func;
79 static DownloaderSendFunc send_func;
80 static DownloaderAbortFunc abort_func;
81 static DownloaderHeaderFunc header_func;
82 static DownloaderBodyFunc body_func;
83 static DownloaderCreateWebRequestFunc request_func;
84 static DownloaderSetResponseHeaderCallbackFunc set_response_header_callback_func;
85 static DownloaderGetResponseFunc get_response_func;
87 // Set by the consumer
88 DownloaderNotifySizeFunc notify_size;
89 DownloaderWriteFunc writer;
90 gpointer user_data;
92 // Set by the supplier.
93 gpointer downloader_state;
95 gpointer context;
97 gint64 file_size;
98 gint64 total;
100 char *filename;
101 char *buffer;
103 char *failed_msg;
105 int send_queued:1;
106 int completed:1;
107 int started:1;
108 int aborted:1;
109 int custom_header_support:1;
110 int disable_cache:1;
112 InternalDownloader *internal_dl;
114 DownloaderAccessPolicy access_policy;
116 protected:
117 virtual ~Downloader ();
119 void SetStatusText (const char *text);
120 void SetStatus (int status);
122 public:
123 /* @PropertyType=double,DefaultValue=0.0,GenerateAccessors */
124 const static int DownloadProgressProperty;
125 /* @PropertyType=string */
126 const static int ResponseTextProperty;
127 /* @PropertyType=gint32,DefaultValue=0,GenerateAccessors */
128 const static int StatusProperty;
129 /* @PropertyType=string,DefaultValue=\"\",GenerateAccessors */
130 const static int StatusTextProperty;
131 /* @PropertyType=Uri,GenerateAccessors */
132 const static int UriProperty;
134 // Events you can AddHandler to
135 const static int CompletedEvent;
136 const static int DownloadProgressChangedEvent;
137 const static int DownloadFailedEvent;
139 /* @GenerateCBinding,GeneratePInvoke */
140 Downloader ();
142 void Abort ();
143 char *GetResponseText (const char *Partname, gint64 *size);
144 char *GetDownloadedFilename (const char *partname);
145 void Open (const char *verb, const char *uri, DownloaderAccessPolicy policy);
146 void Open (const char *verb, Uri *uri, DownloaderAccessPolicy policy);
147 void SendInternal ();
148 void Send ();
149 void SendNow ();
151 // the following is stuff not exposed by C#/js, but is useful
152 // when writing unmanaged code for downloader implementations
153 // or data sinks.
155 void OpenInitialize ();
156 void InternalAbort ();
157 void InternalWrite (void *buf, gint32 offset, gint32 n);
158 void InternalOpen (const char *verb, const char *uri);
159 void InternalSetHeader (const char *header, const char *value);
160 void InternalSetHeaderFormatted (const char *header, char *value); // calls g_free on the value
161 void InternalSetBody (void *body, guint32 length);
163 /* @GenerateCBinding,GeneratePInvoke */
164 void Write (void *buf, gint32 offset, gint32 n);
166 /* @GenerateCBinding,GeneratePInvoke */
167 void NotifyFinished (const char *final_uri);
169 /* @GenerateCBinding,GeneratePInvoke */
170 void NotifyFailed (const char *msg);
172 /* @GenerateCBinding,GeneratePInvoke */
173 void NotifySize (gint64 size);
175 bool CheckRedirectionPolicy (const char *url);
177 void SetFilename (const char *fname);
178 char *GetBuffer () { return buffer; }
179 gint64 GetSize () { return total; }
181 InternalDownloader *GetInternalDownloader () { return internal_dl; }
183 // This is called by the consumer of the downloaded data (the
184 // Image class for instance)
185 void SetStreamFunctions (DownloaderWriteFunc writer,
186 DownloaderNotifySizeFunc notify_size,
187 gpointer user_data);
189 // This is called by the supplier of the downloaded data (the
190 // managed framework, the browser plugin, the demo test)
192 /* @GenerateCBinding,GeneratePInvoke */
193 static void SetFunctions (DownloaderCreateStateFunc create_state,
194 DownloaderDestroyStateFunc destroy_state,
195 DownloaderOpenFunc open,
196 DownloaderSendFunc send,
197 DownloaderAbortFunc abort,
198 DownloaderHeaderFunc header,
199 DownloaderBodyFunc body,
200 DownloaderCreateWebRequestFunc request,
201 DownloaderSetResponseHeaderCallbackFunc response_header_callback,
202 DownloaderGetResponseFunc get_response
205 bool Started ();
206 bool Completed ();
207 bool IsAborted () { return aborted; }
208 const char *GetFailedMessage () { return failed_msg; }
210 void SetRequireCustomHeaderSupport (bool value) { custom_header_support = value; }
211 bool GetRequireCustomHeaderSupport () { return custom_header_support; }
212 void SetDisableCache (bool value) { disable_cache = value; }
213 bool GetDisableCache () { return disable_cache; }
215 void SetContext (gpointer context) { this->context = context;}
216 gpointer GetContext () { return context; }
217 gpointer GetDownloaderState () { return downloader_state; }
219 DownloaderCreateWebRequestFunc GetRequestFunc () {return request_func; }
220 /* @GenerateCBinding,GeneratePInvoke */
221 void *CreateWebRequest (const char *method, const char *uri);
222 void SetResponseHeaderCallback (DownloaderResponseHeaderCallback callback, gpointer context);
224 DownloaderResponse * GetResponse ();
226 // Property Accessors
228 void SetDownloadProgress (double progress);
229 double GetDownloadProgress ();
231 const char *GetStatusText ();
232 int GetStatus ();
234 void SetUri (Uri *uri);
235 Uri *GetUri ();
238 class DownloaderResponse;
239 class DownloaderRequest;
241 /* @CBindingRequisite */
242 typedef guint32 (* DownloaderResponseStartedHandler) (DownloaderResponse *response, gpointer context);
243 /* @CBindingRequisite */
244 typedef guint32 (* DownloaderResponseDataAvailableHandler) (DownloaderResponse *response, gpointer context, char *buffer, guint32 length);
245 /* @CBindingRequisite */
246 typedef guint32 (* DownloaderResponseFinishedHandler) (DownloaderResponse *response, gpointer context, bool success, gpointer data, const char *uri);
248 class IDownloader {
249 private:
250 Deployment *deployment;
252 public:
253 virtual void Abort () = 0;
254 virtual const bool IsAborted () = 0;
255 Deployment *GetDeployment () { return deployment; }
256 void SetDeployment (Deployment *deployment) { this->deployment = deployment; }
259 class DownloaderResponse : public IDownloader {
260 protected:
261 DownloaderResponseStartedHandler started;
262 DownloaderResponseDataAvailableHandler available;
263 DownloaderResponseFinishedHandler finished;
264 gpointer context;
265 DownloaderRequest *request;
266 bool aborted;
268 public:
269 DownloaderResponse ();
270 DownloaderResponse (DownloaderResponseStartedHandler started, DownloaderResponseDataAvailableHandler available, DownloaderResponseFinishedHandler finished, gpointer context);
271 /* @GenerateCBinding,GeneratePInvoke */
272 virtual ~DownloaderResponse ();
274 /* @GenerateCBinding,GeneratePInvoke */
275 virtual void Abort () = 0;
276 virtual const bool IsAborted () { return this->aborted; }
277 /* @GenerateCBinding,GeneratePInvoke */
278 virtual void SetHeaderVisitor (DownloaderResponseHeaderCallback visitor, gpointer context) = 0;
279 /* @GenerateCBinding,GeneratePInvoke */
280 virtual int GetResponseStatus () = 0;
281 /* @GenerateCBinding,GeneratePInvoke */
282 virtual const char * GetResponseStatusText () = 0;
283 DownloaderRequest *GetDownloaderRequest () { return request; }
284 void SetDownloaderRequest (DownloaderRequest *value) { request = value; }
286 virtual void ref () = 0;
287 virtual void unref () = 0;
290 class DownloaderRequest : public IDownloader {
291 protected:
292 DownloaderResponse *response;
293 char *uri;
294 char *method;
296 bool aborted;
298 public:
299 DownloaderRequest (const char *method, const char *uri);
300 /* @GenerateCBinding,GeneratePInvoke */
301 virtual ~DownloaderRequest ();
303 /* @GenerateCBinding,GeneratePInvoke */
304 virtual void Abort () = 0;
305 /* @GenerateCBinding,GeneratePInvoke */
306 virtual bool GetResponse (DownloaderResponseStartedHandler started, DownloaderResponseDataAvailableHandler available, DownloaderResponseFinishedHandler finished, gpointer context) = 0;
307 /* @GenerateCBinding,GeneratePInvoke */
308 virtual const bool IsAborted () { return this->aborted; }
309 /* @GenerateCBinding,GeneratePInvoke */
310 virtual void SetHttpHeader (const char *name, const char *value) = 0;
311 /* @GenerateCBinding,GeneratePInvoke */
312 virtual void SetBody (/* @MarshalAs=byte[] */ void *body, int size) = 0;
313 /* @GenerateCBinding,GeneratePInvoke */
314 DownloaderResponse *GetDownloaderResponse () { return response; }
315 void SetDownloaderResponse (DownloaderResponse *value) { response = value; }
318 G_BEGIN_DECLS
320 void downloader_init (void);
322 G_END_DECLS
324 #endif