Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / test_runner / web_task.h
blob6261405f1023d5ba579d64f2c1d06cb07941d920
1 // Copyright 2013 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.
5 #ifndef COMPONENTS_TEST_RUNNER_WEB_TASK_H_
6 #define COMPONENTS_TEST_RUNNER_WEB_TASK_H_
8 #include <vector>
10 #include "base/macros.h"
12 namespace test_runner {
14 class WebTaskList;
16 // WebTask represents a task which can run by WebTestDelegate::postTask() or
17 // WebTestDelegate::postDelayedTask().
18 class WebTask {
19 public:
20 explicit WebTask(WebTaskList*);
21 virtual ~WebTask();
23 // The main code of this task.
24 // An implementation of run() should return immediately if cancel() was
25 // called.
26 virtual void run() = 0;
27 virtual void cancel() = 0;
29 protected:
30 WebTaskList* task_list_;
33 class WebTaskList {
34 public:
35 WebTaskList();
36 ~WebTaskList();
37 void RegisterTask(WebTask*);
38 void UnregisterTask(WebTask*);
39 void RevokeAll();
41 private:
42 std::vector<WebTask*> tasks_;
44 DISALLOW_COPY_AND_ASSIGN(WebTaskList);
47 // A task containing an object pointer of class T. Derived classes should
48 // override RunIfValid() which in turn can safely invoke methods on the
49 // object_. The Class T must have "WebTaskList* mutable_task_list()".
50 template <class T>
51 class WebMethodTask : public WebTask {
52 public:
53 explicit WebMethodTask(T* object)
54 : WebTask(object->mutable_task_list()), object_(object) {}
56 virtual ~WebMethodTask() {}
58 void run() override {
59 if (object_)
60 RunIfValid();
63 void cancel() override {
64 object_ = 0;
65 task_list_->UnregisterTask(this);
66 task_list_ = 0;
69 virtual void RunIfValid() = 0;
71 protected:
72 T* object_;
75 } // namespace test_runner
77 #endif // COMPONENTS_TEST_RUNNER_WEB_TASK_H_