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