File: NullPlugin.cpp

package info (click to toggle)
mozilla-firefox 1.0.4-2sarge17
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 255,356 kB
  • ctags: 267,207
  • sloc: cpp: 1,623,961; ansic: 792,828; xml: 85,380; makefile: 41,934; perl: 27,802; asm: 14,884; sh: 14,807; cs: 4,507; python: 4,398; java: 4,004; yacc: 1,380; lex: 409; pascal: 354; php: 244; csh: 132; objc: 73; ada: 44; sql: 4
file content (1277 lines) | stat: -rw-r--r-- 35,832 bytes parent folder | download
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Netscape Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/NPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Mozilla Communicator client code.
 *
 * The Initial Developer of the Original Code is 
 * Netscape Communications Corporation.
 * Portions created by the Initial Developer are Copyright (C) 1998
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or 
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the NPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the NPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */


#ifndef _NPAPI_H_
#include "npapi.h"
#endif

#ifdef XP_MACOSX
#undef DARWIN
#include <CoreFoundation/CoreFoundation.h>
#else
#include <Gestalt.h>
#include <Icons.h>
#include <Resources.h>
#include <Processes.h>
#include <Script.h>
#include <TextUtils.h>
#include <CFPreferences.h>
#endif

#include <string.h>
#include <ctype.h>
#include <stdio.h>

#define PLUGINFINDER_COMMAND_BEGINNING "javascript:window.open(\""
#define PLUGINFINDER_COMMAND_END "\",\"plugin\",\"toolbar=no,status=no,resizable=no,scrollbars=no,height=252,width=626\");"
#define PLUGINFINDER_COMMAND_END2 "\",\"plugin\",\"toolbar=no,status=no,resizable=yes,scrollbars=yes,height=252,width=626\");"


//
// Instance state information about the plugin.
//
class CPlugin
{
public:
	enum HiliteState { kUnhilited = 0, kHilited = 1 };	

	static NPError		Initialize();
	static void			Shutdown();
	
	// no ctor because CPlugin is allocated and constructed by hand.
	// ideally, this should use placement |new|.
	
			void		Constructor(NPP instance, NPMIMEType type, uint16 mode, int16 argc, char* argn[], char* argv[]);
			void		Destructor();
						
			void		SetWindow(NPWindow* window);
			void		Print(NPPrint* printInfo);
			Boolean		HandleEvent(EventRecord*);

protected:
			void		Draw(HiliteState hilite);
			void 		DrawString(const unsigned char* text, short width, short height, short centerX, Rect drawRect);
			void		MouseDown();
			
			Boolean		FocusDraw();
			void		RestoreDraw();
			
			void		DetermineURL(int16 argc, char* argn[], char* argv[]);
			char *		MakeDefaultURL(void);
			void		AddMimeTypeToList(StringPtr cTypeString);
			Boolean		CheckMimeTypes();
			void		AskAndLoadURL();
			void		RefreshPluginPage();
			
			Ptr			New(UInt32 size);
			void		Delete(Ptr ptr);
			
			Boolean		IsPluginHidden(int16 argc, char* argn[], char* argv[]);
			
private:
	static 	CIconHandle	sIconHandle;
	static	CursHandle	sHandCursor;
	static	char*		sAltText;
	static	char*		sInstallCommand;
	static	char*		sDefaultPage;
	static  char*		sRefreshText;
	static	char*		sJavaScriptPage;
	static	FSSpec		sDataFileSpec;      // only used for Mac OS 9
	static	Boolean		sRunningOnOSX;

			NPP			fInstance;
			NPWindow*	fWindow;
			uint16		fMode;
			NPMIMEType	fType;
			char*		fPageURL;
			char*		fFileURL;
			NPBool		m_bOffline;
			NPBool		m_bJavaScript;
			
			GrafPtr		fSavePort;
			RgnHandle	fSaveClip;
			Rect		fRevealedRect;
			short		fSavePortTop;
			short		fSavePortLeft;
			Boolean		fUserInstalledPlugin;
			Boolean		fHiddenPlugin;
			Boolean		fAskedLoadURL;
};


CIconHandle CPlugin::sIconHandle 		= NULL;
CursHandle 	CPlugin::sHandCursor 		= NULL;
char*	 	CPlugin::sAltText			= NULL;
char*		CPlugin::sInstallCommand	= NULL; 
char*		CPlugin::sDefaultPage		= NULL;
char*		CPlugin::sRefreshText		= NULL;
char*		CPlugin::sJavaScriptPage	= NULL;
FSSpec		CPlugin::sDataFileSpec;
Boolean		CPlugin::sRunningOnOSX		= false;

extern short 		gResFile;

#if !TARGET_API_MAC_CARBON
extern QDGlobals*	gQDPtr;
#endif

// 'cicn'
const short rBrokenPluginIcon = 326;

// 'CURS'
const short rHandCursor = 128;

// 'STR '
const short rDefaultPluginURL = 128;
const short rAltTextString = 129;
const short rJavaScriptInstallCommand = 130;
const short rRefreshTextString = 131;
const short rJavaScriptPageURL = 132;

