blob: 7fc4cb251a9546b952030bc238dba4ebe6dd2b15 [file] [log] [blame]
[email protected]41d9faf2012-02-28 23:46:021// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]ac84431b2011-09-27 17:26:112// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]1c4fbc02013-11-13 02:52:425#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_UTIL_H_
6#define CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_UTIL_H_
[email protected]ac84431b2011-09-27 17:26:117
dchenga500b692016-04-08 19:55:428#include <memory>
[email protected]ac84431b2011-09-27 17:26:119#include <string>
Kelvin Jiang56584822019-08-26 22:38:3910#include <vector>
[email protected]ac84431b2011-09-27 17:26:1111
[email protected]44e329a2012-07-14 01:13:0612#include "base/callback.h"
limasdf6dcdc442016-02-26 04:58:2613#include "chrome/common/extensions/api/tabs.h"
John Leebfbb37cb2019-08-26 22:23:1814#include "extensions/common/features/feature.h"
[email protected]f47621b2013-01-22 20:50:3315#include "ui/base/window_open_disposition.h"
[email protected]73c1a6842012-07-13 17:39:0416
[email protected]ac84431b2011-09-27 17:26:1117class Browser;
lfg185333072014-09-09 20:16:1118class ChromeExtensionFunctionDetails;
[email protected]45c75e62012-03-21 19:56:3519class GURL;
[email protected]ac84431b2011-09-27 17:26:1120class TabStripModel;
Clark DuVallfd4db3d2019-07-30 19:10:4321class ExtensionFunction;
[email protected]ac84431b2011-09-27 17:26:1122
23namespace base {
24class DictionaryValue;
25class ListValue;
26}
27
[email protected]26b5e322011-12-23 01:36:4728namespace content {
wjmaclean5b11eee2014-09-05 00:55:1429class BrowserContext;
[email protected]26b5e322011-12-23 01:36:4730class WebContents;
31}
32
[email protected]73c1a6842012-07-13 17:39:0433namespace gfx {
34class Rect;
35}
36
[email protected]1c4fbc02013-11-13 02:52:4237namespace extensions {
38class Extension;
39class WindowController;
40
[email protected]ac84431b2011-09-27 17:26:1141// Provides various utility functions that help manipulate tabs.
42class ExtensionTabUtil {
43 public:
Devlin Cronin7050f8e2018-02-07 19:52:0444 enum PopulateTabBehavior {
45 kPopulateTabs,
46 kDontPopulateTabs,
47 };
48
Tim Judkins92389f752019-09-20 21:04:1449 enum ScrubTabBehaviorType {
John Lee76e40a32019-08-20 17:52:4050 kScrubTabFully,
51 kScrubTabUrlToOrigin,
Devlin Cronin1980f44d2018-02-08 23:14:0152 kDontScrubTab,
53 };
54
Tim Judkins92389f752019-09-20 21:04:1455 struct ScrubTabBehavior {
56 ScrubTabBehaviorType committed_info;
57 ScrubTabBehaviorType pending_info;
58 };
59
[email protected]98528302014-05-02 00:34:0860 struct OpenTabParams {
61 OpenTabParams();
62 ~OpenTabParams();
63
64 bool create_browser_if_needed;
dchengc963c7142016-04-08 03:55:2265 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]98528302014-05-02 00:34:0871 };
72
isandrke0951132017-04-24 17:53:3173 // Platform specific delegate.
74 class Delegate {
75 public:
76 virtual ~Delegate() {}
77 // Platform specific scrubbing of tab info for |extension|.
Tim Judkins92389f752019-09-20 21:04:1478 virtual ExtensionTabUtil::ScrubTabBehaviorType GetScrubTabBehavior(
John Lee76e40a32019-08-20 17:52:4079 const Extension* extension) = 0;
isandrke0951132017-04-24 17:53:3180 };
81
[email protected]98528302014-05-02 00:34:0882 // 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 DuVallfd4db3d2019-07-30 19:10:4385 static base::DictionaryValue* OpenTab(ExtensionFunction* function,
rdevlin.cronin3223122d2016-09-07 21:27:2686 const OpenTabParams& params,
erg7b01d692017-02-22 21:57:3587 bool user_gesture,
rdevlin.cronin3223122d2016-09-07 21:27:2688 std::string* error);
[email protected]98528302014-05-02 00:34:0889
[email protected]ac84431b2011-09-27 17:26:1190 static int GetWindowId(const Browser* browser);
[email protected]8c3495c2011-09-28 03:32:3091 static int GetWindowIdOfTabStripModel(const TabStripModel* tab_strip_model);
[email protected]d2bd5fde2014-05-29 02:24:3192 static int GetTabId(const content::WebContents* web_contents);
Chris Hamilton2792dba2020-03-25 20:17:2093 static std::string GetTabStatusText(content::WebContents* web_contents);
[email protected]ea049a02011-12-25 21:37:0994 static int GetWindowIdOfTab(const content::WebContents* web_contents);
dcheng85f24da2016-05-20 22:20:2695 static std::unique_ptr<base::ListValue> CreateTabList(
96 const Browser* browser,
John Leebfbb37cb2019-08-26 22:23:1897 const Extension* extension,
98 Feature::Context context);
lfg185333072014-09-09 20:16:1199
lfg185333072014-09-09 20:16:11100 static Browser* GetBrowserFromWindowID(
101 const ChromeExtensionFunctionDetails& details,
102 int window_id,
103 std::string* error_message);
104
Devlin Cronin7050f8e2018-02-07 19:52:04105 // Returns the tabs:: API constant for the window type of the |browser|.
106 static std::string GetBrowserWindowTypeText(const Browser& browser);
107
[email protected]304fd152013-01-12 16:54:44108 // Creates a Tab object (see chrome/common/extensions/api/tabs.json) with
Devlin Cronin1980f44d2018-02-08 23:14:01109 // 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.
dchengc963c7142016-04-08 03:55:22115 static std::unique_ptr<api::tabs::Tab> CreateTabObject(
[email protected]fc2b46b2014-05-03 16:33:45116 content::WebContents* web_contents,
Devlin Cronin1980f44d2018-02-08 23:14:01117 ScrubTabBehavior scrub_tab_behavior,
[email protected]1c4fbc02013-11-13 02:52:42118 const Extension* extension) {
Devlin Cronin1980f44d2018-02-08 23:14:01119 return CreateTabObject(web_contents, scrub_tab_behavior, extension, nullptr,
120 -1);
[email protected]0c9f3262012-09-17 05:59:06121 }
dchengc963c7142016-04-08 03:55:22122 static std::unique_ptr<api::tabs::Tab> CreateTabObject(
[email protected]fc2b46b2014-05-03 16:33:45123 content::WebContents* web_contents,
Devlin Cronin1980f44d2018-02-08 23:14:01124 ScrubTabBehavior scrub_tab_behavior,
125 const Extension* extension,
[email protected]0c9f3262012-09-17 05:59:06126 TabStripModel* tab_strip,
[email protected]304fd152013-01-12 16:54:44127 int tab_index);
128
Devlin Cronin7050f8e2018-02-07 19:52:04129 // 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 Leebfbb37cb2019-08-26 22:23:18132 // populated in the result. |context| is used to determine the
133 // ScrubTabBehavior for the populated tabs data.
Devlin Cronin7050f8e2018-02-07 19:52:04134 // 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 Leebfbb37cb2019-08-26 22:23:18138 PopulateTabBehavior populate_tab_behavior,
139 Feature::Context context);
Devlin Cronin7050f8e2018-02-07 19:52:04140
miu8b605f22015-08-15 02:56:56141 // Creates a tab MutedInfo object (see chrome/common/extensions/api/tabs.json)
142 // with information about the mute state of a browser tab.
dchengc963c7142016-04-08 03:55:22143 static std::unique_ptr<api::tabs::MutedInfo> CreateMutedInfo(
miu8b605f22015-08-15 02:56:56144 content::WebContents* contents);
145
isandrke0951132017-04-24 17:53:31146 // Platform specific logic moved to delegate. This should be set during
147 // startup.
Ivan Sandrke2b20c62018-09-10 16:23:53148 static void SetPlatformDelegate(std::unique_ptr<Delegate> delegate);
isandrke0951132017-04-24 17:53:31149
John Lee76e40a32019-08-20 17:52:40150 // 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 Leebfbb37cb2019-08-26 22:23:18154 Feature::Context context,
John Lee76e40a32019-08-20 17:52:40155 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 Leebfbb37cb2019-08-26 22:23:18159 Feature::Context context,
John Lee76e40a32019-08-20 17:52:40160 const GURL& url);
161
[email protected]304fd152013-01-12 16:54:44162 // Removes any privacy-sensitive fields from a Tab object if appropriate,
163 // given the permissions of the extension and the tab in question. The
limasdf6dcdc442016-02-26 04:58:26164 // tab object is modified in place.
[email protected]1c4fbc02013-11-13 02:52:42165 static void ScrubTabForExtension(const Extension* extension,
limasdf6dcdc442016-02-26 04:58:26166 content::WebContents* contents,
John Lee76e40a32019-08-20 17:52:40167 api::tabs::Tab* tab,
168 ScrubTabBehavior scrub_tab_behavior);
[email protected]ab3f61412013-01-29 21:55:07169
[email protected]ea049a02011-12-25 21:37:09170 // Gets the |tab_strip_model| and |tab_index| for the given |web_contents|.
171 static bool GetTabStripModel(const content::WebContents* web_contents,
[email protected]ac84431b2011-09-27 17:26:11172 TabStripModel** tab_strip_model,
173 int* tab_index);
174 static bool GetDefaultTab(Browser* browser,
[email protected]72f67972012-10-30 18:53:28175 content::WebContents** contents,
[email protected]ac84431b2011-09-27 17:26:11176 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.
wjmaclean5b11eee2014-09-05 00:55:14179 static bool GetTabById(int tab_id,
180 content::BrowserContext* browser_context,
Kelvin Jiang56584822019-08-26 22:38:39181 bool include_incognito,
[email protected]ac84431b2011-09-27 17:26:11182 Browser** browser,
183 TabStripModel** tab_strip,
[email protected]72f67972012-10-30 18:53:28184 content::WebContents** contents,
[email protected]ac84431b2011-09-27 17:26:11185 int* tab_index);
Istiaque Ahmed406b9182019-07-26 06:28:33186 static bool GetTabById(int tab_id,
187 content::BrowserContext* browser_context,
188 bool include_incognito,
189 content::WebContents** contents);
Kelvin Jiang56584822019-08-26 22:38:39190 // 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]45c75e62012-03-21 19:56:35194
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]1c4fbc02013-11-13 02:52:42204 const Extension* extension);
[email protected]45c75e62012-03-21 19:56:35205
kalmandfefe1a2015-07-13 22:27:54206 // 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]73c1a6842012-07-13 17:39:04210
211 // Opens a tab for the specified |web_contents|.
erikchen38fa4022018-04-26 20:37:52212 static void CreateTab(std::unique_ptr<content::WebContents> web_contents,
[email protected]73c1a6842012-07-13 17:39:04213 const std::string& extension_id,
214 WindowOpenDisposition disposition,
bokan107a47f2015-02-03 23:23:39215 const gfx::Rect& initial_rect,
[email protected]73c1a6842012-07-13 17:39:04216 bool user_gesture);
[email protected]44e329a2012-07-14 01:13:06217
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]e9570fdf2012-07-18 20:01:21221
[email protected]1c4fbc02013-11-13 02:52:42222 static WindowController* GetWindowControllerOfTab(
[email protected]e9570fdf2012-07-18 20:01:21223 const content::WebContents* web_contents);
[email protected]695089782013-04-09 16:03:17224
kalmance22c472015-02-19 02:31:43225 // 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
catmullings32ab4eeb2017-02-21 18:23:54227 // 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
kalmance22c472015-02-19 02:31:43234 // URL does not exist).
235 static bool OpenOptionsPage(const Extension* extension, Browser* browser);
lionel.g.landwerlind2315f72015-07-21 14:17:19236
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 Hamilton2792dba2020-03-25 20:17:20240
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]ac84431b2011-09-27 17:26:11245};
246
[email protected]1c4fbc02013-11-13 02:52:42247} // namespace extensions
248
249#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_UTIL_H_