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_tab_strip_model_delegate.h" 6 7 #include "base/bind.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_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 "content/public/browser/site_instance.h" 24 #include "content/public/browser/web_contents.h" 25 #include "content/public/browser/web_contents_delegate.h" 26 #include "content/public/common/page_transition_types.h" 27 #include "ipc/ipc_message.h" 28 29 namespace chrome { 30 31 //////////////////////////////////////////////////////////////////////////////// 32 // BrowserTabStripModelDelegate, public: 33 34 BrowserTabStripModelDelegate::BrowserTabStripModelDelegate(Browser* browser) 35 : browser_(browser), 36 weak_factory_(this) { 37 } 38 39 BrowserTabStripModelDelegate::~BrowserTabStripModelDelegate() { 40 } 41 42 //////////////////////////////////////////////////////////////////////////////// 43 // BrowserTabStripModelDelegate, TabStripModelDelegate implementation: 44 45 void BrowserTabStripModelDelegate::AddTabAt(const GURL& url, 46 int index, 47 bool foreground) { 48 chrome::AddTabAt(browser_, url, index, foreground); 49 } 50 51 Browser* BrowserTabStripModelDelegate::CreateNewStripWithContents( 52 const std::vector<NewStripContents>& contentses, 53 const gfx::Rect& window_bounds, 54 bool maximize) { 55 DCHECK(browser_->CanSupportWindowFeature(Browser::FEATURE_TABSTRIP)); 56 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(); 65 66 for (size_t i = 0; i < contentses.size(); ++i) { 67 NewStripContents item = contentses[i]; 68 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. 71 if (i == 0) 72 item.add_types |= TabStripModel::ADD_ACTIVE; 73 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); 81 } 82 83 return browser; 84 } 85 86 void BrowserTabStripModelDelegate::WillAddWebContents( 87 content::WebContents* contents) { 88 TabHelpers::AttachTabHelpers(contents); 89 } 90 91 int BrowserTabStripModelDelegate::GetDragActions() const { 92 return TabStripModelDelegate::TAB_TEAROFF_ACTION | 93 (browser_->tab_strip_model()->count() > 1 94 ? TabStripModelDelegate::TAB_MOVE_ACTION : 0); 95 } 96 97 bool BrowserTabStripModelDelegate::CanDuplicateContentsAt(int index) { 98 return CanDuplicateTabAt(browser_, index); 99 } 100 101 void BrowserTabStripModelDelegate::DuplicateContentsAt(int index) { 102 DuplicateTabAt(browser_, index); 103 } 104 105 void BrowserTabStripModelDelegate::CloseFrameAfterDragSession() { 106 #if !defined(OS_MACOSX) 107 // This is scheduled to run after we return to the message loop because 108 // otherwise the frame will think the drag session is still active and ignore 109 // the request. 110 base::MessageLoop::current()->PostTask( 111 FROM_HERE, 112 base::Bind(&BrowserTabStripModelDelegate::CloseFrame, 113 weak_factory_.GetWeakPtr())); 114 #endif 115 } 116 117 void BrowserTabStripModelDelegate::CreateHistoricalTab( 118 content::WebContents* contents) { 119 // We don't create historical tabs for incognito windows or windows without 120 // profiles. 121 if (!browser_->profile() || browser_->profile()->IsOffTheRecord()) 122 return; 123 124 TabRestoreService* service = 125 TabRestoreServiceFactory::GetForProfile(browser_->profile()); 126 127 // We only create historical tab entries for tabbed browser windows. 128 if (service && browser_->CanSupportWindowFeature(Browser::FEATURE_TABSTRIP)) { 129 service->CreateHistoricalTab( 130 contents, 131 browser_->tab_strip_model()->GetIndexOfWebContents(contents)); 132 } 133 } 134 135 bool BrowserTabStripModelDelegate::RunUnloadListenerBeforeClosing( 136 content::WebContents* contents) { 137 if (CommandLine::ForCurrentProcess()->HasSwitch( 138 switches::kEnableFastUnload)) { 139 return chrome::FastUnloadController::RunUnloadEventsHelper(contents); 140 } 141 return chrome::UnloadController::RunUnloadEventsHelper(contents); 142 } 143 144 bool BrowserTabStripModelDelegate::ShouldRunUnloadListenerBeforeClosing( 145 content::WebContents* contents) { 146 if (CommandLine::ForCurrentProcess()->HasSwitch( 147 switches::kEnableFastUnload)) { 148 return chrome::FastUnloadController::ShouldRunUnloadEventsHelper(contents); 149 } 150 return chrome::UnloadController::ShouldRunUnloadEventsHelper(contents); 151 } 152 153 bool BrowserTabStripModelDelegate::CanBookmarkAllTabs() const { 154 return chrome::CanBookmarkAllTabs(browser_); 155 } 156 157 void BrowserTabStripModelDelegate::BookmarkAllTabs() { 158 chrome::BookmarkAllTabs(browser_); 159 } 160 161 TabStripModelDelegate::RestoreTabType 162 BrowserTabStripModelDelegate::GetRestoreTabType() { 163 return chrome::GetRestoreTabType(browser_); 164 } 165 166 void BrowserTabStripModelDelegate::RestoreTab() { 167 chrome::RestoreTab(browser_); 168 } 169 170 //////////////////////////////////////////////////////////////////////////////// 171 // BrowserTabStripModelDelegate, private: 172 173 void BrowserTabStripModelDelegate::CloseFrame() { 174 browser_->window()->Close(); 175 } 176 177 } // namespace chrome 178