File: singleton_tabs.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (211 lines) | stat: -rw-r--r-- 8,065 bytes parent folder | download
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// 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/singleton_tabs.h"

#include "chrome/browser/autocomplete/chrome_autocomplete_provider_client.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search/search.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_navigator.h"
#include "chrome/browser/ui/browser_navigator_params.h"
#include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/url_constants.h"
#include "content/public/browser/browser_url_handler.h"
#include "content/public/browser/web_contents.h"

#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "ash/constants/ash_switches.h"
#include "chrome/browser/ash/crosapi/browser_util.h"
#include "chrome/browser/ash/url_handler.h"
#endif

namespace {

// Returns true if two URLs are equal after taking |replacements| into account.
bool CompareURLsWithReplacements(const GURL& url,
                                 const GURL& other,
                                 const GURL::Replacements& replacements,
                                 ChromeAutocompleteProviderClient* client) {
  GURL url_replaced = url.ReplaceComponents(replacements);
  GURL other_replaced = other.ReplaceComponents(replacements);
  return client->StrippedURLsAreEqual(url_replaced, other_replaced, nullptr);
}

}  // namespace

void ShowSingletonTab(Profile* profile, const GURL& url) {
#if BUILDFLAG(IS_CHROMEOS_ASH)
  if (!crosapi::browser_util::IsAshWebBrowserEnabled()) {
    ash::TryOpenUrl(url, WindowOpenDisposition::SINGLETON_TAB,
                    NavigateParams::RESPECT,
                    ash::ChromeSchemeSemantics::kLacros);
    return;
  }
#endif  // BUILDFLAG(IS_CHROMEOS_ASH)
  chrome::ScopedTabbedBrowserDisplayer displayer(profile);
  NavigateParams params(
      GetSingletonTabNavigateParams(displayer.browser(), url));
  Navigate(&params);
}

void ShowSingletonTab(Browser* browser, const GURL& url) {
#if BUILDFLAG(IS_CHROMEOS_ASH)
  if (!crosapi::browser_util::IsAshWebBrowserEnabled()) {
    ash::TryOpenUrl(url, WindowOpenDisposition::SINGLETON_TAB,
                    NavigateParams::RESPECT,
                    ash::ChromeSchemeSemantics::kLacros);
    return;
  }
#endif  // BUILDFLAG(IS_CHROMEOS_ASH)
  NavigateParams params(GetSingletonTabNavigateParams(browser, url));
  Navigate(&params);
}

void ShowSingletonTabOverwritingNTP(
    Profile* profile,
    const GURL& url,
    NavigateParams::PathBehavior path_behavior) {
#if BUILDFLAG(IS_CHROMEOS_ASH)
  if (!crosapi::browser_util::IsAshWebBrowserEnabled()) {
    ash::TryOpenUrl(url, WindowOpenDisposition::SINGLETON_TAB, path_behavior,
                    ash::ChromeSchemeSemantics::kLacros);
    return;
  }
#endif  // BUILDFLAG(IS_CHROMEOS_ASH)
  chrome::ScopedTabbedBrowserDisplayer displayer(profile);
  NavigateParams params(
      GetSingletonTabNavigateParams(displayer.browser(), url));
  params.path_behavior = path_behavior;
  ShowSingletonTabOverwritingNTP(&params);
}

void ShowSingletonTabOverwritingNTP(
    Browser* browser,
    const GURL& url,
    NavigateParams::PathBehavior path_behavior) {
#if BUILDFLAG(IS_CHROMEOS_ASH)
  if (!crosapi::browser_util::IsAshWebBrowserEnabled()) {
    ash::TryOpenUrl(url, WindowOpenDisposition::SINGLETON_TAB, path_behavior,
                    ash::ChromeSchemeSemantics::kLacros);
    return;
  }
#endif  // BUILDFLAG(IS_CHROMEOS_ASH)
  NavigateParams params(GetSingletonTabNavigateParams(browser, url));
  params.path_behavior = path_behavior;
  ShowSingletonTabOverwritingNTP(&params);
}

