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

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: Created 4 years, 5 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/run_loop.h" 13 #include "base/run_loop.h"
14 #include "base/time/time.h" 14 #include "base/time/time.h"
15 #include "jni/SystemMessageHandler_jni.h" 15 #include "jni/SystemMessageHandler_jni.h"
16 16
17 using base::android::HasException;
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();
danakj 2016/07/20 21:37:08 Why not after this too?
Torne 2016/07/21 13:38:25 It's about to return to the JVM anyway, at the end
gsennton 2016/07/26 14:42:39 Added a comment about this here.
84 } 99 }
85 100
86 namespace base { 101 namespace base {
87 102
88 MessagePumpForUI::MessagePumpForUI() 103 MessagePumpForUI::MessagePumpForUI() : run_loop_(NULL), should_abort_(false) {}
danakj 2016/07/20 21:37:08 nit: nullptr while you're here?
gsennton 2016/07/26 14:42:39 Done.
89 : run_loop_(NULL) {
90 }
91 104
92 MessagePumpForUI::~MessagePumpForUI() { 105 MessagePumpForUI::~MessagePumpForUI() {
93 } 106 }
94 107
95 void MessagePumpForUI::Run(Delegate* delegate) { 108 void MessagePumpForUI::Run(Delegate* delegate) {
96 NOTREACHED() << "UnitTests should rely on MessagePumpForUIStub in" 109 NOTREACHED() << "UnitTests should rely on MessagePumpForUIStub in"
97 " test_stub_android.h"; 110 " test_stub_android.h";
98 } 111 }
99 112
100 void MessagePumpForUI::Start(Delegate* delegate) { 113 void MessagePumpForUI::Start(Delegate* delegate) {
101 run_loop_ = new RunLoop(); 114 run_loop_ = new RunLoop();
102 // Since the RunLoop was just created above, BeforeRun should be guaranteed to 115 // 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). 116 // return true (it only returns false if the RunLoop has been Quit already).
104 if (!run_loop_->BeforeRun()) 117 if (!run_loop_->BeforeRun())
105 NOTREACHED(); 118 NOTREACHED();
106 119
107 DCHECK(system_message_handler_obj_.is_null()); 120 DCHECK(system_message_handler_obj_.is_null());
108 121
109 JNIEnv* env = base::android::AttachCurrentThread(); 122 JNIEnv* env = base::android::AttachCurrentThread();
110 DCHECK(env); 123 DCHECK(env);
111 124
112 system_message_handler_obj_.Reset( 125 system_message_handler_obj_.Reset(Java_SystemMessageHandler_create(
113 Java_SystemMessageHandler_create( 126 env, reinterpret_cast<intptr_t>(delegate),
114 env, reinterpret_cast<intptr_t>(delegate))); 127 reinterpret_cast<intptr_t>(this)));
115 } 128 }
116 129
117 void MessagePumpForUI::Quit() { 130 void MessagePumpForUI::Quit() {
118 if (!system_message_handler_obj_.is_null()) { 131 if (!system_message_handler_obj_.is_null()) {
119 JNIEnv* env = base::android::AttachCurrentThread(); 132 JNIEnv* env = base::android::AttachCurrentThread();
120 DCHECK(env); 133 DCHECK(env);
121 134
135 // We can't call back into java if there is a pending jni exception
danakj 2016/07/20 21:37:09 I'm not sure what this comment means to explain, c
Torne 2016/07/21 13:38:25 We need to avoid calling any Java functions (like
gsennton 2016/07/26 14:42:39 Removed this comment since it doesn't really make
122 Java_SystemMessageHandler_removeAllPendingMessages(env, 136 Java_SystemMessageHandler_removeAllPendingMessages(env,
123 system_message_handler_obj_.obj()); 137 system_message_handler_obj_.obj());
124 system_message_handler_obj_.Reset(); 138 system_message_handler_obj_.Reset();
125 } 139 }
126 140
127 if (run_loop_) { 141 if (run_loop_) {
128 run_loop_->AfterRun(); 142 run_loop_->AfterRun();
129 delete run_loop_; 143 delete run_loop_;
130 run_loop_ = NULL; 144 run_loop_ = NULL;
131 } 145 }
(...skipping 23 matching lines...) Expand all
155 system_message_handler_obj_.obj(), 169 system_message_handler_obj_.obj(),
156 delayed_work_time.ToInternalValue(), millis); 170 delayed_work_time.ToInternalValue(), millis);
157 } 171 }
158 172
159 // static 173 // static
160 bool MessagePumpForUI::RegisterBindings(JNIEnv* env) { 174 bool MessagePumpForUI::RegisterBindings(JNIEnv* env) {
161 return RegisterNativesImpl(env); 175 return RegisterNativesImpl(env);
162 } 176 }
163 177
164 } // namespace base 178 } // namespace base
OLDNEW
« base/message_loop/message_loop.cc ('K') | « base/message_loop/message_pump_android.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698