[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 | |
| 8 | #include <string> |
| 9 | |
[email protected] | 44e329a | 2012-07-14 01:13:06 | [diff] [blame] | 10 | #include "base/callback.h" |
[email protected] | f47621b | 2013-01-22 20:50:33 | [diff] [blame] | 11 | #include "ui/base/window_open_disposition.h" |
[email protected] | 73c1a684 | 2012-07-13 17:39:04 | [diff] [blame] | 12 | |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 13 | class Browser; |
lfg | 18533307 | 2014-09-09 20:16:11 | [diff] [blame^] | 14 | class ChromeExtensionFunctionDetails; |
[email protected] | a0c91a9f | 2014-05-03 03:41:43 | [diff] [blame] | 15 | class ChromeUIThreadExtensionFunction; |
lfg | 18533307 | 2014-09-09 20:16:11 | [diff] [blame^] | 16 | class ExtensionFunction; |
[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 Profile; |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 19 | class TabStripModel; |
| 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 { |
[email protected] | 760f603 | 2014-06-30 23:18:19 | [diff] [blame] | 36 | |
[email protected] | 1c4fbc0 | 2013-11-13 02:52:42 | [diff] [blame] | 37 | class Extension; |
| 38 | class WindowController; |
| 39 | |
[email protected] | 760f603 | 2014-06-30 23:18:19 | [diff] [blame] | 40 | namespace api { |
| 41 | namespace tabs { |
| 42 | struct Tab; |
| 43 | } |
| 44 | } |
| 45 | |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 46 | // Provides various utility functions that help manipulate tabs. |
| 47 | class ExtensionTabUtil { |
| 48 | public: |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 49 | struct OpenTabParams { |
| 50 | OpenTabParams(); |
| 51 | ~OpenTabParams(); |
| 52 | |
| 53 | bool create_browser_if_needed; |
| 54 | scoped_ptr<int> window_id; |
| 55 | scoped_ptr<int> opener_tab_id; |
| 56 | scoped_ptr<std::string> url; |
| 57 | scoped_ptr<bool> active; |
| 58 | scoped_ptr<bool> pinned; |
| 59 | scoped_ptr<int> index; |
| 60 | }; |
| 61 | |
| 62 | // Opens a new tab given an extension function |function| and creation |
| 63 | // parameters |params|. Returns a Tab object if successful, or NULL and |
| 64 | // optionally sets |error| if an error occurs. |
[email protected] | a0c91a9f | 2014-05-03 03:41:43 | [diff] [blame] | 65 | static base::DictionaryValue* OpenTab( |
| 66 | ChromeUIThreadExtensionFunction* function, |
| 67 | const OpenTabParams& params, |
| 68 | std::string* error); |
[email protected] | 9852830 | 2014-05-02 00:34:08 | [diff] [blame] | 69 | |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 70 | static int GetWindowId(const Browser* browser); |
[email protected] | 8c3495c | 2011-09-28 03:32:30 | [diff] [blame] | 71 | static int GetWindowIdOfTabStripModel(const TabStripModel* tab_strip_model); |
[email protected] | d2bd5fde | 2014-05-29 02:24:31 | [diff] [blame] | 72 | static int GetTabId(const content::WebContents* web_contents); |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 73 | static std::string GetTabStatusText(bool is_loading); |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 74 | static int GetWindowIdOfTab(const content::WebContents* web_contents); |
[email protected] | 1c4fbc0 | 2013-11-13 02:52:42 | [diff] [blame] | 75 | static base::ListValue* CreateTabList(const Browser* browser, |
| 76 | const Extension* extension); |
lfg | 18533307 | 2014-09-09 20:16:11 | [diff] [blame^] | 77 | |
| 78 | // DEPRECATED: Please consider using ChromeExtensionFunctionDetails instead |
| 79 | // of the deprecated ChromeUIThreadExtensionFunction and use the overload |
| 80 | // below |
[email protected] | a0c91a9f | 2014-05-03 03:41:43 | [diff] [blame] | 81 | static Browser* GetBrowserFromWindowID( |
| 82 | ChromeUIThreadExtensionFunction* function, |
| 83 | int window_id, |
| 84 | std::string* error_message); |
[email protected] | 0c9f326 | 2012-09-17 05:59:06 | [diff] [blame] | 85 | |
lfg | 18533307 | 2014-09-09 20:16:11 | [diff] [blame^] | 86 | static Browser* GetBrowserFromWindowID( |
| 87 | const ChromeExtensionFunctionDetails& details, |
| 88 | int window_id, |
| 89 | std::string* error_message); |
| 90 | |
[email protected] | 304fd15 | 2013-01-12 16:54:44 | [diff] [blame] | 91 | // Creates a Tab object (see chrome/common/extensions/api/tabs.json) with |
| 92 | // information about the state of a browser tab. Depending on the |
| 93 | // permissions of the extension, the object may or may not include sensitive |
| 94 | // data such as the tab's URL. |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 95 | static base::DictionaryValue* CreateTabValue( |
[email protected] | fc2b46b | 2014-05-03 16:33:45 | [diff] [blame] | 96 | content::WebContents* web_contents, |
[email protected] | 1c4fbc0 | 2013-11-13 02:52:42 | [diff] [blame] | 97 | const Extension* extension) { |
[email protected] | 0c9f326 | 2012-09-17 05:59:06 | [diff] [blame] | 98 | return CreateTabValue(web_contents, NULL, -1, extension); |
| 99 | } |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 100 | static base::DictionaryValue* CreateTabValue( |
[email protected] | fc2b46b | 2014-05-03 16:33:45 | [diff] [blame] | 101 | content::WebContents* web_contents, |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 102 | TabStripModel* tab_strip, |
[email protected] | f34706be | 2012-09-04 07:32:09 | [diff] [blame] | 103 | int tab_index, |
[email protected] | 1c4fbc0 | 2013-11-13 02:52:42 | [diff] [blame] | 104 | const Extension* extension); |
[email protected] | 0c9f326 | 2012-09-17 05:59:06 | [diff] [blame] | 105 | |
[email protected] | 304fd15 | 2013-01-12 16:54:44 | [diff] [blame] | 106 | // Creates a Tab object but performs no extension permissions checks; the |
| 107 | // returned object will contain privacy-sensitive data. |
[email protected] | 0c9f326 | 2012-09-17 05:59:06 | [diff] [blame] | 108 | static base::DictionaryValue* CreateTabValue( |
[email protected] | fc2b46b | 2014-05-03 16:33:45 | [diff] [blame] | 109 | content::WebContents* web_contents) { |
[email protected] | 304fd15 | 2013-01-12 16:54:44 | [diff] [blame] | 110 | return CreateTabValue(web_contents, NULL, -1); |
[email protected] | 0c9f326 | 2012-09-17 05:59:06 | [diff] [blame] | 111 | } |
| 112 | static base::DictionaryValue* CreateTabValue( |
[email protected] | fc2b46b | 2014-05-03 16:33:45 | [diff] [blame] | 113 | content::WebContents* web_contents, |
[email protected] | 0c9f326 | 2012-09-17 05:59:06 | [diff] [blame] | 114 | TabStripModel* tab_strip, |
[email protected] | 304fd15 | 2013-01-12 16:54:44 | [diff] [blame] | 115 | int tab_index); |
| 116 | |
| 117 | // Removes any privacy-sensitive fields from a Tab object if appropriate, |
| 118 | // given the permissions of the extension and the tab in question. The |
| 119 | // tab_info object is modified in place. |
[email protected] | fc2b46b | 2014-05-03 16:33:45 | [diff] [blame] | 120 | static void ScrubTabValueForExtension(content::WebContents* contents, |
[email protected] | 1c4fbc0 | 2013-11-13 02:52:42 | [diff] [blame] | 121 | const Extension* extension, |
[email protected] | 304fd15 | 2013-01-12 16:54:44 | [diff] [blame] | 122 | base::DictionaryValue* tab_info); |
[email protected] | 41d9faf | 2012-02-28 23:46:02 | [diff] [blame] | 123 | |
[email protected] | ab3f6141 | 2013-01-29 21:55:07 | [diff] [blame] | 124 | // Removes any privacy-sensitive fields from a Tab object if appropriate, |
| 125 | // given the permissions of the extension in question. |
[email protected] | 1c4fbc0 | 2013-11-13 02:52:42 | [diff] [blame] | 126 | static void ScrubTabForExtension(const Extension* extension, |
| 127 | api::tabs::Tab* tab); |
[email protected] | ab3f6141 | 2013-01-29 21:55:07 | [diff] [blame] | 128 | |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 129 | // Gets the |tab_strip_model| and |tab_index| for the given |web_contents|. |
| 130 | static bool GetTabStripModel(const content::WebContents* web_contents, |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 131 | TabStripModel** tab_strip_model, |
| 132 | int* tab_index); |
| 133 | static bool GetDefaultTab(Browser* browser, |
[email protected] | 72f6797 | 2012-10-30 18:53:28 | [diff] [blame] | 134 | content::WebContents** contents, |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 135 | int* tab_id); |
| 136 | // Any out parameter (|browser|, |tab_strip|, |contents|, & |tab_index|) may |
| 137 | // be NULL and will not be set within the function. |
wjmaclean | 5b11eee | 2014-09-05 00:55:14 | [diff] [blame] | 138 | static bool GetTabById(int tab_id, |
| 139 | content::BrowserContext* browser_context, |
| 140 | bool incognito_enabled, |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 141 | Browser** browser, |
| 142 | TabStripModel** tab_strip, |
[email protected] | 72f6797 | 2012-10-30 18:53:28 | [diff] [blame] | 143 | content::WebContents** contents, |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 144 | int* tab_index); |
[email protected] | 45c75e6 | 2012-03-21 19:56:35 | [diff] [blame] | 145 | |
| 146 | // Takes |url_string| and returns a GURL which is either valid and absolute |
| 147 | // or invalid. If |url_string| is not directly interpretable as a valid (it is |
| 148 | // likely a relative URL) an attempt is made to resolve it. |extension| is |
| 149 | // provided so it can be resolved relative to its extension base |
| 150 | // (chrome-extension://<id>/). Using the source frame url would be more |
| 151 | // correct, but because the api shipped with urls resolved relative to their |
| 152 | // extension base, we decided it wasn't worth breaking existing extensions to |
| 153 | // fix. |
| 154 | static GURL ResolvePossiblyRelativeURL(const std::string& url_string, |
[email protected] | 1c4fbc0 | 2013-11-13 02:52:42 | [diff] [blame] | 155 | const Extension* extension); |
[email protected] | 45c75e6 | 2012-03-21 19:56:35 | [diff] [blame] | 156 | |
| 157 | // Returns true if |url| is used for testing crashes. |
| 158 | static bool IsCrashURL(const GURL& url); |
[email protected] | 73c1a684 | 2012-07-13 17:39:04 | [diff] [blame] | 159 | |
| 160 | // Opens a tab for the specified |web_contents|. |
| 161 | static void CreateTab(content::WebContents* web_contents, |
| 162 | const std::string& extension_id, |
| 163 | WindowOpenDisposition disposition, |
| 164 | const gfx::Rect& initial_pos, |
| 165 | bool user_gesture); |
[email protected] | 44e329a | 2012-07-14 01:13:06 | [diff] [blame] | 166 | |
| 167 | // Executes the specified callback for all tabs in all browser windows. |
| 168 | static void ForEachTab( |
| 169 | const base::Callback<void(content::WebContents*)>& callback); |
[email protected] | e9570fdf | 2012-07-18 20:01:21 | [diff] [blame] | 170 | |
[email protected] | 1c4fbc0 | 2013-11-13 02:52:42 | [diff] [blame] | 171 | static WindowController* GetWindowControllerOfTab( |
[email protected] | e9570fdf | 2012-07-18 20:01:21 | [diff] [blame] | 172 | const content::WebContents* web_contents); |
[email protected] | 69508978 | 2013-04-09 16:03:17 | [diff] [blame] | 173 | |
| 174 | // Open the extension's options page. |
[email protected] | 1c4fbc0 | 2013-11-13 02:52:42 | [diff] [blame] | 175 | static void OpenOptionsPage(const Extension* extension, Browser* browser); |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 176 | }; |
| 177 | |
[email protected] | 1c4fbc0 | 2013-11-13 02:52:42 | [diff] [blame] | 178 | } // namespace extensions |
| 179 | |
| 180 | #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_UTIL_H_ |