Rename GetIconID to GetIconId
[chromium-blink-merge.git] / chrome / browser / extensions / extension_tab_util.h
blob9af4dcbee510389e294f403b68d95833e8187249
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_
8 #include <string>
10 #include "base/callback.h"
11 #include "ui/base/window_open_disposition.h"
13 class Browser;
14 class ChromeExtensionFunctionDetails;
15 class ChromeUIThreadExtensionFunction;
16 class ExtensionFunction;
17 class GURL;
18 class Profile;
19 class TabStripModel;
21 namespace base {
22 class DictionaryValue;
23 class ListValue;
26 namespace content {
27 class BrowserContext;
28 class WebContents;
31 namespace gfx {
32 class Rect;
35 namespace extensions {
37 class Extension;
38 class WindowController;
40 namespace api {
41 namespace tabs {
42 struct Tab;
46 // Provides various utility functions that help manipulate tabs.
47 class ExtensionTabUtil {
48 public:
49 struct OpenTabParams {
50 OpenTabParams();
51 ~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,
68 std::string* error);
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
80 // below
81 static Browser* GetBrowserFromWindowID(
82 ChromeUIThreadExtensionFunction* function,
83 int window_id,
84 std::string* error_message);
86 static Browser* GetBrowserFromWindowID(
87 const ChromeExtensionFunctionDetails& details,
88 int window_id,
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,
103 int tab_index,
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,
115 int tab_index);
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,
137 int* tab_index);
138 static bool GetDefaultTab(Browser* browser,
139 content::WebContents** contents,
140 int* tab_id);
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,
146 Browser** browser,
147 TabStripModel** tab_strip,
148 content::WebContents** contents,
149 int* tab_index);
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
158 // fix.
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,
172 bool user_gesture);
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_