Bug 860879 - Make PromptService not block when handing result from UI to Gecko thread; r=wesj
authorJim Chen <nchen@mozilla.com>
Thu, 09 May 2013 21:48:00 -0400
changeset 142416 2e1981be9749ddf738f1558b506271dc877746c4
parent 142415 4b3b08aeba3de1307814c6df61f8dc95a42d897b
child 142417 c66ae62f9cb18390e5f9b484653f27652ae172d4
push id2579
push userakeybl@mozilla.com
push dateMon, 24 Jun 2013 18:52:47 +0000
treeherdermozilla-beta@b69b7de8a05a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerswesj
bugs860879
milestone23.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 860879 - Make PromptService not block when handing result from UI to Gecko thread; r=wesj
mobile/android/base/PromptService.java
--- a/mobile/android/base/PromptService.java
+++ b/mobile/android/base/PromptService.java
@@ -43,40 +43,40 @@ import android.widget.ListView;
 import android.widget.ScrollView;
 import android.widget.Spinner;
 import android.widget.TextView;
 import android.widget.TimePicker;
 
 import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.GregorianCalendar;
-import java.util.concurrent.SynchronousQueue;
+import java.util.concurrent.ConcurrentLinkedQueue;
 import java.util.concurrent.TimeUnit;
 
 public class PromptService implements OnClickListener, OnCancelListener, OnItemClickListener, GeckoEventResponder {
     private static final String LOGTAG = "GeckoPromptService";
 
     private String[] mButtons;
     private PromptInput[] mInputs;
     private boolean[] mSelected;
     private AlertDialog mDialog;
 
     private final LayoutInflater mInflater;
-    private final SynchronousQueue<String> mPromptQueue;
+    private final ConcurrentLinkedQueue<String> mPromptQueue;
     private final int mGroupPaddingSize;
     private final int mLeftRightTextWithIconPadding;
     private final int mTopBottomTextWithIconPadding;
     private final int mIconTextPadding;
     private final int mIconSize;
     private final int mInputPaddingSize;
     private final int mMinRowSize;
 
     PromptService() {
         mInflater = LayoutInflater.from(GeckoApp.mAppContext);
-        mPromptQueue = new SynchronousQueue<String>();
+        mPromptQueue = new ConcurrentLinkedQueue<String>();
 
         Resources res = GeckoApp.mAppContext.getResources();
         mGroupPaddingSize = (int) (res.getDimension(R.dimen.prompt_service_group_padding_size));
         mLeftRightTextWithIconPadding = (int) (res.getDimension(R.dimen.prompt_service_left_right_text_with_icon_padding));
         mTopBottomTextWithIconPadding = (int) (res.getDimension(R.dimen.prompt_service_top_bottom_text_with_icon_padding));
         mIconTextPadding = (int) (res.getDimension(R.dimen.prompt_service_icon_text_padding));
         mIconSize = (int) (res.getDimension(R.dimen.prompt_service_icon_size));
         mInputPaddingSize = (int) (res.getDimension(R.dimen.prompt_service_inputs_padding));
@@ -281,24 +281,21 @@ public class PromptService implements On
         });
     }
 
     // GeckoEventResponder implementation
     @Override
     public String getResponse() {
         // we only handle one kind of message in handleMessage, and this is the
         // response we provide for that message
-        String result = null;
-        try {
-            while (null == (result = mPromptQueue.poll(1, TimeUnit.MILLISECONDS))) {
-                GeckoAppShell.processNextNativeEvent(false);
-            }
-        } catch (InterruptedException e) {
+        String result;
+        while (null == (result = mPromptQueue.poll())) {
+            GeckoAppShell.processNextNativeEvent(true);
         }
-        return result != null ? result : "";
+        return result;
     }
 
     private View applyInputStyle(View view) {
         view.setPadding(mInputPaddingSize, 0, mInputPaddingSize, 0);
         return view;
     }
 
     public void show(String aTitle, String aText, PromptListItem[] aMenuList, boolean aMultipleSelection) {
@@ -454,24 +451,19 @@ public class PromptService implements On
         finishDialog(ret.toString());
     }
 
     public void finishDialog(String aReturn) {
         mInputs = null;
         mButtons = null;
         mDialog = null;
         mSelected = null;
-        try {
-            if (!mPromptQueue.offer(aReturn, 5, TimeUnit.SECONDS)) {
-                ThreadUtils.dumpAllStackTraces();
-                throw new ThreadUtils.UiThreadBlockedException();
-            }
-        } catch(InterruptedException ex) {
-            Log.d(LOGTAG, "mPromptQueue not ready yet");
-        }
+        mPromptQueue.offer(aReturn);
+        // poke the Gecko thread in case it's waiting for new events
+        GeckoAppShell.sendEventToGecko(GeckoEvent.createNoOpEvent());
     }
 
     private void processMessage(JSONObject geckoObject) {
         String title = getSafeString(geckoObject, "title");
         String text = getSafeString(geckoObject, "text");
 
         mButtons = getStringArray(geckoObject, "buttons");