diff options
author | samarth@chromium.org <samarth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-02-08 01:13:33 +0000 |
---|---|---|
committer | samarth@chromium.org <samarth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-02-08 01:13:33 +0000 |
commit | 0c9406639ed7eadded6d93a3b6e83e4a7e1e2458 (patch) | |
tree | 2fdc436c9b897d72cdd851279b39d15fdd8f264d /chrome/browser/ui/browser_instant_controller.cc | |
parent | e65bf5b4dbae7cc753405a0bace3f73c8a6bdec2 (diff) | |
download | chromium_src-0c9406639ed7eadded6d93a3b6e83e4a7e1e2458.zip chromium_src-0c9406639ed7eadded6d93a3b6e83e4a7e1e2458.tar.gz chromium_src-0c9406639ed7eadded6d93a3b6e83e4a7e1e2458.tar.bz2 |
InstantExtended: Committed NTP
Use separate dedicated WebContents for the NTP and Instant overlay (both residing in the Instant process). Intercept any navigations to the New Tab Page and swap in the preloaded Instant NTP contents when available.
BUG=168828,174498,174498
Review URL: https://chromiumcodereview.appspot.com/11824050
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@181397 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/ui/browser_instant_controller.cc')
-rw-r--r-- | chrome/browser/ui/browser_instant_controller.cc | 94 |
1 files changed, 66 insertions, 28 deletions
diff --git a/chrome/browser/ui/browser_instant_controller.cc b/chrome/browser/ui/browser_instant_controller.cc index 49d93da..ae5c0b0 100644 --- a/chrome/browser/ui/browser_instant_controller.cc +++ b/chrome/browser/ui/browser_instant_controller.cc @@ -19,6 +19,7 @@ #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h" #include "chrome/common/chrome_notification_types.h" #include "chrome/common/pref_names.h" +#include "chrome/common/url_constants.h" #include "content/public/browser/notification_service.h" #include "grit/theme_resources.h" #include "ui/gfx/color_utils.h" @@ -39,13 +40,13 @@ namespace chrome { BrowserInstantController::BrowserInstantController(Browser* browser) : browser_(browser), instant_(ALLOW_THIS_IN_INITIALIZER_LIST(this), - chrome::search::IsInstantExtendedAPIEnabled(browser->profile())), + chrome::search::IsInstantExtendedAPIEnabled(profile())), instant_unload_handler_(browser), initialized_theme_info_(false), theme_area_height_(0) { - profile_pref_registrar_.Init(browser_->profile()->GetPrefs()); + profile_pref_registrar_.Init(profile()->GetPrefs()); profile_pref_registrar_.Add( - GetInstantPrefName(browser_->profile()), + GetInstantPrefName(profile()), base::Bind(&BrowserInstantController::ResetInstant, base::Unretained(this))); profile_pref_registrar_.Add( @@ -59,7 +60,7 @@ BrowserInstantController::BrowserInstantController(Browser* browser) // Listen for theme installation. registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, content::Source<ThemeService>( - ThemeServiceFactory::GetForProfile(browser_->profile()))); + ThemeServiceFactory::GetForProfile(profile()))); #endif // defined(ENABLE_THEMES) } @@ -98,6 +99,33 @@ void BrowserInstantController::RegisterUserPrefs(PrefServiceSyncable* prefs) { PrefServiceSyncable::SYNCABLE_PREF); } +bool BrowserInstantController::MaybeSwapInInstantNTPContents( + 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) @@ -113,31 +141,43 @@ bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition) { INSTANT_COMMIT_PRESSED_ENTER : INSTANT_COMMIT_PRESSED_ALT_ENTER); } -void BrowserInstantController::CommitInstant(content::WebContents* preview, - bool in_new_tab) { +Profile* BrowserInstantController::profile() const { + return browser_->profile(); +} + +void BrowserInstantController::CommitInstant( + scoped_ptr<content::WebContents> preview, + bool in_new_tab) { + if (profile()->GetExtensionService()->IsInstalledApp(preview->GetURL())) { + AppLauncherHandler::RecordAppLaunchType( + extension_misc::APP_LAUNCH_OMNIBOX_INSTANT); + } 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(); - DCHECK(browser_->profile()->GetExtensionService()); - if (browser_->profile()->GetExtensionService()->IsInstalledApp(url)) { - AppLauncherHandler::RecordAppLaunchType( - extension_misc::APP_LAUNCH_OMNIBOX_INSTANT); - } + ReplaceWebContentsAt( + browser_->tab_strip_model()->active_index(), + preview.Pass()); } } +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); @@ -180,7 +220,7 @@ void BrowserInstantController::UpdateThemeInfoForPreview() { // Initialize |theme_info| if necessary. // |OnThemeChanged| also updates theme area height if necessary. if (!initialized_theme_info_) - OnThemeChanged(ThemeServiceFactory::GetForProfile(browser_->profile())); + OnThemeChanged(ThemeServiceFactory::GetForProfile(profile())); else OnThemeChanged(NULL); } @@ -200,12 +240,10 @@ void BrowserInstantController::SetMarginSize(int start, int end) { } void BrowserInstantController::ResetInstant() { - Profile* profile = browser_->profile(); - - bool instant_enabled = IsInstantEnabled(profile); - bool use_local_preview_only = profile->IsOffTheRecord() || + bool instant_enabled = IsInstantEnabled(profile()); + bool use_local_preview_only = profile()->IsOffTheRecord() || (!instant_enabled && - !profile->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled)); + !profile()->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled)); instant_.SetInstantEnabled(instant_enabled, use_local_preview_only); } |