[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CHROME_FRAME_TASK_MARSHALLER_H_ |
| 6 | #define CHROME_FRAME_TASK_MARSHALLER_H_ |
| 7 | #pragma once |
| 8 | |
| 9 | #include <windows.h> |
| 10 | #include <deque> |
| 11 | #include <queue> |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 12 | |
| 13 | #include "base/synchronization/lock.h" |
[email protected] | c917750 | 2011-01-01 04:48:49 | [diff] [blame] | 14 | #include "base/threading/non_thread_safe.h" |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 15 | #include "base/time.h" |
[email protected] | c917750 | 2011-01-01 04:48:49 | [diff] [blame] | 16 | |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 17 | class Task; |
| 18 | namespace tracked_objects { |
| 19 | class Location; |
| 20 | } |
| 21 | |
| 22 | // TaskMarshallerThroughMessageQueue is similar to base::MessageLoopForUI |
| 23 | // 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 |
| 25 | // the task queue is not empty. |
[email protected] | c917750 | 2011-01-01 04:48:49 | [diff] [blame] | 26 | class TaskMarshallerThroughMessageQueue : public base::NonThreadSafe { |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 27 | public: |
| 28 | TaskMarshallerThroughMessageQueue(); |
| 29 | ~TaskMarshallerThroughMessageQueue(); |
| 30 | |
| 31 | void SetWindow(HWND wnd, UINT msg) { |
| 32 | wnd_ = wnd; |
| 33 | msg_ = msg; |
| 34 | } |
| 35 | |
| 36 | virtual void PostTask(const tracked_objects::Location& from_here, |
| 37 | Task* task); |
| 38 | virtual void PostDelayedTask(const tracked_objects::Location& source, |
| 39 | Task* task, |
| 40 | base::TimeDelta& delay); |
[email protected] | 11b6e60 | 2010-10-13 16:02:26 | [diff] [blame] | 41 | // Called by the owner of the HWND. |
| 42 | BOOL ProcessWindowMessage(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, |
| 43 | LRESULT& lResult, DWORD dwMsgMapID = 0); |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 44 | private: |
| 45 | void DeleteAll(); |
| 46 | inline Task* PopTask(); |
| 47 | inline void ExecuteQueuedTasks(); |
| 48 | void ExecuteDelayedTasks(); |
| 49 | void RunTask(Task* task); |
| 50 | |
| 51 | struct DelayedTask { |
| 52 | DelayedTask(Task* task, base::Time at) : run_at(at), task(task), seq(0) {} |
| 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 | |
| 60 | std::priority_queue<DelayedTask> delayed_tasks_; |
| 61 | std::queue<Task*> pending_tasks_; |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 62 | base::Lock lock_; |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 63 | HWND wnd_; |
| 64 | UINT msg_; |
| 65 | int invoke_task_; |
| 66 | }; |
| 67 | |
| 68 | #endif // CHROME_FRAME_TASK_MARSHALLER_H_ |