From 6b6a3bf89303c7ef84910b28d59523d6441364be Mon Sep 17 00:00:00 2001 From: Ben Murdoch Date: Mon, 25 Jul 2011 12:49:46 +0100 Subject: Query Private Browsing state for NFC on UI thread. The AsyncTask runs on the thread that created it, which in this case is not necessarily the UI thread. Use a Handler created on the UI thread instead. Bug: 5072522 Change-Id: Ia6db6cf396c3f73cfb51e6d4d227617d95946692 --- src/com/android/browser/NfcHandler.java | 76 ++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 35 deletions(-) (limited to 'src/com/android') diff --git a/src/com/android/browser/NfcHandler.java b/src/com/android/browser/NfcHandler.java index bc19950..a8e11d3 100644 --- a/src/com/android/browser/NfcHandler.java +++ b/src/com/android/browser/NfcHandler.java @@ -21,6 +21,10 @@ import android.nfc.NdefMessage; import android.nfc.NdefRecord; import android.nfc.NfcAdapter; import android.os.AsyncTask; +import android.os.Handler; +import android.os.Message; + +import java.util.concurrent.CountDownLatch; /** This class implements sharing the URL of the currently * shown browser page over NFC. Sharing is only active @@ -31,42 +35,26 @@ public class NfcHandler implements NfcAdapter.NdefPushCallback { private NfcAdapter mNfcAdapter; private Activity mActivity; private Controller mController; + private Handler mHandler; + private Tab mCurrentTab; + private boolean mIsPrivate; + private CountDownLatch mPrivateBrowsingSignal; - /** We need an async task to check whether the tab is private - * on the UI thread. - */ - private class CreateMessageTask extends AsyncTask { - private boolean mIsPrivate = false; - private Tab mCurrentTab; - - @Override - protected void onPreExecute() { - mCurrentTab = mController.getCurrentTab(); - if ((mCurrentTab != null) && (mCurrentTab.getWebView() != null)) { - mIsPrivate = mCurrentTab.getWebView().isPrivateBrowsingEnabled(); - } - } - - @Override - protected NdefMessage doInBackground(Void... params) { - if ((mCurrentTab == null) || mIsPrivate) { - return null; - } - String currentUrl = mCurrentTab.getUrl(); - if (currentUrl != null) { - NdefRecord record = NdefRecord.createUri(currentUrl); - NdefMessage msg = new NdefMessage(new NdefRecord[] { record }); - return msg; - } else { - return null; - } - } - } + private static final int GET_PRIVATE_BROWSING_STATE_MSG = 100; public NfcHandler(Activity browser, Controller controller) { mActivity = browser; mController = controller; mNfcAdapter = NfcAdapter.getDefaultAdapter(mActivity); + mHandler = new Handler() { + @Override + public void handleMessage(Message msg) { + if (msg.what == GET_PRIVATE_BROWSING_STATE_MSG) { + mIsPrivate = mCurrentTab.getWebView().isPrivateBrowsingEnabled(); + mPrivateBrowsingSignal.countDown(); + } + } + }; } void onPause() { @@ -83,11 +71,29 @@ public class NfcHandler implements NfcAdapter.NdefPushCallback { @Override public NdefMessage createMessage() { - CreateMessageTask task = new CreateMessageTask(); - task.execute(); - try { - return task.get(); - } catch (Exception e) { + mCurrentTab = mController.getCurrentTab(); + if ((mCurrentTab != null) && (mCurrentTab.getWebView() != null)) { + // We can only read the WebView state on the UI thread, so post + // a message and wait. + mPrivateBrowsingSignal = new CountDownLatch(1); + mHandler.sendMessage(mHandler.obtainMessage(GET_PRIVATE_BROWSING_STATE_MSG)); + try { + mPrivateBrowsingSignal.await(); + } catch (InterruptedException e) { + return null; + } + } + + if ((mCurrentTab == null) || mIsPrivate) { + return null; + } + + String currentUrl = mCurrentTab.getUrl(); + if (currentUrl != null) { + NdefRecord record = NdefRecord.createUri(currentUrl); + NdefMessage msg = new NdefMessage(new NdefRecord[] { record }); + return msg; + } else { return null; } } -- cgit v1.1 From 1b4193001e845711376b2c0406fab6898f4660c6 Mon Sep 17 00:00:00 2001 From: Ben Murdoch Date: Mon, 25 Jul 2011 14:44:29 +0100 Subject: Fix StrictMode violation in InstantSearchEngine Get the current URL from the Tab rather than WebView to avoid calling a WebView method on a background thread. Bug: 5072398 Change-Id: I6e0fe0bac525fc772f659068dbc03511f20addf4 --- src/com/android/browser/InstantSearchEngine.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/com/android') diff --git a/src/com/android/browser/InstantSearchEngine.java b/src/com/android/browser/InstantSearchEngine.java index c913494..4dd83fa 100644 --- a/src/com/android/browser/InstantSearchEngine.java +++ b/src/com/android/browser/InstantSearchEngine.java @@ -199,7 +199,7 @@ public class InstantSearchEngine implements SearchEngine, DropdownChangeListener return false; } - final String currentUrl = current.getUrl(); + final String currentUrl = mController.getCurrentTab().getUrl(); if (currentUrl != null) { Uri uri = Uri.parse(currentUrl); -- cgit v1.1 From aaa1f375bc9d6a6a175819fac34d39202f69a166 Mon Sep 17 00:00:00 2001 From: Ben Murdoch Date: Mon, 25 Jul 2011 15:40:58 +0100 Subject: Cache the factory reset URL. Hitting the browser provider for the factory reset url is one of our most common strict mode violations, and triggers sqlite disk io. So now read it once in a background thread and cache it for future accesses. Needed to make the requireInitialization() function static, so we now wait/notify on BrowserSettings.class rather than the instance. Bug:5072979 Change-Id: I78550965887e32b4f8ad0eaf0013753e63d6f602 --- src/com/android/browser/BrowserSettings.java | 33 ++++++++++++++++------------ 1 file changed, 19 insertions(+), 14 deletions(-) (limited to 'src/com/android') diff --git a/src/com/android/browser/BrowserSettings.java b/src/com/android/browser/BrowserSettings.java index a4f0f04..2023ee6 100644 --- a/src/com/android/browser/BrowserSettings.java +++ b/src/com/android/browser/BrowserSettings.java @@ -103,7 +103,7 @@ public class BrowserSettings implements OnSharedPreferenceChangeListener, private WebStorageSizeManager mWebStorageSizeManager; private AutofillHandler mAutofillHandler; private WeakHashMap mCustomUserAgents; - private boolean mInitialized = false; + private static boolean sInitialized = false; // Looper shared between some lightweight background operations // Specifically, this is created on the thread that initializes browser settings // and is then reused by CrashRecoveryHandler @@ -116,6 +116,8 @@ public class BrowserSettings implements OnSharedPreferenceChangeListener, // Cached settings private SearchEngine mSearchEngine; + private static String sFactoryResetUrl; + public static void initialize(final Context context) { sInstance = new BrowserSettings(context); } @@ -199,21 +201,28 @@ public class BrowserSettings implements OnSharedPreferenceChangeListener, } mPrefs.edit().remove(PREF_TEXT_SIZE).apply(); } + + sFactoryResetUrl = mContext.getResources().getString(R.string.homepage_base); + if (sFactoryResetUrl.indexOf("{CID}") != -1) { + sFactoryResetUrl = sFactoryResetUrl.replace("{CID}", + BrowserProvider.getClientId(mContext.getContentResolver())); + } + Looper.prepare(); mBackgroundLooper = Looper.myLooper(); - synchronized (BrowserSettings.this) { - mInitialized = true; - BrowserSettings.this.notifyAll(); + synchronized (BrowserSettings.class) { + sInitialized = true; + BrowserSettings.class.notifyAll(); } Looper.loop(); } }; - void requireInitialization() { - synchronized (BrowserSettings.this) { - while (!mInitialized) { + private static void requireInitialization() { + synchronized (BrowserSettings.class) { + while (!sInitialized) { try { - BrowserSettings.this.wait(); + BrowserSettings.class.wait(); } catch (InterruptedException e) { } } @@ -334,12 +343,8 @@ public class BrowserSettings implements OnSharedPreferenceChangeListener, } public static String getFactoryResetHomeUrl(Context context) { - String url = context.getResources().getString(R.string.homepage_base); - if (url.indexOf("{CID}") != -1) { - url = url.replace("{CID}", - BrowserProvider.getClientId(context.getContentResolver())); - } - return url; + requireInitialization(); + return sFactoryResetUrl; } public LayoutAlgorithm getLayoutAlgorithm() { -- cgit v1.1