Commit c727f5d1 authored by Bo Liu's avatar Bo Liu Committed by Commit Bot

weblayer: Add thread checks

Add thread check to all embedder to implementation calls.

Change-Id: Ifac0c0de5b2ea365313d151451cbff8ade7ab818
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1879428Reviewed-by: default avatarScott Violet <sky@chromium.org>
Commit-Queue: Bo <boliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#709129}
parent 0d1d7a9a
......@@ -23,23 +23,24 @@ template("weblayer_java") {
android_library(target_name) {
java_files = [
"org/chromium/weblayer/BrowserController.java",
"org/chromium/weblayer/BrowserFragment.java",
"org/chromium/weblayer/BrowserFragmentController.java",
"org/chromium/weblayer/BrowserFragment.java",
"org/chromium/weblayer/BrowserObserver.java",
"org/chromium/weblayer/Callback.java",
"org/chromium/weblayer/ChildProcessService.java",
"org/chromium/weblayer/DownloadDelegate.java",
"org/chromium/weblayer/FullscreenDelegate.java",
"org/chromium/weblayer/ListenableFuture.java",
"org/chromium/weblayer/ListenableResult.java",
"org/chromium/weblayer/Navigation.java",
"org/chromium/weblayer/NavigationController.java",
"org/chromium/weblayer/Navigation.java",
"org/chromium/weblayer/NavigationObserver.java",
"org/chromium/weblayer/ObserverList.java",
"org/chromium/weblayer/Profile.java",
"org/chromium/weblayer/ProfileManager.java",
"org/chromium/weblayer/WebLayer.java",
"org/chromium/weblayer/ChildProcessService.java",
"org/chromium/weblayer/ThreadCheck.java",
"org/chromium/weblayer/UnsupportedVersionException.java",
"org/chromium/weblayer/WebLayer.java",
]
deps = [
......
......@@ -49,6 +49,7 @@ public final class BrowserController {
}
public void setDownloadDelegate(@Nullable DownloadDelegate delegate) {
ThreadCheck.ensureOnUiThread();
try {
if (delegate != null) {
mDownloadDelegateClient = new DownloadDelegateClientImpl(delegate);
......@@ -63,6 +64,7 @@ public final class BrowserController {
}
public void setFullscreenDelegate(@Nullable FullscreenDelegate delegate) {
ThreadCheck.ensureOnUiThread();
try {
if (delegate != null) {
mFullscreenDelegateClient = new FullscreenDelegateClientImpl(delegate);
......@@ -77,6 +79,7 @@ public final class BrowserController {
}
public DownloadDelegate getDownloadDelegate() {
ThreadCheck.ensureOnUiThread();
return mDownloadDelegateClient != null ? mDownloadDelegateClient.getDelegate() : null;
}
......@@ -87,6 +90,7 @@ public final class BrowserController {
*/
public void executeScript(
@NonNull String script, @Nullable ValueCallback<JSONObject> callback) {
ThreadCheck.ensureOnUiThread();
try {
ValueCallback<String> stringCallback = (String result) -> {
if (callback == null) {
......@@ -109,6 +113,7 @@ public final class BrowserController {
@Nullable
public FullscreenDelegate getFullscreenDelegate() {
ThreadCheck.ensureOnUiThread();
return mFullscreenDelegateClient != null ? mFullscreenDelegateClient.getDelegate() : null;
}
......@@ -119,14 +124,17 @@ public final class BrowserController {
@NonNull
public NavigationController getNavigationController() {
ThreadCheck.ensureOnUiThread();
return mNavigationController;
}
public void addObserver(@Nullable BrowserObserver observer) {
ThreadCheck.ensureOnUiThread();
mObservers.addObserver(observer);
}
public void removeObserver(@Nullable BrowserObserver observer) {
ThreadCheck.ensureOnUiThread();
mObservers.removeObserver(observer);
}
......
......@@ -145,6 +145,7 @@ public final class BrowserFragment extends Fragment {
*/
public BrowserFragment() {
super();
ThreadCheck.ensureOnUiThread();
}
/**
......@@ -153,6 +154,7 @@ public final class BrowserFragment extends Fragment {
*/
@NonNull
public BrowserFragmentController getController() {
ThreadCheck.ensureOnUiThread();
if (mBrowserFragmentController == null) {
throw new RuntimeException("BrowserFragmentController is available only between "
+ "BrowserFragment's onCreate() and onDestroy().");
......@@ -162,6 +164,7 @@ public final class BrowserFragment extends Fragment {
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
ThreadCheck.ensureOnUiThread();
try {
mImpl.asRemoteFragment().handleOnActivityResult(
requestCode, resultCode, ObjectWrapper.wrap(data));
......@@ -173,6 +176,7 @@ public final class BrowserFragment extends Fragment {
@SuppressWarnings("MissingSuperCall")
@Override
public void onAttach(Context context) {
ThreadCheck.ensureOnUiThread();
// This is the first lifecycle event and also the first time we can get app context (unless
// the embedder has already called getController). So it's the latest and at the same time
// the earliest moment when we can initialize WebLayer without missing any lifecycle events.
......@@ -206,6 +210,7 @@ public final class BrowserFragment extends Fragment {
@SuppressWarnings("MissingSuperCall")
@Override
public void onCreate(Bundle savedInstanceState) {
ThreadCheck.ensureOnUiThread();
try {
mImpl.asRemoteFragment().handleOnCreate(ObjectWrapper.wrap(savedInstanceState));
mBrowserFragmentController = new BrowserFragmentController(mImpl.getController(),
......@@ -218,6 +223,7 @@ public final class BrowserFragment extends Fragment {
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ThreadCheck.ensureOnUiThread();
try {
return ObjectWrapper.unwrap(mImpl.asRemoteFragment().handleOnCreateView(), View.class);
} catch (RemoteException e) {
......@@ -228,6 +234,7 @@ public final class BrowserFragment extends Fragment {
@SuppressWarnings("MissingSuperCall")
@Override
public void onActivityCreated(Bundle savedInstanceState) {
ThreadCheck.ensureOnUiThread();
try {
mImpl.asRemoteFragment().handleOnActivityCreated(
ObjectWrapper.wrap(savedInstanceState));
......@@ -239,6 +246,7 @@ public final class BrowserFragment extends Fragment {
@SuppressWarnings("MissingSuperCall")
@Override
public void onStart() {
ThreadCheck.ensureOnUiThread();
try {
mImpl.asRemoteFragment().handleOnStart();
} catch (RemoteException e) {
......@@ -249,6 +257,7 @@ public final class BrowserFragment extends Fragment {
@SuppressWarnings("MissingSuperCall")
@Override
public void onResume() {
ThreadCheck.ensureOnUiThread();
try {
mImpl.asRemoteFragment().handleOnResume();
} catch (RemoteException e) {
......@@ -259,6 +268,7 @@ public final class BrowserFragment extends Fragment {
@SuppressWarnings("MissingSuperCall")
@Override
public void onSaveInstanceState(Bundle outState) {
ThreadCheck.ensureOnUiThread();
try {
mImpl.asRemoteFragment().handleOnSaveInstanceState(ObjectWrapper.wrap(outState));
} catch (RemoteException e) {
......@@ -269,6 +279,7 @@ public final class BrowserFragment extends Fragment {
@SuppressWarnings("MissingSuperCall")
@Override
public void onPause() {
ThreadCheck.ensureOnUiThread();
try {
mImpl.asRemoteFragment().handleOnPause();
} catch (RemoteException e) {
......@@ -279,6 +290,7 @@ public final class BrowserFragment extends Fragment {
@SuppressWarnings("MissingSuperCall")
@Override
public void onStop() {
ThreadCheck.ensureOnUiThread();
try {
mImpl.asRemoteFragment().handleOnStop();
} catch (RemoteException e) {
......@@ -289,6 +301,7 @@ public final class BrowserFragment extends Fragment {
@SuppressWarnings("MissingSuperCall")
@Override
public void onDestroyView() {
ThreadCheck.ensureOnUiThread();
try {
mImpl.asRemoteFragment().handleOnDestroyView();
} catch (RemoteException e) {
......@@ -299,6 +312,7 @@ public final class BrowserFragment extends Fragment {
@SuppressWarnings("MissingSuperCall")
@Override
public void onDestroy() {
ThreadCheck.ensureOnUiThread();
try {
mImpl.asRemoteFragment().handleOnDestroy();
// The other side does the clean up automatically in handleOnDestroy()
......@@ -311,6 +325,7 @@ public final class BrowserFragment extends Fragment {
@SuppressWarnings("MissingSuperCall")
@Override
public void onDetach() {
ThreadCheck.ensureOnUiThread();
try {
mImpl.asRemoteFragment().handleOnDetach();
} catch (RemoteException e) {
......
......@@ -32,6 +32,7 @@ public final class BrowserFragmentController {
// TODO(pshmakov): rename this to BrowserTabController.
@NonNull
public BrowserController getBrowserController() {
ThreadCheck.ensureOnUiThread();
if (mController == null) {
try {
mController = new BrowserController(mImpl.getBrowserController());
......@@ -43,6 +44,7 @@ public final class BrowserFragmentController {
}
public void setTopView(@Nullable View view) {
ThreadCheck.ensureOnUiThread();
try {
mImpl.setTopView(ObjectWrapper.wrap(view));
} catch (RemoteException e) {
......@@ -61,6 +63,7 @@ public final class BrowserFragmentController {
*/
@NonNull
public ListenableResult<Boolean> setSupportsEmbedding(boolean enable) {
ThreadCheck.ensureOnUiThread();
try {
final ListenableResult<Boolean> listenableResult = new ListenableResult<Boolean>();
mImpl.setSupportsEmbedding(
......@@ -82,6 +85,7 @@ public final class BrowserFragmentController {
*/
@NonNull
public Profile getProfile() {
ThreadCheck.ensureOnUiThread();
try {
return mProfileManager.getProfileFor(mImpl.getProfile());
} catch (RemoteException e) {
......
......@@ -49,6 +49,7 @@ public final class Navigation extends IClientNavigation.Stub {
}
public State getState() {
ThreadCheck.ensureOnUiThread();
try {
return ipcStateToState(mNavigationImpl.getState());
} catch (RemoteException e) {
......@@ -62,6 +63,7 @@ public final class Navigation extends IClientNavigation.Stub {
*/
@NonNull
public Uri getUri() {
ThreadCheck.ensureOnUiThread();
try {
return Uri.parse(mNavigationImpl.getUri());
} catch (RemoteException e) {
......@@ -75,6 +77,7 @@ public final class Navigation extends IClientNavigation.Stub {
*/
@NonNull
public List<Uri> getRedirectChain() {
ThreadCheck.ensureOnUiThread();
try {
List<Uri> redirects = new ArrayList<Uri>();
for (String r : mNavigationImpl.getRedirectChain()) redirects.add(Uri.parse(r));
......
......@@ -40,6 +40,7 @@ public final class NavigationController {
}
public void navigate(@NonNull Uri uri) {
ThreadCheck.ensureOnUiThread();
try {
mNavigationController.navigate(uri.toString());
} catch (RemoteException e) {
......@@ -48,6 +49,7 @@ public final class NavigationController {
}
public void goBack() {
ThreadCheck.ensureOnUiThread();
try {
mNavigationController.goBack();
} catch (RemoteException e) {
......@@ -56,6 +58,7 @@ public final class NavigationController {
}
public void goForward() {
ThreadCheck.ensureOnUiThread();
try {
mNavigationController.goForward();
} catch (RemoteException e) {
......@@ -64,6 +67,7 @@ public final class NavigationController {
}
public boolean canGoBack() {
ThreadCheck.ensureOnUiThread();
try {
return mNavigationController.canGoBack();
} catch (RemoteException e) {
......@@ -72,6 +76,7 @@ public final class NavigationController {
}
public boolean canGoForward() {
ThreadCheck.ensureOnUiThread();
try {
return mNavigationController.canGoForward();
} catch (RemoteException e) {
......@@ -80,6 +85,7 @@ public final class NavigationController {
}
public void reload() {
ThreadCheck.ensureOnUiThread();
try {
mNavigationController.reload();
} catch (RemoteException e) {
......@@ -88,6 +94,7 @@ public final class NavigationController {
}
public void stop() {
ThreadCheck.ensureOnUiThread();
try {
mNavigationController.stop();
} catch (RemoteException e) {
......@@ -96,6 +103,7 @@ public final class NavigationController {
}
public int getNavigationListSize() {
ThreadCheck.ensureOnUiThread();
try {
return mNavigationController.getNavigationListSize();
} catch (RemoteException e) {
......@@ -104,6 +112,7 @@ public final class NavigationController {
}
public int getNavigationListCurrentIndex() {
ThreadCheck.ensureOnUiThread();
try {
return mNavigationController.getNavigationListCurrentIndex();
} catch (RemoteException e) {
......@@ -113,6 +122,7 @@ public final class NavigationController {
@NonNull
public Uri getNavigationEntryDisplayUri(int index) {
ThreadCheck.ensureOnUiThread();
try {
return Uri.parse(mNavigationController.getNavigationEntryDisplayUri(index));
} catch (RemoteException e) {
......@@ -121,10 +131,12 @@ public final class NavigationController {
}
public void addObserver(@NonNull NavigationObserver observer) {
ThreadCheck.ensureOnUiThread();
mObservers.addObserver(observer);
}
public void removeObserver(@NonNull NavigationObserver observer) {
ThreadCheck.ensureOnUiThread();
mObservers.removeObserver(observer);
}
......
......@@ -29,6 +29,7 @@ public final class Profile {
}
public void clearBrowsingData() {
ThreadCheck.ensureOnUiThread();
try {
mImpl.clearBrowsingData();
} catch (RemoteException e) {
......@@ -37,6 +38,7 @@ public final class Profile {
}
public void destroy() {
ThreadCheck.ensureOnUiThread();
try {
mImpl.destroy();
} catch (RemoteException e) {
......
// Copyright 2019 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.weblayer;
import org.chromium.weblayer_private.aidl.IProfile;
......@@ -23,6 +27,7 @@ import java.util.Map;
/** Destroys all the Profiles. */
void destroy() {
ThreadCheck.ensureOnUiThread();
for (Profile profile : mProfiles.values()) {
profile.destroy();
}
......
// Copyright 2019 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.weblayer;
import android.os.Looper;
import android.util.AndroidRuntimeException;
/* package */ class ThreadCheck {
/* package */ static void ensureOnUiThread() {
if (Looper.getMainLooper() != Looper.myLooper()) {
throw new AndroidRuntimeException("This method needs to be called on the main thread");
}
}
}
......@@ -109,6 +109,7 @@ public final class WebLayer {
@NonNull
public static ListenableFuture<WebLayer> create(Context appContext)
throws UnsupportedVersionException {
ThreadCheck.ensureOnUiThread();
if (sFuture == null) {
// Just in case the app passed an Activity context.
appContext = appContext.getApplicationContext();
......@@ -157,7 +158,8 @@ public final class WebLayer {
}
@Override
public void onLoad() {
/* package */ void onLoad() {
ThreadCheck.ensureOnUiThread();
try {
mIWebLayer.loadSync();
} catch (RemoteException e) {
......@@ -177,6 +179,7 @@ public final class WebLayer {
}
public void destroy() {
ThreadCheck.ensureOnUiThread();
// TODO: implement me.
mProfileManager.destroy();
}
......@@ -187,6 +190,7 @@ public final class WebLayer {
@NonNull
public static BrowserFragment createBrowserFragment(String profilePath) {
ThreadCheck.ensureOnUiThread();
// TODO: use a profile id instead of the path to the actual file.
Bundle args = new Bundle();
args.putString(BrowserFragmentArgs.PROFILE_PATH, profilePath == null ? "" : profilePath);
......
......@@ -191,8 +191,8 @@ public class NavigationTest {
mActivityTestRule.navigateAndWait(URL2);
mActivityTestRule.navigateAndWait(URL3);
NavigationController navigationController =
activity.getBrowserController().getNavigationController();
NavigationController navigationController = runOnUiThreadBlocking(
() -> activity.getBrowserController().getNavigationController());
navigateAndWaitForCompletion(URL2, () -> {
assertTrue(navigationController.canGoBack());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment