Commit 9b4bd427 authored by Scott Violet's avatar Scott Violet Committed by Commit Bot

weblayer: adds static getter for BrowserFragmentController

The hope is when we move to a more recent version of androidx we can
makes BrowserFragment package private (by way of FragmentFactory).
This patch removes the need to use BrowserFragment directly.

BUG=none
TEST=none

Change-Id: I7a4469fcf620f38c36c84a580cf4b8f666542c9f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1888389
Commit-Queue: Scott Violet <sky@chromium.org>
Reviewed-by: default avatarBo <boliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#710879}
parent beaf4c19
......@@ -29,9 +29,14 @@ import java.util.concurrent.Future;
/**
* WebLayer's fragment implementation.
*
* This class is an implementation detail and will eventually be hidden. Use
* {@link BrowserFragmentController#fromFragment} to get the BrowserFragmentController from a
* Fragment created by WebLayer.
*
* All the browser APIs, such as loading pages can be accessed via
* {@link BrowserFragmentController}, which can be retrieved with {@link #getController} after
* the fragment received onCreate the call.
* {@link BrowserFragmentController}, which can be retrieved with {@link
* FragmentSupport#getBrowserFragmentControllerForFragment} after the fragment received onCreate the
* call.
*
* Attaching a BrowserFragment to an Activity requires WebLayer to be initialized, so
* BrowserFragment will block the thread in onAttach until it's done. To prevent this,
......@@ -153,7 +158,7 @@ public final class BrowserFragment extends Fragment {
* The controller is available only between BrowserFragment's onCreate() and onDestroy().
*/
@NonNull
public BrowserFragmentController getController() {
BrowserFragmentController getController() {
ThreadCheck.ensureOnUiThread();
if (mBrowserFragmentController == null) {
throw new RuntimeException("BrowserFragmentController is available only between "
......
......@@ -5,6 +5,7 @@
package org.chromium.weblayer;
import android.os.RemoteException;
import android.support.v4.app.Fragment;
import android.view.View;
import android.webkit.ValueCallback;
......@@ -23,12 +24,23 @@ public final class BrowserFragmentController {
private final ProfileManager mProfileManager;
private BrowserController mController;
BrowserFragmentController(IBrowserFragmentController impl, ProfileManager profileManager) {
mImpl = impl;
mProfileManager = profileManager;
}
/**
* Returns the BrowserFragmentController for the supplied Fragment; null if
* {@link fragment} was not created by WebLayer.
*
* @return the BrowserFragmentController
*/
@Nullable
public static BrowserFragmentController fromFragment(@Nullable Fragment fragment) {
return fragment instanceof BrowserFragment ? ((BrowserFragment) fragment).getController()
: null;
}
/**
* Returns the BrowserController associated with this BrowserFragmentController.
*
......
......@@ -11,6 +11,7 @@ import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v4.app.Fragment;
import android.util.AndroidRuntimeException;
import android.webkit.ValueCallback;
import android.webkit.WebViewDelegate;
......@@ -192,7 +193,7 @@ public final class WebLayer {
}
@NonNull
public static BrowserFragment createBrowserFragment(String profilePath) {
public static Fragment createBrowserFragment(String profilePath) {
ThreadCheck.ensureOnUiThread();
// TODO: use a profile id instead of the path to the actual file.
Bundle args = new Bundle();
......
......@@ -5,6 +5,7 @@
package org.chromium.weblayer_browsertests_apk;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
......@@ -18,7 +19,6 @@ import org.chromium.content_public.browser.BrowserStartupController;
import org.chromium.native_test.NativeBrowserTest;
import org.chromium.native_test.NativeBrowserTestActivity;
import org.chromium.weblayer.BrowserController;
import org.chromium.weblayer.BrowserFragment;
import org.chromium.weblayer.BrowserFragmentController;
import org.chromium.weblayer.BrowserObserver;
import org.chromium.weblayer.Profile;
......@@ -77,13 +77,13 @@ public class WebLayerBrowserTestsActivity extends NativeBrowserTestActivity {
new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
BrowserFragment fragment = WebLayer.createBrowserFragment(null);
Fragment fragment = WebLayer.createBrowserFragment(null);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(viewId, fragment);
transaction.commitNow();
mBrowserFragmentController = fragment.getController();
mBrowserFragmentController = BrowserFragmentController.fromFragment(fragment);
mProfile = mBrowserFragmentController.getProfile();
mBrowserFragmentController.setTopView(topContentsContainer);
......
......@@ -22,7 +22,6 @@ import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import org.chromium.weblayer.BrowserController;
import org.chromium.weblayer.BrowserFragment;
import org.chromium.weblayer.BrowserFragmentController;
import org.chromium.weblayer.BrowserObserver;
import org.chromium.weblayer.Profile;
......@@ -47,7 +46,6 @@ public class InstrumentationActivity extends FragmentActivity {
private View mMainView;
private int mMainViewId;
private ViewGroup mTopContentsContainer;
private BrowserFragment mFragment;
private IntentInterceptor mIntentInterceptor;
public BrowserController getBrowserController() {
......@@ -120,8 +118,8 @@ public class InstrumentationActivity extends FragmentActivity {
private void onWebLayerReady(Bundle savedInstanceState) {
if (isFinishing() || isDestroyed()) return;
mFragment = getOrCreateBrowserFragment(savedInstanceState);
mBrowserFragmentController = mFragment.getController();
Fragment fragment = getOrCreateBrowserFragment(savedInstanceState);
mBrowserFragmentController = BrowserFragmentController.fromFragment(fragment);
mProfile = mBrowserFragmentController.getProfile();
mBrowserFragmentController.setTopView(mTopContentsContainer);
......@@ -135,7 +133,7 @@ public class InstrumentationActivity extends FragmentActivity {
});
}
private BrowserFragment getOrCreateBrowserFragment(Bundle savedInstanceState) {
private Fragment getOrCreateBrowserFragment(Bundle savedInstanceState) {
FragmentManager fragmentManager = getSupportFragmentManager();
if (savedInstanceState != null) {
// FragmentManager could have re-created the fragment.
......@@ -144,7 +142,7 @@ public class InstrumentationActivity extends FragmentActivity {
throw new IllegalStateException("More than one fragment added, shouldn't happen");
}
if (fragments.size() == 1) {
return (BrowserFragment) fragments.get(0);
return fragments.get(0);
}
}
......@@ -156,7 +154,7 @@ public class InstrumentationActivity extends FragmentActivity {
profilePath = new File(getFilesDir(), profileName).getPath();
} // else create an in-memory Profile.
BrowserFragment fragment = WebLayer.createBrowserFragment(profilePath);
Fragment fragment = WebLayer.createBrowserFragment(profilePath);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(mMainViewId, fragment);
......
......@@ -28,7 +28,6 @@ import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import org.chromium.weblayer.BrowserController;
import org.chromium.weblayer.BrowserFragment;
import org.chromium.weblayer.BrowserFragmentController;
import org.chromium.weblayer.BrowserObserver;
import org.chromium.weblayer.DownloadDelegate;
......@@ -57,7 +56,6 @@ public class WebLayerShellActivity extends FragmentActivity {
private View mMainView;
private int mMainViewId;
private ViewGroup mTopContentsContainer;
private BrowserFragment mFragment;
@Override
protected void onCreate(final Bundle savedInstanceState) {
......@@ -125,8 +123,8 @@ public class WebLayerShellActivity extends FragmentActivity {
private void onWebLayerReady(Bundle savedInstanceState) {
if (isFinishing() || isDestroyed()) return;
mFragment = getOrCreateBrowserFragment(savedInstanceState);
mBrowserFragmentController = mFragment.getController();
Fragment fragment = getOrCreateBrowserFragment(savedInstanceState);
mBrowserFragmentController = BrowserFragmentController.fromFragment(fragment);
mBrowserFragmentController.getBrowserController().setFullscreenDelegate(
new FullscreenDelegate() {
private int mSystemVisibilityToRestore;
......@@ -204,7 +202,7 @@ public class WebLayerShellActivity extends FragmentActivity {
});
}
private BrowserFragment getOrCreateBrowserFragment(Bundle savedInstanceState) {
private Fragment getOrCreateBrowserFragment(Bundle savedInstanceState) {
FragmentManager fragmentManager = getSupportFragmentManager();
if (savedInstanceState != null) {
// FragmentManager could have re-created the fragment.
......@@ -213,7 +211,7 @@ public class WebLayerShellActivity extends FragmentActivity {
throw new IllegalStateException("More than one fragment added, shouldn't happen");
}
if (fragments.size() == 1) {
return (BrowserFragment) fragments.get(0);
return fragments.get(0);
}
}
......@@ -223,7 +221,7 @@ public class WebLayerShellActivity extends FragmentActivity {
profilePath = new File(getFilesDir(), profileName).getPath();
} // else create an in-memory Profile.
BrowserFragment fragment = WebLayer.createBrowserFragment(profilePath);
Fragment fragment = WebLayer.createBrowserFragment(profilePath);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(mMainViewId, fragment);
......
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