in plugin/:
[moon.git] / src / downloader.h
blob172bd5071b7eb64aba6429719e39d1645fa2db5b
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 void Abort ();
145 char *GetResponseText (const char *Partname, gint64 *size);
146 char *GetDownloadedFilename (const char *partname);
147 void Open (const char *verb, const char *uri, DownloaderAccessPolicy policy);
148 void Open (const char *verb, Uri *uri, DownloaderAccessPolicy policy);
149 void SendInternal ();
150 void Send ();
151 void SendNow ();
153 // the following is stuff not exposed by C#/js, but is useful
154 // when writing unmanaged code for downloader implementations
155 // or data sinks.
157 void OpenInitialize ();
158 void InternalAbort ();
159 void InternalWrite (void *buf, gint32 offset, gint32 n);
160 void InternalOpen (const char *verb, const char *uri);
161 void InternalSetHeader (const char *header, const char *value);
162 void InternalSetHeaderFormatted (const char *header, char *value); // calls g_free on the value
163 void InternalSetBody (void *body, guint32 length);
165 /* @GenerateCBinding,GeneratePInvoke */
166 void Write (void *buf, gint32 offset, gint32 n);
168 /* @GenerateCBinding,GeneratePInvoke */
169 void NotifyFinished (const char *final_uri);
171 /* @GenerateCBinding,GeneratePInvoke */
172 void NotifyFailed (const char *msg);
174 /* @GenerateCBinding,GeneratePInvoke */
175 void NotifySize (gint64 size);
177 bool CheckRedirectionPolicy (const char *url);
179 void SetFilename (const char *fname);
180 char *GetBuffer () { return buffer; }
181 gint64 GetSize () { return total; }
183 InternalDownloader *GetInternalDownloader () { return internal_dl; }
185 // This is called by the consumer of the downloaded data (the
186 // Image class for instance)
187 void SetStreamFunctions (DownloaderWriteFunc writer,
188 DownloaderNotifySizeFunc notify_size,
189 gpointer user_data);
191 // This is called by the supplier of the downloaded data (the
192 // managed framework, the browser plugin, the demo test)
194 /* @GenerateCBinding,GeneratePInvoke */
195 static void SetFunctions (DownloaderCreateStateFunc create_state,
196 DownloaderDestroyStateFunc destroy_state,
197 DownloaderOpenFunc open,
198 DownloaderSendFunc send,
199 DownloaderAbortFunc abort,
200 DownloaderHeaderFunc header,
201 DownloaderBodyFunc body,
202 DownloaderCreateWebRequestFunc request,
203 DownloaderSetResponseHeaderCallbackFunc response_header_callback,
204 DownloaderGetResponseFunc get_response
207 bool Started ();
208 bool Completed ();
209 bool IsAborted () { return aborted; }
210 const char *GetFailedMessage () { return failed_msg; }
212 void SetRequireCustomHeaderSupport (bool value) { custom_header_support = value; }
213 bool GetRequireCustomHeaderSupport () { return custom_header_support; }
214 void SetDisableCache (bool value) { disable_cache = value; }
215 bool GetDisableCache () { return disable_cache; }
217 void SetContext (gpointer context) { this->context = context;}
218 gpointer GetContext () { return context; }
219 gpointer GetDownloaderState () { return downloader_state; }
221 DownloaderCreateWebRequestFunc GetRequestFunc () {return request_func; }
222 /* @GenerateCBinding,GeneratePInvoke */
223 void *CreateWebRequest (const char *method, const char *uri);
224 void SetResponseHeaderCallback (DownloaderResponseHeaderCallback callback, gpointer context);
226 DownloaderResponse * GetResponse ();
228 // Property Accessors
230 void SetDownloadProgress (double progress);
231 double GetDownloadProgress ();
233 const char *GetStatusText ();
234 int GetStatus ();
236 void SetUri (Uri *uri);
237 Uri *GetUri ();
240 class DownloaderResponse;
241 class DownloaderRequest;
243 /* @CBindingRequisite */
244 typedef guint32 (* DownloaderResponseStartedHandler) (DownloaderResponse *response, gpointer context);
245 /* @CBindingRequisite */
246 typedef guint32 (* DownloaderResponseDataAvailableHandler) (DownloaderResponse *response, gpointer context, char *buffer, guint32 length);
247 /* @CBindingRequisite */
248 typedef guint32 (* DownloaderResponseFinishedHandler) (DownloaderResponse *response, gpointer context, bool success, gpointer data, const char *uri);
250 class IDownloader {
251 private:
252 Deployment *deployment;
254 public:
255 virtual ~IDownloader () {};
257 virtual void Abort () = 0;
258 virtual const bool IsAborted () = 0;
259 Deployment *GetDeployment () { return deployment; }
260 void SetDeployment (Deployment *deployment) { this->deployment = deployment; }
263 class DownloaderResponse : public IDownloader {
264 protected:
265 DownloaderResponseStartedHandler started;
266 DownloaderResponseDataAvailableHandler available;
267 DownloaderResponseFinishedHandler finished;
268 gpointer context;
269 DownloaderRequest *request;
270 bool aborted;
272 public:
273 DownloaderResponse ();
274 DownloaderResponse (DownloaderResponseStartedHandler started, DownloaderResponseDataAvailableHandler available, DownloaderResponseFinishedHandler finished, gpointer context);
275 /* @GenerateCBinding,GeneratePInvoke */
276 virtual ~DownloaderResponse ();
278 /* @GenerateCBinding,GeneratePInvoke */
279 virtual void Abort () = 0;
280 virtual const bool IsAborted () { return this->aborted; }
281 /* @GenerateCBinding,GeneratePInvoke */
282 virtual void SetHeaderVisitor (DownloaderResponseHeaderCallback visitor, gpointer context) = 0;
283 /* @GenerateCBinding,GeneratePInvoke */
284 virtual int GetResponseStatus () = 0;
285 /* @GenerateCBinding,GeneratePInvoke */
286 virtual const char * GetResponseStatusText () = 0;
287 DownloaderRequest *GetDownloaderRequest () { return request; }
288 void SetDownloaderRequest (DownloaderRequest *value) { request = value; }
290 virtual void ref () = 0;
291 virtual void unref () = 0;
294 class DownloaderRequest : public IDownloader {
295 protected:
296 DownloaderResponse *response;
297 char *uri;
298 char *method;
300 bool aborted;
302 public:
303 DownloaderRequest (const char *method, const char *uri);
304 /* @GenerateCBinding,GeneratePInvoke */
305 virtual ~DownloaderRequest ();
307 /* @GenerateCBinding,GeneratePInvoke */
308 virtual void Abort () = 0;
309 /* @GenerateCBinding,GeneratePInvoke */
310 virtual bool GetResponse (DownloaderResponseStartedHandler started, DownloaderResponseDataAvailableHandler available, DownloaderResponseFinishedHandler finished, gpointer context) = 0;
311 /* @GenerateCBinding,GeneratePInvoke */
312 virtual const bool IsAborted () { return this->aborted; }
313 /* @GenerateCBinding,GeneratePInvoke */
314 virtual void SetHttpHeader (const char *name, const char *value) = 0;
315 /* @GenerateCBinding,GeneratePInvoke */
316 virtual void SetBody (/* @MarshalAs=byte[] */ void *body, int size) = 0;
317 /* @GenerateCBinding,GeneratePInvoke */
318 DownloaderResponse *GetDownloaderResponse () { return response; }
319 void SetDownloaderResponse (DownloaderResponse *value) { response = value; }
322 G_BEGIN_DECLS
324 void downloader_init (void);
326 G_END_DECLS
328 #endif