Index: chrome/browser/ui/browser_instant_controller.cc |
diff --git a/chrome/browser/ui/browser_instant_controller.cc b/chrome/browser/ui/browser_instant_controller.cc |
index 7abe3aa900015a2cc93088f32a243fdf112b7983..2c840536651b3c00032831221c621b7d43f11bf9 100644 |
--- a/chrome/browser/ui/browser_instant_controller.cc |
+++ b/chrome/browser/ui/browser_instant_controller.cc |
@@ -78,6 +78,33 @@ void BrowserInstantController::RegisterUserPrefs(PrefServiceSyncable* prefs) { |
PrefServiceSyncable::SYNCABLE_PREF); |
} |
+bool BrowserInstantController::MaybeSwapInInstantContents( |
+ const GURL& url, |
+ content::WebContents* source_contents, |
+ content::WebContents** target_contents) { |
+ if (url != GURL(chrome::kChromeUINewTabURL)) |
+ return false; |
+ |
+ scoped_ptr<content::WebContents> instant_ntp = instant_.ReleaseNTPContents(); |
+ if (!instant_ntp) |
+ return false; |
+ |
+ *target_contents = instant_ntp.get(); |
+ instant_ntp->GetController().PruneAllButActive(); |
+ if (source_contents) { |
+ instant_ntp->GetController().CopyStateFromAndPrune( |
+ &source_contents->GetController()); |
+ ReplaceWebContentsAt( |
+ browser_->tab_strip_model()->GetIndexOfWebContents(source_contents), |
+ instant_ntp.Pass()); |
+ } else { |
+ // If |source_contents| is NULL, then the caller is responsible for |
+ // inserting instant_ntp into the tabstrip and will take ownership. |
+ ignore_result(instant_ntp.release()); |
+ } |
+ return true; |
+} |
+ |
bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition) { |
// Unsupported dispositions. |
if (disposition == NEW_BACKGROUND_TAB || disposition == NEW_WINDOW) |
@@ -93,23 +120,17 @@ bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition) { |
INSTANT_COMMIT_PRESSED_ENTER : INSTANT_COMMIT_PRESSED_ALT_ENTER); |
} |
-void BrowserInstantController::CommitInstant(content::WebContents* preview, |
- bool in_new_tab) { |
+void BrowserInstantController::CommitInstant( |
+ scoped_ptr<content::WebContents> preview, |
+ bool in_new_tab) { |
if (in_new_tab) { |
// TabStripModel takes ownership of |preview|. |
- browser_->tab_strip_model()->AddWebContents(preview, -1, |
+ browser_->tab_strip_model()->AddWebContents(preview.release(), -1, |
instant_.last_transition_type(), TabStripModel::ADD_ACTIVE); |
} else { |
int index = browser_->tab_strip_model()->active_index(); |
- DCHECK_NE(TabStripModel::kNoTab, index); |
- content::WebContents* active_tab = |
- browser_->tab_strip_model()->GetWebContentsAt(index); |
- // TabStripModel takes ownership of |preview|. |
- browser_->tab_strip_model()->ReplaceWebContentsAt(index, preview); |
- // InstantUnloadHandler takes ownership of |active_tab|. |
- instant_unload_handler_.RunUnloadListenersOrDestroy(active_tab, index); |
- |
- GURL url = preview->GetURL(); |
+ const GURL& url = preview->GetURL(); |
+ ReplaceWebContentsAt(index, preview.Pass()); |
DCHECK(browser_->profile()->GetExtensionService()); |
if (browser_->profile()->GetExtensionService()->IsInstalledApp(url)) { |
AppLauncherHandler::RecordAppLaunchType( |
@@ -118,6 +139,21 @@ void BrowserInstantController::CommitInstant(content::WebContents* preview, |
} |
} |
+void BrowserInstantController::ReplaceWebContentsAt( |
+ int index, |
+ scoped_ptr<content::WebContents> new_contents) { |
+ DCHECK_NE(TabStripModel::kNoTab, index); |
+ content::WebContents* old_contents = |
+ browser_->tab_strip_model()->GetWebContentsAt(index); |
+ // TabStripModel takes ownership of |new_contents|. |
+ browser_->tab_strip_model()->ReplaceWebContentsAt( |
+ index, new_contents.release()); |
+ // TODO(samarth): use scoped_ptr instead of comments to document ownership |
+ // transfer. |
+ // InstantUnloadHandler takes ownership of |old_contents|. |
+ instant_unload_handler_.RunUnloadListenersOrDestroy(old_contents, index); |
+} |
+ |
void BrowserInstantController::SetInstantSuggestion( |
const InstantSuggestion& suggestion) { |
browser_->window()->GetLocationBar()->SetInstantSuggestion(suggestion); |