1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * plugin-downloader.cpp: Moonlight plugin download routines.
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.
16 #include "plugin-downloader.h"
17 #include "browser-bridge.h"
18 #include "npstream-request.h"
22 bool downloader_shutdown
= false;
25 plugin_downloader_started (DownloaderResponse
*response
, gpointer state
)
27 d(printf ("plugin_downloader_started (%p, %p).\n", response
, state
));
28 PluginDownloader
*pd
= (PluginDownloader
*)state
;
31 pd
->setResponse (response
);
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
;
45 return pd
->Read (buffer
, length
);
48 return DOWNLOADER_ERR
;
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
;
58 pd
->Finished (success
, data
, uri
);
65 plugin_downloader_create_state (Downloader
*dl
)
67 PluginDownloader
*state
= new PluginDownloader (dl
);
69 d (printf ("plugin_downloader_create_state (%p)\n", state
));
75 plugin_downloader_destroy_state (gpointer data
)
77 d (printf ("plugin_downloader_destroy_state (%p)\n", data
));
79 delete (PluginDownloader
*) data
;
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
);
93 plugin_downloader_send (gpointer state
)
95 d (printf ("plugin_downloader_send (%p)\n", state
));
97 PluginDownloader
*pd
= (PluginDownloader
*) state
;
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
);
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
);
123 plugin_downloader_abort (gpointer state
)
125 d (printf ("plugin_downloader_abort (%p)\n", state
));
127 if (downloader_shutdown
)
130 PluginDownloader
*pd
= (PluginDownloader
*) state
;
136 plugin_downloader_create_webrequest (const char *method
, const char *uri
, gpointer context
)
141 PluginInstance
*instance
= (PluginInstance
*) context
;
142 BrowserBridge
*bridge
= instance
->GetBridge ();
144 return bridge
? bridge
->CreateDownloaderRequest (method
, uri
, false) : NULL
;
148 plugin_downloader_set_response_header_callback (gpointer state
, DownloaderResponseHeaderCallback callback
, gpointer context
)
153 PluginDownloader
*pd
= (PluginDownloader
*) state
;
154 pd
->SetResponseHeaderCallback (callback
, context
);
157 static DownloaderResponse
*
158 plugin_downloader_get_response (gpointer state
)
163 return ((PluginDownloader
*) state
)->getResponse ();
166 PluginDownloader::PluginDownloader (Downloader
*dl
)
168 d (printf ("PluginDownloader::PluginDownloader (), this: %p, dl: %p\n", this, dl
));
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
));
191 PluginDownloader::Abort ()
193 d (printf ("PluginDownloader::Abort (), this: %p, dl: %p, finished: %i, request: %p, response: %p\n", this, dl
, finished
, request
, response
));
198 response_header_callback
= NULL
;
199 response_header_context
= NULL
;
202 this->request
->Abort ();
204 this->request
= NULL
;
206 if (this->response
) {
207 this->response
->Abort ();
208 this->response
->unref ();
209 this->response
= NULL
;
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
));
222 this->verb
= g_strdup (verb
);
223 this->uri
= g_strdup (uri
);
225 if (custom_header_support
) {
226 BrowserBridge
*bridge
= GetPlugin ()->GetBridge ();
228 this->request
= bridge
->CreateDownloaderRequest (this->verb
, this->uri
, disable_cache
);
230 this->request
= new NPStreamRequest (this->verb
, this->uri
, GetPlugin ());
235 PluginDownloader::Send ()
237 d (printf ("PluginDownloader::Send (), this: %p, dl: %p\n", this, dl
));
241 this->request
->GetResponse (plugin_downloader_started
, plugin_downloader_available
, plugin_downloader_finished
, this);
245 PluginDownloader::Started ()
247 d (printf ("PluginDownloader::Started (), this: %p, dl: %p\n", this, dl
));
251 PluginDownloader::setResponse (DownloaderResponse
*response
)
253 d (printf ("PluginDownloader::setResponse (%p)\n", response
));
255 if (this->response
== response
)
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
);
270 PluginDownloader::SetResponseHeaderCallback (DownloaderResponseHeaderCallback callback
, gpointer context
)
272 response_header_callback
= NULL
;
274 response
->SetHeaderVisitor (callback
, context
);
276 response_header_callback
= callback
;
277 response_header_context
= context
;
282 PluginDownloader::getRequest ()
284 return this->request
;
288 PluginDownloader::Read (char *buffer
, guint32 length
)
290 d (printf ("PluginDownloader::Read (), this: %p, dl: %p\n", this, dl
));
293 dl
->Write (buffer
, this->offset
, length
);
294 this->offset
+= length
;
295 return DOWNLOADER_OK
;
298 return DOWNLOADER_ERR
;
302 PluginDownloader::Finished (bool success
, gpointer data
, const char *uri
)
304 d (printf ("PluginDownloader::Finished (), this: %p, dl: %p\n", this, dl
));
310 dl
->NotifySize (this->offset
);
311 dl
->SetFilename ((const char *)data
);
312 dl
->NotifyFinished (uri
);
314 dl
->NotifyFailed ("download failed");
320 PluginDownloader::SetHttpHeader (const char *header
, const char *value
)
322 d (printf ("PluginDownloader::SetHttpHeader (), this: %p, dl: %p\n", this, dl
));
325 request
->SetHttpHeader (header
, value
);
329 PluginDownloader::SetBody (void *body
, guint32 length
)
331 d (printf ("PluginDownloader::SetBody (), this: %p, dl: %p\n", this, dl
));
334 request
->SetBody (body
, length
);
338 PluginDownloader::GetPlugin ()
340 PluginInstance
*instance
= NULL
;
342 if (dl
&& dl
->GetContext ()) {
343 // Get the context from the downloader.
344 instance
= (PluginInstance
*) dl
->GetContext ();
351 downloader_initialize (void)
353 downloader_shutdown
= false;
355 Downloader::SetFunctions (
356 &plugin_downloader_create_state
,
357 &plugin_downloader_destroy_state
,
358 &plugin_downloader_open
,
359 &plugin_downloader_send
,
360 &plugin_downloader_abort
,
361 &plugin_downloader_set_header
,
362 &plugin_downloader_set_body
,
363 &plugin_downloader_create_webrequest
,
364 &plugin_downloader_set_response_header_callback
,
365 &plugin_downloader_get_response
);
369 downloader_destroy (void)
371 downloader_shutdown
= true;