// 'STR#'
const short rTypeListStrings = 129;

static const char szPluginFinderCommandBeginning[] = PLUGINFINDER_COMMAND_BEGINNING;
static const char szPluginFinderCommandEnd[] = PLUGINFINDER_COMMAND_END;

//#ifndef XP_MACOSX

//------------------------------------------------------------------------------------
// strcasecomp: Why don't the MW C libraries have this??
//------------------------------------------------------------------------------------
#define XP_TO_LOWER(i) 	((((unsigned int) (i)) > 0x7f) ? (int) (i) : tolower(i))
int strcasecmp (const char* one, const char *two);

int strcasecmp (const char* one, const char *two)
{
	const char *pA;
	const char *pB;

	for(pA=one, pB=two; *pA && *pB; pA++, pB++) 
	{
		int tmp = XP_TO_LOWER(*pA) - XP_TO_LOWER(*pB);
		if (tmp) 
			return tmp;
	}
	if (*pA) 
		return 1;	
	if (*pB) 
		return -1;
	return 0;	
}

//#endif // XP_MACOSX


//------------------------------------------------------------------------------------
// NPP_Initialize:
//------------------------------------------------------------------------------------
NPError NPP_Initialize(void)
{
	return CPlugin::Initialize();
}


//------------------------------------------------------------------------------------
// NPP_Shutdown:
//------------------------------------------------------------------------------------
void NPP_Shutdown(void)
{
	CPlugin::Shutdown();
}


//------------------------------------------------------------------------------------
// NPP_New:
//------------------------------------------------------------------------------------
NPError NPP_New(NPMIMEType type, NPP instance, uint16 mode, int16 argc, char* argn[], char* argv[], NPSavedData*)
{
	if (instance == NULL)
		return NPERR_INVALID_INSTANCE_ERROR;
		
	CPlugin* This = (CPlugin*) (char*)NPN_MemAlloc(sizeof(CPlugin));
	instance->pdata = This;
	if (This != NULL)
	{
		This->Constructor(instance, type, mode, argc, argn, argv);
		return NPERR_NO_ERROR;
	}
	else
		return NPERR_OUT_OF_MEMORY_ERROR;
}




//------------------------------------------------------------------------------------
// NPP_Destroy:
//------------------------------------------------------------------------------------
NPError NP_LOADDS
NPP_Destroy(NPP instance, NPSavedData** /*save*/)
{
	if (instance == NULL)
		return NPERR_INVALID_INSTANCE_ERROR;

	CPlugin* This = (CPlugin*) instance->pdata;
	
	if (This != NULL)
	{	
		This->Destructor();
		NPN_MemFree(This);
		instance->pdata = NULL;
	}

	return NPERR_NO_ERROR;
}




//------------------------------------------------------------------------------------
// NPP_SetWindow:
//------------------------------------------------------------------------------------
NPError NPP_SetWindow(NPP instance, NPWindow* window)
{
	if (instance == NULL)
		return NPERR_INVALID_INSTANCE_ERROR;

	CPlugin* This = (CPlugin*) instance->pdata;
	if (This != NULL)
		This->SetWindow(window);
	
	return NPERR_NO_ERROR;
}



//------------------------------------------------------------------------------------
// NPP_NewStream:
//------------------------------------------------------------------------------------
NPError NP_LOADDS
NPP_NewStream(NPP instance,
							NPMIMEType /*type*/,
							NPStream* /*stream*/, 
							NPBool /*seekable*/,
							uint16* /*stype*/)
{
	if (instance == NULL)
		return NPERR_INVALID_INSTANCE_ERROR;

	return NPERR_NO_ERROR;
}



int32 STREAMBUFSIZE = 0X0FFFFFFF;   // If we are reading from a file in NPAsFile
                                    // mode so we can take any size stream in our
                                    // write call (since we ignore it)

//------------------------------------------------------------------------------------
// NPP_WriteReady:
//------------------------------------------------------------------------------------
int32 NP_LOADDS
NPP_WriteReady(NPP /*instance*/, NPStream* /*stream*/)
{
	return STREAMBUFSIZE;   // Number of bytes ready to accept in NPP_Write()
}



//------------------------------------------------------------------------------------
// NPP_Write:
//------------------------------------------------------------------------------------
int32 NP_LOADDS
NPP_Write(NPP /*instance*/, NPStream* /*stream*/, int32 /*offset*/, int32 len, void* /*buffer*/)
{
	return len; 			// The number of bytes accepted
}



//------------------------------------------------------------------------------------
// NPP_DestroyStream:
//------------------------------------------------------------------------------------
NPError NP_LOADDS
NPP_DestroyStream(NPP instance, NPStream* /*stream*/, NPError /*reason*/)
{
	if (instance == NULL)
		return NPERR_INVALID_INSTANCE_ERROR;

	return NPERR_NO_ERROR;
}


