ipc/chromium/src/base/message_pump_android.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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) 2010 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_android.h"
michael@0 6
michael@0 7 #include <fcntl.h>
michael@0 8 #include <math.h>
michael@0 9
michael@0 10 #include "base/eintr_wrapper.h"
michael@0 11 #include "base/lazy_instance.h"
michael@0 12 #include "base/logging.h"
michael@0 13 #include "base/platform_thread.h"
michael@0 14
michael@0 15 namespace mozilla {
michael@0 16 bool ProcessNextEvent();
michael@0 17 void NotifyEvent();
michael@0 18 }
michael@0 19
michael@0 20 namespace base {
michael@0 21
michael@0 22 MessagePumpForUI::MessagePumpForUI()
michael@0 23 : state_(NULL)
michael@0 24 , pump(*this)
michael@0 25 {
michael@0 26 }
michael@0 27
michael@0 28 MessagePumpForUI::~MessagePumpForUI() {
michael@0 29 }
michael@0 30
michael@0 31 MessagePumpAndroid::MessagePumpAndroid(MessagePumpForUI &aPump)
michael@0 32 : pump(aPump)
michael@0 33 {
michael@0 34 }
michael@0 35
michael@0 36 MessagePumpAndroid::~MessagePumpAndroid()
michael@0 37 {
michael@0 38 }
michael@0 39
michael@0 40 void MessagePumpForUI::Run(Delegate* delegate) {
michael@0 41 RunState state;
michael@0 42 state.delegate = delegate;
michael@0 43 state.should_quit = false;
michael@0 44 state.run_depth = state_ ? state_->run_depth + 1 : 1;
michael@0 45 // We really only do a single task for each iteration of the loop. If we
michael@0 46 // have done something, assume there is likely something more to do. This
michael@0 47 // will mean that we don't block on the message pump until there was nothing
michael@0 48 // more to do. We also set this to true to make sure not to block on the
michael@0 49 // first iteration of the loop, so RunAllPending() works correctly.
michael@0 50 state.more_work_is_plausible = true;
michael@0 51
michael@0 52 RunState* previous_state = state_;
michael@0 53 state_ = &state;
michael@0 54
michael@0 55 // We run our own loop instead of using g_main_loop_quit in one of the
michael@0 56 // callbacks. This is so we only quit our own loops, and we don't quit
michael@0 57 // nested loops run by others. TODO(deanm): Is this what we want?
michael@0 58
michael@0 59 while (!state_->should_quit) {
michael@0 60 mozilla::ProcessNextEvent();
michael@0 61 if (work_scheduled) {
michael@0 62 work_scheduled = false;
michael@0 63 HandleDispatch();
michael@0 64 }
michael@0 65 }
michael@0 66
michael@0 67 state_ = previous_state;
michael@0 68 }
michael@0 69
michael@0 70 void MessagePumpForUI::HandleDispatch() {
michael@0 71 // We should only ever have a single message on the wakeup pipe, since we
michael@0 72 // are only signaled when the queue went from empty to non-empty. The qApp
michael@0 73 // poll will tell us whether there was data, so this read shouldn't block.
michael@0 74 if (state_->should_quit)
michael@0 75 return;
michael@0 76
michael@0 77 state_->more_work_is_plausible = false;
michael@0 78
michael@0 79 if (state_->delegate->DoWork())
michael@0 80 state_->more_work_is_plausible = true;
michael@0 81
michael@0 82 if (state_->should_quit)
michael@0 83 return;
michael@0 84
michael@0 85 if (state_->delegate->DoDelayedWork(&delayed_work_time_))
michael@0 86 state_->more_work_is_plausible = true;
michael@0 87 if (state_->should_quit)
michael@0 88 return;
michael@0 89
michael@0 90 // Don't do idle work if we think there are more important things
michael@0 91 // that we could be doing.
michael@0 92 if (state_->more_work_is_plausible)
michael@0 93 return;
michael@0 94
michael@0 95 if (state_->delegate->DoIdleWork())
michael@0 96 state_->more_work_is_plausible = true;
michael@0 97 if (state_->should_quit)
michael@0 98 return;
michael@0 99 }
michael@0 100
michael@0 101 void MessagePumpForUI::Quit() {
michael@0 102 if (state_) {
michael@0 103 state_->should_quit = true;
michael@0 104 } else {
michael@0 105 NOTREACHED() << "Quit called outside Run!";
michael@0 106 }
michael@0 107 }
michael@0 108
michael@0 109 void MessagePumpForUI::ScheduleWork() {
michael@0 110 // This can be called on any thread, so we don't want to touch any state
michael@0 111 // variables as we would then need locks all over. This ensures that if
michael@0 112 // we are sleeping in a poll that we will wake up.
michael@0 113 work_scheduled = true;
michael@0 114 mozilla::NotifyEvent();
michael@0 115 }
michael@0 116
michael@0 117 void MessagePumpForUI::ScheduleDelayedWork(const TimeTicks& delayed_work_time) {
michael@0 118 // We need to wake up the loop in case the poll timeout needs to be
michael@0 119 // adjusted. This will cause us to try to do work, but that's ok.
michael@0 120 delayed_work_time_ = delayed_work_time;
michael@0 121 ScheduleWork();
michael@0 122 }
michael@0 123
michael@0 124 } // namespace base

mercurial