2009-09-29 Jeffrey Stedfast <fejj@novell.com>
[moon.git] / plugin / plugin-downloader.cpp
blobff4eee4185100bebd7e6d97da6ee5fc8564835b5
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * plugin-downloader.cpp: Moonlight plugin download routines.
5 * Contact:
6 * Moonlight List (moonlight-list@lists.ximian.com)
8 * Copyright 2007 Novell, Inc. (http://www.novell.com)
10 * See the LICENSE file included with the distribution for details.
14 #include <config.h>
16 #include "plugin-downloader.h"
17 #include "browser-bridge.h"
18 #include "npstream-request.h"
20 #define d(x)
22 bool downloader_shutdown = false;
24 guint32
25 plugin_downloader_started (DownloaderResponse *response, gpointer state)
27 d(printf ("plugin_downloader_started (%p, %p).\n", response, state));
28 PluginDownloader *pd = (PluginDownloader *)state;
30 if (pd != NULL) {
31 pd->setResponse (response);
32 pd->Started ();
35 return DOWNLOADER_OK;
38 guint32
39 plugin_downloader_available (DownloaderResponse *response, gpointer state, char *buffer, guint32 length)
41 d(printf ("plugin_downloader_available (%p, %p, %p, %u).\n", response, state, buffer, length));
42 PluginDownloader *pd = (PluginDownloader *)state;
44 if (pd != NULL) {
45 return pd->Read (buffer, length);
48 return DOWNLOADER_ERR;
51 guint32
52 plugin_downloader_finished (DownloaderResponse *response, gpointer state, bool success, gpointer data, const char *uri)
54 d(printf ("plugin_downloader_finished (%p, %p).\n", response, state));
55 PluginDownloader *pd = (PluginDownloader *)state;
57 if (pd != NULL) {
58 pd->Finished (success, data, uri);
61 return DOWNLOADER_OK;
64 static gpointer
65 plugin_downloader_create_state (Downloader *dl)
67 PluginDownloader *state = new PluginDownloader (dl);
69 d (printf ("plugin_downloader_create_state (%p)\n", state));
71 return state;
74 static void
75 plugin_downloader_destroy_state (gpointer data)
77 d (printf ("plugin_downloader_destroy_state (%p)\n", data));
79 delete (PluginDownloader *) data;
82 static void
83 plugin_downloader_open (gpointer state, const char *verb, const char *uri, bool custom_header_support, bool disable_cache)
85 d (printf ("plugin_downloader_open (%s, %s, %p)\n", verb, uri, state));
87 PluginDownloader *pd = (PluginDownloader *) state;
89 pd->Open (verb, uri, custom_header_support, disable_cache);
92 static void
93 plugin_downloader_send (gpointer state)
95 d (printf ("plugin_downloader_send (%p)\n", state));
97 PluginDownloader *pd = (PluginDownloader *) state;
99 pd->Send ();
102 static void
103 plugin_downloader_set_body (gpointer state, void *body, guint32 length)
105 d (printf ("plugin_downloader_set_body (%p)\n", state));
107 PluginDownloader *pd = (PluginDownloader *) state;
109 pd->SetBody (body, length);
112 static void
113 plugin_downloader_set_header (gpointer state, const char *header, const char *value)
115 d (printf ("plugin_downloader_set_header (%p)\n", state));
117 PluginDownloader *pd = (PluginDownloader *) state;
119 pd->SetHttpHeader (header, value);
122 static void
123 plugin_downloader_abort (gpointer state)
125 d (printf ("plugin_downloader_abort (%p)\n", state));
127 if (downloader_shutdown)
128 return;
130 PluginDownloader *pd = (PluginDownloader *) state;
132 pd->Abort ();
135 static void *
136 plugin_downloader_create_webrequest (const char *method, const char *uri, gpointer context)
138 if (!context)
139 return NULL;
141 PluginInstance *instance = (PluginInstance *) context;
142 BrowserBridge *bridge = instance->GetBridge ();
144 return bridge ? bridge->CreateDownloaderRequest (method, uri, false) : NULL;
147 static void
148 plugin_downloader_set_response_header_callback (gpointer state, DownloaderResponseHeaderCallback callback, gpointer context)
150 if (!state)
151 return;
153 PluginDownloader *pd = (PluginDownloader *) state;
154 pd->SetResponseHeaderCallback (callback, context);
157 static DownloaderResponse *
158 plugin_downloader_get_response (gpointer state)
160 if (!state)
161 return NULL;
163 return ((PluginDownloader *) state)->getResponse ();
166 PluginDownloader::PluginDownloader (Downloader *dl)
168 d (printf ("PluginDownloader::PluginDownloader (), this: %p, dl: %p\n", this, dl));
169 this->dl = dl;
170 this->uri = NULL;
171 this->verb = NULL;
172 this->response = NULL;
173 this->request = NULL;
174 this->finished = false;
175 this->response_header_callback = NULL;
176 this->response_header_context = NULL;
179 PluginDownloader::~PluginDownloader ()
181 d (printf ("PluginDownloader::~PluginDownloader (), this: %p, dl: %p\n", this, dl));
183 Abort ();
185 g_free (verb);
186 g_free (uri);
187 dl = NULL;
190 void
191 PluginDownloader::Abort ()
193 d (printf ("PluginDownloader::Abort (), this: %p, dl: %p, finished: %i, request: %p, response: %p\n", this, dl, finished, request, response));
195 if (finished)
196 return;
198 response_header_callback = NULL;
199 response_header_context = NULL;
201 if (this->request) {
202 this->request->Abort ();
203 delete request;
204 this->request = NULL;
206 if (this->response) {
207 this->response->Abort ();
208 this->response->unref ();
209 this->response = NULL;
213 void
214 PluginDownloader::Open (const char *verb, const char *uri, bool custom_header_support, bool disable_cache)
216 d (printf ("PluginDownloader::Open (), this: %p, dl: %p\n", this, dl));
218 //delete this->bdl;
219 g_free (this->uri);
220 g_free (this->verb);
222 this->verb = g_strdup (verb);
223 this->uri = g_strdup (uri);
225 if (custom_header_support) {
226 BrowserBridge *bridge = GetPlugin ()->GetBridge ();
227 if (bridge)
228 this->request = bridge->CreateDownloaderRequest (this->verb, this->uri, disable_cache);
229 } else {
230 this->request = new NPStreamRequest (this->verb, this->uri, GetPlugin ());
234 void
235 PluginDownloader::Send ()
237 d (printf ("PluginDownloader::Send (), this: %p, dl: %p\n", this, dl));
239 finished = false;
240 this->offset = 0;
241 this->request->GetResponse (plugin_downloader_started, plugin_downloader_available, plugin_downloader_finished, this);
244 void
245 PluginDownloader::Started ()
247 d (printf ("PluginDownloader::Started (), this: %p, dl: %p\n", this, dl));
250 void
251 PluginDownloader::setResponse (DownloaderResponse *response)
253 d (printf ("PluginDownloader::setResponse (%p)\n", response));
255 if (this->response == response)
256 return;
258 if (this->response != NULL)
259 this->response->unref ();
260 this->response = response;
261 if (this->response != NULL) {
262 this->response->ref ();
264 if (response_header_callback != NULL)
265 response->SetHeaderVisitor (response_header_callback, response_header_context);
269 void
270 PluginDownloader::SetResponseHeaderCallback (DownloaderResponseHeaderCallback callback, gpointer context)
272 response_header_callback = NULL;
273 if (response) {
274 response->SetHeaderVisitor (callback, context);
275 } else {
276 response_header_callback = callback;
277 response_header_context = context;
281 DownloaderRequest *
282 PluginDownloader::getRequest ()
284 return this->request;
287 guint32
288 PluginDownloader::Read (char *buffer, guint32 length)
290 d (printf ("PluginDownloader::Read (), this: %p, dl: %p\n", this, dl));
292 if (dl != NULL) {
293 dl->Write (buffer, this->offset, length);
294 this->offset += length;
295 return DOWNLOADER_OK;
298 return DOWNLOADER_ERR;
301 void
302 PluginDownloader::Finished (bool success, gpointer data, const char *uri)
304 d (printf ("PluginDownloader::Finished (), this: %p, dl: %p\n", this, dl));
306 finished = true;
308 if (dl != NULL) {
309 if (success) {
310 dl->NotifySize (this->offset);
311 dl->SetFilename ((const char *)data);
312 dl->NotifyFinished (uri);
313 } else {
314 dl->NotifyFailed ("download failed");
319 void
320 PluginDownloader::SetHttpHeader (const char *header, const char *value)
322 d (printf ("PluginDownloader::SetHttpHeader (), this: %p, dl: %p\n", this, dl));
324 if (request != NULL)
325 request->SetHttpHeader (header, value);
328 void
329 PluginDownloader::SetBody (void *body, guint32 length)
331 d (printf ("PluginDownloader::SetBody (), this: %p, dl: %p\n", this, dl));
333 if (request != NULL)
334 request->SetBody (body, length);
337 PluginInstance *
338 PluginDownloader::GetPlugin ()
340 PluginInstance *instance = NULL;
342 if (dl && dl->GetContext ()) {
343 // Get the context from the downloader.
344 instance = (PluginInstance *) dl->GetContext ();
345 } else if (plugin_instances && plugin_instances->data) {
346 // TODO: Review if we really should allowing download with the first plugin.
347 NPP_t *plugin = (NPP_t *) plugin_instances->data;
348 if (plugin == NULL || plugin->pdata == NULL)
349 return NULL;
350 instance = (PluginInstance*)plugin->pdata;
353 return instance;
356 void
357 downloader_initialize (void)
359 downloader_shutdown = false;
361 Downloader::SetFunctions (
362 &plugin_downloader_create_state,
363 &plugin_downloader_destroy_state,
364 &plugin_downloader_open,
365 &plugin_downloader_send,
366 &plugin_downloader_abort,
367 &plugin_downloader_set_header,
368 &plugin_downloader_set_body,
369 &plugin_downloader_create_webrequest,
370 &plugin_downloader_set_response_header_callback,
371 &plugin_downloader_get_response);
374 void
375 downloader_destroy (void)
377 downloader_shutdown = true;