//------------------------------------------------------------------------------------
// NPP_StreamAsFile:
//------------------------------------------------------------------------------------
void NP_LOADDS
NPP_StreamAsFile(NPP /*instance*/, NPStream */*stream*/, const char* /*fname*/)
{
}



//------------------------------------------------------------------------------------
// NPP_Print:
//------------------------------------------------------------------------------------
void NP_LOADDS
NPP_Print(NPP instance, NPPrint* printInfo)
{
	if (printInfo == NULL)
		return;

	if (instance != NULL)
	{
		if (printInfo->mode == NP_FULL)
			printInfo->print.fullPrint.pluginPrinted = FALSE; // Do the default
		else	// If not fullscreen, we must be embedded
		{
			CPlugin* This = (CPlugin*) instance->pdata;
			if (This != NULL)
				This->Print(printInfo);
		}
	}

}


//------------------------------------------------------------------------------------
// NPP_HandleEvent:
// Mac-only.
//------------------------------------------------------------------------------------
int16 NPP_HandleEvent(NPP instance, void* event)
{
	if (instance != NULL)
	{	
		CPlugin* This = (CPlugin*) instance->pdata;
		if (This != NULL && event != NULL)
			return This->HandleEvent((EventRecord*) event);
	}
	
	return FALSE;
}


//------------------------------------------------------------------------------------
// NPP_URLNotify:
//------------------------------------------------------------------------------------
void NPP_URLNotify(NPP /*instance*/, const char* /*url*/, NPReason /*reason*/, void* /*notifyData*/)
{
}

#ifdef OJI
//------------------------------------------------------------------------------------
// NPP_GetJavaClass:
//------------------------------------------------------------------------------------
jref NPP_GetJavaClass(void)
{
	return NULL;
}
#endif /* OJI */

#pragma mark -

//------------------------------------------------------------------------------------
// CPlugin::Initialize:
//------------------------------------------------------------------------------------
NPError CPlugin::Initialize()
{
	Handle	string;
	short	saveResFile = CurResFile();

	UseResFile(gResFile);

	long	systemVersion;
	OSErr	err = ::Gestalt(gestaltSystemVersion, &systemVersion);
	sRunningOnOSX = (err == noErr) && (systemVersion >= 0x00001000);

	// Get Resources
	CPlugin::sIconHandle = GetCIcon(rBrokenPluginIcon);
	CPlugin::sHandCursor = GetCursor(rHandCursor);

	// Get "alt text" string
	string = Get1Resource('STR ', rAltTextString);
	if (string && *string)
	{
		short stringLen = (*string)[0];
		CPlugin::sAltText = (char*)NPN_MemAlloc(stringLen +1);
		if (CPlugin::sAltText != NULL)
		{
			short src = 1;
			short dest = 0;
			while (src <= stringLen)
				CPlugin::sAltText[dest++] = (*string)[src++];
			CPlugin::sAltText[dest++] = 0;
		}
	}
	ReleaseResource(string);
	
	// Get "refresh text" string
	string = Get1Resource('STR ', rRefreshTextString);
	if (string && *string)
	{
		short stringLen = (*string)[0];
		CPlugin::sRefreshText = (char*)NPN_MemAlloc(stringLen + 1);
		if (CPlugin::sRefreshText != NULL)
		{
			short src = 1;
			short dest = 0;
			while (src <= stringLen)
				CPlugin::sRefreshText[dest++] = (*string)[src++];
			CPlugin::sRefreshText[dest++] = 0;
		}
	}
	ReleaseResource(string);
	
	// Get JavaScript install command string
	string = Get1Resource('STR ', rJavaScriptInstallCommand);
	if (string && *string)
	{
		short stringLen = (*string)[0];
		CPlugin::sInstallCommand = (char*)NPN_MemAlloc(stringLen + 1);
		if (CPlugin::sInstallCommand != NULL)
		{
			short src = 1;
			short dest = 0;
			while (src <= stringLen)
				CPlugin::sInstallCommand[dest++] = (*string)[src++];
			CPlugin::sInstallCommand[dest++] = 0;
		}
	}
	ReleaseResource(string);

	// Get default plug-in page URL
	string = Get1Resource('STR ', rDefaultPluginURL);
	if (string && *string)
	{
		short stringLen = (*string)[0];
		CPlugin::sDefaultPage = (char*)NPN_MemAlloc(stringLen + 1);
		if (CPlugin::sDefaultPage != NULL)
		{
			short src = 1;
			short dest = 0;
			while (src <= stringLen)
				CPlugin::sDefaultPage[dest++] = (*string)[src++];
			CPlugin::sDefaultPage[dest++] = 0;
		}
	}
	ReleaseResource(string);

	// Get javascript plug-in page URL
	string = Get1Resource('STR ', rJavaScriptPageURL);
	if (string && *string)
	{
		short stringLen = (*string)[0];
		CPlugin::sJavaScriptPage = (char*)NPN_MemAlloc(stringLen + 1);
		if (CPlugin::sJavaScriptPage != NULL)
		{
			short src = 1;
			short dest = 0;
			while (src <= stringLen)
				CPlugin::sJavaScriptPage[dest++] = (*string)[src++];
			CPlugin::sJavaScriptPage[dest++] = 0;
		}
	}
	ReleaseResource(string);

	UseResFile(saveResFile);

	if (!sRunningOnOSX) // We'll make some CFPreferences the first time we have to on OS X
	{
		ProcessSerialNumber psn;
		ProcessInfoRec 	info;
		FSSpec	fsTheApp;
		SInt16	wResFile;
		OSErr	wErr;
		
		psn.highLongOfPSN = 0;
		psn.lowLongOfPSN  = kCurrentProcess;

		info.processInfoLength = sizeof(ProcessInfoRec);
		info.processName = nil;
		info.processAppSpec = &fsTheApp;
		wErr = ::GetProcessInformation(&psn, &info);
		if (wErr == noErr) {
			wErr = FSMakeFSSpec(fsTheApp.vRefNum, fsTheApp.parID, "\p:Plug-ins:Default Plug-in Data", &sDataFileSpec);
			if (wErr == fnfErr) {
				FSpCreateResFile(&sDataFileSpec, 'MOSS', 'BINA', smSystemScript);
				wResFile = FSpOpenResFile(&CPlugin::sDataFileSpec, fsRdWrPerm);
				if (wResFile != -1) {
					// create a STR# with 0 entires (i.e. only a count)
					string = NewHandleClear(sizeof(SInt16));
					AddResource(string, 'STR#', rTypeListStrings, "\p");
					UpdateResFile(wResFile);
					ReleaseResource(string);
				}
				FSClose(wResFile);
			}
		}
	}

	return NPERR_NO_ERROR;
}


