OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2013 Google 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 are |
| 6 * met: |
| 7 * |
| 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above |
| 11 * copyright notice, this list of conditions and the following disclaimer |
| 12 * in the documentation and/or other materials provided with the |
| 13 * distribution. |
| 14 * * Neither the name of Google Inc. nor the names of its |
| 15 * contributors may be used to endorse or promote products derived from |
| 16 * this software without specific prior written permission. |
| 17 * |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ |
| 30 |
| 31 #ifndef AsyncMethodRunner_h |
| 32 #define AsyncMethodRunner_h |
| 33 |
| 34 #include "platform/Timer.h" |
| 35 #include "wtf/FastAllocBase.h" |
| 36 #include "wtf/Noncopyable.h" |
| 37 |
| 38 namespace WebCore { |
| 39 |
| 40 template <typename TargetClass> |
| 41 class AsyncMethodRunner { |
| 42 WTF_MAKE_NONCOPYABLE(AsyncMethodRunner); |
| 43 WTF_MAKE_FAST_ALLOCATED; |
| 44 public: |
| 45 typedef void (TargetClass::*TargetMethod)(); |
| 46 |
| 47 AsyncMethodRunner(TargetClass* object, TargetMethod method) |
| 48 : m_timer(this, &AsyncMethodRunner<TargetClass>::fired) |
| 49 , m_object(object) |
| 50 , m_method(method) |
| 51 , m_suspended(false) |
| 52 , m_runWhenResumed(false) |
| 53 { |
| 54 } |
| 55 |
| 56 // Schedules to run the method asynchronously. Do nothing if it's already |
| 57 // scheduled. If it's suspended, remember to schedule to run the method when |
| 58 // resume() is called. |
| 59 void runAsync() |
| 60 { |
| 61 if (m_suspended) { |
| 62 ASSERT(!m_timer.isActive()); |
| 63 m_runWhenResumed = true; |
| 64 return; |
| 65 } |
| 66 |
| 67 if (!m_timer.isActive()) |
| 68 m_timer.startOneShot(0); |
| 69 } |
| 70 |
| 71 // If it's scheduled to run the method, cancel it and remember to schedule |
| 72 // it again when resume() is called. Mainly for implementing |
| 73 // ActiveDOMObject::suspend(). |
| 74 void suspend() |
| 75 { |
| 76 ASSERT(!m_suspended); |
| 77 m_suspended = true; |
| 78 |
| 79 if (!m_timer.isActive()) |
| 80 return; |
| 81 |
| 82 m_timer.stop(); |
| 83 m_runWhenResumed = true; |
| 84 } |
| 85 |
| 86 // Resumes pending method run. |
| 87 void resume() |
| 88 { |
| 89 ASSERT(m_suspended); |
| 90 m_suspended = false; |
| 91 |
| 92 if (!m_runWhenResumed) |
| 93 return; |
| 94 |
| 95 m_runWhenResumed = false; |
| 96 m_timer.startOneShot(0); |
| 97 } |
| 98 |
| 99 void stop() |
| 100 { |
| 101 if (m_suspended) { |
| 102 ASSERT(!m_timer.isActive()); |
| 103 m_runWhenResumed = false; |
| 104 m_suspended = false; |
| 105 return; |
| 106 } |
| 107 |
| 108 ASSERT(!m_runWhenResumed); |
| 109 if (m_timer.isActive()) |
| 110 m_timer.stop(); |
| 111 } |
| 112 |
| 113 private: |
| 114 virtual void fired(Timer<AsyncMethodRunner<TargetClass> >*) { (m_object->*m_
method)(); } |
| 115 |
| 116 Timer<AsyncMethodRunner<TargetClass> > m_timer; |
| 117 |
| 118 TargetClass* m_object; |
| 119 TargetMethod m_method; |
| 120 |
| 121 bool m_suspended; |
| 122 bool m_runWhenResumed; |
| 123 }; |
| 124 |
| 125 } |
| 126 |
| 127 #endif |
OLD | NEW |