From 2de709a5959306fd653cd6b3c970ce08bed3b12d Mon Sep 17 00:00:00 2001 From: "pinkerton%aol.net" Date: Tue, 1 Jun 2004 21:58:50 +0000 Subject: [PATCH] coalesce bookmark notifications and suppress all notifications during loading bookmarks, increases startup speed (bug 236373) --- camino/src/bookmarks/Bookmark.mm | 16 +++++++++++++ camino/src/bookmarks/BookmarkFolder.mm | 4 ++++ camino/src/bookmarks/BookmarkItem.h | 4 ++++ camino/src/bookmarks/BookmarkItem.m | 32 ++++++++++++++++++++++++- camino/src/bookmarks/BookmarkManager.mm | 5 ++++ 5 files changed, 60 insertions(+), 1 deletion(-) diff --git a/camino/src/bookmarks/Bookmark.mm b/camino/src/bookmarks/Bookmark.mm index 98b50045994b..22268f7ff5ab 100644 --- a/camino/src/bookmarks/Bookmark.mm +++ b/camino/src/bookmarks/Bookmark.mm @@ -506,6 +506,9 @@ static void doHTTPUpdateCallBack(CFReadStreamRef stream, CFStreamEventType type, -(BOOL) readNativeDictionary:(NSDictionary *)aDict { + //gather the redundant update notifications + [self setAccumulateUpdateNotifications:YES]; + [self setTitle:[aDict objectForKey:BMTitleKey]]; [self setDescription:[aDict objectForKey:BMDescKey]]; [self setKeyword:[aDict objectForKey:BMKeywordKey]]; @@ -513,14 +516,23 @@ static void doHTTPUpdateCallBack(CFReadStreamRef stream, CFStreamEventType type, [self setLastVisit:[aDict objectForKey:BMLastVisitKey]]; [self setNumberOfVisits:[[aDict objectForKey:BMNumberVisitsKey] unsignedIntValue]]; [self setStatus:[[aDict objectForKey:BMStatusKey] unsignedIntValue]]; + + //fire an update notification + [self setAccumulateUpdateNotifications:NO]; return YES; } -(BOOL) readSafariDictionary:(NSDictionary *)aDict { + //gather the redundant update notifications + [self setAccumulateUpdateNotifications:YES]; + NSDictionary *uriDict = [aDict objectForKey:SafariURIDictKey]; [self setTitle:[uriDict objectForKey:SafariBookmarkTitleKey]]; [self setUrl:[aDict objectForKey:SafariURLStringKey]]; + + //fire an update notification + [self setAccumulateUpdateNotifications:NO]; return YES; } @@ -535,10 +547,14 @@ static void doHTTPUpdateCallBack(CFReadStreamRef stream, CFStreamEventType type, elementInfoPtr = (CFXMLElementInfo *)CFXMLNodeGetInfoPtr(myNode); if (elementInfoPtr) { NSDictionary* attribDict = (NSDictionary*)elementInfoPtr->attributes; + //gather the redundant update notifications + [self setAccumulateUpdateNotifications:YES]; [self setTitle:[[attribDict objectForKey:CaminoNameKey] stringByRemovingAmpEscapes]]; [self setKeyword:[[attribDict objectForKey:CaminoKeywordKey] stringByRemovingAmpEscapes]]; [self setDescription:[[attribDict objectForKey:CaminoDescKey] stringByRemovingAmpEscapes]]; [self setUrl:[[attribDict objectForKey:CaminoURLKey] stringByRemovingAmpEscapes]]; + //fire an update notification + [self setAccumulateUpdateNotifications:NO]; } else { NSLog(@"Bookmark:readCaminoXML - elementInfoPtr null, load failed"); return NO; diff --git a/camino/src/bookmarks/BookmarkFolder.mm b/camino/src/bookmarks/BookmarkFolder.mm index 19bf65c9f91e..22c3e70a8424 100644 --- a/camino/src/bookmarks/BookmarkFolder.mm +++ b/camino/src/bookmarks/BookmarkFolder.mm @@ -760,6 +760,8 @@ NSString* BookmarkFolderDockMenuChangeNotificaton = @"bf_dmc"; -(void) itemAddedNote:(BookmarkItem *)theItem atIndex:(unsigned)anIndex { + if (![BookmarkItem allowNotifications]) return; + NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:theItem,BookmarkFolderChildKey, [NSNumber numberWithUnsignedInt:anIndex],BookmarkFolderChildIndexKey,nil]; @@ -769,6 +771,8 @@ NSString* BookmarkFolderDockMenuChangeNotificaton = @"bf_dmc"; -(void) itemRemovedNote:(BookmarkItem *)theItem { + if (![BookmarkItem allowNotifications]) return; + NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; NSDictionary *dict = [NSDictionary dictionaryWithObject:theItem forKey:BookmarkFolderChildKey]; NSNotification *note = [NSNotification notificationWithName:BookmarkFolderDeletionNotification object:self userInfo:dict]; diff --git a/camino/src/bookmarks/BookmarkItem.h b/camino/src/bookmarks/BookmarkItem.h index be8a8fb62aeb..8ef9897f0f40 100644 --- a/camino/src/bookmarks/BookmarkItem.h +++ b/camino/src/bookmarks/BookmarkItem.h @@ -50,6 +50,7 @@ NSString* mKeyword; NSString* mUUID; NSImage* mIcon; + BOOL mAccumulateItemChangeUpdates; } // Setters/Getters @@ -71,6 +72,9 @@ -(BOOL) isChildOfItem:(BookmarkItem *)anItem; // Notificaiton of Change ++(void) setSuppressAllUpdateNotifications:(BOOL)suppressUpdates; ++(BOOL) allowNotifications; +-(void) setAccumulateUpdateNotifications:(BOOL)suppressUpdates; -(void) itemUpdatedNote; //right now, just on title & icon - for BookmarkButton & BookmarkMenu notes // Methods called on startup for both bookmark & folder diff --git a/camino/src/bookmarks/BookmarkItem.m b/camino/src/bookmarks/BookmarkItem.m index aa495781d58e..b609b507d22b 100644 --- a/camino/src/bookmarks/BookmarkItem.m +++ b/camino/src/bookmarks/BookmarkItem.m @@ -84,6 +84,9 @@ NSString *CaminoTrueKey = @"true"; @implementation BookmarkItem + +static BOOL gSuppressAllUpdates = NO; + //Initialization -(id) init { @@ -96,7 +99,8 @@ NSString *CaminoTrueKey = @"true"; mUUID = nil; // if we set the icon here, we will get a memory leak. so don't. // subclass will provide icon. - mIcon = NULL; + mIcon = NULL; + mAccumulateItemChangeUpdates = NO; } return self; } @@ -221,13 +225,39 @@ NSString *CaminoTrueKey = @"true"; [aIcon retain]; [mIcon release]; mIcon = aIcon; + + if (![BookmarkItem allowNotifications]) return; NSNotification *note = [NSNotification notificationWithName:BookmarkIconChangedNotification object:self userInfo:nil]; [[NSNotificationCenter defaultCenter] postNotification:note]; } +// Prevents all NSNotification posting from any BookmarkItem. +// Useful for suppressing all the pointless notifications during load. ++(void) setSuppressAllUpdateNotifications:(BOOL)suppressUpdates +{ + gSuppressAllUpdates = suppressUpdates; +} + ++(BOOL) allowNotifications +{ + return !gSuppressAllUpdates; +} + +// Helps prevent spamming from itemUpdatedNote: +// calling with YES will prevent itemUpdatedNote from doing anything +// and calling with NO will restore itemUpdatedNote and then call it. +-(void) setAccumulateUpdateNotifications:(BOOL)accumulateUpdates +{ + mAccumulateItemChangeUpdates = accumulateUpdates; + if (!mAccumulateItemChangeUpdates) + [self itemUpdatedNote]; //fire an update to cover the updates that weren't sent +} + -(void) itemUpdatedNote { + if (gSuppressAllUpdates || mAccumulateItemChangeUpdates) return; + NSNotification *note = [NSNotification notificationWithName:BookmarkItemChangedNotification object:self userInfo:nil]; NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; [nc postNotification:note]; diff --git a/camino/src/bookmarks/BookmarkManager.mm b/camino/src/bookmarks/BookmarkManager.mm index f1b00f59da2c..b5d7437bbe6e 100644 --- a/camino/src/bookmarks/BookmarkManager.mm +++ b/camino/src/bookmarks/BookmarkManager.mm @@ -123,6 +123,10 @@ static unsigned gFirstUserCollection = 0; [root setTitle:NSLocalizedString(@"BookmarksRootName", @"")]; [self setRootBookmarks:root]; [root release]; + // Turn off the posting of update notifications while reading in bookmarks. + // All interested parties haven't been init'd yet, and/or will recieve the + // managerStartedNotification when setup is actually complete. + [BookmarkItem setSuppressAllUpdateNotifications:YES]; if (![self readBookmarks]) { // one of two things happened. we are importing off an old xml file // for startup, OR we totally muffed reading the bookmarks. we'll hope @@ -150,6 +154,7 @@ static unsigned gFirstUserCollection = 0; [aFolder setTitle:NSLocalizedString(@"Bookmark Toolbar",@"Bookmark Toolbar")]; } } + [BookmarkItem setSuppressAllUpdateNotifications:NO]; // setup special folders [self setupSmartCollections]; mSmartFolderManager = [[KindaSmartFolderManager alloc] initWithBookmarkManager:self];