* _IsSystemApp() did not work anymore with the new directory hierarchy. It will

now check for the complete path instead of just the prefix. This fixes bug
  #3862.
* Made TRoster.h self contained.
* Style cleanup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@30788 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2009-05-18 09:10:58 +00:00
parent 49fb9a2eab
commit c8e7c8ef86
2 changed files with 310 additions and 297 deletions
+123 -117
View File
@@ -7,8 +7,14 @@
applications.
*/
#include "TRoster.h"
#include <new>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <Application.h>
#include <AppMisc.h>
#include <AutoDeleter.h>
@@ -18,14 +24,9 @@
#include <MessagePrivate.h>
#include <MessengerPrivate.h>
#include <Path.h>
#include <Roster.h> // for B_BACKGROUND_APP
#include <ServerProtocol.h>
#include <storage_support.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include "AppInfoListMessagingTargetSet.h"
#include "Debug.h"
#include "EventMaskWatcher.h"
@@ -33,17 +34,12 @@
#include "RegistrarDefs.h"
#include "RosterAppInfo.h"
#include "RosterSettingsCharStream.h"
#include "TRoster.h"
using std::nothrow;
using namespace BPrivate;
static bool larger_index(const recent_entry *entry1, const recent_entry *entry2);
/*!
\class TRoster
/*! \class TRoster
\brief Implements the application roster.
This class handles the BRoster requests. For each kind a hook method is
@@ -70,20 +66,20 @@ static bool larger_index(const recent_entry *entry1, const recent_entry *entry2)
//! The maximal period of time an app may be early pre-registered (60 s).
const bigtime_t kMaximalEarlyPreRegistrationPeriod = 60000000LL;
//! Applications living in this tree are considered "vital system apps".
static const char *const kVitalSystemAppPathPrefix
= "/boot/system/servers";
//! Applications living in these directory are considered "system apps".
// TODO: move those into a common shared system header
static const char* const kSystemAppPath = "/boot/system";
static const char* const kSystemServerPath = "/boot/system/servers";
// #pragma mark - Private local functions
//! Applications living in this tree are considered "system apps".
static const char *const kSystemAppPathPrefix = "/boot/system";
// get_default_roster_settings_file
/*! \brief Returns the path to the default roster settings.
\param path BPath to be set to the roster settings path.
\return the settings path as C string (\code path.Path() \endcode).
*/
static
const char *
static const char*
get_default_roster_settings_file(BPath& path)
{
// get the path of the settings dir and append the subpath of our file
@@ -99,7 +95,27 @@ get_default_roster_settings_file(BPath &path)
return path.Path();
}
// constructor
/*! \brief Returns true if entry1's index is larger than entry2's index.
Also returns true if either entry is \c NULL.
Used for sorting the recent entry lists loaded from disk into the
proper order.
*/
bool
larger_index(const recent_entry* entry1, const recent_entry* entry2)
{
if (entry1 && entry2)
return entry1->index > entry2->index;
return true;
}
// #pragma mark -
/*! \brief Creates a new roster.
The object is completely initialized and ready to handle requests.
@@ -122,14 +138,14 @@ TRoster::TRoster()
_LoadRosterSettings();
}
// destructor
/*! \brief Frees all resources associated with this object.
*/
TRoster::~TRoster()
{
}
// HandleAddApplication
/*! \brief Handles an AddApplication() request.
\param request The request message
*/
@@ -163,6 +179,7 @@ TRoster::HandleAddApplication(BMessage *request)
port = -1;
if (request->FindBool("full_registration", &fullReg) != B_OK)
fullReg = false;
PRINT(("team: %ld, signature: %s\n", team, signature));
PRINT(("full registration: %d\n", fullReg));
@@ -233,7 +250,8 @@ PRINT(("ref: %ld, %lld, %s\n", ref.device, ref.directory, ref.name));
// add it to the right list
bool addingSuccess = false;
if (team >= 0) {
PRINT(("added ref: %ld, %lld, %s\n", info->ref.device, info->ref.directory, info->ref.name));
PRINT(("added ref: %ld, %lld, %s\n", info->ref.device,
info->ref.directory, info->ref.name));
addingSuccess = (AddApp(info) == B_OK);
if (addingSuccess && fullReg)
_AppAdded(info);
@@ -258,7 +276,6 @@ PRINT(("added to early pre-regs, token: %lu\n", token));
fRecentApps.Add(signature, flags);
else
fRecentApps.Add(&ref, flags);
// fRecentApps.Print();
BMessage reply(B_REG_SUCCESS);
// The token is valid only when no team ID has been supplied.
@@ -278,7 +295,7 @@ PRINT(("added to early pre-regs, token: %lu\n", token));
FUNCTION_END();
}
// HandleCompleteRegistration
/*! \brief Handles a CompleteRegistration() request.
\param request The request message
*/
@@ -342,7 +359,7 @@ TRoster::HandleCompleteRegistration(BMessage *request)
FUNCTION_END();
}
// HandleIsAppRegistered
/*! \brief Handles an IsAppRegistered() request.
\param request The request message
*/
@@ -411,7 +428,7 @@ TRoster::HandleIsAppRegistered(BMessage *request)
FUNCTION_END();
}
// HandleRemovePreRegApp
/*! \brief Handles a RemovePreRegApp() request.
\param request The request message
*/
@@ -449,7 +466,7 @@ TRoster::HandleRemovePreRegApp(BMessage *request)
FUNCTION_END();
}
// HandleRemoveApp
/*! \brief Handles a RemoveApp() request.
\param request The request message
*/
@@ -465,7 +482,9 @@ TRoster::HandleRemoveApp(BMessage *request)
team_id team;
if (request->FindInt32("team", &team) != B_OK)
team = -1;
PRINT(("team: %ld\n", team));
// remove the app
if (error == B_OK) {
if (RosterAppInfo* info = fRegisteredApps.InfoFor(team)) {
@@ -487,7 +506,7 @@ PRINT(("team: %ld\n", team));
FUNCTION_END();
}
// HandleSetThreadAndTeam
/*! \brief Handles a SetThreadAndTeam() request.
\param request The request message
*/
@@ -499,6 +518,7 @@ TRoster::HandleSetThreadAndTeam(BMessage *request)
BAutolock _(fLock);
status_t error = B_OK;
// get the parameters
team_id team;
thread_id thread;
@@ -509,11 +529,14 @@ TRoster::HandleSetThreadAndTeam(BMessage *request)
thread = -1;
if (request->FindInt32("token", (int32*)&token) != B_OK)
SET_ERROR(error, B_BAD_VALUE);
// check the parameters
// team
if (error == B_OK && team < 0)
SET_ERROR(error, B_BAD_VALUE);
PRINT(("team: %ld, thread: %ld, token: %lu\n", team, thread, token));
// update the app_info
if (error == B_OK) {
RosterAppInfo* info = fEarlyPreRegisteredApps.InfoForToken(token);
@@ -575,7 +598,7 @@ PRINT(("team: %ld, thread: %ld, token: %lu\n", team, thread, token));
FUNCTION_END();
}
// HandleSetSignature
/*! \brief Handles a SetSignature() request.
\param request The request message
*/
@@ -614,7 +637,7 @@ TRoster::HandleSetSignature(BMessage *request)
FUNCTION_END();
}
// HandleGetAppInfo
/*! \brief Handles a Get{Running,Active,}AppInfo() request.
\param request The request message
*/
@@ -639,12 +662,14 @@ TRoster::HandleGetAppInfo(BMessage *request)
hasRef = false;
if (request->FindString("signature", &signature) != B_OK)
hasSignature = false;
if (hasTeam)
PRINT(("team: %ld\n", team));
if (hasRef)
PRINT(("ref: %ld, %lld, %s\n", ref.device, ref.directory, ref.name));
if (hasSignature)
PRINT(("signature: %s\n", signature));
// get the info
RosterAppInfo* info = NULL;
if (error == B_OK) {
@@ -683,7 +708,7 @@ PRINT(("signature: %s\n", signature));
FUNCTION_END();
}
// HandleGetAppList
/*! \brief Handles a GetAppList() request.
\param request The request message
*/
@@ -765,7 +790,7 @@ TRoster::HandleUpdateActiveApp(BMessage *request)
FUNCTION_END();
}
// HandleBroadcast
/*! \brief Handles a Broadcast() request.
\param request The request message
*/
@@ -834,7 +859,7 @@ TRoster::HandleBroadcast(BMessage *request)
FUNCTION_END();
}
// HandleStartWatching
/*! \brief Handles a StartWatching() request.
\param request The request message
*/
@@ -877,7 +902,7 @@ TRoster::HandleStartWatching(BMessage *request)
FUNCTION_END();
}
// HandleStopWatching
/*! \brief Handles a StopWatching() request.
\param request The request message
*/
@@ -911,7 +936,7 @@ TRoster::HandleStopWatching(BMessage *request)
FUNCTION_END();
}
// HandleGetRecentDocuments
/*! \brief Handles a GetRecentDocuments() request.
\param request The request message
*/
@@ -927,7 +952,7 @@ TRoster::HandleGetRecentDocuments(BMessage *request)
FUNCTION_END();
}
// HandleGetRecentFolders
/*! \brief Handles a GetRecentFolders() request.
\param request The request message
*/
@@ -943,7 +968,7 @@ TRoster::HandleGetRecentFolders(BMessage *request)
FUNCTION_END();
}
// HandleGetRecentApps
/*! \brief Handles a GetRecentApps() request.
\param request The request message
*/
@@ -971,7 +996,7 @@ TRoster::HandleGetRecentApps(BMessage *request)
FUNCTION_END();
}
// HandleAddToRecentDocuments
/*! \brief Handles an AddToRecentDocuments() request.
\param request The request message
*/
@@ -1002,7 +1027,7 @@ TRoster::HandleAddToRecentDocuments(BMessage *request)
FUNCTION_END();
}
// HandleAddToRecentFolders
/*! \brief Handles an AddToRecentFolders() request.
\param request The request message
*/
@@ -1033,7 +1058,7 @@ TRoster::HandleAddToRecentFolders(BMessage *request)
FUNCTION_END();
}
// HandleAddToRecentApps
/*! \brief Handles an AddToRecentApps() request.
\param request The request message
*/
@@ -1061,6 +1086,7 @@ TRoster::HandleAddToRecentApps(BMessage *request)
FUNCTION_END();
}
void
TRoster::HandleLoadRecentLists(BMessage* request)
{
@@ -1085,6 +1111,7 @@ TRoster::HandleLoadRecentLists(BMessage *request)
FUNCTION_END();
}
void
TRoster::HandleSaveRecentLists(BMessage* request)
{
@@ -1109,7 +1136,7 @@ TRoster::HandleSaveRecentLists(BMessage *request)
FUNCTION_END();
}
// ClearRecentDocuments
/*! \brief Clears the current list of recent documents
*/
void
@@ -1120,7 +1147,7 @@ TRoster::ClearRecentDocuments()
fRecentDocuments.Clear();
}
// ClearRecentFolders
/*! \brief Clears the current list of recent folders
*/
void
@@ -1131,7 +1158,7 @@ TRoster::ClearRecentFolders()
fRecentFolders.Clear();
}
// ClearRecentApps
/*! \brief Clears the current list of recent apps
*/
void
@@ -1142,7 +1169,7 @@ TRoster::ClearRecentApps()
fRecentApps.Clear();
}
// Init
/*! \brief Initializes the roster.
Currently only adds the registrar to the roster.
@@ -1176,8 +1203,7 @@ TRoster::Init()
if (error == B_OK) {
info->Init(be_app->Thread(), be_app->Team(),
BMessenger::Private(be_app_messenger).Port(),
B_EXCLUSIVE_LAUNCH | B_BACKGROUND_APP, &ref,
kRegistrarSignature);
B_EXCLUSIVE_LAUNCH | B_BACKGROUND_APP, &ref, kRegistrarSignature);
info->state = APP_STATE_REGISTERED;
info->registration_time = system_time();
error = AddApp(info);
@@ -1190,7 +1216,7 @@ TRoster::Init()
return error;
}
// AddApp
/*! \brief Add the supplied app info to the list of (pre-)registered apps.
\param info The app info to be added
@@ -1208,7 +1234,7 @@ TRoster::AddApp(RosterAppInfo *info)
return error;
}
// RemoveApp
/*! \brief Removes the supplied app info from the list of (pre-)registered
apps.
@@ -1258,7 +1284,7 @@ TRoster::UpdateActiveApp(RosterAppInfo *info)
}
}
// CheckSanity
/*! \brief Checks whether the (pre-)registered applications are still running.
This is necessary, since killed applications don't unregister properly.
@@ -1301,7 +1327,7 @@ TRoster::CheckSanity()
// don't delete infos a second time
}
// SetShuttingDown
/*! \brief Tells the roster whether a shutdown process is in progess at the
moment.
@@ -1319,7 +1345,7 @@ TRoster::SetShuttingDown(bool shuttingDown)
fShuttingDown = shuttingDown;
}
// GetShutdownApps
/*! \brief Returns lists of applications to be asked to quit on shutdown.
\param userApps List of RosterAppInfos identifying the user applications.
@@ -1410,7 +1436,7 @@ TRoster::GetShutdownApps(AppInfoList &userApps, AppInfoList &systemApps,
return error;
}
// AddWatcher
status_t
TRoster::AddWatcher(Watcher* watcher)
{
@@ -1426,7 +1452,6 @@ TRoster::AddWatcher(Watcher *watcher)
}
// RemoveWatcher
void
TRoster::RemoveWatcher(Watcher* watcher)
{
@@ -1437,7 +1462,6 @@ TRoster::RemoveWatcher(Watcher *watcher)
}
// _AppAdded
/*! \brief Hook method invoked, when an application has been fully registered.
\param info The RosterAppInfo of the added application.
*/
@@ -1451,8 +1475,9 @@ TRoster::_AppAdded(RosterAppInfo *info)
fWatchingService.NotifyWatchers(&message, &filter);
}
// _AppRemoved
/*! \brief Hook method invoked, when a fully registered application has been removed.
/*! \brief Hook method invoked, when a fully registered application has been
removed.
\param info The RosterAppInfo of the removed application.
*/
void
@@ -1471,15 +1496,14 @@ TRoster::_AppRemoved(RosterAppInfo *info)
}
}
// _AppActivated
/*! \brief Hook method invoked, when an application has been activated.
\param info The RosterAppInfo of the activated application.
*/
void
TRoster::_AppActivated(RosterAppInfo* info)
{
if (info) {
if (info->state == APP_STATE_REGISTERED) {
if (info != NULL && info->state == APP_STATE_REGISTERED) {
// send B_APP_ACTIVATED to the app
BMessenger messenger;
BMessenger::Private messengerPrivate(messenger);
@@ -1496,17 +1520,15 @@ TRoster::_AppActivated(RosterAppInfo *info)
fWatchingService.NotifyWatchers(&watcherMessage, &filter);
}
}
}
// _AppDeactivated
/*! \brief Hook method invoked, when an application has been deactivated.
\param info The RosterAppInfo of the deactivated application.
*/
void
TRoster::_AppDeactivated(RosterAppInfo* info)
{
if (info) {
if (info->state == APP_STATE_REGISTERED) {
if (info != NULL && info->state == APP_STATE_REGISTERED) {
// send B_APP_ACTIVATED to the app
BMessenger messenger;
BMessenger::Private messengerPrivate(messenger);
@@ -1517,9 +1539,8 @@ TRoster::_AppDeactivated(RosterAppInfo *info)
MessageDeliverer::Default()->DeliverMessage(&message, messenger);
}
}
}
// _AddMessageAppInfo
/*! \brief Adds an app_info to a message.
The info is added as a flat_app_info to a field "app_info" with the type
@@ -1536,17 +1557,19 @@ TRoster::_AddMessageAppInfo(BMessage *message, const app_info *info)
// pointer. Therefore we flatten the info.
flat_app_info flatInfo;
flatInfo.info = *info;
// set the ref name to NULL and copy it into the flat structure
flatInfo.info.ref.name = NULL;
flatInfo.ref_name[0] = '\0';
if (info->ref.name)
strcpy(flatInfo.ref_name, info->ref.name);
// add the flat info
return message->AddData("app_info", B_REG_APP_INFO_TYPE, &flatInfo,
sizeof(flat_app_info));
}
// _AddMessageWatchingInfo
/*! \brief Adds application monitoring related fields to a message.
\param message The message.
\param info The app_info of the concerned application.
@@ -1569,7 +1592,7 @@ TRoster::_AddMessageWatchingInfo(BMessage *message, const app_info *info)
return error;
}
// _NextToken
/*! \brief Returns the next available token.
\return The token.
*/
@@ -1579,7 +1602,7 @@ TRoster::_NextToken()
return ++fLastToken;
}
// _AddIARRequest
/*! \brief Adds an IsAppRegistered() request to the given map.
If something goes wrong, the method deletes the request.
@@ -1607,7 +1630,7 @@ TRoster::_AddIARRequest(IARRequestMap& map, int32 key, BMessage* request)
requests->AddMessage(request);
}
// _ReplyToIARRequests
/*! \brief Invokes _ReplyToIARRequest() for all messages in the given
message queue.
@@ -1616,8 +1639,7 @@ TRoster::_AddIARRequest(IARRequestMap& map, int32 key, BMessage* request)
(may be \c NULL)
*/
void
TRoster::_ReplyToIARRequests(BMessageQueue *requests,
const RosterAppInfo *info)
TRoster::_ReplyToIARRequests(BMessageQueue* requests, const RosterAppInfo* info)
{
while (BMessage* request = requests->NextMessage()) {
_ReplyToIARRequest(request, info);
@@ -1625,7 +1647,7 @@ TRoster::_ReplyToIARRequests(BMessageQueue *requests,
}
}
// _ReplyToIARRequest
/*! \brief Sends a reply message to an IsAppRegistered() request.
The message to be sent is a simple \c B_REG_SUCCESS message containing
@@ -1663,7 +1685,7 @@ PRINT(("_ReplyToIARRequest(): pre-registered: %d\n", preRegistered));
request->SendReply(&reply);
}
// _HandleGetRecentEntries
/*! \brief Handles requests for both GetRecentDocuments() and
GetRecentFolders().
*/
@@ -1686,7 +1708,8 @@ TRoster::_HandleGetRecentEntries(BMessage *request)
// Look for optional file type(s)
if (!error) {
type_code typeFound;
status_t typeError = request->GetInfo("file type", &typeFound, &fileTypesCount);
status_t typeError = request->GetInfo("file type", &typeFound,
&fileTypesCount);
if (!typeError)
typeError = typeFound == B_STRING_TYPE ? B_OK : B_BAD_TYPE;
if (!typeError) {
@@ -1780,16 +1803,14 @@ bool
TRoster::_IsSystemApp(RosterAppInfo* info) const
{
BPath path;
status_t error = path.SetTo(&info->ref);
if (error != B_OK)
if (path.SetTo(&info->ref) != B_OK || path.GetParent(&path) != B_OK)
return false;
int len = strlen(path.Path());
int prefixLen = strlen(kSystemAppPathPrefix);
return (len > prefixLen
&& strncmp(path.Path(), kSystemAppPathPrefix, prefixLen) == 0);
return !strcmp(path.Path(), kSystemAppPath)
|| !strcmp(path.Path(), kSystemServerPath);
}
status_t
TRoster::_LoadRosterSettings(const char* path)
{
@@ -1859,7 +1880,7 @@ TRoster::_LoadRosterSettings(const char *path)
case etFolder:
{
// For curing laziness
std::list<recent_entry*> *list = (type == etDoc)
std::list<recent_entry*>* list = type == etDoc
? &fRecentDocuments.fEntryList
: &fRecentFolders.fEntryList;
@@ -1890,7 +1911,8 @@ TRoster::_LoadRosterSettings(const char *path)
}
recent_entry* entry = NULL;
if (!streamError) {
entry = new(nothrow) recent_entry(&ref, app, index);
entry = new(nothrow) recent_entry(&ref, app,
index);
streamError = entry ? B_OK : B_NO_MEMORY;
}
if (!streamError) {
@@ -1949,18 +1971,20 @@ TRoster::_LoadRosterSettings(const char *path)
fRecentApps.Print();
printf("----------------------------------------------------------------------\n");
}
if (error)
D(PRINT(("WARNING: TRoster::_LoadRosterSettings(): error loading roster settings "
"from '%s', 0x%lx\n", settingsPath, error)));
if (error) {
D(PRINT(("WARNING: TRoster::_LoadRosterSettings(): error loading roster "
"settings from '%s', 0x%lx\n", settingsPath, error)));
}
return error;
}
status_t
TRoster::_SaveRosterSettings(const char* path)
{
BPath _path;
const char* settingsPath
= path ? path : get_default_roster_settings_file(_path);
= path != NULL ? path : get_default_roster_settings_file(_path);
status_t error;
FILE* file;
@@ -1970,40 +1994,22 @@ TRoster::_SaveRosterSettings(const char *path)
if (!error) {
status_t saveError;
saveError = fRecentDocuments.Save(file, "Recent documents", "RecentDoc");
if (saveError)
D(PRINT(("TRoster::_SaveRosterSettings(): recent documents save failed "
"with error 0x%lx\n", saveError)));
if (saveError) {
D(PRINT(("TRoster::_SaveRosterSettings(): recent documents save "
"failed with error 0x%lx\n", saveError)));
}
saveError = fRecentFolders.Save(file, "Recent folders", "RecentFolder");
if (saveError)
D(PRINT(("TRoster::_SaveRosterSettings(): recent folders save failed "
"with error 0x%lx\n", saveError)));
if (saveError) {
D(PRINT(("TRoster::_SaveRosterSettings(): recent folders save "
"failed with error 0x%lx\n", saveError)));
}
saveError = fRecentApps.Save(file);
if (saveError)
D(PRINT(("TRoster::_SaveRosterSettings(): recent folders save failed "
"with error 0x%lx\n", saveError)));
if (saveError) {
D(PRINT(("TRoster::_SaveRosterSettings(): recent folders save "
"failed with error 0x%lx\n", saveError)));
}
fclose(file);
}
return error;
}
// #pragma mark - Private local functions
/*! \brief Returns true if entry1's index is larger than entry2's index.
Also returns true if either entry is \c NULL.
Used for sorting the recent entry lists loaded from disk into the
proper order.
*/
bool
larger_index(const recent_entry *entry1, const recent_entry *entry2)
{
if (entry1 && entry2)
return entry1->index > entry2->index;
else
return true;
}
+13 -6
View File
@@ -13,6 +13,7 @@
#include <Locker.h>
#include <MessageQueue.h>
#include <Roster.h>
#include <SupportDefs.h>
#include <hash_set>
@@ -70,8 +71,10 @@ public:
void CheckSanity();
void SetShuttingDown(bool shuttingDown);
status_t GetShutdownApps(AppInfoList &userApps, AppInfoList &systemApps,
AppInfoList &backgroundApps, hash_set<team_id> &vitalSystemApps);
status_t GetShutdownApps(AppInfoList& userApps,
AppInfoList& systemApps,
AppInfoList& backgroundApps,
hash_set<team_id>& vitalSystemApps);
status_t AddWatcher(Watcher* watcher);
void RemoveWatcher(Watcher* watcher);
@@ -84,19 +87,23 @@ private:
void _AppDeactivated(RosterAppInfo* info);
// helper functions
static status_t _AddMessageAppInfo(BMessage* message, const app_info* info);
static status_t _AddMessageAppInfo(BMessage* message,
const app_info* info);
static status_t _AddMessageWatchingInfo(BMessage* message,
const app_info* info);
uint32 _NextToken();
void _AddIARRequest(IARRequestMap& map, int32 key, BMessage* request);
void _AddIARRequest(IARRequestMap& map, int32 key,
BMessage* request);
void _ReplyToIARRequests(BMessageQueue* requests,
const RosterAppInfo* info);
void _ReplyToIARRequest(BMessage* request, const RosterAppInfo* info);
void _ReplyToIARRequest(BMessage* request,
const RosterAppInfo* info);
void _HandleGetRecentEntries(BMessage* request);
void _ValidateRunning(const entry_ref& ref, const char* signature);
void _ValidateRunning(const entry_ref& ref,
const char* signature);
bool _IsSystemApp(RosterAppInfo* info) const;
status_t _LoadRosterSettings(const char* path = NULL);