1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_UTIL_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_UTIL_H_
10 #include "base/callback.h"
11 #include "ui/base/window_open_disposition.h"
14 class ChromeExtensionFunctionDetails
;
15 class ChromeUIThreadExtensionFunction
;
16 class ExtensionFunction
;
22 class DictionaryValue
;
35 namespace extensions
{
38 class WindowController
;
46 // Provides various utility functions that help manipulate tabs.
47 class ExtensionTabUtil
{
49 struct OpenTabParams
{
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
;
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.
65 static base::DictionaryValue
* OpenTab(
66 ChromeUIThreadExtensionFunction
* function
,
67 const OpenTabParams
& params
,
70 static int GetWindowId(const Browser
* browser
);
71 static int GetWindowIdOfTabStripModel(const TabStripModel
* tab_strip_model
);
72 static int GetTabId(const content::WebContents
* web_contents
);
73 static std::string
GetTabStatusText(bool is_loading
);
74 static int GetWindowIdOfTab(const content::WebContents
* web_contents
);
75 static base::ListValue
* CreateTabList(const Browser
* browser
,
76 const Extension
* extension
);
78 // DEPRECATED: Please consider using ChromeExtensionFunctionDetails instead
79 // of the deprecated ChromeUIThreadExtensionFunction and use the overload
81 static Browser
* GetBrowserFromWindowID(
82 ChromeUIThreadExtensionFunction
* function
,
84 std::string
* error_message
);
86 static Browser
* GetBrowserFromWindowID(
87 const ChromeExtensionFunctionDetails
& details
,
89 std::string
* error_message
);
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.
95 static base::DictionaryValue
* CreateTabValue(
96 content::WebContents
* web_contents
,
97 const Extension
* extension
) {
98 return CreateTabValue(web_contents
, NULL
, -1, extension
);
100 static base::DictionaryValue
* CreateTabValue(
101 content::WebContents
* web_contents
,
102 TabStripModel
* tab_strip
,
104 const Extension
* extension
);
106 // Creates a Tab object but performs no extension permissions checks; the
107 // returned object will contain privacy-sensitive data.
108 static base::DictionaryValue
* CreateTabValue(
109 content::WebContents
* web_contents
) {
110 return CreateTabValue(web_contents
, NULL
, -1);
112 static base::DictionaryValue
* CreateTabValue(
113 content::WebContents
* web_contents
,
114 TabStripModel
* tab_strip
,
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.
120 static void ScrubTabValueForExtension(content::WebContents
* contents
,
121 const Extension
* extension
,
122 base::DictionaryValue
* tab_info
);
124 // Removes any privacy-sensitive fields from a Tab object if appropriate,
125 // given the permissions of the extension in question.
126 static void ScrubTabForExtension(const Extension
* extension
,
127 api::tabs::Tab
* tab
);
129 // Gets the |tab_strip_model| and |tab_index| for the given |web_contents|.
130 static bool GetTabStripModel(const content::WebContents
* web_contents
,
131 TabStripModel
** tab_strip_model
,
133 static bool GetDefaultTab(Browser
* browser
,
134 content::WebContents
** contents
,
136 // Any out parameter (|browser|, |tab_strip|, |contents|, & |tab_index|) may
137 // be NULL and will not be set within the function.
138 static bool GetTabById(int tab_id
,
139 content::BrowserContext
* browser_context
,
140 bool incognito_enabled
,
142 TabStripModel
** tab_strip
,
143 content::WebContents
** contents
,
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
154 static GURL
ResolvePossiblyRelativeURL(const std::string
& url_string
,
155 const Extension
* extension
);
157 // Returns true if |url| is used for testing crashes.
158 static bool IsCrashURL(const GURL
& url
);
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
,
167 // Executes the specified callback for all tabs in all browser windows.
168 static void ForEachTab(
169 const base::Callback
<void(content::WebContents
*)>& callback
);
171 static WindowController
* GetWindowControllerOfTab(
172 const content::WebContents
* web_contents
);
174 // Open the extension's options page.
175 static void OpenOptionsPage(const Extension
* extension
, Browser
* browser
);
178 } // namespace extensions
180 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_UTIL_H_