void ShowSingletonTabOverwritingNTP(NavigateParams* params) {
  DCHECK_EQ(params->disposition, WindowOpenDisposition::SINGLETON_TAB);

#if BUILDFLAG(IS_CHROMEOS_ASH)
  // TODO(neis): Make this a CHECK after confirming that it doesn't happen in
  // the wild.
  if (!crosapi::browser_util::IsAshWebBrowserEnabled() &&
      !ash::switches::IsAshDebugBrowserEnabled()) {
    base::debug::DumpWithoutCrashing();
    LOG(ERROR) << "Unexpected SINGLETON_TAB navigation in Ash";
  }
#endif  // BUILDFLAG(IS_CHROMEOS_ASH)

  content::WebContents* contents =
      params->browser->tab_strip_model()->GetActiveWebContents();
  if (contents) {
    const GURL& contents_url = contents->GetVisibleURL();
    if (contents_url == chrome::kChromeUINewTabURL ||
        search::IsInstantNTP(contents) || contents_url == url::kAboutBlankURL) {
      int tab_index = GetIndexOfExistingTab(params->browser, *params);
      if (tab_index < 0) {
        params->disposition = WindowOpenDisposition::CURRENT_TAB;
      } else {
        params->switch_to_singleton_tab =
            params->browser->tab_strip_model()->GetWebContentsAt(tab_index);
      }
    }
  }
  Navigate(params);
}

NavigateParams GetSingletonTabNavigateParams(Browser* browser,
                                             const GURL& url) {
  NavigateParams params(browser, url, ui::PAGE_TRANSITION_AUTO_BOOKMARK);
  params.disposition = WindowOpenDisposition::SINGLETON_TAB;
  params.window_action = NavigateParams::SHOW_WINDOW;
  params.user_gesture = true;
  params.tabstrip_add_types |= AddTabTypes::ADD_INHERIT_OPENER;
  return params;
}

// Returns the index of an existing singleton tab in |browser| matching
// the URL specified in |params|.
int GetIndexOfExistingTab(Browser* browser, const NavigateParams& params) {
  if (params.disposition != WindowOpenDisposition::SINGLETON_TAB &&
      params.disposition != WindowOpenDisposition::SWITCH_TO_TAB)
    return -1;

  // In case the URL was rewritten by the BrowserURLHandler we need to ensure
  // that we do not open another URL that will get redirected to the rewritten
  // URL.
  const bool target_is_view_source =
      params.url.SchemeIs(content::kViewSourceScheme);
  GURL rewritten_url(params.url);
  content::BrowserURLHandler::GetInstance()->RewriteURLIfNecessary(
      &rewritten_url, browser->profile());

  ChromeAutocompleteProviderClient client(browser->profile());
  // If there are several matches: prefer the active tab by starting there.
  int start_index = std::max(0, browser->tab_strip_model()->active_index());
  int tab_count = browser->tab_strip_model()->count();
  for (int i = 0; i < tab_count; ++i) {
    int tab_index = (start_index + i) % tab_count;
    content::WebContents* tab =
        browser->tab_strip_model()->GetWebContentsAt(tab_index);

    GURL tab_url = tab->GetVisibleURL();

    // RewriteURLIfNecessary removes the "view-source:" scheme which could lead
    // to incorrect matching, so ensure that the target and the candidate are
    // either both view-source:, or neither is.
    if (tab_url.SchemeIs(content::kViewSourceScheme) != target_is_view_source) {
      continue;
    }

    GURL rewritten_tab_url = tab_url;
    content::BrowserURLHandler::GetInstance()->RewriteURLIfNecessary(
        &rewritten_tab_url, browser->profile());

    GURL::Replacements replacements;
    replacements.ClearRef();
    if (params.path_behavior == NavigateParams::IGNORE_AND_NAVIGATE) {
      replacements.ClearPath();
      replacements.ClearQuery();
    }

    if (CompareURLsWithReplacements(tab_url, params.url, replacements,
                                    &client) ||
        CompareURLsWithReplacements(rewritten_tab_url, rewritten_url,
                                    replacements, &client)) {
      return tab_index;
    }
  }

  return -1;
}

std::pair<Browser*, int> GetIndexAndBrowserOfExistingTab(
    Profile* profile,
    const NavigateParams& params) {
  for (Browser* browser : BrowserList::GetInstance()->OrderedByActivation()) {
    // When tab switching, only look at same profile and anonymity level.
    if (profile == browser->profile() && !browser->is_delete_scheduled()) {
      int index = GetIndexOfExistingTab(browser, params);
      if (index >= 0)
        return {browser, index};
    }
  }
  return {nullptr, -1};
}