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/task_management/web_contents_tags.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_commands.h"
16 #include "chrome/browser/ui/browser_navigator.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/tab_helpers.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 "ipc/ipc_message.h"
31 ////////////////////////////////////////////////////////////////////////////////
32 // BrowserTabStripModelDelegate, public:
34 BrowserTabStripModelDelegate::BrowserTabStripModelDelegate(Browser
* browser
)
39 BrowserTabStripModelDelegate::~BrowserTabStripModelDelegate() {
42 ////////////////////////////////////////////////////////////////////////////////
43 // BrowserTabStripModelDelegate, TabStripModelDelegate implementation:
45 void BrowserTabStripModelDelegate::AddTabAt(const GURL
& url
,
48 chrome::AddTabAt(browser_
, url
, index
, foreground
);
51 Browser
* BrowserTabStripModelDelegate::CreateNewStripWithContents(
52 const std::vector
<NewStripContents
>& contentses
,
53 const gfx::Rect
& window_bounds
,
55 DCHECK(browser_
->CanSupportWindowFeature(Browser::FEATURE_TABSTRIP
));
57 // Create an empty new browser window the same size as the old one.
58 Browser::CreateParams
params(browser_
->profile(),
59 browser_
->host_desktop_type());
60 params
.initial_bounds
= window_bounds
;
61 params
.initial_show_state
=
62 maximize
? ui::SHOW_STATE_MAXIMIZED
: ui::SHOW_STATE_NORMAL
;
63 Browser
* browser
= new Browser(params
);
64 TabStripModel
* new_model
= browser
->tab_strip_model();
66 for (size_t i
= 0; i
< contentses
.size(); ++i
) {
67 NewStripContents item
= contentses
[i
];
69 // Enforce that there is an active tab in the strip at all times by forcing
70 // the first web contents to be marked as active.
72 item
.add_types
|= TabStripModel::ADD_ACTIVE
;
74 new_model
->InsertWebContentsAt(
75 static_cast<int>(i
), item
.web_contents
, item
.add_types
);
76 // Make sure the loading state is updated correctly, otherwise the throbber
77 // won't start if the page is loading.
78 // TODO(beng): find a better way of doing this.
79 static_cast<content::WebContentsDelegate
*>(browser
)->
80 LoadingStateChanged(item
.web_contents
, true);
86 void BrowserTabStripModelDelegate::WillAddWebContents(
87 content::WebContents
* contents
) {
88 TabHelpers::AttachTabHelpers(contents
);
90 // Make the tab show up in the task manager.
91 task_management::WebContentsTags::CreateForTabContents(contents
);
94 int BrowserTabStripModelDelegate::GetDragActions() const {
95 return TabStripModelDelegate::TAB_TEAROFF_ACTION
|
96 (browser_
->tab_strip_model()->count() > 1
97 ? TabStripModelDelegate::TAB_MOVE_ACTION
: 0);
100 bool BrowserTabStripModelDelegate::CanDuplicateContentsAt(int index
) {
101 return CanDuplicateTabAt(browser_
, index
);
104 void BrowserTabStripModelDelegate::DuplicateContentsAt(int index
) {
105 DuplicateTabAt(browser_
, index
);
108 void BrowserTabStripModelDelegate::CreateHistoricalTab(
109 content::WebContents
* contents
) {
110 // We don't create historical tabs for incognito windows or windows without
112 if (!browser_
->profile() || browser_
->profile()->IsOffTheRecord())
115 TabRestoreService
* service
=
116 TabRestoreServiceFactory::GetForProfile(browser_
->profile());
118 // We only create historical tab entries for tabbed browser windows.
119 if (service
&& browser_
->CanSupportWindowFeature(Browser::FEATURE_TABSTRIP
)) {
120 service
->CreateHistoricalTab(
122 browser_
->tab_strip_model()->GetIndexOfWebContents(contents
));
126 bool BrowserTabStripModelDelegate::RunUnloadListenerBeforeClosing(
127 content::WebContents
* contents
) {
128 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
129 switches::kEnableFastUnload
)) {
130 return chrome::FastUnloadController::RunUnloadEventsHelper(contents
);
132 return chrome::UnloadController::RunUnloadEventsHelper(contents
);
135 bool BrowserTabStripModelDelegate::ShouldRunUnloadListenerBeforeClosing(
136 content::WebContents
* contents
) {
137 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
138 switches::kEnableFastUnload
)) {
139 return chrome::FastUnloadController::ShouldRunUnloadEventsHelper(contents
);
141 return chrome::UnloadController::ShouldRunUnloadEventsHelper(contents
);
144 bool BrowserTabStripModelDelegate::CanBookmarkAllTabs() const {
145 return chrome::CanBookmarkAllTabs(browser_
);
148 void BrowserTabStripModelDelegate::BookmarkAllTabs() {
149 chrome::BookmarkAllTabs(browser_
);
152 TabStripModelDelegate::RestoreTabType
153 BrowserTabStripModelDelegate::GetRestoreTabType() {
154 return chrome::GetRestoreTabType(browser_
);
157 void BrowserTabStripModelDelegate::RestoreTab() {
158 chrome::RestoreTab(browser_
);
161 ////////////////////////////////////////////////////////////////////////////////
162 // BrowserTabStripModelDelegate, private:
164 void BrowserTabStripModelDelegate::CloseFrame() {
165 browser_
->window()->Close();
168 } // namespace chrome