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 ChromeUIThreadExtensionFunction
;
20 class DictionaryValue
;
32 namespace extensions
{
35 class WindowController
;
43 // Provides various utility functions that help manipulate tabs.
44 class ExtensionTabUtil
{
46 struct OpenTabParams
{
50 bool create_browser_if_needed
;
51 scoped_ptr
<int> window_id
;
52 scoped_ptr
<int> opener_tab_id
;
53 scoped_ptr
<std::string
> url
;
54 scoped_ptr
<bool> active
;
55 scoped_ptr
<bool> pinned
;
56 scoped_ptr
<int> index
;
59 // Opens a new tab given an extension function |function| and creation
60 // parameters |params|. Returns a Tab object if successful, or NULL and
61 // optionally sets |error| if an error occurs.
62 static base::DictionaryValue
* OpenTab(
63 ChromeUIThreadExtensionFunction
* function
,
64 const OpenTabParams
& params
,
67 static int GetWindowId(const Browser
* browser
);
68 static int GetWindowIdOfTabStripModel(const TabStripModel
* tab_strip_model
);
69 static int GetTabId(const content::WebContents
* web_contents
);
70 static std::string
GetTabStatusText(bool is_loading
);
71 static int GetWindowIdOfTab(const content::WebContents
* web_contents
);
72 static base::ListValue
* CreateTabList(const Browser
* browser
,
73 const Extension
* extension
);
74 static Browser
* GetBrowserFromWindowID(
75 ChromeUIThreadExtensionFunction
* function
,
77 std::string
* error_message
);
79 // Creates a Tab object (see chrome/common/extensions/api/tabs.json) with
80 // information about the state of a browser tab. Depending on the
81 // permissions of the extension, the object may or may not include sensitive
82 // data such as the tab's URL.
83 static base::DictionaryValue
* CreateTabValue(
84 content::WebContents
* web_contents
,
85 const Extension
* extension
) {
86 return CreateTabValue(web_contents
, NULL
, -1, extension
);
88 static base::DictionaryValue
* CreateTabValue(
89 content::WebContents
* web_contents
,
90 TabStripModel
* tab_strip
,
92 const Extension
* extension
);
94 // Creates a Tab object but performs no extension permissions checks; the
95 // returned object will contain privacy-sensitive data.
96 static base::DictionaryValue
* CreateTabValue(
97 content::WebContents
* web_contents
) {
98 return CreateTabValue(web_contents
, NULL
, -1);
100 static base::DictionaryValue
* CreateTabValue(
101 content::WebContents
* web_contents
,
102 TabStripModel
* tab_strip
,
105 // Removes any privacy-sensitive fields from a Tab object if appropriate,
106 // given the permissions of the extension and the tab in question. The
107 // tab_info object is modified in place.
108 static void ScrubTabValueForExtension(content::WebContents
* contents
,
109 const Extension
* extension
,
110 base::DictionaryValue
* tab_info
);
112 // Removes any privacy-sensitive fields from a Tab object if appropriate,
113 // given the permissions of the extension in question.
114 static void ScrubTabForExtension(const Extension
* extension
,
115 api::tabs::Tab
* tab
);
117 // Gets the |tab_strip_model| and |tab_index| for the given |web_contents|.
118 static bool GetTabStripModel(const content::WebContents
* web_contents
,
119 TabStripModel
** tab_strip_model
,
121 static bool GetDefaultTab(Browser
* browser
,
122 content::WebContents
** contents
,
124 // Any out parameter (|browser|, |tab_strip|, |contents|, & |tab_index|) may
125 // be NULL and will not be set within the function.
126 static bool GetTabById(int tab_id
, Profile
* profile
, bool incognito_enabled
,
128 TabStripModel
** tab_strip
,
129 content::WebContents
** contents
,
132 // Takes |url_string| and returns a GURL which is either valid and absolute
133 // or invalid. If |url_string| is not directly interpretable as a valid (it is
134 // likely a relative URL) an attempt is made to resolve it. |extension| is
135 // provided so it can be resolved relative to its extension base
136 // (chrome-extension://<id>/). Using the source frame url would be more
137 // correct, but because the api shipped with urls resolved relative to their
138 // extension base, we decided it wasn't worth breaking existing extensions to
140 static GURL
ResolvePossiblyRelativeURL(const std::string
& url_string
,
141 const Extension
* extension
);
143 // Returns true if |url| is used for testing crashes.
144 static bool IsCrashURL(const GURL
& url
);
146 // Opens a tab for the specified |web_contents|.
147 static void CreateTab(content::WebContents
* web_contents
,
148 const std::string
& extension_id
,
149 WindowOpenDisposition disposition
,
150 const gfx::Rect
& initial_pos
,
153 // Executes the specified callback for all tabs in all browser windows.
154 static void ForEachTab(
155 const base::Callback
<void(content::WebContents
*)>& callback
);
157 static WindowController
* GetWindowControllerOfTab(
158 const content::WebContents
* web_contents
);
160 // Open the extension's options page.
161 static void OpenOptionsPage(const Extension
* extension
, Browser
* browser
);
164 } // namespace extensions
166 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_UTIL_H_