Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(170)

Side by Side Diff: base/message_loop/message_pump_android.cc

Issue 2169553002: Properly throw java exceptions from shouldOverrideUrlLoading (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add test-only subclasses for JavaHandlerThread and SystemMessageHandler. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/message_loop/message_pump_android.h" 5 #include "base/message_loop/message_pump_android.h"
6 6
7 #include <jni.h> 7 #include <jni.h>
8 8
9 #include "base/android/jni_android.h" 9 #include "base/android/jni_android.h"
10 #include "base/android/scoped_java_ref.h" 10 #include "base/android/scoped_java_ref.h"
11 #include "base/lazy_instance.h" 11 #include "base/lazy_instance.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/message_loop/test_system_message_handler_link_android.h"
13 #include "base/run_loop.h" 14 #include "base/run_loop.h"
14 #include "base/time/time.h" 15 #include "base/time/time.h"
15 #include "jni/SystemMessageHandler_jni.h" 16 #include "jni/SystemMessageHandler_jni.h"
16 17
17 using base::android::ScopedJavaLocalRef; 18 using base::android::ScopedJavaLocalRef;
18 19
19 // ---------------------------------------------------------------------------- 20 // ----------------------------------------------------------------------------
20 // Native JNI methods called by Java. 21 // Native JNI methods called by Java.
21 // ---------------------------------------------------------------------------- 22 // ----------------------------------------------------------------------------
22 // This method can not move to anonymous namespace as it has been declared as 23 // This method can not move to anonymous namespace as it has been declared as
23 // 'static' in system_message_handler_jni.h. 24 // 'static' in system_message_handler_jni.h.
24 static void DoRunLoopOnce(JNIEnv* env, 25 static void DoRunLoopOnce(JNIEnv* env,
25 const JavaParamRef<jobject>& obj, 26 const JavaParamRef<jobject>& obj,
26 jlong native_delegate, 27 jlong native_delegate,
28 jlong native_message_pump,
27 jlong delayed_scheduled_time_ticks) { 29 jlong delayed_scheduled_time_ticks) {
28 base::MessagePump::Delegate* delegate = 30 base::MessagePump::Delegate* delegate =
29 reinterpret_cast<base::MessagePump::Delegate*>(native_delegate); 31 reinterpret_cast<base::MessagePump::Delegate*>(native_delegate);
30 DCHECK(delegate); 32 DCHECK(delegate);
33 base::MessagePumpForUI* pump =
34 reinterpret_cast<base::MessagePumpForUI*>(native_message_pump);
35 DCHECK(pump);
31 // This is based on MessagePumpForUI::DoRunLoop() from desktop. 36 // This is based on MessagePumpForUI::DoRunLoop() from desktop.
32 // Note however that our system queue is handled in the java side. 37 // Note however that our system queue is handled in the java side.
33 // In desktop we inspect and process a single system message and then 38 // In desktop we inspect and process a single system message and then
34 // we call DoWork() / DoDelayedWork(). 39 // we call DoWork() / DoDelayedWork().
35 // On Android, the java message queue may contain messages for other handlers 40 // On Android, the java message queue may contain messages for other handlers
36 // that will be processed before calling here again. 41 // that will be processed before calling here again.
37 bool did_work = delegate->DoWork(); 42 bool did_work = delegate->DoWork();
43 if (pump->ShouldAbort()) {
44 // There is a pending JNI exception, return to Java so that the exception is
45 // thrown correctly.
46 return;
47 }
38 48
39 // In the java side, |SystemMessageHandler| keeps a single "delayed" message. 49 // In the java side, |SystemMessageHandler| keeps a single "delayed" message.
40 // It's an expensive operation to |removeMessage| there, so this is optimized 50 // It's an expensive operation to |removeMessage| there, so this is optimized
41 // to avoid those calls. 51 // to avoid those calls.
42 // 52 //
43 // At this stage, |next_delayed_work_time| can be: 53 // At this stage, |next_delayed_work_time| can be:
44 // 1) The same as previously scheduled: nothing to be done, move along. This 54 // 1) The same as previously scheduled: nothing to be done, move along. This
45 // is the typical case, since this method is called for every single message. 55 // is the typical case, since this method is called for every single message.
46 // 56 //
47 // 2) Not previously scheduled: just post a new message in java. 57 // 2) Not previously scheduled: just post a new message in java.
48 // 58 //
49 // 3) Shorter than previously scheduled: far less common. In this case, 59 // 3) Shorter than previously scheduled: far less common. In this case,
50 // |removeMessage| and post a new one. 60 // |removeMessage| and post a new one.
51 // 61 //
52 // 4) Longer than previously scheduled (or null): nothing to be done, move 62 // 4) Longer than previously scheduled (or null): nothing to be done, move
53 // along. 63 // along.
54 // 64 //
55 // Side note: base::TimeTicks is a C++ representation and can't be 65 // Side note: base::TimeTicks is a C++ representation and can't be
56 // compared in java. When calling |scheduleDelayedWork|, pass the 66 // compared in java. When calling |scheduleDelayedWork|, pass the
57 // |InternalValue()| to java and then back to C++ so the comparisons can be 67 // |InternalValue()| to java and then back to C++ so the comparisons can be
58 // done here. 68 // done here.
59 // This roundtrip allows comparing TimeTicks directly (cheap) and 69 // This roundtrip allows comparing TimeTicks directly (cheap) and
60 // avoid comparisons with TimeDelta / Now() (expensive). 70 // avoid comparisons with TimeDelta / Now() (expensive).
61 base::TimeTicks next_delayed_work_time; 71 base::TimeTicks next_delayed_work_time;
62 did_work |= delegate->DoDelayedWork(&next_delayed_work_time); 72 did_work |= delegate->DoDelayedWork(&next_delayed_work_time);
73 if (pump->ShouldAbort()) {
74 // There is a pending JNI exception, return to Java so that the exception is
75 // thrown correctly
76 return;
77 }
63 78
64 if (!next_delayed_work_time.is_null()) { 79 if (!next_delayed_work_time.is_null()) {
65 // Schedule a new message if there's nothing already scheduled or there's a 80 // Schedule a new message if there's nothing already scheduled or there's a
66 // shorter delay than previously scheduled (see (2) and (3) above). 81 // shorter delay than previously scheduled (see (2) and (3) above).
67 if (delayed_scheduled_time_ticks == 0 || 82 if (delayed_scheduled_time_ticks == 0 ||
68 next_delayed_work_time < base::TimeTicks::FromInternalValue( 83 next_delayed_work_time < base::TimeTicks::FromInternalValue(
69 delayed_scheduled_time_ticks)) { 84 delayed_scheduled_time_ticks)) {
70 Java_SystemMessageHandler_scheduleDelayedWork(env, obj, 85 Java_SystemMessageHandler_scheduleDelayedWork(env, obj,
71 next_delayed_work_time.ToInternalValue(), 86 next_delayed_work_time.ToInternalValue(),
72 (next_delayed_work_time - 87 (next_delayed_work_time -
73 base::TimeTicks::Now()).InMillisecondsRoundedUp()); 88 base::TimeTicks::Now()).InMillisecondsRoundedUp());
74 } 89 }
75 } 90 }
76 91
77 // This is a major difference between android and other platforms: since we 92 // This is a major difference between android and other platforms: since we
78 // can't inspect it and process just one single message, instead we'll yeld 93 // can't inspect it and process just one single message, instead we'll yeld
79 // the callstack. 94 // the callstack.
80 if (did_work) 95 if (did_work)
81 return; 96 return;
82 97
83 delegate->DoIdleWork(); 98 delegate->DoIdleWork();
99 // Note that we do not check whether we should abort here since we are
100 // returning to the JVM anyway. If, in the future, we add any more code after
101 // the call to DoIdleWork() here, we should add an abort-check and return
102 // immediately if the check passes.
84 } 103 }
85 104
86 namespace base { 105 namespace base {
87 106
88 MessagePumpForUI::MessagePumpForUI() 107 MessagePumpForUI::MessagePumpForUI()
89 : run_loop_(NULL) { 108 : run_loop_(nullptr), should_abort_(false) {}
90 }
91 109
92 MessagePumpForUI::~MessagePumpForUI() { 110 MessagePumpForUI::~MessagePumpForUI() {
93 } 111 }
94 112
95 void MessagePumpForUI::Run(Delegate* delegate) { 113 void MessagePumpForUI::Run(Delegate* delegate) {
96 NOTREACHED() << "UnitTests should rely on MessagePumpForUIStub in" 114 NOTREACHED() << "UnitTests should rely on MessagePumpForUIStub in"
97 " test_stub_android.h"; 115 " test_stub_android.h";
98 } 116 }
99 117
100 void MessagePumpForUI::Start(Delegate* delegate) { 118 JNIEnv* MessagePumpForUI::StartInternal() {
101 run_loop_ = new RunLoop(); 119 run_loop_ = new RunLoop();
102 // Since the RunLoop was just created above, BeforeRun should be guaranteed to 120 // Since the RunLoop was just created above, BeforeRun should be guaranteed to
103 // return true (it only returns false if the RunLoop has been Quit already). 121 // return true (it only returns false if the RunLoop has been Quit already).
104 if (!run_loop_->BeforeRun()) 122 if (!run_loop_->BeforeRun())
105 NOTREACHED(); 123 NOTREACHED();
106 124
107 DCHECK(system_message_handler_obj_.is_null()); 125 DCHECK(system_message_handler_obj_.is_null());
108 126
109 JNIEnv* env = base::android::AttachCurrentThread(); 127 JNIEnv* env = base::android::AttachCurrentThread();
110 DCHECK(env); 128 DCHECK(env);
129 return env;
130 }
111 131
132 void MessagePumpForUI::Start(Delegate* delegate) {
133 JNIEnv* env = StartInternal();
134 system_message_handler_obj_.Reset(Java_SystemMessageHandler_create(
135 env, reinterpret_cast<intptr_t>(delegate),
136 reinterpret_cast<intptr_t>(this)));
137 }
138
139 void MessagePumpForUI::StartForTesting(Delegate* delegate,
140 WaitableEvent* test_done_event) {
141 JNIEnv* env = StartInternal();
112 system_message_handler_obj_.Reset( 142 system_message_handler_obj_.Reset(
113 Java_SystemMessageHandler_create( 143 TestSystemMessageHandlerLink::createTestSystemMessageHandler(
nyquist 2016/08/16 17:46:39 Would it be possible to plumb through a factory (o
gsennton 2016/08/17 17:05:48 Done.
114 env, reinterpret_cast<intptr_t>(delegate))); 144 env, delegate, this, test_done_event));
115 } 145 }
116 146
117 void MessagePumpForUI::Quit() { 147 void MessagePumpForUI::Quit() {
118 if (!system_message_handler_obj_.is_null()) { 148 if (!system_message_handler_obj_.is_null()) {
119 JNIEnv* env = base::android::AttachCurrentThread(); 149 JNIEnv* env = base::android::AttachCurrentThread();
120 DCHECK(env); 150 DCHECK(env);
121 151
122 Java_SystemMessageHandler_removeAllPendingMessages(env, 152 Java_SystemMessageHandler_removeAllPendingMessages(env,
123 system_message_handler_obj_.obj()); 153 system_message_handler_obj_.obj());
124 system_message_handler_obj_.Reset(); 154 system_message_handler_obj_.Reset();
(...skipping 26 matching lines...) Expand all
151 (delayed_work_time - TimeTicks::Now()).InMillisecondsRoundedUp(); 181 (delayed_work_time - TimeTicks::Now()).InMillisecondsRoundedUp();
152 // Note that we're truncating to milliseconds as required by the java side, 182 // Note that we're truncating to milliseconds as required by the java side,
153 // even though delayed_work_time is microseconds resolution. 183 // even though delayed_work_time is microseconds resolution.
154 Java_SystemMessageHandler_scheduleDelayedWork(env, 184 Java_SystemMessageHandler_scheduleDelayedWork(env,
155 system_message_handler_obj_.obj(), 185 system_message_handler_obj_.obj(),
156 delayed_work_time.ToInternalValue(), millis); 186 delayed_work_time.ToInternalValue(), millis);
157 } 187 }
158 188
159 // static 189 // static
160 bool MessagePumpForUI::RegisterBindings(JNIEnv* env) { 190 bool MessagePumpForUI::RegisterBindings(JNIEnv* env) {
161 return RegisterNativesImpl(env); 191 return RegisterNativesImpl(env) &&
192 TestSystemMessageHandlerLink::RegisterJNI(env);
162 } 193 }
163 194
164 } // namespace base 195 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698