Skip to content

Commit

Permalink
[WebLayer] Make BrowserFragmentImpl extend FragmentHostingRemoteFragm…
Browse files Browse the repository at this point in the history
…entImpl

BrowserFragmentImpl extending FragmentHostingRemoteFragmentImpl means we
have a FragmentController (and therefore a FragmentManager) available to
us in the main WebLayer UI. Upcoming page info changes will use this
FragmentManager, and mediarouter could be refactored to use this as well
instead of creating its own RemoteFragment.

Bug: 1077766
Change-Id: Id659555b827feed4293b062f8b0d8c6f4889daba
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2575753
Commit-Queue: Robbie McElrath <rmcelrath@chromium.org>
Reviewed-by: Bo <boliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#835045}
  • Loading branch information
robbiemc authored and Chromium LUCI CQ committed Dec 9, 2020
1 parent b2af6d8 commit 708cc13
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
/**
* Implementation of RemoteFragmentImpl which forwards logic to BrowserImpl.
*/
public class BrowserFragmentImpl extends RemoteFragmentImpl {
public class BrowserFragmentImpl extends FragmentHostingRemoteFragmentImpl {
private static int sResumedCount;
private static long sSessionStartTimeMs;

Expand All @@ -41,11 +41,6 @@ public class BrowserFragmentImpl extends RemoteFragmentImpl {
// embedder.
private Context mEmbedderActivityContext;

// The WebLayer-wrapped context object. This context gets assets and resources from WebLayer,
// not from the embedder. Use this for the most part, especially to resolve WebLayer-specific
// resource IDs.
private Context mContext;

public BrowserFragmentImpl(
ProfileManager profileManager, IRemoteFragmentClient client, Bundle fragmentArgs) {
super(client);
Expand All @@ -66,11 +61,9 @@ public void onAttach(Context context) {
StrictModeWorkaround.apply();
super.onAttach(context);
mEmbedderActivityContext = context;
mContext = new ContextThemeWrapper(
ClassLoaderContextWrapperFactory.get(context), R.style.Theme_BrowserUI);
if (mBrowser != null) { // On first creation, onAttach is called before onCreate
mBrowser.onFragmentAttached(
mEmbedderActivityContext, new FragmentWindowAndroid(mContext, this));
mBrowser.onFragmentAttached(mEmbedderActivityContext,
new FragmentWindowAndroid(getWebLayerContext(), this));
}
}

Expand All @@ -80,12 +73,12 @@ public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// onCreate() is only called once
assert mBrowser == null;
// onCreate() is always called after onAttach(). onAttach() sets |mContext| and
// onCreate() is always called after onAttach(). onAttach() sets |getWebLayerContext()| and
// |mEmbedderContext|.
assert mContext != null;
assert getWebLayerContext() != null;
assert mEmbedderActivityContext != null;
mBrowser = new BrowserImpl(mEmbedderActivityContext, mProfile, mPersistenceId,
savedInstanceState, new FragmentWindowAndroid(mContext, this));
savedInstanceState, new FragmentWindowAndroid(getWebLayerContext(), this));
}

@Override
Expand Down Expand Up @@ -123,7 +116,6 @@ public void onDetach() {
if (mBrowser != null) {
mBrowser.onFragmentDetached();
}
mContext = null;
}

@Override
Expand Down Expand Up @@ -189,4 +181,12 @@ public IBrowser getBrowser() {
}
};
}

@Override
protected FragmentHostingRemoteFragmentImpl.RemoteFragmentContext createRemoteFragmentContext(
Context embedderContext) {
Context wrappedContext = ClassLoaderContextWrapperFactory.get(embedderContext);
Context themedContext = new ContextThemeWrapper(wrappedContext, R.style.Theme_BrowserUI);
return new FragmentHostingRemoteFragmentImpl.RemoteFragmentContext(themedContext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ public void onAttach(Context embedderContext) {
mContext = createRemoteFragmentContext(embedderContext);
mFragmentController =
FragmentController.createController(new RemoteFragmentHostCallback(this));
mFragmentController.attachHost(null);

// Some appcompat functionality depends on Fragments being hosted from within an
// AppCompatActivity, which performs some static initialization. Even if we're running
Expand All @@ -172,10 +173,7 @@ public void onAttach(Context embedderContext) {
@Override
public void onCreate(Bundle savedInstanceState) {
StrictModeWorkaround.apply();
mFragmentController.attachHost(null);

super.onCreate(savedInstanceState);

mFragmentController.dispatchCreate();
}

Expand All @@ -198,6 +196,14 @@ public void onDetach() {
StrictModeWorkaround.apply();
super.onDetach();
mContext = null;

// If the Fragment is retained, onDestory won't be called during configuration changes. We
// have to create a new FragmentController that's attached to the correct Context when
// reattaching this Fragment, so destroy the existing one here.
if (!mFragmentController.getSupportFragmentManager().isDestroyed()) {
mFragmentController.dispatchDestroy();
assert mFragmentController.getSupportFragmentManager().isDestroyed();
}
}

@Override
Expand Down

0 comments on commit 708cc13

Please sign in to comment.