Revert of Fix foreign tabs with > 6 navigations not showing up on Android. (https://codereview.chromium.org/433633002/)

Reason for revert:
The CL is suspected to cause old crashes to reappear. See issue 399763.

[email protected]
BUG=378992, 399763

Original issue's description:
> Fix foreign tabs with > 6 navigations not showing up on Android.
> 
> The code being deleted here was introduced in
> http://crrev.com/23514039 because tabs with invalid indexes
> were being received. I have now identified the cause of that
> original issue, which is that the current index of a tab
> isn't updated when we truncate the navigation stack for
> syncing. For now, I'm making Android behave like all other
> clients and bounding the index inside the navigations array.
> 
> BUG=378992
> 
> Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=286879

[email protected]
NOTREECHECKS=true
NOTRY=true
BUG=378992

Review URL: https://codereview.chromium.org/461813002

Cr-Commit-Position: refs/heads/master@{#288864}
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@288864 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/chrome/browser/android/foreign_session_helper.cc b/chrome/browser/android/foreign_session_helper.cc
index 54de7a86..f1b3565 100644
--- a/chrome/browser/android/foreign_session_helper.cc
+++ b/chrome/browser/android/foreign_session_helper.cc
@@ -46,6 +46,44 @@
   return service->GetOpenTabsUIDelegate();
 }
 
+bool ShouldSkipTab(const SessionTab& session_tab) {
+    if (session_tab.navigations.empty())
+      return true;
+
+    int selected_index = session_tab.current_navigation_index;
+    if (selected_index < 0 ||
+        selected_index >= static_cast<int>(session_tab.navigations.size()))
+      return true;
+
+    const ::sessions::SerializedNavigationEntry& current_navigation =
+        session_tab.navigations.at(selected_index);
+
+    if (current_navigation.virtual_url().is_empty())
+      return true;
+
+    return false;
+}
+
+bool ShouldSkipWindow(const SessionWindow& window) {
+  for (std::vector<SessionTab*>::const_iterator tab_it = window.tabs.begin();
+      tab_it != window.tabs.end(); ++tab_it) {
+    const SessionTab &session_tab = **tab_it;
+    if (!ShouldSkipTab(session_tab))
+      return false;
+  }
+  return true;
+}
+
+bool ShouldSkipSession(const browser_sync::SyncedSession& session) {
+  for (SyncedSession::SyncedWindowMap::const_iterator it =
+      session.windows.begin(); it != session.windows.end(); ++it) {
+    const SessionWindow  &window = *(it->second);
+    if (!ShouldSkipWindow(window))
+      return false;
+  }
+  return true;
+}
+
 void CopyTabsToJava(
     JNIEnv* env,
     const SessionWindow& window,
@@ -54,7 +92,12 @@
       tab_it != window.tabs.end(); ++tab_it) {
     const SessionTab &session_tab = **tab_it;
 
-    int selected_index = session_tab.normalized_navigation_index();
+    if (ShouldSkipTab(session_tab))
+      continue;
+
+    int selected_index = session_tab.current_navigation_index;
+    DCHECK(selected_index >= 0);
+    DCHECK(selected_index < static_cast<int>(session_tab.navigations.size()));
 
     const ::sessions::SerializedNavigationEntry& current_navigation =
         session_tab.navigations.at(selected_index);
@@ -78,6 +121,9 @@
       session.windows.begin(); it != session.windows.end(); ++it) {
     const SessionWindow &window = *(it->second);
 
+    if (ShouldSkipWindow(window))
+      continue;
+
     ScopedJavaLocalRef<jobject> last_pushed_window;
     last_pushed_window.Reset(
         Java_ForeignSessionHelper_pushWindow(
@@ -178,6 +224,8 @@
   // Note: we don't own the SyncedSessions themselves.
   for (size_t i = 0; i < sessions.size(); ++i) {
     const browser_sync::SyncedSession &session = *(sessions[i]);
+    if (ShouldSkipSession(session))
+      continue;
 
     const bool is_collapsed = collapsed_sessions->HasKey(session.session_tag);