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.
5 #include "chrome/browser/ui/browser_tab_strip_model_delegate.h"
8 #include "base/command_line.h"
9 #include "base/message_loop/message_loop.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/sessions/tab_restore_service.h"
12 #include "chrome/browser/sessions/tab_restore_service_factory.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_commands.h"
15 #include "chrome/browser/ui/browser_navigator.h"
16 #include "chrome/browser/ui/browser_tab_contents.h"
17 #include "chrome/browser/ui/browser_tabstrip.h"
18 #include "chrome/browser/ui/browser_window.h"
19 #include "chrome/browser/ui/fast_unload_controller.h"
20 #include "chrome/browser/ui/tabs/dock_info.h"
21 #include "chrome/browser/ui/tabs/tab_strip_model.h"
22 #include "chrome/browser/ui/unload_controller.h"
23 #include "chrome/common/chrome_switches.h"
24 #include "content/public/browser/site_instance.h"
25 #include "content/public/browser/web_contents.h"
26 #include "content/public/browser/web_contents_delegate.h"
27 #include "content/public/common/page_transition_types.h"
28 #include "ipc/ipc_message.h"
32 ////////////////////////////////////////////////////////////////////////////////
33 // BrowserTabStripModelDelegate, public:
35 BrowserTabStripModelDelegate::BrowserTabStripModelDelegate(Browser
* browser
)
40 BrowserTabStripModelDelegate::~BrowserTabStripModelDelegate() {
43 ////////////////////////////////////////////////////////////////////////////////
44 // BrowserTabStripModelDelegate, TabStripModelDelegate implementation:
46 void BrowserTabStripModelDelegate::AddTabAt(const GURL
& url
,
49 chrome::AddTabAt(browser_
, url
, index
, foreground
);
52 Browser
* BrowserTabStripModelDelegate::CreateNewStripWithContents(
53 const std::vector
<NewStripContents
>& contentses
,
54 const gfx::Rect
& window_bounds
,
55 const DockInfo
& dock_info
,
57 DCHECK(browser_
->CanSupportWindowFeature(Browser::FEATURE_TABSTRIP
));
59 gfx::Rect new_window_bounds
= window_bounds
;
60 if (dock_info
.GetNewWindowBounds(&new_window_bounds
, &maximize
))
61 dock_info
.AdjustOtherWindowBounds();
63 // Create an empty new browser window the same size as the old one.
64 Browser::CreateParams
params(browser_
->profile(),
65 browser_
->host_desktop_type());
66 params
.initial_bounds
= new_window_bounds
;
67 params
.initial_show_state
=
68 maximize
? ui::SHOW_STATE_MAXIMIZED
: ui::SHOW_STATE_NORMAL
;
69 Browser
* browser
= new Browser(params
);
70 TabStripModel
* new_model
= browser
->tab_strip_model();
72 for (size_t i
= 0; i
< contentses
.size(); ++i
) {
73 NewStripContents item
= contentses
[i
];
75 // Enforce that there is an active tab in the strip at all times by forcing
76 // the first web contents to be marked as active.
78 item
.add_types
|= TabStripModel::ADD_ACTIVE
;
80 new_model
->InsertWebContentsAt(
81 static_cast<int>(i
), item
.web_contents
, item
.add_types
);
82 // Make sure the loading state is updated correctly, otherwise the throbber
83 // won't start if the page is loading.
84 // TODO(beng): find a better way of doing this.
85 static_cast<content::WebContentsDelegate
*>(browser
)->
86 LoadingStateChanged(item
.web_contents
);
92 void BrowserTabStripModelDelegate::WillAddWebContents(
93 content::WebContents
* contents
) {
94 BrowserTabContents::AttachTabHelpers(contents
);
97 int BrowserTabStripModelDelegate::GetDragActions() const {
98 return TabStripModelDelegate::TAB_TEAROFF_ACTION
|
99 (browser_
->tab_strip_model()->count() > 1
100 ? TabStripModelDelegate::TAB_MOVE_ACTION
: 0);
103 bool BrowserTabStripModelDelegate::CanDuplicateContentsAt(int index
) {
104 return CanDuplicateTabAt(browser_
, index
);
107 void BrowserTabStripModelDelegate::DuplicateContentsAt(int index
) {
108 DuplicateTabAt(browser_
, index
);
111 void BrowserTabStripModelDelegate::CloseFrameAfterDragSession() {
112 #if !defined(OS_MACOSX)
113 // This is scheduled to run after we return to the message loop because
114 // otherwise the frame will think the drag session is still active and ignore
116 base::MessageLoop::current()->PostTask(
118 base::Bind(&BrowserTabStripModelDelegate::CloseFrame
,
119 weak_factory_
.GetWeakPtr()));
123 void BrowserTabStripModelDelegate::CreateHistoricalTab(
124 content::WebContents
* contents
) {
125 // We don't create historical tabs for incognito windows or windows without
127 if (!browser_
->profile() || browser_
->profile()->IsOffTheRecord())
130 TabRestoreService
* service
=
131 TabRestoreServiceFactory::GetForProfile(browser_
->profile());
133 // We only create historical tab entries for tabbed browser windows.
134 if (service
&& browser_
->CanSupportWindowFeature(Browser::FEATURE_TABSTRIP
)) {
135 service
->CreateHistoricalTab(
137 browser_
->tab_strip_model()->GetIndexOfWebContents(contents
));
141 bool BrowserTabStripModelDelegate::RunUnloadListenerBeforeClosing(
142 content::WebContents
* contents
) {
143 if (CommandLine::ForCurrentProcess()->HasSwitch(
144 switches::kEnableFastUnload
)) {
145 return chrome::FastUnloadController::RunUnloadEventsHelper(contents
);
147 return chrome::UnloadController::RunUnloadEventsHelper(contents
);
150 bool BrowserTabStripModelDelegate::ShouldRunUnloadListenerBeforeClosing(
151 content::WebContents
* contents
) {
152 if (CommandLine::ForCurrentProcess()->HasSwitch(
153 switches::kEnableFastUnload
)) {
154 return chrome::FastUnloadController::ShouldRunUnloadEventsHelper(contents
);
156 return chrome::UnloadController::ShouldRunUnloadEventsHelper(contents
);
159 bool BrowserTabStripModelDelegate::CanBookmarkAllTabs() const {
160 return chrome::CanBookmarkAllTabs(browser_
);
163 void BrowserTabStripModelDelegate::BookmarkAllTabs() {
164 chrome::BookmarkAllTabs(browser_
);
167 TabStripModelDelegate::RestoreTabType
168 BrowserTabStripModelDelegate::GetRestoreTabType() {
169 return chrome::GetRestoreTabType(browser_
);
172 void BrowserTabStripModelDelegate::RestoreTab() {
173 chrome::RestoreTab(browser_
);
176 ////////////////////////////////////////////////////////////////////////////////
177 // BrowserTabStripModelDelegate, private:
179 void BrowserTabStripModelDelegate::CloseFrame() {
180 browser_
->window()->Close();
183 } // namespace chrome