[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" |
Avi Drissman | 5f0fb8c | 2018-12-25 23:20:49 | [diff] [blame] | 12 | #include "base/stl_util.h" |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 13 | #include "base/strings/string_number_conversions.h" |
ericzeng | f97b7c2 | 2014-08-26 03:07:30 | [diff] [blame] | 14 | #include "base/strings/stringprintf.h" |
limasdf | 6dcdc44 | 2016-02-26 04:58:26 | [diff] [blame] | 15 | #include "base/strings/utf_string_conversions.h" |
andersoncss | 12caff1 | 2016-07-14 14:43:41 | [diff] [blame] | 16 | #include "chrome/browser/browser_process.h" |
[email protected] | b19451b | 2012-06-08 17:36:19 | [diff] [blame] | 17 | #include "chrome/browser/extensions/api/tabs/tabs_constants.h" |
Devlin Cronin | ad230bb | 2018-05-30 18:41:46 | [diff] [blame] | 18 | #include "chrome/browser/extensions/browser_extension_window_controller.h" |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 19 | #include "chrome/browser/extensions/chrome_extension_function.h" |
lfg | 18533307 | 2014-09-09 20:16:11 | [diff] [blame] | 20 | #include "chrome/browser/extensions/chrome_extension_function_details.h" |
[email protected] | e9570fdf | 2012-07-18 20:01:21 | [diff] [blame] | 21 | #include "chrome/browser/extensions/tab_helper.h" |
Ivan Sandrk | e0e9245 | 2019-03-21 09:32:48 | [diff] [blame] | 22 | #include "chrome/browser/platform_util.h" |
[email protected] | b56e2e3 | 2012-05-11 21:18:04 | [diff] [blame] | 23 | #include "chrome/browser/profiles/profile.h" |
Francois Doray | 77be021 | 2018-04-05 14:26:26 | [diff] [blame] | 24 | #include "chrome/browser/resource_coordinator/tab_lifecycle_unit_external.h" |
[email protected] | e3f90c60 | 2014-08-18 12:41:59 | [diff] [blame] | 25 | #include "chrome/browser/sessions/session_tab_helper.h" |
[email protected] | b56e2e3 | 2012-05-11 21:18:04 | [diff] [blame] | 26 | #include "chrome/browser/ui/browser.h" |
[email protected] | 73c1a684 | 2012-07-13 17:39:04 | [diff] [blame] | 27 | #include "chrome/browser/ui/browser_finder.h" |
thestig | e8082124 | 2015-09-30 23:46:08 | [diff] [blame] | 28 | #include "chrome/browser/ui/browser_navigator_params.h" |
[email protected] | b56e2e3 | 2012-05-11 21:18:04 | [diff] [blame] | 29 | #include "chrome/browser/ui/browser_window.h" |
Chris Hamilton | 2013965 | 2018-06-01 15:55:01 | [diff] [blame] | 30 | #include "chrome/browser/ui/recently_audible_helper.h" |
[email protected] | 0edcdec | 2013-10-31 06:43:08 | [diff] [blame] | 31 | #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" |
ericzeng | f97b7c2 | 2014-08-26 03:07:30 | [diff] [blame] | 32 | #include "chrome/browser/ui/singleton_tabs.h" |
[email protected] | 44e329a | 2012-07-14 01:13:06 | [diff] [blame] | 33 | #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" |
[email protected] | b56e2e3 | 2012-05-11 21:18:04 | [diff] [blame] | 34 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
jared.sohn | 09a3ccc | 2015-07-09 03:47:30 | [diff] [blame] | 35 | #include "chrome/browser/ui/tabs/tab_utils.h" |
[email protected] | 760f603 | 2014-06-30 23:18:19 | [diff] [blame] | 36 | #include "chrome/common/extensions/api/tabs.h" |
[email protected] | 45c75e6 | 2012-03-21 19:56:35 | [diff] [blame] | 37 | #include "chrome/common/url_constants.h" |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 38 | #include "components/url_formatter/url_fixer.h" |
[email protected] | ad23a09 | 2011-12-28 07:02:04 | [diff] [blame] | 39 | #include "content/public/browser/favicon_status.h" |
| 40 | #include "content/public/browser/navigation_entry.h" |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 41 | #include "content/public/browser/web_contents.h" |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 42 | #include "extensions/common/constants.h" |
| 43 | #include "extensions/common/error_utils.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 44 | #include "extensions/common/extension.h" |
ericzeng | f97b7c2 | 2014-08-26 03:07:30 | [diff] [blame] | 45 | #include "extensions/common/feature_switch.h" |
[email protected] | 0c3c973 | 2013-09-16 08:53:41 | [diff] [blame] | 46 | #include "extensions/common/manifest_constants.h" |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 47 | #include "extensions/common/manifest_handlers/incognito_info.h" |
ericzeng | 09a7e00 | 2014-09-09 21:43:47 | [diff] [blame] | 48 | #include "extensions/common/manifest_handlers/options_page_info.h" |
[email protected] | 793964a | 2013-10-08 00:47:19 | [diff] [blame] | 49 | #include "extensions/common/permissions/api_permission.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 50 | #include "extensions/common/permissions/permissions_data.h" |
[email protected] | a6483d2 | 2013-07-03 22:11:00 | [diff] [blame] | 51 | #include "url/gurl.h" |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 52 | |
[email protected] | 10f417c5 | 2011-12-28 21:04:23 | [diff] [blame] | 53 | using content::NavigationEntry; |
[email protected] | 26b5e32 | 2011-12-23 01:36:47 | [diff] [blame] | 54 | using content::WebContents; |
[email protected] | 1c4fbc0 | 2013-11-13 02:52:42 | [diff] [blame] | 55 | |
| 56 | namespace extensions { |
[email protected] | 26b5e32 | 2011-12-23 01:36:47 | [diff] [blame] | 57 | |
[email protected] | f1c102b | 2013-02-15 07:44:12 | [diff] [blame] | 58 | namespace { |
| 59 | |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 60 | // |error_message| can optionally be passed in and will be set with an |
| 61 | // appropriate message if the window cannot be found by id. |
| 62 | Browser* GetBrowserInProfileWithId(Profile* profile, |
| 63 | const int window_id, |
Ramin Halavati | acdd17f1 | 2018-06-06 07:05:12 | [diff] [blame] | 64 | bool match_incognito_profile, |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 65 | std::string* error_message) { |
| 66 | Profile* incognito_profile = |
Ramin Halavati | acdd17f1 | 2018-06-06 07:05:12 | [diff] [blame] | 67 | match_incognito_profile && profile->HasOffTheRecordProfile() |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 68 | ? profile->GetOffTheRecordProfile() |
Catherine Mullings | 0524e204 | 2017-06-26 23:09:45 | [diff] [blame] | 69 | : nullptr; |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 70 | for (auto* browser : *BrowserList::GetInstance()) { |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 71 | if ((browser->profile() == profile || |
| 72 | browser->profile() == incognito_profile) && |
| 73 | ExtensionTabUtil::GetWindowId(browser) == window_id && |
| 74 | browser->window()) { |
| 75 | return browser; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | if (error_message) |
| 80 | *error_message = ErrorUtils::FormatErrorMessage( |
Raul Tambre | fff51b75 | 2019-02-04 13:09:47 | [diff] [blame] | 81 | tabs_constants::kWindowNotFoundError, base::NumberToString(window_id)); |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 82 | |
Catherine Mullings | 0524e204 | 2017-06-26 23:09:45 | [diff] [blame] | 83 | return nullptr; |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 84 | } |
| 85 | |
erg | 7b01d69 | 2017-02-22 21:57:35 | [diff] [blame] | 86 | Browser* CreateBrowser(Profile* profile, |
| 87 | int window_id, |
| 88 | bool user_gesture, |
| 89 | std::string* error) { |
Joel Hockey | 2687ab5 | 2019-08-14 23:59:46 | [diff] [blame] | 90 | Browser::CreateParams params(Browser::TYPE_NORMAL, profile, user_gesture); |
Toni Barzic | 0070574 | 2019-02-25 22:09:41 | [diff] [blame] | 91 | Browser* browser = Browser::Create(params); |
| 92 | if (!browser) { |
| 93 | *error = tabs_constants::kBrowserWindowNotAllowed; |
| 94 | return nullptr; |
| 95 | } |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 96 | browser->window()->Show(); |
| 97 | return browser; |
| 98 | } |
| 99 | |
lionel.g.landwerlin | 85ad204 | 2015-07-23 11:26:11 | [diff] [blame] | 100 | // Use this function for reporting a tab id to an extension. It will |
| 101 | // take care of setting the id to TAB_ID_NONE if necessary (for |
| 102 | // example with devtools). |
| 103 | int GetTabIdForExtensions(const WebContents* web_contents) { |
| 104 | Browser* browser = chrome::FindBrowserWithWebContents(web_contents); |
| 105 | if (browser && !ExtensionTabUtil::BrowserSupportsTabs(browser)) |
| 106 | return -1; |
Mikel Astiz | 2f127c42 | 2018-04-05 19:10:27 | [diff] [blame] | 107 | return SessionTabHelper::IdForTab(web_contents).id(); |
lionel.g.landwerlin | 85ad204 | 2015-07-23 11:26:11 | [diff] [blame] | 108 | } |
| 109 | |
Daniel Bratell | be89e344 | 2018-09-11 15:44:57 | [diff] [blame] | 110 | std::unique_ptr<ExtensionTabUtil::Delegate>& |
| 111 | GetExtensionTabUtilDelegateWrapper() { |
Ivan Sandrk | e2b20c6 | 2018-09-10 16:23:53 | [diff] [blame] | 112 | static base::NoDestructor<std::unique_ptr<ExtensionTabUtil::Delegate>> |
| 113 | delegate_wrapper; |
| 114 | return *delegate_wrapper; |
| 115 | } |
| 116 | |
Daniel Bratell | be89e344 | 2018-09-11 15:44:57 | [diff] [blame] | 117 | ExtensionTabUtil::Delegate* GetExtensionTabUtilDelegate() { |
| 118 | return GetExtensionTabUtilDelegateWrapper().get(); |
Ivan Sandrk | e2b20c6 | 2018-09-10 16:23:53 | [diff] [blame] | 119 | } |
isandrk | e095113 | 2017-04-24 17:53:31 | [diff] [blame] | 120 | |
John Lee | 76e40a3 | 2019-08-20 17:52:40 | [diff] [blame] | 121 | ExtensionTabUtil::ScrubTabBehavior GetScrubTabBehaviorImpl( |
| 122 | const Extension* extension, |
John Lee | bfbb37cb | 2019-08-26 22:23:18 | [diff] [blame] | 123 | Feature::Context context, |
John Lee | 76e40a3 | 2019-08-20 17:52:40 | [diff] [blame] | 124 | const GURL& url, |
| 125 | int tab_id) { |
John Lee | bfbb37cb | 2019-08-26 22:23:18 | [diff] [blame] | 126 | if (context == Feature::Context::WEBUI_CONTEXT) { |
| 127 | return ExtensionTabUtil::kDontScrubTab; |
| 128 | } |
| 129 | |
John Lee | 76e40a3 | 2019-08-20 17:52:40 | [diff] [blame] | 130 | bool has_permission = false; |
| 131 | |
| 132 | if (extension) { |
| 133 | bool api_permission = false; |
| 134 | if (tab_id == api::tabs::TAB_ID_NONE) { |
| 135 | api_permission = |
| 136 | extension->permissions_data()->HasAPIPermission(APIPermission::kTab); |
| 137 | } else { |
| 138 | api_permission = extension->permissions_data()->HasAPIPermissionForTab( |
| 139 | tab_id, APIPermission::kTab); |
| 140 | } |
| 141 | |
| 142 | bool host_permission = extension->permissions_data() |
| 143 | ->active_permissions() |
| 144 | .HasExplicitAccessToOrigin(url); |
| 145 | has_permission = api_permission || host_permission; |
| 146 | } |
| 147 | |
| 148 | if (!has_permission) { |
| 149 | return ExtensionTabUtil::kScrubTabFully; |
| 150 | } |
| 151 | |
| 152 | if (GetExtensionTabUtilDelegate()) { |
| 153 | return GetExtensionTabUtilDelegate()->GetScrubTabBehavior(extension); |
| 154 | } |
| 155 | |
| 156 | return ExtensionTabUtil::kDontScrubTab; |
| 157 | } |
| 158 | |
[email protected] | f1c102b | 2013-02-15 07:44:12 | [diff] [blame] | 159 | } // namespace |
| 160 | |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 161 | ExtensionTabUtil::OpenTabParams::OpenTabParams() |
| 162 | : create_browser_if_needed(false) { |
| 163 | } |
| 164 | |
| 165 | ExtensionTabUtil::OpenTabParams::~OpenTabParams() { |
| 166 | } |
| 167 | |
Catherine Mullings | 0524e204 | 2017-06-26 23:09:45 | [diff] [blame] | 168 | // 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] | 169 | // error occurs. |
Clark DuVall | fd4db3d | 2019-07-30 19:10:43 | [diff] [blame] | 170 | base::DictionaryValue* ExtensionTabUtil::OpenTab(ExtensionFunction* function, |
| 171 | const OpenTabParams& params, |
| 172 | bool user_gesture, |
| 173 | std::string* error) { |
rdevlin.cronin | 3223122d | 2016-09-07 21:27:26 | [diff] [blame] | 174 | ChromeExtensionFunctionDetails chrome_details(function); |
| 175 | Profile* profile = chrome_details.GetProfile(); |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 176 | // windowId defaults to "current" window. |
| 177 | int window_id = extension_misc::kCurrentWindowId; |
| 178 | if (params.window_id.get()) |
| 179 | window_id = *params.window_id; |
| 180 | |
rdevlin.cronin | 3223122d | 2016-09-07 21:27:26 | [diff] [blame] | 181 | Browser* browser = GetBrowserFromWindowID(chrome_details, window_id, error); |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 182 | if (!browser) { |
| 183 | if (!params.create_browser_if_needed) { |
Catherine Mullings | 0524e204 | 2017-06-26 23:09:45 | [diff] [blame] | 184 | return nullptr; |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 185 | } |
erg | 7b01d69 | 2017-02-22 21:57:35 | [diff] [blame] | 186 | browser = CreateBrowser(profile, window_id, user_gesture, error); |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 187 | if (!browser) |
Catherine Mullings | 0524e204 | 2017-06-26 23:09:45 | [diff] [blame] | 188 | return nullptr; |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 189 | } |
| 190 | |
Joel Hockey | 2687ab5 | 2019-08-14 23:59:46 | [diff] [blame] | 191 | // Ensure the selected browser is normal. |
| 192 | if (!browser->is_type_normal() && browser->IsAttemptingToCloseBrowser()) |
Ramin Halavati | acdd17f1 | 2018-06-06 07:05:12 | [diff] [blame] | 193 | browser = chrome::FindTabbedBrowser( |
| 194 | profile, function->include_incognito_information()); |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 195 | if (!browser || !browser->window()) { |
deepak.m1 | 21895c01 | 2015-05-11 08:11:19 | [diff] [blame] | 196 | if (error) |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 197 | *error = tabs_constants::kNoCurrentWindowError; |
Catherine Mullings | 0524e204 | 2017-06-26 23:09:45 | [diff] [blame] | 198 | return nullptr; |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | // TODO(jstritar): Add a constant, chrome.tabs.TAB_ID_ACTIVE, that |
| 202 | // represents the active tab. |
Catherine Mullings | 0524e204 | 2017-06-26 23:09:45 | [diff] [blame] | 203 | WebContents* opener = nullptr; |
| 204 | Browser* opener_browser = nullptr; |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 205 | if (params.opener_tab_id.get()) { |
| 206 | int opener_id = *params.opener_tab_id; |
| 207 | |
Catherine Mullings | 0524e204 | 2017-06-26 23:09:45 | [diff] [blame] | 208 | if (!ExtensionTabUtil::GetTabById( |
Ramin Halavati | acdd17f1 | 2018-06-06 07:05:12 | [diff] [blame] | 209 | opener_id, profile, function->include_incognito_information(), |
| 210 | &opener_browser, nullptr, &opener, nullptr)) { |
deepak.m1 | 21895c01 | 2015-05-11 08:11:19 | [diff] [blame] | 211 | if (error) { |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 212 | *error = ErrorUtils::FormatErrorMessage( |
Raul Tambre | fff51b75 | 2019-02-04 13:09:47 | [diff] [blame] | 213 | tabs_constants::kTabNotFoundError, base::NumberToString(opener_id)); |
deepak.m1 | 21895c01 | 2015-05-11 08:11:19 | [diff] [blame] | 214 | } |
Catherine Mullings | 0524e204 | 2017-06-26 23:09:45 | [diff] [blame] | 215 | return nullptr; |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
| 219 | // TODO(rafaelw): handle setting remaining tab properties: |
| 220 | // -title |
| 221 | // -favIconUrl |
| 222 | |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 223 | GURL url; |
| 224 | if (params.url.get()) { |
bokan | 107a47f | 2015-02-03 23:23:39 | [diff] [blame] | 225 | std::string url_string = *params.url; |
[email protected] | eba8f7d | 2014-07-28 22:09:23 | [diff] [blame] | 226 | url = ExtensionTabUtil::ResolvePossiblyRelativeURL(url_string, |
| 227 | function->extension()); |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 228 | if (!url.is_valid()) { |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 229 | *error = ErrorUtils::FormatErrorMessage(tabs_constants::kInvalidUrlError, |
| 230 | url_string); |
Catherine Mullings | 0524e204 | 2017-06-26 23:09:45 | [diff] [blame] | 231 | return nullptr; |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 232 | } |
[email protected] | f5ec3c92 | 2014-05-22 15:04:03 | [diff] [blame] | 233 | } else { |
| 234 | url = GURL(chrome::kChromeUINewTabURL); |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | // Don't let extensions crash the browser or renderers. |
kalman | dfefe1a | 2015-07-13 22:27:54 | [diff] [blame] | 238 | if (ExtensionTabUtil::IsKillURL(url)) { |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 239 | *error = tabs_constants::kNoCrashBrowserError; |
Catherine Mullings | 0524e204 | 2017-06-26 23:09:45 | [diff] [blame] | 240 | return nullptr; |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | // Default to foreground for the new tab. The presence of 'active' property |
| 244 | // will override this default. |
| 245 | bool active = true; |
| 246 | if (params.active.get()) |
| 247 | active = *params.active; |
| 248 | |
| 249 | // Default to not pinning the tab. Setting the 'pinned' property to true |
| 250 | // will override this default. |
| 251 | bool pinned = false; |
| 252 | if (params.pinned.get()) |
| 253 | pinned = *params.pinned; |
| 254 | |
| 255 | // We can't load extension URLs into incognito windows unless the extension |
| 256 | // uses split mode. Special case to fall back to a tabbed window. |
| 257 | if (url.SchemeIs(kExtensionScheme) && |
calamity | 862d9032 | 2017-05-22 07:14:28 | [diff] [blame] | 258 | (!function->extension() || |
| 259 | !IncognitoInfo::IsSplitMode(function->extension())) && |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 260 | browser->profile()->IsOffTheRecord()) { |
| 261 | Profile* profile = browser->profile()->GetOriginalProfile(); |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 262 | |
scottmg | 34c5dd88 | 2016-02-03 05:21:54 | [diff] [blame] | 263 | browser = chrome::FindTabbedBrowser(profile, false); |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 264 | if (!browser) { |
erg | 7b01d69 | 2017-02-22 21:57:35 | [diff] [blame] | 265 | Browser::CreateParams params = |
Joel Hockey | 2687ab5 | 2019-08-14 23:59:46 | [diff] [blame] | 266 | Browser::CreateParams(Browser::TYPE_NORMAL, profile, user_gesture); |
Toni Barzic | 0070574 | 2019-02-25 22:09:41 | [diff] [blame] | 267 | browser = Browser::Create(params); |
| 268 | if (!browser) { |
| 269 | *error = tabs_constants::kBrowserWindowNotAllowed; |
| 270 | return nullptr; |
| 271 | } |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 272 | browser->window()->Show(); |
| 273 | } |
| 274 | } |
| 275 | |
Catherine Mullings | 0524e204 | 2017-06-26 23:09:45 | [diff] [blame] | 276 | if (opener_browser && browser != opener_browser) { |
| 277 | if (error) { |
| 278 | *error = "Tab opener must be in the same window as the updated tab."; |
| 279 | } |
| 280 | return nullptr; |
| 281 | } |
| 282 | |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 283 | // If index is specified, honor the value, but keep it bound to |
| 284 | // -1 <= index <= tab_strip->count() where -1 invokes the default behavior. |
| 285 | int index = -1; |
| 286 | if (params.index.get()) |
| 287 | index = *params.index; |
| 288 | |
| 289 | TabStripModel* tab_strip = browser->tab_strip_model(); |
| 290 | |
| 291 | index = std::min(std::max(index, -1), tab_strip->count()); |
| 292 | |
| 293 | int add_types = active ? TabStripModel::ADD_ACTIVE : TabStripModel::ADD_NONE; |
| 294 | add_types |= TabStripModel::ADD_FORCE_INDEX; |
| 295 | if (pinned) |
| 296 | add_types |= TabStripModel::ADD_PINNED; |
cm.sanchi | 2522bc9 | 2017-12-04 08:04:13 | [diff] [blame] | 297 | NavigateParams navigate_params(browser, url, ui::PAGE_TRANSITION_LINK); |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 298 | navigate_params.disposition = active |
| 299 | ? WindowOpenDisposition::NEW_FOREGROUND_TAB |
| 300 | : WindowOpenDisposition::NEW_BACKGROUND_TAB; |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 301 | navigate_params.tabstrip_index = index; |
| 302 | navigate_params.tabstrip_add_types = add_types; |
cm.sanchi | 2522bc9 | 2017-12-04 08:04:13 | [diff] [blame] | 303 | Navigate(&navigate_params); |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 304 | |
| 305 | // The tab may have been created in a different window, so make sure we look |
| 306 | // at the right tab strip. |
| 307 | tab_strip = navigate_params.browser->tab_strip_model(); |
erikchen | 38fa402 | 2018-04-26 20:37:52 | [diff] [blame] | 308 | int new_index = tab_strip->GetIndexOfWebContents( |
| 309 | navigate_params.navigated_or_inserted_contents); |
rdevlin.cronin | 5b3827b6 | 2017-03-08 02:38:09 | [diff] [blame] | 310 | if (opener) { |
| 311 | // Only set the opener if the opener tab is in the same tab strip as the |
| 312 | // new tab. |
rdevlin.cronin | 5b3827b6 | 2017-03-08 02:38:09 | [diff] [blame] | 313 | if (tab_strip->GetIndexOfWebContents(opener) != TabStripModel::kNoTab) |
| 314 | tab_strip->SetOpenerOfWebContentsAt(new_index, opener); |
| 315 | } |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 316 | |
| 317 | if (active) |
erikchen | 38fa402 | 2018-04-26 20:37:52 | [diff] [blame] | 318 | navigate_params.navigated_or_inserted_contents->SetInitialFocus(); |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 319 | |
John Lee | 76e40a3 | 2019-08-20 17:52:40 | [diff] [blame] | 320 | ExtensionTabUtil::ScrubTabBehavior scrub_tab_behavior = |
| 321 | ExtensionTabUtil::GetScrubTabBehavior( |
John Lee | bfbb37cb | 2019-08-26 22:23:18 | [diff] [blame] | 322 | function->extension(), function->source_context_type(), |
John Lee | 76e40a3 | 2019-08-20 17:52:40 | [diff] [blame] | 323 | navigate_params.navigated_or_inserted_contents); |
| 324 | |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 325 | // Return data about the newly created tab. |
erikchen | 38fa402 | 2018-04-26 20:37:52 | [diff] [blame] | 326 | return ExtensionTabUtil::CreateTabObject( |
John Lee | 76e40a3 | 2019-08-20 17:52:40 | [diff] [blame] | 327 | navigate_params.navigated_or_inserted_contents, scrub_tab_behavior, |
erikchen | 38fa402 | 2018-04-26 20:37:52 | [diff] [blame] | 328 | function->extension(), tab_strip, new_index) |
limasdf | 6dcdc44 | 2016-02-26 04:58:26 | [diff] [blame] | 329 | ->ToValue() |
| 330 | .release(); |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | Browser* ExtensionTabUtil::GetBrowserFromWindowID( |
lfg | 18533307 | 2014-09-09 20:16:11 | [diff] [blame] | 334 | const ChromeExtensionFunctionDetails& details, |
| 335 | int window_id, |
| 336 | std::string* error) { |
| 337 | if (window_id == extension_misc::kCurrentWindowId) { |
| 338 | Browser* result = details.GetCurrentBrowser(); |
| 339 | if (!result || !result->window()) { |
| 340 | if (error) |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 341 | *error = tabs_constants::kNoCurrentWindowError; |
Catherine Mullings | 0524e204 | 2017-06-26 23:09:45 | [diff] [blame] | 342 | return nullptr; |
lfg | 18533307 | 2014-09-09 20:16:11 | [diff] [blame] | 343 | } |
| 344 | return result; |
| 345 | } else { |
Ramin Halavati | acdd17f1 | 2018-06-06 07:05:12 | [diff] [blame] | 346 | return GetBrowserInProfileWithId( |
| 347 | details.GetProfile(), window_id, |
| 348 | details.function()->include_incognito_information(), error); |
lfg | 18533307 | 2014-09-09 20:16:11 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 352 | int ExtensionTabUtil::GetWindowId(const Browser* browser) { |
| 353 | return browser->session_id().id(); |
| 354 | } |
| 355 | |
[email protected] | 8c3495c | 2011-09-28 03:32:30 | [diff] [blame] | 356 | int ExtensionTabUtil::GetWindowIdOfTabStripModel( |
| 357 | const TabStripModel* tab_strip_model) { |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 358 | for (auto* browser : *BrowserList::GetInstance()) { |
| 359 | if (browser->tab_strip_model() == tab_strip_model) |
| 360 | return GetWindowId(browser); |
[email protected] | 8c3495c | 2011-09-28 03:32:30 | [diff] [blame] | 361 | } |
| 362 | return -1; |
| 363 | } |
| 364 | |
[email protected] | d2bd5fde | 2014-05-29 02:24:31 | [diff] [blame] | 365 | int ExtensionTabUtil::GetTabId(const WebContents* web_contents) { |
Mikel Astiz | 2f127c42 | 2018-04-05 19:10:27 | [diff] [blame] | 366 | return SessionTabHelper::IdForTab(web_contents).id(); |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | std::string ExtensionTabUtil::GetTabStatusText(bool is_loading) { |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 370 | return is_loading ? tabs_constants::kStatusValueLoading |
| 371 | : tabs_constants::kStatusValueComplete; |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 372 | } |
| 373 | |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 374 | int ExtensionTabUtil::GetWindowIdOfTab(const WebContents* web_contents) { |
Mikel Astiz | 2f127c42 | 2018-04-05 19:10:27 | [diff] [blame] | 375 | return SessionTabHelper::IdForWindowContainingTab(web_contents).id(); |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 376 | } |
| 377 | |
limasdf | 6dcdc44 | 2016-02-26 04:58:26 | [diff] [blame] | 378 | // static |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 379 | std::string ExtensionTabUtil::GetBrowserWindowTypeText(const Browser& browser) { |
Joel Hockey | 2687ab5 | 2019-08-14 23:59:46 | [diff] [blame] | 380 | if (browser.is_type_devtools()) |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 381 | return tabs_constants::kWindowTypeValueDevTools; |
Joel Hockey | 2687ab5 | 2019-08-14 23:59:46 | [diff] [blame] | 382 | // TODO(crbug.com/990158): We return 'popup' for both popup and app since |
| 383 | // chrome.tabs.create({type: 'popup'}) uses |
| 384 | // Browser::CreateParams::CreateForApp. |
| 385 | if (browser.is_type_popup() || browser.is_type_app()) |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 386 | return tabs_constants::kWindowTypeValuePopup; |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 387 | return tabs_constants::kWindowTypeValueNormal; |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | // static |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 391 | std::unique_ptr<api::tabs::Tab> ExtensionTabUtil::CreateTabObject( |
[email protected] | fc2b46b | 2014-05-03 16:33:45 | [diff] [blame] | 392 | WebContents* contents, |
Devlin Cronin | 1980f44d | 2018-02-08 23:14:01 | [diff] [blame] | 393 | ScrubTabBehavior scrub_tab_behavior, |
| 394 | const Extension* extension, |
[email protected] | f34706be | 2012-09-04 07:32:09 | [diff] [blame] | 395 | TabStripModel* tab_strip, |
[email protected] | 304fd15 | 2013-01-12 16:54:44 | [diff] [blame] | 396 | int tab_index) { |
[email protected] | 0c9f326 | 2012-09-17 05:59:06 | [diff] [blame] | 397 | if (!tab_strip) |
| 398 | ExtensionTabUtil::GetTabStripModel(contents, &tab_strip, &tab_index); |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 399 | bool is_loading = contents->IsLoading(); |
Jinho Bang | b5216cec | 2018-01-17 19:43:11 | [diff] [blame] | 400 | auto tab_object = std::make_unique<api::tabs::Tab>(); |
| 401 | tab_object->id = std::make_unique<int>(GetTabIdForExtensions(contents)); |
limasdf | 6dcdc44 | 2016-02-26 04:58:26 | [diff] [blame] | 402 | tab_object->index = tab_index; |
| 403 | tab_object->window_id = GetWindowIdOfTab(contents); |
lazyboy | 8b1df30 | 2017-04-26 23:37:41 | [diff] [blame] | 404 | tab_object->status = |
Jinho Bang | b5216cec | 2018-01-17 19:43:11 | [diff] [blame] | 405 | std::make_unique<std::string>(GetTabStatusText(is_loading)); |
limasdf | 6dcdc44 | 2016-02-26 04:58:26 | [diff] [blame] | 406 | tab_object->active = tab_strip && tab_index == tab_strip->active_index(); |
| 407 | tab_object->selected = tab_strip && tab_index == tab_strip->active_index(); |
| 408 | tab_object->highlighted = tab_strip && tab_strip->IsTabSelected(tab_index); |
| 409 | tab_object->pinned = tab_strip && tab_strip->IsTabPinned(tab_index); |
Chris Hamilton | 2013965 | 2018-06-01 15:55:01 | [diff] [blame] | 410 | auto* audible_helper = RecentlyAudibleHelper::FromWebContents(contents); |
Chris Hamilton | 737e22b4 | 2018-06-04 18:33:14 | [diff] [blame] | 411 | bool audible = false; |
| 412 | if (audible_helper) { |
| 413 | // WebContents in a tab strip have RecentlyAudible helpers. They endow the |
| 414 | // tab with a notion of audibility that has a timeout for quiet periods. Use |
| 415 | // that if available. |
| 416 | audible = audible_helper->WasRecentlyAudible(); |
| 417 | } else { |
| 418 | // Otherwise use the instantaneous notion of audibility. |
| 419 | audible = contents->IsCurrentlyAudible(); |
| 420 | } |
| 421 | tab_object->audible = std::make_unique<bool>(audible); |
Francois Doray | 77be021 | 2018-04-05 14:26:26 | [diff] [blame] | 422 | auto* tab_lifeycle_unit_external = |
| 423 | resource_coordinator::TabLifecycleUnitExternal::FromWebContents(contents); |
andersoncss | fd947a4 | 2016-08-12 14:10:42 | [diff] [blame] | 424 | tab_object->discarded = |
Francois Doray | 77be021 | 2018-04-05 14:26:26 | [diff] [blame] | 425 | tab_lifeycle_unit_external && tab_lifeycle_unit_external->IsDiscarded(); |
andersoncss | fd947a4 | 2016-08-12 14:10:42 | [diff] [blame] | 426 | tab_object->auto_discardable = |
Francois Doray | 77be021 | 2018-04-05 14:26:26 | [diff] [blame] | 427 | !tab_lifeycle_unit_external || |
| 428 | tab_lifeycle_unit_external->IsAutoDiscardable(); |
limasdf | 6dcdc44 | 2016-02-26 04:58:26 | [diff] [blame] | 429 | tab_object->muted_info = CreateMutedInfo(contents); |
| 430 | tab_object->incognito = contents->GetBrowserContext()->IsOffTheRecord(); |
lazyboy | 8b1df30 | 2017-04-26 23:37:41 | [diff] [blame] | 431 | gfx::Size contents_size = contents->GetContainerBounds().size(); |
Jinho Bang | b5216cec | 2018-01-17 19:43:11 | [diff] [blame] | 432 | tab_object->width = std::make_unique<int>(contents_size.width()); |
| 433 | tab_object->height = std::make_unique<int>(contents_size.height()); |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 434 | |
Jinho Bang | b5216cec | 2018-01-17 19:43:11 | [diff] [blame] | 435 | tab_object->url = std::make_unique<std::string>(contents->GetURL().spec()); |
lazyboy | 8b1df30 | 2017-04-26 23:37:41 | [diff] [blame] | 436 | tab_object->title = |
Jinho Bang | b5216cec | 2018-01-17 19:43:11 | [diff] [blame] | 437 | std::make_unique<std::string>(base::UTF16ToUTF8(contents->GetTitle())); |
limasdf | 1f07189 | 2016-02-09 10:18:00 | [diff] [blame] | 438 | NavigationEntry* entry = contents->GetController().GetVisibleEntry(); |
lazyboy | 8b1df30 | 2017-04-26 23:37:41 | [diff] [blame] | 439 | if (entry && entry->GetFavicon().valid) { |
| 440 | tab_object->fav_icon_url = |
Jinho Bang | b5216cec | 2018-01-17 19:43:11 | [diff] [blame] | 441 | std::make_unique<std::string>(entry->GetFavicon().url.spec()); |
lazyboy | 8b1df30 | 2017-04-26 23:37:41 | [diff] [blame] | 442 | } |
[email protected] | 327640a | 2012-01-24 21:57:59 | [diff] [blame] | 443 | if (tab_strip) { |
[email protected] | 6707540 | 2012-10-26 08:26:25 | [diff] [blame] | 444 | WebContents* opener = tab_strip->GetOpenerOfWebContentsAt(tab_index); |
lazyboy | 8b1df30 | 2017-04-26 23:37:41 | [diff] [blame] | 445 | if (opener) { |
| 446 | tab_object->opener_tab_id = |
Jinho Bang | b5216cec | 2018-01-17 19:43:11 | [diff] [blame] | 447 | std::make_unique<int>(GetTabIdForExtensions(opener)); |
lazyboy | 8b1df30 | 2017-04-26 23:37:41 | [diff] [blame] | 448 | } |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 449 | } |
| 450 | |
John Lee | 76e40a3 | 2019-08-20 17:52:40 | [diff] [blame] | 451 | if (scrub_tab_behavior != kDontScrubTab) |
| 452 | ScrubTabForExtension(extension, contents, tab_object.get(), |
| 453 | scrub_tab_behavior); |
limasdf | 6dcdc44 | 2016-02-26 04:58:26 | [diff] [blame] | 454 | return tab_object; |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 455 | } |
| 456 | |
Devlin Cronin | 1980f44d | 2018-02-08 23:14:01 | [diff] [blame] | 457 | std::unique_ptr<base::ListValue> ExtensionTabUtil::CreateTabList( |
| 458 | const Browser* browser, |
John Lee | bfbb37cb | 2019-08-26 22:23:18 | [diff] [blame] | 459 | const Extension* extension, |
| 460 | Feature::Context context) { |
Devlin Cronin | 1980f44d | 2018-02-08 23:14:01 | [diff] [blame] | 461 | std::unique_ptr<base::ListValue> tab_list(new base::ListValue()); |
| 462 | TabStripModel* tab_strip = browser->tab_strip_model(); |
| 463 | for (int i = 0; i < tab_strip->count(); ++i) { |
John Lee | 76e40a3 | 2019-08-20 17:52:40 | [diff] [blame] | 464 | WebContents* web_contents = tab_strip->GetWebContentsAt(i); |
| 465 | ExtensionTabUtil::ScrubTabBehavior scrub_tab_behavior = |
John Lee | bfbb37cb | 2019-08-26 22:23:18 | [diff] [blame] | 466 | ExtensionTabUtil::GetScrubTabBehavior(extension, context, web_contents); |
John Lee | 76e40a3 | 2019-08-20 17:52:40 | [diff] [blame] | 467 | tab_list->Append(CreateTabObject(web_contents, scrub_tab_behavior, |
Devlin Cronin | 1980f44d | 2018-02-08 23:14:01 | [diff] [blame] | 468 | extension, tab_strip, i) |
| 469 | ->ToValue()); |
| 470 | } |
| 471 | |
| 472 | return tab_list; |
| 473 | } |
| 474 | |
miu | 8b605f2 | 2015-08-15 02:56:56 | [diff] [blame] | 475 | // static |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 476 | std::unique_ptr<base::DictionaryValue> |
| 477 | ExtensionTabUtil::CreateWindowValueForExtension( |
| 478 | const Browser& browser, |
| 479 | const Extension* extension, |
John Lee | bfbb37cb | 2019-08-26 22:23:18 | [diff] [blame] | 480 | PopulateTabBehavior populate_tab_behavior, |
| 481 | Feature::Context context) { |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 482 | auto result = std::make_unique<base::DictionaryValue>(); |
| 483 | |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 484 | result->SetInteger(tabs_constants::kIdKey, browser.session_id().id()); |
| 485 | result->SetString(tabs_constants::kWindowTypeKey, |
| 486 | GetBrowserWindowTypeText(browser)); |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 487 | ui::BaseWindow* window = browser.window(); |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 488 | result->SetBoolean(tabs_constants::kFocusedKey, window->IsActive()); |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 489 | const Profile* profile = browser.profile(); |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 490 | result->SetBoolean(tabs_constants::kIncognitoKey, profile->IsOffTheRecord()); |
Avi Drissman | 9e653328 | 2019-07-17 17:25:12 | [diff] [blame] | 491 | result->SetBoolean( |
| 492 | tabs_constants::kAlwaysOnTopKey, |
| 493 | window->GetZOrderLevel() == ui::ZOrderLevel::kFloatingWindow); |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 494 | |
| 495 | std::string window_state; |
| 496 | if (window->IsMinimized()) { |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 497 | window_state = tabs_constants::kShowStateValueMinimized; |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 498 | } else if (window->IsFullscreen()) { |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 499 | window_state = tabs_constants::kShowStateValueFullscreen; |
Ivan Sandrk | e0e9245 | 2019-03-21 09:32:48 | [diff] [blame] | 500 | if (platform_util::IsBrowserLockedFullscreen(&browser)) |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 501 | window_state = tabs_constants::kShowStateValueLockedFullscreen; |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 502 | } else if (window->IsMaximized()) { |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 503 | window_state = tabs_constants::kShowStateValueMaximized; |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 504 | } else { |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 505 | window_state = tabs_constants::kShowStateValueNormal; |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 506 | } |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 507 | result->SetString(tabs_constants::kShowStateKey, window_state); |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 508 | |
| 509 | gfx::Rect bounds; |
| 510 | if (window->IsMinimized()) |
| 511 | bounds = window->GetRestoredBounds(); |
| 512 | else |
| 513 | bounds = window->GetBounds(); |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 514 | result->SetInteger(tabs_constants::kLeftKey, bounds.x()); |
| 515 | result->SetInteger(tabs_constants::kTopKey, bounds.y()); |
| 516 | result->SetInteger(tabs_constants::kWidthKey, bounds.width()); |
| 517 | result->SetInteger(tabs_constants::kHeightKey, bounds.height()); |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 518 | |
| 519 | if (populate_tab_behavior == kPopulateTabs) |
John Lee | bfbb37cb | 2019-08-26 22:23:18 | [diff] [blame] | 520 | result->Set(tabs_constants::kTabsKey, |
| 521 | CreateTabList(&browser, extension, context)); |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 522 | |
| 523 | return result; |
| 524 | } |
| 525 | |
| 526 | // static |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 527 | std::unique_ptr<api::tabs::MutedInfo> ExtensionTabUtil::CreateMutedInfo( |
miu | 8b605f2 | 2015-08-15 02:56:56 | [diff] [blame] | 528 | content::WebContents* contents) { |
| 529 | DCHECK(contents); |
Peter Kasting | 479cc175 | 2018-09-28 22:34:37 | [diff] [blame] | 530 | auto info = std::make_unique<api::tabs::MutedInfo>(); |
limasdf | 6dcdc44 | 2016-02-26 04:58:26 | [diff] [blame] | 531 | info->muted = contents->IsAudioMuted(); |
miu | 8b605f2 | 2015-08-15 02:56:56 | [diff] [blame] | 532 | switch (chrome::GetTabAudioMutedReason(contents)) { |
ortuno | 71c3f14 | 2016-03-30 18:26:58 | [diff] [blame] | 533 | case TabMutedReason::NONE: |
miu | 8b605f2 | 2015-08-15 02:56:56 | [diff] [blame] | 534 | break; |
Tommy Steimel | a8c3757e | 2017-08-24 00:15:46 | [diff] [blame] | 535 | case TabMutedReason::CONTENT_SETTING: |
Tommy Steimel | 37112950 | 2017-11-29 19:41:45 | [diff] [blame] | 536 | case TabMutedReason::CONTENT_SETTING_CHROME: |
Tommy Steimel | a8c3757e | 2017-08-24 00:15:46 | [diff] [blame] | 537 | case TabMutedReason::CONTEXT_MENU: |
limasdf | 6dcdc44 | 2016-02-26 04:58:26 | [diff] [blame] | 538 | info->reason = api::tabs::MUTED_INFO_REASON_USER; |
miu | 8b605f2 | 2015-08-15 02:56:56 | [diff] [blame] | 539 | break; |
ortuno | 71c3f14 | 2016-03-30 18:26:58 | [diff] [blame] | 540 | case TabMutedReason::MEDIA_CAPTURE: |
limasdf | 6dcdc44 | 2016-02-26 04:58:26 | [diff] [blame] | 541 | info->reason = api::tabs::MUTED_INFO_REASON_CAPTURE; |
miu | 8b605f2 | 2015-08-15 02:56:56 | [diff] [blame] | 542 | break; |
ortuno | 71c3f14 | 2016-03-30 18:26:58 | [diff] [blame] | 543 | case TabMutedReason::EXTENSION: |
limasdf | 6dcdc44 | 2016-02-26 04:58:26 | [diff] [blame] | 544 | info->reason = api::tabs::MUTED_INFO_REASON_EXTENSION; |
Peter Kasting | 479cc175 | 2018-09-28 22:34:37 | [diff] [blame] | 545 | info->extension_id = std::make_unique<std::string>( |
| 546 | LastMuteMetadata::FromWebContents(contents)->extension_id); |
| 547 | DCHECK(!info->extension_id->empty()); |
miu | 8b605f2 | 2015-08-15 02:56:56 | [diff] [blame] | 548 | break; |
| 549 | } |
limasdf | 6dcdc44 | 2016-02-26 04:58:26 | [diff] [blame] | 550 | return info; |
miu | 8b605f2 | 2015-08-15 02:56:56 | [diff] [blame] | 551 | } |
| 552 | |
limasdf | 6dcdc44 | 2016-02-26 04:58:26 | [diff] [blame] | 553 | // static |
Ivan Sandrk | e2b20c6 | 2018-09-10 16:23:53 | [diff] [blame] | 554 | void ExtensionTabUtil::SetPlatformDelegate(std::unique_ptr<Delegate> delegate) { |
Daniel Bratell | be89e344 | 2018-09-11 15:44:57 | [diff] [blame] | 555 | GetExtensionTabUtilDelegateWrapper() = std::move(delegate); |
isandrk | e095113 | 2017-04-24 17:53:31 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | // static |
John Lee | 76e40a3 | 2019-08-20 17:52:40 | [diff] [blame] | 559 | ExtensionTabUtil::ScrubTabBehavior ExtensionTabUtil::GetScrubTabBehavior( |
| 560 | const Extension* extension, |
John Lee | bfbb37cb | 2019-08-26 22:23:18 | [diff] [blame] | 561 | Feature::Context context, |
John Lee | 76e40a3 | 2019-08-20 17:52:40 | [diff] [blame] | 562 | content::WebContents* contents) { |
John Lee | bfbb37cb | 2019-08-26 22:23:18 | [diff] [blame] | 563 | return GetScrubTabBehaviorImpl(extension, context, contents->GetURL(), |
John Lee | 76e40a3 | 2019-08-20 17:52:40 | [diff] [blame] | 564 | GetTabId(contents)); |
| 565 | } |
| 566 | |
| 567 | // static |
| 568 | ExtensionTabUtil::ScrubTabBehavior ExtensionTabUtil::GetScrubTabBehavior( |
| 569 | const Extension* extension, |
John Lee | bfbb37cb | 2019-08-26 22:23:18 | [diff] [blame] | 570 | Feature::Context context, |
John Lee | 76e40a3 | 2019-08-20 17:52:40 | [diff] [blame] | 571 | const GURL& url) { |
John Lee | bfbb37cb | 2019-08-26 22:23:18 | [diff] [blame] | 572 | return GetScrubTabBehaviorImpl(extension, context, url, |
| 573 | api::tabs::TAB_ID_NONE); |
John Lee | 76e40a3 | 2019-08-20 17:52:40 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | // static |
| 577 | void ExtensionTabUtil::ScrubTabForExtension( |
| 578 | const Extension* extension, |
| 579 | content::WebContents* contents, |
| 580 | api::tabs::Tab* tab, |
| 581 | ScrubTabBehavior scrub_tab_behavior) { |
| 582 | switch (scrub_tab_behavior) { |
| 583 | case kScrubTabFully: |
| 584 | tab->url.reset(); |
| 585 | tab->title.reset(); |
| 586 | tab->fav_icon_url.reset(); |
| 587 | break; |
| 588 | case kScrubTabUrlToOrigin: |
| 589 | tab->url = |
| 590 | std::make_unique<std::string>(GURL(*tab->url).GetOrigin().spec()); |
| 591 | break; |
| 592 | case kDontScrubTab: |
| 593 | NOTREACHED(); |
Daniel Bratell | be89e344 | 2018-09-11 15:44:57 | [diff] [blame] | 594 | } |
[email protected] | ab3f6141 | 2013-01-29 21:55:07 | [diff] [blame] | 595 | } |
| 596 | |
Kelvin Jiang | 5658482 | 2019-08-26 22:38:39 | [diff] [blame^] | 597 | // static |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 598 | bool ExtensionTabUtil::GetTabStripModel(const WebContents* web_contents, |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 599 | TabStripModel** tab_strip_model, |
| 600 | int* tab_index) { |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 601 | DCHECK(web_contents); |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 602 | DCHECK(tab_strip_model); |
| 603 | DCHECK(tab_index); |
| 604 | |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 605 | for (auto* browser : *BrowserList::GetInstance()) { |
| 606 | TabStripModel* tab_strip = browser->tab_strip_model(); |
[email protected] | e52d0a4 | 2012-06-08 22:44:16 | [diff] [blame] | 607 | int index = tab_strip->GetIndexOfWebContents(web_contents); |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 608 | if (index != -1) { |
| 609 | *tab_strip_model = tab_strip; |
| 610 | *tab_index = index; |
| 611 | return true; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | return false; |
| 616 | } |
| 617 | |
| 618 | bool ExtensionTabUtil::GetDefaultTab(Browser* browser, |
[email protected] | 72f6797 | 2012-10-30 18:53:28 | [diff] [blame] | 619 | WebContents** contents, |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 620 | int* tab_id) { |
| 621 | DCHECK(browser); |
| 622 | DCHECK(contents); |
| 623 | |
[email protected] | 617ee96 | 2013-01-29 20:49:12 | [diff] [blame] | 624 | *contents = browser->tab_strip_model()->GetActiveWebContents(); |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 625 | if (*contents) { |
| 626 | if (tab_id) |
[email protected] | 72f6797 | 2012-10-30 18:53:28 | [diff] [blame] | 627 | *tab_id = GetTabId(*contents); |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 628 | return true; |
| 629 | } |
| 630 | |
| 631 | return false; |
| 632 | } |
| 633 | |
Istiaque Ahmed | 406b918 | 2019-07-26 06:28:33 | [diff] [blame] | 634 | // static |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 635 | bool ExtensionTabUtil::GetTabById(int tab_id, |
wjmaclean | 5b11eee | 2014-09-05 00:55:14 | [diff] [blame] | 636 | content::BrowserContext* browser_context, |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 637 | bool include_incognito, |
| 638 | Browser** browser, |
| 639 | TabStripModel** tab_strip, |
[email protected] | 72f6797 | 2012-10-30 18:53:28 | [diff] [blame] | 640 | WebContents** contents, |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 641 | int* tab_index) { |
lionel.g.landwerlin | d2315f7 | 2015-07-21 14:17:19 | [diff] [blame] | 642 | if (tab_id == api::tabs::TAB_ID_NONE) |
| 643 | return false; |
wjmaclean | 5b11eee | 2014-09-05 00:55:14 | [diff] [blame] | 644 | Profile* profile = Profile::FromBrowserContext(browser_context); |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 645 | Profile* incognito_profile = |
Catherine Mullings | 0524e204 | 2017-06-26 23:09:45 | [diff] [blame] | 646 | include_incognito && profile->HasOffTheRecordProfile() |
| 647 | ? profile->GetOffTheRecordProfile() |
| 648 | : nullptr; |
scottmg | 8abbff83 | 2016-01-28 22:57:37 | [diff] [blame] | 649 | for (auto* target_browser : *BrowserList::GetInstance()) { |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 650 | if (target_browser->profile() == profile || |
| 651 | target_browser->profile() == incognito_profile) { |
[email protected] | c084925 | 2012-05-12 13:51:27 | [diff] [blame] | 652 | TabStripModel* target_tab_strip = target_browser->tab_strip_model(); |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 653 | for (int i = 0; i < target_tab_strip->count(); ++i) { |
[email protected] | 72f6797 | 2012-10-30 18:53:28 | [diff] [blame] | 654 | WebContents* target_contents = target_tab_strip->GetWebContentsAt(i); |
Mikel Astiz | 2f127c42 | 2018-04-05 19:10:27 | [diff] [blame] | 655 | if (SessionTabHelper::IdForTab(target_contents).id() == tab_id) { |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 656 | if (browser) |
| 657 | *browser = target_browser; |
| 658 | if (tab_strip) |
| 659 | *tab_strip = target_tab_strip; |
| 660 | if (contents) |
| 661 | *contents = target_contents; |
| 662 | if (tab_index) |
| 663 | *tab_index = i; |
| 664 | return true; |
| 665 | } |
| 666 | } |
| 667 | } |
| 668 | } |
| 669 | return false; |
| 670 | } |
[email protected] | 45c75e6 | 2012-03-21 19:56:35 | [diff] [blame] | 671 | |
Istiaque Ahmed | 406b918 | 2019-07-26 06:28:33 | [diff] [blame] | 672 | // static |
| 673 | bool ExtensionTabUtil::GetTabById(int tab_id, |
| 674 | content::BrowserContext* browser_context, |
| 675 | bool include_incognito, |
| 676 | WebContents** contents) { |
| 677 | return GetTabById(tab_id, browser_context, include_incognito, nullptr, |
| 678 | nullptr, contents, nullptr); |
| 679 | } |
| 680 | |
Kelvin Jiang | 5658482 | 2019-08-26 22:38:39 | [diff] [blame^] | 681 | // static |
| 682 | std::vector<content::WebContents*> |
| 683 | ExtensionTabUtil::GetAllActiveWebContentsForContext( |
| 684 | content::BrowserContext* browser_context, |
| 685 | bool include_incognito) { |
| 686 | std::vector<content::WebContents*> active_contents; |
| 687 | |
| 688 | Profile* profile = Profile::FromBrowserContext(browser_context); |
| 689 | Profile* incognito_profile = |
| 690 | include_incognito && profile->HasOffTheRecordProfile() |
| 691 | ? profile->GetOffTheRecordProfile() |
| 692 | : nullptr; |
| 693 | for (auto* target_browser : *BrowserList::GetInstance()) { |
| 694 | if (target_browser->profile() == profile || |
| 695 | target_browser->profile() == incognito_profile) { |
| 696 | TabStripModel* target_tab_strip = target_browser->tab_strip_model(); |
| 697 | |
| 698 | active_contents.push_back(target_tab_strip->GetActiveWebContents()); |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | return active_contents; |
| 703 | } |
| 704 | |
[email protected] | 45c75e6 | 2012-03-21 19:56:35 | [diff] [blame] | 705 | GURL ExtensionTabUtil::ResolvePossiblyRelativeURL(const std::string& url_string, |
[email protected] | 1c4fbc0 | 2013-11-13 02:52:42 | [diff] [blame] | 706 | const Extension* extension) { |
[email protected] | 45c75e6 | 2012-03-21 19:56:35 | [diff] [blame] | 707 | GURL url = GURL(url_string); |
| 708 | if (!url.is_valid()) |
| 709 | url = extension->GetResourceURL(url_string); |
| 710 | |
| 711 | return url; |
| 712 | } |
| 713 | |
kalman | dfefe1a | 2015-07-13 22:27:54 | [diff] [blame] | 714 | bool ExtensionTabUtil::IsKillURL(const GURL& url) { |
Lei Zhang | 4c28b528 | 2017-08-24 04:26:05 | [diff] [blame] | 715 | static const char* const kill_hosts[] = { |
| 716 | chrome::kChromeUICrashHost, chrome::kChromeUIDelayedHangUIHost, |
| 717 | chrome::kChromeUIHangUIHost, chrome::kChromeUIKillHost, |
| 718 | chrome::kChromeUIQuitHost, chrome::kChromeUIRestartHost, |
| 719 | content::kChromeUIBrowserCrashHost, content::kChromeUIMemoryExhaustHost, |
kalman | dfefe1a | 2015-07-13 22:27:54 | [diff] [blame] | 720 | }; |
| 721 | |
[email protected] | 45c75e6 | 2012-03-21 19:56:35 | [diff] [blame] | 722 | // Check a fixed-up URL, to normalize the scheme and parse hosts correctly. |
| 723 | GURL fixed_url = |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 724 | url_formatter::FixupURL(url.possibly_invalid_spec(), std::string()); |
kalman | dfefe1a | 2015-07-13 22:27:54 | [diff] [blame] | 725 | if (!fixed_url.SchemeIs(content::kChromeUIScheme)) |
| 726 | return false; |
| 727 | |
brettw | adc84688 | 2015-09-25 01:16:22 | [diff] [blame] | 728 | base::StringPiece fixed_host = fixed_url.host_piece(); |
Avi Drissman | 5f0fb8c | 2018-12-25 23:20:49 | [diff] [blame] | 729 | for (size_t i = 0; i < base::size(kill_hosts); ++i) { |
brettw | adc84688 | 2015-09-25 01:16:22 | [diff] [blame] | 730 | if (fixed_host == kill_hosts[i]) |
kalman | dfefe1a | 2015-07-13 22:27:54 | [diff] [blame] | 731 | return true; |
| 732 | } |
| 733 | |
| 734 | return false; |
[email protected] | 45c75e6 | 2012-03-21 19:56:35 | [diff] [blame] | 735 | } |
[email protected] | 73c1a684 | 2012-07-13 17:39:04 | [diff] [blame] | 736 | |
erikchen | 38fa402 | 2018-04-26 20:37:52 | [diff] [blame] | 737 | void ExtensionTabUtil::CreateTab(std::unique_ptr<WebContents> web_contents, |
[email protected] | 73c1a684 | 2012-07-13 17:39:04 | [diff] [blame] | 738 | const std::string& extension_id, |
| 739 | WindowOpenDisposition disposition, |
bokan | 107a47f | 2015-02-03 23:23:39 | [diff] [blame] | 740 | const gfx::Rect& initial_rect, |
[email protected] | 73c1a684 | 2012-07-13 17:39:04 | [diff] [blame] | 741 | bool user_gesture) { |
[email protected] | 73c1a684 | 2012-07-13 17:39:04 | [diff] [blame] | 742 | Profile* profile = |
| 743 | Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
scottmg | 34c5dd88 | 2016-02-03 05:21:54 | [diff] [blame] | 744 | Browser* browser = chrome::FindTabbedBrowser(profile, false); |
[email protected] | 2764cab7 | 2012-07-19 17:02:10 | [diff] [blame] | 745 | const bool browser_created = !browser; |
erg | 7b01d69 | 2017-02-22 21:57:35 | [diff] [blame] | 746 | if (!browser) { |
| 747 | Browser::CreateParams params = Browser::CreateParams(profile, user_gesture); |
Toni Barzic | 0070574 | 2019-02-25 22:09:41 | [diff] [blame] | 748 | browser = Browser::Create(params); |
erg | 7b01d69 | 2017-02-22 21:57:35 | [diff] [blame] | 749 | } |
Toni Barzic | 0070574 | 2019-02-25 22:09:41 | [diff] [blame] | 750 | |
| 751 | if (!browser) |
| 752 | return; |
| 753 | |
erikchen | 38fa402 | 2018-04-26 20:37:52 | [diff] [blame] | 754 | NavigateParams params(browser, std::move(web_contents)); |
[email protected] | 73c1a684 | 2012-07-13 17:39:04 | [diff] [blame] | 755 | |
| 756 | // The extension_app_id parameter ends up as app_name in the Browser |
| 757 | // which causes the Browser to return true for is_app(). This affects |
| 758 | // among other things, whether the location bar gets displayed. |
| 759 | // TODO(mpcomplete): This seems wrong. What if the extension content is hosted |
| 760 | // in a tab? |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 761 | if (disposition == WindowOpenDisposition::NEW_POPUP) |
[email protected] | 73c1a684 | 2012-07-13 17:39:04 | [diff] [blame] | 762 | params.extension_app_id = extension_id; |
| 763 | |
[email protected] | 73c1a684 | 2012-07-13 17:39:04 | [diff] [blame] | 764 | params.disposition = disposition; |
bokan | 107a47f | 2015-02-03 23:23:39 | [diff] [blame] | 765 | params.window_bounds = initial_rect; |
cm.sanchi | 2522bc9 | 2017-12-04 08:04:13 | [diff] [blame] | 766 | params.window_action = NavigateParams::SHOW_WINDOW; |
[email protected] | 73c1a684 | 2012-07-13 17:39:04 | [diff] [blame] | 767 | params.user_gesture = user_gesture; |
cm.sanchi | 2522bc9 | 2017-12-04 08:04:13 | [diff] [blame] | 768 | Navigate(¶ms); |
[email protected] | 2764cab7 | 2012-07-19 17:02:10 | [diff] [blame] | 769 | |
cm.sanchi | 2522bc9 | 2017-12-04 08:04:13 | [diff] [blame] | 770 | // Close the browser if Navigate created a new one. |
[email protected] | 2764cab7 | 2012-07-19 17:02:10 | [diff] [blame] | 771 | if (browser_created && (browser != params.browser)) |
| 772 | browser->window()->Close(); |
[email protected] | 73c1a684 | 2012-07-13 17:39:04 | [diff] [blame] | 773 | } |
[email protected] | 44e329a | 2012-07-14 01:13:06 | [diff] [blame] | 774 | |
| 775 | // static |
| 776 | void ExtensionTabUtil::ForEachTab( |
| 777 | const base::Callback<void(WebContents*)>& callback) { |
Avi Drissman | 2f97ce5 | 2018-02-07 23:48:23 | [diff] [blame] | 778 | for (auto* web_contents : AllTabContentses()) |
| 779 | callback.Run(web_contents); |
[email protected] | 44e329a | 2012-07-14 01:13:06 | [diff] [blame] | 780 | } |
[email protected] | e9570fdf | 2012-07-18 20:01:21 | [diff] [blame] | 781 | |
| 782 | // static |
[email protected] | 1c4fbc0 | 2013-11-13 02:52:42 | [diff] [blame] | 783 | WindowController* ExtensionTabUtil::GetWindowControllerOfTab( |
[email protected] | e9570fdf | 2012-07-18 20:01:21 | [diff] [blame] | 784 | const WebContents* web_contents) { |
[email protected] | f7b4b9e | 2012-12-02 07:43:17 | [diff] [blame] | 785 | Browser* browser = chrome::FindBrowserWithWebContents(web_contents); |
Catherine Mullings | 0524e204 | 2017-06-26 23:09:45 | [diff] [blame] | 786 | if (browser != nullptr) |
[email protected] | e9570fdf | 2012-07-18 20:01:21 | [diff] [blame] | 787 | return browser->extension_window_controller(); |
| 788 | |
Catherine Mullings | 0524e204 | 2017-06-26 23:09:45 | [diff] [blame] | 789 | return nullptr; |
[email protected] | e9570fdf | 2012-07-18 20:01:21 | [diff] [blame] | 790 | } |
[email protected] | 69508978 | 2013-04-09 16:03:17 | [diff] [blame] | 791 | |
catmullings | 32ab4eeb | 2017-02-21 18:23:54 | [diff] [blame] | 792 | bool ExtensionTabUtil::OpenOptionsPageFromAPI( |
| 793 | const Extension* extension, |
| 794 | content::BrowserContext* browser_context) { |
| 795 | if (!OptionsPageInfo::HasOptionsPage(extension)) |
| 796 | return false; |
| 797 | Profile* profile = Profile::FromBrowserContext(browser_context); |
| 798 | // This version of OpenOptionsPage() is only called when the extension |
| 799 | // initiated the command via chrome.runtime.openOptionsPage. For a spanning |
| 800 | // mode extension, this API could only be called from a regular profile, since |
| 801 | // that's the only place it's running. |
| 802 | DCHECK(!profile->IsOffTheRecord() || IncognitoInfo::IsSplitMode(extension)); |
| 803 | Browser* browser = chrome::FindBrowserWithProfile(profile); |
| 804 | if (!browser) |
Toni Barzic | 0070574 | 2019-02-25 22:09:41 | [diff] [blame] | 805 | browser = Browser::Create(Browser::CreateParams(profile, true)); |
| 806 | if (!browser) |
| 807 | return false; |
catmullings | 32ab4eeb | 2017-02-21 18:23:54 | [diff] [blame] | 808 | return extensions::ExtensionTabUtil::OpenOptionsPage(extension, browser); |
| 809 | } |
| 810 | |
kalman | ce22c47 | 2015-02-19 02:31:43 | [diff] [blame] | 811 | bool ExtensionTabUtil::OpenOptionsPage(const Extension* extension, |
[email protected] | 69508978 | 2013-04-09 16:03:17 | [diff] [blame] | 812 | Browser* browser) { |
kalman | ce22c47 | 2015-02-19 02:31:43 | [diff] [blame] | 813 | if (!OptionsPageInfo::HasOptionsPage(extension)) |
| 814 | return false; |
[email protected] | 69508978 | 2013-04-09 16:03:17 | [diff] [blame] | 815 | |
catmullings | 32ab4eeb | 2017-02-21 18:23:54 | [diff] [blame] | 816 | // Force the options page to open in non-OTR window if the extension is not |
| 817 | // running in split mode, because it won't be able to save settings from OTR. |
| 818 | // This version of OpenOptionsPage() can be called from an OTR window via e.g. |
| 819 | // the action menu, since that's not initiated by the extension. |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 820 | std::unique_ptr<chrome::ScopedTabbedBrowserDisplayer> displayer; |
catmullings | 32ab4eeb | 2017-02-21 18:23:54 | [diff] [blame] | 821 | if (browser->profile()->IsOffTheRecord() && |
| 822 | !IncognitoInfo::IsSplitMode(extension)) { |
[email protected] | 0edcdec | 2013-10-31 06:43:08 | [diff] [blame] | 823 | displayer.reset(new chrome::ScopedTabbedBrowserDisplayer( |
scottmg | 85194900 | 2016-02-09 20:09:44 | [diff] [blame] | 824 | browser->profile()->GetOriginalProfile())); |
[email protected] | 0edcdec | 2013-10-31 06:43:08 | [diff] [blame] | 825 | browser = displayer->browser(); |
[email protected] | 69508978 | 2013-04-09 16:03:17 | [diff] [blame] | 826 | } |
| 827 | |
kalman | 4427c25 | 2015-03-13 01:55:48 | [diff] [blame] | 828 | GURL url_to_navigate; |
rdevlin.cronin | 6ef722e | 2016-03-16 21:42:27 | [diff] [blame] | 829 | bool open_in_tab = OptionsPageInfo::ShouldOpenInTab(extension); |
| 830 | if (open_in_tab) { |
kalman | 4427c25 | 2015-03-13 01:55:48 | [diff] [blame] | 831 | // Options page tab is simply e.g. chrome-extension://.../options.html. |
| 832 | url_to_navigate = OptionsPageInfo::GetOptionsPage(extension); |
| 833 | } else { |
| 834 | // Options page tab is Extension settings pointed at that Extension's ID, |
| 835 | // e.g. chrome://extensions?options=... |
| 836 | url_to_navigate = GURL(chrome::kChromeUIExtensionsURL); |
ericzeng | f97b7c2 | 2014-08-26 03:07:30 | [diff] [blame] | 837 | GURL::Replacements replacements; |
| 838 | std::string query = |
| 839 | base::StringPrintf("options=%s", extension->id().c_str()); |
| 840 | replacements.SetQueryStr(query); |
kalman | 4427c25 | 2015-03-13 01:55:48 | [diff] [blame] | 841 | url_to_navigate = url_to_navigate.ReplaceComponents(replacements); |
ericzeng | f97b7c2 | 2014-08-26 03:07:30 | [diff] [blame] | 842 | } |
kalman | ce22c47 | 2015-02-19 02:31:43 | [diff] [blame] | 843 | |
cm.sanchi | 2522bc9 | 2017-12-04 08:04:13 | [diff] [blame] | 844 | NavigateParams params( |
| 845 | GetSingletonTabNavigateParams(browser, url_to_navigate)); |
rdevlin.cronin | 10a6ef01 | 2016-02-19 00:26:03 | [diff] [blame] | 846 | // We need to respect path differences because we don't want opening the |
| 847 | // options page to close a page that might be open to extension content. |
rdevlin.cronin | 6ef722e | 2016-03-16 21:42:27 | [diff] [blame] | 848 | // However, if the options page opens inside the chrome://extensions page, we |
| 849 | // can override an existing page. |
Kevin Bailey | 55841fa7 | 2018-03-12 22:52:05 | [diff] [blame] | 850 | // Note: ref behavior is to ignore. |
cm.sanchi | 2522bc9 | 2017-12-04 08:04:13 | [diff] [blame] | 851 | params.path_behavior = open_in_tab ? NavigateParams::RESPECT |
| 852 | : NavigateParams::IGNORE_AND_NAVIGATE; |
kalman | 4427c25 | 2015-03-13 01:55:48 | [diff] [blame] | 853 | params.url = url_to_navigate; |
erikchen | 38fa402 | 2018-04-26 20:37:52 | [diff] [blame] | 854 | ShowSingletonTabOverwritingNTP(browser, std::move(params)); |
kalman | ce22c47 | 2015-02-19 02:31:43 | [diff] [blame] | 855 | return true; |
[email protected] | 69508978 | 2013-04-09 16:03:17 | [diff] [blame] | 856 | } |
[email protected] | 1c4fbc0 | 2013-11-13 02:52:42 | [diff] [blame] | 857 | |
lionel.g.landwerlin | d2315f7 | 2015-07-21 14:17:19 | [diff] [blame] | 858 | // static |
| 859 | bool ExtensionTabUtil::BrowserSupportsTabs(Browser* browser) { |
Joel Hockey | 2687ab5 | 2019-08-14 23:59:46 | [diff] [blame] | 860 | return browser && !browser->is_type_devtools(); |
lionel.g.landwerlin | d2315f7 | 2015-07-21 14:17:19 | [diff] [blame] | 861 | } |
| 862 | |
[email protected] | 1c4fbc0 | 2013-11-13 02:52:42 | [diff] [blame] | 863 | } // namespace extensions |