OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "sky/engine/config.h" |
| 6 #include "sky/engine/tonic/dart_timer_heap.h" |
| 7 |
| 8 #include "base/bind.h" |
| 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/time/time.h" |
| 11 #include "sky/engine/tonic/dart_api_scope.h" |
| 12 #include "sky/engine/tonic/dart_invoke.h" |
| 13 #include "sky/engine/tonic/dart_isolate_scope.h" |
| 14 #include "sky/engine/tonic/dart_state.h" |
| 15 |
| 16 namespace blink { |
| 17 |
| 18 DartTimerHeap::DartTimerHeap() : next_timer_id_(1), weak_factory_(this) { |
| 19 } |
| 20 |
| 21 DartTimerHeap::~DartTimerHeap() { |
| 22 } |
| 23 |
| 24 int DartTimerHeap::Add(PassOwnPtr<Task> task) { |
| 25 int id = next_timer_id_++; |
| 26 Schedule(id, task); |
| 27 return id; |
| 28 } |
| 29 |
| 30 void DartTimerHeap::Remove(int id) { |
| 31 heap_.remove(id); |
| 32 } |
| 33 |
| 34 void DartTimerHeap::Schedule(int id, PassOwnPtr<Task> task) { |
| 35 base::TimeDelta delay = task->delay; |
| 36 heap_.add(id, task); |
| 37 base::MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 38 base::Bind(&DartTimerHeap::Run, weak_factory_.GetWeakPtr(), id), delay); |
| 39 } |
| 40 |
| 41 void DartTimerHeap::Run(int id) { |
| 42 auto it = heap_.find(id); |
| 43 if (it == heap_.end()) |
| 44 return; |
| 45 OwnPtr<Task> task = it->value.release(); |
| 46 heap_.remove(it); |
| 47 if (!task->closure.dart_state()) |
| 48 return; |
| 49 DartIsolateScope scope(task->closure.dart_state()->isolate()); |
| 50 DartApiScope api_scope; |
| 51 DartInvokeAppClosure(task->closure.value(), 0, nullptr); |
| 52 if (task->repeating) |
| 53 Schedule(id, task.release()); |
| 54 } |
| 55 |
| 56 } |
OLD | NEW |