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