| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_TEST_RUNNER_WEB_TASK_H_ | 5 #ifndef COMPONENTS_TEST_RUNNER_WEB_TASK_H_ |
| 6 #define COMPONENTS_TEST_RUNNER_WEB_TASK_H_ | 6 #define COMPONENTS_TEST_RUNNER_WEB_TASK_H_ |
| 7 | 7 |
| 8 #include <vector> | 8 #include "base/callback.h" |
| 9 | |
| 10 #include "base/macros.h" | 9 #include "base/macros.h" |
| 11 #include "third_party/WebKit/public/platform/WebTaskRunner.h" | 10 #include "third_party/WebKit/public/platform/WebTaskRunner.h" |
| 12 | 11 |
| 13 namespace test_runner { | 12 namespace test_runner { |
| 14 | 13 |
| 15 class WebTaskList; | 14 // blink::WebTaskRunner::Task that wraps a base::Closure. |
| 15 class WebCallbackTask : public blink::WebTaskRunner::Task { |
| 16 public: |
| 17 WebCallbackTask(const base::Closure& callback); |
| 18 ~WebCallbackTask() override; |
| 16 | 19 |
| 17 // WebTask represents a task which can run by WebTestDelegate::postTask() or | 20 void run() override; |
| 18 // WebTestDelegate::postDelayedTask(). | |
| 19 class WebTask : public blink::WebTaskRunner::Task { | |
| 20 public: | |
| 21 explicit WebTask(WebTaskList*); | |
| 22 ~WebTask() override; | |
| 23 | |
| 24 // The main code of this task. | |
| 25 // An implementation of run() should return immediately if cancel() was | |
| 26 // called. | |
| 27 void run() override = 0; | |
| 28 virtual void cancel() = 0; | |
| 29 | |
| 30 protected: | |
| 31 WebTaskList* task_list_; | |
| 32 }; | |
| 33 | |
| 34 class WebTaskList { | |
| 35 public: | |
| 36 WebTaskList(); | |
| 37 ~WebTaskList(); | |
| 38 void RegisterTask(WebTask*); | |
| 39 void UnregisterTask(WebTask*); | |
| 40 void RevokeAll(); | |
| 41 | 21 |
| 42 private: | 22 private: |
| 43 std::vector<WebTask*> tasks_; | 23 base::Closure callback_; |
| 44 | 24 |
| 45 DISALLOW_COPY_AND_ASSIGN(WebTaskList); | 25 DISALLOW_COPY_AND_ASSIGN(WebCallbackTask); |
| 46 }; | |
| 47 | |
| 48 // A task containing an object pointer of class T. Derived classes should | |
| 49 // override RunIfValid() which in turn can safely invoke methods on the | |
| 50 // object_. The Class T must have "WebTaskList* mutable_task_list()". | |
| 51 template <class T> | |
| 52 class WebMethodTask : public WebTask { | |
| 53 public: | |
| 54 explicit WebMethodTask(T* object) | |
| 55 : WebTask(object->mutable_task_list()), object_(object) {} | |
| 56 | |
| 57 virtual ~WebMethodTask() {} | |
| 58 | |
| 59 void run() override { | |
| 60 if (object_) | |
| 61 RunIfValid(); | |
| 62 } | |
| 63 | |
| 64 void cancel() override { | |
| 65 object_ = 0; | |
| 66 task_list_->UnregisterTask(this); | |
| 67 task_list_ = 0; | |
| 68 } | |
| 69 | |
| 70 virtual void RunIfValid() = 0; | |
| 71 | |
| 72 protected: | |
| 73 T* object_; | |
| 74 }; | 26 }; |
| 75 | 27 |
| 76 } // namespace test_runner | 28 } // namespace test_runner |
| 77 | 29 |
| 78 #endif // COMPONENTS_TEST_RUNNER_WEB_TASK_H_ | 30 #endif // COMPONENTS_TEST_RUNNER_WEB_TASK_H_ |
| OLD | NEW |