//------------------------------------------------------------------------------------
// CPlugin::Shutdown:
//------------------------------------------------------------------------------------
void CPlugin::Shutdown()
{
	if (CPlugin::sIconHandle != NULL)
		::ReleaseResource((Handle) CPlugin::sIconHandle);
	if (CPlugin::sHandCursor != NULL)
		::ReleaseResource((Handle) CPlugin::sHandCursor);
	
	if (CPlugin::sAltText != NULL)
		NPN_MemFree(CPlugin::sAltText);
	if (CPlugin::sInstallCommand != NULL)
		NPN_MemFree(CPlugin::sInstallCommand);
	if (CPlugin::sDefaultPage != NULL)
		NPN_MemFree(CPlugin::sDefaultPage);
	if(CPlugin::sRefreshText != NULL)
		NPN_MemFree(CPlugin::sRefreshText);
}


//------------------------------------------------------------------------------------
// CPlugin::Constructor:
//------------------------------------------------------------------------------------
void CPlugin::Constructor(NPP instance, NPMIMEType type, uint16 mode, int16 argc, char* argn[], char* argv[])
{
	fWindow = NULL;
	fPageURL = NULL;
	fFileURL = NULL;
	fInstance = instance;
	fMode = mode;    // Mode is NP_EMBED, NP_FULL, or NP_BACKGROUND (see npapi.h)
	fAskedLoadURL = false;
	fUserInstalledPlugin = false;
	
	// Save a copy of our mime type string
	short typeLength = strlen(type);
	fType = (char*)NPN_MemAlloc(typeLength+1);
	if (fType != NULL)
		strcpy(fType, type);
	
	// Make a handy region for use in FocusDraw
	fSaveClip = NewRgn();

	// determine if the plugin is specified as HIDDEN
	if(IsPluginHidden(argc, argn, argv))
		fHiddenPlugin = true;
	else
		fHiddenPlugin = false;

	// Get some information about our environment
	NPN_GetValue(fInstance, NPNVisOfflineBool, (void *)&m_bOffline);
	NPN_GetValue(fInstance, NPNVjavascriptEnabledBool, (void *)&m_bJavaScript);

	// Figure out what URL we will go to
	DetermineURL(argc, argn, argv);
}


//------------------------------------------------------------------------------------
// CPlugin::Destructor:
//------------------------------------------------------------------------------------
void CPlugin::Destructor()
{
	if (fSaveClip != NULL)
		DisposeRgn(fSaveClip);
	
	if (fType != NULL)
		NPN_MemFree(fType);
	if (fFileURL != NULL)
		NPN_MemFree(fFileURL);
	if (fPageURL != NULL)
		NPN_MemFree(fPageURL);
}		



//------------------------------------------------------------------------------------
// CPlugin::SetWindow:
//------------------------------------------------------------------------------------
void CPlugin::SetWindow(NPWindow* window)
{
	fWindow = window;
}


//------------------------------------------------------------------------------------
// CPlugin::Print:
// 
// To print, we need to retrieve the printing window from the printInfo,
// temporarily make it our window, draw into it, and restore the window.
//
//------------------------------------------------------------------------------------
void CPlugin::Print(NPPrint* printInfo)
{
	NPWindow* printWindow = &(printInfo->print.embedPrint.window);
	
	NPWindow* oldWindow = fWindow;
	fWindow = printWindow;

	if (FocusDraw())
	{
		Draw(kUnhilited);
		RestoreDraw();
	}
	
	fWindow = oldWindow;
}


