Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / sessions / session_restore.cc
index fcd601a..12ed758 100644 (file)
 #include "base/bind_helpers.h"
 #include "base/callback.h"
 #include "base/command_line.h"
+#include "base/debug/alias.h"
 #include "base/memory/scoped_ptr.h"
 #include "base/memory/scoped_vector.h"
 #include "base/metrics/histogram.h"
-#include "base/platform_file.h"
 #include "base/run_loop.h"
 #include "base/stl_util.h"
 #include "base/strings/stringprintf.h"
@@ -153,6 +153,9 @@ class TabLoader : public content::NotificationObserver,
   // Called when a tab goes away or a load completes.
   void HandleTabClosedOrLoaded(NavigationController* controller);
 
+  // TODO(sky): remove. For debugging 368236.
+  void CheckNotObserving(NavigationController* controller);
+
   content::NotificationRegistrar registrar_;
 
   // Current delay before a new tab is loaded. See class description for
@@ -204,6 +207,7 @@ TabLoader* TabLoader::GetTabLoader(base::TimeTicks restore_started) {
 }
 
 void TabLoader::ScheduleLoad(NavigationController* controller) {
+  CheckNotObserving(controller);
   DCHECK(controller);
   DCHECK(find(tabs_to_load_.begin(), tabs_to_load_.end(), controller) ==
          tabs_to_load_.end());
@@ -212,6 +216,7 @@ void TabLoader::ScheduleLoad(NavigationController* controller) {
 }
 
 void TabLoader::TabIsLoading(NavigationController* controller) {
+  CheckNotObserving(controller);
   DCHECK(controller);
   DCHECK(find(tabs_loading_.begin(), tabs_loading_.end(), controller) ==
          tabs_loading_.end());
@@ -481,6 +486,29 @@ void TabLoader::HandleTabClosedOrLoaded(NavigationController* tab) {
   }
 }
 
+void TabLoader::CheckNotObserving(NavigationController* controller) {
+  const bool in_tabs_to_load =
+      find(tabs_to_load_.begin(), tabs_to_load_.end(), controller) !=
+          tabs_to_load_.end();
+  const bool in_tabs_loading =
+      find(tabs_loading_.begin(), tabs_loading_.end(), controller) !=
+          tabs_loading_.end();
+  const bool observing =
+      registrar_.IsRegistered(
+          this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
+          content::Source<WebContents>(controller->GetWebContents())) ||
+      registrar_.IsRegistered(
+          this, content::NOTIFICATION_LOAD_STOP,
+          content::Source<NavigationController>(controller)) ||
+      registrar_.IsRegistered(
+          this, content::NOTIFICATION_LOAD_START,
+          content::Source<NavigationController>(controller));
+  base::debug::Alias(&in_tabs_to_load);
+  base::debug::Alias(&in_tabs_loading);
+  base::debug::Alias(&observing);
+  CHECK(!in_tabs_to_load && !in_tabs_loading && !observing);
+}
+
 // SessionRestoreImpl ---------------------------------------------------------
 
 // SessionRestoreImpl is responsible for fetching the set of tabs to create
@@ -710,9 +738,8 @@ class SessionRestoreImpl : public content::NotificationObserver {
                                                   host_desktop_type_));
       if (urls_to_open_.empty()) {
         // No tab browsers were created and no URLs were supplied on the command
-        // line. Add an empty URL, which is treated as opening the users home
-        // page.
-        urls_to_open_.push_back(GURL());
+        // line. Open the new tab page.
+        urls_to_open_.push_back(GURL(chrome::kChromeUINewTabURL));
       }
       AppendURLsToBrowser(browser, urls_to_open_);
       browser->window()->Show();
@@ -946,21 +973,24 @@ class SessionRestoreImpl : public content::NotificationObserver {
 
         // Loads are scheduled for each restored tab unless the tab is going to
         // be selected as ShowBrowser() will load the selected tab.
-        bool is_not_selected_tab = (i != selected_tab_index);
+        bool is_selected_tab = (i == selected_tab_index);
         WebContents* restored_tab =
-            RestoreTab(tab, i, browser, is_not_selected_tab);
+            RestoreTab(tab, i, browser, is_selected_tab);
 
         // RestoreTab can return NULL if |tab| doesn't have valid data.
         if (!restored_tab)
           continue;
 
         // If this isn't the selected tab, there's nothing else to do.
-        if (is_not_selected_tab)
+        if (!is_selected_tab)
           continue;
 
         ShowBrowser(
             browser,
             browser->tab_strip_model()->GetIndexOfWebContents(restored_tab));
+        // TODO(sky): remove. For debugging 368236.
+        CHECK_EQ(browser->tab_strip_model()->GetActiveWebContents(),
+                 restored_tab);
         tab_loader_->TabIsLoading(&browser->tab_strip_model()
                                        ->GetActiveWebContents()
                                        ->GetController());
@@ -973,19 +1003,19 @@ class SessionRestoreImpl : public content::NotificationObserver {
       for (int i = 0; i < static_cast<int>(window.tabs.size()); ++i) {
         const SessionTab& tab = *(window.tabs[i]);
         // Always schedule loads as we will not be calling ShowBrowser().
-        RestoreTab(tab, tab_index_offset + i, browser, true);
+        RestoreTab(tab, tab_index_offset + i, browser, false);
       }
     }
   }
 
   // |tab_index| is ignored for pinned tabs which will always be pushed behind
   // the last existing pinned tab.
-  // |schedule_load| will let |tab_loader_| know that it should schedule this
-  // tab for loading.
+  // |tab_loader_| will schedule this tab for loading if |is_selected_tab| is
+  // false.
   WebContents* RestoreTab(const SessionTab& tab,
                           const int tab_index,
                           Browser* browser,
-                          bool schedule_load) {
+                          bool is_selected_tab) {
     // It's possible (particularly for foreign sessions) to receive a tab
     // without valid navigations. In that case, just skip it.
     // See crbug.com/154129.
@@ -1035,7 +1065,7 @@ class SessionRestoreImpl : public content::NotificationObserver {
                                                                         *file);
     }
 
-    if (schedule_load)
+    if (!is_selected_tab)
       tab_loader_->ScheduleLoad(&web_contents->GetController());
     return web_contents;
   }