| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 CHROME_FRAME_TASK_MARSHALLER_H_ | 5 #ifndef CHROME_FRAME_TASK_MARSHALLER_H_ |
| 6 #define CHROME_FRAME_TASK_MARSHALLER_H_ | 6 #define CHROME_FRAME_TASK_MARSHALLER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <windows.h> | 9 #include <windows.h> |
| 10 #include <deque> | 10 #include <deque> |
| 11 #include <queue> | 11 #include <queue> |
| 12 | 12 |
| 13 #include "base/callback.h" |
| 14 #include "base/pending_task.h" |
| 13 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
| 14 #include "base/threading/non_thread_safe.h" | 16 #include "base/threading/non_thread_safe.h" |
| 15 #include "base/time.h" | 17 #include "base/time.h" |
| 16 | 18 |
| 17 class Task; | 19 class Task; |
| 18 namespace tracked_objects { | 20 namespace tracked_objects { |
| 19 class Location; | 21 class Location; |
| 20 } | 22 } |
| 21 | 23 |
| 22 // TaskMarshallerThroughMessageQueue is similar to base::MessageLoopForUI | 24 // TaskMarshallerThroughMessageQueue is similar to base::MessageLoopForUI |
| 23 // in cases where we do not control the thread lifetime and message retrieval | 25 // in cases where we do not control the thread lifetime and message retrieval |
| 24 // and dispatching. It uses a HWND to ::PostMessage to it as a signal that | 26 // and dispatching. It uses a HWND to ::PostMessage to it as a signal that |
| 25 // the task queue is not empty. | 27 // the task queue is not empty. |
| 26 class TaskMarshallerThroughMessageQueue : public base::NonThreadSafe { | 28 class TaskMarshallerThroughMessageQueue : public base::NonThreadSafe { |
| 27 public: | 29 public: |
| 28 TaskMarshallerThroughMessageQueue(); | 30 TaskMarshallerThroughMessageQueue(); |
| 29 ~TaskMarshallerThroughMessageQueue(); | 31 virtual ~TaskMarshallerThroughMessageQueue(); |
| 30 | 32 |
| 31 void SetWindow(HWND wnd, UINT msg) { | 33 void SetWindow(HWND wnd, UINT msg) { |
| 32 wnd_ = wnd; | 34 wnd_ = wnd; |
| 33 msg_ = msg; | 35 msg_ = msg; |
| 34 } | 36 } |
| 35 | 37 |
| 36 virtual void PostTask(const tracked_objects::Location& from_here, | 38 virtual void PostTask(const tracked_objects::Location& from_here, |
| 37 Task* task); | 39 const base::Closure& task); |
| 38 virtual void PostDelayedTask(const tracked_objects::Location& source, | 40 virtual void PostDelayedTask(const tracked_objects::Location& source, |
| 39 Task* task, | 41 const base::Closure& task, |
| 40 base::TimeDelta& delay); | 42 base::TimeDelta& delay); |
| 43 |
| 41 // Called by the owner of the HWND. | 44 // Called by the owner of the HWND. |
| 42 BOOL ProcessWindowMessage(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, | 45 BOOL ProcessWindowMessage(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, |
| 43 LRESULT& lResult, DWORD dwMsgMapID = 0); | 46 LRESULT& lResult, DWORD dwMsgMapID = 0); |
| 44 private: | 47 private: |
| 45 void DeleteAll(); | 48 void ClearTasks(); |
| 46 inline Task* PopTask(); | 49 inline base::Closure PopTask(); |
| 47 inline void ExecuteQueuedTasks(); | 50 inline void ExecuteQueuedTasks(); |
| 48 void ExecuteDelayedTasks(); | 51 void ExecuteDelayedTasks(); |
| 49 void RunTask(Task* task); | |
| 50 | 52 |
| 51 struct DelayedTask { | 53 // Shortest delays ordered at the top of the queue. |
| 52 DelayedTask(Task* task, base::Time at) : run_at(at), task(task), seq(0) {} | 54 base::DelayedTaskQueue delayed_tasks_; |
| 53 base::Time run_at; | |
| 54 Task* task; | |
| 55 int seq; | |
| 56 // To support sorting based on time in priority_queue. | |
| 57 bool operator<(const DelayedTask& other) const; | |
| 58 }; | |
| 59 | 55 |
| 60 std::priority_queue<DelayedTask> delayed_tasks_; | 56 // A list of tasks that need to be processed by this instance. |
| 61 std::queue<Task*> pending_tasks_; | 57 std::queue<base::Closure> pending_tasks_; |
| 58 |
| 59 // Lock accesses to |pending_tasks_|. |
| 62 base::Lock lock_; | 60 base::Lock lock_; |
| 61 |
| 62 // ::PostMessage parameters. |
| 63 HWND wnd_; | 63 HWND wnd_; |
| 64 UINT msg_; | 64 UINT msg_; |
| 65 int invoke_task_; | |
| 66 }; | 65 }; |
| 67 | 66 |
| 68 #endif // CHROME_FRAME_TASK_MARSHALLER_H_ | 67 #endif // CHROME_FRAME_TASK_MARSHALLER_H_ |
| OLD | NEW |