Changeset 40468 in webkit


Ignore:
Timestamp:
Jan 31, 2009 4:59:19 PM (15 years ago)
Author:
sullivan@apple.com
Message:

2009-01-31 John Sullivan <sullivan@apple.com>

https://bugs.webkit.org/show_bug.cgi?id=23665

Cleaned up code to add/remove NSNotification observers, to avoid performance hit
of calling removeObserver with unspecified notifications, or calling removeObserver
multiple times for the same notification.

Reviewed by Darin Adler

  • WebView/WebHTMLView.mm: added observingMouseMovedNotifications, observingSuperviewNotifications, and observingWindowNotifications as BOOL ivars of _private object (-[WebHTMLView _removeMouseMovedObserverUnconditionally]): moved to file-internal section of file, added leading underscore, now bails out if we aren't observing the relevant notifications, now records that we are no longer observing the relevant notifications (-[WebHTMLView _removeSuperviewObservers]): ditto, also stores [NSNoticationCenter defaultCenter] in local var to avoid objc dispatch (-[WebHTMLView _removeWindowObservers]): ditto (-[WebHTMLView close]): replace general removeObserver: call with three specific calls for all the notifications that this class actually observes (-[WebHTMLView addMouseMovedObserver]): bail out if already observing relevant notifications, now records that we are observing the relevant notifications (-[WebHTMLView removeMouseMovedObserver]): updated for name change (-[WebHTMLView addSuperviewObservers]): bail out if already observing relevant notifications, now records that we are observing the relevant notifications; also stores [NSNoticationCenter defaultCenter] in local var to avoid objc dispatch (-[WebHTMLView addWindowObservers]): ditto (-[WebHTMLView viewWillMoveToSuperview:]): updated for name change (-[WebHTMLView viewWillMoveToWindow:]): updated for name changes