//------------------------------------------------------------------------------------
// CPlugin::HandleEvent:
//------------------------------------------------------------------------------------
Boolean CPlugin::HandleEvent(EventRecord* ev)
{
	Boolean eventHandled = false;
	
	switch (ev->what)
	{
		case mouseDown:
			MouseDown();
			eventHandled = true;
			break;
			
		case updateEvt:
			if (FocusDraw()) {
				Draw(kUnhilited);
				RestoreDraw();
			}
			eventHandled = true;
			break;
			
		case NPEventType_AdjustCursorEvent:
			if (CPlugin::sHandCursor != NULL)
				SetCursor(*CPlugin::sHandCursor);
			if (fUserInstalledPlugin) {
				if (CPlugin::sRefreshText != NULL)
					NPN_Status(fInstance, CPlugin::sRefreshText);				
			} else {
				if (CPlugin::sAltText != NULL)
					NPN_Status(fInstance, CPlugin::sAltText);
			}
			eventHandled = true;
			break;
			
		case nullEvent:
			//
			// NOTE: We have to wait until idle time
			// to ask the user if they want to visit
			// the URL to avoid reentering XP code.
			//
			if (!fAskedLoadURL) {
				if (CheckMimeTypes())
					AskAndLoadURL();
				fAskedLoadURL = true;
			}
			break;
		default:
			break;
	}
		
	return eventHandled;
}



//------------------------------------------------------------------------------------
// CPlugin::Draw:
//------------------------------------------------------------------------------------
void CPlugin::Draw(HiliteState hilite)
{
	UInt8		*pTheText;
	SInt32		height = fWindow->height;
	SInt32		width = fWindow->width;
	SInt32		centerX = (width) >> 1;
	SInt32		centerY = (height) >> 1;
	Rect		drawRect;
	RGBColor	black = { 0x0000, 0x0000, 0x0000 };
	RGBColor	white = { 0xFFFF, 0xFFFF, 0xFFFF };
	RGBColor	hiliteColor = { 0x0000, 0x0000, 0x0000 };
	short		transform;

	drawRect.top = 0;
	drawRect.left = 0;
	drawRect.bottom = height;
	drawRect.right = width;

	if (height < 4 && width < 4)
		return;
		
	PenNormal();
	RGBForeColor(&black);
	RGBBackColor(&white);

#if !TARGET_API_MAC_CARBON
	FillRect(&drawRect, &(gQDPtr->white));
#else
	Pattern qdWhite;
	FillRect(&drawRect, GetQDGlobalsWhite(&qdWhite));
#endif

	if (hilite == kHilited) {
		hiliteColor.red = 0xFFFF;
		transform = ttSelected;
	} else {
		hiliteColor.blue = 0xFFFF;
		transform = ttNone;
	}

	RGBForeColor(&hiliteColor);
	FrameRect(&drawRect);
	
	if (height > 32 && width > 32 && CPlugin::sIconHandle != NULL)
	{
		drawRect.top = centerY - 16;
		drawRect.bottom = centerY + 16;
		drawRect.left = centerX - 16;
		drawRect.right = centerX + 16;
		PlotCIconHandle(&drawRect, atAbsoluteCenter, transform, CPlugin::sIconHandle);
	}

	if (fUserInstalledPlugin) {
		pTheText = (unsigned char*)CPlugin::sRefreshText;
	} else {
		pTheText = (unsigned char*)CPlugin::sAltText;
	}
		DrawString(pTheText, width, height, centerX, drawRect);
}



//------------------------------------------------------------------------------------
// CPlugin::MouseDown:
//
// Track the click in our plugin by drawing the icon enabled or disabled
// as the user moves the mouse in and out with the button held down.  If
// they let up the mouse while still inside, get the URL.
//
//------------------------------------------------------------------------------------
void  CPlugin::MouseDown()
{
	if (FocusDraw())	
	{
		Draw(kHilited);
		Boolean inside = true;
  
		// evil CPU-hogging loop on Mac OS X!
		while (StillDown())
		{
			Point localMouse;
			GetMouse(&localMouse);
			Boolean insideNow = ::PtInRect(localMouse, &fRevealedRect);

			if (insideNow != inside)
			{
				Draw(insideNow ? kHilited : kUnhilited);
				inside = insideNow;
			}
		}
		
		if (inside) {
			Draw(kUnhilited);
			if (!fUserInstalledPlugin)
				AskAndLoadURL();
			else
				RefreshPluginPage();
		}

		RestoreDraw();
	}
}


