ipc/chromium/src/base/message_pump_android.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/base/message_pump_android.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,124 @@
     1.4 +// Copyright (c) 2010 The Chromium Authors. All rights reserved.
     1.5 +// Use of this source code is governed by a BSD-style license that can be
     1.6 +// found in the LICENSE file.
     1.7 +
     1.8 +#include "base/message_pump_android.h"
     1.9 +
    1.10 +#include <fcntl.h>
    1.11 +#include <math.h>
    1.12 +
    1.13 +#include "base/eintr_wrapper.h"
    1.14 +#include "base/lazy_instance.h"
    1.15 +#include "base/logging.h"
    1.16 +#include "base/platform_thread.h"
    1.17 +
    1.18 +namespace mozilla {
    1.19 +bool ProcessNextEvent();
    1.20 +void NotifyEvent();
    1.21 +}
    1.22 +
    1.23 +namespace base {
    1.24 +
    1.25 +MessagePumpForUI::MessagePumpForUI()
    1.26 +  : state_(NULL)
    1.27 +  , pump(*this)
    1.28 +{
    1.29 +}
    1.30 +
    1.31 +MessagePumpForUI::~MessagePumpForUI() {
    1.32 +}
    1.33 +
    1.34 +MessagePumpAndroid::MessagePumpAndroid(MessagePumpForUI &aPump)
    1.35 +  : pump(aPump)
    1.36 +{
    1.37 +}
    1.38 +
    1.39 +MessagePumpAndroid::~MessagePumpAndroid()
    1.40 +{
    1.41 +}
    1.42 +
    1.43 +void MessagePumpForUI::Run(Delegate* delegate) {
    1.44 +  RunState state;
    1.45 +  state.delegate = delegate;
    1.46 +  state.should_quit = false;
    1.47 +  state.run_depth = state_ ? state_->run_depth + 1 : 1;
    1.48 +  // We really only do a single task for each iteration of the loop.  If we
    1.49 +  // have done something, assume there is likely something more to do.  This
    1.50 +  // will mean that we don't block on the message pump until there was nothing
    1.51 +  // more to do.  We also set this to true to make sure not to block on the
    1.52 +  // first iteration of the loop, so RunAllPending() works correctly.
    1.53 +  state.more_work_is_plausible = true;
    1.54 +
    1.55 +  RunState* previous_state = state_;
    1.56 +  state_ = &state;
    1.57 +
    1.58 +  // We run our own loop instead of using g_main_loop_quit in one of the
    1.59 +  // callbacks.  This is so we only quit our own loops, and we don't quit
    1.60 +  // nested loops run by others.  TODO(deanm): Is this what we want?
    1.61 +
    1.62 +  while (!state_->should_quit) {
    1.63 +    mozilla::ProcessNextEvent();
    1.64 +    if (work_scheduled) {
    1.65 +      work_scheduled = false;
    1.66 +      HandleDispatch();
    1.67 +    }
    1.68 +  }
    1.69 +
    1.70 +  state_ = previous_state;
    1.71 +}
    1.72 +
    1.73 +void MessagePumpForUI::HandleDispatch() {
    1.74 +  // We should only ever have a single message on the wakeup pipe, since we
    1.75 +  // are only signaled when the queue went from empty to non-empty.  The qApp
    1.76 +  // poll will tell us whether there was data, so this read shouldn't block.
    1.77 +  if (state_->should_quit)
    1.78 +    return;
    1.79 +
    1.80 +  state_->more_work_is_plausible = false;
    1.81 +
    1.82 +  if (state_->delegate->DoWork())
    1.83 +    state_->more_work_is_plausible = true;
    1.84 +
    1.85 +  if (state_->should_quit)
    1.86 +    return;
    1.87 +
    1.88 +  if (state_->delegate->DoDelayedWork(&delayed_work_time_))
    1.89 +    state_->more_work_is_plausible = true;
    1.90 +  if (state_->should_quit)
    1.91 +    return;
    1.92 +
    1.93 +  // Don't do idle work if we think there are more important things
    1.94 +  // that we could be doing.
    1.95 +  if (state_->more_work_is_plausible)
    1.96 +    return;
    1.97 +
    1.98 +  if (state_->delegate->DoIdleWork())
    1.99 +    state_->more_work_is_plausible = true;
   1.100 +  if (state_->should_quit)
   1.101 +    return;
   1.102 +}
   1.103 +
   1.104 +void MessagePumpForUI::Quit() {
   1.105 +  if (state_) {
   1.106 +    state_->should_quit = true;
   1.107 +  } else {
   1.108 +    NOTREACHED() << "Quit called outside Run!";
   1.109 +  }
   1.110 +}
   1.111 +
   1.112 +void MessagePumpForUI::ScheduleWork() {
   1.113 +  // This can be called on any thread, so we don't want to touch any state
   1.114 +  // variables as we would then need locks all over.  This ensures that if
   1.115 +  // we are sleeping in a poll that we will wake up.
   1.116 +  work_scheduled = true;
   1.117 +  mozilla::NotifyEvent();
   1.118 +}
   1.119 +
   1.120 +void MessagePumpForUI::ScheduleDelayedWork(const TimeTicks& delayed_work_time) {
   1.121 +  // We need to wake up the loop in case the poll timeout needs to be
   1.122 +  // adjusted.  This will cause us to try to do work, but that's ok.
   1.123 +  delayed_work_time_ = delayed_work_time;
   1.124 +  ScheduleWork();
   1.125 +}
   1.126 +
   1.127 +}  // namespace base

mercurial