OLD | NEW |
(Empty) | |
| 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/message_loop.h" |
| 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/sessions/tab_restore_service.h" |
| 11 #include "chrome/browser/sessions/tab_restore_service_factory.h" |
| 12 #include "chrome/browser/ui/browser.h" |
| 13 #include "chrome/browser/ui/browser_commands.h" |
| 14 #include "chrome/browser/ui/browser_navigator.h" |
| 15 #include "chrome/browser/ui/browser_tabstrip.h" |
| 16 #include "chrome/browser/ui/browser_window.h" |
| 17 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 18 #include "chrome/browser/ui/tabs/dock_info.h" |
| 19 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 20 #include "chrome/common/url_constants.h" |
| 21 #include "content/public/browser/site_instance.h" |
| 22 #include "content/public/browser/web_contents.h" |
| 23 #include "content/public/browser/web_contents_delegate.h" |
| 24 #include "content/public/common/page_transition_types.h" |
| 25 #include "ipc/ipc_message.h" |
| 26 |
| 27 namespace chrome { |
| 28 |
| 29 //////////////////////////////////////////////////////////////////////////////// |
| 30 // BrowserTabStripModelDelegate, public: |
| 31 |
| 32 BrowserTabStripModelDelegate::BrowserTabStripModelDelegate(Browser* browser) |
| 33 : browser_(browser), |
| 34 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 35 } |
| 36 |
| 37 BrowserTabStripModelDelegate::~BrowserTabStripModelDelegate() { |
| 38 } |
| 39 |
| 40 //////////////////////////////////////////////////////////////////////////////// |
| 41 // BrowserTabStripModelDelegate, TabStripModelDelegate implementation: |
| 42 |
| 43 TabContents* BrowserTabStripModelDelegate::AddBlankTab(bool foreground) { |
| 44 return chrome::AddBlankTab(browser_, foreground); |
| 45 } |
| 46 |
| 47 TabContents* BrowserTabStripModelDelegate::AddBlankTabAt(int index, |
| 48 bool foreground) { |
| 49 return chrome::AddBlankTabAt(browser_, index, foreground); |
| 50 } |
| 51 |
| 52 Browser* BrowserTabStripModelDelegate::CreateNewStripWithContents( |
| 53 TabContents* detached_contents, |
| 54 const gfx::Rect& window_bounds, |
| 55 const DockInfo& dock_info, |
| 56 bool maximize) { |
| 57 DCHECK(browser_->CanSupportWindowFeature(Browser::FEATURE_TABSTRIP)); |
| 58 |
| 59 gfx::Rect new_window_bounds = window_bounds; |
| 60 if (dock_info.GetNewWindowBounds(&new_window_bounds, &maximize)) |
| 61 dock_info.AdjustOtherWindowBounds(); |
| 62 |
| 63 // Create an empty new browser window the same size as the old one. |
| 64 Browser* browser = new Browser(Browser::TYPE_TABBED, browser_->profile()); |
| 65 browser->set_override_bounds(new_window_bounds); |
| 66 browser->set_initial_show_state( |
| 67 maximize ? ui::SHOW_STATE_MAXIMIZED : ui::SHOW_STATE_NORMAL); |
| 68 browser->InitBrowserWindow(); |
| 69 browser->tab_strip_model()->AppendTabContents(detached_contents, true); |
| 70 // Make sure the loading state is updated correctly, otherwise the throbber |
| 71 // won't start if the page is loading. |
| 72 // TODO(beng): find a better way of doing this. |
| 73 static_cast<content::WebContentsDelegate*>(browser)-> |
| 74 LoadingStateChanged(detached_contents->web_contents()); |
| 75 return browser; |
| 76 } |
| 77 |
| 78 int BrowserTabStripModelDelegate::GetDragActions() const { |
| 79 return TabStripModelDelegate::TAB_TEAROFF_ACTION | |
| 80 (browser_->tab_count() > 1 ? TabStripModelDelegate::TAB_MOVE_ACTION : 0); |
| 81 } |
| 82 |
| 83 TabContents* BrowserTabStripModelDelegate::CreateTabContentsForURL( |
| 84 const GURL& url, const content::Referrer& referrer, Profile* profile, |
| 85 content::PageTransition transition, bool defer_load, |
| 86 content::SiteInstance* instance) const { |
| 87 TabContents* contents = TabContentsFactory(profile, instance, |
| 88 MSG_ROUTING_NONE, GetActiveWebContents(browser_), NULL); |
| 89 if (!defer_load) { |
| 90 // Load the initial URL before adding the new tab contents to the tab strip |
| 91 // so that the tab contents has navigation state. |
| 92 contents->web_contents()->GetController().LoadURL( |
| 93 url, referrer, transition, std::string()); |
| 94 } |
| 95 |
| 96 return contents; |
| 97 } |
| 98 |
| 99 bool BrowserTabStripModelDelegate::CanDuplicateContentsAt(int index) { |
| 100 return CanDuplicateTabAt(browser_, index); |
| 101 } |
| 102 |
| 103 void BrowserTabStripModelDelegate::DuplicateContentsAt(int index) { |
| 104 DuplicateTabAt(browser_, index); |
| 105 } |
| 106 |
| 107 void BrowserTabStripModelDelegate::CloseFrameAfterDragSession() { |
| 108 #if !defined(OS_MACOSX) |
| 109 // This is scheduled to run after we return to the message loop because |
| 110 // otherwise the frame will think the drag session is still active and ignore |
| 111 // the request. |
| 112 MessageLoop::current()->PostTask( |
| 113 FROM_HERE, |
| 114 base::Bind(&BrowserTabStripModelDelegate::CloseFrame, |
| 115 weak_factory_.GetWeakPtr())); |
| 116 #endif |
| 117 } |
| 118 |
| 119 void BrowserTabStripModelDelegate::CreateHistoricalTab(TabContents* contents) { |
| 120 // We don't create historical tabs for incognito windows or windows without |
| 121 // profiles. |
| 122 if (!browser_->profile() || browser_->profile()->IsOffTheRecord()) |
| 123 return; |
| 124 |
| 125 // We don't create historical tabs for print preview tabs. |
| 126 if (contents->web_contents()->GetURL() == GURL(kChromeUIPrintURL)) |
| 127 return; |
| 128 |
| 129 TabRestoreService* service = |
| 130 TabRestoreServiceFactory::GetForProfile(browser_->profile()); |
| 131 |
| 132 // We only create historical tab entries for tabbed browser windows. |
| 133 if (service && browser_->CanSupportWindowFeature(Browser::FEATURE_TABSTRIP)) { |
| 134 service->CreateHistoricalTab(contents->web_contents(), |
| 135 browser_->tab_strip_model()->GetIndexOfTabContents(contents)); |
| 136 } |
| 137 } |
| 138 |
| 139 bool BrowserTabStripModelDelegate::RunUnloadListenerBeforeClosing( |
| 140 TabContents* contents) { |
| 141 return Browser::RunUnloadEventsHelper(contents->web_contents()); |
| 142 } |
| 143 |
| 144 bool BrowserTabStripModelDelegate::CanBookmarkAllTabs() const { |
| 145 return chrome::CanBookmarkAllTabs(browser_); |
| 146 } |
| 147 |
| 148 void BrowserTabStripModelDelegate::BookmarkAllTabs() { |
| 149 chrome::BookmarkAllTabs(browser_); |
| 150 } |
| 151 |
| 152 bool BrowserTabStripModelDelegate::CanRestoreTab() { |
| 153 return chrome::CanRestoreTab(browser_); |
| 154 } |
| 155 |
| 156 void BrowserTabStripModelDelegate::RestoreTab() { |
| 157 chrome::RestoreTab(browser_); |
| 158 } |
| 159 |
| 160 //////////////////////////////////////////////////////////////////////////////// |
| 161 // BrowserTabStripModelDelegate, private: |
| 162 |
| 163 void BrowserTabStripModelDelegate::CloseFrame() { |
| 164 browser_->window()->Close(); |
| 165 } |
| 166 |
| 167 } // namespace chrome |
OLD | NEW |