//------------------------------------------------------------------------------------
// CPlugin::FocusDraw:
//------------------------------------------------------------------------------------
Boolean CPlugin::FocusDraw()
{
	if (fWindow == NULL)
		return false;
		
	NP_Port* npport = (NP_Port*) fWindow->window;
	CGrafPtr ourPort = npport->port;
	
	if (fWindow->clipRect.left < fWindow->clipRect.right)
	{
		GetPort(&fSavePort);
		SetPort((GrafPtr) ourPort);
		Rect portRect;
#if !TARGET_API_MAC_CARBON
		portRect = ourPort->portRect;
#else
		GetPortBounds(ourPort, &portRect);
#endif
		fSavePortTop = portRect.top;
		fSavePortLeft = portRect.left;
		GetClip(fSaveClip);
		
		fRevealedRect.top = fWindow->clipRect.top + npport->porty;
		fRevealedRect.left = fWindow->clipRect.left + npport->portx;
		fRevealedRect.bottom = fWindow->clipRect.bottom + npport->porty;
		fRevealedRect.right = fWindow->clipRect.right + npport->portx;
		SetOrigin(npport->portx, npport->porty);
		ClipRect(&fRevealedRect);

		return true;
	}
	else
		return false;
}


//------------------------------------------------------------------------------------
// CPlugin::RestoreDraw:
//------------------------------------------------------------------------------------
void CPlugin::RestoreDraw()
{
	SetOrigin(fSavePortLeft, fSavePortTop);
	SetClip(fSaveClip);
	SetPort(fSavePort);
}



//------------------------------------------------------------------------------------
// CPlugin::DetermineURL:
//
// Get a URL from either the parameters passed from the EMBED.
// Append "?" and our mime type and save for later use.
//
//------------------------------------------------------------------------------------
void CPlugin::DetermineURL(int16 argc, char* argn[], char* argv[])
{
	char*	url;
	SInt32	additionalLength = 0;
	SInt32	i;

	// Appended to the URL will be a "?" and the mime type of this instance.  This lets the server
	// do something intelligent with a CGI script.

	if (fType != NULL)
		additionalLength += (strlen(fType) + 1);		// Add 1 for '?'

	// The page designer can specify a URL where the plugin for this type can be downloaded.  Here
	// we scan the arguments for this attribute and save it away if we
	// find it for later use by LoadPluginURL().
	//
	for (i = 0; i < argc; i++) {
		if ((strcasecmp(argn[i], "PLUGINSPAGE") == 0) || (strcasecmp(argn[i], "CODEBASE") == 0)) {
			url = argv[i];
			fPageURL = (char*)NPN_MemAlloc(strlen(url) + 1 + additionalLength);	// Add 1 for '\0'
			if (fPageURL != NULL) {
				if (additionalLength > 0) {
					sprintf(fPageURL, "%s?%s", url, fType);
				} else {
					strcpy(fPageURL, url);	
				}
			}
			break;
		} else if ((strcasecmp(argn[i], "PLUGINURL") == 0) || (strcasecmp(argn[i], "CLASSID") == 0)) {
			url = argv[i];
			if (CPlugin::sInstallCommand != NULL) {
				// Allocate a new string
				fFileURL = (char*)NPN_MemAlloc(strlen(CPlugin::sInstallCommand) + 1 + strlen(url));	
				if (fFileURL != NULL)
					sprintf(fFileURL, CPlugin::sInstallCommand, url);
			}
			break;
		}
	}
}



//------------------------------------------------------------------------------------
// CPlugin::MakeDefaultURL:
//
// Get a URL from our resources.  Append "?" and our mime type and save for later use.
//
//------------------------------------------------------------------------------------
char *CPlugin::MakeDefaultURL(void)
{
	char	*pDefURL = NULL;
	SInt32	additionalLength = 0;

	// Appended to the URL will be a "?" and the mime type of this instance.  This lets the server
	// do something intelligent with a CGI script.

	if (fType != NULL)
		additionalLength += (strlen(fType) + 1);		// Add 1 for '?'

	if (!m_bJavaScript) {
		if (CPlugin::sDefaultPage != NULL) {
			pDefURL = (char*)NPN_MemAlloc(strlen(CPlugin::sDefaultPage) + 1 + additionalLength);
			if (pDefURL != NULL) {
				if (additionalLength > 0) {
					sprintf(pDefURL, "%s?%s", CPlugin::sDefaultPage, fType);
				} else {
					strcpy(pDefURL, CPlugin::sDefaultPage);	
				}
			}
		}
	} else {
		if (CPlugin::sJavaScriptPage != NULL) {
			pDefURL = (char*)NPN_MemAlloc(strlen(szPluginFinderCommandBeginning) +
						strlen(CPlugin::sJavaScriptPage) +
						additionalLength + strlen(szPluginFinderCommandEnd) + 1);
			if (pDefURL != NULL) {
				sprintf(pDefURL, "%s%s%s%s", szPluginFinderCommandBeginning,
						CPlugin::sJavaScriptPage, fType, szPluginFinderCommandEnd);
			}
		}
	}
	return(pDefURL);
}



