browser_tabstrip.cc revision 0f1bc08d4cfcc34181b0b5cbf065c40f687bf740
1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/ui/browser_tabstrip.h"
6
7#include "base/command_line.h"
8#include "chrome/browser/profiles/profile.h"
9#include "chrome/browser/ui/browser.h"
10#include "chrome/browser/ui/browser_navigator.h"
11#include "chrome/browser/ui/tab_contents/core_tab_helper.h"
12#include "chrome/browser/ui/tabs/tab_strip_model.h"
13#include "chrome/common/chrome_switches.h"
14#include "chrome/common/url_constants.h"
15#include "content/public/browser/navigation_entry.h"
16#include "content/public/browser/render_view_host.h"
17#include "content/public/browser/web_contents.h"
18
19namespace chrome {
20
21void AddBlankTabAt(Browser* browser, int index, bool foreground) {
22  // Time new tab page creation time.  We keep track of the timing data in
23  // WebContents, but we want to include the time it takes to create the
24  // WebContents object too.
25  base::TimeTicks new_tab_start_time = base::TimeTicks::Now();
26  chrome::NavigateParams params(browser, GURL(chrome::kChromeUINewTabURL),
27                                content::PAGE_TRANSITION_TYPED);
28  params.disposition = foreground ? NEW_FOREGROUND_TAB : NEW_BACKGROUND_TAB;
29  params.tabstrip_index = index;
30  chrome::Navigate(&params);
31  CoreTabHelper* core_tab_helper =
32      CoreTabHelper::FromWebContents(params.target_contents);
33  core_tab_helper->set_new_tab_start_time(new_tab_start_time);
34}
35
36content::WebContents* AddSelectedTabWithURL(
37    Browser* browser,
38    const GURL& url,
39    content::PageTransition transition) {
40  NavigateParams params(browser, url, transition);
41  params.disposition = NEW_FOREGROUND_TAB;
42  Navigate(&params);
43  return params.target_contents;
44}
45
46void AddWebContents(Browser* browser,
47                    content::WebContents* source_contents,
48                    content::WebContents* new_contents,
49                    WindowOpenDisposition disposition,
50                    const gfx::Rect& initial_pos,
51                    bool user_gesture,
52                    bool* was_blocked) {
53  // No code for this yet.
54  DCHECK(disposition != SAVE_TO_DISK);
55  // Can't create a new contents for the current tab - invalid case.
56  DCHECK(disposition != CURRENT_TAB);
57
58  NavigateParams params(browser, new_contents);
59  params.source_contents = source_contents;
60  params.disposition = disposition;
61  params.window_bounds = initial_pos;
62  params.window_action = NavigateParams::SHOW_WINDOW;
63  // At this point, we're already beyond the popup blocker. Even if the popup
64  // was created without a user gesture, we have to set |user_gesture| to true,
65  // so it gets correctly focused.
66  params.user_gesture = true;
67  Navigate(&params);
68}
69
70void CloseWebContents(Browser* browser,
71                      content::WebContents* contents,
72                      bool add_to_history) {
73  int index = browser->tab_strip_model()->GetIndexOfWebContents(contents);
74  if (index == TabStripModel::kNoTab) {
75    NOTREACHED() << "CloseWebContents called for tab not in our strip";
76    return;
77  }
78
79  browser->tab_strip_model()->CloseWebContentsAt(
80      index,
81      add_to_history ? TabStripModel::CLOSE_CREATE_HISTORICAL_TAB
82                     : TabStripModel::CLOSE_NONE);
83}
84
85}  // namespace chrome
86