browser_tabstrip.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)// found in the LICENSE file.
409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#include "chrome/browser/ui/browser_tabstrip.h"
609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#include "base/command_line.h"
809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#include "chrome/browser/profiles/profile.h"
909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#include "chrome/browser/ui/browser.h"
1009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#include "chrome/browser/ui/browser_navigator.h"
11c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)#include "chrome/browser/ui/tab_contents/core_tab_helper.h"
1209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#include "chrome/browser/ui/tabs/tab_strip_model.h"
1307a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch#include "chrome/common/chrome_switches.h"
1409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#include "chrome/common/url_constants.h"
1509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#include "content/public/browser/navigation_entry.h"
1609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#include "content/public/browser/render_view_host.h"
1709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#include "content/public/browser/web_contents.h"
1809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
1909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)namespace chrome {
2009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
21d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)void AddTabAt(Browser* browser, const GURL& url, int idx, bool foreground) {
2209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)  // Time new tab page creation time.  We keep track of the timing data in
2309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)  // WebContents, but we want to include the time it takes to create the
2409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)  // WebContents object too.
2509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)  base::TimeTicks new_tab_start_time = base::TimeTicks::Now();
2609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)  chrome::NavigateParams params(browser,
2709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)      url.is_empty() ? GURL(chrome::kChromeUINewTabURL) : url,
2809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)      ui::PAGE_TRANSITION_TYPED);
2907a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch  params.disposition = foreground ? NEW_FOREGROUND_TAB : NEW_BACKGROUND_TAB;
3007a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch  params.tabstrip_index = idx;
3107a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch  chrome::Navigate(&params);
3207a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch  CoreTabHelper* core_tab_helper =
3307a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch      CoreTabHelper::FromWebContents(params.target_contents);
3409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)  core_tab_helper->set_new_tab_start_time(new_tab_start_time);
35d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)}
3607a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch
37d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)content::WebContents* AddSelectedTabWithURL(
3809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    Browser* browser,
3909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    const GURL& url,
40c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    ui::PageTransition transition) {
4109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)  NavigateParams params(browser, url, transition);
4209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)  params.disposition = NEW_FOREGROUND_TAB;
4309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)  Navigate(&params);
44  return params.target_contents;
45}
46
47void AddWebContents(Browser* browser,
48                    content::WebContents* source_contents,
49                    content::WebContents* new_contents,
50                    WindowOpenDisposition disposition,
51                    const gfx::Rect& initial_pos,
52                    bool user_gesture,
53                    bool* was_blocked) {
54  // No code for this yet.
55  DCHECK(disposition != SAVE_TO_DISK);
56  // Can't create a new contents for the current tab - invalid case.
57  DCHECK(disposition != CURRENT_TAB);
58
59  NavigateParams params(browser, new_contents);
60  params.source_contents = source_contents;
61  params.disposition = disposition;
62  params.window_bounds = initial_pos;
63  params.window_action = NavigateParams::SHOW_WINDOW;
64  // At this point, we're already beyond the popup blocker. Even if the popup
65  // was created without a user gesture, we have to set |user_gesture| to true,
66  // so it gets correctly focused.
67  params.user_gesture = true;
68  Navigate(&params);
69}
70
71void CloseWebContents(Browser* browser,
72                      content::WebContents* contents,
73                      bool add_to_history) {
74  int index = browser->tab_strip_model()->GetIndexOfWebContents(contents);
75  if (index == TabStripModel::kNoTab) {
76    NOTREACHED() << "CloseWebContents called for tab not in our strip";
77    return;
78  }
79
80  browser->tab_strip_model()->CloseWebContentsAt(
81      index,
82      add_to_history ? TabStripModel::CLOSE_CREATE_HISTORICAL_TAB
83                     : TabStripModel::CLOSE_NONE);
84}
85
86}  // namespace chrome
87