blob: b3f87b8814d957b30cc9bc66bad15ef1e7f60e72 [file] [log] [blame]
[email protected]5a8db802010-10-06 17:34:431// 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"
15class Task;
16namespace 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.
24class 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);
39 private:
40 void DeleteAll();
41 inline Task* PopTask();
42 inline void ExecuteQueuedTasks();
43 void ExecuteDelayedTasks();
44 void RunTask(Task* task);
45
46 struct DelayedTask {
47 DelayedTask(Task* task, base::Time at) : run_at(at), task(task), seq(0) {}
48 base::Time run_at;
49 Task* task;
50 int seq;
51 // To support sorting based on time in priority_queue.
52 bool operator<(const DelayedTask& other) const;
53 };
54
55 std::priority_queue<DelayedTask> delayed_tasks_;
56 std::queue<Task*> pending_tasks_;
57 Lock lock_;
58 HWND wnd_;
59 UINT msg_;
60 int invoke_task_;
61};
62
63#endif // CHROME_FRAME_TASK_MARSHALLER_H_