2009-11-05 Jeffrey Stedfast <fejj@novell.com>
[moon.git] / src / downloader.h
blobfaadf6ff16102d531cbcd591118070c3acb43bae
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 /* @ManagedEvents=Manual */
77 class Downloader : public DependencyObject {
78 static DownloaderCreateStateFunc create_state;
79 static DownloaderDestroyStateFunc destroy_state;
80 static DownloaderOpenFunc open_func;
81 static DownloaderSendFunc send_func;
82 static DownloaderAbortFunc abort_func;
83 static DownloaderHeaderFunc header_func;
84 static DownloaderBodyFunc body_func;
85 static DownloaderCreateWebRequestFunc request_func;
86 static DownloaderSetResponseHeaderCallbackFunc set_response_header_callback_func;
87 static DownloaderGetResponseFunc get_response_func;
89 // Set by the consumer
90 DownloaderNotifySizeFunc notify_size;
91 DownloaderWriteFunc writer;
92 gpointer user_data;
94 // Set by the supplier.
95 gpointer downloader_state;
97 gpointer context;
99 gint64 file_size;
100 gint64 total;
102 char *filename;
103 char *buffer;
105 char *failed_msg;
107 int send_queued:1;
108 int completed:1;
109 int started:1;
110 int aborted:1;
111 int custom_header_support:1;
112 int disable_cache:1;
114 InternalDownloader *internal_dl;
116 DownloaderAccessPolicy access_policy;
118 protected:
119 virtual ~Downloader ();
121 void SetStatusText (const char *text);
122 void SetStatus (int status);
124 public:
125 /* @PropertyType=double,DefaultValue=0.0,GenerateAccessors */
126 const static int DownloadProgressProperty;
127 /* @PropertyType=string */
128 const static int ResponseTextProperty;
129 /* @PropertyType=gint32,DefaultValue=0,GenerateAccessors */
130 const static int StatusProperty;
131 /* @PropertyType=string,DefaultValue=\"\",GenerateAccessors */
132 const static int StatusTextProperty;
133 /* @PropertyType=Uri,GenerateAccessors */
134 const static int UriProperty;
136 // Events you can AddHandler to
137 const static int CompletedEvent;
138 const static int DownloadProgressChangedEvent;
139 const static int DownloadFailedEvent;
141 /* @GenerateCBinding,GeneratePInvoke */
142 Downloader ();
144 static bool ValidateDownloadPolicy (const char *location, Uri *uri, DownloaderAccessPolicy policy);
146 void Abort ();
147 char *GetResponseText (const char *Partname, gint64 *size);
148 char *GetDownloadedFilename (const char *partname);
149 void Open (const char *verb, const char *uri, DownloaderAccessPolicy policy);
150 void Open (const char *verb, Uri *uri, DownloaderAccessPolicy policy);
151 void SendInternal ();
152 void Send ();
153 void SendNow ();
155 // the following is stuff not exposed by C#/js, but is useful
156 // when writing unmanaged code for downloader implementations
157 // or data sinks.
159 void OpenInitialize ();
160 void InternalAbort ();
161 void InternalWrite (void *buf, gint32 offset, gint32 n);
162 void InternalOpen (const char *verb, const char *uri);
163 void InternalSetHeader (const char *header, const char *value);
164 void InternalSetHeaderFormatted (const char *header, char *value); // calls g_free on the value
165 void InternalSetBody (void *body, guint32 length);
167 /* @GenerateCBinding,GeneratePInvoke */
168 void Write (void *buf, gint32 offset, gint32 n);
170 /* @GenerateCBinding,GeneratePInvoke */
171 void NotifyFinished (const char *final_uri);
173 /* @GenerateCBinding,GeneratePInvoke */
174 void NotifyFailed (const char *msg);
176 /* @GenerateCBinding,GeneratePInvoke */
177 void NotifySize (gint64 size);
179 bool CheckRedirectionPolicy (const char *url);
181 void SetFilename (const char *fname);
182 char *GetBuffer () { return buffer; }
183 gint64 GetSize () { return total; }
185 InternalDownloader *GetInternalDownloader () { return internal_dl; }
187 // This is called by the consumer of the downloaded data (the
188 // Image class for instance)
189 void SetStreamFunctions (DownloaderWriteFunc writer,
190 DownloaderNotifySizeFunc notify_size,
191 gpointer user_data);
193 // This is called by the supplier of the downloaded data (the
194 // managed framework, the browser plugin, the demo test)
196 /* @GenerateCBinding,GeneratePInvoke */
197 static void SetFunctions (DownloaderCreateStateFunc create_state,
198 DownloaderDestroyStateFunc destroy_state,
199 DownloaderOpenFunc open,
200 DownloaderSendFunc send,
201 DownloaderAbortFunc abort,
202 DownloaderHeaderFunc header,
203 DownloaderBodyFunc body,
204 DownloaderCreateWebRequestFunc request,
205 DownloaderSetResponseHeaderCallbackFunc response_header_callback,
206 DownloaderGetResponseFunc get_response
209 bool Started ();
210 bool Completed ();
211 bool IsAborted () { return aborted; }
212 const char *GetFailedMessage () { return failed_msg; }
214 void SetRequireCustomHeaderSupport (bool value) { custom_header_support = value; }
215 bool GetRequireCustomHeaderSupport () { return custom_header_support; }
216 void SetDisableCache (bool value) { disable_cache = value; }
217 bool GetDisableCache () { return disable_cache; }
219 void SetContext (gpointer context) { this->context = context;}
220 gpointer GetContext () { return context; }
221 gpointer GetDownloaderState () { return downloader_state; }
223 DownloaderCreateWebRequestFunc GetRequestFunc () {return request_func; }
224 /* @GenerateCBinding,GeneratePInvoke */
225 void *CreateWebRequest (const char *method, const char *uri);
226 void SetResponseHeaderCallback (DownloaderResponseHeaderCallback callback, gpointer context);
228 DownloaderResponse * GetResponse ();
230 // Property Accessors
232 void SetDownloadProgress (double progress);
233 double GetDownloadProgress ();
235 const char *GetStatusText ();
236 int GetStatus ();
238 void SetUri (Uri *uri);
239 Uri *GetUri ();
242 class DownloaderResponse;
243 class DownloaderRequest;
245 /* @CBindingRequisite */
246 typedef guint32 (* DownloaderResponseStartedHandler) (DownloaderResponse *response, gpointer context);
247 /* @CBindingRequisite */
248 typedef guint32 (* DownloaderResponseDataAvailableHandler) (DownloaderResponse *response, gpointer context, char *buffer, guint32 length);
249 /* @CBindingRequisite */
250 typedef guint32 (* DownloaderResponseFinishedHandler) (DownloaderResponse *response, gpointer context, bool success, gpointer data, const char *uri);
252 class IDownloader {
253 private:
254 Deployment *deployment;
256 public:
257 virtual ~IDownloader () {};
259 virtual void Abort () = 0;
260 virtual const bool IsAborted () = 0;
261 Deployment *GetDeployment () { return deployment; }
262 void SetDeployment (Deployment *deployment) { this->deployment = deployment; }
265 class DownloaderResponse : public IDownloader {
266 protected:
267 DownloaderResponseStartedHandler started;
268 DownloaderResponseDataAvailableHandler available;
269 DownloaderResponseFinishedHandler finished;
270 gpointer context;
271 DownloaderRequest *request;
272 bool aborted;
274 public:
275 DownloaderResponse ();
276 DownloaderResponse (DownloaderResponseStartedHandler started, DownloaderResponseDataAvailableHandler available, DownloaderResponseFinishedHandler finished, gpointer context);
277 /* @GenerateCBinding,GeneratePInvoke */
278 virtual ~DownloaderResponse ();
280 /* @GenerateCBinding,GeneratePInvoke */
281 virtual void Abort () = 0;
282 virtual const bool IsAborted () { return this->aborted; }
283 /* @GenerateCBinding,GeneratePInvoke */
284 virtual void SetHeaderVisitor (DownloaderResponseHeaderCallback visitor, gpointer context) = 0;
285 /* @GenerateCBinding,GeneratePInvoke */
286 virtual int GetResponseStatus () = 0;
287 /* @GenerateCBinding,GeneratePInvoke */
288 virtual const char * GetResponseStatusText () = 0;
289 DownloaderRequest *GetDownloaderRequest () { return request; }
290 void SetDownloaderRequest (DownloaderRequest *value) { request = value; }
292 virtual void ref () = 0;
293 virtual void unref () = 0;
296 class DownloaderRequest : public IDownloader {
297 protected:
298 DownloaderResponse *response;
299 char *uri;
300 char *method;
302 bool aborted;
304 public:
305 DownloaderRequest (const char *method, const char *uri);
306 /* @GenerateCBinding,GeneratePInvoke */
307 virtual ~DownloaderRequest ();
309 /* @GenerateCBinding,GeneratePInvoke */
310 virtual void Abort () = 0;
311 /* @GenerateCBinding,GeneratePInvoke */
312 virtual bool GetResponse (DownloaderResponseStartedHandler started, DownloaderResponseDataAvailableHandler available, DownloaderResponseFinishedHandler finished, gpointer context) = 0;
313 /* @GenerateCBinding,GeneratePInvoke */
314 virtual const bool IsAborted () { return this->aborted; }
315 /* @GenerateCBinding,GeneratePInvoke */
316 virtual void SetHttpHeader (const char *name, const char *value) = 0;
317 /* @GenerateCBinding,GeneratePInvoke */
318 virtual void SetBody (/* @MarshalAs=byte[] */ void *body, int size) = 0;
319 /* @GenerateCBinding,GeneratePInvoke */
320 DownloaderResponse *GetDownloaderResponse () { return response; }
321 void SetDownloaderResponse (DownloaderResponse *value) { response = value; }
324 G_BEGIN_DECLS
326 void downloader_init (void);
328 G_END_DECLS
330 #endif