Location:
trunk/WebKit/mac
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebKit/mac/ChangeLog

    r40465 r40468  
     12009-01-31  John Sullivan  <sullivan@apple.com>
     2
     3        https://bugs.webkit.org/show_bug.cgi?id=23665
     4
     5        Cleaned up code to add/remove NSNotification observers, to avoid performance hit
     6        of calling removeObserver with unspecified notifications, or calling removeObserver
     7        multiple times for the same notification.
     8
     9        Reviewed by Darin Adler
     10
     11        * WebView/WebHTMLView.mm:
     12        added observingMouseMovedNotifications, observingSuperviewNotifications, and
     13        observingWindowNotifications as BOOL ivars of _private object
     14        (-[WebHTMLView _removeMouseMovedObserverUnconditionally]):
     15        moved to file-internal section of file, added leading underscore, now bails out
     16        if we aren't observing the relevant notifications, now records that we are no longer
     17        observing the relevant notifications
     18        (-[WebHTMLView _removeSuperviewObservers]):
     19        ditto, also stores [NSNoticationCenter defaultCenter] in local var to avoid objc dispatch
     20        (-[WebHTMLView _removeWindowObservers]):
     21        ditto
     22        (-[WebHTMLView close]):
     23        replace general removeObserver: call with three specific calls for all the notifications
     24        that this class actually observes
     25        (-[WebHTMLView addMouseMovedObserver]):
     26        bail out if already observing relevant notifications, now records that we are observing
     27        the relevant notifications
     28        (-[WebHTMLView removeMouseMovedObserver]):
     29        updated for name change
     30        (-[WebHTMLView addSuperviewObservers]):
     31        bail out if already observing relevant notifications, now records that we are observing
     32        the relevant notifications; also stores [NSNoticationCenter defaultCenter] in local var
     33        to avoid objc dispatch
     34        (-[WebHTMLView addWindowObservers]):
     35        ditto
     36        (-[WebHTMLView viewWillMoveToSuperview:]):
     37        updated for name change
     38        (-[WebHTMLView viewWillMoveToWindow:]):
     39        updated for name changes
     40
    1412009-01-31  Darin Adler  <darin@apple.com>
    242
  • trunk/WebKit/mac/WebView/WebHTMLView.mm

    r40465 r40468  
    290290- (void)_pasteWithPasteboard:(NSPasteboard *)pasteboard allowPlainText:(BOOL)allowPlainText;
    291291- (void)_pasteAsPlainTextWithPasteboard:(NSPasteboard *)pasteboard;
     292- (void)_removeMouseMovedObserverUnconditionally;
     293- (void)_removeSuperviewObservers;
     294- (void)_removeWindowObservers;
    292295- (BOOL)_shouldInsertFragment:(DOMDocumentFragment *)fragment replacingDOMRange:(DOMRange *)range givenAction:(WebViewInsertAction)action;
    293296- (BOOL)_shouldInsertText:(NSString *)text replacingDOMRange:(DOMRange *)range givenAction:(WebViewInsertAction)action;
     
    373376    BOOL printing;
    374377    BOOL avoidingPrintOrphan;
     378    BOOL observingMouseMovedNotifications;
     379    BOOL observingSuperviewNotifications;
     380    BOOL observingWindowNotifications;
    375381   
    376382    id savedSubviews;
     
    756762}
    757763
     764- (void)_removeMouseMovedObserverUnconditionally
     765{
     766    if (!_private->observingMouseMovedNotifications)
     767        return;
     768   
     769    [[NSNotificationCenter defaultCenter] removeObserver:self name:WKMouseMovedNotification() object:nil];
     770    _private->observingMouseMovedNotifications = false;
     771}
     772
     773- (void)_removeSuperviewObservers
     774{
     775    if (!_private->observingSuperviewNotifications)
     776        return;
     777   
     778    NSView *superview = [self superview];
     779    if (!superview || ![self window])
     780        return;
     781   
     782    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
     783    [notificationCenter removeObserver:self name:NSViewFrameDidChangeNotification object:superview];
     784    [notificationCenter removeObserver:self name:NSViewBoundsDidChangeNotification object:superview];
     785   
     786    _private->observingSuperviewNotifications = false;
     787}
     788
     789- (void)_removeWindowObservers
     790{
     791    if (!_private->observingWindowNotifications)
     792        return;
     793   
     794    NSWindow *window = [self window];
     795    if (!window)
     796        return;
     797   
     798    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
     799    [notificationCenter removeObserver:self name:NSWindowDidBecomeKeyNotification object:nil];
     800    [notificationCenter removeObserver:self name:NSWindowDidResignKeyNotification object:nil];
     801    [notificationCenter removeObserver:self name:NSWindowWillCloseNotification object:window];
     802    [notificationCenter removeObserver:self name:WKWindowWillOrderOnScreenNotification() object:window];
     803   
     804    _private->observingWindowNotifications = false;
     805}
     806
    758807- (BOOL)_shouldInsertFragment:(DOMDocumentFragment *)fragment replacingDOMRange:(DOMRange *)range givenAction:(WebViewInsertAction)action
    759808{
     
    18441893    [self _cancelUpdateFocusedAndActiveStateTimer];
    18451894    [self _clearLastHitViewIfSelf];
    1846     // FIXME: This is slow; should remove individual observers instead.
    1847     [[NSNotificationCenter defaultCenter] removeObserver:self];
     1895    [self _removeMouseMovedObserverUnconditionally];
     1896    [self _removeWindowObservers];
     1897    [self _removeSuperviewObservers];
    18481898    [_private->pluginController destroyAllPlugins];
    18491899    [_private->pluginController setDataSource:nil];
     
    25672617- (void)addMouseMovedObserver
    25682618{
    2569     if (!_private->dataSource || ![self _isTopHTMLView])
     2619    if (!_private->dataSource || ![self _isTopHTMLView] || _private->observingMouseMovedNotifications)
    25702620        return;
    25712621
     
    25812631        name:WKMouseMovedNotification() object:nil];
    25822632    [self _frameOrBoundsChanged];
    2583 }
    2584 
    2585 - (void)removeMouseMovedObserverUnconditionally
    2586 {
    2587     [[NSNotificationCenter defaultCenter] removeObserver:self
    2588         name:WKMouseMovedNotification() object:nil];
     2633    _private->observingMouseMovedNotifications = true;
    25892634}
    25902635
     
    25982643
    25992644    [[self _webView] _mouseDidMoveOverElement:nil modifierFlags:0];
    2600     [self removeMouseMovedObserverUnconditionally];
     2645    [self _removeMouseMovedObserverUnconditionally];
    26012646}
    26022647
     
    26112656    // sizes that are based on the total size of the view.
    26122657   
     2658    if (_private->observingSuperviewNotifications)
     2659        return;
     2660
    26132661    NSView *superview = [self superview];
    2614     if (superview && [self window]) {
    2615         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_frameOrBoundsChanged)
    2616             name:NSViewFrameDidChangeNotification object:superview];
    2617         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_frameOrBoundsChanged)
    2618             name:NSViewBoundsDidChangeNotification object:superview];
    2619 
    2620         // In addition to registering for frame/bounds change notifications, call -_frameOrBoundsChanged.
    2621         // It will check the current size/scroll against the previous layout's size/scroll.  We need to
    2622         // do this here to catch the case where the WebView is laid out at one size, removed from its
    2623         // window, resized, and inserted into another window.  Our frame/bounds changed notifications
    2624         // will not be sent in that situation, since we only watch for changes while in the view hierarchy.
    2625         [self _frameOrBoundsChanged];
    2626     }
    2627 }
    2628 
    2629 - (void)removeSuperviewObservers
    2630 {
    2631     NSView *superview = [self superview];
    2632     if (superview && [self window]) {
    2633         [[NSNotificationCenter defaultCenter] removeObserver:self
    2634             name:NSViewFrameDidChangeNotification object:superview];
    2635         [[NSNotificationCenter defaultCenter] removeObserver:self
    2636             name:NSViewBoundsDidChangeNotification object:superview];
    2637     }
     2662    if (!superview || ![self window])
     2663        return;
     2664   
     2665    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
     2666    [notificationCenter addObserver:self selector:@selector(_frameOrBoundsChanged) name:NSViewFrameDidChangeNotification object:superview];
     2667    [notificationCenter addObserver:self selector:@selector(_frameOrBoundsChanged) name:NSViewBoundsDidChangeNotification object:superview];
     2668   
     2669    // In addition to registering for frame/bounds change notifications, call -_frameOrBoundsChanged.
     2670    // It will check the current size/scroll against the previous layout's size/scroll.  We need to
     2671    // do this here to catch the case where the WebView is laid out at one size, removed from its
     2672    // window, resized, and inserted into another window.  Our frame/bounds changed notifications
     2673    // will not be sent in that situation, since we only watch for changes while in the view hierarchy.
     2674    [self _frameOrBoundsChanged];
     2675   
     2676    _private->observingSuperviewNotifications = true;
    26382677}
    26392678
    26402679- (void)addWindowObservers
    26412680{
     2681    if (_private->observingWindowNotifications)
     2682        return;
     2683   
    26422684    NSWindow *window = [self window];
    2643     if (window) {
    2644         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidBecomeKey:)
    2645             name:NSWindowDidBecomeKeyNotification object:nil];
    2646         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidResignKey:)
    2647             name:NSWindowDidResignKeyNotification object:nil];
    2648         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowWillClose:)
    2649             name:NSWindowWillCloseNotification object:window];
    2650         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowWillOrderOnScreen:)
    2651             name:WKWindowWillOrderOnScreenNotification() object:window];
    2652     }
    2653 }
    2654 
    2655 - (void)removeWindowObservers
    2656 {
    2657     NSWindow *window = [self window];
    2658     if (window) {
    2659         [[NSNotificationCenter defaultCenter] removeObserver:self
    2660             name:NSWindowDidBecomeKeyNotification object:nil];
    2661         [[NSNotificationCenter defaultCenter] removeObserver:self
    2662             name:NSWindowDidResignKeyNotification object:nil];
    2663         [[NSNotificationCenter defaultCenter] removeObserver:self
    2664             name:NSWindowWillCloseNotification object:window];
    2665         [[NSNotificationCenter defaultCenter] removeObserver:self
    2666             name:WKWindowWillOrderOnScreenNotification() object:window];
    2667     }
     2685    if (!window)
     2686        return;
     2687   
     2688    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
     2689    [notificationCenter addObserver:self selector:@selector(windowDidBecomeKey:) name:NSWindowDidBecomeKeyNotification object:nil];
     2690    [notificationCenter addObserver:self selector:@selector(windowDidResignKey:) name:NSWindowDidResignKeyNotification object:nil];
     2691    [notificationCenter addObserver:self selector:@selector(windowWillClose:) name:NSWindowWillCloseNotification object:window];
     2692    [notificationCenter addObserver:self selector:@selector(windowWillOrderOnScreen:) name:WKWindowWillOrderOnScreenNotification() object:window];
     2693   
     2694    _private->observingWindowNotifications = true;
    26682695}
    26692696
    26702697- (void)viewWillMoveToSuperview:(NSView *)newSuperview
    26712698{
    2672     [self removeSuperviewObservers];
     2699    [self _removeSuperviewObservers];
    26732700}
    26742701
     
    26952722
    26962723    // FIXME: Some of these calls may not work because this view may be already removed from it's superview.
    2697     [self removeMouseMovedObserverUnconditionally];
    2698     [self removeWindowObservers];
    2699     [self removeSuperviewObservers];
     2724    [self _removeMouseMovedObserverUnconditionally];
     2725    [self _removeWindowObservers];
     2726    [self _removeSuperviewObservers];
    27002727    [self _cancelUpdateMouseoverTimer];
    27012728    [self _cancelUpdateFocusedAndActiveStateTimer];
Note: See TracChangeset for help on using the changeset viewer.