Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #include "base/message_pump_default.h" |
michael@0 | 6 | |
michael@0 | 7 | #include "base/logging.h" |
michael@0 | 8 | #include "base/message_loop.h" |
michael@0 | 9 | #include "base/scoped_nsautorelease_pool.h" |
michael@0 | 10 | #include "GeckoProfiler.h" |
michael@0 | 11 | |
michael@0 | 12 | #include "mozilla/BackgroundHangMonitor.h" |
michael@0 | 13 | |
michael@0 | 14 | namespace base { |
michael@0 | 15 | |
michael@0 | 16 | MessagePumpDefault::MessagePumpDefault() |
michael@0 | 17 | : keep_running_(true), |
michael@0 | 18 | event_(false, false) { |
michael@0 | 19 | } |
michael@0 | 20 | |
michael@0 | 21 | void MessagePumpDefault::Run(Delegate* delegate) { |
michael@0 | 22 | DCHECK(keep_running_) << "Quit must have been called outside of Run!"; |
michael@0 | 23 | |
michael@0 | 24 | const MessageLoop* const loop = MessageLoop::current(); |
michael@0 | 25 | mozilla::BackgroundHangMonitor hangMonitor( |
michael@0 | 26 | loop->thread_name().c_str(), |
michael@0 | 27 | loop->transient_hang_timeout(), |
michael@0 | 28 | loop->permanent_hang_timeout()); |
michael@0 | 29 | |
michael@0 | 30 | for (;;) { |
michael@0 | 31 | ScopedNSAutoreleasePool autorelease_pool; |
michael@0 | 32 | |
michael@0 | 33 | hangMonitor.NotifyActivity(); |
michael@0 | 34 | bool did_work = delegate->DoWork(); |
michael@0 | 35 | if (!keep_running_) |
michael@0 | 36 | break; |
michael@0 | 37 | |
michael@0 | 38 | hangMonitor.NotifyActivity(); |
michael@0 | 39 | did_work |= delegate->DoDelayedWork(&delayed_work_time_); |
michael@0 | 40 | if (!keep_running_) |
michael@0 | 41 | break; |
michael@0 | 42 | |
michael@0 | 43 | if (did_work) |
michael@0 | 44 | continue; |
michael@0 | 45 | |
michael@0 | 46 | hangMonitor.NotifyActivity(); |
michael@0 | 47 | did_work = delegate->DoIdleWork(); |
michael@0 | 48 | if (!keep_running_) |
michael@0 | 49 | break; |
michael@0 | 50 | |
michael@0 | 51 | if (did_work) |
michael@0 | 52 | continue; |
michael@0 | 53 | |
michael@0 | 54 | if (delayed_work_time_.is_null()) { |
michael@0 | 55 | hangMonitor.NotifyWait(); |
michael@0 | 56 | PROFILER_LABEL("MessagePump", "Wait"); |
michael@0 | 57 | { |
michael@0 | 58 | GeckoProfilerSleepRAII profiler_sleep; |
michael@0 | 59 | event_.Wait(); |
michael@0 | 60 | } |
michael@0 | 61 | } else { |
michael@0 | 62 | TimeDelta delay = delayed_work_time_ - TimeTicks::Now(); |
michael@0 | 63 | if (delay > TimeDelta()) { |
michael@0 | 64 | hangMonitor.NotifyWait(); |
michael@0 | 65 | PROFILER_LABEL("MessagePump", "Wait"); |
michael@0 | 66 | { |
michael@0 | 67 | GeckoProfilerSleepRAII profiler_sleep; |
michael@0 | 68 | event_.TimedWait(delay); |
michael@0 | 69 | } |
michael@0 | 70 | } else { |
michael@0 | 71 | // It looks like delayed_work_time_ indicates a time in the past, so we |
michael@0 | 72 | // need to call DoDelayedWork now. |
michael@0 | 73 | delayed_work_time_ = TimeTicks(); |
michael@0 | 74 | } |
michael@0 | 75 | } |
michael@0 | 76 | // Since event_ is auto-reset, we don't need to do anything special here |
michael@0 | 77 | // other than service each delegate method. |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | keep_running_ = true; |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | void MessagePumpDefault::Quit() { |
michael@0 | 84 | keep_running_ = false; |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | void MessagePumpDefault::ScheduleWork() { |
michael@0 | 88 | // Since this can be called on any thread, we need to ensure that our Run |
michael@0 | 89 | // loop wakes up. |
michael@0 | 90 | event_.Signal(); |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | void MessagePumpDefault::ScheduleDelayedWork( |
michael@0 | 94 | const TimeTicks& delayed_work_time) { |
michael@0 | 95 | // We know that we can't be blocked on Wait right now since this method can |
michael@0 | 96 | // only be called on the same thread as Run, so we only need to update our |
michael@0 | 97 | // record of how long to sleep when we do sleep. |
michael@0 | 98 | delayed_work_time_ = delayed_work_time; |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | } // namespace base |