summaryrefslogtreecommitdiffstats
path: root/components/test_runner/web_task.h
blob: 6261405f1023d5ba579d64f2c1d06cb07941d920 (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_TEST_RUNNER_WEB_TASK_H_
#define COMPONENTS_TEST_RUNNER_WEB_TASK_H_

#include <vector>

#include "base/macros.h"

namespace test_runner {

class WebTaskList;

// WebTask represents a task which can run by WebTestDelegate::postTask() or
// WebTestDelegate::postDelayedTask().
class WebTask {
 public:
  explicit WebTask(WebTaskList*);
  virtual ~WebTask();

  // The main code of this task.
  // An implementation of run() should return immediately if cancel() was
  // called.
  virtual void run() = 0;
  virtual void cancel() = 0;

 protected:
  WebTaskList* task_list_;
};

class WebTaskList {
 public:
  WebTaskList();
  ~WebTaskList();
  void RegisterTask(WebTask*);
  void UnregisterTask(WebTask*);
  void RevokeAll();

 private:
  std::vector<WebTask*> tasks_;

  DISALLOW_COPY_AND_ASSIGN(WebTaskList);
};

// A task containing an object pointer of class T. Derived classes should
// override RunIfValid() which in turn can safely invoke methods on the
// object_. The Class T must have "WebTaskList* mutable_task_list()".
template <class T>
class WebMethodTask : public WebTask {
 public:
  explicit WebMethodTask(T* object)
      : WebTask(object->mutable_task_list()), object_(object) {}

  virtual ~WebMethodTask() {}

  void run() override {
    if (object_)
      RunIfValid();
  }

  void cancel() override {
    object_ = 0;
    task_list_->UnregisterTask(this);
    task_list_ = 0;
  }

  virtual void RunIfValid() = 0;

 protected:
  T* object_;
};

}  // namespace test_runner

#endif  // COMPONENTS_TEST_RUNNER_WEB_TASK_H_