Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / browser_tab_strip_model_delegate.cc
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/dock_info.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 "content/public/common/page_transition_types.h"
28 #include "ipc/ipc_message.h"
29
30 namespace chrome {
31
32 ////////////////////////////////////////////////////////////////////////////////
33 // BrowserTabStripModelDelegate, public:
34
35 BrowserTabStripModelDelegate::BrowserTabStripModelDelegate(Browser* browser)
36     : browser_(browser),
37       weak_factory_(this) {
38 }
39
40 BrowserTabStripModelDelegate::~BrowserTabStripModelDelegate() {
41 }
42
43 ////////////////////////////////////////////////////////////////////////////////
44 // BrowserTabStripModelDelegate, TabStripModelDelegate implementation:
45
46 void BrowserTabStripModelDelegate::AddTabAt(const GURL& url,
47                                             int index,
48                                             bool foreground) {
49   chrome::AddTabAt(browser_, url, index, foreground);
50 }
51
52 Browser* BrowserTabStripModelDelegate::CreateNewStripWithContents(
53     const std::vector<NewStripContents>& contentses,
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::CreateParams params(browser_->profile(),
65                                browser_->host_desktop_type());
66   params.initial_bounds = new_window_bounds;
67   params.initial_show_state =
68       maximize ? ui::SHOW_STATE_MAXIMIZED : ui::SHOW_STATE_NORMAL;
69   Browser* browser = new Browser(params);
70   TabStripModel* new_model = browser->tab_strip_model();
71
72   for (size_t i = 0; i < contentses.size(); ++i) {
73     NewStripContents item = contentses[i];
74
75     // Enforce that there is an active tab in the strip at all times by forcing
76     // the first web contents to be marked as active.
77     if (i == 0)
78       item.add_types |= TabStripModel::ADD_ACTIVE;
79
80     new_model->InsertWebContentsAt(
81         static_cast<int>(i), item.web_contents, item.add_types);
82     // Make sure the loading state is updated correctly, otherwise the throbber
83     // won't start if the page is loading.
84     // TODO(beng): find a better way of doing this.
85     static_cast<content::WebContentsDelegate*>(browser)->
86         LoadingStateChanged(item.web_contents);
87   }
88
89   return browser;
90 }
91
92 void BrowserTabStripModelDelegate::WillAddWebContents(
93     content::WebContents* contents) {
94   TabHelpers::AttachTabHelpers(contents);
95 }
96
97 int BrowserTabStripModelDelegate::GetDragActions() const {
98   return TabStripModelDelegate::TAB_TEAROFF_ACTION |
99       (browser_->tab_strip_model()->count() > 1
100           ? TabStripModelDelegate::TAB_MOVE_ACTION : 0);
101 }
102
103 bool BrowserTabStripModelDelegate::CanDuplicateContentsAt(int index) {
104   return CanDuplicateTabAt(browser_, index);
105 }
106
107 void BrowserTabStripModelDelegate::DuplicateContentsAt(int index) {
108   DuplicateTabAt(browser_, index);
109 }
110
111 void BrowserTabStripModelDelegate::CloseFrameAfterDragSession() {
112 #if !defined(OS_MACOSX)
113   // This is scheduled to run after we return to the message loop because
114   // otherwise the frame will think the drag session is still active and ignore
115   // the request.
116   base::MessageLoop::current()->PostTask(
117       FROM_HERE,
118       base::Bind(&BrowserTabStripModelDelegate::CloseFrame,
119                  weak_factory_.GetWeakPtr()));
120 #endif
121 }
122
123 void BrowserTabStripModelDelegate::CreateHistoricalTab(
124     content::WebContents* contents) {
125   // We don't create historical tabs for incognito windows or windows without
126   // profiles.
127   if (!browser_->profile() || browser_->profile()->IsOffTheRecord())
128     return;
129
130   TabRestoreService* service =
131       TabRestoreServiceFactory::GetForProfile(browser_->profile());
132
133   // We only create historical tab entries for tabbed browser windows.
134   if (service && browser_->CanSupportWindowFeature(Browser::FEATURE_TABSTRIP)) {
135     service->CreateHistoricalTab(
136         contents,
137         browser_->tab_strip_model()->GetIndexOfWebContents(contents));
138   }
139 }
140
141 bool BrowserTabStripModelDelegate::RunUnloadListenerBeforeClosing(
142     content::WebContents* contents) {
143   if (CommandLine::ForCurrentProcess()->HasSwitch(
144           switches::kEnableFastUnload)) {
145     return chrome::FastUnloadController::RunUnloadEventsHelper(contents);
146   }
147   return chrome::UnloadController::RunUnloadEventsHelper(contents);
148 }
149
150 bool BrowserTabStripModelDelegate::ShouldRunUnloadListenerBeforeClosing(
151     content::WebContents* contents) {
152   if (CommandLine::ForCurrentProcess()->HasSwitch(
153           switches::kEnableFastUnload)) {
154     return chrome::FastUnloadController::ShouldRunUnloadEventsHelper(contents);
155   }
156   return chrome::UnloadController::ShouldRunUnloadEventsHelper(contents);
157 }
158
159 bool BrowserTabStripModelDelegate::CanBookmarkAllTabs() const {
160   return chrome::CanBookmarkAllTabs(browser_);
161 }
162
163 void BrowserTabStripModelDelegate::BookmarkAllTabs() {
164   chrome::BookmarkAllTabs(browser_);
165 }
166
167 TabStripModelDelegate::RestoreTabType
168 BrowserTabStripModelDelegate::GetRestoreTabType() {
169   return chrome::GetRestoreTabType(browser_);
170 }
171
172 void BrowserTabStripModelDelegate::RestoreTab() {
173   chrome::RestoreTab(browser_);
174 }
175
176 ////////////////////////////////////////////////////////////////////////////////
177 // BrowserTabStripModelDelegate, private:
178
179 void BrowserTabStripModelDelegate::CloseFrame() {
180   browser_->window()->Close();
181 }
182
183 }  // namespace chrome