abhishek.a21 | ab71acb | 2014-09-12 16:46:31 | [diff] [blame] | 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. |
| 4 | |
jochen | 73e711c | 2015-06-03 10:01:46 | [diff] [blame] | 5 | #ifndef COMPONENTS_TEST_RUNNER_WEB_TASK_H_ |
| 6 | #define COMPONENTS_TEST_RUNNER_WEB_TASK_H_ |
abhishek.a21 | ab71acb | 2014-09-12 16:46:31 | [diff] [blame] | 7 | |
| 8 | #include <vector> |
| 9 | |
| 10 | #include "base/macros.h" |
| 11 | |
jochen | f5f3175 | 2015-06-03 12:06:34 | [diff] [blame^] | 12 | namespace test_runner { |
abhishek.a21 | ab71acb | 2014-09-12 16:46:31 | [diff] [blame] | 13 | |
| 14 | class WebTaskList; |
| 15 | |
| 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(); |
| 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 | |
| 33 | class 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()". |
| 50 | template <class T> |
| 51 | class WebMethodTask : public WebTask { |
| 52 | public: |
| 53 | explicit WebMethodTask(T* object) |
| 54 | : WebTask(object->mutable_task_list()), object_(object) {} |
| 55 | |
| 56 | virtual ~WebMethodTask() {} |
| 57 | |
dmichael | c1a4941 | 2014-12-18 17:35:42 | [diff] [blame] | 58 | void run() override { |
abhishek.a21 | ab71acb | 2014-09-12 16:46:31 | [diff] [blame] | 59 | if (object_) |
| 60 | RunIfValid(); |
| 61 | } |
| 62 | |
dmichael | c1a4941 | 2014-12-18 17:35:42 | [diff] [blame] | 63 | void cancel() override { |
abhishek.a21 | ab71acb | 2014-09-12 16:46:31 | [diff] [blame] | 64 | 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 | |
jochen | f5f3175 | 2015-06-03 12:06:34 | [diff] [blame^] | 75 | } // namespace test_runner |
abhishek.a21 | ab71acb | 2014-09-12 16:46:31 | [diff] [blame] | 76 | |
jochen | 73e711c | 2015-06-03 10:01:46 | [diff] [blame] | 77 | #endif // COMPONENTS_TEST_RUNNER_WEB_TASK_H_ |