blob: ec45d2c4a73591963066e5f220ea50e1b877fa34 [file] [log] [blame]
[email protected]327640a2012-01-24 21:57:591// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]ac84431b2011-09-27 17:26:112// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]ac84431b2011-09-27 17:26:115#include "chrome/browser/extensions/extension_tab_util.h"
[email protected]41d9faf2012-02-28 23:46:026
avia2f4804a2015-12-24 23:11:137#include <stddef.h>
Devlin Cronin7050f8e2018-02-07 19:52:048#include <algorithm>
Ivan Sandrke2b20c62018-09-10 16:23:539#include <utility>
avia2f4804a2015-12-24 23:11:1310
Ivan Sandrke2b20c62018-09-10 16:23:5311#include "base/no_destructor.h"
Peter Kasting1f016552019-09-09 22:20:5512#include "base/numerics/ranges.h"
Avi Drissman5f0fb8c2018-12-25 23:20:4913#include "base/stl_util.h"
[email protected]98528302014-05-02 00:34:0814#include "base/strings/string_number_conversions.h"
ericzengf97b7c22014-08-26 03:07:3015#include "base/strings/stringprintf.h"
limasdf6dcdc442016-02-26 04:58:2616#include "base/strings/utf_string_conversions.h"
andersoncss12caff12016-07-14 14:43:4117#include "chrome/browser/browser_process.h"
[email protected]b19451b2012-06-08 17:36:1918#include "chrome/browser/extensions/api/tabs/tabs_constants.h"
Devlin Croninad230bb2018-05-30 18:41:4619#include "chrome/browser/extensions/browser_extension_window_controller.h"
[email protected]98528302014-05-02 00:34:0820#include "chrome/browser/extensions/chrome_extension_function.h"
lfg185333072014-09-09 20:16:1121#include "chrome/browser/extensions/chrome_extension_function_details.h"
[email protected]e9570fdf2012-07-18 20:01:2122#include "chrome/browser/extensions/tab_helper.h"
Ivan Sandrke0e92452019-03-21 09:32:4823#include "chrome/browser/platform_util.h"
[email protected]b56e2e32012-05-11 21:18:0424#include "chrome/browser/profiles/profile.h"
Francois Doray77be0212018-04-05 14:26:2625#include "chrome/browser/resource_coordinator/tab_lifecycle_unit_external.h"
[email protected]e3f90c602014-08-18 12:41:5926#include "chrome/browser/sessions/session_tab_helper.h"
[email protected]b56e2e32012-05-11 21:18:0427#include "chrome/browser/ui/browser.h"
[email protected]73c1a6842012-07-13 17:39:0428#include "chrome/browser/ui/browser_finder.h"
thestige80821242015-09-30 23:46:0829#include "chrome/browser/ui/browser_navigator_params.h"
[email protected]b56e2e32012-05-11 21:18:0430#include "chrome/browser/ui/browser_window.h"
Chris Hamilton20139652018-06-01 15:55:0131#include "chrome/browser/ui/recently_audible_helper.h"
[email protected]0edcdec2013-10-31 06:43:0832#include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
ericzengf97b7c22014-08-26 03:07:3033#include "chrome/browser/ui/singleton_tabs.h"
[email protected]44e329a2012-07-14 01:13:0634#include "chrome/browser/ui/tab_contents/tab_contents_iterator.h"
[email protected]b56e2e32012-05-11 21:18:0435#include "chrome/browser/ui/tabs/tab_strip_model.h"
jared.sohn09a3ccc2015-07-09 03:47:3036#include "chrome/browser/ui/tabs/tab_utils.h"
[email protected]760f6032014-06-30 23:18:1937#include "chrome/common/extensions/api/tabs.h"
[email protected]45c75e62012-03-21 19:56:3538#include "chrome/common/url_constants.h"
rsleevi24f64dc22015-08-07 21:39:2139#include "components/url_formatter/url_fixer.h"
[email protected]ad23a092011-12-28 07:02:0440#include "content/public/browser/favicon_status.h"
41#include "content/public/browser/navigation_entry.h"
[email protected]6acde6352012-01-04 16:52:2042#include "content/public/browser/web_contents.h"
[email protected]98528302014-05-02 00:34:0843#include "extensions/common/constants.h"
44#include "extensions/common/error_utils.h"
[email protected]e4452d32013-11-15 23:07:4145#include "extensions/common/extension.h"
ericzengf97b7c22014-08-26 03:07:3046#include "extensions/common/feature_switch.h"
[email protected]0c3c9732013-09-16 08:53:4147#include "extensions/common/manifest_constants.h"
[email protected]98528302014-05-02 00:34:0848#include "extensions/common/manifest_handlers/incognito_info.h"
ericzeng09a7e002014-09-09 21:43:4749#include "extensions/common/manifest_handlers/options_page_info.h"
[email protected]793964a2013-10-08 00:47:1950#include "extensions/common/permissions/api_permission.h"
[email protected]e4452d32013-11-15 23:07:4151#include "extensions/common/permissions/permissions_data.h"
[email protected]a6483d22013-07-03 22:11:0052#include "url/gurl.h"
[email protected]ac84431b2011-09-27 17:26:1153
[email protected]10f417c52011-12-28 21:04:2354using content::NavigationEntry;
[email protected]26b5e322011-12-23 01:36:4755using content::WebContents;
[email protected]1c4fbc02013-11-13 02:52:4256
57namespace extensions {
[email protected]26b5e322011-12-23 01:36:4758
[email protected]f1c102b2013-02-15 07:44:1259namespace {
60
[email protected]98528302014-05-02 00:34:0861// |error_message| can optionally be passed in and will be set with an
62// appropriate message if the window cannot be found by id.
63Browser* GetBrowserInProfileWithId(Profile* profile,
64 const int window_id,
Ramin Halavatiacdd17f12018-06-06 07:05:1265 bool match_incognito_profile,
[email protected]98528302014-05-02 00:34:0866 std::string* error_message) {
67 Profile* incognito_profile =
Ramin Halavatiacdd17f12018-06-06 07:05:1268 match_incognito_profile && profile->HasOffTheRecordProfile()
[email protected]98528302014-05-02 00:34:0869 ? profile->GetOffTheRecordProfile()
Catherine Mullings0524e2042017-06-26 23:09:4570 : nullptr;
scottmg8abbff832016-01-28 22:57:3771 for (auto* browser : *BrowserList::GetInstance()) {
[email protected]98528302014-05-02 00:34:0872 if ((browser->profile() == profile ||
73 browser->profile() == incognito_profile) &&
74 ExtensionTabUtil::GetWindowId(browser) == window_id &&
75 browser->window()) {
76 return browser;
77 }
78 }
79
80 if (error_message)
81 *error_message = ErrorUtils::FormatErrorMessage(
Raul Tambrefff51b752019-02-04 13:09:4782 tabs_constants::kWindowNotFoundError, base::NumberToString(window_id));
[email protected]98528302014-05-02 00:34:0883
Catherine Mullings0524e2042017-06-26 23:09:4584 return nullptr;
[email protected]98528302014-05-02 00:34:0885}
86
erg7b01d692017-02-22 21:57:3587Browser* CreateBrowser(Profile* profile,
88 int window_id,
89 bool user_gesture,
90 std::string* error) {
Joel Hockey2687ab52019-08-14 23:59:4691 Browser::CreateParams params(Browser::TYPE_NORMAL, profile, user_gesture);
Toni Barzic00705742019-02-25 22:09:4192 Browser* browser = Browser::Create(params);
93 if (!browser) {
94 *error = tabs_constants::kBrowserWindowNotAllowed;
95 return nullptr;
96 }
[email protected]98528302014-05-02 00:34:0897 browser->window()->Show();
98 return browser;
99}
100
lionel.g.landwerlin85ad2042015-07-23 11:26:11101// Use this function for reporting a tab id to an extension. It will
102// take care of setting the id to TAB_ID_NONE if necessary (for
103// example with devtools).
104int GetTabIdForExtensions(const WebContents* web_contents) {
105 Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
106 if (browser && !ExtensionTabUtil::BrowserSupportsTabs(browser))
107 return -1;
Mikel Astiz2f127c422018-04-05 19:10:27108 return SessionTabHelper::IdForTab(web_contents).id();
lionel.g.landwerlin85ad2042015-07-23 11:26:11109}
110
Daniel Bratellbe89e3442018-09-11 15:44:57111std::unique_ptr<ExtensionTabUtil::Delegate>&
112GetExtensionTabUtilDelegateWrapper() {
Ivan Sandrke2b20c62018-09-10 16:23:53113 static base::NoDestructor<std::unique_ptr<ExtensionTabUtil::Delegate>>
114 delegate_wrapper;
115 return *delegate_wrapper;
116}
117
Daniel Bratellbe89e3442018-09-11 15:44:57118ExtensionTabUtil::Delegate* GetExtensionTabUtilDelegate() {
119 return GetExtensionTabUtilDelegateWrapper().get();
Ivan Sandrke2b20c62018-09-10 16:23:53120}
isandrke0951132017-04-24 17:53:31121
John Lee76e40a32019-08-20 17:52:40122ExtensionTabUtil::ScrubTabBehavior GetScrubTabBehaviorImpl(
123 const Extension* extension,
John Leebfbb37cb2019-08-26 22:23:18124 Feature::Context context,
John Lee76e40a32019-08-20 17:52:40125 const GURL& url,
126 int tab_id) {
John Leebfbb37cb2019-08-26 22:23:18127 if (context == Feature::Context::WEBUI_CONTEXT) {
128 return ExtensionTabUtil::kDontScrubTab;
129 }
130
John Lee76e40a32019-08-20 17:52:40131 bool has_permission = false;
132
133 if (extension) {
134 bool api_permission = false;
135 if (tab_id == api::tabs::TAB_ID_NONE) {
136 api_permission =
137 extension->permissions_data()->HasAPIPermission(APIPermission::kTab);
138 } else {
139 api_permission = extension->permissions_data()->HasAPIPermissionForTab(
140 tab_id, APIPermission::kTab);
141 }
142
143 bool host_permission = extension->permissions_data()
144 ->active_permissions()
145 .HasExplicitAccessToOrigin(url);
146 has_permission = api_permission || host_permission;
147 }
148
149 if (!has_permission) {
150 return ExtensionTabUtil::kScrubTabFully;
151 }
152
153 if (GetExtensionTabUtilDelegate()) {
154 return GetExtensionTabUtilDelegate()->GetScrubTabBehavior(extension);
155 }
156
157 return ExtensionTabUtil::kDontScrubTab;
158}
159
[email protected]f1c102b2013-02-15 07:44:12160} // namespace
161
[email protected]98528302014-05-02 00:34:08162ExtensionTabUtil::OpenTabParams::OpenTabParams()
163 : create_browser_if_needed(false) {
164}
165
166ExtensionTabUtil::OpenTabParams::~OpenTabParams() {
167}
168
Catherine Mullings0524e2042017-06-26 23:09:45169// Opens a new tab for a given extension. Returns nullptr and sets |error| if an
[email protected]98528302014-05-02 00:34:08170// error occurs.
Clark DuVallfd4db3d2019-07-30 19:10:43171base::DictionaryValue* ExtensionTabUtil::OpenTab(ExtensionFunction* function,
172 const OpenTabParams& params,
173 bool user_gesture,
174 std::string* error) {
rdevlin.cronin3223122d2016-09-07 21:27:26175 ChromeExtensionFunctionDetails chrome_details(function);
176 Profile* profile = chrome_details.GetProfile();
[email protected]98528302014-05-02 00:34:08177 // windowId defaults to "current" window.
178 int window_id = extension_misc::kCurrentWindowId;
179 if (params.window_id.get())
180 window_id = *params.window_id;
181
rdevlin.cronin3223122d2016-09-07 21:27:26182 Browser* browser = GetBrowserFromWindowID(chrome_details, window_id, error);
[email protected]98528302014-05-02 00:34:08183 if (!browser) {
184 if (!params.create_browser_if_needed) {
Catherine Mullings0524e2042017-06-26 23:09:45185 return nullptr;
[email protected]98528302014-05-02 00:34:08186 }
erg7b01d692017-02-22 21:57:35187 browser = CreateBrowser(profile, window_id, user_gesture, error);
[email protected]98528302014-05-02 00:34:08188 if (!browser)
Catherine Mullings0524e2042017-06-26 23:09:45189 return nullptr;
[email protected]98528302014-05-02 00:34:08190 }
191
Joel Hockey2687ab52019-08-14 23:59:46192 // Ensure the selected browser is normal.
193 if (!browser->is_type_normal() && browser->IsAttemptingToCloseBrowser())
Ramin Halavatiacdd17f12018-06-06 07:05:12194 browser = chrome::FindTabbedBrowser(
195 profile, function->include_incognito_information());
[email protected]98528302014-05-02 00:34:08196 if (!browser || !browser->window()) {
deepak.m121895c012015-05-11 08:11:19197 if (error)
oscarjohansson77164012018-06-11 07:00:11198 *error = tabs_constants::kNoCurrentWindowError;
Catherine Mullings0524e2042017-06-26 23:09:45199 return nullptr;
[email protected]98528302014-05-02 00:34:08200 }
201
202 // TODO(jstritar): Add a constant, chrome.tabs.TAB_ID_ACTIVE, that
203 // represents the active tab.
Catherine Mullings0524e2042017-06-26 23:09:45204 WebContents* opener = nullptr;
205 Browser* opener_browser = nullptr;
[email protected]98528302014-05-02 00:34:08206 if (params.opener_tab_id.get()) {
207 int opener_id = *params.opener_tab_id;
208
Catherine Mullings0524e2042017-06-26 23:09:45209 if (!ExtensionTabUtil::GetTabById(
Ramin Halavatiacdd17f12018-06-06 07:05:12210 opener_id, profile, function->include_incognito_information(),
211 &opener_browser, nullptr, &opener, nullptr)) {
deepak.m121895c012015-05-11 08:11:19212 if (error) {
oscarjohansson77164012018-06-11 07:00:11213 *error = ErrorUtils::FormatErrorMessage(
Raul Tambrefff51b752019-02-04 13:09:47214 tabs_constants::kTabNotFoundError, base::NumberToString(opener_id));
deepak.m121895c012015-05-11 08:11:19215 }
Catherine Mullings0524e2042017-06-26 23:09:45216 return nullptr;
[email protected]98528302014-05-02 00:34:08217 }
218 }
219
220 // TODO(rafaelw): handle setting remaining tab properties:
221 // -title
222 // -favIconUrl
223
[email protected]98528302014-05-02 00:34:08224 GURL url;
225 if (params.url.get()) {
bokan107a47f2015-02-03 23:23:39226 std::string url_string = *params.url;
[email protected]eba8f7d2014-07-28 22:09:23227 url = ExtensionTabUtil::ResolvePossiblyRelativeURL(url_string,
228 function->extension());
[email protected]98528302014-05-02 00:34:08229 if (!url.is_valid()) {
oscarjohansson77164012018-06-11 07:00:11230 *error = ErrorUtils::FormatErrorMessage(tabs_constants::kInvalidUrlError,
231 url_string);
Catherine Mullings0524e2042017-06-26 23:09:45232 return nullptr;
[email protected]98528302014-05-02 00:34:08233 }
[email protected]f5ec3c922014-05-22 15:04:03234 } else {
235 url = GURL(chrome::kChromeUINewTabURL);
[email protected]98528302014-05-02 00:34:08236 }
237
238 // Don't let extensions crash the browser or renderers.
kalmandfefe1a2015-07-13 22:27:54239 if (ExtensionTabUtil::IsKillURL(url)) {
oscarjohansson77164012018-06-11 07:00:11240 *error = tabs_constants::kNoCrashBrowserError;
Catherine Mullings0524e2042017-06-26 23:09:45241 return nullptr;
[email protected]98528302014-05-02 00:34:08242 }
243
244 // Default to foreground for the new tab. The presence of 'active' property
245 // will override this default.
246 bool active = true;
247 if (params.active.get())
248 active = *params.active;
249
250 // Default to not pinning the tab. Setting the 'pinned' property to true
251 // will override this default.
252 bool pinned = false;
253 if (params.pinned.get())
254 pinned = *params.pinned;
255
256 // We can't load extension URLs into incognito windows unless the extension
257 // uses split mode. Special case to fall back to a tabbed window.
258 if (url.SchemeIs(kExtensionScheme) &&
calamity862d90322017-05-22 07:14:28259 (!function->extension() ||
260 !IncognitoInfo::IsSplitMode(function->extension())) &&
[email protected]98528302014-05-02 00:34:08261 browser->profile()->IsOffTheRecord()) {
262 Profile* profile = browser->profile()->GetOriginalProfile();
[email protected]98528302014-05-02 00:34:08263
scottmg34c5dd882016-02-03 05:21:54264 browser = chrome::FindTabbedBrowser(profile, false);
[email protected]98528302014-05-02 00:34:08265 if (!browser) {
erg7b01d692017-02-22 21:57:35266 Browser::CreateParams params =
Joel Hockey2687ab52019-08-14 23:59:46267 Browser::CreateParams(Browser::TYPE_NORMAL, profile, user_gesture);
Toni Barzic00705742019-02-25 22:09:41268 browser = Browser::Create(params);
269 if (!browser) {
270 *error = tabs_constants::kBrowserWindowNotAllowed;
271 return nullptr;
272 }
[email protected]98528302014-05-02 00:34:08273 browser->window()->Show();
274 }
275 }
276
Catherine Mullings0524e2042017-06-26 23:09:45277 if (opener_browser && browser != opener_browser) {
278 if (error) {
279 *error = "Tab opener must be in the same window as the updated tab.";
280 }
281 return nullptr;
282 }
283
[email protected]98528302014-05-02 00:34:08284 // If index is specified, honor the value, but keep it bound to
285 // -1 <= index <= tab_strip->count() where -1 invokes the default behavior.
286 int index = -1;
287 if (params.index.get())
288 index = *params.index;
Peter Kasting1f016552019-09-09 22:20:55289 index = base::ClampToRange(index, -1, browser->tab_strip_model()->count());
[email protected]98528302014-05-02 00:34:08290
291 int add_types = active ? TabStripModel::ADD_ACTIVE : TabStripModel::ADD_NONE;
292 add_types |= TabStripModel::ADD_FORCE_INDEX;
293 if (pinned)
294 add_types |= TabStripModel::ADD_PINNED;
cm.sanchi2522bc92017-12-04 08:04:13295 NavigateParams navigate_params(browser, url, ui::PAGE_TRANSITION_LINK);
nick3b04f322016-08-31 19:29:19296 navigate_params.disposition = active
297 ? WindowOpenDisposition::NEW_FOREGROUND_TAB
298 : WindowOpenDisposition::NEW_BACKGROUND_TAB;
[email protected]98528302014-05-02 00:34:08299 navigate_params.tabstrip_index = index;
300 navigate_params.tabstrip_add_types = add_types;
cm.sanchi2522bc92017-12-04 08:04:13301 Navigate(&navigate_params);
[email protected]98528302014-05-02 00:34:08302
303 // The tab may have been created in a different window, so make sure we look
304 // at the right tab strip.
Peter Kasting1f016552019-09-09 22:20:55305 TabStripModel* tab_strip = navigate_params.browser->tab_strip_model();
erikchen38fa4022018-04-26 20:37:52306 int new_index = tab_strip->GetIndexOfWebContents(
307 navigate_params.navigated_or_inserted_contents);
rdevlin.cronin5b3827b62017-03-08 02:38:09308 if (opener) {
309 // Only set the opener if the opener tab is in the same tab strip as the
310 // new tab.
rdevlin.cronin5b3827b62017-03-08 02:38:09311 if (tab_strip->GetIndexOfWebContents(opener) != TabStripModel::kNoTab)
312 tab_strip->SetOpenerOfWebContentsAt(new_index, opener);
313 }
[email protected]98528302014-05-02 00:34:08314
315 if (active)
erikchen38fa4022018-04-26 20:37:52316 navigate_params.navigated_or_inserted_contents->SetInitialFocus();
[email protected]98528302014-05-02 00:34:08317
John Lee76e40a32019-08-20 17:52:40318 ExtensionTabUtil::ScrubTabBehavior scrub_tab_behavior =
319 ExtensionTabUtil::GetScrubTabBehavior(
John Leebfbb37cb2019-08-26 22:23:18320 function->extension(), function->source_context_type(),
John Lee76e40a32019-08-20 17:52:40321 navigate_params.navigated_or_inserted_contents);
322
[email protected]98528302014-05-02 00:34:08323 // Return data about the newly created tab.
erikchen38fa4022018-04-26 20:37:52324 return ExtensionTabUtil::CreateTabObject(
John Lee76e40a32019-08-20 17:52:40325 navigate_params.navigated_or_inserted_contents, scrub_tab_behavior,
erikchen38fa4022018-04-26 20:37:52326 function->extension(), tab_strip, new_index)
limasdf6dcdc442016-02-26 04:58:26327 ->ToValue()
328 .release();
[email protected]98528302014-05-02 00:34:08329}
330
331Browser* ExtensionTabUtil::GetBrowserFromWindowID(
lfg185333072014-09-09 20:16:11332 const ChromeExtensionFunctionDetails& details,
333 int window_id,
334 std::string* error) {
335 if (window_id == extension_misc::kCurrentWindowId) {
336 Browser* result = details.GetCurrentBrowser();
337 if (!result || !result->window()) {
338 if (error)
oscarjohansson77164012018-06-11 07:00:11339 *error = tabs_constants::kNoCurrentWindowError;
Catherine Mullings0524e2042017-06-26 23:09:45340 return nullptr;
lfg185333072014-09-09 20:16:11341 }
342 return result;
343 } else {
Ramin Halavatiacdd17f12018-06-06 07:05:12344 return GetBrowserInProfileWithId(
345 details.GetProfile(), window_id,
346 details.function()->include_incognito_information(), error);
lfg185333072014-09-09 20:16:11347 }
348}
349
[email protected]ac84431b2011-09-27 17:26:11350int ExtensionTabUtil::GetWindowId(const Browser* browser) {
351 return browser->session_id().id();
352}
353
[email protected]8c3495c2011-09-28 03:32:30354int ExtensionTabUtil::GetWindowIdOfTabStripModel(
355 const TabStripModel* tab_strip_model) {
scottmg8abbff832016-01-28 22:57:37356 for (auto* browser : *BrowserList::GetInstance()) {
357 if (browser->tab_strip_model() == tab_strip_model)
358 return GetWindowId(browser);
[email protected]8c3495c2011-09-28 03:32:30359 }
360 return -1;
361}
362
[email protected]d2bd5fde2014-05-29 02:24:31363int ExtensionTabUtil::GetTabId(const WebContents* web_contents) {
Mikel Astiz2f127c422018-04-05 19:10:27364 return SessionTabHelper::IdForTab(web_contents).id();
[email protected]ac84431b2011-09-27 17:26:11365}
366
367std::string ExtensionTabUtil::GetTabStatusText(bool is_loading) {
oscarjohansson77164012018-06-11 07:00:11368 return is_loading ? tabs_constants::kStatusValueLoading
369 : tabs_constants::kStatusValueComplete;
[email protected]ac84431b2011-09-27 17:26:11370}
371
[email protected]ea049a02011-12-25 21:37:09372int ExtensionTabUtil::GetWindowIdOfTab(const WebContents* web_contents) {
Mikel Astiz2f127c422018-04-05 19:10:27373 return SessionTabHelper::IdForWindowContainingTab(web_contents).id();
[email protected]ac84431b2011-09-27 17:26:11374}
375
limasdf6dcdc442016-02-26 04:58:26376// static
Devlin Cronin7050f8e2018-02-07 19:52:04377std::string ExtensionTabUtil::GetBrowserWindowTypeText(const Browser& browser) {
Joel Hockey2687ab52019-08-14 23:59:46378 if (browser.is_type_devtools())
oscarjohansson77164012018-06-11 07:00:11379 return tabs_constants::kWindowTypeValueDevTools;
Joel Hockey2687ab52019-08-14 23:59:46380 // TODO(crbug.com/990158): We return 'popup' for both popup and app since
381 // chrome.tabs.create({type: 'popup'}) uses
382 // Browser::CreateParams::CreateForApp.
383 if (browser.is_type_popup() || browser.is_type_app())
oscarjohansson77164012018-06-11 07:00:11384 return tabs_constants::kWindowTypeValuePopup;
oscarjohansson77164012018-06-11 07:00:11385 return tabs_constants::kWindowTypeValueNormal;
Devlin Cronin7050f8e2018-02-07 19:52:04386}
387
388// static
dchengc963c7142016-04-08 03:55:22389std::unique_ptr<api::tabs::Tab> ExtensionTabUtil::CreateTabObject(
[email protected]fc2b46b2014-05-03 16:33:45390 WebContents* contents,
Devlin Cronin1980f44d2018-02-08 23:14:01391 ScrubTabBehavior scrub_tab_behavior,
392 const Extension* extension,
[email protected]f34706be2012-09-04 07:32:09393 TabStripModel* tab_strip,
[email protected]304fd152013-01-12 16:54:44394 int tab_index) {
[email protected]0c9f3262012-09-17 05:59:06395 if (!tab_strip)
396 ExtensionTabUtil::GetTabStripModel(contents, &tab_strip, &tab_index);
[email protected]ac84431b2011-09-27 17:26:11397 bool is_loading = contents->IsLoading();
Jinho Bangb5216cec2018-01-17 19:43:11398 auto tab_object = std::make_unique<api::tabs::Tab>();
399 tab_object->id = std::make_unique<int>(GetTabIdForExtensions(contents));
limasdf6dcdc442016-02-26 04:58:26400 tab_object->index = tab_index;
401 tab_object->window_id = GetWindowIdOfTab(contents);
lazyboy8b1df302017-04-26 23:37:41402 tab_object->status =
Jinho Bangb5216cec2018-01-17 19:43:11403 std::make_unique<std::string>(GetTabStatusText(is_loading));
limasdf6dcdc442016-02-26 04:58:26404 tab_object->active = tab_strip && tab_index == tab_strip->active_index();
405 tab_object->selected = tab_strip && tab_index == tab_strip->active_index();
406 tab_object->highlighted = tab_strip && tab_strip->IsTabSelected(tab_index);
407 tab_object->pinned = tab_strip && tab_strip->IsTabPinned(tab_index);
Chris Hamilton20139652018-06-01 15:55:01408 auto* audible_helper = RecentlyAudibleHelper::FromWebContents(contents);
Chris Hamilton737e22b42018-06-04 18:33:14409 bool audible = false;
410 if (audible_helper) {
411 // WebContents in a tab strip have RecentlyAudible helpers. They endow the
412 // tab with a notion of audibility that has a timeout for quiet periods. Use
413 // that if available.
414 audible = audible_helper->WasRecentlyAudible();
415 } else {
416 // Otherwise use the instantaneous notion of audibility.
417 audible = contents->IsCurrentlyAudible();
418 }
419 tab_object->audible = std::make_unique<bool>(audible);
Francois Doray77be0212018-04-05 14:26:26420 auto* tab_lifeycle_unit_external =
421 resource_coordinator::TabLifecycleUnitExternal::FromWebContents(contents);
andersoncssfd947a42016-08-12 14:10:42422 tab_object->discarded =
Francois Doray77be0212018-04-05 14:26:26423 tab_lifeycle_unit_external && tab_lifeycle_unit_external->IsDiscarded();
andersoncssfd947a42016-08-12 14:10:42424 tab_object->auto_discardable =
Francois Doray77be0212018-04-05 14:26:26425 !tab_lifeycle_unit_external ||
426 tab_lifeycle_unit_external->IsAutoDiscardable();
limasdf6dcdc442016-02-26 04:58:26427 tab_object->muted_info = CreateMutedInfo(contents);
428 tab_object->incognito = contents->GetBrowserContext()->IsOffTheRecord();
lazyboy8b1df302017-04-26 23:37:41429 gfx::Size contents_size = contents->GetContainerBounds().size();
Jinho Bangb5216cec2018-01-17 19:43:11430 tab_object->width = std::make_unique<int>(contents_size.width());
431 tab_object->height = std::make_unique<int>(contents_size.height());
[email protected]ac84431b2011-09-27 17:26:11432
Jinho Bangb5216cec2018-01-17 19:43:11433 tab_object->url = std::make_unique<std::string>(contents->GetURL().spec());
lazyboy8b1df302017-04-26 23:37:41434 tab_object->title =
Jinho Bangb5216cec2018-01-17 19:43:11435 std::make_unique<std::string>(base::UTF16ToUTF8(contents->GetTitle()));
limasdf1f071892016-02-09 10:18:00436 NavigationEntry* entry = contents->GetController().GetVisibleEntry();
lazyboy8b1df302017-04-26 23:37:41437 if (entry && entry->GetFavicon().valid) {
438 tab_object->fav_icon_url =
Jinho Bangb5216cec2018-01-17 19:43:11439 std::make_unique<std::string>(entry->GetFavicon().url.spec());
lazyboy8b1df302017-04-26 23:37:41440 }
[email protected]327640a2012-01-24 21:57:59441 if (tab_strip) {
[email protected]67075402012-10-26 08:26:25442 WebContents* opener = tab_strip->GetOpenerOfWebContentsAt(tab_index);
lazyboy8b1df302017-04-26 23:37:41443 if (opener) {
444 tab_object->opener_tab_id =
Jinho Bangb5216cec2018-01-17 19:43:11445 std::make_unique<int>(GetTabIdForExtensions(opener));
lazyboy8b1df302017-04-26 23:37:41446 }
[email protected]ac84431b2011-09-27 17:26:11447 }
448
John Lee76e40a32019-08-20 17:52:40449 if (scrub_tab_behavior != kDontScrubTab)
450 ScrubTabForExtension(extension, contents, tab_object.get(),
451 scrub_tab_behavior);
limasdf6dcdc442016-02-26 04:58:26452 return tab_object;
[email protected]ac84431b2011-09-27 17:26:11453}
454
Devlin Cronin1980f44d2018-02-08 23:14:01455std::unique_ptr<base::ListValue> ExtensionTabUtil::CreateTabList(
456 const Browser* browser,
John Leebfbb37cb2019-08-26 22:23:18457 const Extension* extension,
458 Feature::Context context) {
Devlin Cronin1980f44d2018-02-08 23:14:01459 std::unique_ptr<base::ListValue> tab_list(new base::ListValue());
460 TabStripModel* tab_strip = browser->tab_strip_model();
461 for (int i = 0; i < tab_strip->count(); ++i) {
John Lee76e40a32019-08-20 17:52:40462 WebContents* web_contents = tab_strip->GetWebContentsAt(i);
463 ExtensionTabUtil::ScrubTabBehavior scrub_tab_behavior =
John Leebfbb37cb2019-08-26 22:23:18464 ExtensionTabUtil::GetScrubTabBehavior(extension, context, web_contents);
John Lee76e40a32019-08-20 17:52:40465 tab_list->Append(CreateTabObject(web_contents, scrub_tab_behavior,
Devlin Cronin1980f44d2018-02-08 23:14:01466 extension, tab_strip, i)
467 ->ToValue());
468 }
469
470 return tab_list;
471}
472
miu8b605f22015-08-15 02:56:56473// static
Devlin Cronin7050f8e2018-02-07 19:52:04474std::unique_ptr<base::DictionaryValue>
475ExtensionTabUtil::CreateWindowValueForExtension(
476 const Browser& browser,
477 const Extension* extension,
John Leebfbb37cb2019-08-26 22:23:18478 PopulateTabBehavior populate_tab_behavior,
479 Feature::Context context) {
Devlin Cronin7050f8e2018-02-07 19:52:04480 auto result = std::make_unique<base::DictionaryValue>();
481
oscarjohansson77164012018-06-11 07:00:11482 result->SetInteger(tabs_constants::kIdKey, browser.session_id().id());
483 result->SetString(tabs_constants::kWindowTypeKey,
484 GetBrowserWindowTypeText(browser));
Devlin Cronin7050f8e2018-02-07 19:52:04485 ui::BaseWindow* window = browser.window();
oscarjohansson77164012018-06-11 07:00:11486 result->SetBoolean(tabs_constants::kFocusedKey, window->IsActive());
Devlin Cronin7050f8e2018-02-07 19:52:04487 const Profile* profile = browser.profile();
oscarjohansson77164012018-06-11 07:00:11488 result->SetBoolean(tabs_constants::kIncognitoKey, profile->IsOffTheRecord());
Avi Drissman9e6533282019-07-17 17:25:12489 result->SetBoolean(
490 tabs_constants::kAlwaysOnTopKey,
491 window->GetZOrderLevel() == ui::ZOrderLevel::kFloatingWindow);
Devlin Cronin7050f8e2018-02-07 19:52:04492
493 std::string window_state;
494 if (window->IsMinimized()) {
oscarjohansson77164012018-06-11 07:00:11495 window_state = tabs_constants::kShowStateValueMinimized;
Devlin Cronin7050f8e2018-02-07 19:52:04496 } else if (window->IsFullscreen()) {
oscarjohansson77164012018-06-11 07:00:11497 window_state = tabs_constants::kShowStateValueFullscreen;
Ivan Sandrke0e92452019-03-21 09:32:48498 if (platform_util::IsBrowserLockedFullscreen(&browser))
oscarjohansson77164012018-06-11 07:00:11499 window_state = tabs_constants::kShowStateValueLockedFullscreen;
Devlin Cronin7050f8e2018-02-07 19:52:04500 } else if (window->IsMaximized()) {
oscarjohansson77164012018-06-11 07:00:11501 window_state = tabs_constants::kShowStateValueMaximized;
Devlin Cronin7050f8e2018-02-07 19:52:04502 } else {
oscarjohansson77164012018-06-11 07:00:11503 window_state = tabs_constants::kShowStateValueNormal;
Devlin Cronin7050f8e2018-02-07 19:52:04504 }
oscarjohansson77164012018-06-11 07:00:11505 result->SetString(tabs_constants::kShowStateKey, window_state);
Devlin Cronin7050f8e2018-02-07 19:52:04506
507 gfx::Rect bounds;
508 if (window->IsMinimized())
509 bounds = window->GetRestoredBounds();
510 else
511 bounds = window->GetBounds();
oscarjohansson77164012018-06-11 07:00:11512 result->SetInteger(tabs_constants::kLeftKey, bounds.x());
513 result->SetInteger(tabs_constants::kTopKey, bounds.y());
514 result->SetInteger(tabs_constants::kWidthKey, bounds.width());
515 result->SetInteger(tabs_constants::kHeightKey, bounds.height());
Devlin Cronin7050f8e2018-02-07 19:52:04516
517 if (populate_tab_behavior == kPopulateTabs)
John Leebfbb37cb2019-08-26 22:23:18518 result->Set(tabs_constants::kTabsKey,
519 CreateTabList(&browser, extension, context));
Devlin Cronin7050f8e2018-02-07 19:52:04520
521 return result;
522}
523
524// static
dchengc963c7142016-04-08 03:55:22525std::unique_ptr<api::tabs::MutedInfo> ExtensionTabUtil::CreateMutedInfo(
miu8b605f22015-08-15 02:56:56526 content::WebContents* contents) {
527 DCHECK(contents);
Peter Kasting479cc1752018-09-28 22:34:37528 auto info = std::make_unique<api::tabs::MutedInfo>();
limasdf6dcdc442016-02-26 04:58:26529 info->muted = contents->IsAudioMuted();
miu8b605f22015-08-15 02:56:56530 switch (chrome::GetTabAudioMutedReason(contents)) {
ortuno71c3f142016-03-30 18:26:58531 case TabMutedReason::NONE:
miu8b605f22015-08-15 02:56:56532 break;
Tommy Steimela8c3757e2017-08-24 00:15:46533 case TabMutedReason::CONTENT_SETTING:
Tommy Steimel371129502017-11-29 19:41:45534 case TabMutedReason::CONTENT_SETTING_CHROME:
Tommy Steimela8c3757e2017-08-24 00:15:46535 case TabMutedReason::CONTEXT_MENU:
limasdf6dcdc442016-02-26 04:58:26536 info->reason = api::tabs::MUTED_INFO_REASON_USER;
miu8b605f22015-08-15 02:56:56537 break;
ortuno71c3f142016-03-30 18:26:58538 case TabMutedReason::MEDIA_CAPTURE:
limasdf6dcdc442016-02-26 04:58:26539 info->reason = api::tabs::MUTED_INFO_REASON_CAPTURE;
miu8b605f22015-08-15 02:56:56540 break;
ortuno71c3f142016-03-30 18:26:58541 case TabMutedReason::EXTENSION:
limasdf6dcdc442016-02-26 04:58:26542 info->reason = api::tabs::MUTED_INFO_REASON_EXTENSION;
Peter Kasting479cc1752018-09-28 22:34:37543 info->extension_id = std::make_unique<std::string>(
544 LastMuteMetadata::FromWebContents(contents)->extension_id);
545 DCHECK(!info->extension_id->empty());
miu8b605f22015-08-15 02:56:56546 break;
547 }
limasdf6dcdc442016-02-26 04:58:26548 return info;
miu8b605f22015-08-15 02:56:56549}
550
limasdf6dcdc442016-02-26 04:58:26551// static
Ivan Sandrke2b20c62018-09-10 16:23:53552void ExtensionTabUtil::SetPlatformDelegate(std::unique_ptr<Delegate> delegate) {
Daniel Bratellbe89e3442018-09-11 15:44:57553 GetExtensionTabUtilDelegateWrapper() = std::move(delegate);
isandrke0951132017-04-24 17:53:31554}
555
556// static
John Lee76e40a32019-08-20 17:52:40557ExtensionTabUtil::ScrubTabBehavior ExtensionTabUtil::GetScrubTabBehavior(
558 const Extension* extension,
John Leebfbb37cb2019-08-26 22:23:18559 Feature::Context context,
John Lee76e40a32019-08-20 17:52:40560 content::WebContents* contents) {
John Leebfbb37cb2019-08-26 22:23:18561 return GetScrubTabBehaviorImpl(extension, context, contents->GetURL(),
John Lee76e40a32019-08-20 17:52:40562 GetTabId(contents));
563}
564
565// static
566ExtensionTabUtil::ScrubTabBehavior ExtensionTabUtil::GetScrubTabBehavior(
567 const Extension* extension,
John Leebfbb37cb2019-08-26 22:23:18568 Feature::Context context,
John Lee76e40a32019-08-20 17:52:40569 const GURL& url) {
John Leebfbb37cb2019-08-26 22:23:18570 return GetScrubTabBehaviorImpl(extension, context, url,
571 api::tabs::TAB_ID_NONE);
John Lee76e40a32019-08-20 17:52:40572}
573
574// static
575void ExtensionTabUtil::ScrubTabForExtension(
576 const Extension* extension,
577 content::WebContents* contents,
578 api::tabs::Tab* tab,
579 ScrubTabBehavior scrub_tab_behavior) {
580 switch (scrub_tab_behavior) {
581 case kScrubTabFully:
582 tab->url.reset();
583 tab->title.reset();
584 tab->fav_icon_url.reset();
585 break;
586 case kScrubTabUrlToOrigin:
587 tab->url =
588 std::make_unique<std::string>(GURL(*tab->url).GetOrigin().spec());
589 break;
590 case kDontScrubTab:
591 NOTREACHED();
Daniel Bratellbe89e3442018-09-11 15:44:57592 }
[email protected]ab3f61412013-01-29 21:55:07593}
594
Kelvin Jiang56584822019-08-26 22:38:39595// static
[email protected]ea049a02011-12-25 21:37:09596bool ExtensionTabUtil::GetTabStripModel(const WebContents* web_contents,
[email protected]ac84431b2011-09-27 17:26:11597 TabStripModel** tab_strip_model,
598 int* tab_index) {
[email protected]ea049a02011-12-25 21:37:09599 DCHECK(web_contents);
[email protected]ac84431b2011-09-27 17:26:11600 DCHECK(tab_strip_model);
601 DCHECK(tab_index);
602
scottmg8abbff832016-01-28 22:57:37603 for (auto* browser : *BrowserList::GetInstance()) {
604 TabStripModel* tab_strip = browser->tab_strip_model();
[email protected]e52d0a42012-06-08 22:44:16605 int index = tab_strip->GetIndexOfWebContents(web_contents);
[email protected]ac84431b2011-09-27 17:26:11606 if (index != -1) {
607 *tab_strip_model = tab_strip;
608 *tab_index = index;
609 return true;
610 }
611 }
612
613 return false;
614}
615
616bool ExtensionTabUtil::GetDefaultTab(Browser* browser,
[email protected]72f67972012-10-30 18:53:28617 WebContents** contents,
[email protected]ac84431b2011-09-27 17:26:11618 int* tab_id) {
619 DCHECK(browser);
620 DCHECK(contents);
621
[email protected]617ee962013-01-29 20:49:12622 *contents = browser->tab_strip_model()->GetActiveWebContents();
[email protected]ac84431b2011-09-27 17:26:11623 if (*contents) {
624 if (tab_id)
[email protected]72f67972012-10-30 18:53:28625 *tab_id = GetTabId(*contents);
[email protected]ac84431b2011-09-27 17:26:11626 return true;
627 }
628
629 return false;
630}
631
Istiaque Ahmed406b9182019-07-26 06:28:33632// static
[email protected]ac84431b2011-09-27 17:26:11633bool ExtensionTabUtil::GetTabById(int tab_id,
wjmaclean5b11eee2014-09-05 00:55:14634 content::BrowserContext* browser_context,
[email protected]ac84431b2011-09-27 17:26:11635 bool include_incognito,
636 Browser** browser,
637 TabStripModel** tab_strip,
[email protected]72f67972012-10-30 18:53:28638 WebContents** contents,
[email protected]ac84431b2011-09-27 17:26:11639 int* tab_index) {
lionel.g.landwerlind2315f72015-07-21 14:17:19640 if (tab_id == api::tabs::TAB_ID_NONE)
641 return false;
wjmaclean5b11eee2014-09-05 00:55:14642 Profile* profile = Profile::FromBrowserContext(browser_context);
[email protected]ac84431b2011-09-27 17:26:11643 Profile* incognito_profile =
Catherine Mullings0524e2042017-06-26 23:09:45644 include_incognito && profile->HasOffTheRecordProfile()
645 ? profile->GetOffTheRecordProfile()
646 : nullptr;
scottmg8abbff832016-01-28 22:57:37647 for (auto* target_browser : *BrowserList::GetInstance()) {
[email protected]ac84431b2011-09-27 17:26:11648 if (target_browser->profile() == profile ||
649 target_browser->profile() == incognito_profile) {
[email protected]c0849252012-05-12 13:51:27650 TabStripModel* target_tab_strip = target_browser->tab_strip_model();
[email protected]ac84431b2011-09-27 17:26:11651 for (int i = 0; i < target_tab_strip->count(); ++i) {
[email protected]72f67972012-10-30 18:53:28652 WebContents* target_contents = target_tab_strip->GetWebContentsAt(i);
Mikel Astiz2f127c422018-04-05 19:10:27653 if (SessionTabHelper::IdForTab(target_contents).id() == tab_id) {
[email protected]ac84431b2011-09-27 17:26:11654 if (browser)
655 *browser = target_browser;
656 if (tab_strip)
657 *tab_strip = target_tab_strip;
658 if (contents)
659 *contents = target_contents;
660 if (tab_index)
661 *tab_index = i;
662 return true;
663 }
664 }
665 }
666 }
667 return false;
668}
[email protected]45c75e62012-03-21 19:56:35669
Istiaque Ahmed406b9182019-07-26 06:28:33670// static
671bool ExtensionTabUtil::GetTabById(int tab_id,
672 content::BrowserContext* browser_context,
673 bool include_incognito,
674 WebContents** contents) {
675 return GetTabById(tab_id, browser_context, include_incognito, nullptr,
676 nullptr, contents, nullptr);
677}
678
Kelvin Jiang56584822019-08-26 22:38:39679// static
680std::vector<content::WebContents*>
681ExtensionTabUtil::GetAllActiveWebContentsForContext(
682 content::BrowserContext* browser_context,
683 bool include_incognito) {
684 std::vector<content::WebContents*> active_contents;
685
686 Profile* profile = Profile::FromBrowserContext(browser_context);
687 Profile* incognito_profile =
688 include_incognito && profile->HasOffTheRecordProfile()
689 ? profile->GetOffTheRecordProfile()
690 : nullptr;
691 for (auto* target_browser : *BrowserList::GetInstance()) {
692 if (target_browser->profile() == profile ||
693 target_browser->profile() == incognito_profile) {
694 TabStripModel* target_tab_strip = target_browser->tab_strip_model();
695
696 active_contents.push_back(target_tab_strip->GetActiveWebContents());
697 }
698 }
699
700 return active_contents;
701}
702
[email protected]45c75e62012-03-21 19:56:35703GURL ExtensionTabUtil::ResolvePossiblyRelativeURL(const std::string& url_string,
[email protected]1c4fbc02013-11-13 02:52:42704 const Extension* extension) {
[email protected]45c75e62012-03-21 19:56:35705 GURL url = GURL(url_string);
706 if (!url.is_valid())
707 url = extension->GetResourceURL(url_string);
708
709 return url;
710}
711
kalmandfefe1a2015-07-13 22:27:54712bool ExtensionTabUtil::IsKillURL(const GURL& url) {
Lei Zhang4c28b5282017-08-24 04:26:05713 static const char* const kill_hosts[] = {
714 chrome::kChromeUICrashHost, chrome::kChromeUIDelayedHangUIHost,
715 chrome::kChromeUIHangUIHost, chrome::kChromeUIKillHost,
716 chrome::kChromeUIQuitHost, chrome::kChromeUIRestartHost,
717 content::kChromeUIBrowserCrashHost, content::kChromeUIMemoryExhaustHost,
kalmandfefe1a2015-07-13 22:27:54718 };
719
[email protected]45c75e62012-03-21 19:56:35720 // Check a fixed-up URL, to normalize the scheme and parse hosts correctly.
721 GURL fixed_url =
rsleevi24f64dc22015-08-07 21:39:21722 url_formatter::FixupURL(url.possibly_invalid_spec(), std::string());
kalmandfefe1a2015-07-13 22:27:54723 if (!fixed_url.SchemeIs(content::kChromeUIScheme))
724 return false;
725
brettwadc846882015-09-25 01:16:22726 base::StringPiece fixed_host = fixed_url.host_piece();
Avi Drissman5f0fb8c2018-12-25 23:20:49727 for (size_t i = 0; i < base::size(kill_hosts); ++i) {
brettwadc846882015-09-25 01:16:22728 if (fixed_host == kill_hosts[i])
kalmandfefe1a2015-07-13 22:27:54729 return true;
730 }
731
732 return false;
[email protected]45c75e62012-03-21 19:56:35733}
[email protected]73c1a6842012-07-13 17:39:04734
erikchen38fa4022018-04-26 20:37:52735void ExtensionTabUtil::CreateTab(std::unique_ptr<WebContents> web_contents,
[email protected]73c1a6842012-07-13 17:39:04736 const std::string& extension_id,
737 WindowOpenDisposition disposition,
bokan107a47f2015-02-03 23:23:39738 const gfx::Rect& initial_rect,
[email protected]73c1a6842012-07-13 17:39:04739 bool user_gesture) {
[email protected]73c1a6842012-07-13 17:39:04740 Profile* profile =
741 Profile::FromBrowserContext(web_contents->GetBrowserContext());
scottmg34c5dd882016-02-03 05:21:54742 Browser* browser = chrome::FindTabbedBrowser(profile, false);
[email protected]2764cab72012-07-19 17:02:10743 const bool browser_created = !browser;
erg7b01d692017-02-22 21:57:35744 if (!browser) {
745 Browser::CreateParams params = Browser::CreateParams(profile, user_gesture);
Toni Barzic00705742019-02-25 22:09:41746 browser = Browser::Create(params);
erg7b01d692017-02-22 21:57:35747 }
Toni Barzic00705742019-02-25 22:09:41748
749 if (!browser)
750 return;
751
erikchen38fa4022018-04-26 20:37:52752 NavigateParams params(browser, std::move(web_contents));
[email protected]73c1a6842012-07-13 17:39:04753
754 // The extension_app_id parameter ends up as app_name in the Browser
755 // which causes the Browser to return true for is_app(). This affects
756 // among other things, whether the location bar gets displayed.
757 // TODO(mpcomplete): This seems wrong. What if the extension content is hosted
758 // in a tab?
nick3b04f322016-08-31 19:29:19759 if (disposition == WindowOpenDisposition::NEW_POPUP)
[email protected]73c1a6842012-07-13 17:39:04760 params.extension_app_id = extension_id;
761
[email protected]73c1a6842012-07-13 17:39:04762 params.disposition = disposition;
bokan107a47f2015-02-03 23:23:39763 params.window_bounds = initial_rect;
cm.sanchi2522bc92017-12-04 08:04:13764 params.window_action = NavigateParams::SHOW_WINDOW;
[email protected]73c1a6842012-07-13 17:39:04765 params.user_gesture = user_gesture;
cm.sanchi2522bc92017-12-04 08:04:13766 Navigate(&params);
[email protected]2764cab72012-07-19 17:02:10767
cm.sanchi2522bc92017-12-04 08:04:13768 // Close the browser if Navigate created a new one.
[email protected]2764cab72012-07-19 17:02:10769 if (browser_created && (browser != params.browser))
770 browser->window()->Close();
[email protected]73c1a6842012-07-13 17:39:04771}
[email protected]44e329a2012-07-14 01:13:06772
773// static
774void ExtensionTabUtil::ForEachTab(
775 const base::Callback<void(WebContents*)>& callback) {
Avi Drissman2f97ce52018-02-07 23:48:23776 for (auto* web_contents : AllTabContentses())
777 callback.Run(web_contents);
[email protected]44e329a2012-07-14 01:13:06778}
[email protected]e9570fdf2012-07-18 20:01:21779
780// static
[email protected]1c4fbc02013-11-13 02:52:42781WindowController* ExtensionTabUtil::GetWindowControllerOfTab(
[email protected]e9570fdf2012-07-18 20:01:21782 const WebContents* web_contents) {
[email protected]f7b4b9e2012-12-02 07:43:17783 Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
Catherine Mullings0524e2042017-06-26 23:09:45784 if (browser != nullptr)
[email protected]e9570fdf2012-07-18 20:01:21785 return browser->extension_window_controller();
786
Catherine Mullings0524e2042017-06-26 23:09:45787 return nullptr;
[email protected]e9570fdf2012-07-18 20:01:21788}
[email protected]695089782013-04-09 16:03:17789
catmullings32ab4eeb2017-02-21 18:23:54790bool ExtensionTabUtil::OpenOptionsPageFromAPI(
791 const Extension* extension,
792 content::BrowserContext* browser_context) {
793 if (!OptionsPageInfo::HasOptionsPage(extension))
794 return false;
795 Profile* profile = Profile::FromBrowserContext(browser_context);
796 // This version of OpenOptionsPage() is only called when the extension
797 // initiated the command via chrome.runtime.openOptionsPage. For a spanning
798 // mode extension, this API could only be called from a regular profile, since
799 // that's the only place it's running.
800 DCHECK(!profile->IsOffTheRecord() || IncognitoInfo::IsSplitMode(extension));
801 Browser* browser = chrome::FindBrowserWithProfile(profile);
802 if (!browser)
Toni Barzic00705742019-02-25 22:09:41803 browser = Browser::Create(Browser::CreateParams(profile, true));
804 if (!browser)
805 return false;
catmullings32ab4eeb2017-02-21 18:23:54806 return extensions::ExtensionTabUtil::OpenOptionsPage(extension, browser);
807}
808
kalmance22c472015-02-19 02:31:43809bool ExtensionTabUtil::OpenOptionsPage(const Extension* extension,
[email protected]695089782013-04-09 16:03:17810 Browser* browser) {
kalmance22c472015-02-19 02:31:43811 if (!OptionsPageInfo::HasOptionsPage(extension))
812 return false;
[email protected]695089782013-04-09 16:03:17813
catmullings32ab4eeb2017-02-21 18:23:54814 // Force the options page to open in non-OTR window if the extension is not
815 // running in split mode, because it won't be able to save settings from OTR.
816 // This version of OpenOptionsPage() can be called from an OTR window via e.g.
817 // the action menu, since that's not initiated by the extension.
dchengc963c7142016-04-08 03:55:22818 std::unique_ptr<chrome::ScopedTabbedBrowserDisplayer> displayer;
catmullings32ab4eeb2017-02-21 18:23:54819 if (browser->profile()->IsOffTheRecord() &&
820 !IncognitoInfo::IsSplitMode(extension)) {
[email protected]0edcdec2013-10-31 06:43:08821 displayer.reset(new chrome::ScopedTabbedBrowserDisplayer(
scottmg851949002016-02-09 20:09:44822 browser->profile()->GetOriginalProfile()));
[email protected]0edcdec2013-10-31 06:43:08823 browser = displayer->browser();
[email protected]695089782013-04-09 16:03:17824 }
825
kalman4427c252015-03-13 01:55:48826 GURL url_to_navigate;
rdevlin.cronin6ef722e2016-03-16 21:42:27827 bool open_in_tab = OptionsPageInfo::ShouldOpenInTab(extension);
828 if (open_in_tab) {
kalman4427c252015-03-13 01:55:48829 // Options page tab is simply e.g. chrome-extension://.../options.html.
830 url_to_navigate = OptionsPageInfo::GetOptionsPage(extension);
831 } else {
832 // Options page tab is Extension settings pointed at that Extension's ID,
833 // e.g. chrome://extensions?options=...
834 url_to_navigate = GURL(chrome::kChromeUIExtensionsURL);
ericzengf97b7c22014-08-26 03:07:30835 GURL::Replacements replacements;
836 std::string query =
837 base::StringPrintf("options=%s", extension->id().c_str());
838 replacements.SetQueryStr(query);
kalman4427c252015-03-13 01:55:48839 url_to_navigate = url_to_navigate.ReplaceComponents(replacements);
ericzengf97b7c22014-08-26 03:07:30840 }
kalmance22c472015-02-19 02:31:43841
cm.sanchi2522bc92017-12-04 08:04:13842 NavigateParams params(
843 GetSingletonTabNavigateParams(browser, url_to_navigate));
rdevlin.cronin10a6ef012016-02-19 00:26:03844 // We need to respect path differences because we don't want opening the
845 // options page to close a page that might be open to extension content.
rdevlin.cronin6ef722e2016-03-16 21:42:27846 // However, if the options page opens inside the chrome://extensions page, we
847 // can override an existing page.
Kevin Bailey55841fa72018-03-12 22:52:05848 // Note: ref behavior is to ignore.
cm.sanchi2522bc92017-12-04 08:04:13849 params.path_behavior = open_in_tab ? NavigateParams::RESPECT
850 : NavigateParams::IGNORE_AND_NAVIGATE;
kalman4427c252015-03-13 01:55:48851 params.url = url_to_navigate;
erikchen38fa4022018-04-26 20:37:52852 ShowSingletonTabOverwritingNTP(browser, std::move(params));
kalmance22c472015-02-19 02:31:43853 return true;
[email protected]695089782013-04-09 16:03:17854}
[email protected]1c4fbc02013-11-13 02:52:42855
lionel.g.landwerlind2315f72015-07-21 14:17:19856// static
857bool ExtensionTabUtil::BrowserSupportsTabs(Browser* browser) {
Joel Hockey2687ab52019-08-14 23:59:46858 return browser && !browser->is_type_devtools();
lionel.g.landwerlind2315f72015-07-21 14:17:19859}
860
[email protected]1c4fbc02013-11-13 02:52:42861} // namespace extensions