diff options
author | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-02 15:15:33 +0000 |
---|---|---|
committer | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-02 15:15:33 +0000 |
commit | 616381f0a55322de447465573e39612e5af18b31 (patch) | |
tree | 4cbf687bb163f3dcd95e55e7be7fc9197e0dd8fd | |
parent | e5925962c61dbb9c8f5a5425ca2dc2953fb6a3e1 (diff) | |
download | chromium_src-616381f0a55322de447465573e39612e5af18b31.zip chromium_src-616381f0a55322de447465573e39612e5af18b31.tar.gz chromium_src-616381f0a55322de447465573e39612e5af18b31.tar.bz2 |
Make all browser code use browser::Navigate to open tabs.
BUG=none
TEST=existing unittests. Also, test all places where new tabs and windows are opened from UI, e.g. links in options, new tab button, Ctrl+T, popup windows, etc.
Review URL: http://codereview.chromium.org/3834002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@64745 0039d316-1c4b-4281-b951-d872f2087c98
26 files changed, 289 insertions, 511 deletions
diff --git a/chrome/browser/autocomplete/autocomplete_browsertest.cc b/chrome/browser/autocomplete/autocomplete_browsertest.cc index f44191d..877fe7a 100644 --- a/chrome/browser/autocomplete/autocomplete_browsertest.cc +++ b/chrome/browser/autocomplete/autocomplete_browsertest.cc @@ -140,10 +140,8 @@ IN_PROC_BROWSER_TEST_F(AutocompleteBrowserTest, TabAwayRevertSelect) { EXPECT_EQ(UTF8ToWide(chrome::kAboutBlankURL), location_bar->location_entry()->GetText()); location_bar->location_entry()->SetUserText(L""); - Browser::AddTabWithURLParams params(GURL(chrome::kAboutBlankURL), - PageTransition::START_PAGE); - browser()->AddTabWithURL(¶ms); - EXPECT_EQ(browser(), params.target); + browser()->AddSelectedTabWithURL(GURL(chrome::kAboutBlankURL), + PageTransition::START_PAGE); ui_test_utils::WaitForNavigation( &browser()->GetSelectedTabContents()->controller()); EXPECT_EQ(UTF8ToWide(chrome::kAboutBlankURL), diff --git a/chrome/browser/browser.cc b/chrome/browser/browser.cc index 8c70ffa..11633d0 100644 --- a/chrome/browser/browser.cc +++ b/chrome/browser/browser.cc @@ -32,6 +32,7 @@ #include "chrome/browser/bookmarks/bookmark_model.h" #include "chrome/browser/bookmarks/bookmark_utils.h" #include "chrome/browser/browser_list.h" +#include "chrome/browser/browser_navigator.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/browser_shutdown.h" #include "chrome/browser/browser_window.h" @@ -610,9 +611,9 @@ TabContents* Browser::OpenApplicationTab(Profile* profile, // TODO(erikkay): START_PAGE doesn't seem like the right transition in all // cases. - AddTabWithURLParams params(extension->GetFullLaunchURL(), - PageTransition::START_PAGE); - params.add_types = add_type; + browser::NavigateParams params(browser, extension->GetFullLaunchURL(), + PageTransition::START_PAGE); + params.tabstrip_add_types = add_type; // Launch the application in the existing TabContents, if it was supplied. if (existing_tab) { @@ -621,17 +622,18 @@ TabContents* Browser::OpenApplicationTab(Profile* profile, existing_tab->OpenURL(extension->GetFullLaunchURL(), existing_tab->GetURL(), CURRENT_TAB, PageTransition::LINK); - if (params.add_types & TabStripModel::ADD_PINNED) { - // Pin the tab and fetch its index, since it may move when being pinned. + if (params.tabstrip_add_types & TabStripModel::ADD_PINNED) { model->SetTabPinned(tab_index, true); tab_index = model->GetIndexOfTabContents(existing_tab); } - if (params.add_types & TabStripModel::ADD_SELECTED) + if (params.tabstrip_add_types & TabStripModel::ADD_SELECTED) model->SelectTabContentsAt(tab_index, true); contents = existing_tab; } else { - contents = browser->AddTabWithURL(¶ms); + params.disposition = NEW_FOREGROUND_TAB; + browser::Navigate(¶ms); + contents = params.target_contents; } if (launch_type == ExtensionPrefs::LAUNCH_FULLSCREEN) @@ -919,57 +921,10 @@ int Browser::GetIndexForInsertionDuringRestore(int relative_index) { TabContents* Browser::AddSelectedTabWithURL(const GURL& url, PageTransition::Type transition) { - AddTabWithURLParams params(url, transition); - return AddTabWithURL(¶ms); -} - -Browser::AddTabWithURLParams::AddTabWithURLParams( - const GURL& a_url, - PageTransition::Type a_transition) - : url(a_url), - transition(a_transition), - index(-1), - add_types(TabStripModel::ADD_SELECTED), - instance(NULL), - target(NULL) { -} - -Browser::AddTabWithURLParams::~AddTabWithURLParams() { -} - -TabContents* Browser::AddTabWithURL(AddTabWithURLParams* params) { - TabContents* contents = NULL; - if (CanSupportWindowFeature(FEATURE_TABSTRIP) || tabstrip_model()->empty()) { - GURL url_to_load = params->url; - if (url_to_load.is_empty()) - url_to_load = GetHomePage(); - contents = CreateTabContentsForURL(url_to_load, params->referrer, profile_, - params->transition, false, - params->instance); - contents->SetExtensionAppById(params->extension_app_id); - tab_handler_->GetTabStripModel()->AddTabContents(contents, params->index, - params->transition, - params->add_types); - // TODO(sky): figure out why this is needed. Without it we seem to get - // failures in startup tests. - // By default, content believes it is not hidden. When adding contents - // in the background, tell it that it's hidden. - if ((params->add_types & TabStripModel::ADD_SELECTED) == 0) { - // TabStripModel::AddTabContents invokes HideContents if not foreground. - contents->WasHidden(); - } - params->target = this; - } else { - // We're in an app window or a popup window. Find an existing browser to - // open this URL in, creating one if none exists. - Browser* browser = - BrowserList::FindBrowserWithFeature(profile_, FEATURE_TABSTRIP); - if (!browser) - browser = Browser::Create(profile_); - contents = browser->AddTabWithURL(params); - DCHECK(params->target); - } - return contents; + browser::NavigateParams params(this, url, transition); + params.disposition = NEW_FOREGROUND_TAB; + browser::Navigate(¶ms); + return params.target_contents; } TabContents* Browser::AddTab(TabContents* tab_contents, @@ -1081,27 +1036,10 @@ bool Browser::NavigateToIndexWithDisposition(int index, } void Browser::ShowSingletonTab(const GURL& url) { - // In case the URL was rewritten by the BrowserURLHandler we need to ensure - // that we do not open another URL that will get redirected to the rewritten - // URL. - GURL rewritten_url(url); - bool reverse_on_redirect = false; - BrowserURLHandler::RewriteURLIfNecessary(&rewritten_url, profile_, - &reverse_on_redirect); - - // See if we already have a tab with the given URL and select it if so. - TabStripModel* model = tab_handler_->GetTabStripModel(); - for (int i = 0; i < model->count(); i++) { - TabContents* tc = model->GetTabContentsAt(i); - if (CompareURLsIgnoreRef(tc->GetURL(), url) || - CompareURLsIgnoreRef(tc->GetURL(), rewritten_url)) { - model->SelectTabContentsAt(i, false); - return; - } - } - - // Otherwise, just create a new tab. - AddSelectedTabWithURL(url, PageTransition::AUTO_BOOKMARK); + browser::NavigateParams params(this, url, PageTransition::AUTO_BOOKMARK); + params.disposition = SINGLETON_TAB; + params.show_window = true; + browser::Navigate(¶ms); } void Browser::UpdateCommandsForFullscreenMode(bool is_fullscreen) { @@ -1303,15 +1241,15 @@ void Browser::OpenCurrentURL() { return; GURL url(WideToUTF8(location_bar->GetInputString())); - + browser::NavigateParams params(this, url, location_bar->GetPageTransition()); + params.disposition = open_disposition; // Use ADD_INHERIT_OPENER so that all pages opened by the omnibox at least // inherit the opener. In some cases the tabstrip will determine the group // should be inherited, in which case the group is inherited instead of the // opener. - OpenURLAtIndex(NULL, url, GURL(), open_disposition, - location_bar->GetPageTransition(), -1, - TabStripModel::ADD_FORCE_INDEX | - TabStripModel::ADD_INHERIT_OPENER); + params.tabstrip_add_types = + TabStripModel::ADD_FORCE_INDEX | TabStripModel::ADD_INHERIT_OPENER; + browser::Navigate(¶ms); } void Browser::Stop() { @@ -1828,8 +1766,9 @@ void Browser::ShowOptionsTab(const std::string& sub_page) { // this may cause us to unnecessarily reload the same page. We can't // really detect that unless the options page is permitted to change the // URL in the address bar, but security policy doesn't allow that. - OpenURLAtIndex(tc, url, GURL(), CURRENT_TAB, PageTransition::GENERATED, - -1, -1); + browser::NavigateParams params(this, url, PageTransition::GENERATED); + params.source_contents = tc; + browser::Navigate(¶ms); model->SelectTabContentsAt(i, false); return; } @@ -1919,9 +1858,8 @@ void Browser::OpenHelpTab() { } void Browser::OpenThemeGalleryTabAndActivate() { - OpenURL(GURL(l10n_util::GetStringUTF8(IDS_THEMES_GALLERY_URL)), - GURL(), NEW_FOREGROUND_TAB, PageTransition::LINK); - window_->Activate(); + AddSelectedTabWithURL(GURL(l10n_util::GetStringUTF8(IDS_THEMES_GALLERY_URL)), + PageTransition::LINK); } void Browser::OpenPrivacyDashboardTabAndActivate() { @@ -1931,9 +1869,8 @@ void Browser::OpenPrivacyDashboardTabAndActivate() { } void Browser::OpenAutoFillHelpTabAndActivate() { - OpenURL(GURL(l10n_util::GetStringUTF8(IDS_AUTOFILL_HELP_URL)), - GURL(), NEW_FOREGROUND_TAB, PageTransition::LINK); - window_->Activate(); + AddSelectedTabWithURL(GURL(l10n_util::GetStringUTF8(IDS_AUTOFILL_HELP_URL)), + PageTransition::LINK); } void Browser::OpenSearchEngineOptionsDialog() { @@ -2411,14 +2348,15 @@ TabContents* Browser::AddBlankTabAt(int index, bool foreground) { // TabContents, but we want to include the time it takes to create the // TabContents object too. base::TimeTicks new_tab_start_time = base::TimeTicks::Now(); - AddTabWithURLParams params(GURL(chrome::kChromeUINewTabURL), - PageTransition::TYPED); - params.add_types = + browser::NavigateParams params(this, GURL(chrome::kChromeUINewTabURL), + PageTransition::TYPED); + params.tabstrip_add_types = foreground ? TabStripModel::ADD_SELECTED : TabStripModel::ADD_NONE; - params.index = index; - TabContents* contents = AddTabWithURL(¶ms); - contents->set_new_tab_start_time(new_tab_start_time); - return contents; + params.disposition = foreground ? NEW_FOREGROUND_TAB : NEW_BACKGROUND_TAB; + params.tabstrip_index = index; + browser::Navigate(¶ms); + params.target_contents->set_new_tab_start_time(new_tab_start_time); + return params.target_contents; } Browser* Browser::CreateNewStripWithContents(TabContents* detached_contents, @@ -2774,8 +2712,12 @@ void Browser::OpenURLFromTab(TabContents* source, const GURL& referrer, WindowOpenDisposition disposition, PageTransition::Type transition) { - OpenURLAtIndex(source, url, referrer, disposition, transition, -1, - TabStripModel::ADD_NONE); + browser::NavigateParams params(this, url, transition); + params.source_contents = source; + params.referrer = referrer; + params.disposition = disposition; + params.tabstrip_add_types = TabStripModel::ADD_NONE; + browser::Navigate(¶ms); } void Browser::NavigationStateChanged(const TabContents* source, @@ -2795,73 +2737,34 @@ void Browser::AddNewContents(TabContents* source, WindowOpenDisposition disposition, const gfx::Rect& initial_pos, bool user_gesture) { - DCHECK(disposition != SAVE_TO_DISK); // No code for this yet - DCHECK(disposition != CURRENT_TAB); // Can't create a new contents for the - // current tab. + // No code for this yet + DCHECK(disposition != SAVE_TO_DISK); + // Can't create a new contents for the current tab - invalid case. + DCHECK(disposition != CURRENT_TAB); + // TODO(beng): This belongs behind the platform-specific View interface. + // That's why it's there. #if defined(OS_CHROMEOS) - if (disposition == NEW_POPUP) { - // If the popup is bigger than a given factor of the screen, then - // turn it into a foreground tab (on chrome os only) - // Also check for width or height == 0, which would otherwise indicate - // a tab sized popup window. - GdkScreen* screen = gdk_screen_get_default(); - int max_width = gdk_screen_get_width(screen) * kPopupMaxWidthFactor; - int max_height = gdk_screen_get_height(screen) * kPopupMaxHeightFactor; - if (initial_pos.width() > max_width || initial_pos.width() == 0 || - initial_pos.height() > max_height || initial_pos.height() == 0) { - disposition = NEW_FOREGROUND_TAB; - } - } -#endif - - // If this is a window with no tabstrip, we can only have one tab so we need - // to process this in tabbed browser window. - if (!CanSupportWindowFeature(FEATURE_TABSTRIP) && - tab_handler_->GetTabStripModel()->count() > 0 && - disposition != NEW_WINDOW && - disposition != NEW_POPUP) { - Browser* b = GetOrCreateTabbedBrowser(profile_); - DCHECK(b); - PageTransition::Type transition = PageTransition::LINK; - // If we were called from an "installed webapp" we want to emulate the code - // that is run from browser_init.cc for links from external applications. - // This means we need to open the tab with the START PAGE transition. - // AddNewContents doesn't support this but the TabStripModel's - // AddTabContents method does. - if (type_ & TYPE_APP) - transition = PageTransition::START_PAGE; - b->tabstrip_model()->AddTabContents( - new_contents, -1, transition, TabStripModel::ADD_SELECTED); - b->window()->Show(); - return; - } - if (disposition == NEW_POPUP) { - Type browser_type; - if ((type_ & TYPE_APP) || (source && source->is_app())) { - // New app popup, or an app is creating a popup. - browser_type = TYPE_APP_POPUP; - } else { - browser_type = TYPE_POPUP; + // If the popup is bigger than a given factor of the screen, then + // turn it into a foreground tab (on chrome os only) + // Also check for width or height == 0, which would otherwise indicate + // a tab sized popup window. + GdkScreen* screen = gdk_screen_get_default(); + int max_width = gdk_screen_get_width(screen) * kPopupMaxWidthFactor; + int max_height = gdk_screen_get_height(screen) * kPopupMaxHeightFactor; + if (initial_pos.width() > max_width || initial_pos.width() == 0 || + initial_pos.height() > max_height || initial_pos.height() == 0) { + disposition = NEW_FOREGROUND_TAB; } - Browser* browser = Browser::CreateForPopup(browser_type, - profile_, - new_contents, - initial_pos); - browser->window()->Show(); - - } else if (disposition == NEW_WINDOW) { - Browser* browser = Browser::Create(profile_); - browser->AddNewContents(source, new_contents, NEW_FOREGROUND_TAB, - initial_pos, user_gesture); - browser->window()->Show(); - } else if (disposition != SUPPRESS_OPEN) { - tab_handler_->GetTabStripModel()->AddTabContents( - new_contents, -1, PageTransition::LINK, - disposition == NEW_FOREGROUND_TAB ? TabStripModel::ADD_SELECTED : - TabStripModel::ADD_NONE); } +#endif + + browser::NavigateParams params(this, new_contents); + params.source_contents = source; + params.disposition = disposition; + params.window_bounds = initial_pos; + browser::Navigate(¶ms); } void Browser::ActivateContents(TabContents* contents) { @@ -4072,138 +3975,6 @@ Browser* Browser::GetOrCreateTabbedBrowser(Profile* profile) { return browser; } -void Browser::OpenURLAtIndex(TabContents* source, - const GURL& url, - const GURL& referrer, - WindowOpenDisposition disposition, - PageTransition::Type transition, - int index, - int add_types) { - // TODO(beng): Move all this code into a separate helper that has unit tests. - - // No code for these yet - DCHECK((disposition != NEW_POPUP) && (disposition != SAVE_TO_DISK)); - - TabContents* current_tab = source ? source : GetSelectedTabContents(); - bool source_tab_was_frontmost = (current_tab == GetSelectedTabContents()); - TabContents* new_contents = NULL; - - // Opening a bookmark counts as a user gesture, so we don't need to avoid - // carpet-bombing here. - PageTransition::Type baseTransitionType = - PageTransition::StripQualifier(transition); - if ((baseTransitionType == PageTransition::TYPED || - baseTransitionType == PageTransition::AUTO_BOOKMARK) && - current_tab != NULL) { - RenderViewHostDelegate::BrowserIntegration* delegate = current_tab; - delegate->OnUserGesture(); - } - - // If the URL is part of the same web site, then load it in the same - // SiteInstance (and thus the same process). This is an optimization to - // reduce process overhead; it is not necessary for compatibility. (That is, - // the new tab will not have script connections to the previous tab, so it - // does not need to be part of the same SiteInstance or BrowsingInstance.) - // Default to loading in a new SiteInstance and BrowsingInstance. - // TODO(creis): should this apply to applications? - SiteInstance* instance = NULL; - // Don't use this logic when "--process-per-tab" is specified. - if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kProcessPerTab)) { - if (current_tab) { - const GURL& current_url = current_tab->GetURL(); - if (SiteInstance::IsSameWebSite(profile_, current_url, url)) - instance = current_tab->GetSiteInstance(); - } - } - - // If this browser doeesn't support tabs, we can only have one tab so a new - // tab always goes into a tabbed browser window. - if (!CanSupportWindowFeature(FEATURE_TABSTRIP) && - disposition != CURRENT_TAB && disposition != NEW_WINDOW) { - // If the disposition is OFF_THE_RECORD we don't want to create a new - // browser that will itself create another OTR browser. This will result in - // a browser leak (and crash below because no tab is created or selected). - if (disposition == OFF_THE_RECORD) { - OpenURLOffTheRecord(profile_, url); - return; - } - - Browser* b = GetOrCreateTabbedBrowser(profile_); - DCHECK(b); - - // If we have just created a new browser window, make sure we select the - // tab. - if (b->tab_count() == 0 && disposition == NEW_BACKGROUND_TAB) - disposition = NEW_FOREGROUND_TAB; - - b->OpenURL(url, referrer, disposition, transition); - b->window()->Show(); - return; - } - - if (profile_->IsOffTheRecord() && disposition == OFF_THE_RECORD) - disposition = NEW_FOREGROUND_TAB; - - if (disposition == SINGLETON_TAB) { - ShowSingletonTab(url); - return; - } else if (disposition == NEW_WINDOW) { - Browser* browser = Browser::Create(profile_); - AddTabWithURLParams params(url, transition); - params.referrer = referrer; - params.index = index; - params.add_types = TabStripModel::ADD_SELECTED | add_types; - params.instance = instance; - new_contents = browser->AddTabWithURL(¶ms); - browser->window()->Show(); - } else if ((disposition == CURRENT_TAB) && current_tab) { - tab_handler_->GetTabStripModel()->TabNavigating(current_tab, transition); - - bool user_initiated = (PageTransition::StripQualifier(transition) == - PageTransition::AUTO_BOOKMARK); - - if (user_initiated && source_tab_was_frontmost && - window_->GetLocationBar()) { - // Forcibly reset the location bar if the url is going to change in the - // current tab, since otherwise it won't discard any ongoing user edits, - // since it doesn't realize this is a user-initiated action. - window_->GetLocationBar()->Revert(); - } - - current_tab->controller().LoadURL(url, referrer, transition); - new_contents = current_tab; - if (GetStatusBubble()) - GetStatusBubble()->Hide(); - - // Update the location bar. This is synchronous. We specifically don't - // update the load state since the load hasn't started yet and updating it - // will put it out of sync with the actual state like whether we're - // displaying a favicon, which controls the throbber. If we updated it here, - // the throbber will show the default favicon for a split second when - // navigating away from the new tab page. - ScheduleUIUpdate(current_tab, TabContents::INVALIDATE_URL); - } else if (disposition == OFF_THE_RECORD) { - OpenURLOffTheRecord(profile_, url); - return; - } else if (disposition != SUPPRESS_OPEN) { - if (disposition != NEW_BACKGROUND_TAB) - add_types |= TabStripModel::ADD_SELECTED; - AddTabWithURLParams params(url, transition); - params.referrer = referrer; - params.index = index; - params.add_types = add_types; - params.instance = instance; - new_contents = AddTabWithURL(¶ms); - } - - if (disposition != NEW_BACKGROUND_TAB && source_tab_was_frontmost && - new_contents) { - // Give the focus to the newly navigated tab, if the source tab was - // front-most. - new_contents->Focus(); - } -} - void Browser::FindInPage(bool find_next, bool forward_direction) { ShowFindBar(); if (find_next) { diff --git a/chrome/browser/browser.h b/chrome/browser/browser.h index 5654106..7d89334 100644 --- a/chrome/browser/browser.h +++ b/chrome/browser/browser.h @@ -343,40 +343,6 @@ class Browser : public TabHandlerDelegate, TabContents* AddSelectedTabWithURL(const GURL& url, PageTransition::Type transition); - // Parameters for AddTabWithURL. - struct AddTabWithURLParams { - AddTabWithURLParams(const GURL& a_url, PageTransition::Type a_transition); - ~AddTabWithURLParams(); - - // Basic configuration. - GURL url; - GURL referrer; - PageTransition::Type transition; - - // The index to open the tab at. This could be ignored by the tabstrip - // depending on the combination of |add_types| specified. - int index; - - // A bitmask of values defined in TabStripModel::AddTabTypes. - // The default is ADD_SELECTED. - int add_types; - - // If non-NULL, used to render the tab. - SiteInstance* instance; - - // If non-empty, the new tab is an app tab. - std::string extension_app_id; - - // The browser where the tab was added. - Browser* target; - - private: - AddTabWithURLParams(); - }; - - // Adds a tab to the browser (or another) and returns the created TabContents. - TabContents* AddTabWithURL(AddTabWithURLParams* params); - // Add a new tab, given a TabContents. A TabContents appropriate to // display the last committed entry is created and returned. TabContents* AddTab(TabContents* tab_contents, PageTransition::Type type); @@ -928,20 +894,6 @@ class Browser : public TabHandlerDelegate, WindowOpenDisposition *disposition, PageTransition::Type transition); - // The low-level function that other OpenURL...() functions call. This - // determines the appropriate SiteInstance to pass to AddTabWithURL(), focuses - // the newly created tab as needed, and does other miscellaneous housekeeping. - // |add_types| is a bitmask of TabStripModel::AddTabTypes and may be used to - // pass specific flags to the TabStripModel. |add_types| is only used if - // the disposition is NEW_WINDOW or SUPPRESS_OPEN. - void OpenURLAtIndex(TabContents* source, - const GURL& url, - const GURL& referrer, - WindowOpenDisposition disposition, - PageTransition::Type transition, - int index, - int add_types); - // Shows the Find Bar, optionally selecting the next entry that matches the // existing search string for that Tab. |forward_direction| controls the // search direction. diff --git a/chrome/browser/browser_browsertest.cc b/chrome/browser/browser_browsertest.cc index 6c57f93..317695e 100644 --- a/chrome/browser/browser_browsertest.cc +++ b/chrome/browser/browser_browsertest.cc @@ -14,6 +14,7 @@ #include "chrome/browser/browser.h" #include "chrome/browser/browser_init.h" #include "chrome/browser/browser_list.h" +#include "chrome/browser/browser_navigator.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/browser_window.h" #include "chrome/browser/defaults.h" @@ -206,10 +207,7 @@ IN_PROC_BROWSER_TEST_F(BrowserTest, MAYBE_JavascriptAlertActivatesTab) { GURL url(ui_test_utils::GetTestUrl(FilePath(FilePath::kCurrentDirectory), FilePath(kTitle1File))); ui_test_utils::NavigateToURL(browser(), url); - Browser::AddTabWithURLParams params(url, PageTransition::TYPED); - params.index = 0; - browser()->AddTabWithURL(¶ms); - EXPECT_EQ(browser(), params.target); + AddTabAtIndex(0, url, PageTransition::TYPED); EXPECT_EQ(2, browser()->tab_count()); EXPECT_EQ(0, browser()->selected_index()); TabContents* second_tab = browser()->GetTabContentsAt(1); @@ -233,12 +231,8 @@ IN_PROC_BROWSER_TEST_F(BrowserTest, ThirtyFourTabs) { FilePath(kTitle2File))); // There is one initial tab. - for (int ix = 0; ix != 33; ++ix) { - Browser::AddTabWithURLParams params(url, PageTransition::TYPED); - params.index = 0; - browser()->AddTabWithURL(¶ms); - EXPECT_EQ(browser(), params.target); - } + for (int ix = 0; ix != 33; ++ix) + browser()->AddSelectedTabWithURL(url, PageTransition::TYPED); EXPECT_EQ(34, browser()->tab_count()); // See browser\renderer_host\render_process_host.cc for the algorithm to diff --git a/chrome/browser/browser_focus_uitest.cc b/chrome/browser/browser_focus_uitest.cc index 1099103..d9771db 100644 --- a/chrome/browser/browser_focus_uitest.cc +++ b/chrome/browser/browser_focus_uitest.cc @@ -217,11 +217,8 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabsRememberFocus) { ui_test_utils::NavigateToURL(browser(), url); // Create several tabs. - for (int i = 0; i < 4; ++i) { - Browser::AddTabWithURLParams params(url, PageTransition::TYPED); - browser()->AddTabWithURL(¶ms); - EXPECT_EQ(browser(), params.target); - } + for (int i = 0; i < 4; ++i) + browser()->AddSelectedTabWithURL(url, PageTransition::TYPED); // Alternate focus for the tab. const bool kFocusPage[3][5] = { @@ -296,9 +293,7 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) { browser()->FocusLocationBar(); // Create a 2nd tab. - Browser::AddTabWithURLParams params(url, PageTransition::TYPED); - browser()->AddTabWithURL(¶ms); - EXPECT_EQ(browser(), params.target); + browser()->AddSelectedTabWithURL(url, PageTransition::TYPED); // Focus should be on the recently opened tab page. ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); diff --git a/chrome/browser/browser_init.cc b/chrome/browser/browser_init.cc index 228f092..15bfb0b 100644 --- a/chrome/browser/browser_init.cc +++ b/chrome/browser/browser_init.cc @@ -22,6 +22,7 @@ #include "chrome/browser/automation/chrome_frame_automation_provider.h" #include "chrome/browser/automation/testing_automation_provider.h" #include "chrome/browser/browser_list.h" +#include "chrome/browser/browser_navigator.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/browser_thread.h" #include "chrome/browser/browser_window.h" @@ -765,16 +766,17 @@ Browser* BrowserInit::LaunchWithProfile::OpenTabsInBrowser( add_types |= TabStripModel::ADD_PINNED; int index = browser->GetIndexForInsertionDuringRestore(i); - Browser::AddTabWithURLParams params(tabs[i].url, - PageTransition::START_PAGE); - params.index = index; - params.add_types = add_types; + browser::NavigateParams params(browser, tabs[i].url, + PageTransition::START_PAGE); + params.disposition = first_tab ? NEW_FOREGROUND_TAB : NEW_BACKGROUND_TAB; + params.tabstrip_index = index; + params.tabstrip_add_types = add_types; params.extension_app_id = tabs[i].app_id; - TabContents* tab = browser->AddTabWithURL(¶ms); + browser::Navigate(¶ms); if (profile_ && first_tab && process_startup) { - AddCrashedInfoBarIfNecessary(tab); - AddBadFlagsInfoBarIfNecessary(tab); + AddCrashedInfoBarIfNecessary(params.target_contents); + AddBadFlagsInfoBarIfNecessary(params.target_contents); } first_tab = false; diff --git a/chrome/browser/browser_navigator.cc b/chrome/browser/browser_navigator.cc index 2950d1f..d0791ba 100644 --- a/chrome/browser/browser_navigator.cc +++ b/chrome/browser/browser_navigator.cc @@ -22,6 +22,9 @@ namespace { // Returns the SiteInstance for |source_contents| if it represents the same // website as |url|, or NULL otherwise. |source_contents| cannot be NULL. SiteInstance* GetSiteInstance(TabContents* source_contents, const GURL& url) { + if (!source_contents) + return NULL; + // Don't use this logic when "--process-per-tab" is specified. if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kProcessPerTab) && SiteInstance::IsSameWebSite(source_contents->profile(), @@ -149,7 +152,8 @@ Browser* GetBrowserForDisposition(browser::NavigateParams* params) { void NormalizeDisposition(browser::NavigateParams* params) { // Calculate the WindowOpenDisposition if necessary. if (params->browser->tabstrip_model()->empty() && - params->disposition == NEW_BACKGROUND_TAB) { + (params->disposition == NEW_BACKGROUND_TAB || + params->disposition == CURRENT_TAB)) { params->disposition = NEW_FOREGROUND_TAB; } if (params->browser->profile()->IsOffTheRecord() && @@ -162,6 +166,13 @@ void NormalizeDisposition(browser::NavigateParams* params) { // background. if (params->disposition == NEW_BACKGROUND_TAB) params->tabstrip_add_types &= ~TabStripModel::ADD_SELECTED; + + // Code that wants to open a new window typically expects it to be shown + // automatically. + if (params->disposition == NEW_WINDOW || params->disposition == NEW_POPUP) { + params->show_window = true; + params->tabstrip_add_types |= TabStripModel::ADD_SELECTED; + } } // This class makes sure the Browser object held in |params| is made visible @@ -266,6 +277,13 @@ void Navigate(NavigateParams* params) { // Some dispositions need coercion to base types. NormalizeDisposition(params); + // Determine if the navigation was user initiated. If it was, we need to + // inform the target TabContents, and we may need to update the UI. + PageTransition::Type base_transition = + PageTransition::StripQualifier(params->transition); + bool user_initiated = base_transition == PageTransition::TYPED || + base_transition == PageTransition::AUTO_BOOKMARK; + // If no target TabContents was specified, we need to construct one if we are // supposed to target a new tab. if (!params->target_contents) { @@ -293,25 +311,23 @@ void Navigate(NavigateParams* params) { // same as the source. params->target_contents = params->source_contents; } - } - // Determine if the navigation was user initiated. If it was, we need to - // inform the target TabContents, and we may need to update the UI. - PageTransition::Type base_transition = - PageTransition::StripQualifier(params->transition); - bool user_initiated = base_transition == PageTransition::TYPED || - base_transition == PageTransition::AUTO_BOOKMARK; - if (user_initiated) { - RenderViewHostDelegate::BrowserIntegration* integration = - params->target_contents; - integration->OnUserGesture(); - } + if (user_initiated) { + RenderViewHostDelegate::BrowserIntegration* integration = + params->target_contents; + integration->OnUserGesture(); + } - // Perform the actual navigation. - GURL url = params->url.is_empty() ? params->browser->GetHomePage() - : params->url; - params->target_contents->controller().LoadURL(url, params->referrer, - params->transition); + // Perform the actual navigation. + GURL url = params->url.is_empty() ? params->browser->GetHomePage() + : params->url; + params->target_contents->controller().LoadURL(url, params->referrer, + params->transition); + } else { + // |target_contents| was specified non-NULL, and so we assume it has already + // been navigated appropriately. We need to do nothing more other than + // add it to the appropriate tabstrip. + } if (params->source_contents == params->target_contents) { // The navigation occurred in the source tab, so update the UI. @@ -325,6 +341,11 @@ void Navigate(NavigateParams* params) { // The navigation should re-select an existing tab in the target Browser. params->browser->SelectTabContentsAt(singleton_index, user_initiated); } else { + // If some non-default value is set for the index, we should tell the + // TabStripModel to respect it. + if (params->tabstrip_index != -1) + params->tabstrip_add_types |= TabStripModel::ADD_FORCE_INDEX; + // The navigation should insert a new tab into the target Browser. params->browser->tabstrip_model()->AddTabContents( params->target_contents, diff --git a/chrome/browser/browser_navigator.h b/chrome/browser/browser_navigator.h index 277830ef..c770d8a 100644 --- a/chrome/browser/browser_navigator.h +++ b/chrome/browser/browser_navigator.h @@ -47,13 +47,15 @@ struct NavigateParams { NavigateParams(Browser* browser, TabContents* a_target_contents); ~NavigateParams(); - // The URL/referrer to be loaded. Can be empty if |contents| is specified - // non-NULL. + // The URL/referrer to be loaded. Ignored if |target_contents| is non-NULL. GURL url; GURL referrer; // [in] A TabContents to be navigated or inserted into the target Browser's - // tabstrip. If NULL, |url| or the homepage will be used instead. + // tabstrip. If NULL, |url| or the homepage will be used instead. When + // non-NULL, Navigate() assumes it has already been navigated to its + // intended destination and will not load any URL in it (i.e. |url| is + // ignored). // Default is NULL. // [out] The TabContents in which the navigation occurred or that was // inserted. Guaranteed non-NULL except for note below: @@ -70,7 +72,18 @@ struct NavigateParams { TabContents* source_contents; // The disposition requested by the navigation source. Default is - // CURRENT_TAB. + // CURRENT_TAB. What follows is a set of coercions that happen to this value + // when other factors are at play: + // + // [in]: Condition: [out]: + // NEW_BACKGROUND_TAB target browser tabstrip is empty NEW_FOREGROUND_TAB + // CURRENT_TAB " " " NEW_FOREGROUND_TAB + // OFF_THE_RECORD target browser profile is incog. NEW_FOREGROUND_TAB + // + // If disposition is NEW_WINDOW or NEW_POPUP, |show_window| is set to true + // automatically. + // If disposition is NEW_BACKGROUND_TAB, TabStripModel::ADD_SELECTED is + // removed from |tabstrip_add_types| automatically. WindowOpenDisposition disposition; // The transition type of the navigation. Default is PageTransition::LINK @@ -99,7 +112,8 @@ struct NavigateParams { gfx::Rect window_bounds; // True if the target window should be made visible at the end of the call - // to Navigate(). Default is false. + // to Navigate(). This activates the window if it was already visible. + // Default is false. bool show_window; // [in] Specifies a Browser object where the navigation could occur or the diff --git a/chrome/browser/chromeos/tab_closeable_state_watcher_browsertest.cc b/chrome/browser/chromeos/tab_closeable_state_watcher_browsertest.cc index b9399de..97362b7 100644 --- a/chrome/browser/chromeos/tab_closeable_state_watcher_browsertest.cc +++ b/chrome/browser/chromeos/tab_closeable_state_watcher_browsertest.cc @@ -37,9 +37,7 @@ class TabCloseableStateWatcherTest : public InProcessBrowserTest { protected: // Wrapper for Browser::AddTabWithURL void AddTabWithURL(Browser* browser, const GURL& url) { - Browser::AddTabWithURLParams params(url, PageTransition::TYPED); - params.index = 0; - browser->AddTabWithURL(¶ms); + AddTabAtIndexToBrowser(browser, 0, url, PageTransition::TYPED); // Wait for page to finish loading. ui_test_utils::WaitForNavigation( &browser->GetSelectedTabContents()->controller()); diff --git a/chrome/browser/cocoa/applescript/window_applescript.mm b/chrome/browser/cocoa/applescript/window_applescript.mm index c51e9d8..07401d3 100644 --- a/chrome/browser/cocoa/applescript/window_applescript.mm +++ b/chrome/browser/cocoa/applescript/window_applescript.mm @@ -11,6 +11,7 @@ #import "chrome/browser/app_controller_mac.h" #include "chrome/browser/browser.h" #include "chrome/browser/browser_list.h" +#include "chrome/browser/browser_navigator.h" #include "chrome/browser/browser_window.h" #import "chrome/browser/chrome_browser_application_mac.h" #include "chrome/browser/cocoa/applescript/constants_applescript.h" @@ -185,13 +186,15 @@ // Set how long it takes a tab to be created. base::TimeTicks newTabStartTime = base::TimeTicks::Now(); - Browser::AddTabWithURLParams params(GURL(chrome::kChromeUINewTabURL), - PageTransition::TYPED); - params.index = index; - TabContents* contents = browser_->AddTabWithURL(¶ms); - contents->set_new_tab_start_time(newTabStartTime); - - [aTab setTabContent:contents]; + browser::NavigateParams params(browser_, + GURL(chrome::kChromeUINewTabURL), + PageTransition::TYPED); + params.disposition = NEW_FOREGROUND_TAB; + params.tabstrip_index = index; + browser::Navigate(¶ms); + params.target_contents->set_new_tab_start_time(newTabStartTime); + + [aTab setTabContent:params.target_contents]; } - (void)removeFromTabsAtIndex:(int)index { diff --git a/chrome/browser/cocoa/tab_strip_controller.mm b/chrome/browser/cocoa/tab_strip_controller.mm index fab844b..e3d5a1d 100644 --- a/chrome/browser/cocoa/tab_strip_controller.mm +++ b/chrome/browser/cocoa/tab_strip_controller.mm @@ -16,6 +16,7 @@ #include "base/sys_string_conversions.h" #include "chrome/app/chrome_dll_resource.h" #include "chrome/browser/browser.h" +#include "chrome/browser/browser_navigator.h" #include "chrome/browser/find_bar.h" #include "chrome/browser/find_bar_controller.h" #include "chrome/browser/metrics/user_metrics.h" @@ -1697,11 +1698,12 @@ private: case NEW_FOREGROUND_TAB: { UserMetrics::RecordAction(UserMetricsAction("Tab_DropURLBetweenTabs"), browser_->profile()); - Browser::AddTabWithURLParams params(url, PageTransition::TYPED); - params.index = index; - params.add_types = + browser::NavigateParams params(browser_, url, PageTransition::TYPED); + params.disposition = disposition; + params.tabstrip_index = index; + params.tabstrip_add_types = TabStripModel::ADD_SELECTED | TabStripModel::ADD_FORCE_INDEX; - browser_->AddTabWithURL(¶ms); + browser::Navigate(¶ms); break; } case CURRENT_TAB: diff --git a/chrome/browser/dom_ui/filebrowse_ui.cc b/chrome/browser/dom_ui/filebrowse_ui.cc index 90b8dbf..7c34413 100644 --- a/chrome/browser/dom_ui/filebrowse_ui.cc +++ b/chrome/browser/dom_ui/filebrowse_ui.cc @@ -21,6 +21,7 @@ #include "chrome/browser/bookmarks/bookmark_model.h" #include "chrome/browser/browser.h" #include "chrome/browser/browser_list.h" +#include "chrome/browser/browser_navigator.h" #include "chrome/browser/browser_thread.h" #include "chrome/browser/browser_window.h" #include "chrome/browser/dom_ui/dom_ui_favicon_source.h" @@ -696,14 +697,16 @@ void FilebrowseHandler::OpenNewWindow(const ListValue* args, bool popup) { Browser* browser = popup ? Browser::CreateForType(Browser::TYPE_APP_PANEL, profile_) : BrowserList::GetLastActive(); - Browser::AddTabWithURLParams params(GURL(url), PageTransition::LINK); - browser->AddTabWithURL(¶ms); + browser::NavigateParams params(browser, GURL(url), PageTransition::LINK); + params.disposition = NEW_FOREGROUND_TAB; + browser::Navigate(¶ms); + // TODO(beng): The following two calls should be automatic by Navigate(). if (popup) { // TODO(dhg): Remove these from being hardcoded. Allow javascript // to specify. - params.target->window()->SetBounds(gfx::Rect(0, 0, 400, 300)); + params.browser->window()->SetBounds(gfx::Rect(0, 0, 400, 300)); } - params.target->window()->Show(); + params.browser->window()->Show(); } void FilebrowseHandler::SendPicasawebRequest() { @@ -1061,14 +1064,16 @@ Browser* FileBrowseUI::OpenPopup(Profile* profile, url.append(hashArgument); } - Browser::AddTabWithURLParams params(GURL(url), PageTransition::LINK); - browser->AddTabWithURL(¶ms); - params.target->window()->SetBounds(gfx::Rect(kPopupLeft, - kPopupTop, - width, - height)); + browser::NavigateParams params(browser, GURL(url), PageTransition::LINK); + params.disposition = NEW_FOREGROUND_TAB; + browser::Navigate(¶ms); + // TODO(beng): The following two calls should be automatic by Navigate(). + params.browser->window()->SetBounds(gfx::Rect(kPopupLeft, + kPopupTop, + width, + height)); - params.target->window()->Show(); + params.browser->window()->Show(); } else { browser->window()->Show(); } diff --git a/chrome/browser/dom_ui/html_dialog_tab_contents_delegate.cc b/chrome/browser/dom_ui/html_dialog_tab_contents_delegate.cc index 1c9c5bb..303d071 100644 --- a/chrome/browser/dom_ui/html_dialog_tab_contents_delegate.cc +++ b/chrome/browser/dom_ui/html_dialog_tab_contents_delegate.cc @@ -5,7 +5,7 @@ #include "chrome/browser/dom_ui/html_dialog_tab_contents_delegate.h" #include "chrome/browser/browser.h" -#include "chrome/browser/browser_window.h" +#include "chrome/browser/browser_navigator.h" #include "chrome/browser/profile.h" #include "chrome/browser/tab_contents/tab_contents.h" #include "chrome/browser/tabs/tab_strip_model.h" @@ -40,11 +40,11 @@ void HtmlDialogTabContentsDelegate::OpenURLFromTab( // disposition. This is a tabless, modal dialog so we can't just // open it in the current frame. Code adapted from // Browser::OpenURLFromTab() with disposition == NEW_WINDOW. - Browser* browser = CreateBrowser(); - Browser::AddTabWithURLParams params(url, transition); + browser::NavigateParams params(CreateBrowser(), url, transition); params.referrer = referrer; - browser->AddTabWithURL(¶ms); - browser->window()->Show(); + params.disposition = NEW_FOREGROUND_TAB; + params.show_window = true; + browser::Navigate(¶ms); } } @@ -59,13 +59,14 @@ void HtmlDialogTabContentsDelegate::AddNewContents( WindowOpenDisposition disposition, const gfx::Rect& initial_pos, bool user_gesture) { if (profile_) { - // Force this to open in a new window, too. Code adapted from - // Browser::AddNewContents() with disposition == NEW_WINDOW. - Browser* browser = CreateBrowser(); - static_cast<TabContentsDelegate*>(browser)-> - AddNewContents(source, new_contents, NEW_FOREGROUND_TAB, - initial_pos, user_gesture); - browser->window()->Show(); + // Force this to open in a new window so that it appears at the top + // of the z-index. + browser::NavigateParams params(CreateBrowser(), new_contents); + params.source_contents = source; + params.disposition = NEW_FOREGROUND_TAB; + params.window_bounds = initial_pos; + params.show_window = true; + browser::Navigate(¶ms); } } diff --git a/chrome/browser/extensions/extension_tabs_module.cc b/chrome/browser/extensions/extension_tabs_module.cc index ce3a92a..3598808 100644 --- a/chrome/browser/extensions/extension_tabs_module.cc +++ b/chrome/browser/extensions/extension_tabs_module.cc @@ -11,6 +11,7 @@ #include "base/utf_string_conversions.h" #include "chrome/browser/browser.h" #include "chrome/browser/browser_list.h" +#include "chrome/browser/browser_navigator.h" #include "chrome/browser/browser_window.h" #include "chrome/browser/extensions/extension_function_dispatcher.h" #include "chrome/browser/extensions/extension_host.h" @@ -406,11 +407,8 @@ bool CreateWindowFunction::RunImpl() { } Browser* new_window = Browser::CreateForType(window_type, window_profile); - for (std::vector<GURL>::iterator i = urls.begin(); i != urls.end(); ++i) { - Browser::AddTabWithURLParams addTabParams = - Browser::AddTabWithURLParams(*i, PageTransition::LINK); - new_window->AddTabWithURL(&addTabParams); - } + for (std::vector<GURL>::iterator i = urls.begin(); i != urls.end(); ++i) + new_window->AddSelectedTabWithURL(*i, PageTransition::LINK); if (urls.size() == 0) new_window->NewTab(); new_window->SelectNumberedTab(0); @@ -626,20 +624,23 @@ bool CreateTabFunction::RunImpl() { int add_types = selected ? TabStripModel::ADD_SELECTED : TabStripModel::ADD_NONE; add_types |= TabStripModel::ADD_FORCE_INDEX; - Browser::AddTabWithURLParams params(url, PageTransition::LINK); - params.index = index; - params.add_types = add_types; - TabContents* contents = browser->AddTabWithURL(¶ms); - index = browser->tabstrip_model()->GetIndexOfTabContents(contents); + browser::NavigateParams params(browser, url, PageTransition::LINK); + params.disposition = selected ? NEW_FOREGROUND_TAB : NEW_BACKGROUND_TAB; + params.tabstrip_index = index; + params.tabstrip_add_types = add_types; + browser::Navigate(¶ms); if (selected) - contents->view()->SetInitialFocus(); + params.target_contents->view()->SetInitialFocus(); // Return data about the newly created tab. - if (has_callback()) - result_.reset(ExtensionTabUtil::CreateTabValue(contents, - browser->tabstrip_model(), - index)); + if (has_callback()) { + result_.reset(ExtensionTabUtil::CreateTabValue( + params.target_contents, + params.browser->tabstrip_model(), + params.browser->tabstrip_model()->GetIndexOfTabContents( + params.target_contents))); + } return true; } diff --git a/chrome/browser/find_bar_host_browsertest.cc b/chrome/browser/find_bar_host_browsertest.cc index 62bae77..f36dd32 100644 --- a/chrome/browser/find_bar_host_browsertest.cc +++ b/chrome/browser/find_bar_host_browsertest.cc @@ -7,6 +7,7 @@ #include "base/string_util.h" #include "base/utf_string_conversions.h" #include "chrome/browser/browser.h" +#include "chrome/browser/browser_navigator.h" #include "chrome/browser/browser_window.h" #include "chrome/browser/find_bar.h" #include "chrome/browser/find_bar_controller.h" @@ -823,9 +824,13 @@ IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, PreferPreviousSearch) { EXPECT_EQ(1, FindInPageWchar(tab1, L"Default", kFwd, kIgnoreCase, &ordinal)); // Create a second tab. - Browser::AddTabWithURLParams params(url, PageTransition::TYPED); - browser()->AddTabWithURL(¶ms); - EXPECT_EQ(browser(), params.target); + // For some reason we can't use AddSelectedTabWithURL here on ChromeOS. It + // could be some delicate assumption about the tab starting off unselected or + // something relating to user gesture. + browser::NavigateParams params(browser(), url, PageTransition::TYPED); + params.disposition = NEW_BACKGROUND_TAB; + params.tabstrip_add_types = TabStripModel::ADD_NONE; + browser::Navigate(¶ms); browser()->SelectTabContentsAt(1, false); TabContents* tab2 = browser()->GetSelectedTabContents(); EXPECT_NE(tab1, tab2); @@ -899,10 +904,7 @@ IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, PrepopulateInNewTab) { EXPECT_EQ(1, FindInPageWchar(tab1, L"page", kFwd, kIgnoreCase, &ordinal)); // Now create a second tab and load the same page. - Browser::AddTabWithURLParams params(url, PageTransition::TYPED); - browser()->AddTabWithURL(¶ms); - EXPECT_EQ(browser(), params.target); - browser()->SelectTabContentsAt(1, false); + browser()->AddSelectedTabWithURL(url, PageTransition::TYPED); TabContents* tab2 = browser()->GetSelectedTabContents(); EXPECT_NE(tab1, tab2); @@ -944,9 +946,10 @@ IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, PrepopulatePreserveLast) { FindBarController::kKeepSelection); // Now create a second tab and load the same page. - Browser::AddTabWithURLParams params(url, PageTransition::TYPED); - browser()->AddTabWithURL(¶ms); - EXPECT_EQ(browser(), params.target); + browser::NavigateParams params(browser(), url, PageTransition::TYPED); + params.disposition = NEW_BACKGROUND_TAB; + params.tabstrip_add_types = TabStripModel::ADD_NONE; + browser::Navigate(¶ms); browser()->SelectTabContentsAt(1, false); TabContents* tab2 = browser()->GetSelectedTabContents(); EXPECT_NE(tab1, tab2); @@ -1018,9 +1021,7 @@ IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, MAYBE_NoIncognitoPrepopulate) { // Open a new incognito window and navigate to the same page. Profile* incognito_profile = browser()->profile()->GetOffTheRecordProfile(); Browser* incognito_browser = Browser::Create(incognito_profile); - Browser::AddTabWithURLParams params1(url, PageTransition::START_PAGE); - incognito_browser->AddTabWithURL(¶ms1); - EXPECT_EQ(incognito_browser, params1.target); + incognito_browser->AddSelectedTabWithURL(url, PageTransition::START_PAGE); ui_test_utils::WaitForNavigation( &incognito_browser->GetSelectedTabContents()->controller()); incognito_browser->window()->Show(); @@ -1040,10 +1041,7 @@ IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, MAYBE_NoIncognitoPrepopulate) { FindBarController::kKeepSelection); // Now open a new tab in the original (non-incognito) browser. - Browser::AddTabWithURLParams params2(url, PageTransition::TYPED); - browser()->AddTabWithURL(¶ms2); - EXPECT_EQ(browser(), params2.target); - browser()->SelectTabContentsAt(1, false); + browser()->AddSelectedTabWithURL(url, PageTransition::TYPED); TabContents* tab2 = browser()->GetSelectedTabContents(); EXPECT_NE(tab1, tab2); diff --git a/chrome/browser/gtk/bookmark_bar_gtk_interactive_uitest.cc b/chrome/browser/gtk/bookmark_bar_gtk_interactive_uitest.cc index f3bffa2..0fc9101 100644 --- a/chrome/browser/gtk/bookmark_bar_gtk_interactive_uitest.cc +++ b/chrome/browser/gtk/bookmark_bar_gtk_interactive_uitest.cc @@ -36,9 +36,7 @@ IN_PROC_BROWSER_TEST_F(BookmarkBarGtkBrowserTest, FindBarTest) { // Create new tab with an arbitrary URL. GURL url = test_server()->GetURL(kSimplePage); - Browser::AddTabWithURLParams params(url, PageTransition::TYPED); - browser()->AddTabWithURL(¶ms); - EXPECT_EQ(browser(), params.target); + browser()->AddSelectedTabWithURL(url, PageTransition::TYPED); // Switch back to the NTP with the active findbar. browser()->SelectTabContentsAt(1, false); diff --git a/chrome/browser/importer/importer.cc b/chrome/browser/importer/importer.cc index 9a40471..32f48d3b 100644 --- a/chrome/browser/importer/importer.cc +++ b/chrome/browser/importer/importer.cc @@ -9,6 +9,7 @@ #include "base/values.h" #include "chrome/browser/bookmarks/bookmark_model.h" #include "chrome/browser/browser_list.h" +#include "chrome/browser/browser_navigator.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/browser_thread.h" #include "chrome/browser/browsing_instance.h" @@ -200,15 +201,11 @@ void ImporterHost::StartImportSettings( MB_OK | MB_TOPMOST); GURL url("https://www.google.com/accounts/ServiceLogin"); - Browser* browser = BrowserList::GetLastActive(); - Browser::AddTabWithURLParams params(url, PageTransition::TYPED); - // BrowsingInstance is refcounted. - BrowsingInstance* instance = new BrowsingInstance(writer_->profile()); - params.instance = instance->GetSiteInstanceForURL(url); - browser->AddTabWithURL(¶ms); + BrowserList::GetLastActive()->AddSelectedTabWithURL( + url, PageTransition::TYPED); MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( - this, &ImporterHost::OnLockViewEnd, false)); + this, &ImporterHost::OnLockViewEnd, false)); is_source_readable_ = false; } diff --git a/chrome/browser/sessions/session_restore.cc b/chrome/browser/sessions/session_restore.cc index 48cea7f..1321422 100644 --- a/chrome/browser/sessions/session_restore.cc +++ b/chrome/browser/sessions/session_restore.cc @@ -13,6 +13,7 @@ #include "base/string_util.h" #include "chrome/browser/browser.h" #include "chrome/browser/browser_list.h" +#include "chrome/browser/browser_navigator.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/browser_window.h" #include "chrome/browser/extensions/extensions_service.h" @@ -552,10 +553,12 @@ class SessionRestoreImpl : public NotificationObserver { if (i == 0) add_types |= TabStripModel::ADD_SELECTED; int index = browser->GetIndexForInsertionDuringRestore(i); - Browser::AddTabWithURLParams params(urls[i], PageTransition::START_PAGE); - params.index = index; - params.add_types = add_types; - browser->AddTabWithURL(¶ms); + browser::NavigateParams params(browser, urls[i], + PageTransition::START_PAGE); + params.disposition = i == 0 ? NEW_FOREGROUND_TAB : NEW_BACKGROUND_TAB; + params.tabstrip_index = index; + params.tabstrip_add_types = add_types; + browser::Navigate(¶ms); } } diff --git a/chrome/browser/sessions/session_restore_browsertest.cc b/chrome/browser/sessions/session_restore_browsertest.cc index 9ff065d..c05a34b 100644 --- a/chrome/browser/sessions/session_restore_browsertest.cc +++ b/chrome/browser/sessions/session_restore_browsertest.cc @@ -77,14 +77,10 @@ IN_PROC_BROWSER_TEST_F(SessionRestoreTest, RestoreIndividualTabFromWindow) { // Add and navigate three tabs. ui_test_utils::NavigateToURL(browser(), url1); - Browser::AddTabWithURLParams params1(url2, PageTransition::LINK); - browser()->AddTabWithURL(¶ms1); - EXPECT_EQ(browser(), params1.target); + browser()->AddSelectedTabWithURL(url2, PageTransition::LINK); ui_test_utils::WaitForNavigationInCurrentTab(browser()); - Browser::AddTabWithURLParams params2(url3, PageTransition::LINK); - browser()->AddTabWithURL(¶ms2); - EXPECT_EQ(browser(), params2.target); + browser()->AddSelectedTabWithURL(url3, PageTransition::LINK); ui_test_utils::WaitForNavigationInCurrentTab(browser()); TabRestoreService* service = browser()->profile()->GetTabRestoreService(); diff --git a/chrome/browser/ssl/ssl_browser_tests.cc b/chrome/browser/ssl/ssl_browser_tests.cc index b3831cd..12ecedb 100644 --- a/chrome/browser/ssl/ssl_browser_tests.cc +++ b/chrome/browser/ssl/ssl_browser_tests.cc @@ -5,6 +5,7 @@ #include "base/time.h" #include "chrome/app/chrome_dll_resource.h" #include "chrome/browser/browser.h" +#include "chrome/browser/browser_navigator.h" #include "chrome/browser/prefs/pref_service.h" #include "chrome/browser/profile.h" #include "chrome/browser/tab_contents/interstitial_page.h" @@ -464,10 +465,12 @@ IN_PROC_BROWSER_TEST_F(SSLUITest, TestDisplaysInsecureContentTwoTabs) { // Create a new tab. GURL url = https_server_.GetURL( "files/ssl/page_displays_insecure_content.html"); - Browser::AddTabWithURLParams params(url, PageTransition::TYPED); - params.index = 0; - params.instance = tab1->GetSiteInstance(); - TabContents* tab2 = browser()->AddTabWithURL(¶ms); + browser::NavigateParams params(browser(), url, PageTransition::TYPED); + params.disposition = NEW_FOREGROUND_TAB; + params.tabstrip_index = 0; + params.source_contents = tab1; + browser::Navigate(¶ms); + TabContents* tab2 = params.target_contents; ui_test_utils::WaitForNavigation(&(tab2->controller())); // The new tab has insecure content. @@ -495,9 +498,11 @@ IN_PROC_BROWSER_TEST_F(SSLUITest, TestRunsInsecureContentTwoTabs) { // Create a new tab. GURL url = https_server_.GetURL("files/ssl/page_runs_insecure_content.html"); - Browser::AddTabWithURLParams params(url, PageTransition::TYPED); - params.instance = tab1->GetSiteInstance(); - TabContents* tab2 = browser()->AddTabWithURL(¶ms); + browser::NavigateParams params(browser(), url, PageTransition::TYPED); + params.disposition = NEW_FOREGROUND_TAB; + params.source_contents = tab1; + browser::Navigate(¶ms); + TabContents* tab2 = params.target_contents; ui_test_utils::WaitForNavigation(&(tab2->controller())); // The new tab has insecure content. @@ -639,13 +644,10 @@ IN_PROC_BROWSER_TEST_F(SSLUITest, DISABLED_TestCloseTabWithUnsafePopup) { // Let's add another tab to make sure the browser does not exit when we close // the first tab. GURL url = test_server()->GetURL("files/ssl/google.html"); - Browser::AddTabWithURLParams params(url, PageTransition::TYPED); - TabContents* tab2 = browser()->AddTabWithURL(¶ms); + TabContents* tab2 = + browser()->AddSelectedTabWithURL(url, PageTransition::TYPED); ui_test_utils::WaitForNavigation(&(tab2->controller())); - // Ensure that the tab was created in the correct browser. - EXPECT_EQ(browser(), params.target); - // Close the first tab. browser()->CloseTabContents(tab1); } diff --git a/chrome/browser/tabs/tab_strip_model.cc b/chrome/browser/tabs/tab_strip_model.cc index 80f048b..a3804b7 100644 --- a/chrome/browser/tabs/tab_strip_model.cc +++ b/chrome/browser/tabs/tab_strip_model.cc @@ -644,7 +644,7 @@ void TabStripModel::ExecuteContextMenuCommand( case CommandNewTab: UserMetrics::RecordAction(UserMetricsAction("TabContextMenu_NewTab"), profile_); - delegate()->AddBlankTabAt(context_index + 1, true); + delegate()->AddBlankTab(true); break; case CommandReload: UserMetrics::RecordAction(UserMetricsAction("TabContextMenu_Reload"), diff --git a/chrome/browser/task_manager/task_manager_browsertest.cc b/chrome/browser/task_manager/task_manager_browsertest.cc index 69fa80a..5828c22 100644 --- a/chrome/browser/task_manager/task_manager_browsertest.cc +++ b/chrome/browser/task_manager/task_manager_browsertest.cc @@ -7,6 +7,7 @@ #include "app/l10n_util.h" #include "base/file_path.h" #include "chrome/browser/browser.h" +#include "chrome/browser/browser_navigator.h" #include "chrome/browser/browser_window.h" #include "chrome/browser/extensions/crashed_extension_infobar.h" #include "chrome/browser/extensions/extension_browsertest.h" @@ -102,10 +103,7 @@ IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, NoticeTabContentsChanges) { // Open a new tab and make sure we notice that. GURL url(ui_test_utils::GetTestUrl(FilePath(FilePath::kCurrentDirectory), FilePath(kTitle1File))); - Browser::AddTabWithURLParams params(url, PageTransition::TYPED); - params.index = 0; - browser()->AddTabWithURL(¶ms); - EXPECT_EQ(browser(), params.target); + AddTabAtIndex(0, url, PageTransition::TYPED); WaitForResourceChange(3); // Close the tab and verify that we notice. @@ -251,9 +249,7 @@ IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, // Open a new tab and make sure we notice that. GURL url(ui_test_utils::GetTestUrl(FilePath(FilePath::kCurrentDirectory), FilePath(kTitle1File))); - Browser::AddTabWithURLParams params(url, PageTransition::TYPED); - params.index = 0; - browser()->AddTabWithURL(¶ms); + AddTabAtIndex(0, url, PageTransition::TYPED); WaitForResourceChange(3); // Check that we get some value for the cache columns. diff --git a/chrome/browser/views/find_bar_host_interactive_uitest.cc b/chrome/browser/views/find_bar_host_interactive_uitest.cc index 9c2750e..12537fb 100644 --- a/chrome/browser/views/find_bar_host_interactive_uitest.cc +++ b/chrome/browser/views/find_bar_host_interactive_uitest.cc @@ -50,9 +50,7 @@ IN_PROC_BROWSER_TEST_F(FindInPageTest, CrashEscHandlers) { browser()->Find(); // Open another tab (tab B). - Browser::AddTabWithURLParams params(url, PageTransition::TYPED); - browser()->AddTabWithURL(¶ms); - EXPECT_EQ(browser(), params.target); + browser()->AddSelectedTabWithURL(url, PageTransition::TYPED); browser()->Find(); EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), diff --git a/chrome/test/browser_with_test_window_test.cc b/chrome/test/browser_with_test_window_test.cc index 9813dfd..368838b 100644 --- a/chrome/test/browser_with_test_window_test.cc +++ b/chrome/test/browser_with_test_window_test.cc @@ -9,6 +9,7 @@ #endif // defined(OS_WIN) #include "chrome/browser/browser.h" +#include "chrome/browser/browser_navigator.h" #include "chrome/browser/tab_contents/navigation_controller.h" #include "chrome/browser/tab_contents/navigation_entry.h" #include "chrome/browser/tab_contents/tab_contents.h" @@ -60,10 +61,11 @@ TestRenderViewHost* BrowserWithTestWindowTest::TestRenderViewHostForTab( } void BrowserWithTestWindowTest::AddTab(Browser* browser, const GURL& url) { - Browser::AddTabWithURLParams params(url, PageTransition::TYPED); - params.index = 0; - TabContents* contents = browser->AddTabWithURL(¶ms); - CommitPendingLoad(&contents->controller()); + browser::NavigateParams params(browser, url, PageTransition::TYPED); + params.tabstrip_index = 0; + params.disposition = NEW_FOREGROUND_TAB; + browser::Navigate(¶ms); + CommitPendingLoad(¶ms.target_contents->controller()); } void BrowserWithTestWindowTest::CommitPendingLoad( diff --git a/chrome/test/in_process_browser_test.cc b/chrome/test/in_process_browser_test.cc index 5bfa802..8275fd6 100644 --- a/chrome/test/in_process_browser_test.cc +++ b/chrome/test/in_process_browser_test.cc @@ -13,6 +13,7 @@ #include "base/test/test_file_util.h" #include "chrome/browser/browser.h" #include "chrome/browser/browser_list.h" +#include "chrome/browser/browser_navigator.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/browser_shutdown.h" #include "chrome/browser/browser_thread.h" @@ -269,6 +270,24 @@ void InProcessBrowserTest::TearDown() { RenderProcessHost::set_run_renderer_in_process(original_single_process_); } +void InProcessBrowserTest::AddTabAtIndexToBrowser( + Browser* browser, + int index, + const GURL& url, + PageTransition::Type transition) { + browser::NavigateParams params(browser, url, transition); + params.tabstrip_index = index; + params.disposition = NEW_FOREGROUND_TAB; + browser::Navigate(¶ms); +} + +void InProcessBrowserTest::AddTabAtIndex( + int index, + const GURL& url, + PageTransition::Type transition) { + AddTabAtIndexToBrowser(browser(), index, url, transition); +} + // Creates a browser with a single tab (about:blank), waits for the tab to // finish loading and shows the browser. Browser* InProcessBrowserTest::CreateBrowser(Profile* profile) { diff --git a/chrome/test/in_process_browser_test.h b/chrome/test/in_process_browser_test.h index 67736f7..67658b5 100644 --- a/chrome/test/in_process_browser_test.h +++ b/chrome/test/in_process_browser_test.h @@ -10,6 +10,7 @@ #include "base/ref_counted.h" #include "base/scoped_ptr.h" #include "base/scoped_temp_dir.h" +#include "chrome/common/page_transition_types.h" #include "net/test/test_server.h" #include "testing/gtest/include/gtest/gtest.h" @@ -67,6 +68,17 @@ class InProcessBrowserTest : public testing::Test { // Returns the browser created by CreateBrowser. Browser* browser() const { return browser_; } + // Convenience methods for adding tabs to a Browser. + void AddTabAtIndexToBrowser(Browser* browser, + int index, + const GURL& url, + PageTransition::Type transition); + void AddTabAtIndex(int index, const GURL& url, + PageTransition::Type transition); + + // Adds a selected tab at |index| to |url| with the specified |transition|. + void AddTabAt(int index, const GURL& url, PageTransition::Type transition); + // Override this rather than TestBody. virtual void RunTestOnMainThread() = 0; |