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_factory.h"
12 #include "chrome/browser/task_management/web_contents_tags.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_tabstrip.h"
17 #include "chrome/browser/ui/browser_window.h"
18 #include "chrome/browser/ui/fast_unload_controller.h"
19 #include "chrome/browser/ui/tab_helpers.h"
20 #include "chrome/browser/ui/tabs/tab_strip_model.h"
21 #include "chrome/browser/ui/unload_controller.h"
22 #include "chrome/common/chrome_switches.h"
23 #include "components/sessions/content/content_live_tab.h"
24 #include "components/sessions/core/tab_restore_service.h"
25 #include "content/public/browser/site_instance.h"
26 #include "content/public/browser/web_contents.h"
27 #include "content/public/browser/web_contents_delegate.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
,
56 DCHECK(browser_
->CanSupportWindowFeature(Browser::FEATURE_TABSTRIP
));
58 // Create an empty new browser window the same size as the old one.
59 Browser::CreateParams
params(browser_
->profile(),
60 browser_
->host_desktop_type());
61 params
.initial_bounds
= window_bounds
;
62 params
.initial_show_state
=
63 maximize
? ui::SHOW_STATE_MAXIMIZED
: ui::SHOW_STATE_NORMAL
;
64 Browser
* browser
= new Browser(params
);
65 TabStripModel
* new_model
= browser
->tab_strip_model();
67 for (size_t i
= 0; i
< contentses
.size(); ++i
) {
68 NewStripContents item
= contentses
[i
];
70 // Enforce that there is an active tab in the strip at all times by forcing
71 // the first web contents to be marked as active.
73 item
.add_types
|= TabStripModel::ADD_ACTIVE
;
75 new_model
->InsertWebContentsAt(
76 static_cast<int>(i
), item
.web_contents
, item
.add_types
);
77 // Make sure the loading state is updated correctly, otherwise the throbber
78 // won't start if the page is loading.
79 // TODO(beng): find a better way of doing this.
80 static_cast<content::WebContentsDelegate
*>(browser
)->
81 LoadingStateChanged(item
.web_contents
, true);
87 void BrowserTabStripModelDelegate::WillAddWebContents(
88 content::WebContents
* contents
) {
89 TabHelpers::AttachTabHelpers(contents
);
91 // Make the tab show up in the task manager.
92 task_management::WebContentsTags::CreateForTabContents(contents
);
95 int BrowserTabStripModelDelegate::GetDragActions() const {
96 return TabStripModelDelegate::TAB_TEAROFF_ACTION
|
97 (browser_
->tab_strip_model()->count() > 1
98 ? TabStripModelDelegate::TAB_MOVE_ACTION
: 0);
101 bool BrowserTabStripModelDelegate::CanDuplicateContentsAt(int index
) {
102 return CanDuplicateTabAt(browser_
, index
);
105 void BrowserTabStripModelDelegate::DuplicateContentsAt(int index
) {
106 DuplicateTabAt(browser_
, index
);
109 void BrowserTabStripModelDelegate::CreateHistoricalTab(
110 content::WebContents
* contents
) {
111 // We don't create historical tabs for incognito windows or windows without
113 if (!browser_
->profile() || browser_
->profile()->IsOffTheRecord())
116 sessions::TabRestoreService
* service
=
117 TabRestoreServiceFactory::GetForProfile(browser_
->profile());
119 // We only create historical tab entries for tabbed browser windows.
120 if (service
&& browser_
->CanSupportWindowFeature(Browser::FEATURE_TABSTRIP
)) {
121 service
->CreateHistoricalTab(
122 sessions::ContentLiveTab::FromWebContents(contents
),
123 browser_
->tab_strip_model()->GetIndexOfWebContents(contents
));
127 bool BrowserTabStripModelDelegate::RunUnloadListenerBeforeClosing(
128 content::WebContents
* contents
) {
129 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
130 switches::kEnableFastUnload
)) {
131 return chrome::FastUnloadController::RunUnloadEventsHelper(contents
);
133 return chrome::UnloadController::RunUnloadEventsHelper(contents
);
136 bool BrowserTabStripModelDelegate::ShouldRunUnloadListenerBeforeClosing(
137 content::WebContents
* contents
) {
138 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
139 switches::kEnableFastUnload
)) {
140 return chrome::FastUnloadController::ShouldRunUnloadEventsHelper(contents
);
142 return chrome::UnloadController::ShouldRunUnloadEventsHelper(contents
);
145 bool BrowserTabStripModelDelegate::CanBookmarkAllTabs() const {
146 return chrome::CanBookmarkAllTabs(browser_
);
149 void BrowserTabStripModelDelegate::BookmarkAllTabs() {
150 chrome::BookmarkAllTabs(browser_
);
153 TabStripModelDelegate::RestoreTabType
154 BrowserTabStripModelDelegate::GetRestoreTabType() {
155 return chrome::GetRestoreTabType(browser_
);
158 void BrowserTabStripModelDelegate::RestoreTab() {
159 chrome::RestoreTab(browser_
);
162 ////////////////////////////////////////////////////////////////////////////////
163 // BrowserTabStripModelDelegate, private:
165 void BrowserTabStripModelDelegate::CloseFrame() {
166 browser_
->window()->Close();
169 } // namespace chrome