summaryrefslogtreecommitdiffstats
path: root/content/public/test/nested_message_pump_android.cc
blob: c3adb2cda694f4044e11938bb36dc0e545d2dcd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/public/test/nested_message_pump_android.h"

#include "base/android/jni_android.h"
#include "base/android/scoped_java_ref.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/synchronization/waitable_event.h"
#include "base/threading/thread_restrictions.h"
#include "base/time/time.h"
#include "jni/NestedSystemMessageHandler_jni.h"

namespace {

base::LazyInstance<base::android::ScopedJavaGlobalRef<jobject> >
    g_message_handler_obj = LAZY_INSTANCE_INITIALIZER;

}  // namespace


namespace content {

struct NestedMessagePumpAndroid::RunState {
  RunState(base::MessagePump::Delegate* delegate, int run_depth)
      : delegate(delegate),
        run_depth(run_depth),
        should_quit(false),
        waitable_event(false, false) {
  }

  base::MessagePump::Delegate* delegate;

  // Used to count how many Run() invocations are on the stack.
  int run_depth;

  // Used to flag that the current Run() invocation should return ASAP.
  bool should_quit;

  // Used to sleep until there is more work to do.
  base::WaitableEvent waitable_event;

  // The time at which we should call DoDelayedWork.
  base::TimeTicks delayed_work_time;
};

NestedMessagePumpAndroid::NestedMessagePumpAndroid()
    : state_(NULL) {
}

NestedMessagePumpAndroid::~NestedMessagePumpAndroid() {
}

void NestedMessagePumpAndroid::Run(Delegate* delegate) {
  RunState state(delegate, state_ ? state_->run_depth + 1 : 1);
  RunState* previous_state = state_;
  state_ = &state;

  JNIEnv* env = base::android::AttachCurrentThread();
  DCHECK(env);

  // Need to cap the wait time to allow task processing on the java
  // side. Otherwise, a long wait time on the native will starve java
  // tasks.
  base::TimeDelta max_delay = base::TimeDelta::FromMilliseconds(100);

  for (;;) {
    if (state_->should_quit)
      break;

    bool did_work = state_->delegate->DoWork();
    if (state_->should_quit)
      break;

    did_work |= state_->delegate->DoDelayedWork(&state_->delayed_work_time);
    if (state_->should_quit)
      break;

    if (did_work) {
      continue;
    }

    did_work = state_->delegate->DoIdleWork();
    if (state_->should_quit)
      break;

    if (did_work)
      continue;

    // No native tasks to process right now. Process tasks from the Java
    // System message handler. This will return when the java message queue
    // is idle.
    bool ret = Java_NestedSystemMessageHandler_runNestedLoopTillIdle(env,
        g_message_handler_obj.Get().obj());
    CHECK(ret) << "Error running java message loop, tests will likely fail.";

    base::ThreadRestrictions::ScopedAllowWait allow_wait;
    if (state_->delayed_work_time.is_null()) {
      state_->waitable_event.TimedWait(max_delay);
    } else {
      base::TimeDelta delay =
          state_->delayed_work_time - base::TimeTicks::Now();
      if (delay > max_delay)
        delay = max_delay;
      if (delay > base::TimeDelta()) {
        state_->waitable_event.TimedWait(delay);
      } else {
        // It looks like delayed_work_time indicates a time in the past, so we
        // need to call DoDelayedWork now.
        state_->delayed_work_time = base::TimeTicks();
      }
    }
  }

  state_ = previous_state;
}

void NestedMessagePumpAndroid::Start(
    base::MessagePump::Delegate* delegate) {
  JNIEnv* env = base::android::AttachCurrentThread();
  DCHECK(env);
  g_message_handler_obj.Get().Reset(
      Java_NestedSystemMessageHandler_create(env));

  base::MessagePumpForUI::Start(delegate);
}

void NestedMessagePumpAndroid::Quit() {
  if (state_) {
    state_->should_quit = true;
    state_->waitable_event.Signal();
    return;
  }
  base::MessagePumpForUI::Quit();
}

void NestedMessagePumpAndroid::ScheduleWork() {
  if (state_) {
    state_->waitable_event.Signal();
    return;
  }

  base::MessagePumpForUI::ScheduleWork();
}

void NestedMessagePumpAndroid::ScheduleDelayedWork(
    const base::TimeTicks& delayed_work_time) {
  if (state_) {
    // We know that we can't be blocked on Wait right now since this method can
    // only be called on the same thread as Run, so we only need to update our
    // record of how long to sleep when we do sleep.
    state_->delayed_work_time = delayed_work_time;
    return;
  }

  base::MessagePumpForUI::ScheduleDelayedWork(delayed_work_time);
}

// static
bool NestedMessagePumpAndroid::RegisterJni(JNIEnv* env) {
  return RegisterNativesImpl(env);
}

}  // namespace content