app_shell: Add version number in user agent
[chromium-blink-merge.git] / chrome / browser / ui / browser_tab_strip_model_delegate.cc
blobff7b0b7738c398b3a1db6d5dd7bb1955592ee234
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"
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"
29 namespace chrome {
31 ////////////////////////////////////////////////////////////////////////////////
32 // BrowserTabStripModelDelegate, public:
34 BrowserTabStripModelDelegate::BrowserTabStripModelDelegate(Browser* browser)
35 : browser_(browser),
36 weak_factory_(this) {
39 BrowserTabStripModelDelegate::~BrowserTabStripModelDelegate() {
42 ////////////////////////////////////////////////////////////////////////////////
43 // BrowserTabStripModelDelegate, TabStripModelDelegate implementation:
45 void BrowserTabStripModelDelegate::AddTabAt(const GURL& url,
46 int index,
47 bool foreground) {
48 chrome::AddTabAt(browser_, url, index, foreground);
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));
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.
71 if (i == 0)
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);
83 return browser;
86 void BrowserTabStripModelDelegate::WillAddWebContents(
87 content::WebContents* contents) {
88 TabHelpers::AttachTabHelpers(contents);
91 int BrowserTabStripModelDelegate::GetDragActions() const {
92 return TabStripModelDelegate::TAB_TEAROFF_ACTION |
93 (browser_->tab_strip_model()->count() > 1
94 ? TabStripModelDelegate::TAB_MOVE_ACTION : 0);
97 bool BrowserTabStripModelDelegate::CanDuplicateContentsAt(int index) {
98 return CanDuplicateTabAt(browser_, index);
101 void BrowserTabStripModelDelegate::DuplicateContentsAt(int index) {
102 DuplicateTabAt(browser_, index);
105 void BrowserTabStripModelDelegate::CreateHistoricalTab(
106 content::WebContents* contents) {
107 // We don't create historical tabs for incognito windows or windows without
108 // profiles.
109 if (!browser_->profile() || browser_->profile()->IsOffTheRecord())
110 return;
112 TabRestoreService* service =
113 TabRestoreServiceFactory::GetForProfile(browser_->profile());
115 // We only create historical tab entries for tabbed browser windows.
116 if (service && browser_->CanSupportWindowFeature(Browser::FEATURE_TABSTRIP)) {
117 service->CreateHistoricalTab(
118 contents,
119 browser_->tab_strip_model()->GetIndexOfWebContents(contents));
123 bool BrowserTabStripModelDelegate::RunUnloadListenerBeforeClosing(
124 content::WebContents* contents) {
125 if (CommandLine::ForCurrentProcess()->HasSwitch(
126 switches::kEnableFastUnload)) {
127 return chrome::FastUnloadController::RunUnloadEventsHelper(contents);
129 return chrome::UnloadController::RunUnloadEventsHelper(contents);
132 bool BrowserTabStripModelDelegate::ShouldRunUnloadListenerBeforeClosing(
133 content::WebContents* contents) {
134 if (CommandLine::ForCurrentProcess()->HasSwitch(
135 switches::kEnableFastUnload)) {
136 return chrome::FastUnloadController::ShouldRunUnloadEventsHelper(contents);
138 return chrome::UnloadController::ShouldRunUnloadEventsHelper(contents);
141 bool BrowserTabStripModelDelegate::CanBookmarkAllTabs() const {
142 return chrome::CanBookmarkAllTabs(browser_);
145 void BrowserTabStripModelDelegate::BookmarkAllTabs() {
146 chrome::BookmarkAllTabs(browser_);
149 TabStripModelDelegate::RestoreTabType
150 BrowserTabStripModelDelegate::GetRestoreTabType() {
151 return chrome::GetRestoreTabType(browser_);
154 void BrowserTabStripModelDelegate::RestoreTab() {
155 chrome::RestoreTab(browser_);
158 ////////////////////////////////////////////////////////////////////////////////
159 // BrowserTabStripModelDelegate, private:
161 void BrowserTabStripModelDelegate::CloseFrame() {
162 browser_->window()->Close();
165 } // namespace chrome