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_tabstrip.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.h" |
| 10 #include "chrome/browser/ui/browser.h" |
| 11 #include "chrome/browser/ui/browser_navigator.h" |
| 12 #include "chrome/browser/ui/browser_window.h" |
| 13 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 15 #include "chrome/common/chrome_switches.h" |
| 16 #include "content/public/browser/navigation_entry.h" |
| 17 #include "content/public/browser/render_view_host.h" |
| 18 #include "content/public/browser/web_contents.h" |
| 19 |
| 20 namespace chrome { |
| 21 |
| 22 int GetIndexOfTab(const Browser* browser, |
| 23 const content::WebContents* contents) { |
| 24 return browser->tab_strip_model()->GetIndexOfWebContents(contents); |
| 25 } |
| 26 |
| 27 TabContents* GetActiveTabContents(const Browser* browser) { |
| 28 return browser->tab_strip_model()->GetActiveTabContents(); |
| 29 } |
| 30 |
| 31 content::WebContents* GetActiveWebContents(const Browser* browser) { |
| 32 TabContents* active_tab = GetActiveTabContents(browser); |
| 33 return active_tab ? active_tab->web_contents() : NULL; |
| 34 } |
| 35 |
| 36 TabContents* GetTabContentsAt(const Browser* browser, int index) { |
| 37 return browser->tab_strip_model()->GetTabContentsAt(index); |
| 38 } |
| 39 |
| 40 content::WebContents* GetWebContentsAt(const Browser* browser, int index) { |
| 41 TabContents* tab = GetTabContentsAt(browser, index); |
| 42 return tab ? tab->web_contents() : NULL; |
| 43 } |
| 44 |
| 45 void ActivateTabAt(Browser* browser, int index, bool user_gesture) { |
| 46 browser->tab_strip_model()->ActivateTabAt(index, user_gesture); |
| 47 } |
| 48 |
| 49 bool IsTabStripEditable(Browser* browser) { |
| 50 return browser->window()->IsTabStripEditable(); |
| 51 } |
| 52 |
| 53 TabContents* AddSelectedTabWithURL(Browser* browser, |
| 54 const GURL& url, |
| 55 content::PageTransition transition) { |
| 56 browser::NavigateParams params(browser, url, transition); |
| 57 params.disposition = NEW_FOREGROUND_TAB; |
| 58 browser::Navigate(¶ms); |
| 59 return params.target_contents; |
| 60 } |
| 61 |
| 62 void AddTab(Browser* browser, |
| 63 TabContents* tab_contents, |
| 64 content::PageTransition type) { |
| 65 browser->tab_strip_model()->AddTabContents(tab_contents, -1, type, |
| 66 TabStripModel::ADD_ACTIVE); |
| 67 } |
| 68 |
| 69 void AddWebContents(Browser* browser, |
| 70 content::WebContents* source_contents, |
| 71 content::WebContents* new_contents, |
| 72 WindowOpenDisposition disposition, |
| 73 const gfx::Rect& initial_pos, |
| 74 bool user_gesture) { |
| 75 // No code for this yet. |
| 76 DCHECK(disposition != SAVE_TO_DISK); |
| 77 // Can't create a new contents for the current tab - invalid case. |
| 78 DCHECK(disposition != CURRENT_TAB); |
| 79 |
| 80 TabContents* source_tab_contents = NULL; |
| 81 BlockedContentTabHelper* source_blocked_content = NULL; |
| 82 TabContents* new_tab_contents = TabContents::FromWebContents(new_contents); |
| 83 if (!new_tab_contents) |
| 84 new_tab_contents = new TabContents(new_contents); |
| 85 if (source_contents) { |
| 86 source_tab_contents = TabContents::FromWebContents(source_contents); |
| 87 source_blocked_content = source_tab_contents->blocked_content_tab_helper(); |
| 88 } |
| 89 |
| 90 if (source_tab_contents) { |
| 91 // Handle blocking of all contents. |
| 92 if (source_blocked_content->all_contents_blocked()) { |
| 93 source_blocked_content->AddTabContents(new_tab_contents, |
| 94 disposition, |
| 95 initial_pos, |
| 96 user_gesture); |
| 97 return; |
| 98 } |
| 99 |
| 100 // Handle blocking of popups. |
| 101 if ((disposition == NEW_POPUP) && !user_gesture && |
| 102 !CommandLine::ForCurrentProcess()->HasSwitch( |
| 103 switches::kDisablePopupBlocking)) { |
| 104 // Unrequested popups from normal pages are constrained unless they're in |
| 105 // the white list. The popup owner will handle checking this. |
| 106 source_tab_contents->blocked_content_tab_helper()-> |
| 107 AddPopup(new_tab_contents, initial_pos, user_gesture); |
| 108 return; |
| 109 } |
| 110 |
| 111 new_contents->GetRenderViewHost()->DisassociateFromPopupCount(); |
| 112 } |
| 113 |
| 114 browser::NavigateParams params(browser, new_tab_contents); |
| 115 params.source_contents = source_contents ? |
| 116 GetTabContentsAt(browser, GetIndexOfTab(browser, source_contents)) : NULL; |
| 117 params.disposition = disposition; |
| 118 params.window_bounds = initial_pos; |
| 119 params.window_action = browser::NavigateParams::SHOW_WINDOW; |
| 120 params.user_gesture = user_gesture; |
| 121 browser::Navigate(¶ms); |
| 122 } |
| 123 |
| 124 void CloseWebContents(Browser* browser, content::WebContents* contents) { |
| 125 int index = browser->tab_strip_model()->GetIndexOfWebContents(contents); |
| 126 if (index == TabStripModel::kNoTab) { |
| 127 NOTREACHED() << "CloseWebContents called for tab not in our strip"; |
| 128 return; |
| 129 } |
| 130 browser->tab_strip_model()->CloseTabContentsAt( |
| 131 index, |
| 132 TabStripModel::CLOSE_CREATE_HISTORICAL_TAB); |
| 133 } |
| 134 |
| 135 void CloseAllTabs(Browser* browser) { |
| 136 browser->tab_strip_model()->CloseAllTabs(); |
| 137 } |
| 138 |
| 139 TabContents* TabContentsFactory( |
| 140 Profile* profile, |
| 141 content::SiteInstance* site_instance, |
| 142 int routing_id, |
| 143 const content::WebContents* base_web_contents, |
| 144 content::SessionStorageNamespace* session_storage_namespace) { |
| 145 return new TabContents(content::WebContents::Create( |
| 146 profile, |
| 147 site_instance, |
| 148 routing_id, |
| 149 base_web_contents, |
| 150 session_storage_namespace)); |
| 151 } |
| 152 |
| 153 } // namespace chrome |
OLD | NEW |