1 /* 2 * Copyright 2009, The Android Open Source Project 3 * Copyright (C) 2008 Google Inc. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include "config.h" 28 #include "PluginTimer.h" 29 30 namespace WebCore { 31 32 static uint32 gTimerID; 33 PluginTimer(PluginTimer ** list,NPP instance,bool repeat,void (* timerFunc)(NPP npp,uint32 timerID))34 PluginTimer::PluginTimer(PluginTimer** list, NPP instance, bool repeat, 35 void (*timerFunc)(NPP npp, uint32 timerID)) 36 : m_list(list), 37 m_instance(instance), 38 m_timerFunc(timerFunc), 39 m_repeat(repeat) 40 { 41 m_timerID = ++gTimerID; 42 43 m_next = *list; 44 if (m_next) { 45 m_next->m_prev = this; 46 } 47 m_prev = 0; 48 *list = this; 49 } 50 ~PluginTimer()51 PluginTimer::~PluginTimer() 52 { 53 if (m_next) { 54 m_next->m_prev = m_prev; 55 } 56 if (m_prev) { 57 m_prev->m_next = m_next; 58 } else { 59 *m_list = m_next; 60 } 61 } 62 fired()63 void PluginTimer::fired() 64 { 65 m_timerFunc(m_instance, m_timerID); 66 if (!m_repeat) { 67 delete this; 68 } 69 } 70 71 // may return null if timerID is not found Find(PluginTimer * list,uint32 timerID)72 PluginTimer* PluginTimer::Find(PluginTimer* list, uint32 timerID) 73 { 74 PluginTimer* curr = list; 75 while (curr) { 76 if (curr->m_timerID == timerID) { 77 break; 78 } 79 curr = curr->m_next; 80 } 81 return curr; 82 } 83 84 /////////////////////////////////////////////////////////////////////////// 85 ~PluginTimerList()86 PluginTimerList::~PluginTimerList() 87 { 88 while (m_list) { 89 delete m_list; 90 } 91 } 92 schedule(NPP instance,uint32 interval,bool repeat,void (* proc)(NPP npp,uint32 timerID))93 uint32 PluginTimerList::schedule(NPP instance, uint32 interval, bool repeat, 94 void (*proc)(NPP npp, uint32 timerID)) 95 { 96 PluginTimer* timer = new PluginTimer(&m_list, instance, repeat, proc); 97 98 double dinterval = interval * 0.001; // milliseconds to seconds 99 if (repeat) { 100 timer->startRepeating(dinterval); 101 } else { 102 timer->startOneShot(dinterval); 103 } 104 return timer->timerID(); 105 } 106 unschedule(NPP instance,uint32 timerID)107 void PluginTimerList::unschedule(NPP instance, uint32 timerID) 108 { 109 delete PluginTimer::Find(m_list, timerID); 110 } 111 112 } // namespace WebCore 113