[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> |
| 10 | |
[email protected] | 44e329a | 2012-07-14 01:13:06 | [diff] [blame] | 11 | #include "base/callback.h" |
limasdf | 6dcdc44 | 2016-02-26 04:58:26 | [diff] [blame] | 12 | #include "chrome/common/extensions/api/tabs.h" |
[email protected] | f47621b | 2013-01-22 20:50:33 | [diff] [blame] | 13 | #include "ui/base/window_open_disposition.h" |
[email protected] | 73c1a684 | 2012-07-13 17:39:04 | [diff] [blame] | 14 | |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 15 | class Browser; |
lfg | 18533307 | 2014-09-09 20:16:11 | [diff] [blame] | 16 | class ChromeExtensionFunctionDetails; |
[email protected] | 45c75e6 | 2012-03-21 19:56:35 | [diff] [blame] | 17 | class GURL; |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 18 | class TabStripModel; |
rdevlin.cronin | 3223122d | 2016-09-07 21:27:26 | [diff] [blame] | 19 | class UIThreadExtensionFunction; |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 20 | |
| 21 | namespace base { |
| 22 | class DictionaryValue; |
| 23 | class ListValue; |
| 24 | } |
| 25 | |
[email protected] | 26b5e32 | 2011-12-23 01:36:47 | [diff] [blame] | 26 | namespace content { |
wjmaclean | 5b11eee | 2014-09-05 00:55:14 | [diff] [blame] | 27 | class BrowserContext; |
[email protected] | 26b5e32 | 2011-12-23 01:36:47 | [diff] [blame] | 28 | class WebContents; |
| 29 | } |
| 30 | |
[email protected] | 73c1a684 | 2012-07-13 17:39:04 | [diff] [blame] | 31 | namespace gfx { |
| 32 | class Rect; |
| 33 | } |
| 34 | |
[email protected] | 1c4fbc0 | 2013-11-13 02:52:42 | [diff] [blame] | 35 | namespace extensions { |
| 36 | class Extension; |
| 37 | class WindowController; |
| 38 | |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 39 | // Provides various utility functions that help manipulate tabs. |
| 40 | class ExtensionTabUtil { |
| 41 | public: |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 42 | enum PopulateTabBehavior { |
| 43 | kPopulateTabs, |
| 44 | kDontPopulateTabs, |
| 45 | }; |
| 46 | |
Devlin Cronin | 1980f44d | 2018-02-08 23:14:01 | [diff] [blame] | 47 | enum ScrubTabBehavior { |
| 48 | kScrubTab, |
| 49 | kDontScrubTab, |
| 50 | }; |
| 51 | |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 52 | struct OpenTabParams { |
| 53 | OpenTabParams(); |
| 54 | ~OpenTabParams(); |
| 55 | |
| 56 | bool create_browser_if_needed; |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 57 | 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] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 63 | }; |
| 64 | |
isandrk | e095113 | 2017-04-24 17:53:31 | [diff] [blame] | 65 | // 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] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 75 | // 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.cronin | 3223122d | 2016-09-07 21:27:26 | [diff] [blame] | 78 | static base::DictionaryValue* OpenTab(UIThreadExtensionFunction* function, |
| 79 | const OpenTabParams& params, |
erg | 7b01d69 | 2017-02-22 21:57:35 | [diff] [blame] | 80 | bool user_gesture, |
rdevlin.cronin | 3223122d | 2016-09-07 21:27:26 | [diff] [blame] | 81 | std::string* error); |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 82 | |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 83 | static int GetWindowId(const Browser* browser); |
[email protected] | 8c3495c | 2011-09-28 03:32:30 | [diff] [blame] | 84 | static int GetWindowIdOfTabStripModel(const TabStripModel* tab_strip_model); |
[email protected] | d2bd5fde | 2014-05-29 02:24:31 | [diff] [blame] | 85 | static int GetTabId(const content::WebContents* web_contents); |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 86 | static std::string GetTabStatusText(bool is_loading); |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 87 | static int GetWindowIdOfTab(const content::WebContents* web_contents); |
dcheng | 85f24da | 2016-05-20 22:20:26 | [diff] [blame] | 88 | static std::unique_ptr<base::ListValue> CreateTabList( |
| 89 | const Browser* browser, |
| 90 | const Extension* extension); |
lfg | 18533307 | 2014-09-09 20:16:11 | [diff] [blame] | 91 | |
lfg | 18533307 | 2014-09-09 20:16:11 | [diff] [blame] | 92 | static Browser* GetBrowserFromWindowID( |
| 93 | const ChromeExtensionFunctionDetails& details, |
| 94 | int window_id, |
| 95 | std::string* error_message); |
| 96 | |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 97 | // Returns the tabs:: API constant for the window type of the |browser|. |
| 98 | static std::string GetBrowserWindowTypeText(const Browser& browser); |
| 99 | |
[email protected] | 304fd15 | 2013-01-12 16:54:44 | [diff] [blame] | 100 | // Creates a Tab object (see chrome/common/extensions/api/tabs.json) with |
Devlin Cronin | 1980f44d | 2018-02-08 23:14:01 | [diff] [blame] | 101 | // 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. |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 107 | static std::unique_ptr<api::tabs::Tab> CreateTabObject( |
[email protected] | fc2b46b | 2014-05-03 16:33:45 | [diff] [blame] | 108 | content::WebContents* web_contents, |
Devlin Cronin | 1980f44d | 2018-02-08 23:14:01 | [diff] [blame] | 109 | ScrubTabBehavior scrub_tab_behavior, |
[email protected] | 1c4fbc0 | 2013-11-13 02:52:42 | [diff] [blame] | 110 | const Extension* extension) { |
Devlin Cronin | 1980f44d | 2018-02-08 23:14:01 | [diff] [blame] | 111 | return CreateTabObject(web_contents, scrub_tab_behavior, extension, nullptr, |
| 112 | -1); |
[email protected] | 0c9f326 | 2012-09-17 05:59:06 | [diff] [blame] | 113 | } |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 114 | static std::unique_ptr<api::tabs::Tab> CreateTabObject( |
[email protected] | fc2b46b | 2014-05-03 16:33:45 | [diff] [blame] | 115 | content::WebContents* web_contents, |
Devlin Cronin | 1980f44d | 2018-02-08 23:14:01 | [diff] [blame] | 116 | ScrubTabBehavior scrub_tab_behavior, |
| 117 | const Extension* extension, |
[email protected] | 0c9f326 | 2012-09-17 05:59:06 | [diff] [blame] | 118 | TabStripModel* tab_strip, |
[email protected] | 304fd15 | 2013-01-12 16:54:44 | [diff] [blame] | 119 | int tab_index); |
| 120 | |
Devlin Cronin | 7050f8e | 2018-02-07 19:52:04 | [diff] [blame] | 121 | // 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 | |
miu | 8b605f2 | 2015-08-15 02:56:56 | [diff] [blame] | 131 | // Creates a tab MutedInfo object (see chrome/common/extensions/api/tabs.json) |
| 132 | // with information about the mute state of a browser tab. |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 133 | static std::unique_ptr<api::tabs::MutedInfo> CreateMutedInfo( |
miu | 8b605f2 | 2015-08-15 02:56:56 | [diff] [blame] | 134 | content::WebContents* contents); |
| 135 | |
isandrk | e095113 | 2017-04-24 17:53:31 | [diff] [blame] | 136 | // 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] | 304fd15 | 2013-01-12 16:54:44 | [diff] [blame] | 141 | // Removes any privacy-sensitive fields from a Tab object if appropriate, |
| 142 | // given the permissions of the extension and the tab in question. The |
limasdf | 6dcdc44 | 2016-02-26 04:58:26 | [diff] [blame] | 143 | // tab object is modified in place. |
[email protected] | 1c4fbc0 | 2013-11-13 02:52:42 | [diff] [blame] | 144 | static void ScrubTabForExtension(const Extension* extension, |
limasdf | 6dcdc44 | 2016-02-26 04:58:26 | [diff] [blame] | 145 | content::WebContents* contents, |
[email protected] | 1c4fbc0 | 2013-11-13 02:52:42 | [diff] [blame] | 146 | api::tabs::Tab* tab); |
[email protected] | ab3f6141 | 2013-01-29 21:55:07 | [diff] [blame] | 147 | |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 148 | // Gets the |tab_strip_model| and |tab_index| for the given |web_contents|. |
| 149 | static bool GetTabStripModel(const content::WebContents* web_contents, |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 150 | TabStripModel** tab_strip_model, |
| 151 | int* tab_index); |
| 152 | static bool GetDefaultTab(Browser* browser, |
[email protected] | 72f6797 | 2012-10-30 18:53:28 | [diff] [blame] | 153 | content::WebContents** contents, |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 154 | 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. |
wjmaclean | 5b11eee | 2014-09-05 00:55:14 | [diff] [blame] | 157 | static bool GetTabById(int tab_id, |
| 158 | content::BrowserContext* browser_context, |
| 159 | bool incognito_enabled, |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 160 | Browser** browser, |
| 161 | TabStripModel** tab_strip, |
[email protected] | 72f6797 | 2012-10-30 18:53:28 | [diff] [blame] | 162 | content::WebContents** contents, |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 163 | int* tab_index); |
[email protected] | 45c75e6 | 2012-03-21 19:56:35 | [diff] [blame] | 164 | |
| 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] | 1c4fbc0 | 2013-11-13 02:52:42 | [diff] [blame] | 174 | const Extension* extension); |
[email protected] | 45c75e6 | 2012-03-21 19:56:35 | [diff] [blame] | 175 | |
kalman | dfefe1a | 2015-07-13 22:27:54 | [diff] [blame] | 176 | // 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] | 73c1a684 | 2012-07-13 17:39:04 | [diff] [blame] | 180 | |
| 181 | // Opens a tab for the specified |web_contents|. |
erikchen | 38fa402 | 2018-04-26 20:37:52 | [diff] [blame^] | 182 | static void CreateTab(std::unique_ptr<content::WebContents> web_contents, |
[email protected] | 73c1a684 | 2012-07-13 17:39:04 | [diff] [blame] | 183 | const std::string& extension_id, |
| 184 | WindowOpenDisposition disposition, |
bokan | 107a47f | 2015-02-03 23:23:39 | [diff] [blame] | 185 | const gfx::Rect& initial_rect, |
[email protected] | 73c1a684 | 2012-07-13 17:39:04 | [diff] [blame] | 186 | bool user_gesture); |
[email protected] | 44e329a | 2012-07-14 01:13:06 | [diff] [blame] | 187 | |
| 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] | e9570fdf | 2012-07-18 20:01:21 | [diff] [blame] | 191 | |
[email protected] | 1c4fbc0 | 2013-11-13 02:52:42 | [diff] [blame] | 192 | static WindowController* GetWindowControllerOfTab( |
[email protected] | e9570fdf | 2012-07-18 20:01:21 | [diff] [blame] | 193 | const content::WebContents* web_contents); |
[email protected] | 69508978 | 2013-04-09 16:03:17 | [diff] [blame] | 194 | |
kalman | ce22c47 | 2015-02-19 02:31:43 | [diff] [blame] | 195 | // 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 |
catmullings | 32ab4eeb | 2017-02-21 18:23:54 | [diff] [blame] | 197 | // 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 |
kalman | ce22c47 | 2015-02-19 02:31:43 | [diff] [blame] | 204 | // URL does not exist). |
| 205 | static bool OpenOptionsPage(const Extension* extension, Browser* browser); |
lionel.g.landwerlin | d2315f7 | 2015-07-21 14:17:19 | [diff] [blame] | 206 | |
| 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] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 210 | }; |
| 211 | |
[email protected] | 1c4fbc0 | 2013-11-13 02:52:42 | [diff] [blame] | 212 | } // namespace extensions |
| 213 | |
| 214 | #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_UTIL_H_ |