[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
[email protected] | 2b9a9f16 | 2010-10-19 20:30:45 | [diff] [blame] | 4 | |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 5 | #include "chrome_frame/task_marshaller.h" |
[email protected] | c694427 | 2012-01-06 22:12:28 | [diff] [blame] | 6 | |
| 7 | #include "base/callback.h" |
| 8 | #include "base/logging.h" |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 9 | |
[email protected] | 262060ff | 2011-11-17 23:26:53 | [diff] [blame] | 10 | TaskMarshallerThroughMessageQueue::TaskMarshallerThroughMessageQueue() |
| 11 | : wnd_(NULL), |
| 12 | msg_(0xFFFF) { |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 13 | } |
| 14 | |
| 15 | TaskMarshallerThroughMessageQueue::~TaskMarshallerThroughMessageQueue() { |
[email protected] | 262060ff | 2011-11-17 23:26:53 | [diff] [blame] | 16 | ClearTasks(); |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | void TaskMarshallerThroughMessageQueue::PostTask( |
[email protected] | 262060ff | 2011-11-17 23:26:53 | [diff] [blame] | 20 | const tracked_objects::Location& from_here, const base::Closure& task) { |
[email protected] | 11b6e60 | 2010-10-13 16:02:26 | [diff] [blame] | 21 | DCHECK(wnd_ != NULL); |
[email protected] | bb04c4e | 2011-11-18 20:44:22 | [diff] [blame] | 22 | |
[email protected] | 11b6e60 | 2010-10-13 16:02:26 | [diff] [blame] | 23 | lock_.Acquire(); |
| 24 | bool has_work = !pending_tasks_.empty(); |
| 25 | pending_tasks_.push(task); |
| 26 | lock_.Release(); |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 27 | |
[email protected] | 11b6e60 | 2010-10-13 16:02:26 | [diff] [blame] | 28 | // Don't post message if there is already one. |
| 29 | if (has_work) |
| 30 | return; |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 31 | |
[email protected] | 11b6e60 | 2010-10-13 16:02:26 | [diff] [blame] | 32 | if (!::PostMessage(wnd_, msg_, 0, 0)) { |
[email protected] | 2b9a9f16 | 2010-10-19 20:30:45 | [diff] [blame] | 33 | DVLOG(1) << "Dropping MSG_EXECUTE_TASK message for destroyed window."; |
[email protected] | 262060ff | 2011-11-17 23:26:53 | [diff] [blame] | 34 | ClearTasks(); |
[email protected] | 11b6e60 | 2010-10-13 16:02:26 | [diff] [blame] | 35 | } |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | void TaskMarshallerThroughMessageQueue::PostDelayedTask( |
[email protected] | 2b9a9f16 | 2010-10-19 20:30:45 | [diff] [blame] | 39 | const tracked_objects::Location& source, |
[email protected] | 262060ff | 2011-11-17 23:26:53 | [diff] [blame] | 40 | const base::Closure& task, |
[email protected] | 11b6e60 | 2010-10-13 16:02:26 | [diff] [blame] | 41 | base::TimeDelta& delay) { |
[email protected] | 262060ff | 2011-11-17 23:26:53 | [diff] [blame] | 42 | DCHECK(wnd_); |
| 43 | |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 44 | base::AutoLock lock(lock_); |
[email protected] | 262060ff | 2011-11-17 23:26:53 | [diff] [blame] | 45 | base::PendingTask delayed_task(source, task, base::TimeTicks::Now() + delay, |
| 46 | true); |
| 47 | base::TimeTicks top_run_time = delayed_tasks_.top().delayed_run_time; |
[email protected] | 11b6e60 | 2010-10-13 16:02:26 | [diff] [blame] | 48 | delayed_tasks_.push(delayed_task); |
[email protected] | 262060ff | 2011-11-17 23:26:53 | [diff] [blame] | 49 | |
| 50 | // Reschedule the timer if |delayed_task| will be the next delayed task to |
| 51 | // run. |
| 52 | if (delayed_task.delayed_run_time < top_run_time) { |
[email protected] | 11b6e60 | 2010-10-13 16:02:26 | [diff] [blame] | 53 | ::SetTimer(wnd_, reinterpret_cast<UINT_PTR>(this), |
[email protected] | 262060ff | 2011-11-17 23:26:53 | [diff] [blame] | 54 | static_cast<DWORD>(delay.InMilliseconds()), NULL); |
[email protected] | 11b6e60 | 2010-10-13 16:02:26 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
| 58 | BOOL TaskMarshallerThroughMessageQueue::ProcessWindowMessage(HWND hWnd, |
[email protected] | 2b9a9f16 | 2010-10-19 20:30:45 | [diff] [blame] | 59 | UINT uMsg, |
| 60 | WPARAM wParam, |
| 61 | LPARAM lParam, |
| 62 | LRESULT& lResult, |
| 63 | DWORD dwMsgMapID) { |
[email protected] | 11b6e60 | 2010-10-13 16:02:26 | [diff] [blame] | 64 | if (hWnd == wnd_ && uMsg == msg_) { |
| 65 | ExecuteQueuedTasks(); |
| 66 | lResult = 0; |
| 67 | return TRUE; |
| 68 | } |
| 69 | |
| 70 | if (hWnd == wnd_ && uMsg == WM_TIMER) { |
| 71 | ExecuteDelayedTasks(); |
| 72 | lResult = 0; |
| 73 | return TRUE; |
| 74 | } |
| 75 | |
| 76 | return FALSE; |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 77 | } |
| 78 | |
[email protected] | 262060ff | 2011-11-17 23:26:53 | [diff] [blame] | 79 | base::Closure TaskMarshallerThroughMessageQueue::PopTask() { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 80 | base::AutoLock lock(lock_); |
[email protected] | 262060ff | 2011-11-17 23:26:53 | [diff] [blame] | 81 | if (pending_tasks_.empty()) |
| 82 | return base::Closure(); |
| 83 | |
| 84 | base::Closure task = pending_tasks_.front(); |
| 85 | pending_tasks_.pop(); |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 86 | return task; |
| 87 | } |
| 88 | |
| 89 | void TaskMarshallerThroughMessageQueue::ExecuteQueuedTasks() { |
| 90 | DCHECK(CalledOnValidThread()); |
[email protected] | 262060ff | 2011-11-17 23:26:53 | [diff] [blame] | 91 | base::Closure task; |
| 92 | while (!(task = PopTask()).is_null()) |
| 93 | task.Run(); |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void TaskMarshallerThroughMessageQueue::ExecuteDelayedTasks() { |
| 97 | DCHECK(CalledOnValidThread()); |
| 98 | ::KillTimer(wnd_, reinterpret_cast<UINT_PTR>(this)); |
[email protected] | 262060ff | 2011-11-17 23:26:53 | [diff] [blame] | 99 | while (true) { |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 100 | lock_.Acquire(); |
| 101 | |
| 102 | if (delayed_tasks_.empty()) { |
| 103 | lock_.Release(); |
| 104 | return; |
| 105 | } |
| 106 | |
[email protected] | 262060ff | 2011-11-17 23:26:53 | [diff] [blame] | 107 | base::PendingTask next_task = delayed_tasks_.top(); |
| 108 | base::TimeTicks now = base::TimeTicks::Now(); |
| 109 | base::TimeTicks next_run = next_task.delayed_run_time; |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 110 | if (next_run > now) { |
| 111 | int64 delay = (next_run - now).InMillisecondsRoundedUp(); |
| 112 | ::SetTimer(wnd_, reinterpret_cast<UINT_PTR>(this), |
[email protected] | 262060ff | 2011-11-17 23:26:53 | [diff] [blame] | 113 | static_cast<DWORD>(delay), NULL); |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 114 | lock_.Release(); |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | delayed_tasks_.pop(); |
| 119 | lock_.Release(); |
| 120 | |
| 121 | // Run the task outside the lock. |
[email protected] | 262060ff | 2011-11-17 23:26:53 | [diff] [blame] | 122 | next_task.task.Run(); |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
[email protected] | 262060ff | 2011-11-17 23:26:53 | [diff] [blame] | 126 | void TaskMarshallerThroughMessageQueue::ClearTasks() { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 127 | base::AutoLock lock(lock_); |
[email protected] | 5044da8 | 2010-10-27 01:09:16 | [diff] [blame] | 128 | DVLOG_IF(1, !pending_tasks_.empty()) << "Destroying " |
| 129 | << pending_tasks_.size() |
| 130 | << " pending tasks."; |
[email protected] | 262060ff | 2011-11-17 23:26:53 | [diff] [blame] | 131 | while (!pending_tasks_.empty()) |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 132 | pending_tasks_.pop(); |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 133 | |
[email protected] | 262060ff | 2011-11-17 23:26:53 | [diff] [blame] | 134 | while (!delayed_tasks_.empty()) |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 135 | delayed_tasks_.pop(); |
[email protected] | 5a8db80 | 2010-10-06 17:34:43 | [diff] [blame] | 136 | } |