forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change JS Sandbox client library in line with AndroidX lint rules
AndroidX lint rules are more stricter than the chromium lint rules, so make changes here to align with the AndroidX rules. Also, add other minor improvements to the client code. Bug: 1297672 Change-Id: Id3cd7d88518361eae76a7bf53ac9714a1b06a058 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3714103 Reviewed-by: Richard Coles <torne@chromium.org> Commit-Queue: Abhijith Nair <abhijithnair@chromium.org> Reviewed-by: Robert Sesek <rsesek@chromium.org> Cr-Commit-Position: refs/heads/main@{#1019539}
- Loading branch information
Abhijith Nair
authored and
Chromium LUCI CQ
committed
Jun 30, 2022
1 parent
dafab3b
commit bf9afa9
Showing
12 changed files
with
228 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
.../js_sandbox/java/src/org/chromium/android_webview/js_sandbox/client/CloseGuardHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright 2022 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. | ||
|
||
package org.chromium.android_webview.js_sandbox.client; | ||
|
||
import android.os.Build; | ||
import android.util.CloseGuard; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.RequiresApi; | ||
import androidx.core.util.Preconditions; | ||
|
||
/** | ||
* Helper for accessing CloseGuard on API levels that support it. | ||
* | ||
* <p>All operations will be no-ops on non-supported API levels. | ||
*/ | ||
@RequiresApi(21) | ||
public final class CloseGuardHelper { | ||
private final CloseGuardImpl mImpl; | ||
|
||
private CloseGuardHelper(CloseGuardImpl impl) { | ||
mImpl = impl; | ||
} | ||
|
||
/** | ||
* Returns a {@link CloseGuardHelper} which defers to the platform close guard if it is | ||
* available. | ||
*/ | ||
@NonNull | ||
public static CloseGuardHelper create() { | ||
if (Build.VERSION.SDK_INT >= 30) { | ||
return new CloseGuardHelper(new CloseGuardApi30Impl()); | ||
} | ||
|
||
return new CloseGuardHelper(new CloseGuardNoOpImpl()); | ||
} | ||
|
||
/** | ||
* Initializes the instance with a warning that the caller should have explicitly called the | ||
* {@code closeMethodName} method instead of relying on finalization. | ||
* | ||
* @param closeMethodName non-null name of explicit termination method. Printed by | ||
* warnIfOpen. | ||
* @throws NullPointerException if closeMethodName is null. | ||
*/ | ||
public void open(@NonNull String closeMethodName) { | ||
mImpl.open(closeMethodName); | ||
} | ||
|
||
/** Marks this CloseGuard instance as closed to avoid warnings on finalization. */ | ||
public void close() { | ||
mImpl.close(); | ||
} | ||
|
||
/** | ||
* Logs a warning if the caller did not properly cleanup by calling an explicit close method | ||
* before finalization. | ||
*/ | ||
public void warnIfOpen() { | ||
mImpl.warnIfOpen(); | ||
} | ||
|
||
private interface CloseGuardImpl { | ||
void open(@NonNull String closeMethodName); | ||
void close(); | ||
void warnIfOpen(); | ||
} | ||
|
||
@RequiresApi(30) | ||
static final class CloseGuardApi30Impl implements CloseGuardImpl { | ||
private final CloseGuard mPlatformImpl = new CloseGuard(); | ||
|
||
@Override | ||
public void open(@NonNull String closeMethodName) { | ||
mPlatformImpl.open(closeMethodName); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
mPlatformImpl.close(); | ||
} | ||
|
||
@Override | ||
public void warnIfOpen() { | ||
mPlatformImpl.warnIfOpen(); | ||
} | ||
} | ||
|
||
static final class CloseGuardNoOpImpl implements CloseGuardImpl { | ||
@Override | ||
public void open(@NonNull String closeMethodName) { | ||
Preconditions.checkNotNull(closeMethodName, "CloseMethodName must not be null."); | ||
} | ||
|
||
@Override | ||
public void close() {} | ||
|
||
@Override | ||
public void warnIfOpen() {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.