AddOnManager.h

Go to the documentation of this file.
00001 /***************************************************************************
00002 //
00003 //  File:           AddOnManager.h
00004 //
00005 //  Description:    Classes for standard add-on roster implementation.
00006 //
00007 //  Copyright 2000, Be Incorporated, All Rights Reserved.
00008 //
00009 ***************************************************************************/
00010 
00011 #ifndef _ADD_ON_MANAGER_H
00012 #define _ADD_ON_MANAGER_H
00013 
00014 #ifndef _ENTRY_H
00015 #include <Entry.h>
00016 #endif
00017 
00018 #ifndef _IMAGE_H
00019 #include <image.h>
00020 #endif
00021 
00022 #ifndef _LIST_H
00023 #include <List.h>
00024 #endif
00025 
00026 #ifndef _LOCKER_H
00027 #include <Locker.h>
00028 #endif
00029 
00030 #ifndef _MESSAGE_H
00031 #include <Message.h>
00032 #endif
00033 
00034 #ifndef _SEARCH_PATH_H
00035 #include <SearchPath.h>
00036 #endif
00037 
00038 class BNode;
00039 namespace BPrivate {
00040     class AddOnSearchPath;
00041 }
00042 
00043 enum {
00044     B_ADD_ONS_CHANGED       = 'ADCG'
00045 };
00046 
00047 // Flags for Flush().
00048 enum {
00049     B_FLUSH_KEEP_LOADED     = (1<<0),
00050     B_FLUSH_DYNAMIC         = (1<<1),
00051     B_FLUSH_REFERENCED      = (1<<2)
00052 };
00053 
00054 class BAddOnHandle {
00055 public:
00056     // This is a handle on a single available add-on, which may
00057     // not actually be loaded yet.  The entry_ref is required
00058     // to load the add-on from disk; the node_ref is used only
00059     // for identification during node monitoring.
00060                     BAddOnHandle(const entry_ref* entry=NULL,
00061                                  const node_ref* node=NULL);
00062     
00063     // Reference counting of handles.
00064         int32       Acquire() const;
00065         int32       Release() const;
00066     
00067     // Where the add-on lives.
00068         status_t    SetEntryRef(const entry_ref* ref);
00069         entry_ref   EntryRef() const;
00070     
00071         node_ref    NodeRef() const;
00072     
00073     // Access to the add-on image.  Note that Open() on a handle
00074     // with no entry_ref WILL succeeded, but won't load an add_on --
00075     // it will just return the main application image.
00076         image_id    Open();
00077         void        Close();
00078         bool        Flush(uint32 flags=0);
00079     
00080     // Return true if this add-ons image is currently loaded.
00081         bool        IsLoaded() const;
00082     
00083     // Return true if this is a static add-on -- i.e., it is
00084     // opened, but there is no associated image file.
00085         bool        IsStatic() const;
00086     
00087     // Return true to keep this add-on loaded, even if its user
00088     // count has gone down to zero.  Note that it may still be
00089     // unloaded if the object is being destroyed.
00090 virtual bool        KeepLoaded() const;
00091     
00092     // Return true if the identifiers for this add-on can
00093     // change, so it must always be loaded instead of storing
00094     // the identifiers in an attribute.  Note that this
00095     // implies KeepLoaded(), since the add-on must always be
00096     // queried for its attributes.
00097 virtual bool        IsDynamic() const;
00098     
00099     // Return the amount of memory this -image- uses, if it is
00100     // loaded.
00101 virtual size_t      GetMemoryUsage() const;
00102     
00103     // Retrieve identifying characteristics of this add-on.  If
00104     // 'quick' is true, only the cached attribute will be used --
00105     // the add-on image will not be loaded, if it isn't already.
00106         status_t    GetIdentifiers(BMessage* into, bool quick=false) const;
00107     
00108     // Return true if there is an identifier named 'name' with
00109     // the text 'value'.  This is a caseless comparison.
00110         bool        MatchIdentifier(const char* name, const char* value,
00111                                     bool quick=false) const;
00112     
00113     // Return the number of seconds since this add-on was last opened.
00114         time_t      SecondsSinceOpen() const;
00115 
00116 protected:
00117 virtual             ~BAddOnHandle();
00118     
00119     // Hook functions.  These are are called with the BAddOnHandle
00120     // object locked.
00121     
00122     // Called right after the add-on image is loaded.
00123 virtual void        ImageLoaded(image_id image);
00124 
00125     // Override this to retrieve information from a loaded add-on
00126     // image, which will be cached in its attributes.
00127 virtual status_t    LoadIdentifiers(BMessage* into, image_id from);
00128     
00129     // Called right before the add-on image is unloaded.  At this
00130     // point you should delete any objects that your add-on created
00131     // (in particular, whatever top-level add-on API class that
00132     // you got your identifiers from).
00133 virtual void        ImageUnloading(image_id image);
00134     
00135     // Override to change the name of the attribute that its
00136     // identifier BMessage is stored in.
00137 virtual const char* AttrBaseName() const;
00138     
00139     // Quick access to add-on identifiers, locking them down
00140     // while in use.  Be sure not to acquire any other locks
00141     // between these two calls.
00142         const BMessage* LockIdentifiers(bool quick=false) const;
00143         void        UnlockIdentifiers(const BMessage* ident) const;
00144     
00145     // Locking the handle's state.  Do not call any other
00146     // BAddOnHandle methods while this lock is held.
00147         void        Lock() const;
00148         void        Unlock() const;
00149         
00150 private:
00151         /* FBC */
00152 virtual void        _HoldTheAddOnHandle1();
00153 virtual void        _HoldTheAddOnHandle2();
00154 virtual void        _HoldTheAddOnHandle3();
00155 virtual void        _HoldTheAddOnHandle4();
00156 virtual void        _HoldTheAddOnHandle5();
00157 virtual void        _HoldTheAddOnHandle6();
00158 virtual void        _HoldTheAddOnHandle7();
00159 virtual void        _HoldTheAddOnHandle8();
00160 virtual void        _HoldTheAddOnHandle9();
00161 virtual void        _HoldTheAddOnHandle10();
00162 virtual void        _HoldTheAddOnHandle11();
00163 virtual void        _HoldTheAddOnHandle12();
00164 virtual void        _HoldTheAddOnHandle13();
00165 virtual void        _HoldTheAddOnHandle14();
00166 virtual void        _HoldTheAddOnHandle15();
00167 virtual void        _HoldTheAddOnHandle16();
00168         uint32      _holdTheData[8];
00169     
00170         image_id    do_open();
00171         void        do_close();
00172         bool        do_flush(uint32 flags=0);
00173         status_t    do_get_identifiers(BMessage* into, bool quick);
00174     
00175         status_t    do_read_identifiers(BMessage* into, BNode* from);
00176         status_t    do_write_identifiers(BNode* into, const BMessage* from);
00177     
00178 mutable int32       fAccess;
00179 mutable int32       fRefCount;
00180         node_ref    fNode;
00181         entry_ref   fEntry;
00182         image_id    fImage;
00183         status_t    fImageErr;
00184         int32       fUsers;
00185         time_t      fLastUsed;          // Last time this add-on was used.
00186         bool        fHasIdentifiers;
00187         bool        fStaticImage;       // Add-on was opened with no entry_ref?
00188         
00189         BMessage    fIdentifiers;
00190 };
00191 
00192 class BAddOnManager {
00193 public:
00194                     BAddOnManager(const char* name);
00195 virtual             ~BAddOnManager();
00196 
00197     // Add directories to search (see BSearchPath for details).
00198         status_t    AddDirectory(const BEntry* dir);
00199         status_t    AddDirectory(const entry_ref* dir);
00200         status_t    AddDirectory(const char* dir, const char* leaf=NULL);
00201         status_t    AddDirectory(directory_which which, const char* leaf=NULL);
00202         status_t    AddSearchPath(const char* path, const char* leaf=NULL);
00203         status_t    AddEnvVar(const char* name, const char* leaf=NULL,
00204                               const char* defEnvVal=NULL);
00205 
00206     // Do a one-time scan of the current search path, installing
00207     // the add-ons found.
00208         status_t    Scan();
00209         bool        IsScanned() const;
00210         
00211     // Begin handling all add-ons found in the previously
00212     // set search path.
00213 virtual status_t    Run();
00214         bool        IsRunning() const;
00215 
00216 virtual void        Shutdown(bool force_unload=false);
00217 
00218     // Set up notification of changes.
00219         status_t    StartWatching(BMessenger receiver);
00220         status_t    StopWatching(BMessenger receiver);
00221     
00222     // Access control for this object.
00223         BLocker*    Locker() const;
00224     
00225     // BList-like access to available add-ons.
00226         int32       CountAddOns() const;
00227         BAddOnHandle* AddOnAt(int32 i) const;
00228     
00229         BAddOnHandle* FindAddOn(const node_ref* node) const;
00230     
00231     // Manually adding and removing add-on handles.
00232         BAddOnHandle* InstallAddOn(const entry_ref* entry,
00233                                const node_ref* node);
00234 virtual void        InstallAddOn(BAddOnHandle* addon);
00235 virtual bool        RemoveAddOn(BAddOnHandle* addon);
00236     
00237     // Return amount of memory used by all add-ons.
00238     // NOTE: This is a fairly heavy-weight operation, as it
00239     // needs to run through every add-on and sum up the
00240     // results of their own GetMemoryUsage().
00241 virtual size_t      GetMemoryUsage() const;
00242     
00243     // Note that we are going to use an add-on.  This will
00244     // cause the add-on to be shuffled up to the front of the
00245     // list of all add-ons.
00246         void        UsingAddOn(int32 i);
00247     
00248     // Flush add-ons that are currently not in-use, until the
00249     // requested amount of memory is reclaimed.  Add-ons are
00250     // flushed in order from the least recently used to the
00251     // latest used.
00252     // Returns amount of memory that was left over, that is
00253     // memory_needed - (amount flushed).
00254         ssize_t     PruneAddOnMemory(ssize_t memory_needed=-1);
00255     
00256     // Flush add-ons that are currently not in-use, based on
00257     // the amount of time since they were last opened.  Add-ons
00258     // are flushed in order from the least recently used to the
00259     // latest used; the minimum time needed for a flush starts
00260     // at min_seconds, and increases by min_seconds*growth/100
00261     // for each later used add-on.
00262     // Returns the number of add-ons that were actually pruned.
00263         int32       PruneAddOnTime(time_t min_seconds, int32 growth=100);
00264     
00265 protected:
00266     // Override this to implement your own BAddOnHandle subclass.
00267 virtual BAddOnHandle* InstantiateHandle(const entry_ref* entry,
00268                                         const node_ref* node);
00269     
00270 private:
00271         /* FBC */
00272 virtual void        _AmyTheAddOnManager1();
00273 virtual void        _AmyTheAddOnManager2();
00274 virtual void        _AmyTheAddOnManager3();
00275 virtual void        _AmyTheAddOnManager4();
00276 virtual void        _AmyTheAddOnManager5();
00277 virtual void        _AmyTheAddOnManager6();
00278 virtual void        _AmyTheAddOnManager7();
00279 virtual void        _AmyTheAddOnManager8();
00280 virtual void        _AmyTheAddOnManager9();
00281 virtual void        _AmyTheAddOnManager10();
00282 virtual void        _AmyTheAddOnManager11();
00283 virtual void        _AmyTheAddOnManager12();
00284 virtual void        _AmyTheAddOnManager13();
00285 virtual void        _AmyTheAddOnManager14();
00286 virtual void        _AmyTheAddOnManager15();
00287 virtual void        _AmyTheAddOnManager16();
00288         uint32      _amyTheData[8];
00289 
00290 friend class        BPrivate::AddOnSearchPath;
00291 
00292         void        mark_changed(bool needWakeup=true);
00293         void        send_notices();
00294         
00295 mutable BLocker     fLock;
00296     // Combined list of directories to search.
00297     BPrivate::AddOnSearchPath* fSearchPath;
00298         BList       fAddOns;                // List of BAddOnHandle*
00299         BList       fWatchers;              // List of BMessenger*
00300         bool        fScanned;
00301         bool        fRunning;
00302         int32       fChanged;
00303 };
00304 
00305 #endif

Copyright 2005 by yellowTAB GmbH, Be Inc., Palm Source Inc. and their respective legal successors
All rights reserved.