[email protected] | 327640a | 2012-01-24 21:57:59 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 5 | #include "chrome/browser/extensions/extension_tab_util.h" |
[email protected] | 41d9faf | 2012-02-28 23:46:02 | [diff] [blame] | 6 | |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 7 | #include <stddef.h> |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 8 | #include <algorithm> |
Ivan Sandrk | e2b20c6 | 2018-09-10 16:23:53 | [diff] [blame] | 9 | #include <utility> |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 10 | |
Ivan Sandrk | e2b20c6 | 2018-09-10 16:23:53 | [diff] [blame] | 11 | #include "base/no_destructor.h" |
Peter Kasting | 1f01655 | 2019-09-09 22:20:55 | [diff] [blame] | 12 | #include "base/numerics/ranges.h" |
Avi Drissman | 5f0fb8c | 2018-12-25 23:20:49 | [diff] [blame] | 13 | #include "base/stl_util.h" |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 14 | #include "base/strings/string_number_conversions.h" |
ericzeng | f97b7c2 | 2014-08-26 03:07:30 | [diff] [blame] | 15 | #include "base/strings/stringprintf.h" |
limasdf | 6dcdc44 | 2016-02-26 04:58:26 | [diff] [blame] | 16 | #include "base/strings/utf_string_conversions.h" |
andersoncss | 12caff1 | 2016-07-14 14:43:41 | [diff] [blame] | 17 | #include "chrome/browser/browser_process.h" |
[email protected] | b19451b | 2012-06-08 17:36:19 | [diff] [blame] | 18 | #include "chrome/browser/extensions/api/tabs/tabs_constants.h" |
Devlin Cronin | ad230bb | 2018-05-30 18:41:46 | [diff] [blame] | 19 | #include "chrome/browser/extensions/browser_extension_window_controller.h" |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 20 | #include "chrome/browser/extensions/chrome_extension_function.h" |
lfg | 18533307 | 2014-09-09 20:16:11 | [diff] [blame] | 21 | #include "chrome/browser/extensions/chrome_extension_function_details.h" |
[email protected] | e9570fdf | 2012-07-18 20:01:21 | [diff] [blame] | 22 | #include "chrome/browser/extensions/tab_helper.h" |
Ivan Sandrk | e0e9245 | 2019-03-21 09:32:48 | [diff] [blame] | 23 | #include "chrome/browser/platform_util.h" |
[email protected] | b56e2e3 | 2012-05-11 21:18:04 | [diff] [blame] | 24 | #include "chrome/browser/profiles/profile.h" |
Francois Doray | 77be021 | 2018-04-05 14:26:26 | [diff] [blame] | 25 | #include "chrome/browser/resource_coordinator/tab_lifecycle_unit_external.h" |
[email protected] | e3f90c60 | 2014-08-18 12:41:59 | [diff] [blame] | 26 | #include "chrome/browser/sessions/session_tab_helper.h" |
[email protected] | b56e2e3 | 2012-05-11 21:18:04 | [diff] [blame] | 27 | #include "chrome/browser/ui/browser.h" |
[email protected] | 73c1a684 | 2012-07-13 17:39:04 | [diff] [blame] | 28 | #include "chrome/browser/ui/browser_finder.h" |
thestig | e8082124 | 2015-09-30 23:46:08 | [diff] [blame] | 29 | #include "chrome/browser/ui/browser_navigator_params.h" |
[email protected] | b56e2e3 | 2012-05-11 21:18:04 | [diff] [blame] | 30 | #include "chrome/browser/ui/browser_window.h" |
Chris Hamilton | 2013965 | 2018-06-01 15:55:01 | [diff] [blame] | 31 | #include "chrome/browser/ui/recently_audible_helper.h" |
[email protected] | 0edcdec | 2013-10-31 06:43:08 | [diff] [blame] | 32 | #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" |
ericzeng | f97b7c2 | 2014-08-26 03:07:30 | [diff] [blame] | 33 | #include "chrome/browser/ui/singleton_tabs.h" |
[email protected] | 44e329a | 2012-07-14 01:13:06 | [diff] [blame] | 34 | #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" |
[email protected] | b56e2e3 | 2012-05-11 21:18:04 | [diff] [blame] | 35 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
jared.sohn | 09a3ccc | 2015-07-09 03:47:30 | [diff] [blame] | 36 | #include "chrome/browser/ui/tabs/tab_utils.h" |
[email protected] | 760f603 | 2014-06-30 23:18:19 | [diff] [blame] | 37 | #include "chrome/common/extensions/api/tabs.h" |
[email protected] | 45c75e6 | 2012-03-21 19:56:35 | [diff] [blame] | 38 | #include "chrome/common/url_constants.h" |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 39 | #include "components/url_formatter/url_fixer.h" |
[email protected] | ad23a09 | 2011-12-28 07:02:04 | [diff] [blame] | 40 | #include "content/public/browser/favicon_status.h" |
| 41 | #include "content/public/browser/navigation_entry.h" |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 42 | #include "content/public/browser/web_contents.h" |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 43 | #include "extensions/common/constants.h" |
| 44 | #include "extensions/common/error_utils.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 45 | #include "extensions/common/extension.h" |
ericzeng | f97b7c2 | 2014-08-26 03:07:30 | [diff] [blame] | 46 | #include "extensions/common/feature_switch.h" |
[email protected] | 0c3c973 | 2013-09-16 08:53:41 | [diff] [blame] | 47 | #include "extensions/common/manifest_constants.h" |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 48 | #include "extensions/common/manifest_handlers/incognito_info.h" |
ericzeng | 09a7e00 | 2014-09-09 21:43:47 | [diff] [blame] | 49 | #include "extensions/common/manifest_handlers/options_page_info.h" |
[email protected] | 793964a | 2013-10-08 00:47:19 | [diff] [blame] | 50 | #include "extensions/common/permissions/api_permission.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 51 | #include "extensions/common/permissions/permissions_data.h" |
[email protected] | a6483d2 | 2013-07-03 22:11:00 | [diff] [blame] | 52 | #include "url/gurl.h" |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 53 | |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 54 | using content::NavigationEntry; |
[email protected] | 26b5e32 | 2011-12-23 01:36:47 | [diff] [blame] | 55 | using content::WebContents; |
[email protected] | 1c4fbc0 | 2013-11-13 02:52:42 | [diff] [blame] | 56 | |
| 57 | namespace extensions { |
[email protected] | 26b5e32 | 2011-12-23 01:36:47 | [diff] [blame] | 58 | |
[email protected] | f1c102b | 2013-02-15 07:44:12 | [diff] [blame] | 59 | namespace { |
| 60 | |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 61 | // |error_message| can optionally be passed in and will be set with an |
| 62 | // appropriate message if the window cannot be found by id. |
| 63 | Browser* GetBrowserInProfileWithId(Profile* profile, |
| 64 | const int window_id, |
Ramin Halavati | acdd17f1 | 2018-06-06 07:05:12 | [diff] [blame] | 65 | bool match_incognito_profile, |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 66 | std::string* error_message) { |
| 67 | Profile* incognito_profile = |
Ramin Halavati | acdd17f1 | 2018-06-06 07:05:12 | [diff] [blame] | 68 | match_incognito_profile && profile->HasOffTheRecordProfile() |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 69 | ? profile->GetOffTheRecordProfile() |
Catherine Mullings | 0524e204 | 2017-06-26 23:09:45 | [diff] [blame] | 70 | : nullptr; |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 71 | for (auto* browser : *BrowserList::GetInstance()) { |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 72 | 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 Tambre | fff51b75 | 2019-02-04 13:09:47 | [diff] [blame] | 82 | tabs_constants::kWindowNotFoundError, base::NumberToString(window_id)); |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 83 | |
Catherine Mullings | 0524e204 | 2017-06-26 23:09:45 | [diff] [blame] | 84 | return nullptr; |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 85 | } |
| 86 | |
erg | 7b01d69 | 2017-02-22 21:57:35 | [diff] [blame] | 87 | Browser* CreateBrowser(Profile* profile, |
| 88 | int window_id, |
| 89 | bool user_gesture, |
| 90 | std::string* error) { |
Joel Hockey | 2687ab5 | 2019-08-14 23:59:46 | [diff] [blame] | 91 | Browser::CreateParams params(Browser::TYPE_NORMAL, profile, user_gesture); |
Toni Barzic | 0070574 | 2019-02-25 22:09:41 | [diff] [blame] | 92 | Browser* browser = Browser::Create(params); |
| 93 | if (!browser) { |
| 94 | *error = tabs_constants::kBrowserWindowNotAllowed; |
| 95 | return nullptr; |
| 96 | } |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 97 | browser->window()->Show(); |
| 98 | return browser; |
| 99 | } |
| 100 | |
lionel.g.landwerlin | 85ad204 | 2015-07-23 11:26:11 | [diff] [blame] | 101 | // 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). |
| 104 | int GetTabIdForExtensions(const WebContents* web_contents) { |
| 105 | Browser* browser = chrome::FindBrowserWithWebContents(web_contents); |
| 106 | if (browser && !ExtensionTabUtil::BrowserSupportsTabs(browser)) |
| 107 | return -1; |
Mikel Astiz | 2f127c42 | 2018-04-05 19:10:27 | [diff] [blame] | 108 | return SessionTabHelper::IdForTab(web_contents).id(); |
lionel.g.landwerlin | 85ad204 | 2015-07-23 11:26:11 | [diff] [blame] | 109 | } |
| 110 | |
Daniel Bratell | be89e344 | 2018-09-11 15:44:57 | [diff] [blame] | 111 | std::unique_ptr<ExtensionTabUtil::Delegate>& |
| 112 | GetExtensionTabUtilDelegateWrapper() { |
Ivan Sandrk | e2b20c6 | 2018-09-10 16:23:53 | [diff] [blame] | 113 | static base::NoDestructor<std::unique_ptr<ExtensionTabUtil::Delegate>> |
| 114 | delegate_wrapper; |
| 115 | return *delegate_wrapper; |
| 116 | } |
| 117 | |
Daniel Bratell | be89e344 | 2018-09-11 15:44:57 | [diff] [blame] | 118 | ExtensionTabUtil::Delegate* GetExtensionTabUtilDelegate() { |
| 119 | return GetExtensionTabUtilDelegateWrapper().get(); |
Ivan Sandrk | e2b20c6 | 2018-09-10 16:23:53 | [diff] [blame] | 120 | } |
isandrk | e095113 | 2017-04-24 17:53:31 | [diff] [blame] | 121 | |
John Lee | 76e40a3 | 2019-08-20 17:52:40 | [diff] [blame] | 122 | ExtensionTabUtil::ScrubTabBehavior GetScrubTabBehaviorImpl( |
| 123 | const Extension* extension, |
John Lee | bfbb37cb | 2019-08-26 22:23:18 | [diff] [blame] | 124 | Feature::Context context, |
John Lee | 76e40a3 | 2019-08-20 17:52:40 | [diff] [blame] | 125 | const GURL& url, |
| 126 | int tab_id) { |
John Lee | bfbb37cb | 2019-08-26 22:23:18 | [diff] [blame] | 127 | if (context == Feature::Context::WEBUI_CONTEXT) { |
| 128 | return ExtensionTabUtil::kDontScrubTab; |
| 129 | } |
| 130 | |
John Lee | 76e40a3 | 2019-08-20 17:52:40 | [diff] [blame] | 131 | 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] | f1c102b | 2013-02-15 07:44:12 | [diff] [blame] | 160 | } // namespace |
| 161 | |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 162 | ExtensionTabUtil::OpenTabParams::OpenTabParams() |
| 163 | : create_browser_if_needed(false) { |
| 164 | } |
| 165 | |
| 166 | ExtensionTabUtil::OpenTabParams::~OpenTabParams() { |
| 167 | } |
| 168 | |
Catherine Mullings | 0524e204 | 2017-06-26 23:09:45 | [diff] [blame] | 169 | // Opens a new tab for a given extension. Returns nullptr and sets |error| if an |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 170 | // error occurs. |
Clark DuVall | fd4db3d | 2019-07-30 19:10:43 | [diff] [blame] | 171 | base::DictionaryValue* ExtensionTabUtil::OpenTab(ExtensionFunction* function, |
| 172 | const OpenTabParams& params, |
| 173 | bool user_gesture, |
| 174 | std::string* error) { |
rdevlin.cronin | 3223122d | 2016-09-07 21:27:26 | [diff] [blame] | 175 | ChromeExtensionFunctionDetails chrome_details(function); |
| 176 | Profile* profile = chrome_details.GetProfile(); |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 177 | // 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.cronin | 3223122d | 2016-09-07 21:27:26 | [diff] [blame] | 182 | Browser* browser = GetBrowserFromWindowID(chrome_details, window_id, error); |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 183 | if (!browser) { |
| 184 | if (!params.create_browser_if_needed) { |
Catherine Mullings | 0524e204 | 2017-06-26 23:09:45 | [diff] [blame] | 185 | return nullptr; |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 186 | } |
erg | 7b01d69 | 2017-02-22 21:57:35 | [diff] [blame] | 187 | browser = CreateBrowser(profile, window_id, user_gesture, error); |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 188 | if (!browser) |
Catherine Mullings | 0524e204 | 2017-06-26 23:09:45 | [diff] [blame] | 189 | return nullptr; |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 190 | } |
| 191 | |
Joel Hockey | 2687ab5 | 2019-08-14 23:59:46 | [diff] [blame] | 192 | // Ensure the selected browser is normal. |
| 193 | if (!browser->is_type_normal() && browser->IsAttemptingToCloseBrowser()) |
Ramin Halavati | acdd17f1 | 2018-06-06 07:05:12 | [diff] [blame] | 194 | browser = chrome::FindTabbedBrowser( |
| 195 | profile, function->include_incognito_information()); |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 196 | if (!browser || !browser->window()) { |
deepak.m1 | 21895c01 | 2015-05-11 08:11:19 | [diff] [blame] | 197 | if (error) |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 198 | *error = tabs_constants::kNoCurrentWindowError; |
Catherine Mullings | 0524e204 | 2017-06-26 23:09:45 | [diff] [blame] | 199 | return nullptr; |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | // TODO(jstritar): Add a constant, chrome.tabs.TAB_ID_ACTIVE, that |
| 203 | // represents the active tab. |
Catherine Mullings | 0524e204 | 2017-06-26 23:09:45 | [diff] [blame] | 204 | WebContents* opener = nullptr; |
| 205 | Browser* opener_browser = nullptr; |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 206 | if (params.opener_tab_id.get()) { |
| 207 | int opener_id = *params.opener_tab_id; |
| 208 | |
Catherine Mullings | 0524e204 | 2017-06-26 23:09:45 | [diff] [blame] | 209 | if (!ExtensionTabUtil::GetTabById( |
Ramin Halavati | acdd17f1 | 2018-06-06 07:05:12 | [diff] [blame] | 210 | opener_id, profile, function->include_incognito_information(), |
| 211 | &opener_browser, nullptr, &opener, nullptr)) { |
deepak.m1 | 21895c01 | 2015-05-11 08:11:19 | [diff] [blame] | 212 | if (error) { |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 213 | *error = ErrorUtils::FormatErrorMessage( |
Raul Tambre | fff51b75 | 2019-02-04 13:09:47 | [diff] [blame] | 214 | tabs_constants::kTabNotFoundError, base::NumberToString(opener_id)); |
deepak.m1 | 21895c01 | 2015-05-11 08:11:19 | [diff] [blame] | 215 | } |
Catherine Mullings | 0524e204 | 2017-06-26 23:09:45 | [diff] [blame] | 216 | return nullptr; |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
| 220 | // TODO(rafaelw): handle setting remaining tab properties: |
| 221 | // -title |
| 222 | // -favIconUrl |
| 223 | |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 224 | GURL url; |
| 225 | if (params.url.get()) { |
bokan | 107a47f | 2015-02-03 23:23:39 | [diff] [blame] | 226 | std::string url_string = *params.url; |
[email protected] | eba8f7d | 2014-07-28 22:09:23 | [diff] [blame] | 227 | url = ExtensionTabUtil::ResolvePossiblyRelativeURL(url_string, |
| 228 | function->extension()); |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 229 | if (!url.is_valid()) { |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 230 | *error = ErrorUtils::FormatErrorMessage(tabs_constants::kInvalidUrlError, |
| 231 | url_string); |
Catherine Mullings | 0524e204 | 2017-06-26 23:09:45 | [diff] [blame] | 232 | return nullptr; |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 233 | } |
[email protected] | f5ec3c92 | 2014-05-22 15:04:03 | [diff] [blame] | 234 | } else { |
| 235 | url = GURL(chrome::kChromeUINewTabURL); |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | // Don't let extensions crash the browser or renderers. |
kalman | dfefe1a | 2015-07-13 22:27:54 | [diff] [blame] | 239 | if (ExtensionTabUtil::IsKillURL(url)) { |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 240 | *error = tabs_constants::kNoCrashBrowserError; |
Catherine Mullings | 0524e204 | 2017-06-26 23:09:45 | [diff] [blame] | 241 | return nullptr; |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 242 | } |
| 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) && |
calamity | 862d9032 | 2017-05-22 07:14:28 | [diff] [blame] | 259 | (!function->extension() || |
| 260 | !IncognitoInfo::IsSplitMode(function->extension())) && |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 261 | browser->profile()->IsOffTheRecord()) { |
| 262 | Profile* profile = browser->profile()->GetOriginalProfile(); |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 263 | |
scottmg | 34c5dd88 | 2016-02-03 05:21:54 | [diff] [blame] | 264 | browser = chrome::FindTabbedBrowser(profile, false); |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 265 | if (!browser) { |
erg | 7b01d69 | 2017-02-22 21:57:35 | [diff] [blame] | 266 | Browser::CreateParams params = |
Joel Hockey | 2687ab5 | 2019-08-14 23:59:46 | [diff] [blame] | 267 | Browser::CreateParams(Browser::TYPE_NORMAL, profile, user_gesture); |
Toni Barzic | 0070574 | 2019-02-25 22:09:41 | [diff] [blame] | 268 | browser = Browser::Create(params); |
| 269 | if (!browser) { |
| 270 | *error = tabs_constants::kBrowserWindowNotAllowed; |
| 271 | return nullptr; |
| 272 | } |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 273 | browser->window()->Show(); |
| 274 | } |
| 275 | } |
| 276 | |
Catherine Mullings | 0524e204 | 2017-06-26 23:09:45 | [diff] [blame] | 277 | 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] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 284 | // 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 Kasting | 1f01655 | 2019-09-09 22:20:55 | [diff] [blame] | 289 | index = base::ClampToRange(index, -1, browser->tab_strip_model()->count()); |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 290 | |
| 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.sanchi | 2522bc9 | 2017-12-04 08:04:13 | [diff] [blame] | 295 | NavigateParams navigate_params(browser, url, ui::PAGE_TRANSITION_LINK); |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 296 | navigate_params.disposition = active |
| 297 | ? WindowOpenDisposition::NEW_FOREGROUND_TAB |
| 298 | : WindowOpenDisposition::NEW_BACKGROUND_TAB; |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 299 | navigate_params.tabstrip_index = index; |
| 300 | navigate_params.tabstrip_add_types = add_types; |
cm.sanchi | 2522bc9 | 2017-12-04 08:04:13 | [diff] [blame] | 301 | Navigate(&navigate_params); |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 302 | |
| 303 | // The tab may have been created in a different window, so make sure we look |
| 304 | // at the right tab strip. |
Peter Kasting | 1f01655 | 2019-09-09 22:20:55 | [diff] [blame] | 305 | TabStripModel* tab_strip = navigate_params.browser->tab_strip_model(); |
erikchen | 38fa402 | 2018-04-26 20:37:52 | [diff] [blame] | 306 | int new_index = tab_strip->GetIndexOfWebContents( |
| 307 | navigate_params.navigated_or_inserted_contents); |
rdevlin.cronin | 5b3827b6 | 2017-03-08 02:38:09 | [diff] [blame] | 308 | if (opener) { |
| 309 | // Only set the opener if the opener tab is in the same tab strip as the |
| 310 | // new tab. |
rdevlin.cronin | 5b3827b6 | 2017-03-08 02:38:09 | [diff] [blame] | 311 | if (tab_strip->GetIndexOfWebContents(opener) != TabStripModel::kNoTab) |
| 312 | tab_strip->SetOpenerOfWebContentsAt(new_index, opener); |
| 313 | } |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 314 | |
| 315 | if (active) |
erikchen | 38fa402 | 2018-04-26 20:37:52 | [diff] [blame] | 316 | navigate_params.navigated_or_inserted_contents->SetInitialFocus(); |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 317 | |
John Lee | 76e40a3 | 2019-08-20 17:52:40 | [diff] [blame] | 318 | ExtensionTabUtil::ScrubTabBehavior scrub_tab_behavior = |
| 319 | ExtensionTabUtil::GetScrubTabBehavior( |
John Lee | bfbb37cb | 2019-08-26 22:23:18 | [diff] [blame] | 320 | function->extension(), function->source_context_type(), |
John Lee | 76e40a3 | 2019-08-20 17:52:40 | [diff] [blame] | 321 | navigate_params.navigated_or_inserted_contents); |
| 322 | |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 323 | // Return data about the newly created tab. |
erikchen | 38fa402 | 2018-04-26 20:37:52 | [diff] [blame] | 324 | return ExtensionTabUtil::CreateTabObject( |
John Lee | 76e40a3 | 2019-08-20 17:52:40 | [diff] [blame] | 325 | navigate_params.navigated_or_inserted_contents, scrub_tab_behavior, |
erikchen | 38fa402 | 2018-04-26 20:37:52 | [diff] [blame] | 326 | function->extension(), tab_strip, new_index) |
limasdf | 6dcdc44 | 2016-02-26 04:58:26 | [diff] [blame] | 327 | ->ToValue() |
| 328 | .release(); |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | Browser* ExtensionTabUtil::GetBrowserFromWindowID( |
lfg | 18533307 | 2014-09-09 20:16:11 | [diff] [blame] | 332 | 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) |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 339 | *error = tabs_constants::kNoCurrentWindowError; |
Catherine Mullings | 0524e204 | 2017-06-26 23:09:45 | [diff] [blame] | 340 | return nullptr; |
lfg | 18533307 | 2014-09-09 20:16:11 | [diff] [blame] | 341 | } |
| 342 | return result; |
| 343 | } else { |
Ramin Halavati | acdd17f1 | 2018-06-06 07:05:12 | [diff] [blame] | 344 | return GetBrowserInProfileWithId( |
| 345 | details.GetProfile(), window_id, |
| 346 | details.function()->include_incognito_information(), error); |
lfg | 18533307 | 2014-09-09 20:16:11 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 350 | int ExtensionTabUtil::GetWindowId(const Browser* browser) { |
| 351 | return browser->session_id().id(); |
| 352 | } |
| 353 | |
[email protected] | 8c3495c | 2011-09-28 03:32:30 | [diff] [blame] | 354 | int ExtensionTabUtil::GetWindowIdOfTabStripModel( |
| 355 | const TabStripModel* tab_strip_model) { |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 356 | for (auto* browser : *BrowserList::GetInstance()) { |
| 357 | if (browser->tab_strip_model() == tab_strip_model) |
| 358 | return GetWindowId(browser); |
[email protected] | 8c3495c | 2011-09-28 03:32:30 | [diff] [blame] | 359 | } |
| 360 | return -1; |
| 361 | } |
| 362 | |
[email protected] | d2bd5fde | 2014-05-29 02:24:31 | [diff] [blame] | 363 | int ExtensionTabUtil::GetTabId(const WebContents* web_contents) { |
Mikel Astiz | 2f127c42 | 2018-04-05 19:10:27 | [diff] [blame] | 364 | return SessionTabHelper::IdForTab(web_contents).id(); |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | std::string ExtensionTabUtil::GetTabStatusText(bool is_loading) { |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 368 | return is_loading ? tabs_constants::kStatusValueLoading |
| 369 | : tabs_constants::kStatusValueComplete; |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 370 | } |
| 371 | |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 372 | int ExtensionTabUtil::GetWindowIdOfTab(const WebContents* web_contents) { |
Mikel Astiz | 2f127c42 | 2018-04-05 19:10:27 | [diff] [blame] | 373 | return SessionTabHelper::IdForWindowContainingTab(web_contents).id(); |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 374 | } |
| 375 | |
limasdf | 6dcdc44 | 2016-02-26 04:58:26 | [diff] [blame] | 376 | // static |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 377 | std::string ExtensionTabUtil::GetBrowserWindowTypeText(const Browser& browser) { |
Joel Hockey | 2687ab5 | 2019-08-14 23:59:46 | [diff] [blame] | 378 | if (browser.is_type_devtools()) |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 379 | return tabs_constants::kWindowTypeValueDevTools; |
Joel Hockey | 2687ab5 | 2019-08-14 23:59:46 | [diff] [blame] | 380 | // 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()) |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 384 | return tabs_constants::kWindowTypeValuePopup; |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 385 | return tabs_constants::kWindowTypeValueNormal; |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | // static |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 389 | std::unique_ptr<api::tabs::Tab> ExtensionTabUtil::CreateTabObject( |
[email protected] | fc2b46b | 2014-05-03 16:33:45 | [diff] [blame] | 390 | WebContents* contents, |
Devlin Cronin | 1980f44d | 2018-02-08 23:14:01 | [diff] [blame] | 391 | ScrubTabBehavior scrub_tab_behavior, |
| 392 | const Extension* extension, |
[email protected] | f34706be | 2012-09-04 07:32:09 | [diff] [blame] | 393 | TabStripModel* tab_strip, |
[email protected] | 304fd15 | 2013-01-12 16:54:44 | [diff] [blame] | 394 | int tab_index) { |
[email protected] | 0c9f326 | 2012-09-17 05:59:06 | [diff] [blame] | 395 | if (!tab_strip) |
| 396 | ExtensionTabUtil::GetTabStripModel(contents, &tab_strip, &tab_index); |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 397 | bool is_loading = contents->IsLoading(); |
Jinho Bang | b5216cec | 2018-01-17 19:43:11 | [diff] [blame] | 398 | auto tab_object = std::make_unique<api::tabs::Tab>(); |
| 399 | tab_object->id = std::make_unique<int>(GetTabIdForExtensions(contents)); |
limasdf | 6dcdc44 | 2016-02-26 04:58:26 | [diff] [blame] | 400 | tab_object->index = tab_index; |
| 401 | tab_object->window_id = GetWindowIdOfTab(contents); |
lazyboy | 8b1df30 | 2017-04-26 23:37:41 | [diff] [blame] | 402 | tab_object->status = |
Jinho Bang | b5216cec | 2018-01-17 19:43:11 | [diff] [blame] | 403 | std::make_unique<std::string>(GetTabStatusText(is_loading)); |
limasdf | 6dcdc44 | 2016-02-26 04:58:26 | [diff] [blame] | 404 | 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 Hamilton | 2013965 | 2018-06-01 15:55:01 | [diff] [blame] | 408 | auto* audible_helper = RecentlyAudibleHelper::FromWebContents(contents); |
Chris Hamilton | 737e22b4 | 2018-06-04 18:33:14 | [diff] [blame] | 409 | 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 Doray | 77be021 | 2018-04-05 14:26:26 | [diff] [blame] | 420 | auto* tab_lifeycle_unit_external = |
| 421 | resource_coordinator::TabLifecycleUnitExternal::FromWebContents(contents); |
andersoncss | fd947a4 | 2016-08-12 14:10:42 | [diff] [blame] | 422 | tab_object->discarded = |
Francois Doray | 77be021 | 2018-04-05 14:26:26 | [diff] [blame] | 423 | tab_lifeycle_unit_external && tab_lifeycle_unit_external->IsDiscarded(); |
andersoncss | fd947a4 | 2016-08-12 14:10:42 | [diff] [blame] | 424 | tab_object->auto_discardable = |
Francois Doray | 77be021 | 2018-04-05 14:26:26 | [diff] [blame] | 425 | !tab_lifeycle_unit_external || |
| 426 | tab_lifeycle_unit_external->IsAutoDiscardable(); |
limasdf | 6dcdc44 | 2016-02-26 04:58:26 | [diff] [blame] | 427 | tab_object->muted_info = CreateMutedInfo(contents); |
| 428 | tab_object->incognito = contents->GetBrowserContext()->IsOffTheRecord(); |
lazyboy | 8b1df30 | 2017-04-26 23:37:41 | [diff] [blame] | 429 | gfx::Size contents_size = contents->GetContainerBounds().size(); |
Jinho Bang | b5216cec | 2018-01-17 19:43:11 | [diff] [blame] | 430 | tab_object->width = std::make_unique<int>(contents_size.width()); |
| 431 | tab_object->height = std::make_unique<int>(contents_size.height()); |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 432 | |
Jinho Bang | b5216cec | 2018-01-17 19:43:11 | [diff] [blame] | 433 | tab_object->url = std::make_unique<std::string>(contents->GetURL().spec()); |
lazyboy | 8b1df30 | 2017-04-26 23:37:41 | [diff] [blame] | 434 | tab_object->title = |
Jinho Bang | b5216cec | 2018-01-17 19:43:11 | [diff] [blame] | 435 | std::make_unique<std::string>(base::UTF16ToUTF8(contents->GetTitle())); |
limasdf | 1f07189 | 2016-02-09 10:18:00 | [diff] [blame] | 436 | NavigationEntry* entry = contents->GetController().GetVisibleEntry(); |
lazyboy | 8b1df30 | 2017-04-26 23:37:41 | [diff] [blame] | 437 | if (entry && entry->GetFavicon().valid) { |
| 438 | tab_object->fav_icon_url = |
Jinho Bang | b5216cec | 2018-01-17 19:43:11 | [diff] [blame] | 439 | std::make_unique<std::string>(entry->GetFavicon().url.spec()); |
lazyboy | 8b1df30 | 2017-04-26 23:37:41 | [diff] [blame] | 440 | } |
[email protected] | 327640a | 2012-01-24 21:57:59 | [diff] [blame] | 441 | if (tab_strip) { |
[email protected] | 6707540 | 2012-10-26 08:26:25 | [diff] [blame] | 442 | WebContents* opener = tab_strip->GetOpenerOfWebContentsAt(tab_index); |
lazyboy | 8b1df30 | 2017-04-26 23:37:41 | [diff] [blame] | 443 | if (opener) { |
| 444 | tab_object->opener_tab_id = |
Jinho Bang | b5216cec | 2018-01-17 19:43:11 | [diff] [blame] | 445 | std::make_unique<int>(GetTabIdForExtensions(opener)); |
lazyboy | 8b1df30 | 2017-04-26 23:37:41 | [diff] [blame] | 446 | } |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 447 | } |
| 448 | |
John Lee | 76e40a3 | 2019-08-20 17:52:40 | [diff] [blame] | 449 | if (scrub_tab_behavior != kDontScrubTab) |
| 450 | ScrubTabForExtension(extension, contents, tab_object.get(), |
| 451 | scrub_tab_behavior); |
limasdf | 6dcdc44 | 2016-02-26 04:58:26 | [diff] [blame] | 452 | return tab_object; |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 453 | } |
| 454 | |
Devlin Cronin | 1980f44d | 2018-02-08 23:14:01 | [diff] [blame] | 455 | std::unique_ptr<base::ListValue> ExtensionTabUtil::CreateTabList( |
| 456 | const Browser* browser, |
John Lee | bfbb37cb | 2019-08-26 22:23:18 | [diff] [blame] | 457 | const Extension* extension, |
| 458 | Feature::Context context) { |
Devlin Cronin | 1980f44d | 2018-02-08 23:14:01 | [diff] [blame] | 459 | 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 Lee | 76e40a3 | 2019-08-20 17:52:40 | [diff] [blame] | 462 | WebContents* web_contents = tab_strip->GetWebContentsAt(i); |
| 463 | ExtensionTabUtil::ScrubTabBehavior scrub_tab_behavior = |
John Lee | bfbb37cb | 2019-08-26 22:23:18 | [diff] [blame] | 464 | ExtensionTabUtil::GetScrubTabBehavior(extension, context, web_contents); |
John Lee | 76e40a3 | 2019-08-20 17:52:40 | [diff] [blame] | 465 | tab_list->Append(CreateTabObject(web_contents, scrub_tab_behavior, |
Devlin Cronin | 1980f44d | 2018-02-08 23:14:01 | [diff] [blame] | 466 | extension, tab_strip, i) |
| 467 | ->ToValue()); |
| 468 | } |
| 469 | |
| 470 | return tab_list; |
| 471 | } |
| 472 | |
miu | 8b605f2 | 2015-08-15 02:56:56 | [diff] [blame] | 473 | // static |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 474 | std::unique_ptr<base::DictionaryValue> |
| 475 | ExtensionTabUtil::CreateWindowValueForExtension( |
| 476 | const Browser& browser, |
| 477 | const Extension* extension, |
John Lee | bfbb37cb | 2019-08-26 22:23:18 | [diff] [blame] | 478 | PopulateTabBehavior populate_tab_behavior, |
| 479 | Feature::Context context) { |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 480 | auto result = std::make_unique<base::DictionaryValue>(); |
| 481 | |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 482 | result->SetInteger(tabs_constants::kIdKey, browser.session_id().id()); |
| 483 | result->SetString(tabs_constants::kWindowTypeKey, |
| 484 | GetBrowserWindowTypeText(browser)); |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 485 | ui::BaseWindow* window = browser.window(); |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 486 | result->SetBoolean(tabs_constants::kFocusedKey, window->IsActive()); |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 487 | const Profile* profile = browser.profile(); |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 488 | result->SetBoolean(tabs_constants::kIncognitoKey, profile->IsOffTheRecord()); |
Avi Drissman | 9e653328 | 2019-07-17 17:25:12 | [diff] [blame] | 489 | result->SetBoolean( |
| 490 | tabs_constants::kAlwaysOnTopKey, |
| 491 | window->GetZOrderLevel() == ui::ZOrderLevel::kFloatingWindow); |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 492 | |
| 493 | std::string window_state; |
| 494 | if (window->IsMinimized()) { |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 495 | window_state = tabs_constants::kShowStateValueMinimized; |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 496 | } else if (window->IsFullscreen()) { |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 497 | window_state = tabs_constants::kShowStateValueFullscreen; |
Ivan Sandrk | e0e9245 | 2019-03-21 09:32:48 | [diff] [blame] | 498 | if (platform_util::IsBrowserLockedFullscreen(&browser)) |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 499 | window_state = tabs_constants::kShowStateValueLockedFullscreen; |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 500 | } else if (window->IsMaximized()) { |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 501 | window_state = tabs_constants::kShowStateValueMaximized; |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 502 | } else { |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 503 | window_state = tabs_constants::kShowStateValueNormal; |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 504 | } |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 505 | result->SetString(tabs_constants::kShowStateKey, window_state); |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 506 | |
| 507 | gfx::Rect bounds; |
| 508 | if (window->IsMinimized()) |
| 509 | bounds = window->GetRestoredBounds(); |
| 510 | else |
| 511 | bounds = window->GetBounds(); |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 512 | 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 Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 516 | |
| 517 | if (populate_tab_behavior == kPopulateTabs) |
John Lee | bfbb37cb | 2019-08-26 22:23:18 | [diff] [blame] | 518 | result->Set(tabs_constants::kTabsKey, |
| 519 | CreateTabList(&browser, extension, context)); |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 520 | |
| 521 | return result; |
| 522 | } |
| 523 | |
| 524 | // static |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 525 | std::unique_ptr<api::tabs::MutedInfo> ExtensionTabUtil::CreateMutedInfo( |
miu | 8b605f2 | 2015-08-15 02:56:56 | [diff] [blame] | 526 | content::WebContents* contents) { |
| 527 | DCHECK(contents); |
Peter Kasting | 479cc175 | 2018-09-28 22:34:37 | [diff] [blame] | 528 | auto info = std::make_unique<api::tabs::MutedInfo>(); |
limasdf | 6dcdc44 | 2016-02-26 04:58:26 | [diff] [blame] | 529 | info->muted = contents->IsAudioMuted(); |
miu | 8b605f2 | 2015-08-15 02:56:56 | [diff] [blame] | 530 | switch (chrome::GetTabAudioMutedReason(contents)) { |
ortuno | 71c3f14 | 2016-03-30 18:26:58 | [diff] [blame] | 531 | case TabMutedReason::NONE: |
miu | 8b605f2 | 2015-08-15 02:56:56 | [diff] [blame] | 532 | break; |
Tommy Steimel | a8c3757e | 2017-08-24 00:15:46 | [diff] [blame] | 533 | case TabMutedReason::CONTENT_SETTING: |
Tommy Steimel | 37112950 | 2017-11-29 19:41:45 | [diff] [blame] | 534 | case TabMutedReason::CONTENT_SETTING_CHROME: |
Tommy Steimel | a8c3757e | 2017-08-24 00:15:46 | [diff] [blame] | 535 | case TabMutedReason::CONTEXT_MENU: |
limasdf | 6dcdc44 | 2016-02-26 04:58:26 | [diff] [blame] | 536 | info->reason = api::tabs::MUTED_INFO_REASON_USER; |
miu | 8b605f2 | 2015-08-15 02:56:56 | [diff] [blame] | 537 | break; |
ortuno | 71c3f14 | 2016-03-30 18:26:58 | [diff] [blame] | 538 | case TabMutedReason::MEDIA_CAPTURE: |
limasdf | 6dcdc44 | 2016-02-26 04:58:26 | [diff] [blame] | 539 | info->reason = api::tabs::MUTED_INFO_REASON_CAPTURE; |
miu | 8b605f2 | 2015-08-15 02:56:56 | [diff] [blame] | 540 | break; |
ortuno | 71c3f14 | 2016-03-30 18:26:58 | [diff] [blame] | 541 | case TabMutedReason::EXTENSION: |
limasdf | 6dcdc44 | 2016-02-26 04:58:26 | [diff] [blame] | 542 | info->reason = api::tabs::MUTED_INFO_REASON_EXTENSION; |
Peter Kasting | 479cc175 | 2018-09-28 22:34:37 | [diff] [blame] | 543 | info->extension_id = std::make_unique<std::string>( |
| 544 | LastMuteMetadata::FromWebContents(contents)->extension_id); |
| 545 | DCHECK(!info->extension_id->empty()); |
miu | 8b605f2 | 2015-08-15 02:56:56 | [diff] [blame] | 546 | break; |
| 547 | } |
limasdf | 6dcdc44 | 2016-02-26 04:58:26 | [diff] [blame] | 548 | return info; |
miu | 8b605f2 | 2015-08-15 02:56:56 | [diff] [blame] | 549 | } |
| 550 | |
limasdf | 6dcdc44 | 2016-02-26 04:58:26 | [diff] [blame] | 551 | // static |
Ivan Sandrk | e2b20c6 | 2018-09-10 16:23:53 | [diff] [blame] | 552 | void ExtensionTabUtil::SetPlatformDelegate(std::unique_ptr<Delegate> delegate) { |
Daniel Bratell | be89e344 | 2018-09-11 15:44:57 | [diff] [blame] | 553 | GetExtensionTabUtilDelegateWrapper() = std::move(delegate); |
isandrk | e095113 | 2017-04-24 17:53:31 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | // static |
John Lee | 76e40a3 | 2019-08-20 17:52:40 | [diff] [blame] | 557 | ExtensionTabUtil::ScrubTabBehavior ExtensionTabUtil::GetScrubTabBehavior( |
| 558 | const Extension* extension, |
John Lee | bfbb37cb | 2019-08-26 22:23:18 | [diff] [blame] | 559 | Feature::Context context, |
John Lee | 76e40a3 | 2019-08-20 17:52:40 | [diff] [blame] | 560 | content::WebContents* contents) { |
John Lee | bfbb37cb | 2019-08-26 22:23:18 | [diff] [blame] | 561 | return GetScrubTabBehaviorImpl(extension, context, contents->GetURL(), |
John Lee | 76e40a3 | 2019-08-20 17:52:40 | [diff] [blame] | 562 | GetTabId(contents)); |
| 563 | } |
| 564 | |
| 565 | // static |
| 566 | ExtensionTabUtil::ScrubTabBehavior ExtensionTabUtil::GetScrubTabBehavior( |
| 567 | const Extension* extension, |
John Lee | bfbb37cb | 2019-08-26 22:23:18 | [diff] [blame] | 568 | Feature::Context context, |
John Lee | 76e40a3 | 2019-08-20 17:52:40 | [diff] [blame] | 569 | const GURL& url) { |
John Lee | bfbb37cb | 2019-08-26 22:23:18 | [diff] [blame] | 570 | return GetScrubTabBehaviorImpl(extension, context, url, |
| 571 | api::tabs::TAB_ID_NONE); |
John Lee | 76e40a3 | 2019-08-20 17:52:40 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | // static |
| 575 | void 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 Bratell | be89e344 | 2018-09-11 15:44:57 | [diff] [blame] | 592 | } |
[email protected] | ab3f6141 | 2013-01-29 21:55:07 | [diff] [blame] | 593 | } |
| 594 | |
Kelvin Jiang | 5658482 | 2019-08-26 22:38:39 | [diff] [blame] | 595 | // static |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 596 | bool ExtensionTabUtil::GetTabStripModel(const WebContents* web_contents, |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 597 | TabStripModel** tab_strip_model, |
| 598 | int* tab_index) { |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 599 | DCHECK(web_contents); |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 600 | DCHECK(tab_strip_model); |
| 601 | DCHECK(tab_index); |
| 602 | |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 603 | for (auto* browser : *BrowserList::GetInstance()) { |
| 604 | TabStripModel* tab_strip = browser->tab_strip_model(); |
[email protected] | e52d0a4 | 2012-06-08 22:44:16 | [diff] [blame] | 605 | int index = tab_strip->GetIndexOfWebContents(web_contents); |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 606 | if (index != -1) { |
| 607 | *tab_strip_model = tab_strip; |
| 608 | *tab_index = index; |
| 609 | return true; |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | return false; |
| 614 | } |
| 615 | |
| 616 | bool ExtensionTabUtil::GetDefaultTab(Browser* browser, |
[email protected] | 72f6797 | 2012-10-30 18:53:28 | [diff] [blame] | 617 | WebContents** contents, |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 618 | int* tab_id) { |
| 619 | DCHECK(browser); |
| 620 | DCHECK(contents); |
| 621 | |
[email protected] | 617ee96 | 2013-01-29 20:49:12 | [diff] [blame] | 622 | *contents = browser->tab_strip_model()->GetActiveWebContents(); |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 623 | if (*contents) { |
| 624 | if (tab_id) |
[email protected] | 72f6797 | 2012-10-30 18:53:28 | [diff] [blame] | 625 | *tab_id = GetTabId(*contents); |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 626 | return true; |
| 627 | } |
| 628 | |
| 629 | return false; |
| 630 | } |
| 631 | |
Istiaque Ahmed | 406b918 | 2019-07-26 06:28:33 | [diff] [blame] | 632 | // static |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 633 | bool ExtensionTabUtil::GetTabById(int tab_id, |
wjmaclean | 5b11eee | 2014-09-05 00:55:14 | [diff] [blame] | 634 | content::BrowserContext* browser_context, |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 635 | bool include_incognito, |
| 636 | Browser** browser, |
| 637 | TabStripModel** tab_strip, |
[email protected] | 72f6797 | 2012-10-30 18:53:28 | [diff] [blame] | 638 | WebContents** contents, |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 639 | int* tab_index) { |
lionel.g.landwerlin | d2315f7 | 2015-07-21 14:17:19 | [diff] [blame] | 640 | if (tab_id == api::tabs::TAB_ID_NONE) |
| 641 | return false; |
wjmaclean | 5b11eee | 2014-09-05 00:55:14 | [diff] [blame] | 642 | Profile* profile = Profile::FromBrowserContext(browser_context); |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 643 | Profile* incognito_profile = |
Catherine Mullings | 0524e204 | 2017-06-26 23:09:45 | [diff] [blame] | 644 | include_incognito && profile->HasOffTheRecordProfile() |
| 645 | ? profile->GetOffTheRecordProfile() |
| 646 | : nullptr; |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 647 | for (auto* target_browser : *BrowserList::GetInstance()) { |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 648 | if (target_browser->profile() == profile || |
| 649 | target_browser->profile() == incognito_profile) { |
[email protected] | c084925 | 2012-05-12 13:51:27 | [diff] [blame] | 650 | TabStripModel* target_tab_strip = target_browser->tab_strip_model(); |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 651 | for (int i = 0; i < target_tab_strip->count(); ++i) { |
[email protected] | 72f6797 | 2012-10-30 18:53:28 | [diff] [blame] | 652 | WebContents* target_contents = target_tab_strip->GetWebContentsAt(i); |
Mikel Astiz | 2f127c42 | 2018-04-05 19:10:27 | [diff] [blame] | 653 | if (SessionTabHelper::IdForTab(target_contents).id() == tab_id) { |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 654 | 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] | 45c75e6 | 2012-03-21 19:56:35 | [diff] [blame] | 669 | |
Istiaque Ahmed | 406b918 | 2019-07-26 06:28:33 | [diff] [blame] | 670 | // static |
| 671 | bool 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 Jiang | 5658482 | 2019-08-26 22:38:39 | [diff] [blame] | 679 | // static |
| 680 | std::vector<content::WebContents*> |
| 681 | ExtensionTabUtil::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] | 45c75e6 | 2012-03-21 19:56:35 | [diff] [blame] | 703 | GURL ExtensionTabUtil::ResolvePossiblyRelativeURL(const std::string& url_string, |
[email protected] | 1c4fbc0 | 2013-11-13 02:52:42 | [diff] [blame] | 704 | const Extension* extension) { |
[email protected] | 45c75e6 | 2012-03-21 19:56:35 | [diff] [blame] | 705 | GURL url = GURL(url_string); |
| 706 | if (!url.is_valid()) |
| 707 | url = extension->GetResourceURL(url_string); |
| 708 | |
| 709 | return url; |
| 710 | } |
| 711 | |
kalman | dfefe1a | 2015-07-13 22:27:54 | [diff] [blame] | 712 | bool ExtensionTabUtil::IsKillURL(const GURL& url) { |
Lei Zhang | 4c28b528 | 2017-08-24 04:26:05 | [diff] [blame] | 713 | 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, |
kalman | dfefe1a | 2015-07-13 22:27:54 | [diff] [blame] | 718 | }; |
| 719 | |
[email protected] | 45c75e6 | 2012-03-21 19:56:35 | [diff] [blame] | 720 | // Check a fixed-up URL, to normalize the scheme and parse hosts correctly. |
| 721 | GURL fixed_url = |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 722 | url_formatter::FixupURL(url.possibly_invalid_spec(), std::string()); |
kalman | dfefe1a | 2015-07-13 22:27:54 | [diff] [blame] | 723 | if (!fixed_url.SchemeIs(content::kChromeUIScheme)) |
| 724 | return false; |
| 725 | |
brettw | adc84688 | 2015-09-25 01:16:22 | [diff] [blame] | 726 | base::StringPiece fixed_host = fixed_url.host_piece(); |
Avi Drissman | 5f0fb8c | 2018-12-25 23:20:49 | [diff] [blame] | 727 | for (size_t i = 0; i < base::size(kill_hosts); ++i) { |
brettw | adc84688 | 2015-09-25 01:16:22 | [diff] [blame] | 728 | if (fixed_host == kill_hosts[i]) |
kalman | dfefe1a | 2015-07-13 22:27:54 | [diff] [blame] | 729 | return true; |
| 730 | } |
| 731 | |
| 732 | return false; |
[email protected] | 45c75e6 | 2012-03-21 19:56:35 | [diff] [blame] | 733 | } |
[email protected] | 73c1a684 | 2012-07-13 17:39:04 | [diff] [blame] | 734 | |
erikchen | 38fa402 | 2018-04-26 20:37:52 | [diff] [blame] | 735 | void ExtensionTabUtil::CreateTab(std::unique_ptr<WebContents> web_contents, |
[email protected] | 73c1a684 | 2012-07-13 17:39:04 | [diff] [blame] | 736 | const std::string& extension_id, |
| 737 | WindowOpenDisposition disposition, |
bokan | 107a47f | 2015-02-03 23:23:39 | [diff] [blame] | 738 | const gfx::Rect& initial_rect, |
[email protected] | 73c1a684 | 2012-07-13 17:39:04 | [diff] [blame] | 739 | bool user_gesture) { |
[email protected] | 73c1a684 | 2012-07-13 17:39:04 | [diff] [blame] | 740 | Profile* profile = |
| 741 | Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
scottmg | 34c5dd88 | 2016-02-03 05:21:54 | [diff] [blame] | 742 | Browser* browser = chrome::FindTabbedBrowser(profile, false); |
[email protected] | 2764cab7 | 2012-07-19 17:02:10 | [diff] [blame] | 743 | const bool browser_created = !browser; |
erg | 7b01d69 | 2017-02-22 21:57:35 | [diff] [blame] | 744 | if (!browser) { |
| 745 | Browser::CreateParams params = Browser::CreateParams(profile, user_gesture); |
Toni Barzic | 0070574 | 2019-02-25 22:09:41 | [diff] [blame] | 746 | browser = Browser::Create(params); |
erg | 7b01d69 | 2017-02-22 21:57:35 | [diff] [blame] | 747 | } |
Toni Barzic | 0070574 | 2019-02-25 22:09:41 | [diff] [blame] | 748 | |
| 749 | if (!browser) |
| 750 | return; |
| 751 | |
erikchen | 38fa402 | 2018-04-26 20:37:52 | [diff] [blame] | 752 | NavigateParams params(browser, std::move(web_contents)); |
[email protected] | 73c1a684 | 2012-07-13 17:39:04 | [diff] [blame] | 753 | |
| 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? |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 759 | if (disposition == WindowOpenDisposition::NEW_POPUP) |
[email protected] | 73c1a684 | 2012-07-13 17:39:04 | [diff] [blame] | 760 | params.extension_app_id = extension_id; |
| 761 | |
[email protected] | 73c1a684 | 2012-07-13 17:39:04 | [diff] [blame] | 762 | params.disposition = disposition; |
bokan | 107a47f | 2015-02-03 23:23:39 | [diff] [blame] | 763 | params.window_bounds = initial_rect; |
cm.sanchi | 2522bc9 | 2017-12-04 08:04:13 | [diff] [blame] | 764 | params.window_action = NavigateParams::SHOW_WINDOW; |
[email protected] | 73c1a684 | 2012-07-13 17:39:04 | [diff] [blame] | 765 | params.user_gesture = user_gesture; |
cm.sanchi | 2522bc9 | 2017-12-04 08:04:13 | [diff] [blame] | 766 | Navigate(¶ms); |
[email protected] | 2764cab7 | 2012-07-19 17:02:10 | [diff] [blame] | 767 | |
cm.sanchi | 2522bc9 | 2017-12-04 08:04:13 | [diff] [blame] | 768 | // Close the browser if Navigate created a new one. |
[email protected] | 2764cab7 | 2012-07-19 17:02:10 | [diff] [blame] | 769 | if (browser_created && (browser != params.browser)) |
| 770 | browser->window()->Close(); |
[email protected] | 73c1a684 | 2012-07-13 17:39:04 | [diff] [blame] | 771 | } |
[email protected] | 44e329a | 2012-07-14 01:13:06 | [diff] [blame] | 772 | |
| 773 | // static |
| 774 | void ExtensionTabUtil::ForEachTab( |
| 775 | const base::Callback<void(WebContents*)>& callback) { |
Avi Drissman | 2f97ce5 | 2018-02-07 23:48:23 | [diff] [blame] | 776 | for (auto* web_contents : AllTabContentses()) |
| 777 | callback.Run(web_contents); |
[email protected] | 44e329a | 2012-07-14 01:13:06 | [diff] [blame] | 778 | } |
[email protected] | e9570fdf | 2012-07-18 20:01:21 | [diff] [blame] | 779 | |
| 780 | // static |
[email protected] | 1c4fbc0 | 2013-11-13 02:52:42 | [diff] [blame] | 781 | WindowController* ExtensionTabUtil::GetWindowControllerOfTab( |
[email protected] | e9570fdf | 2012-07-18 20:01:21 | [diff] [blame] | 782 | const WebContents* web_contents) { |
[email protected] | f7b4b9e | 2012-12-02 07:43:17 | [diff] [blame] | 783 | Browser* browser = chrome::FindBrowserWithWebContents(web_contents); |
Catherine Mullings | 0524e204 | 2017-06-26 23:09:45 | [diff] [blame] | 784 | if (browser != nullptr) |
[email protected] | e9570fdf | 2012-07-18 20:01:21 | [diff] [blame] | 785 | return browser->extension_window_controller(); |
| 786 | |
Catherine Mullings | 0524e204 | 2017-06-26 23:09:45 | [diff] [blame] | 787 | return nullptr; |
[email protected] | e9570fdf | 2012-07-18 20:01:21 | [diff] [blame] | 788 | } |
[email protected] | 69508978 | 2013-04-09 16:03:17 | [diff] [blame] | 789 | |
catmullings | 32ab4eeb | 2017-02-21 18:23:54 | [diff] [blame] | 790 | bool 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 Barzic | 0070574 | 2019-02-25 22:09:41 | [diff] [blame] | 803 | browser = Browser::Create(Browser::CreateParams(profile, true)); |
| 804 | if (!browser) |
| 805 | return false; |
catmullings | 32ab4eeb | 2017-02-21 18:23:54 | [diff] [blame] | 806 | return extensions::ExtensionTabUtil::OpenOptionsPage(extension, browser); |
| 807 | } |
| 808 | |
kalman | ce22c47 | 2015-02-19 02:31:43 | [diff] [blame] | 809 | bool ExtensionTabUtil::OpenOptionsPage(const Extension* extension, |
[email protected] | 69508978 | 2013-04-09 16:03:17 | [diff] [blame] | 810 | Browser* browser) { |
kalman | ce22c47 | 2015-02-19 02:31:43 | [diff] [blame] | 811 | if (!OptionsPageInfo::HasOptionsPage(extension)) |
| 812 | return false; |
[email protected] | 69508978 | 2013-04-09 16:03:17 | [diff] [blame] | 813 | |
catmullings | 32ab4eeb | 2017-02-21 18:23:54 | [diff] [blame] | 814 | // 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. |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 818 | std::unique_ptr<chrome::ScopedTabbedBrowserDisplayer> displayer; |
catmullings | 32ab4eeb | 2017-02-21 18:23:54 | [diff] [blame] | 819 | if (browser->profile()->IsOffTheRecord() && |
| 820 | !IncognitoInfo::IsSplitMode(extension)) { |
[email protected] | 0edcdec | 2013-10-31 06:43:08 | [diff] [blame] | 821 | displayer.reset(new chrome::ScopedTabbedBrowserDisplayer( |
scottmg | 85194900 | 2016-02-09 20:09:44 | [diff] [blame] | 822 | browser->profile()->GetOriginalProfile())); |
[email protected] | 0edcdec | 2013-10-31 06:43:08 | [diff] [blame] | 823 | browser = displayer->browser(); |
[email protected] | 69508978 | 2013-04-09 16:03:17 | [diff] [blame] | 824 | } |
| 825 | |
kalman | 4427c25 | 2015-03-13 01:55:48 | [diff] [blame] | 826 | GURL url_to_navigate; |
rdevlin.cronin | 6ef722e | 2016-03-16 21:42:27 | [diff] [blame] | 827 | bool open_in_tab = OptionsPageInfo::ShouldOpenInTab(extension); |
| 828 | if (open_in_tab) { |
kalman | 4427c25 | 2015-03-13 01:55:48 | [diff] [blame] | 829 | // 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); |
ericzeng | f97b7c2 | 2014-08-26 03:07:30 | [diff] [blame] | 835 | GURL::Replacements replacements; |
| 836 | std::string query = |
| 837 | base::StringPrintf("options=%s", extension->id().c_str()); |
| 838 | replacements.SetQueryStr(query); |
kalman | 4427c25 | 2015-03-13 01:55:48 | [diff] [blame] | 839 | url_to_navigate = url_to_navigate.ReplaceComponents(replacements); |
ericzeng | f97b7c2 | 2014-08-26 03:07:30 | [diff] [blame] | 840 | } |
kalman | ce22c47 | 2015-02-19 02:31:43 | [diff] [blame] | 841 | |
cm.sanchi | 2522bc9 | 2017-12-04 08:04:13 | [diff] [blame] | 842 | NavigateParams params( |
| 843 | GetSingletonTabNavigateParams(browser, url_to_navigate)); |
rdevlin.cronin | 10a6ef01 | 2016-02-19 00:26:03 | [diff] [blame] | 844 | // 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.cronin | 6ef722e | 2016-03-16 21:42:27 | [diff] [blame] | 846 | // However, if the options page opens inside the chrome://extensions page, we |
| 847 | // can override an existing page. |
Kevin Bailey | 55841fa7 | 2018-03-12 22:52:05 | [diff] [blame] | 848 | // Note: ref behavior is to ignore. |
cm.sanchi | 2522bc9 | 2017-12-04 08:04:13 | [diff] [blame] | 849 | params.path_behavior = open_in_tab ? NavigateParams::RESPECT |
| 850 | : NavigateParams::IGNORE_AND_NAVIGATE; |
kalman | 4427c25 | 2015-03-13 01:55:48 | [diff] [blame] | 851 | params.url = url_to_navigate; |
erikchen | 38fa402 | 2018-04-26 20:37:52 | [diff] [blame] | 852 | ShowSingletonTabOverwritingNTP(browser, std::move(params)); |
kalman | ce22c47 | 2015-02-19 02:31:43 | [diff] [blame] | 853 | return true; |
[email protected] | 69508978 | 2013-04-09 16:03:17 | [diff] [blame] | 854 | } |
[email protected] | 1c4fbc0 | 2013-11-13 02:52:42 | [diff] [blame] | 855 | |
lionel.g.landwerlin | d2315f7 | 2015-07-21 14:17:19 | [diff] [blame] | 856 | // static |
| 857 | bool ExtensionTabUtil::BrowserSupportsTabs(Browser* browser) { |
Joel Hockey | 2687ab5 | 2019-08-14 23:59:46 | [diff] [blame] | 858 | return browser && !browser->is_type_devtools(); |
lionel.g.landwerlin | d2315f7 | 2015-07-21 14:17:19 | [diff] [blame] | 859 | } |
| 860 | |
[email protected] | 1c4fbc0 | 2013-11-13 02:52:42 | [diff] [blame] | 861 | } // namespace extensions |