summaryrefslogtreecommitdiffstats
path: root/chrome_frame/task_marshaller.h
blob: 75d945e0c597c76ba5110a751b41867f9b8ea6fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_FRAME_TASK_MARSHALLER_H_
#define CHROME_FRAME_TASK_MARSHALLER_H_
#pragma once

#include <windows.h>
#include <deque>
#include <queue>

#include "base/synchronization/lock.h"
#include "base/threading/non_thread_safe.h"
#include "base/time.h"

class Task;
namespace tracked_objects {
  class Location;
}

// TaskMarshallerThroughMessageQueue is similar to base::MessageLoopForUI
// in cases where we do not control the thread lifetime and message retrieval
// and dispatching. It uses a HWND to ::PostMessage to it as a signal that
// the task queue is not empty.
class TaskMarshallerThroughMessageQueue : public base::NonThreadSafe {
 public:
  TaskMarshallerThroughMessageQueue();
  ~TaskMarshallerThroughMessageQueue();

  void SetWindow(HWND wnd, UINT msg) {
    wnd_ = wnd;
    msg_ = msg;
  }

  virtual void PostTask(const tracked_objects::Location& from_here,
                        Task* task);
  virtual void PostDelayedTask(const tracked_objects::Location& source,
                               Task* task,
                               base::TimeDelta& delay);
  // Called by the owner of the HWND.
  BOOL ProcessWindowMessage(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
                            LRESULT& lResult, DWORD dwMsgMapID = 0);
 private:
  void DeleteAll();
  inline Task* PopTask();
  inline void ExecuteQueuedTasks();
  void ExecuteDelayedTasks();
  void RunTask(Task* task);

  struct DelayedTask {
    DelayedTask(Task* task, base::Time at) : run_at(at), task(task), seq(0) {}
    base::Time run_at;
    Task* task;
    int seq;
    // To support sorting based on time in priority_queue.
    bool operator<(const DelayedTask& other) const;
  };

  std::priority_queue<DelayedTask> delayed_tasks_;
  std::queue<Task*> pending_tasks_;
  base::Lock lock_;
  HWND wnd_;
  UINT msg_;
  int invoke_task_;
};

#endif  // CHROME_FRAME_TASK_MARSHALLER_H_