Bug 1485151 - Part 1 - Import StrictModeContext from Chromium. r=jchen,mhoye

Differential Revision: https://phabricator.services.mozilla.com/D4888

--HG--
extra : source : 424fcd7c1bef5f9591d749555c042c184cfbf313
extra : histedit_source : 614bd7bac33d7312079337aed2184b1e47d04418
This commit is contained in:
Jan Henning 2018-08-22 20:16:34 +02:00
Родитель b26b734898
Коммит 3e5cdb5011
2 изменённых файлов: 88 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,87 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Copied from Chromium's /src/base/android/java/src/org/chromium/base/StrictModeContext.java.
package org.mozilla.gecko.util;
import android.os.StrictMode;
import java.io.Closeable;
/**
* Enables try-with-resources compatible StrictMode violation whitelisting.
*
* Example:
* <pre>
* try (StrictModeContext unused = StrictModeContext.allowDiskWrites()) {
* return Example.doThingThatRequiresDiskWrites();
* }
* </pre>
*
*/
public final class StrictModeContext implements Closeable {
private final StrictMode.ThreadPolicy mThreadPolicy;
private final StrictMode.VmPolicy mVmPolicy;
private StrictModeContext(StrictMode.ThreadPolicy threadPolicy, StrictMode.VmPolicy vmPolicy) {
mThreadPolicy = threadPolicy;
mVmPolicy = vmPolicy;
}
private StrictModeContext(StrictMode.ThreadPolicy threadPolicy) {
this(threadPolicy, null);
}
private StrictModeContext(StrictMode.VmPolicy vmPolicy) {
this(null, vmPolicy);
}
/**
* Convenience method for disabling all VM-level StrictMode checks with try-with-resources.
* Includes everything listed here:
* https://developer.android.com/reference/android/os/StrictMode.VmPolicy.Builder.html
*/
public static StrictModeContext allowAllVmPolicies() {
StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
StrictMode.setVmPolicy(StrictMode.VmPolicy.LAX);
return new StrictModeContext(oldPolicy);
}
/**
* Convenience method for disabling StrictMode for disk-writes with try-with-resources.
*/
public static StrictModeContext allowDiskWrites() {
StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
return new StrictModeContext(oldPolicy);
}
/**
* Convenience method for disabling StrictMode for disk-reads with try-with-resources.
*/
public static StrictModeContext allowDiskReads() {
StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
return new StrictModeContext(oldPolicy);
}
/**
* Convenience method for disabling StrictMode for slow calls with try-with-resources.
*/
public static StrictModeContext allowSlowCalls() {
StrictMode.ThreadPolicy oldPolicy = StrictMode.getThreadPolicy();
StrictMode.setThreadPolicy(
new StrictMode.ThreadPolicy.Builder(oldPolicy).permitCustomSlowCalls().build());
return new StrictModeContext(oldPolicy);
}
@Override
public void close() {
if (mThreadPolicy != null) {
StrictMode.setThreadPolicy(mThreadPolicy);
}
if (mVmPolicy != null) {
StrictMode.setVmPolicy(mVmPolicy);
}
}
}

Просмотреть файл

@ -2832,6 +2832,7 @@ WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#endif
<li><code>browser/extensions/mortar/host/common/opengles2-utils.jsm</code></li>
<li><code>editor/libeditor/EditorEventListener.cpp</code></li>
<li><code>mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/StrictModeContext.java</code></li>
<li><code>security/sandbox/</code></li>
<li><code>widget/cocoa/GfxInfo.mm</code></li>
</ul>