[email protected] | 41d9faf | 2012-02-28 23:46:02 | [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] | 1c4fbc0 | 2013-11-13 02:52:42 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_UTIL_H_ |
| 6 | #define CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_UTIL_H_ |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 7 | |
dcheng | a500b69 | 2016-04-08 19:55:42 | [diff] [blame] | 8 | #include <memory> |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 9 | #include <string> |
Kelvin Jiang | 5658482 | 2019-08-26 22:38:39 | [diff] [blame] | 10 | #include <vector> |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 11 | |
[email protected] | 44e329a | 2012-07-14 01:13:06 | [diff] [blame] | 12 | #include "base/callback.h" |
limasdf | 6dcdc44 | 2016-02-26 04:58:26 | [diff] [blame] | 13 | #include "chrome/common/extensions/api/tabs.h" |
John Lee | bfbb37cb | 2019-08-26 22:23:18 | [diff] [blame] | 14 | #include "extensions/common/features/feature.h" |
[email protected] | f47621b | 2013-01-22 20:50:33 | [diff] [blame] | 15 | #include "ui/base/window_open_disposition.h" |
[email protected] | 73c1a684 | 2012-07-13 17:39:04 | [diff] [blame] | 16 | |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 17 | class Browser; |
lfg | 18533307 | 2014-09-09 20:16:11 | [diff] [blame] | 18 | class ChromeExtensionFunctionDetails; |
[email protected] | 45c75e6 | 2012-03-21 19:56:35 | [diff] [blame] | 19 | class GURL; |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 20 | class TabStripModel; |
Clark DuVall | fd4db3d | 2019-07-30 19:10:43 | [diff] [blame] | 21 | class ExtensionFunction; |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 22 | |
| 23 | namespace base { |
| 24 | class DictionaryValue; |
| 25 | class ListValue; |
| 26 | } |
| 27 | |
[email protected] | 26b5e32 | 2011-12-23 01:36:47 | [diff] [blame] | 28 | namespace content { |
wjmaclean | 5b11eee | 2014-09-05 00:55:14 | [diff] [blame] | 29 | class BrowserContext; |
[email protected] | 26b5e32 | 2011-12-23 01:36:47 | [diff] [blame] | 30 | class WebContents; |
| 31 | } |
| 32 | |
[email protected] | 73c1a684 | 2012-07-13 17:39:04 | [diff] [blame] | 33 | namespace gfx { |
| 34 | class Rect; |
| 35 | } |
| 36 | |
[email protected] | 1c4fbc0 | 2013-11-13 02:52:42 | [diff] [blame] | 37 | namespace extensions { |
| 38 | class Extension; |
| 39 | class WindowController; |
| 40 | |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 41 | // Provides various utility functions that help manipulate tabs. |
| 42 | class ExtensionTabUtil { |
| 43 | public: |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 44 | enum PopulateTabBehavior { |
| 45 | kPopulateTabs, |
| 46 | kDontPopulateTabs, |
| 47 | }; |
| 48 | |
Tim Judkins | 92389f75 | 2019-09-20 21:04:14 | [diff] [blame] | 49 | enum ScrubTabBehaviorType { |
John Lee | 76e40a3 | 2019-08-20 17:52:40 | [diff] [blame] | 50 | kScrubTabFully, |
| 51 | kScrubTabUrlToOrigin, |
Devlin Cronin | 1980f44d | 2018-02-08 23:14:01 | [diff] [blame] | 52 | kDontScrubTab, |
| 53 | }; |
| 54 | |
Tim Judkins | 92389f75 | 2019-09-20 21:04:14 | [diff] [blame] | 55 | struct ScrubTabBehavior { |
| 56 | ScrubTabBehaviorType committed_info; |
| 57 | ScrubTabBehaviorType pending_info; |
| 58 | }; |
| 59 | |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 60 | struct OpenTabParams { |
| 61 | OpenTabParams(); |
| 62 | ~OpenTabParams(); |
| 63 | |
| 64 | bool create_browser_if_needed; |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 65 | std::unique_ptr<int> window_id; |
| 66 | std::unique_ptr<int> opener_tab_id; |
| 67 | std::unique_ptr<std::string> url; |
| 68 | std::unique_ptr<bool> active; |
| 69 | std::unique_ptr<bool> pinned; |
| 70 | std::unique_ptr<int> index; |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 71 | }; |
| 72 | |
isandrk | e095113 | 2017-04-24 17:53:31 | [diff] [blame] | 73 | // Platform specific delegate. |
| 74 | class Delegate { |
| 75 | public: |
| 76 | virtual ~Delegate() {} |
| 77 | // Platform specific scrubbing of tab info for |extension|. |
Tim Judkins | 92389f75 | 2019-09-20 21:04:14 | [diff] [blame] | 78 | virtual ExtensionTabUtil::ScrubTabBehaviorType GetScrubTabBehavior( |
John Lee | 76e40a3 | 2019-08-20 17:52:40 | [diff] [blame] | 79 | const Extension* extension) = 0; |
isandrk | e095113 | 2017-04-24 17:53:31 | [diff] [blame] | 80 | }; |
| 81 | |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 82 | // Opens a new tab given an extension function |function| and creation |
| 83 | // parameters |params|. Returns a Tab object if successful, or NULL and |
| 84 | // optionally sets |error| if an error occurs. |
Clark DuVall | fd4db3d | 2019-07-30 19:10:43 | [diff] [blame] | 85 | static base::DictionaryValue* OpenTab(ExtensionFunction* function, |
rdevlin.cronin | 3223122d | 2016-09-07 21:27:26 | [diff] [blame] | 86 | const OpenTabParams& params, |
erg | 7b01d69 | 2017-02-22 21:57:35 | [diff] [blame] | 87 | bool user_gesture, |
rdevlin.cronin | 3223122d | 2016-09-07 21:27:26 | [diff] [blame] | 88 | std::string* error); |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 89 | |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 90 | static int GetWindowId(const Browser* browser); |
[email protected] | 8c3495c | 2011-09-28 03:32:30 | [diff] [blame] | 91 | static int GetWindowIdOfTabStripModel(const TabStripModel* tab_strip_model); |
[email protected] | d2bd5fde | 2014-05-29 02:24:31 | [diff] [blame] | 92 | static int GetTabId(const content::WebContents* web_contents); |
Chris Hamilton | 2792dba | 2020-03-25 20:17:20 | [diff] [blame^] | 93 | static std::string GetTabStatusText(content::WebContents* web_contents); |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 94 | static int GetWindowIdOfTab(const content::WebContents* web_contents); |
dcheng | 85f24da | 2016-05-20 22:20:26 | [diff] [blame] | 95 | static std::unique_ptr<base::ListValue> CreateTabList( |
| 96 | const Browser* browser, |
John Lee | bfbb37cb | 2019-08-26 22:23:18 | [diff] [blame] | 97 | const Extension* extension, |
| 98 | Feature::Context context); |
lfg | 18533307 | 2014-09-09 20:16:11 | [diff] [blame] | 99 | |
lfg | 18533307 | 2014-09-09 20:16:11 | [diff] [blame] | 100 | static Browser* GetBrowserFromWindowID( |
| 101 | const ChromeExtensionFunctionDetails& details, |
| 102 | int window_id, |
| 103 | std::string* error_message); |
| 104 | |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 105 | // Returns the tabs:: API constant for the window type of the |browser|. |
| 106 | static std::string GetBrowserWindowTypeText(const Browser& browser); |
| 107 | |
[email protected] | 304fd15 | 2013-01-12 16:54:44 | [diff] [blame] | 108 | // Creates a Tab object (see chrome/common/extensions/api/tabs.json) with |
Devlin Cronin | 1980f44d | 2018-02-08 23:14:01 | [diff] [blame] | 109 | // information about the state of a browser tab for the given |web_contents|. |
| 110 | // This will scrub the tab of sensitive data (URL, favicon, title) according |
| 111 | // to |scrub_tab_behavior| and |extension|'s permissions. A null extension is |
| 112 | // treated as having no permissions. |
| 113 | // By default, tab information should always be scrubbed (kScrubTab) for any |
| 114 | // data passed to any extension. |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 115 | static std::unique_ptr<api::tabs::Tab> CreateTabObject( |
[email protected] | fc2b46b | 2014-05-03 16:33:45 | [diff] [blame] | 116 | content::WebContents* web_contents, |
Devlin Cronin | 1980f44d | 2018-02-08 23:14:01 | [diff] [blame] | 117 | ScrubTabBehavior scrub_tab_behavior, |
[email protected] | 1c4fbc0 | 2013-11-13 02:52:42 | [diff] [blame] | 118 | const Extension* extension) { |
Devlin Cronin | 1980f44d | 2018-02-08 23:14:01 | [diff] [blame] | 119 | return CreateTabObject(web_contents, scrub_tab_behavior, extension, nullptr, |
| 120 | -1); |
[email protected] | 0c9f326 | 2012-09-17 05:59:06 | [diff] [blame] | 121 | } |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 122 | static std::unique_ptr<api::tabs::Tab> CreateTabObject( |
[email protected] | fc2b46b | 2014-05-03 16:33:45 | [diff] [blame] | 123 | content::WebContents* web_contents, |
Devlin Cronin | 1980f44d | 2018-02-08 23:14:01 | [diff] [blame] | 124 | ScrubTabBehavior scrub_tab_behavior, |
| 125 | const Extension* extension, |
[email protected] | 0c9f326 | 2012-09-17 05:59:06 | [diff] [blame] | 126 | TabStripModel* tab_strip, |
[email protected] | 304fd15 | 2013-01-12 16:54:44 | [diff] [blame] | 127 | int tab_index); |
| 128 | |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 129 | // Creates a DictionaryValue representing the window for the given |browser|, |
| 130 | // and scrubs any privacy-sensitive data that |extension| does not have |
| 131 | // access to. |populate_tab_behavior| determines whether tabs will be |
John Lee | bfbb37cb | 2019-08-26 22:23:18 | [diff] [blame] | 132 | // populated in the result. |context| is used to determine the |
| 133 | // ScrubTabBehavior for the populated tabs data. |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 134 | // TODO(devlin): Convert this to a api::Windows::Window object. |
| 135 | static std::unique_ptr<base::DictionaryValue> CreateWindowValueForExtension( |
| 136 | const Browser& browser, |
| 137 | const Extension* extension, |
John Lee | bfbb37cb | 2019-08-26 22:23:18 | [diff] [blame] | 138 | PopulateTabBehavior populate_tab_behavior, |
| 139 | Feature::Context context); |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 140 | |
miu | 8b605f2 | 2015-08-15 02:56:56 | [diff] [blame] | 141 | // Creates a tab MutedInfo object (see chrome/common/extensions/api/tabs.json) |
| 142 | // with information about the mute state of a browser tab. |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 143 | static std::unique_ptr<api::tabs::MutedInfo> CreateMutedInfo( |
miu | 8b605f2 | 2015-08-15 02:56:56 | [diff] [blame] | 144 | content::WebContents* contents); |
| 145 | |
isandrk | e095113 | 2017-04-24 17:53:31 | [diff] [blame] | 146 | // Platform specific logic moved to delegate. This should be set during |
| 147 | // startup. |
Ivan Sandrk | e2b20c6 | 2018-09-10 16:23:53 | [diff] [blame] | 148 | static void SetPlatformDelegate(std::unique_ptr<Delegate> delegate); |
isandrk | e095113 | 2017-04-24 17:53:31 | [diff] [blame] | 149 | |
John Lee | 76e40a3 | 2019-08-20 17:52:40 | [diff] [blame] | 150 | // Gets the level of scrubbing of tab data that needs to happen for a given |
| 151 | // extension and web contents. This is the preferred way to get |
| 152 | // ScrubTabBehavior. |
| 153 | static ScrubTabBehavior GetScrubTabBehavior(const Extension* extension, |
John Lee | bfbb37cb | 2019-08-26 22:23:18 | [diff] [blame] | 154 | Feature::Context context, |
John Lee | 76e40a3 | 2019-08-20 17:52:40 | [diff] [blame] | 155 | content::WebContents* contents); |
| 156 | // Only use this if there is no access to a specific WebContents, such as when |
| 157 | // the tab has been closed and there is no active WebContents anymore. |
| 158 | static ScrubTabBehavior GetScrubTabBehavior(const Extension* extension, |
John Lee | bfbb37cb | 2019-08-26 22:23:18 | [diff] [blame] | 159 | Feature::Context context, |
John Lee | 76e40a3 | 2019-08-20 17:52:40 | [diff] [blame] | 160 | const GURL& url); |
| 161 | |
[email protected] | 304fd15 | 2013-01-12 16:54:44 | [diff] [blame] | 162 | // Removes any privacy-sensitive fields from a Tab object if appropriate, |
| 163 | // given the permissions of the extension and the tab in question. The |
limasdf | 6dcdc44 | 2016-02-26 04:58:26 | [diff] [blame] | 164 | // tab object is modified in place. |
[email protected] | 1c4fbc0 | 2013-11-13 02:52:42 | [diff] [blame] | 165 | static void ScrubTabForExtension(const Extension* extension, |
limasdf | 6dcdc44 | 2016-02-26 04:58:26 | [diff] [blame] | 166 | content::WebContents* contents, |
John Lee | 76e40a3 | 2019-08-20 17:52:40 | [diff] [blame] | 167 | api::tabs::Tab* tab, |
| 168 | ScrubTabBehavior scrub_tab_behavior); |
[email protected] | ab3f6141 | 2013-01-29 21:55:07 | [diff] [blame] | 169 | |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 170 | // Gets the |tab_strip_model| and |tab_index| for the given |web_contents|. |
| 171 | static bool GetTabStripModel(const content::WebContents* web_contents, |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 172 | TabStripModel** tab_strip_model, |
| 173 | int* tab_index); |
| 174 | static bool GetDefaultTab(Browser* browser, |
[email protected] | 72f6797 | 2012-10-30 18:53:28 | [diff] [blame] | 175 | content::WebContents** contents, |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 176 | int* tab_id); |
| 177 | // Any out parameter (|browser|, |tab_strip|, |contents|, & |tab_index|) may |
| 178 | // be NULL and will not be set within the function. |
wjmaclean | 5b11eee | 2014-09-05 00:55:14 | [diff] [blame] | 179 | static bool GetTabById(int tab_id, |
| 180 | content::BrowserContext* browser_context, |
Kelvin Jiang | 5658482 | 2019-08-26 22:38:39 | [diff] [blame] | 181 | bool include_incognito, |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 182 | Browser** browser, |
| 183 | TabStripModel** tab_strip, |
[email protected] | 72f6797 | 2012-10-30 18:53:28 | [diff] [blame] | 184 | content::WebContents** contents, |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 185 | int* tab_index); |
Istiaque Ahmed | 406b918 | 2019-07-26 06:28:33 | [diff] [blame] | 186 | static bool GetTabById(int tab_id, |
| 187 | content::BrowserContext* browser_context, |
| 188 | bool include_incognito, |
| 189 | content::WebContents** contents); |
Kelvin Jiang | 5658482 | 2019-08-26 22:38:39 | [diff] [blame] | 190 | // Returns all active web contents for the given |browser_context|. |
| 191 | static std::vector<content::WebContents*> GetAllActiveWebContentsForContext( |
| 192 | content::BrowserContext* browser_context, |
| 193 | bool include_incognito); |
[email protected] | 45c75e6 | 2012-03-21 19:56:35 | [diff] [blame] | 194 | |
| 195 | // Takes |url_string| and returns a GURL which is either valid and absolute |
| 196 | // or invalid. If |url_string| is not directly interpretable as a valid (it is |
| 197 | // likely a relative URL) an attempt is made to resolve it. |extension| is |
| 198 | // provided so it can be resolved relative to its extension base |
| 199 | // (chrome-extension://<id>/). Using the source frame url would be more |
| 200 | // correct, but because the api shipped with urls resolved relative to their |
| 201 | // extension base, we decided it wasn't worth breaking existing extensions to |
| 202 | // fix. |
| 203 | static GURL ResolvePossiblyRelativeURL(const std::string& url_string, |
[email protected] | 1c4fbc0 | 2013-11-13 02:52:42 | [diff] [blame] | 204 | const Extension* extension); |
[email protected] | 45c75e6 | 2012-03-21 19:56:35 | [diff] [blame] | 205 | |
kalman | dfefe1a | 2015-07-13 22:27:54 | [diff] [blame] | 206 | // Returns true if navigating to |url| would kill a page or the browser |
| 207 | // itself, whether by simulating a crash, browser quit, thread hang, or |
| 208 | // equivalent. Extensions should be prevented from navigating to such URLs. |
| 209 | static bool IsKillURL(const GURL& url); |
[email protected] | 73c1a684 | 2012-07-13 17:39:04 | [diff] [blame] | 210 | |
| 211 | // Opens a tab for the specified |web_contents|. |
erikchen | 38fa402 | 2018-04-26 20:37:52 | [diff] [blame] | 212 | static void CreateTab(std::unique_ptr<content::WebContents> web_contents, |
[email protected] | 73c1a684 | 2012-07-13 17:39:04 | [diff] [blame] | 213 | const std::string& extension_id, |
| 214 | WindowOpenDisposition disposition, |
bokan | 107a47f | 2015-02-03 23:23:39 | [diff] [blame] | 215 | const gfx::Rect& initial_rect, |
[email protected] | 73c1a684 | 2012-07-13 17:39:04 | [diff] [blame] | 216 | bool user_gesture); |
[email protected] | 44e329a | 2012-07-14 01:13:06 | [diff] [blame] | 217 | |
| 218 | // Executes the specified callback for all tabs in all browser windows. |
| 219 | static void ForEachTab( |
| 220 | const base::Callback<void(content::WebContents*)>& callback); |
[email protected] | e9570fdf | 2012-07-18 20:01:21 | [diff] [blame] | 221 | |
[email protected] | 1c4fbc0 | 2013-11-13 02:52:42 | [diff] [blame] | 222 | static WindowController* GetWindowControllerOfTab( |
[email protected] | e9570fdf | 2012-07-18 20:01:21 | [diff] [blame] | 223 | const content::WebContents* web_contents); |
[email protected] | 69508978 | 2013-04-09 16:03:17 | [diff] [blame] | 224 | |
kalman | ce22c47 | 2015-02-19 02:31:43 | [diff] [blame] | 225 | // Open the extension's options page. Returns true if an options page was |
| 226 | // successfully opened (though it may not necessarily *load*, e.g. if the |
catmullings | 32ab4eeb | 2017-02-21 18:23:54 | [diff] [blame] | 227 | // URL does not exist). This call to open the options page is iniatiated by |
| 228 | // the extension via chrome.runtime.openOptionsPage. |
| 229 | static bool OpenOptionsPageFromAPI(const Extension* extension, |
| 230 | content::BrowserContext* browser_context); |
| 231 | |
| 232 | // Open the extension's options page. Returns true if an options page was |
| 233 | // successfully opened (though it may not necessarily *load*, e.g. if the |
kalman | ce22c47 | 2015-02-19 02:31:43 | [diff] [blame] | 234 | // URL does not exist). |
| 235 | static bool OpenOptionsPage(const Extension* extension, Browser* browser); |
lionel.g.landwerlin | d2315f7 | 2015-07-21 14:17:19 | [diff] [blame] | 236 | |
| 237 | // Returns true if the given Browser can report tabs to extensions. |
| 238 | // Example of Browsers which don't support tabs include apps and devtools. |
| 239 | static bool BrowserSupportsTabs(Browser* browser); |
Chris Hamilton | 2792dba | 2020-03-25 20:17:20 | [diff] [blame^] | 240 | |
| 241 | // Determines the loading status of the given |contents|. This needs to access |
| 242 | // some non-const member functions of |contents|, but actually leaves it |
| 243 | // unmodified. |
| 244 | static api::tabs::TabStatus GetLoadingStatus(content::WebContents* contents); |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 245 | }; |
| 246 | |
[email protected] | 1c4fbc0 | 2013-11-13 02:52:42 | [diff] [blame] | 247 | } // namespace extensions |
| 248 | |
| 249 | #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_UTIL_H_ |