Revert 285173 "Removed InProcessBrowserTest::CleanUpOnMainThread()"
[chromium-blink-merge.git] / chrome / browser / extensions / extension_tab_util.h
blob8b556216d80de5e5e6afa1b6a16b2f9dec7c6c6d
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 ChromeUIThreadExtensionFunction;
15 class GURL;
16 class Profile;
17 class TabStripModel;
19 namespace base {
20 class DictionaryValue;
21 class ListValue;
24 namespace content {
25 class WebContents;
28 namespace gfx {
29 class Rect;
32 namespace extensions {
34 class Extension;
35 class WindowController;
37 namespace api {
38 namespace tabs {
39 struct Tab;
43 // Provides various utility functions that help manipulate tabs.
44 class ExtensionTabUtil {
45 public:
46 struct OpenTabParams {
47 OpenTabParams();
48 ~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,
65 std::string* error);
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,
76 int window_id,
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,
91 int tab_index,
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,
103 int tab_index);
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,
120 int* tab_index);
121 static bool GetDefaultTab(Browser* browser,
122 content::WebContents** contents,
123 int* tab_id);
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,
127 Browser** browser,
128 TabStripModel** tab_strip,
129 content::WebContents** contents,
130 int* tab_index);
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
139 // fix.
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,
151 bool user_gesture);
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_