summaryrefslogtreecommitdiff
path: root/XMPFiles/source/PluginHandler/HostAPIImpl.cpp
blob: cf6a3895373fdc7136ec9b6a5ee1a501269307bf (plain)
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
// =================================================================================================
// ADOBE SYSTEMS INCORPORATED
// Copyright 2011 Adobe Systems Incorporated
// All Rights Reserved
//
// NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the terms
// of the Adobe license agreement accompanying it.
// =================================================================================================

#include "HostAPI.h"
#include "PluginManager.h"
#include "FileHandlerInstance.h"
#include "source/XIO.hpp"
#include "XMPFiles/source/HandlerRegistry.h"

using namespace Common;

namespace XMP_PLUGIN
{

///////////////////////////////////////////////////////////////////////////////
//
//		Exception handler
//

static void HandleException( WXMP_Error* wError ) 
{
	try
	{
		throw;
	}
	catch(  XMP_Error& xmpErr )
	{
		wError->mErrorMsg = xmpErr.GetErrMsg();
		wError->mErrorID = xmpErr.GetID();
	}
	catch( std::exception& stdErr )
	{
		wError->mErrorMsg = stdErr.what();
	}
	catch( ... )
	{
		wError->mErrorMsg = "Caught unknown exception";
	}
}

///////////////////////////////////////////////////////////////////////////////
//
//		FileIO_API
//

static XMPErrorID FileSysRead( XMP_IORef io, void* buffer, XMP_Uns32 count, XMP_Bool readAll, XMP_Uns32& byteRead, WXMP_Error * wError )
{
	if( wError == NULL )	return kXMPErr_BadParam;

	wError->mErrorID = kXMPErr_InternalFailure;

	try
	{
		if( io != NULL )
		{
			::XMP_IO * thiz = (::XMP_IO*)io;
			byteRead = thiz->Read( buffer, count, ConvertXMP_BoolToBool( readAll ) );
			wError->mErrorID = kXMPErr_NoError;
		}
	}
	catch( ... )
	{
		HandleException( wError );
	}

	return wError->mErrorID;
}

static XMPErrorID FileSysWrite( XMP_IORef io, void* buffer, XMP_Uns32 count, WXMP_Error * wError )
{
	if( wError == NULL )	return kXMPErr_BadParam;

	wError->mErrorID = kXMPErr_InternalFailure;

	try
	{
		if( io != NULL )
		{
			::XMP_IO * thiz = (::XMP_IO*)io;
			thiz->Write( buffer, count );
			wError->mErrorID = kXMPErr_NoError;
		}
	}
	catch( ... )
	{
		HandleException( wError );
	}

	return wError->mErrorID;
}

static XMPErrorID FileSysSeek( XMP_IORef io, XMP_Int64& offset, SeekMode mode, WXMP_Error * wError )
{
	if( wError == NULL )	return kXMPErr_BadParam;

	wError->mErrorID = kXMPErr_InternalFailure;

	try
	{
		if( io != NULL )
		{
			::XMP_IO * thiz = (::XMP_IO*)io;
			thiz->Seek( offset, mode );
			wError->mErrorID = kXMPErr_NoError;
		}
	}
	catch( ... )
	{
		HandleException( wError );
	}

	return wError->mErrorID;
}

static XMPErrorID FileSysLength( XMP_IORef io, XMP_Int64& length, WXMP_Error * wError )
{
	if( wError == NULL )	return kXMPErr_BadParam;

	wError->mErrorID = kXMPErr_InternalFailure;

	try
	{
		if( io != NULL )
		{
			::XMP_IO * thiz = (::XMP_IO*)io;
			length = thiz->Length();
			wError->mErrorID = kXMPErr_NoError;
		}
	}
	catch( ... )
	{
		HandleException( wError );
	}

	return wError->mErrorID;
}

static XMPErrorID FileSysTruncate( XMP_IORef io, XMP_Int64 length, WXMP_Error * wError )
{
	if( wError == NULL )	return kXMPErr_BadParam;

	wError->mErrorID = kXMPErr_InternalFailure;

	try
	{
		if( io != NULL )
		{
			::XMP_IO * thiz = (::XMP_IO*)io;
			thiz->Truncate( length );
			wError->mErrorID = kXMPErr_NoError;
		}
	}
	catch( ... )
	{
		HandleException( wError );
	}

	return wError->mErrorID;
}

static XMPErrorID FileSysDeriveTemp( XMP_IORef io, XMP_IORef& tempIO, WXMP_Error * wError )
{
	if( wError == NULL )	return kXMPErr_BadParam;

	wError->mErrorID = kXMPErr_InternalFailure;

	try
	{
		if( io != NULL )
		{
			::XMP_IO * thiz = (::XMP_IO*)io;
			tempIO = thiz->DeriveTemp();
			wError->mErrorID = kXMPErr_NoError;
		}
	}
	catch( ... )
	{
		HandleException( wError );
	}

	return wError->mErrorID;
}

static XMPErrorID FileSysAbsorbTemp( XMP_IORef io, WXMP_Error * wError )
{
	if( wError == NULL )	return kXMPErr_BadParam;

	wError->mErrorID = kXMPErr_InternalFailure;

	try
	{
		if( io != NULL )
		{
			::XMP_IO * thiz = (::XMP_IO*)io;
			thiz->AbsorbTemp();
			wError->mErrorID = kXMPErr_NoError;
		}
	}
	catch( ... )
	{
		HandleException( wError );
	}

	return wError->mErrorID;
}

static XMPErrorID FileSysDeleteTemp( XMP_IORef io, WXMP_Error * wError )
{
	if( wError == NULL )	return kXMPErr_BadParam;

	wError->mErrorID = kXMPErr_InternalFailure;

	try
	{
		if( io != NULL )
		{
			::XMP_IO * thiz = (::XMP_IO*)io;
			thiz->DeleteTemp();
			wError->mErrorID = kXMPErr_NoError;
		}
	}
	catch( ... )
	{
		HandleException( wError );
	}

	return wError->mErrorID;
}

static void GetFileSysAPI( FileIO_API* fileSys )
{
	if( fileSys )
	{
		fileSys->mReadProc			=	FileSysRead;
		fileSys->mWriteProc			=	FileSysWrite;
		fileSys->mSeekProc			=	FileSysSeek;
		fileSys->mLengthProc		=	FileSysLength;
		fileSys->mTruncateProc		=	FileSysTruncate;
		fileSys->mDeriveTempProc	=	FileSysDeriveTemp;
		fileSys->mAbsorbTempProc	=	FileSysAbsorbTemp;
		fileSys->mDeleteTempProc	=	FileSysDeleteTemp;
	}
}

///////////////////////////////////////////////////////////////////////////////
//
//		String_API
//

static XMPErrorID CreateBuffer( StringPtr* buffer, XMP_Uns32 size, WXMP_Error * wError )
{
	if( wError == NULL )	return kXMPErr_BadParam;

	wError->mErrorID = kXMPErr_InternalFailure;

	try 
	{
		if( buffer != NULL )
		{
			*buffer = (StringPtr) malloc( size );
			if( *buffer != NULL )
			{
				wError->mErrorID = kXMPErr_NoError;
			}
			else
			{
				wError->mErrorMsg = "Allocation failed";
			}
		}
	}
	catch( ... )
	{
		HandleException( wError );
	}

	return wError->mErrorID;
}

static XMPErrorID ReleaseBuffer( StringPtr buffer, WXMP_Error * wError )
{
	if( wError == NULL )	return kXMPErr_BadParam;

	wError->mErrorID = kXMPErr_InternalFailure;

	try 
	{
		if( buffer )
		{
			free( buffer );
			wError->mErrorID = kXMPErr_NoError;
		}
	}
	catch( ... )
	{
		HandleException( wError );
	}

	return wError->mErrorID;
}

static void GetStringAPI( String_API* strAPI )
{
	if( strAPI )
	{
		strAPI->mCreateBufferProc			=	CreateBuffer;
		strAPI->mReleaseBufferProc			=	ReleaseBuffer;
	}
}

///////////////////////////////////////////////////////////////////////////////
//
//		Abort_API
//

static XMPErrorID CheckAbort( SessionRef session, XMP_Bool * aborted, WXMP_Error* wError )
{
	if( wError == NULL )	return kXMPErr_BadParam;

	wError->mErrorID = kXMPErr_InternalFailure;

	if( aborted )
	{
		*aborted = kXMP_Bool_False;

		//
		// find FileHandlerInstance associated to session reference
		//
		FileHandlerInstancePtr instance = PluginManager::getHandlerInstance( session );

		if( instance != NULL )
		{
			//
			// execute abort procedure if available
			//
			wError->mErrorID	= kXMPErr_NoError;
			XMP_AbortProc proc	= instance->parent->abortProc;
			void* arg			= instance->parent->abortArg;

			if( proc )
			{
				try
				{
					*aborted = ConvertBoolToXMP_Bool( proc( arg ) );
				}
				catch( ... )
				{
					HandleException( wError );
				}
			}
		}
	}
	else
	{
		wError->mErrorMsg = "Invalid parameter";
	}

	return wError->mErrorID;
}

static void GetAbortAPI( Abort_API* abortAPI )
{
	if( abortAPI )
	{
		abortAPI->mCheckAbort = CheckAbort;
	}
}

///////////////////////////////////////////////////////////////////////////////
//
//		StandardHandler_API
//

static XMPErrorID CheckFormatStandardHandler( SessionRef session, XMP_FileFormat format, StringPtr path, XMP_Bool & checkOK, WXMP_Error* wError )
{
	if( wError == NULL )	return kXMPErr_BadParam;

	wError->mErrorID	= kXMPErr_InternalFailure;
	wError->mErrorMsg	= NULL;
	checkOK				= false;

	//
	// find FileHandlerInstance associated to session reference
	//
	FileHandlerInstancePtr instance = PluginManager::getHandlerInstance( session );

	if( instance != NULL && PluginManager::getHandlerPriority( instance ) == PluginManager::kReplacementHandler )
	{
		//
		// find previous file handler for file format identifier
		//
		XMPFileHandlerInfo* hdlInfo = HandlerRegistry::getInstance().getStandardHandlerInfo( format );

		if( hdlInfo != NULL && HandlerRegistry::getInstance().isReplaced( format ) )
		{
			if( hdlInfo->checkProc != NULL )
			{
				try
				{
					//
					// setup temporary XMPFiles instance
					//
					XMPFiles standardClient;
					standardClient.format = format;
					standardClient.SetFilePath( path );

					if( hdlInfo->flags & kXMPFiles_FolderBasedFormat )
					{
						//
						// process folder based handler
						//
						if( path != NULL )
						{
							// The following code corresponds to the one found in HandlerRegistry::selectSmartHandler,
							// in the folder based handler selection section of the function
							// In this case the format is already known, therefor no folder checks are needed here,
							// but the path must be split into the meaningful parts to call checkFolderFormat correctly

							std::string rootPath = path;
							std::string leafName;
							std::string fileExt;
							std::string gpName;
							std::string parentName;

							XIO::SplitLeafName ( &rootPath, &leafName );

							if( !leafName.empty() )
							{
								size_t extPos = leafName.size();

								for ( --extPos; extPos > 0; --extPos ) if ( leafName[extPos] == '.' ) break;

								if( leafName[extPos] == '.' ) 
								{
									fileExt.assign( &leafName[extPos+1] );
									MakeLowerCase( &fileExt );
									leafName.erase( extPos );
								}

								CheckFolderFormatProc CheckProc = (CheckFolderFormatProc) (hdlInfo->checkProc);

								// The case that a logical path to a clip has been passed, which does not point to a real file 
								if( Host_IO::GetFileMode( path ) == Host_IO::kFMode_DoesNotExist )
								{
									checkOK = CheckProc( hdlInfo->format, rootPath, gpName, parentName, leafName, &standardClient );
								}
								else
								{
									XIO::SplitLeafName( &rootPath, &parentName );
									XIO::SplitLeafName( &rootPath, &gpName );
									std::string origGPName ( gpName );	// ! Save the original case for XDCAM-FAM.
									MakeUpperCase( &parentName );
									MakeUpperCase( &gpName );

									if( format == kXMP_XDCAM_FAMFile && ( (parentName == "CLIP") || (parentName == "EDIT") || (parentName == "SUB") ) ) 
									{
										// ! The standard says Clip/Edit/Sub, but we just shifted to upper case.
										gpName = origGPName;	// ! XDCAM-FAM has just 1 level of inner folder, preserve the "MyMovie" case.
									}

									checkOK = CheckProc( hdlInfo->format, rootPath, gpName, parentName, leafName, &standardClient );
								}
							}
							else
							{
								wError->mErrorID = kXMPErr_BadParam;
							}
						}
						else
						{
							wError->mErrorID = kXMPErr_BadParam;
						}
					}
					else
					{
						//
						// file based handler (requires XMP_IO object)
						//
						CheckFileFormatProc CheckProc = (CheckFileFormatProc) (hdlInfo->checkProc);
						XMPFiles_IO* io = XMPFiles_IO::New_XMPFiles_IO ( path, true );
						checkOK = CheckProc( hdlInfo->format, path, io, &standardClient );
						delete io;
					}

					wError->mErrorID = kXMPErr_NoError;
				}
				catch( ... )
				{
					HandleException( wError );
				}
			}
		}
		else
		{
			wError->mErrorMsg = "No standard handler available";
		}
	}
	else
	{
		wError->mErrorMsg = "Standard file handler can't call prior handler";
	}

	return wError->mErrorID;
}

static XMPErrorID GetXMPStandardHandler( SessionRef session, XMP_FileFormat format, StringPtr path, XMPMetaRef xmpRef, XMP_Bool * xmpExists, WXMP_Error* wError )
{
	if( wError == NULL )	return kXMPErr_BadParam;

	wError->mErrorID	= kXMPErr_InternalFailure;
	wError->mErrorMsg	= NULL;

	//
	// find FileHandlerInstance associated to session reference
	//
	FileHandlerInstancePtr instance = PluginManager::getHandlerInstance( session );
	
	if( instance != NULL && PluginManager::getHandlerPriority( instance ) == PluginManager::kReplacementHandler )
	{
		//
		// find previous file handler for file format identifier
		//
		XMPFileHandlerInfo* hdlInfo = HandlerRegistry::getInstance().getStandardHandlerInfo( format );

		if( hdlInfo != NULL && HandlerRegistry::getInstance().isReplaced( format ) )
		{
			//
			// check format first
			//
			XMP_Bool suc = kXMP_Bool_False;

			XMPErrorID errorID = CheckFormatStandardHandler( session, format, path, suc, wError );

			if( errorID == kXMPErr_NoError && ConvertXMP_BoolToBool( suc ) )
			{
				//
				// setup temporary XMPFiles instance
				//
				XMPFiles standardClient;
				standardClient.format = format;
				standardClient.SetFilePath( path );

				SXMPMeta meta( xmpRef );
			
				try
				{
					//
					// open with passed handler info
					//
					suc = standardClient.OpenFile( *hdlInfo, path, kXMPFiles_OpenForRead );

					if( suc )
					{
						//
						// read meta data
						//
						suc = standardClient.GetXMP( &meta );

						if( xmpExists != NULL )		*xmpExists = suc;
					}
				}
				catch( ... )
				{
					HandleException( wError );
				}

				//
				// close and cleanup
				//
				try
				{
					standardClient.CloseFile();
				}
				catch( ... )
				{
					HandleException( wError );
				}
			}
			else if( errorID == kXMPErr_NoError )
			{
				wError->mErrorID = kXMPErr_BadFileFormat;
				wError->mErrorMsg = "Standard handler can't process file format";
			}
		}
		else
		{
			wError->mErrorID = kXMPErr_NoFileHandler;
			wError->mErrorMsg = "No standard handler available";
		}
	}
	else
	{
		wError->mErrorMsg = "Standard file handler can't call prior handler";
	}

	return wError->mErrorID;
}

static XMPErrorID GetXMPStandardHandler_V2( SessionRef session, XMP_FileFormat format, StringPtr path, XMP_StringPtr* xmpStr, XMP_Bool * xmpExists, WXMP_Error* wError )
{
	SXMPMeta meta;
    std::string xmp;
	GetXMPStandardHandler( session, format, path, meta.GetInternalRef(),  xmpExists, wError ) ;
	if( wError->mErrorID != kXMPErr_NoError ) 
		return wError->mErrorID;

	meta.SerializeToBuffer(&xmp, kXMP_NoOptions, 0);
	XMP_Uns32 length = (XMP_Uns32)xmp.size() + 1 ;
	StringPtr buffer = NULL;
	CreateBuffer( &buffer,length ,wError);	
	if( wError->mErrorID != kXMPErr_NoError ) 
		return wError->mErrorID;

	memcpy( buffer, xmp.c_str(), length );
	*xmpStr = buffer; // callee function should free the memory.
	return wError->mErrorID ;
}


static void GetStandardHandlerAPI( StandardHandler_API* standardHandlerAPI )
{
	if( standardHandlerAPI )
	{
		standardHandlerAPI->mCheckFormatStandardHandler	= CheckFormatStandardHandler;
		standardHandlerAPI->mGetXMPStandardHandler		= GetXMPStandardHandler;
	}

}


static XMPErrorID RequestAPISuite( const char* apiName, XMP_Uns32 apiVersion, void** apiSuite, WXMP_Error* wError )
{
	if( wError == NULL )
	{
		return kXMPErr_BadParam;
	}
	
	wError->mErrorID = kXMPErr_NoError;
	
	if( apiName == NULL
	   || apiVersion == 0
	   || apiSuite == NULL )
	{
		wError->mErrorID = kXMPErr_BadParam;
		return kXMPErr_BadParam;
	}
	
	// dummy suite used by unit test
	if( strcmp( apiName, "testDummy" ) == 0 && apiVersion == 1 )
	{
		*apiSuite = (void*) &RequestAPISuite;
	}
	else if ( ! strcmp( apiName, "StandardHandler" ) && apiVersion == 2 ) 
	{
		static const StandardHandler_API_V2 standardHandlerAPI =
		{
			&CheckFormatStandardHandler,
			&GetXMPStandardHandler_V2
		};
		*apiSuite=(void*)&standardHandlerAPI;
	}
	else
	{
		wError->mErrorID = kXMPErr_Unimplemented;
	}
	
	return wError->mErrorID;
}
	

///////////////////////////////////////////////////////////////////////////////
//
//		Init/Term Host APIs
//

// Because of changes to the plugin versioning strategy,
// the host API version is no longer tied to the plugin version
// and the host API struct is supposed to be frozen.
// New host APIs can be requested through the new function mRequestAPISuite.

void PluginManager::SetupHostAPI_V1( HostAPIRef hostAPI )
{
	// Get XMP_IO APIs
	hostAPI->mFileIOAPI = new FileIO_API();
	GetFileSysAPI( hostAPI->mFileIOAPI );

	// Get String APIs
	hostAPI->mStrAPI = new String_API();
	GetStringAPI( hostAPI->mStrAPI );

	// Get Abort API
	hostAPI->mAbortAPI = new Abort_API();
	GetAbortAPI( hostAPI->mAbortAPI );

	// Get standard handler APIs
	hostAPI->mStandardHandlerAPI = new StandardHandler_API();
	GetStandardHandlerAPI( hostAPI->mStandardHandlerAPI );

	hostAPI->mRequestAPISuite = NULL;
}

void PluginManager::SetupHostAPI_V2( HostAPIRef hostAPI )
{
	SetupHostAPI_V1 ( hostAPI );
}

void PluginManager::SetupHostAPI_V3( HostAPIRef hostAPI )
{
	SetupHostAPI_V2 ( hostAPI );
}

void PluginManager::SetupHostAPI_V4( HostAPIRef hostAPI )
{
	SetupHostAPI_V3 ( hostAPI );
	hostAPI->mRequestAPISuite = RequestAPISuite;
}

} //namespace XMP_PLUGIN