aboutsummaryrefslogtreecommitdiffstats
path: root/chromeos/message_loops/message_loop_utils.cc
blob: cb2eb2269b47fd381dc57fccfb4584a3878d2854 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Copyright 2015 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <chromeos/message_loops/message_loop_utils.h>

#include <base/location.h>
#include <chromeos/bind_lambda.h>

namespace chromeos {

void MessageLoopRunUntil(
    MessageLoop* loop,
    base::TimeDelta timeout,
    base::Callback<bool()> terminate) {
  bool timeout_called = false;
  MessageLoop::TaskId task_id = loop->PostDelayedTask(
      FROM_HERE,
      base::Bind([&timeout_called]() { timeout_called = true; }),
      timeout);
  while (!timeout_called && (terminate.is_null() || !terminate.Run()))
    loop->RunOnce(true);

  if (!timeout_called)
    loop->CancelTask(task_id);
}

int MessageLoopRunMaxIterations(MessageLoop* loop, int iterations) {
  int result;
  for (result = 0; result < iterations && loop->RunOnce(false); result++) {}
  return result;
}

}  // namespace chromeos