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

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

Powered by Google App Engine
This is Rietveld 408576698