[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 | |
| 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] | ab3f6141 | 2013-01-29 21:55:07 | [diff] [blame^] | 11 | #include "chrome/common/extensions/api/tabs.h" |
[email protected] | f47621b | 2013-01-22 20:50:33 | [diff] [blame] | 12 | #include "ui/base/window_open_disposition.h" |
[email protected] | 73c1a684 | 2012-07-13 17:39:04 | [diff] [blame] | 13 | |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 14 | class Browser; |
[email protected] | 45c75e6 | 2012-03-21 19:56:35 | [diff] [blame] | 15 | class GURL; |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 16 | class Profile; |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 17 | class TabStripModel; |
| 18 | |
| 19 | namespace base { |
| 20 | class DictionaryValue; |
| 21 | class ListValue; |
| 22 | } |
| 23 | |
[email protected] | 26b5e32 | 2011-12-23 01:36:47 | [diff] [blame] | 24 | namespace content { |
| 25 | class WebContents; |
| 26 | } |
| 27 | |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 28 | namespace extensions { |
| 29 | class Extension; |
[email protected] | e9570fdf | 2012-07-18 20:01:21 | [diff] [blame] | 30 | class WindowController; |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 31 | } |
| 32 | |
[email protected] | 73c1a684 | 2012-07-13 17:39:04 | [diff] [blame] | 33 | namespace gfx { |
| 34 | class Rect; |
| 35 | } |
| 36 | |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 37 | // Provides various utility functions that help manipulate tabs. |
| 38 | class ExtensionTabUtil { |
| 39 | public: |
| 40 | static int GetWindowId(const Browser* browser); |
[email protected] | 8c3495c | 2011-09-28 03:32:30 | [diff] [blame] | 41 | static int GetWindowIdOfTabStripModel(const TabStripModel* tab_strip_model); |
[email protected] | 26b5e32 | 2011-12-23 01:36:47 | [diff] [blame] | 42 | static int GetTabId(const content::WebContents* web_contents); |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 43 | static std::string GetTabStatusText(bool is_loading); |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 44 | static int GetWindowIdOfTab(const content::WebContents* web_contents); |
[email protected] | f34706be | 2012-09-04 07:32:09 | [diff] [blame] | 45 | static base::ListValue* CreateTabList( |
| 46 | const Browser* browser, |
| 47 | const extensions::Extension* extension); |
[email protected] | 0c9f326 | 2012-09-17 05:59:06 | [diff] [blame] | 48 | |
[email protected] | 304fd15 | 2013-01-12 16:54:44 | [diff] [blame] | 49 | // Creates a Tab object (see chrome/common/extensions/api/tabs.json) with |
| 50 | // information about the state of a browser tab. Depending on the |
| 51 | // permissions of the extension, the object may or may not include sensitive |
| 52 | // data such as the tab's URL. |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 53 | static base::DictionaryValue* CreateTabValue( |
[email protected] | f34706be | 2012-09-04 07:32:09 | [diff] [blame] | 54 | const content::WebContents* web_contents, |
[email protected] | 0c9f326 | 2012-09-17 05:59:06 | [diff] [blame] | 55 | const extensions::Extension* extension) { |
| 56 | return CreateTabValue(web_contents, NULL, -1, extension); |
| 57 | } |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 58 | static base::DictionaryValue* CreateTabValue( |
| 59 | const content::WebContents* web_contents, |
| 60 | TabStripModel* tab_strip, |
[email protected] | f34706be | 2012-09-04 07:32:09 | [diff] [blame] | 61 | int tab_index, |
| 62 | const extensions::Extension* extension); |
[email protected] | 0c9f326 | 2012-09-17 05:59:06 | [diff] [blame] | 63 | |
[email protected] | 304fd15 | 2013-01-12 16:54:44 | [diff] [blame] | 64 | // Creates a Tab object but performs no extension permissions checks; the |
| 65 | // returned object will contain privacy-sensitive data. |
[email protected] | 0c9f326 | 2012-09-17 05:59:06 | [diff] [blame] | 66 | static base::DictionaryValue* CreateTabValue( |
[email protected] | 304fd15 | 2013-01-12 16:54:44 | [diff] [blame] | 67 | const content::WebContents* web_contents) { |
| 68 | return CreateTabValue(web_contents, NULL, -1); |
[email protected] | 0c9f326 | 2012-09-17 05:59:06 | [diff] [blame] | 69 | } |
| 70 | static base::DictionaryValue* CreateTabValue( |
| 71 | const content::WebContents* web_contents, |
| 72 | TabStripModel* tab_strip, |
[email protected] | 304fd15 | 2013-01-12 16:54:44 | [diff] [blame] | 73 | int tab_index); |
| 74 | |
| 75 | // Removes any privacy-sensitive fields from a Tab object if appropriate, |
| 76 | // given the permissions of the extension and the tab in question. The |
| 77 | // tab_info object is modified in place. |
| 78 | static void ScrubTabValueForExtension(const content::WebContents* contents, |
| 79 | const extensions::Extension* extension, |
| 80 | base::DictionaryValue* tab_info); |
[email protected] | 41d9faf | 2012-02-28 23:46:02 | [diff] [blame] | 81 | |
[email protected] | ab3f6141 | 2013-01-29 21:55:07 | [diff] [blame^] | 82 | // Removes any privacy-sensitive fields from a Tab object if appropriate, |
| 83 | // given the permissions of the extension in question. |
| 84 | static void ScrubTabForExtension(const extensions::Extension* extension, |
| 85 | extensions::api::tabs::Tab* tab); |
| 86 | |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 87 | // Gets the |tab_strip_model| and |tab_index| for the given |web_contents|. |
| 88 | static bool GetTabStripModel(const content::WebContents* web_contents, |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 89 | TabStripModel** tab_strip_model, |
| 90 | int* tab_index); |
| 91 | static bool GetDefaultTab(Browser* browser, |
[email protected] | 72f6797 | 2012-10-30 18:53:28 | [diff] [blame] | 92 | content::WebContents** contents, |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 93 | int* tab_id); |
| 94 | // Any out parameter (|browser|, |tab_strip|, |contents|, & |tab_index|) may |
| 95 | // be NULL and will not be set within the function. |
| 96 | static bool GetTabById(int tab_id, Profile* profile, bool incognito_enabled, |
| 97 | Browser** browser, |
| 98 | TabStripModel** tab_strip, |
[email protected] | 72f6797 | 2012-10-30 18:53:28 | [diff] [blame] | 99 | content::WebContents** contents, |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 100 | int* tab_index); |
[email protected] | 45c75e6 | 2012-03-21 19:56:35 | [diff] [blame] | 101 | |
| 102 | // Takes |url_string| and returns a GURL which is either valid and absolute |
| 103 | // or invalid. If |url_string| is not directly interpretable as a valid (it is |
| 104 | // likely a relative URL) an attempt is made to resolve it. |extension| is |
| 105 | // provided so it can be resolved relative to its extension base |
| 106 | // (chrome-extension://<id>/). Using the source frame url would be more |
| 107 | // correct, but because the api shipped with urls resolved relative to their |
| 108 | // extension base, we decided it wasn't worth breaking existing extensions to |
| 109 | // fix. |
| 110 | static GURL ResolvePossiblyRelativeURL(const std::string& url_string, |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 111 | const extensions::Extension* extension); |
[email protected] | 45c75e6 | 2012-03-21 19:56:35 | [diff] [blame] | 112 | |
| 113 | // Returns true if |url| is used for testing crashes. |
| 114 | static bool IsCrashURL(const GURL& url); |
[email protected] | 73c1a684 | 2012-07-13 17:39:04 | [diff] [blame] | 115 | |
| 116 | // Opens a tab for the specified |web_contents|. |
| 117 | static void CreateTab(content::WebContents* web_contents, |
| 118 | const std::string& extension_id, |
| 119 | WindowOpenDisposition disposition, |
| 120 | const gfx::Rect& initial_pos, |
| 121 | bool user_gesture); |
[email protected] | 44e329a | 2012-07-14 01:13:06 | [diff] [blame] | 122 | |
| 123 | // Executes the specified callback for all tabs in all browser windows. |
| 124 | static void ForEachTab( |
| 125 | const base::Callback<void(content::WebContents*)>& callback); |
[email protected] | e9570fdf | 2012-07-18 20:01:21 | [diff] [blame] | 126 | |
| 127 | static extensions::WindowController* GetWindowControllerOfTab( |
| 128 | const content::WebContents* web_contents); |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_UTIL_H__ |