WebKit2/Platform/mac/RunLoopMac.mm
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 2010 Apple Inc. All rights reserved.
       
     3  *
       
     4  * Redistribution and use in source and binary forms, with or without
       
     5  * modification, are permitted provided that the following conditions
       
     6  * are met:
       
     7  * 1. Redistributions of source code must retain the above copyright
       
     8  *    notice, this list of conditions and the following disclaimer.
       
     9  * 2. Redistributions in binary form must reproduce the above copyright
       
    10  *    notice, this list of conditions and the following disclaimer in the
       
    11  *    documentation and/or other materials provided with the distribution.
       
    12  *
       
    13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
       
    14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
       
    15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
       
    16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
       
    17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
       
    18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
       
    19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
       
    20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
       
    21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
       
    22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
       
    23  * THE POSSIBILITY OF SUCH DAMAGE.
       
    24  */
       
    25 
       
    26 #include "RunLoop.h"
       
    27 
       
    28 #include "WorkItem.h"
       
    29 
       
    30 void RunLoop::performWork(void* context)
       
    31 {
       
    32     static_cast<RunLoop*>(context)->performWork();
       
    33 }
       
    34 
       
    35 RunLoop::RunLoop()
       
    36 {
       
    37     m_runLoop = CFRunLoopGetCurrent();
       
    38 
       
    39     CFRunLoopSourceContext context = { 0, this, 0, 0, 0, 0, 0, 0, 0, performWork };
       
    40     m_runLoopSource = CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &context);
       
    41     CFRunLoopAddSource(m_runLoop, m_runLoopSource, kCFRunLoopCommonModes);
       
    42 }
       
    43 
       
    44 RunLoop::~RunLoop()
       
    45 {
       
    46     // FIXME: Tear down the work item queue here.
       
    47     CFRunLoopSourceInvalidate(m_runLoopSource);
       
    48     CFRelease(m_runLoopSource);
       
    49 }
       
    50 
       
    51 void RunLoop::run()
       
    52 {
       
    53     if (current() == main()) {
       
    54         // Use -[NSApplication run] for the main run loop.
       
    55         [NSApp run];
       
    56     } else {
       
    57         // Otherwise, use NSRunLoop. We do this because it sets up an autorelease pool for us.
       
    58         [[NSRunLoop currentRunLoop] run];
       
    59     }        
       
    60 }
       
    61 
       
    62 void RunLoop::stop()
       
    63 {
       
    64     ASSERT(m_runLoop == CFRunLoopGetCurrent());
       
    65     
       
    66     if (m_runLoop == main()->m_runLoop) {
       
    67         [NSApp stop:nil];
       
    68         NSEvent *event = [NSEvent otherEventWithType:NSApplicationDefined
       
    69                                             location:NSMakePoint(0, 0)
       
    70                                        modifierFlags:0
       
    71                                            timestamp:0.0
       
    72                                          windowNumber:0
       
    73                                              context:nil
       
    74                                              subtype: 0
       
    75                                                data1:0
       
    76                                                data2:0];
       
    77         [NSApp postEvent:event atStart:true];
       
    78     } else
       
    79         CFRunLoopStop(m_runLoop);
       
    80 }
       
    81 
       
    82 void RunLoop::wakeUp()
       
    83 {
       
    84     CFRunLoopSourceSignal(m_runLoopSource);
       
    85     CFRunLoopWakeUp(m_runLoop);
       
    86 }
       
    87 
       
    88 // RunLoop::Timer
       
    89 
       
    90 void RunLoop::TimerBase::timerFired(CFRunLoopTimerRef, void* context)
       
    91 {
       
    92     TimerBase* timer = static_cast<TimerBase*>(context);
       
    93     timer->fired();
       
    94 }
       
    95 
       
    96 RunLoop::TimerBase::TimerBase(RunLoop* runLoop)
       
    97     : m_runLoop(runLoop)
       
    98     , m_timer(0)
       
    99 {
       
   100 }
       
   101 
       
   102 RunLoop::TimerBase::~TimerBase()
       
   103 {
       
   104     stop();
       
   105 }
       
   106 
       
   107 void RunLoop::TimerBase::start(double nextFireInterval, double repeatInterval)
       
   108 {
       
   109     if (m_timer)
       
   110         stop();
       
   111 
       
   112     CFRunLoopTimerContext context = { 0, this, 0, 0, 0 };
       
   113     m_timer = CFRunLoopTimerCreate(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent() + nextFireInterval, repeatInterval, 0, 0, timerFired, &context);
       
   114     CFRunLoopAddTimer(m_runLoop->m_runLoop, m_timer, kCFRunLoopCommonModes);
       
   115 }
       
   116 
       
   117 void RunLoop::TimerBase::stop()
       
   118 {
       
   119     if (!m_timer)
       
   120         return;
       
   121 
       
   122     CFRunLoopTimerInvalidate(m_timer);
       
   123     CFRelease(m_timer);
       
   124     m_timer = 0;
       
   125 }
       
   126 
       
   127 bool RunLoop::TimerBase::isActive() const
       
   128 {
       
   129     return m_timer && CFRunLoopTimerIsValid(m_timer);
       
   130 }