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