Don't merge tab models until native resume has triggered

When using the Chrome Home promo in multi-window mode, restarting
the activity can cause a race condition where the multi-window state
changes before onResumeWithNative is called. This causes a crash when
the browser concequently tries to merge tab models. This change blocks
the logic in onMultiWindowModeChanged until onResumeWithNative has
been called.

BUG=775890
[email protected]

(cherry picked from commit 36e53e77b46fd48a35e5a20d00963eb108eba759)

Change-Id: Ic0f9f032ff0f7d99e5f29a4c603496937db0f66c
Reviewed-on: https://chromium-review.googlesource.com/739760
Commit-Queue: Matthew Jones <[email protected]>
Reviewed-by: Ted Choc <[email protected]>
Cr-Original-Commit-Position: refs/heads/master@{#512350}
Reviewed-on: https://chromium-review.googlesource.com/754075
Reviewed-by: Matthew Jones <[email protected]>
Cr-Commit-Position: refs/branch-heads/3239@{#385}
Cr-Branched-From: adb61db19020ed8ecee5e91b1a0ea4c924ae2988-refs/heads/master@{#508578}
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ChromeActivity.java b/chrome/android/java/src/org/chromium/chrome/browser/ChromeActivity.java
index 65a73489..f822ad13 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/ChromeActivity.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ChromeActivity.java
@@ -237,7 +237,7 @@
     private boolean mDeferredStartupPosted;
 
     private boolean mTabModelsInitialized;
-    private boolean mNativeInitialized;
+    protected boolean mNativeInitialized;
     private boolean mRemoveWindowBackgroundDone;
 
     // The class cannot implement TouchExplorationStateChangeListener,
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ChromeTabbedActivity.java b/chrome/android/java/src/org/chromium/chrome/browser/ChromeTabbedActivity.java
index 0388a79..88b21a3 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/ChromeTabbedActivity.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ChromeTabbedActivity.java
@@ -2265,7 +2265,7 @@
     @Override
     public void onMultiWindowModeChanged(boolean isInMultiWindowMode) {
         super.onMultiWindowModeChanged(isInMultiWindowMode);
-        if (!FeatureUtilities.isTabModelMergingEnabled()) return;
+        if (!FeatureUtilities.isTabModelMergingEnabled() || !mNativeInitialized) return;
         if (!isInMultiWindowMode) {
             // If the activity is currently resumed when multi-window mode is exited, try to merge
             // tabs from the other activity instance.