[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> |
| 12 | #include "base/lock.h" |
| 13 | #include "base/non_thread_safe.h" |
| 14 | #include "base/time.h" |
| 15 | class Task; |
| 16 | namespace tracked_objects { |
| 17 | class Location; |
| 18 | } |
| 19 | |
| 20 | // TaskMarshallerThroughMessageQueue is similar to base::MessageLoopForUI |
| 21 | // in cases where we do not control the thread lifetime and message retrieval |
| 22 | // and dispatching. It uses a HWND to ::PostMessage to it as a signal that |
| 23 | // the task queue is not empty. |
| 24 | class TaskMarshallerThroughMessageQueue : public NonThreadSafe { |
| 25 | public: |
| 26 | TaskMarshallerThroughMessageQueue(); |
| 27 | ~TaskMarshallerThroughMessageQueue(); |
| 28 | |
| 29 | void SetWindow(HWND wnd, UINT msg) { |
| 30 | wnd_ = wnd; |
| 31 | msg_ = msg; |
| 32 | } |
| 33 | |
| 34 | virtual void PostTask(const tracked_objects::Location& from_here, |
| 35 | Task* task); |
| 36 | virtual void PostDelayedTask(const tracked_objects::Location& source, |
| 37 | Task* task, |
| 38 | base::TimeDelta& delay); |
[email protected] | 11b6e60 | 2010-10-13 16:02:26 | [diff] [blame^] | 39 | // Called by the owner of the HWND. |
| 40 | BOOL ProcessWindowMessage(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, |
| 41 | LRESULT& lResult, DWORD dwMsgMapID = 0); |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 42 | private: |
| 43 | void DeleteAll(); |
| 44 | inline Task* PopTask(); |
| 45 | inline void ExecuteQueuedTasks(); |
| 46 | void ExecuteDelayedTasks(); |
| 47 | void RunTask(Task* task); |
| 48 | |
| 49 | struct DelayedTask { |
| 50 | DelayedTask(Task* task, base::Time at) : run_at(at), task(task), seq(0) {} |
| 51 | base::Time run_at; |
| 52 | Task* task; |
| 53 | int seq; |
| 54 | // To support sorting based on time in priority_queue. |
| 55 | bool operator<(const DelayedTask& other) const; |
| 56 | }; |
| 57 | |
| 58 | std::priority_queue<DelayedTask> delayed_tasks_; |
| 59 | std::queue<Task*> pending_tasks_; |
| 60 | Lock lock_; |
| 61 | HWND wnd_; |
| 62 | UINT msg_; |
| 63 | int invoke_task_; |
| 64 | }; |
| 65 | |
| 66 | #endif // CHROME_FRAME_TASK_MARSHALLER_H_ |