1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/browser_tabstrip.h"
#include "base/command_line.h"
#include "base/feature_list.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_navigator.h"
#include "chrome/browser/ui/browser_navigator_params.h"
#include "chrome/browser/ui/tab_contents/core_tab_helper.h"
#include "chrome/browser/ui/tabs/tab_enums.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/url_constants.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "third_party/blink/public/mojom/window_features/window_features.mojom.h"
#include "ui/base/page_transition_types.h"
#include "url/gurl.h"
namespace chrome {
content::WebContents* AddAndReturnTabAt(
Browser* browser,
const GURL& url,
int idx,
bool foreground,
std::optional<tab_groups::TabGroupId> group) {
// Time new tab page creation time. We keep track of the timing data in
// WebContents, but we want to include the time it takes to create the
// WebContents object too.
base::TimeTicks new_tab_start_time = base::TimeTicks::Now();
NavigateParams params(browser, url.is_empty() ? browser->GetNewTabURL() : url,
ui::PAGE_TRANSITION_TYPED);
params.disposition = foreground ? WindowOpenDisposition::NEW_FOREGROUND_TAB
: WindowOpenDisposition::NEW_BACKGROUND_TAB;
params.tabstrip_index = idx;
params.group = group;
params.pwa_navigation_capturing_force_off = true;
Navigate(¶ms);
if (!params.navigated_or_inserted_contents) {
return nullptr;
}
CoreTabHelper* core_tab_helper =
CoreTabHelper::FromWebContents(params.navigated_or_inserted_contents);
core_tab_helper->set_new_tab_start_time(new_tab_start_time);
return params.navigated_or_inserted_contents;
}
void AddTabAt(Browser* browser,
const GURL& url,
int idx,
bool foreground,
std::optional<tab_groups::TabGroupId> group) {
/*void*/ AddAndReturnTabAt(browser, url, idx, foreground, std::move(group));
}
content::WebContents* AddSelectedTabWithURL(Browser* browser,
const GURL& url,
ui::PageTransition transition) {
NavigateParams params(browser, url, transition);
params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
params.pwa_navigation_capturing_force_off = true;
Navigate(¶ms);
return params.navigated_or_inserted_contents;
}
content::WebContents* AddWebContents(
Browser* browser,
content::WebContents* source_contents,
std::unique_ptr<content::WebContents> new_contents,
const GURL& target_url,
WindowOpenDisposition disposition,
const blink::mojom::WindowFeatures& window_features,
NavigateParams::WindowAction window_action,
bool user_gesture) {
// No code for this yet.
DCHECK(disposition != WindowOpenDisposition::SAVE_TO_DISK);
// Can't create a new contents for the current tab - invalid case.
DCHECK(disposition != WindowOpenDisposition::CURRENT_TAB);
NavigateParams params(browser, std::move(new_contents));
params.source_contents = source_contents;
params.url = target_url;
params.disposition = disposition;
params.window_features = window_features;
params.window_action = window_action;
// At this point, we're already beyond the popup blocker. Even if the popup
// was created without a user gesture, we have to set |user_gesture| to true,
// so it gets correctly focused.
params.user_gesture = true;
params.original_user_gesture = user_gesture;
ConfigureTabGroupForNavigation(¶ms);
Navigate(¶ms);
return params.navigated_or_inserted_contents;
}
void CloseWebContents(Browser* browser,
content::WebContents* contents,
bool add_to_history) {
int index = browser->tab_strip_model()->GetIndexOfWebContents(contents);
if (index == TabStripModel::kNoTab) {
DUMP_WILL_BE_NOTREACHED()
<< "CloseWebContents called for tab not in our strip";
return;
}
browser->tab_strip_model()->CloseWebContentsAt(
index, add_to_history ? TabCloseTypes::CLOSE_CREATE_HISTORICAL_TAB
: TabCloseTypes::CLOSE_NONE);
}
void ConfigureTabGroupForNavigation(NavigateParams* nav_params) {
if (!nav_params->source_contents) {
return;
}
if (!nav_params->browser || !nav_params->browser->SupportsWindowFeature(
Browser::WindowFeature::FEATURE_TABSTRIP)) {
return;
}
TabStripModel* model = nav_params->browser->tab_strip_model();
DCHECK(model);
const int source_index =
model->GetIndexOfWebContents(nav_params->source_contents);
// If the source tab is not in the current tab strip (e.g. if the current
// navigation is in a new window), don't set the group. Groups cannot be
// shared across multiple windows.
if (source_index == TabStripModel::kNoTab) {
return;
}
// Do not set the group when the navigation is from bookmarks.
if (ui::PageTransitionCoreTypeIs(nav_params->transition,
ui::PAGE_TRANSITION_AUTO_BOOKMARK)) {
return;
}
if (nav_params->disposition == WindowOpenDisposition::NEW_FOREGROUND_TAB ||
nav_params->disposition == WindowOpenDisposition::NEW_BACKGROUND_TAB) {
nav_params->group = model->GetTabGroupForTab(source_index);
}
}
} // namespace chrome
|