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 // Creates a tab MutedInfo object (see chrome/common/extensions/api/tabs.json)
118 // with information about the mute state of a browser tab.
119 static scoped_ptr
<base::DictionaryValue
> CreateMutedInfo(
120 content::WebContents
* contents
);
122 // Removes any privacy-sensitive fields from a Tab object if appropriate,
123 // given the permissions of the extension and the tab in question. The
124 // tab_info object is modified in place.
125 static void ScrubTabValueForExtension(content::WebContents
* contents
,
126 const Extension
* extension
,
127 base::DictionaryValue
* tab_info
);
129 // Removes any privacy-sensitive fields from a Tab object if appropriate,
130 // given the permissions of the extension in question.
131 static void ScrubTabForExtension(const Extension
* extension
,
132 api::tabs::Tab
* tab
);
134 // Gets the |tab_strip_model| and |tab_index| for the given |web_contents|.
135 static bool GetTabStripModel(const content::WebContents
* web_contents
,
136 TabStripModel
** tab_strip_model
,
138 static bool GetDefaultTab(Browser
* browser
,
139 content::WebContents
** contents
,
141 // Any out parameter (|browser|, |tab_strip|, |contents|, & |tab_index|) may
142 // be NULL and will not be set within the function.
143 static bool GetTabById(int tab_id
,
144 content::BrowserContext
* browser_context
,
145 bool incognito_enabled
,
147 TabStripModel
** tab_strip
,
148 content::WebContents
** contents
,
151 // Takes |url_string| and returns a GURL which is either valid and absolute
152 // or invalid. If |url_string| is not directly interpretable as a valid (it is
153 // likely a relative URL) an attempt is made to resolve it. |extension| is
154 // provided so it can be resolved relative to its extension base
155 // (chrome-extension://<id>/). Using the source frame url would be more
156 // correct, but because the api shipped with urls resolved relative to their
157 // extension base, we decided it wasn't worth breaking existing extensions to
159 static GURL
ResolvePossiblyRelativeURL(const std::string
& url_string
,
160 const Extension
* extension
);
162 // Returns true if navigating to |url| would kill a page or the browser
163 // itself, whether by simulating a crash, browser quit, thread hang, or
164 // equivalent. Extensions should be prevented from navigating to such URLs.
165 static bool IsKillURL(const GURL
& url
);
167 // Opens a tab for the specified |web_contents|.
168 static void CreateTab(content::WebContents
* web_contents
,
169 const std::string
& extension_id
,
170 WindowOpenDisposition disposition
,
171 const gfx::Rect
& initial_rect
,
174 // Executes the specified callback for all tabs in all browser windows.
175 static void ForEachTab(
176 const base::Callback
<void(content::WebContents
*)>& callback
);
178 static WindowController
* GetWindowControllerOfTab(
179 const content::WebContents
* web_contents
);
181 // Open the extension's options page. Returns true if an options page was
182 // successfully opened (though it may not necessarily *load*, e.g. if the
183 // URL does not exist).
184 static bool OpenOptionsPage(const Extension
* extension
, Browser
* browser
);
186 // Returns true if the given Browser can report tabs to extensions.
187 // Example of Browsers which don't support tabs include apps and devtools.
188 static bool BrowserSupportsTabs(Browser
* browser
);
191 } // namespace extensions
193 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_UTIL_H_