browser_tabstrip.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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/blocked_content/blocked_content_tab_helper.h"
10#include "chrome/browser/ui/browser.h"
11#include "chrome/browser/ui/browser_navigator.h"
12#include "chrome/browser/ui/browser_window.h"
13#include "chrome/browser/ui/tab_contents/tab_contents.h"
14#include "chrome/browser/ui/tabs/tab_strip_model.h"
15#include "chrome/common/chrome_switches.h"
16#include "chrome/common/url_constants.h"
17#include "content/public/browser/navigation_entry.h"
18#include "content/public/browser/render_view_host.h"
19#include "content/public/browser/web_contents.h"
20
21// TODO(avi): Kill this when TabContents goes away.
22class BrowserTabstripTabContentsCreator {
23 public:
24  static TabContents* CreateTabContents(content::WebContents* contents) {
25    return TabContents::Factory::CreateTabContents(contents);
26  }
27};
28
29namespace chrome {
30
31int GetIndexOfTab(const Browser* browser,
32                  const content::WebContents* contents) {
33  return browser->tab_strip_model()->GetIndexOfWebContents(contents);
34}
35
36TabContents* GetActiveTabContents(const Browser* browser) {
37  return browser->tab_strip_model()->GetActiveTabContents();
38}
39
40content::WebContents* GetActiveWebContents(const Browser* browser) {
41  TabContents* active_tab = GetActiveTabContents(browser);
42  return active_tab ? active_tab->web_contents() : NULL;
43}
44
45TabContents* GetTabContentsAt(const Browser* browser, int index) {
46  return browser->tab_strip_model()->GetTabContentsAt(index);
47}
48
49content::WebContents* GetWebContentsAt(const Browser* browser, int index) {
50  TabContents* tab = GetTabContentsAt(browser, index);
51  return tab ? tab->web_contents() : NULL;
52}
53
54void ActivateTabAt(Browser* browser, int index, bool user_gesture) {
55  browser->tab_strip_model()->ActivateTabAt(index, user_gesture);
56}
57
58void AddBlankTabAt(Browser* browser, int index, bool foreground) {
59  // Time new tab page creation time.  We keep track of the timing data in
60  // WebContents, but we want to include the time it takes to create the
61  // WebContents object too.
62  base::TimeTicks new_tab_start_time = base::TimeTicks::Now();
63  chrome::NavigateParams params(browser, GURL(chrome::kChromeUINewTabURL),
64                                content::PAGE_TRANSITION_TYPED);
65  params.disposition = foreground ? NEW_FOREGROUND_TAB : NEW_BACKGROUND_TAB;
66  params.tabstrip_index = index;
67  chrome::Navigate(&params);
68  params.target_contents->web_contents()->SetNewTabStartTime(
69      new_tab_start_time);
70}
71
72bool IsTabStripEditable(Browser* browser) {
73  return browser->window()->IsTabStripEditable();
74}
75
76TabContents* AddSelectedTabWithURL(Browser* browser,
77                                   const GURL& url,
78                                   content::PageTransition transition) {
79  NavigateParams params(browser, url, transition);
80  params.disposition = NEW_FOREGROUND_TAB;
81  Navigate(&params);
82  return params.target_contents;
83}
84
85void AddTab(Browser* browser,
86            TabContents* tab_contents,
87            content::PageTransition type) {
88  browser->tab_strip_model()->AddTabContents(tab_contents, -1, type,
89                                             TabStripModel::ADD_ACTIVE);
90}
91
92void AddWebContents(Browser* browser,
93                    content::WebContents* source_contents,
94                    content::WebContents* new_contents,
95                    WindowOpenDisposition disposition,
96                    const gfx::Rect& initial_pos,
97                    bool user_gesture,
98                    bool* was_blocked) {
99  // No code for this yet.
100  DCHECK(disposition != SAVE_TO_DISK);
101  // Can't create a new contents for the current tab - invalid case.
102  DCHECK(disposition != CURRENT_TAB);
103
104  // TODO(avi): Use browser tab contents adoption here.
105  TabContents* new_tab_contents = TabContents::FromWebContents(new_contents);
106  if (!new_tab_contents) {
107    new_tab_contents =
108        BrowserTabstripTabContentsCreator::CreateTabContents(new_contents);
109  }
110
111  BlockedContentTabHelper* source_blocked_content = NULL;
112  if (source_contents) {
113    source_blocked_content =
114        BlockedContentTabHelper::FromWebContents(source_contents);
115  }
116
117  if (source_contents) {
118    // Handle blocking of tabs.
119    if (source_blocked_content->all_contents_blocked()) {
120      source_blocked_content->AddWebContents(
121          new_contents, disposition, initial_pos, user_gesture);
122      if (was_blocked)
123        *was_blocked = true;
124      return;
125    }
126
127    // Handle blocking of popups.
128    if ((disposition == NEW_POPUP || disposition == NEW_FOREGROUND_TAB) &&
129        !user_gesture &&
130        !CommandLine::ForCurrentProcess()->HasSwitch(
131            switches::kDisablePopupBlocking)) {
132      // Unrequested popups from normal pages are constrained unless they're in
133      // the white list.  The popup owner will handle checking this.
134      source_blocked_content->AddPopup(
135          new_contents, disposition, initial_pos, user_gesture);
136      if (was_blocked)
137        *was_blocked = true;
138      return;
139    }
140
141    new_contents->GetRenderViewHost()->DisassociateFromPopupCount();
142  }
143
144  NavigateParams params(browser, new_tab_contents);
145  params.source_contents = source_contents ?
146      GetTabContentsAt(browser, GetIndexOfTab(browser, source_contents)) : NULL;
147  params.disposition = disposition;
148  params.window_bounds = initial_pos;
149  params.window_action = NavigateParams::SHOW_WINDOW;
150  params.user_gesture = user_gesture;
151  Navigate(&params);
152}
153
154void CloseWebContents(Browser* browser, content::WebContents* contents) {
155  int index = browser->tab_strip_model()->GetIndexOfWebContents(contents);
156  if (index == TabStripModel::kNoTab) {
157    NOTREACHED() << "CloseWebContents called for tab not in our strip";
158    return;
159  }
160  browser->tab_strip_model()->CloseTabContentsAt(
161      index,
162      TabStripModel::CLOSE_CREATE_HISTORICAL_TAB);
163}
164
165void CloseAllTabs(Browser* browser) {
166  browser->tab_strip_model()->CloseAllTabs();
167}
168
169TabContents* TabContentsFactory(
170    Profile* profile,
171    content::SiteInstance* site_instance,
172    int routing_id,
173    const content::WebContents* base_web_contents) {
174  return BrowserTabstripTabContentsCreator::CreateTabContents(
175      content::WebContents::Create(profile,
176      site_instance,
177      routing_id,
178      base_web_contents));
179}
180
181TabContents* TabContentsWithSessionStorageFactory(
182    Profile* profile,
183    content::SiteInstance* site_instance,
184    int routing_id,
185    const content::WebContents* base_web_contents,
186    const content::SessionStorageNamespaceMap& session_storage_namespace_map) {
187  return BrowserTabstripTabContentsCreator::CreateTabContents(
188      content::WebContents::CreateWithSessionStorage(
189          profile,
190          site_instance,
191          routing_id,
192          base_web_contents,
193          session_storage_namespace_map));
194}
195
196}  // namespace chrome
197