/
plugin.cpp
470 lines (417 loc) · 10.9 KB
/
plugin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/**************************************************************************
Lighspark, a free flash player implementation
Copyright (C) 2009 Alessandro Pignotti (a.pignotti@sssup.it)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
**************************************************************************/
#include "plugin.h"
#include "logger.h"
#define MIME_TYPES_HANDLED "application/x-shockwave-flash"
//#define MIME_TYPES_HANDLED "application/x-lightspark"
#define PLUGIN_NAME "Shockwave Flash"
#define MIME_TYPES_DESCRIPTION MIME_TYPES_HANDLED":swf:"PLUGIN_NAME
//#define PLUGIN_DESCRIPTION "Shockwave Flash 10.0 r00 - Lightspark implementation"
#define PLUGIN_DESCRIPTION "Shockwave Flash 10.0 r00"
#include "class.h"
#include <gtk/gtk.h>
using namespace std;
__thread lightspark::SystemState* sys;
TLSDATA lightspark::RenderThread* rt=NULL;
TLSDATA lightspark::ParseThread* pt=NULL;
NPDownloadManager::NPDownloadManager(NPP i):instance(i)
{
sem_init(&mutex,0,1);
}
NPDownloadManager::~NPDownloadManager()
{
if(!pendingLoads.empty())
abort();
sem_destroy(&mutex);
}
lightspark::Downloader* NPDownloadManager::download(const lightspark::tiny_string& u)
{
sem_wait(&mutex);
//TODO: Url encode the string
std::string tmp2;
tmp2.reserve(u.len()*2);
for(int i=0;i<u.len();i++)
{
if(u[i]==' ')
{
char buf[4];
sprintf(buf,"%%%x",(unsigned char)u[i]);
tmp2+=buf;
}
else
tmp2.push_back(u[i]);
}
lightspark::tiny_string url=tmp2.c_str();
//Register this download
NPDownloader* ret=new NPDownloader(instance, url);
pendingLoads.push_back(make_pair(url,ret));
sem_post(&mutex);
return ret;
}
void NPDownloadManager::destroy(lightspark::Downloader* d)
{
//First of all, wait for termination
if(!sys->shutdown)
d->wait();
sem_wait(&mutex);
list<pair<lightspark::tiny_string,NPDownloader*> >::iterator it=pendingLoads.begin();
for(;it!=pendingLoads.end();it++)
{
if(it->second==d)
{
pendingLoads.erase(it);
break;
}
}
sem_post(&mutex);
delete d;
}
NPDownloader* NPDownloadManager::getDownloaderForUrl(const char* u)
{
sem_wait(&mutex);
list<pair<lightspark::tiny_string,NPDownloader*> >::iterator it=pendingLoads.begin();
NPDownloader* ret=NULL;
for(;it!=pendingLoads.end();it++)
{
cout << it->first << endl;
if(it->first==u)
{
ret=it->second;
pendingLoads.erase(it);
break;
}
}
sem_post(&mutex);
return ret;
}
NPDownloader::NPDownloader(NPP i, const lightspark::tiny_string& u):instance(i),url(u),started(false)
{
NPN_PluginThreadAsyncCall(instance, dlStartCallback, this);
}
void NPDownloader::terminate()
{
sem_post(&terminated);
}
void NPDownloader::dlStartCallback(void* t)
{
NPDownloader* th=static_cast<NPDownloader*>(t);
cerr << "Start download for " << th->url << endl;
th->started=true;
NPError e=NPN_GetURLNotify(th->instance, th->url.raw_buf(), NULL, th);
//NPError e=NPN_GetURLNotify(th->instance, "http://www.google.com", "_blank", th);
//NPError e=NPN_GetURL(instance, "http://www.google.com", "_blank");
if(e!=NPERR_NO_ERROR)
abort();
}
char* NPP_GetMIMEDescription(void)
{
return (char*)(MIME_TYPES_DESCRIPTION);
}
/////////////////////////////////////
// general initialization and shutdown
//
NPError NS_PluginInitialize()
{
// Log::initLogging(LOG_NOT_IMPLEMENTED);
Log::initLogging(LOG_NO_INFO);
return NPERR_NO_ERROR;
}
void NS_PluginShutdown()
{
}
// get values per plugin
NPError NS_PluginGetValue(NPPVariable aVariable, void *aValue)
{
NPError err = NPERR_NO_ERROR;
switch (aVariable)
{
case NPPVpluginNameString:
*((char **)aValue) = (char*)PLUGIN_NAME;
break;
case NPPVpluginDescriptionString:
*((char **)aValue) = (char*)PLUGIN_DESCRIPTION;
break;
case NPPVpluginNeedsXEmbed:
*((bool *)aValue) = true;
break;
default:
err = NPERR_INVALID_PARAM;
break;
}
return err;
}
/////////////////////////////////////////////////////////////
//
// construction and destruction of our plugin instance object
//
nsPluginInstanceBase * NS_NewPluginInstance(nsPluginCreateData * aCreateDataStruct)
{
if(!aCreateDataStruct)
return NULL;
nsPluginInstance * plugin = new nsPluginInstance(aCreateDataStruct->instance,
aCreateDataStruct->argc,
aCreateDataStruct->argn,
aCreateDataStruct->argv);
return plugin;
}
void NS_DestroyPluginInstance(nsPluginInstanceBase * aPlugin)
{
if(aPlugin)
delete (nsPluginInstance *)aPlugin;
}
////////////////////////////////////////
//
// nsPluginInstance class implementation
//
nsPluginInstance::nsPluginInstance(NPP aInstance, int16_t argc, char** argn, char** argv) : nsPluginInstanceBase(),
mInstance(aInstance),mInitialized(FALSE),mWindow(0),swf_stream(&swf_buf),
m_pt(&m_sys,swf_stream),m_it(NULL),m_rt(NULL)
{
//Find flashvars argument
for(int i=0;i<argc;i++)
{
if(argn[i]==NULL || argv[i]==NULL)
continue;
if(strcmp(argn[i],"flashvars")!=0)
continue;
lightspark::ASObject* params=new lightspark::ASObject;
//Add arguments to SystemState
std::string vars(argv[i]);
uint32_t cur=0;
while(cur<vars.size())
{
int n1=vars.find('=',cur);
if(n1==-1) //Incomplete parameters string, ignore the last
break;
int n2=vars.find('&',n1+1);
if(n2==-1)
n2=vars.size();
std::string varName=vars.substr(cur,(n1-cur));
//The variable value has to be urldecoded
bool ok=true;
std::string varValue;
varValue.reserve(n2-n1); //The maximum lenght
for(int j=n1+1;j<n2;j++)
{
if(vars[j]!='%')
varValue.push_back(vars[j]);
else
{
if((n2-j)<3) //Not enough characters
{
ok=false;
break;
}
int t1=hexToInt(vars[j+1]);
int t2=hexToInt(vars[j+2]);
if(t1==-1 || t2==-1)
{
ok=false;
break;
}
int c=(t1*16)+t2;
varValue.push_back(c);
j+=2;
}
}
if(ok)
{
cout << varName << ' ' << varValue << endl;
params->setVariableByQName(varName.c_str(),"",
lightspark::Class<lightspark::ASString>::getInstanceS(varValue));
}
cur=n2+1;
}
sys->setParameters(params);
break;
}
}
int nsPluginInstance::hexToInt(char c)
{
if(c>='0' && c<='9')
return c-'0';
else if(c>='a' && c<='f')
return c-'a'+10;
else if(c>='A' && c<='F')
return c-'A'+10;
else
return -1;
}
nsPluginInstance::~nsPluginInstance()
{
//Shutdown the system
cerr << "instance dying" << endl;
m_sys.setShutdownFlag();
m_sys.wait();
if(m_rt)
m_rt->wait();
if(m_it)
m_it->wait();
delete m_rt;
delete m_it;
}
void nsPluginInstance::draw()
{
/* if(m_rt==NULL || m_it==NULL)
return;
m_rt->draw();*/
}
NPBool nsPluginInstance::init(NPWindow* aWindow)
{
if(aWindow == NULL)
return FALSE;
if (SetWindow(aWindow))
mInitialized = TRUE;
return mInitialized;
}
void nsPluginInstance::shut()
{
mInitialized = FALSE;
}
const char * nsPluginInstance::getVersion()
{
return NPN_UserAgent(mInstance);
}
NPError nsPluginInstance::GetValue(NPPVariable aVariable, void *aValue)
{
NPError err = NPERR_NO_ERROR;
switch (aVariable) {
case NPPVpluginNameString:
case NPPVpluginDescriptionString:
case NPPVpluginNeedsXEmbed:
return NS_PluginGetValue(aVariable, aValue) ;
break;
default:
err = NPERR_INVALID_PARAM;
break;
}
return err;
}
NPError nsPluginInstance::SetWindow(NPWindow* aWindow)
{
if(aWindow == NULL)
return FALSE;
mX = aWindow->x;
mY = aWindow->y;
mWidth = aWindow->width;
mHeight = aWindow->height;
if(mHeight==0 || mHeight==0)
assert(false);
if (mWindow == (Window) aWindow->window)
{
// The page with the plugin is being resized.
// Save any UI information because the next time
// around expect a SetWindow with a new window id.
}
else
{
mWindow = (Window) aWindow->window;
NPSetWindowCallbackStruct *ws_info = (NPSetWindowCallbackStruct *)aWindow->ws_info;
mDisplay = ws_info->display;
mVisual = ws_info->visual;
mDepth = ws_info->depth;
mColormap = ws_info->colormap;
lightspark::NPAPI_params* p=new lightspark::NPAPI_params;
p->display=mDisplay;
p->visual=XVisualIDFromVisual(mVisual);
p->window=mWindow;
p->width=mWidth;
p->height=mHeight;
p->container=gtk_plug_new((GdkNativeWindow)p->window);
lightspark::NPAPI_params* p2=new lightspark::NPAPI_params(*p);
if(m_rt!=NULL)
{
cout << "destroy old context" << endl;
abort();
}
if(p->width==0 || p->height==0)
abort();
m_rt=new lightspark::RenderThread(&m_sys,lightspark::GTKPLUG,p);
if(m_it!=NULL)
{
cout << "destroy old input" << endl;
abort();
}
m_it=new lightspark::InputThread(&m_sys,lightspark::GTKPLUG,p2);
sys=NULL;
m_sys.inputThread=m_it;
m_sys.renderThread=m_rt;
m_sys.downloadManager=new NPDownloadManager(mInstance);
m_sys.addJob(&m_pt);
}
//draw();
return TRUE;
}
NPError nsPluginInstance::NewStream(NPMIMEType type, NPStream* stream, NPBool seekable, uint16_t* stype)
{
//We have to cast the downloadanager to a NPDownloadManager
NPDownloadManager* manager=static_cast<NPDownloadManager*>(m_sys.downloadManager);
lightspark::Downloader* dl=manager->getDownloaderForUrl(stream->url);
cerr << "Newstream for " << stream->url << endl;
cerr << stream->headers << endl;
if(dl)
{
cerr << "via NPDownloader" << endl;
dl->setLen(stream->end);
}
//The downloader is set as the private data for this stream
stream->pdata=dl;
*stype=NP_NORMAL;
return NPERR_NO_ERROR;
}
int32_t nsPluginInstance::WriteReady(NPStream *stream)
{
if(stream->pdata) //This is a Downloader based download
return 1024*1024;
else
return swf_buf.getFree();
}
int32_t nsPluginInstance::Write(NPStream *stream, int32_t offset, int32_t len, void *buffer)
{
if(stream->pdata)
{
lightspark::Downloader* dl=static_cast<lightspark::Downloader*>(stream->pdata);
if(dl->hasFailed())
return -1;
dl->append((uint8_t*)buffer,len);
return len;
}
else
return swf_buf.write((char*)buffer,len);
}
NPError nsPluginInstance::DestroyStream(NPStream *stream, NPError reason)
{
if(stream->pdata)
{
cerr << "Destroy " << stream->pdata << endl;
NPDownloader* dl=static_cast<NPDownloader*>(stream->pdata);
dl->terminate();
}
return NPERR_NO_ERROR;
}
void nsPluginInstance::URLNotify(const char* url, NPReason reason, void* notifyData)
{
cerr << "URLnotify" << notifyData << endl;
cerr << url << endl;
cerr << reason << endl;
cerr << notifyData << endl;
/* if(reason!=NPRES_USER_BREAK)
{
cerr << url << endl;
cerr << reason << endl;
cerr << notifyData << endl;
abort();
}*/
}