//------------------------------------------------------------------------------------
// CPlugin::AddMimeTypeToList:
//
// Check the mime type of this instance against our list
// of types weve seen before.  If we find our type in the
// list, return false; otherwise, return true.
//
// type 'STR#' {
//      integer = $$Countof(StringArray);
//      array StringArray {
//              pstring;
//      };
//
//------------------------------------------------------------------------------------
void CPlugin::AddMimeTypeToList(StringPtr cTypeString)
{
	if (sRunningOnOSX)
	{
		CFStringRef		pluginKey	= CFSTR("DefaultPluginSeenTypes"); // don't release this
		CFStringRef		mimeType	= ::CFStringCreateWithPascalString(kCFAllocatorDefault, cTypeString, kCFStringEncodingASCII);
		CFArrayRef		prefsList	= (CFArrayRef)::CFPreferencesCopyAppValue(pluginKey, kCFPreferencesCurrentApplication);
		Boolean			foundType	= false;

		if (prefsList == NULL)
		{
			CFStringRef stringArray[1];
			stringArray[0] = mimeType;
			
			prefsList = ::CFArrayCreate(kCFAllocatorDefault, (const void **)stringArray, 1, &kCFTypeArrayCallBacks);
			if (prefsList)
			{
				::CFPreferencesSetAppValue(pluginKey, prefsList, kCFPreferencesCurrentApplication);
				::CFRelease(prefsList);
			}
		}
		else
		{
			if (::CFGetTypeID(prefsList) == ::CFArrayGetTypeID())
			{
				// first make sure it's not in the list
				CFIndex count = ::CFArrayGetCount(prefsList);
				for (CFIndex i = 0; i < count; i ++)
				{
					CFStringRef item = (CFStringRef)::CFArrayGetValueAtIndex(prefsList, i); // does not retain
					if (item &&
						(::CFGetTypeID(item) == ::CFStringGetTypeID()) &&
						(::CFStringCompareWithOptions(item, mimeType,
								CFRangeMake(0, ::CFStringGetLength(item)), kCFCompareCaseInsensitive) == kCFCompareEqualTo))
					{
						foundType = true;
						break;
					}
				}
				
				if (!foundType && !fHiddenPlugin)
				{
					CFMutableArrayRef typesArray = ::CFArrayCreateMutableCopy(kCFAllocatorDefault, 0, (CFArrayRef)prefsList);
					if (typesArray)
					{
						::CFArrayAppendValue(typesArray, mimeType);
						::CFPreferencesSetAppValue(pluginKey, typesArray, kCFPreferencesCurrentApplication);
					}
				}
			}
			::CFRelease(prefsList);
		}
		::CFRelease(mimeType);
	}
	else
	{
		Handle	hTypeList;
		SInt32	dwCount;
		SInt32	index;
		Str255	oldType;
		SInt16	wResFile;
		Boolean failedToFind = true;

		wResFile = FSpOpenResFile(&CPlugin::sDataFileSpec, fsRdWrPerm);
		if (wResFile != -1) {
			hTypeList = Get1Resource('STR#', rTypeListStrings);
			if (hTypeList != NULL) {
				dwCount = **((short **)hTypeList);
				
				// First make sure it's not already in the list.
				for (index = 1; index <= dwCount; index++) {
					GetIndString(oldType, rTypeListStrings, index);

					// if the mimetype already exists in our list, or the plugin is NOT hidden,
					// don't bring up the dialog box
					if (EqualString(cTypeString, oldType, true, true) && !fHiddenPlugin) {
						failedToFind = false;
						break;							// Found a match, so bail out!
					}
				}
				if (failedToFind) {
					// Grow the string list handle
					Size itsSize = GetHandleSize(hTypeList);
					Size typeSize = cTypeString[0] + 1;
					SetHandleSize(hTypeList, itsSize + typeSize);
				
					// Increment the count of strings in the list
					(**((short**)hTypeList)) = (short)(++dwCount);	

					// Copy the data from our string into the handle
					long dwCount = Munger(hTypeList, itsSize, NULL, typeSize, cTypeString, typeSize);

					// Mark the resource as changed so it will be written out
					if (dwCount > 0) {
						ChangedResource(hTypeList);
						UpdateResFile(wResFile);
					}
				}
				ReleaseResource(hTypeList);
			}
			FSClose(wResFile);
		}
	}
}



