1 /*****************************************************************************
2 * addons.c: VLC addons manager
3 *****************************************************************************
4 * Copyright (C) 2014 VLC authors and VideoLAN
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
25 #include <vlc_common.h>
26 #include <vlc_atomic.h>
27 #include <vlc_modules.h>
28 #include <vlc_arrays.h>
29 #include <vlc_interrupt.h>
32 #include <vlc_addons.h>
34 /*****************************************************************************
35 * Structures/definitions
36 *****************************************************************************/
38 typedef struct addon_entry_owner
42 } addon_entry_owner_t
;
44 struct addons_manager_private_t
46 vlc_object_t
*p_parent
;
54 vlc_interrupt_t
*p_interrupt
;
55 DECL_ARRAY(char*) uris
;
56 DECL_ARRAY(addon_entry_t
*) entries
;
65 vlc_interrupt_t
*p_interrupt
;
66 DECL_ARRAY(addon_entry_t
*) entries
;
70 static void *FinderThread( void * );
71 static void LoadLocalStorage( addons_manager_t
*p_manager
);
73 /*****************************************************************************
75 *****************************************************************************/
77 addon_entry_t
* addon_entry_New(void)
79 addon_entry_owner_t
*owner
= calloc( 1, sizeof(addon_entry_owner_t
) );
80 if( unlikely(owner
== NULL
) )
83 vlc_atomic_rc_init( &owner
->rc
);
85 addon_entry_t
*p_entry
= &owner
->entry
;
86 vlc_mutex_init( &p_entry
->lock
);
87 ARRAY_INIT( p_entry
->files
);
91 addon_entry_t
* addon_entry_Hold( addon_entry_t
* p_entry
)
93 addon_entry_owner_t
*owner
= (addon_entry_owner_t
*) p_entry
;
95 vlc_atomic_rc_inc( &owner
->rc
);
99 void addon_entry_Release( addon_entry_t
* p_entry
)
101 addon_entry_owner_t
*owner
= (addon_entry_owner_t
*) p_entry
;
103 if( !vlc_atomic_rc_dec( &owner
->rc
) )
106 free( p_entry
->psz_name
);
107 free( p_entry
->psz_summary
);
108 free( p_entry
->psz_description
);
109 free( p_entry
->psz_archive_uri
);
110 free( p_entry
->psz_author
);
111 free( p_entry
->psz_source_uri
);
112 free( p_entry
->psz_image_uri
);
113 free( p_entry
->psz_image_data
);
114 free( p_entry
->psz_source_module
);
115 free( p_entry
->psz_version
);
116 free( p_entry
->p_custom
);
118 addon_file_t
*p_file
;
119 ARRAY_FOREACH( p_file
, p_entry
->files
)
121 free( p_file
->psz_filename
);
122 free( p_file
->psz_download_uri
);
125 ARRAY_RESET( p_entry
->files
);
130 addons_manager_t
*addons_manager_New( vlc_object_t
*p_this
,
131 const struct addons_manager_owner
*restrict owner
)
133 addons_manager_t
*p_manager
= malloc( sizeof(addons_manager_t
) );
134 if ( !p_manager
) return NULL
;
136 p_manager
->p_priv
= malloc( sizeof(addons_manager_private_t
) );
137 if ( !p_manager
->p_priv
)
143 p_manager
->owner
= *owner
;
144 p_manager
->p_priv
->p_parent
= p_this
;
146 p_manager
->p_priv
->finder
.p_interrupt
= vlc_interrupt_create();
147 p_manager
->p_priv
->installer
.p_interrupt
= vlc_interrupt_create();
148 if ( !p_manager
->p_priv
->finder
.p_interrupt
||
149 !p_manager
->p_priv
->installer
.p_interrupt
)
151 if( p_manager
->p_priv
->finder
.p_interrupt
)
152 vlc_interrupt_destroy( p_manager
->p_priv
->finder
.p_interrupt
);
153 if( p_manager
->p_priv
->installer
.p_interrupt
)
154 vlc_interrupt_destroy( p_manager
->p_priv
->installer
.p_interrupt
);
155 free( p_manager
->p_priv
);
160 #define INIT_QUEUE( name ) \
161 p_manager->p_priv->name.b_live = false;\
162 vlc_mutex_init( &p_manager->p_priv->name.lock );\
163 vlc_cond_init( &p_manager->p_priv->name.waitcond );\
164 ARRAY_INIT( p_manager->p_priv->name.entries );
167 INIT_QUEUE( installer
)
168 ARRAY_INIT( p_manager
->p_priv
->finder
.uris
);
173 void addons_manager_Delete( addons_manager_t
*p_manager
)
177 vlc_mutex_lock( &p_manager
->p_priv
->finder
.lock
);
178 b_live
= p_manager
->p_priv
->finder
.b_live
;
179 vlc_mutex_unlock( &p_manager
->p_priv
->finder
.lock
);
182 vlc_interrupt_kill( p_manager
->p_priv
->finder
.p_interrupt
);
183 vlc_join( p_manager
->p_priv
->finder
.thread
, NULL
);
186 vlc_mutex_lock( &p_manager
->p_priv
->installer
.lock
);
187 b_live
= p_manager
->p_priv
->installer
.b_live
;
188 vlc_mutex_unlock( &p_manager
->p_priv
->installer
.lock
);
191 vlc_interrupt_kill( p_manager
->p_priv
->installer
.p_interrupt
);
192 vlc_join( p_manager
->p_priv
->installer
.thread
, NULL
);
195 addon_entry_t
*p_entry
;
197 #define FREE_QUEUE( name ) \
198 ARRAY_FOREACH( p_entry, p_manager->p_priv->name.entries )\
199 addon_entry_Release( p_entry );\
200 ARRAY_RESET( p_manager->p_priv->name.entries );\
201 vlc_interrupt_destroy( p_manager->p_priv->name.p_interrupt );
204 FREE_QUEUE( installer
)
207 ARRAY_FOREACH( psz_uri
, p_manager
->p_priv
->finder
.uris
)
209 ARRAY_RESET( p_manager
->p_priv
->finder
.uris
);
211 free( p_manager
->p_priv
);
215 void addons_manager_Gather( addons_manager_t
*p_manager
, const char *psz_uri
)
220 vlc_mutex_lock( &p_manager
->p_priv
->finder
.lock
);
222 ARRAY_APPEND( p_manager
->p_priv
->finder
.uris
, strdup( psz_uri
) );
224 if( !p_manager
->p_priv
->finder
.b_live
)
226 if( vlc_clone( &p_manager
->p_priv
->finder
.thread
, FinderThread
, p_manager
,
227 VLC_THREAD_PRIORITY_LOW
) )
229 msg_Err( p_manager
->p_priv
->p_parent
,
230 "cannot spawn entries provider thread" );
231 vlc_mutex_unlock( &p_manager
->p_priv
->finder
.lock
);
234 p_manager
->p_priv
->finder
.b_live
= true;
237 vlc_mutex_unlock( &p_manager
->p_priv
->finder
.lock
);
238 vlc_cond_signal( &p_manager
->p_priv
->finder
.waitcond
);
241 /*****************************************************************************
243 *****************************************************************************/
245 static addon_entry_t
* getHeldEntryByUUID( addons_manager_t
*p_manager
,
246 const addon_uuid_t uuid
)
248 addon_entry_t
*p_return
= NULL
;
249 vlc_mutex_lock( &p_manager
->p_priv
->finder
.lock
);
250 addon_entry_t
*p_entry
;
251 ARRAY_FOREACH( p_entry
, p_manager
->p_priv
->finder
.entries
)
253 if ( !memcmp( p_entry
->uuid
, uuid
, sizeof( addon_uuid_t
) ) )
256 addon_entry_Hold( p_return
);
260 vlc_mutex_unlock( &p_manager
->p_priv
->finder
.lock
);
264 static void MergeSources( addons_manager_t
*p_manager
,
265 addon_entry_t
**pp_addons
, int i_count
)
267 addon_entry_t
*p_entry
, *p_manager_entry
;
268 addon_uuid_t zerouuid
;
269 memset( zerouuid
, 0, sizeof( addon_uuid_t
) );
270 for ( int i
=0; i
< i_count
; i
++ )
272 p_entry
= pp_addons
[i
];
273 vlc_mutex_lock( &p_entry
->lock
);
274 if ( memcmp( p_entry
->uuid
, zerouuid
, sizeof( addon_uuid_t
) ) )
275 p_manager_entry
= getHeldEntryByUUID( p_manager
, p_entry
->uuid
);
277 p_manager_entry
= NULL
;
278 if ( !p_manager_entry
)
280 ARRAY_APPEND( p_manager
->p_priv
->finder
.entries
, p_entry
);
281 p_manager
->owner
.addon_found( p_manager
, p_entry
);
285 vlc_mutex_lock( &p_manager_entry
->lock
);
286 if ( ( p_manager_entry
->psz_version
&& p_entry
->psz_version
)
287 && /* FIXME: better version comparison */
288 strcmp( p_manager_entry
->psz_version
, p_entry
->psz_version
)
291 p_manager_entry
->e_flags
|= ADDON_UPDATABLE
;
293 vlc_mutex_unlock( &p_manager_entry
->lock
);
294 addon_entry_Release( p_manager_entry
);
296 vlc_mutex_unlock( &p_entry
->lock
);
300 static void LoadLocalStorage( addons_manager_t
*p_manager
)
302 addons_finder_t
*p_finder
=
303 vlc_custom_create( p_manager
->p_priv
->p_parent
, sizeof( *p_finder
), "entries finder" );
304 p_finder
->obj
.no_interact
= true;
306 module_t
*p_module
= module_need( p_finder
, "addons finder",
307 "addons.store.list", true );
310 ARRAY_INIT( p_finder
->entries
);
311 p_finder
->psz_uri
= NULL
;
312 p_finder
->pf_find( p_finder
);
313 module_unneed( p_finder
, p_module
);
315 MergeSources( p_manager
, p_finder
->entries
.p_elems
, p_finder
->entries
.i_size
);
317 ARRAY_RESET( p_finder
->entries
);
319 vlc_object_delete(p_finder
);
322 static void finder_thread_interrupted( void* p_data
)
324 addons_manager_t
*p_manager
= p_data
;
325 vlc_mutex_lock( &p_manager
->p_priv
->finder
.lock
);
326 p_manager
->p_priv
->finder
.b_live
= false;
327 vlc_cond_signal( &p_manager
->p_priv
->finder
.waitcond
);
328 vlc_mutex_unlock( &p_manager
->p_priv
->finder
.lock
);
331 static void *FinderThread( void *p_data
)
333 addons_manager_t
*p_manager
= p_data
;
334 int i_cancel
= vlc_savecancel();
335 vlc_interrupt_set( p_manager
->p_priv
->finder
.p_interrupt
);
337 vlc_mutex_lock( &p_manager
->p_priv
->finder
.lock
);
338 while( p_manager
->p_priv
->finder
.b_live
)
342 vlc_interrupt_register( finder_thread_interrupted
, p_data
);
343 while( p_manager
->p_priv
->finder
.uris
.i_size
== 0 &&
344 p_manager
->p_priv
->finder
.b_live
)
346 vlc_cond_wait( &p_manager
->p_priv
->finder
.waitcond
,
347 &p_manager
->p_priv
->finder
.lock
);
349 vlc_interrupt_unregister();
350 if( !p_manager
->p_priv
->finder
.b_live
)
352 psz_uri
= p_manager
->p_priv
->finder
.uris
.p_elems
[0];
353 ARRAY_REMOVE( p_manager
->p_priv
->finder
.uris
, 0 );
355 vlc_mutex_unlock( &p_manager
->p_priv
->finder
.lock
);
357 addons_finder_t
*p_finder
=
358 vlc_custom_create( p_manager
->p_priv
->p_parent
, sizeof( *p_finder
), "entries finder" );
360 if( p_finder
!= NULL
)
362 p_finder
->obj
.no_interact
= true;
364 ARRAY_INIT( p_finder
->entries
);
365 p_finder
->psz_uri
= psz_uri
;
367 p_module
= module_need( p_finder
, "addons finder", NULL
, false );
370 p_finder
->pf_find( p_finder
);
371 module_unneed( p_finder
, p_module
);
372 MergeSources( p_manager
, p_finder
->entries
.p_elems
, p_finder
->entries
.i_size
);
374 ARRAY_RESET( p_finder
->entries
);
376 vlc_object_delete(p_finder
);
379 p_manager
->owner
.discovery_ended( p_manager
);
380 vlc_mutex_lock( &p_manager
->p_priv
->finder
.lock
);
383 vlc_mutex_unlock( &p_manager
->p_priv
->finder
.lock
);
384 vlc_restorecancel( i_cancel
);
388 static int addons_manager_WriteCatalog( addons_manager_t
*p_manager
)
390 int i_return
= VLC_EGENERIC
;
392 addons_storage_t
*p_storage
=
393 vlc_custom_create( p_manager
->p_priv
->p_parent
, sizeof( *p_storage
), "entries storage" );
394 p_storage
->obj
.no_interact
= true;
396 module_t
*p_module
= module_need( p_storage
, "addons storage",
397 "addons.store.install", true );
400 vlc_mutex_lock( &p_manager
->p_priv
->finder
.lock
);
401 i_return
= p_storage
->pf_catalog( p_storage
, p_manager
->p_priv
->finder
.entries
.p_elems
,
402 p_manager
->p_priv
->finder
.entries
.i_size
);
403 vlc_mutex_unlock( &p_manager
->p_priv
->finder
.lock
);
404 module_unneed( p_storage
, p_module
);
406 vlc_object_delete(p_storage
);
411 int addons_manager_LoadCatalog( addons_manager_t
*p_manager
)
413 LoadLocalStorage( p_manager
);
417 static int installOrRemoveAddon( addons_manager_t
*p_manager
, addon_entry_t
*p_entry
, bool b_install
)
419 int i_return
= VLC_EGENERIC
;
421 addons_storage_t
*p_storage
=
422 vlc_custom_create( p_manager
->p_priv
->p_parent
, sizeof( *p_storage
), "entries storage" );
423 p_storage
->obj
.no_interact
= true;
425 module_t
*p_module
= module_need( p_storage
, "addons storage",
426 "addons.store.install", true );
430 i_return
= p_storage
->pf_install( p_storage
, p_entry
);
432 i_return
= p_storage
->pf_remove( p_storage
, p_entry
);
433 module_unneed( p_storage
, p_module
);
434 msg_Dbg( p_manager
->p_priv
->p_parent
, "InstallAddon returns %d", i_return
);
435 if ( i_return
== VLC_SUCCESS
)
438 vlc_mutex_lock( &p_entry
->lock
);
439 p_entry
->e_flags
= ADDON_MANAGEABLE
;
440 vlc_mutex_unlock( &p_entry
->lock
);
443 vlc_object_delete(p_storage
);
448 static void installer_thread_interrupted( void* p_data
)
450 addons_manager_t
*p_manager
= p_data
;
451 vlc_mutex_lock( &p_manager
->p_priv
->installer
.lock
);
452 p_manager
->p_priv
->installer
.b_live
= false;
453 vlc_cond_signal( &p_manager
->p_priv
->installer
.waitcond
);
454 vlc_mutex_unlock( &p_manager
->p_priv
->installer
.lock
);
457 static void *InstallerThread( void *p_data
)
459 addons_manager_t
*p_manager
= p_data
;
460 int i_cancel
= vlc_savecancel();
461 vlc_interrupt_set( p_manager
->p_priv
->installer
.p_interrupt
);
464 vlc_mutex_lock( &p_manager
->p_priv
->installer
.lock
);
465 while( p_manager
->p_priv
->installer
.b_live
)
467 vlc_interrupt_register( installer_thread_interrupted
, p_data
);
468 while ( !p_manager
->p_priv
->installer
.entries
.i_size
&&
469 p_manager
->p_priv
->installer
.b_live
)
471 /* No queued addons */
472 vlc_cond_wait( &p_manager
->p_priv
->installer
.waitcond
,
473 &p_manager
->p_priv
->installer
.lock
);
475 vlc_interrupt_unregister();
476 if( !p_manager
->p_priv
->installer
.b_live
)
479 addon_entry_t
*p_entry
= p_manager
->p_priv
->installer
.entries
.p_elems
[0];
480 ARRAY_REMOVE( p_manager
->p_priv
->installer
.entries
, 0 );
481 addon_entry_Hold( p_entry
);
482 vlc_mutex_unlock( &p_manager
->p_priv
->installer
.lock
);
484 vlc_mutex_lock( &p_entry
->lock
);
486 if ( p_entry
->e_state
== ADDON_INSTALLED
)
488 p_entry
->e_state
= ADDON_UNINSTALLING
;
489 vlc_mutex_unlock( &p_entry
->lock
);
492 p_manager
->owner
.addon_changed( p_manager
, p_entry
);
494 i_ret
= installOrRemoveAddon( p_manager
, p_entry
, false );
496 vlc_mutex_lock( &p_entry
->lock
);
497 p_entry
->e_state
= ( i_ret
== VLC_SUCCESS
) ? ADDON_NOTINSTALLED
500 else if ( p_entry
->e_state
== ADDON_NOTINSTALLED
)
502 p_entry
->e_state
= ADDON_INSTALLING
;
503 vlc_mutex_unlock( &p_entry
->lock
);
506 p_manager
->owner
.addon_changed( p_manager
, p_entry
);
508 i_ret
= installOrRemoveAddon( p_manager
, p_entry
, true );
510 vlc_mutex_lock( &p_entry
->lock
);
511 p_entry
->e_state
= ( i_ret
== VLC_SUCCESS
) ? ADDON_INSTALLED
512 : ADDON_NOTINSTALLED
;
514 vlc_mutex_unlock( &p_entry
->lock
);
517 p_manager
->owner
.addon_changed( p_manager
, p_entry
);
519 addon_entry_Release( p_entry
);
521 addons_manager_WriteCatalog( p_manager
);
522 vlc_mutex_lock( &p_manager
->p_priv
->installer
.lock
);
524 vlc_mutex_unlock( &p_manager
->p_priv
->installer
.lock
);
525 vlc_restorecancel( i_cancel
);
529 static int InstallEntry( addons_manager_t
*p_manager
, addon_entry_t
*p_entry
)
531 if ( p_entry
->e_type
== ADDON_UNKNOWN
||
532 p_entry
->e_type
== ADDON_PLUGIN
||
533 p_entry
->e_type
== ADDON_OTHER
)
536 vlc_mutex_lock( &p_manager
->p_priv
->installer
.lock
);
537 ARRAY_APPEND( p_manager
->p_priv
->installer
.entries
, p_entry
);
538 if( !p_manager
->p_priv
->installer
.b_live
)
540 if( vlc_clone( &p_manager
->p_priv
->installer
.thread
, InstallerThread
, p_manager
,
541 VLC_THREAD_PRIORITY_LOW
) )
543 msg_Err( p_manager
->p_priv
->p_parent
,
544 "cannot spawn addons installer thread" );
545 vlc_mutex_unlock( &p_manager
->p_priv
->installer
.lock
);
549 p_manager
->p_priv
->installer
.b_live
= true;
551 vlc_mutex_unlock( &p_manager
->p_priv
->installer
.lock
);
552 vlc_cond_signal( &p_manager
->p_priv
->installer
.waitcond
);
556 int addons_manager_Install( addons_manager_t
*p_manager
, const addon_uuid_t uuid
)
558 addon_entry_t
*p_install_entry
= getHeldEntryByUUID( p_manager
, uuid
);
559 if ( ! p_install_entry
) return VLC_EGENERIC
;
560 int i_ret
= InstallEntry( p_manager
, p_install_entry
);
561 addon_entry_Release( p_install_entry
);
565 int addons_manager_Remove( addons_manager_t
*p_manager
, const addon_uuid_t uuid
)
567 return addons_manager_Install( p_manager
, uuid
);