summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/browser_tabstrip.cc
blob: a68688d126a440b845677447c3c31e97235a0486 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/browser_tabstrip.h"

#include "base/command_line.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_navigator.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tab_contents/tab_contents.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/url_constants.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"

// TODO(avi): Kill this when TabContents goes away.
class BrowserTabstripTabContentsCreator {
 public:
  static TabContents* CreateTabContents(content::WebContents* contents) {
    return TabContents::Factory::CreateTabContents(contents);
  }
};

namespace chrome {

int GetIndexOfTab(const Browser* browser,
                  const content::WebContents* contents) {
  return browser->tab_strip_model()->GetIndexOfWebContents(contents);
}

TabContents* GetActiveTabContents(const Browser* browser) {
  return browser->tab_strip_model()->GetActiveTabContents();
}

content::WebContents* GetActiveWebContents(const Browser* browser) {
  TabContents* active_tab = GetActiveTabContents(browser);
  return active_tab ? active_tab->web_contents() : NULL;
}

TabContents* GetTabContentsAt(const Browser* browser, int index) {
  return browser->tab_strip_model()->GetTabContentsAt(index);
}

content::WebContents* GetWebContentsAt(const Browser* browser, int index) {
  TabContents* tab = GetTabContentsAt(browser, index);
  return tab ? tab->web_contents() : NULL;
}

void ActivateTabAt(Browser* browser, int index, bool user_gesture) {
  browser->tab_strip_model()->ActivateTabAt(index, user_gesture);
}

TabContents* AddBlankTab(Browser* browser, bool foreground) {
  return AddBlankTabAt(browser, -1, foreground);
}

TabContents* AddBlankTabAt(Browser* browser, int index, bool foreground) {
  // Time new tab page creation time.  We keep track of the timing data in
  // WebContents, but we want to include the time it takes to create the
  // WebContents object too.
  base::TimeTicks new_tab_start_time = base::TimeTicks::Now();
  chrome::NavigateParams params(browser, GURL(chrome::kChromeUINewTabURL),
                                content::PAGE_TRANSITION_TYPED);
  params.disposition = foreground ? NEW_FOREGROUND_TAB : NEW_BACKGROUND_TAB;
  params.tabstrip_index = index;
  chrome::Navigate(&params);
  params.target_contents->web_contents()->SetNewTabStartTime(
      new_tab_start_time);
  return params.target_contents;
}

bool IsTabStripEditable(Browser* browser) {
  return browser->window()->IsTabStripEditable();
}

TabContents* AddSelectedTabWithURL(Browser* browser,
                                   const GURL& url,
                                   content::PageTransition transition) {
  NavigateParams params(browser, url, transition);
  params.disposition = NEW_FOREGROUND_TAB;
  Navigate(&params);
  return params.target_contents;
}

void AddTab(Browser* browser,
            TabContents* tab_contents,
            content::PageTransition type) {
  browser->tab_strip_model()->AddTabContents(tab_contents, -1, type,
                                             TabStripModel::ADD_ACTIVE);
}

void AddWebContents(Browser* browser,
                    content::WebContents* source_contents,
                    content::WebContents* new_contents,
                    WindowOpenDisposition disposition,
                    const gfx::Rect& initial_pos,
                    bool user_gesture) {
  // 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);

  TabContents* source_tab_contents = NULL;
  BlockedContentTabHelper* source_blocked_content = NULL;
  TabContents* new_tab_contents = TabContents::FromWebContents(new_contents);
  if (!new_tab_contents) {
    new_tab_contents =
        BrowserTabstripTabContentsCreator::CreateTabContents(new_contents);
  }
  if (source_contents) {
    source_tab_contents = TabContents::FromWebContents(source_contents);
    source_blocked_content = source_tab_contents->blocked_content_tab_helper();
  }

  if (source_tab_contents) {
    // Handle blocking of all contents.
    if (source_blocked_content->all_contents_blocked()) {
      source_blocked_content->AddTabContents(new_tab_contents,
                                             disposition,
                                             initial_pos,
                                             user_gesture);
      return;
    }

    // Handle blocking of popups.
    if ((disposition == NEW_POPUP) && !user_gesture &&
        !CommandLine::ForCurrentProcess()->HasSwitch(
            switches::kDisablePopupBlocking)) {
      // Unrequested popups from normal pages are constrained unless they're in
      // the white list.  The popup owner will handle checking this.
      source_tab_contents->blocked_content_tab_helper()->
          AddPopup(new_tab_contents, initial_pos, user_gesture);
      return;
    }

    new_contents->GetRenderViewHost()->DisassociateFromPopupCount();
  }

  NavigateParams params(browser, new_tab_contents);
  params.source_contents = source_contents ?
      GetTabContentsAt(browser, GetIndexOfTab(browser, source_contents)) : NULL;
  params.disposition = disposition;
  params.window_bounds = initial_pos;
  params.window_action = NavigateParams::SHOW_WINDOW;
  params.user_gesture = user_gesture;
  Navigate(&params);
}

void CloseWebContents(Browser* browser, content::WebContents* contents) {
  int index = browser->tab_strip_model()->GetIndexOfWebContents(contents);
  if (index == TabStripModel::kNoTab) {
    NOTREACHED() << "CloseWebContents called for tab not in our strip";
    return;
  }
  browser->tab_strip_model()->CloseTabContentsAt(
      index,
      TabStripModel::CLOSE_CREATE_HISTORICAL_TAB);
}

void CloseAllTabs(Browser* browser) {
  browser->tab_strip_model()->CloseAllTabs();
}

TabContents* TabContentsFactory(
    Profile* profile,
    content::SiteInstance* site_instance,
    int routing_id,
    const content::WebContents* base_web_contents) {
  return BrowserTabstripTabContentsCreator::CreateTabContents(
      content::WebContents::Create(profile,
      site_instance,
      routing_id,
      base_web_contents));
}

TabContents* TabContentsWithSessionStorageFactory(
    Profile* profile,
    content::SiteInstance* site_instance,
    int routing_id,
    const content::WebContents* base_web_contents,
    const content::SessionStorageNamespaceMap& session_storage_namespace_map) {
  return BrowserTabstripTabContentsCreator::CreateTabContents(
      content::WebContents::CreateWithSessionStorage(
          profile,
          site_instance,
          routing_id,
          base_web_contents,
          session_storage_namespace_map));
}

}  // namespace chrome