blob: a81330265ef6c7c3708bab715f7e4deb56fbf8ff [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>
10
[email protected]44e329a2012-07-14 01:13:0611#include "base/callback.h"
limasdf6dcdc442016-02-26 04:58:2612#include "chrome/common/extensions/api/tabs.h"
[email protected]f47621b2013-01-22 20:50:3313#include "ui/base/window_open_disposition.h"
[email protected]73c1a6842012-07-13 17:39:0414
[email protected]ac84431b2011-09-27 17:26:1115class Browser;
lfg185333072014-09-09 20:16:1116class ChromeExtensionFunctionDetails;
[email protected]45c75e62012-03-21 19:56:3517class GURL;
[email protected]ac84431b2011-09-27 17:26:1118class TabStripModel;
rdevlin.cronin3223122d2016-09-07 21:27:2619class UIThreadExtensionFunction;
[email protected]ac84431b2011-09-27 17:26:1120
21namespace base {
22class DictionaryValue;
23class ListValue;
24}
25
[email protected]26b5e322011-12-23 01:36:4726namespace content {
wjmaclean5b11eee2014-09-05 00:55:1427class BrowserContext;
[email protected]26b5e322011-12-23 01:36:4728class WebContents;
29}
30
[email protected]73c1a6842012-07-13 17:39:0431namespace gfx {
32class Rect;
33}
34
[email protected]1c4fbc02013-11-13 02:52:4235namespace extensions {
36class Extension;
37class WindowController;
38
[email protected]ac84431b2011-09-27 17:26:1139// Provides various utility functions that help manipulate tabs.
40class ExtensionTabUtil {
41 public:
Devlin Cronin7050f8e2018-02-07 19:52:0442 enum PopulateTabBehavior {
43 kPopulateTabs,
44 kDontPopulateTabs,
45 };
46
Devlin Cronin1980f44d2018-02-08 23:14:0147 enum ScrubTabBehavior {
48 kScrubTab,
49 kDontScrubTab,
50 };
51
[email protected]98528302014-05-02 00:34:0852 struct OpenTabParams {
53 OpenTabParams();
54 ~OpenTabParams();
55
56 bool create_browser_if_needed;
dchengc963c7142016-04-08 03:55:2257 std::unique_ptr<int> window_id;
58 std::unique_ptr<int> opener_tab_id;
59 std::unique_ptr<std::string> url;
60 std::unique_ptr<bool> active;
61 std::unique_ptr<bool> pinned;
62 std::unique_ptr<int> index;
[email protected]98528302014-05-02 00:34:0863 };
64
isandrke0951132017-04-24 17:53:3165 // Platform specific delegate.
66 class Delegate {
67 public:
68 virtual ~Delegate() {}
69 // Platform specific scrubbing of tab info for |extension|.
70 virtual void ScrubTabForExtension(const Extension* extension,
71 content::WebContents* contents,
72 api::tabs::Tab* tab) = 0;
73 };
74
[email protected]98528302014-05-02 00:34:0875 // Opens a new tab given an extension function |function| and creation
76 // parameters |params|. Returns a Tab object if successful, or NULL and
77 // optionally sets |error| if an error occurs.
rdevlin.cronin3223122d2016-09-07 21:27:2678 static base::DictionaryValue* OpenTab(UIThreadExtensionFunction* function,
79 const OpenTabParams& params,
erg7b01d692017-02-22 21:57:3580 bool user_gesture,
rdevlin.cronin3223122d2016-09-07 21:27:2681 std::string* error);
[email protected]98528302014-05-02 00:34:0882
[email protected]ac84431b2011-09-27 17:26:1183 static int GetWindowId(const Browser* browser);
[email protected]8c3495c2011-09-28 03:32:3084 static int GetWindowIdOfTabStripModel(const TabStripModel* tab_strip_model);
[email protected]d2bd5fde2014-05-29 02:24:3185 static int GetTabId(const content::WebContents* web_contents);
[email protected]ac84431b2011-09-27 17:26:1186 static std::string GetTabStatusText(bool is_loading);
[email protected]ea049a02011-12-25 21:37:0987 static int GetWindowIdOfTab(const content::WebContents* web_contents);
dcheng85f24da2016-05-20 22:20:2688 static std::unique_ptr<base::ListValue> CreateTabList(
89 const Browser* browser,
90 const Extension* extension);
lfg185333072014-09-09 20:16:1191
lfg185333072014-09-09 20:16:1192 static Browser* GetBrowserFromWindowID(
93 const ChromeExtensionFunctionDetails& details,
94 int window_id,
95 std::string* error_message);
96
Devlin Cronin7050f8e2018-02-07 19:52:0497 // Returns the tabs:: API constant for the window type of the |browser|.
98 static std::string GetBrowserWindowTypeText(const Browser& browser);
99
[email protected]304fd152013-01-12 16:54:44100 // Creates a Tab object (see chrome/common/extensions/api/tabs.json) with
Devlin Cronin1980f44d2018-02-08 23:14:01101 // information about the state of a browser tab for the given |web_contents|.
102 // This will scrub the tab of sensitive data (URL, favicon, title) according
103 // to |scrub_tab_behavior| and |extension|'s permissions. A null extension is
104 // treated as having no permissions.
105 // By default, tab information should always be scrubbed (kScrubTab) for any
106 // data passed to any extension.
dchengc963c7142016-04-08 03:55:22107 static std::unique_ptr<api::tabs::Tab> CreateTabObject(
[email protected]fc2b46b2014-05-03 16:33:45108 content::WebContents* web_contents,
Devlin Cronin1980f44d2018-02-08 23:14:01109 ScrubTabBehavior scrub_tab_behavior,
[email protected]1c4fbc02013-11-13 02:52:42110 const Extension* extension) {
Devlin Cronin1980f44d2018-02-08 23:14:01111 return CreateTabObject(web_contents, scrub_tab_behavior, extension, nullptr,
112 -1);
[email protected]0c9f3262012-09-17 05:59:06113 }
dchengc963c7142016-04-08 03:55:22114 static std::unique_ptr<api::tabs::Tab> CreateTabObject(
[email protected]fc2b46b2014-05-03 16:33:45115 content::WebContents* web_contents,
Devlin Cronin1980f44d2018-02-08 23:14:01116 ScrubTabBehavior scrub_tab_behavior,
117 const Extension* extension,
[email protected]0c9f3262012-09-17 05:59:06118 TabStripModel* tab_strip,
[email protected]304fd152013-01-12 16:54:44119 int tab_index);
120
Devlin Cronin7050f8e2018-02-07 19:52:04121 // Creates a DictionaryValue representing the window for the given |browser|,
122 // and scrubs any privacy-sensitive data that |extension| does not have
123 // access to. |populate_tab_behavior| determines whether tabs will be
124 // populated in the result.
125 // TODO(devlin): Convert this to a api::Windows::Window object.
126 static std::unique_ptr<base::DictionaryValue> CreateWindowValueForExtension(
127 const Browser& browser,
128 const Extension* extension,
129 PopulateTabBehavior populate_tab_behavior);
130
miu8b605f22015-08-15 02:56:56131 // Creates a tab MutedInfo object (see chrome/common/extensions/api/tabs.json)
132 // with information about the mute state of a browser tab.
dchengc963c7142016-04-08 03:55:22133 static std::unique_ptr<api::tabs::MutedInfo> CreateMutedInfo(
miu8b605f22015-08-15 02:56:56134 content::WebContents* contents);
135
isandrke0951132017-04-24 17:53:31136 // Platform specific logic moved to delegate. This should be set during
137 // startup.
138 // |delegate| is a singleton instance and is leaked.
139 static void SetPlatformDelegate(Delegate* delegate);
140
[email protected]304fd152013-01-12 16:54:44141 // Removes any privacy-sensitive fields from a Tab object if appropriate,
142 // given the permissions of the extension and the tab in question. The
limasdf6dcdc442016-02-26 04:58:26143 // tab object is modified in place.
[email protected]1c4fbc02013-11-13 02:52:42144 static void ScrubTabForExtension(const Extension* extension,
limasdf6dcdc442016-02-26 04:58:26145 content::WebContents* contents,
[email protected]1c4fbc02013-11-13 02:52:42146 api::tabs::Tab* tab);
[email protected]ab3f61412013-01-29 21:55:07147
[email protected]ea049a02011-12-25 21:37:09148 // Gets the |tab_strip_model| and |tab_index| for the given |web_contents|.
149 static bool GetTabStripModel(const content::WebContents* web_contents,
[email protected]ac84431b2011-09-27 17:26:11150 TabStripModel** tab_strip_model,
151 int* tab_index);
152 static bool GetDefaultTab(Browser* browser,
[email protected]72f67972012-10-30 18:53:28153 content::WebContents** contents,
[email protected]ac84431b2011-09-27 17:26:11154 int* tab_id);
155 // Any out parameter (|browser|, |tab_strip|, |contents|, & |tab_index|) may
156 // be NULL and will not be set within the function.
wjmaclean5b11eee2014-09-05 00:55:14157 static bool GetTabById(int tab_id,
158 content::BrowserContext* browser_context,
159 bool incognito_enabled,
[email protected]ac84431b2011-09-27 17:26:11160 Browser** browser,
161 TabStripModel** tab_strip,
[email protected]72f67972012-10-30 18:53:28162 content::WebContents** contents,
[email protected]ac84431b2011-09-27 17:26:11163 int* tab_index);
[email protected]45c75e62012-03-21 19:56:35164
165 // Takes |url_string| and returns a GURL which is either valid and absolute
166 // or invalid. If |url_string| is not directly interpretable as a valid (it is
167 // likely a relative URL) an attempt is made to resolve it. |extension| is
168 // provided so it can be resolved relative to its extension base
169 // (chrome-extension://<id>/). Using the source frame url would be more
170 // correct, but because the api shipped with urls resolved relative to their
171 // extension base, we decided it wasn't worth breaking existing extensions to
172 // fix.
173 static GURL ResolvePossiblyRelativeURL(const std::string& url_string,
[email protected]1c4fbc02013-11-13 02:52:42174 const Extension* extension);
[email protected]45c75e62012-03-21 19:56:35175
kalmandfefe1a2015-07-13 22:27:54176 // Returns true if navigating to |url| would kill a page or the browser
177 // itself, whether by simulating a crash, browser quit, thread hang, or
178 // equivalent. Extensions should be prevented from navigating to such URLs.
179 static bool IsKillURL(const GURL& url);
[email protected]73c1a6842012-07-13 17:39:04180
181 // Opens a tab for the specified |web_contents|.
182 static void CreateTab(content::WebContents* web_contents,
183 const std::string& extension_id,
184 WindowOpenDisposition disposition,
bokan107a47f2015-02-03 23:23:39185 const gfx::Rect& initial_rect,
[email protected]73c1a6842012-07-13 17:39:04186 bool user_gesture);
[email protected]44e329a2012-07-14 01:13:06187
188 // Executes the specified callback for all tabs in all browser windows.
189 static void ForEachTab(
190 const base::Callback<void(content::WebContents*)>& callback);
[email protected]e9570fdf2012-07-18 20:01:21191
[email protected]1c4fbc02013-11-13 02:52:42192 static WindowController* GetWindowControllerOfTab(
[email protected]e9570fdf2012-07-18 20:01:21193 const content::WebContents* web_contents);
[email protected]695089782013-04-09 16:03:17194
kalmance22c472015-02-19 02:31:43195 // Open the extension's options page. Returns true if an options page was
196 // successfully opened (though it may not necessarily *load*, e.g. if the
catmullings32ab4eeb2017-02-21 18:23:54197 // URL does not exist). This call to open the options page is iniatiated by
198 // the extension via chrome.runtime.openOptionsPage.
199 static bool OpenOptionsPageFromAPI(const Extension* extension,
200 content::BrowserContext* browser_context);
201
202 // Open the extension's options page. Returns true if an options page was
203 // successfully opened (though it may not necessarily *load*, e.g. if the
kalmance22c472015-02-19 02:31:43204 // URL does not exist).
205 static bool OpenOptionsPage(const Extension* extension, Browser* browser);
lionel.g.landwerlind2315f72015-07-21 14:17:19206
207 // Returns true if the given Browser can report tabs to extensions.
208 // Example of Browsers which don't support tabs include apps and devtools.
209 static bool BrowserSupportsTabs(Browser* browser);
[email protected]ac84431b2011-09-27 17:26:11210};
211
[email protected]1c4fbc02013-11-13 02:52:42212} // namespace extensions
213
214#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_UTIL_H_