//------------------------------------------------------------------------------------
// CPlugin::CheckMimeTypes:
//
// Check the mime type of this instance against our list
// of types weve seen before.  If we find our type in the
// list, return false; otherwise, return true.
//
// type 'STR#' {
//      integer = $$Countof(StringArray);
//      array StringArray {
//              pstring;
//      };
//
//------------------------------------------------------------------------------------
Boolean CPlugin::CheckMimeTypes()
{
	Boolean failedToFind = true;

	if (sRunningOnOSX)
	{
		CFStringRef		pluginKey = CFSTR("DefaultPluginSeenTypes"); // don't release this
		CFStringRef		mimeType  = ::CFStringCreateWithCString(kCFAllocatorDefault, fType, kCFStringEncodingASCII);
		CFArrayRef		prefsList = (CFArrayRef)::CFPreferencesCopyAppValue(pluginKey, kCFPreferencesCurrentApplication);
		if (prefsList)
		{
			if (::CFGetTypeID(prefsList) == ::CFArrayGetTypeID())
			{
				CFIndex count = ::CFArrayGetCount(prefsList);
				for (CFIndex i = 0; i < count; i ++)
				{
					CFStringRef item = (CFStringRef)::CFArrayGetValueAtIndex(prefsList, i); // does not retain
					if (item &&
						(::CFGetTypeID(item) == ::CFStringGetTypeID()) &&
						(::CFStringCompareWithOptions(item, mimeType,
								CFRangeMake(0, ::CFStringGetLength(item)), kCFCompareCaseInsensitive) == kCFCompareEqualTo))
					{
						failedToFind = false;
						break;
					}
				}
			}
			::CFRelease(prefsList);
		}
		::CFRelease(mimeType);
	}
	else
	{
		Handle	hTypeList;
		SInt32	index;
		Str255	oldType;
		Str255	ourType;
		SInt16	wResFile;

		wResFile = FSpOpenResFile(&CPlugin::sDataFileSpec, fsRdPerm);
		if (wResFile != -1) {
			hTypeList = Get1Resource('STR#', rTypeListStrings);
			if (hTypeList != NULL) {
				// Convert the mime-type C string to a Pascal string.
				index = strlen(fType);
				if (index > 255) {		// don't blow out the Str255
					index = 255;
				}
				BlockMoveData(fType, &ourType[1], index);
				ourType[0] = index;

				short count = **((short **)hTypeList);
				
				// Iterate through all the strings in the list.
				for (index = 1; index <= count; index++) {
					GetIndString(oldType, rTypeListStrings, index);

					// if the mimetype already exists in our list, or the plugin is NOT hidden,
					// don't bring up the dialog box
					if (EqualString(ourType, oldType, true, true) && !fHiddenPlugin) {
						failedToFind = false;
						break;							// Found a match, so bail out!
					}
				}
				ReleaseResource(hTypeList);
			}
			FSClose(wResFile);
		}
	}
	return(failedToFind);
}



//------------------------------------------------------------------------------------
// CPlugin::AskAndLoadURL:
//------------------------------------------------------------------------------------
void CPlugin::AskAndLoadURL()
{
	char	*pTheURL;
	SInt32	dwLen;
	Str255	ourType;

	if (!m_bOffline) {
		// Convert the mime-type C string to a Pascal string.
		dwLen = strlen(fType);
		if (dwLen > 255) {		// don't blow out the Str255
			dwLen = 255;
		}
		BlockMoveData(fType, &ourType[1], dwLen);
		ourType[0] = dwLen;

		// NOTE: We need to set the cursor because almost always we will have set it to the
		// hand cursor before we get here.
#if !TARGET_API_MAC_CARBON
		SetCursor(&(gQDPtr->arrow));
#else
		Cursor qdArrow;
		SetCursor(GetQDGlobalsArrow(&qdArrow));
#endif

		// Now that weve queried the user about this mime type,
		// add it to our list so we wont bug them again.
		AddMimeTypeToList(ourType);
		
		//
		// If the user clicked "Get the Plug-in", either execute the
		// JavaScript file-installation URL, or ask Netscape to open
		// a new window with the page URL.  The title of the window
		// is arbitrary since it has nothing to do with the actual
		// window title shown to the user (its only used internally).
		//
		if (fFileURL != NULL) {
			(void) NPN_GetURL(fInstance, fFileURL, "_current");
		} else if (fPageURL != NULL) {
			NPN_GetURL(fInstance, fPageURL, "_blank");
		} else {
			pTheURL = MakeDefaultURL();
			if (!m_bJavaScript) {
				NPN_GetURL(fInstance, pTheURL, "_blank");
			} else {
				NPN_GetURL(fInstance, pTheURL, NULL);
			}
			NPN_MemFree(pTheURL);
		}

		fUserInstalledPlugin = true;
		if (FocusDraw()) {
			Draw(kUnhilited);
			RestoreDraw();
		}
	}
}

void CPlugin::RefreshPluginPage()
{
	(void) NPN_GetURL(fInstance, "javascript:navigator.plugins.refresh(true);", "_self");
}

void CPlugin::DrawString(const unsigned char* text, short width, short height, short centerX, Rect drawRect)
{
	short length, textHeight, textWidth;
 
	if(text == NULL)
		return;
	
	length = strlen((char*)text);
	TextFont(20);
	TextFace(underline);
	TextMode(srcCopy);
	TextSize(10);
	
	FontInfo fontInfo;
	GetFontInfo(&fontInfo);

	textHeight = fontInfo.ascent + fontInfo.descent + fontInfo.leading;
	textWidth = TextWidth(text, 0, length);
		
	if (width > textWidth && height > textHeight + 32)
	{
		MoveTo(centerX - (textWidth >> 1), drawRect.bottom + textHeight);
		DrawText(text, 0, length);
	}		
}

Boolean CPlugin::IsPluginHidden(int16 argc, char* argn[], char* argv[])
{
	int i;
	for(i=0; i<argc; i++)
	{
		if(!strcasecmp(argn[i], "HIDDEN"))
			if(!strcasecmp(argv[i], "TRUE"))
				return true;
	}
	return false;
}