blob: 6261405f1023d5ba579d64f2c1d06cb07941d920 [file] [log] [blame]
abhishek.a21ab71acb2014-09-12 16:46:311// 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.
4
jochen73e711c2015-06-03 10:01:465#ifndef COMPONENTS_TEST_RUNNER_WEB_TASK_H_
6#define COMPONENTS_TEST_RUNNER_WEB_TASK_H_
abhishek.a21ab71acb2014-09-12 16:46:317
8#include <vector>
9
10#include "base/macros.h"
11
jochenf5f31752015-06-03 12:06:3412namespace test_runner {
abhishek.a21ab71acb2014-09-12 16:46:3113
14class WebTaskList;
15
16// WebTask represents a task which can run by WebTestDelegate::postTask() or
17// WebTestDelegate::postDelayedTask().
18class WebTask {
19 public:
20 explicit WebTask(WebTaskList*);
21 virtual ~WebTask();
22
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;
28
29 protected:
30 WebTaskList* task_list_;
31};
32
33class WebTaskList {
34 public:
35 WebTaskList();
36 ~WebTaskList();
37 void RegisterTask(WebTask*);
38 void UnregisterTask(WebTask*);
39 void RevokeAll();
40
41 private:
42 std::vector<WebTask*> tasks_;
43
44 DISALLOW_COPY_AND_ASSIGN(WebTaskList);
45};
46
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()".
50template <class T>
51class WebMethodTask : public WebTask {
52 public:
53 explicit WebMethodTask(T* object)
54 : WebTask(object->mutable_task_list()), object_(object) {}
55
56 virtual ~WebMethodTask() {}
57
dmichaelc1a49412014-12-18 17:35:4258 void run() override {
abhishek.a21ab71acb2014-09-12 16:46:3159 if (object_)
60 RunIfValid();
61 }
62
dmichaelc1a49412014-12-18 17:35:4263 void cancel() override {
abhishek.a21ab71acb2014-09-12 16:46:3164 object_ = 0;
65 task_list_->UnregisterTask(this);
66 task_list_ = 0;
67 }
68
69 virtual void RunIfValid() = 0;
70
71 protected:
72 T* object_;
73};
74
jochenf5f31752015-06-03 12:06:3475} // namespace test_runner
abhishek.a21ab71acb2014-09-12 16:46:3176
jochen73e711c2015-06-03 10:01:4677#endif // COMPONENTS_TEST_RUNNER_WEB_TASK_H_