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"
20 class DictionaryValue;
28 namespace extensions {
30 class WindowController;
37 // Provides various utility functions that help manipulate tabs.
38 class ExtensionTabUtil {
40 static int GetWindowId(const Browser* browser);
41 static int GetWindowIdOfTabStripModel(const TabStripModel* tab_strip_model);
42 static int GetTabId(const content::WebContents* web_contents);
43 static std::string GetTabStatusText(bool is_loading);
44 static int GetWindowIdOfTab(const content::WebContents* web_contents);
45 static base::ListValue* CreateTabList(
46 const Browser* browser,
47 const extensions::Extension* extension);
49 // Creates a Tab object (see chrome/common/extensions/api/tabs.json) with
50 // information about the state of a browser tab. Depending on the
51 // permissions of the extension, the object may or may not include sensitive
52 // data such as the tab's URL.
53 static base::DictionaryValue* CreateTabValue(
54 const content::WebContents* web_contents,
55 const extensions::Extension* extension) {
56 return CreateTabValue(web_contents, NULL, -1, extension);
58 static base::DictionaryValue* CreateTabValue(
59 const content::WebContents* web_contents,
60 TabStripModel* tab_strip,
62 const extensions::Extension* extension);
64 // Creates a Tab object but performs no extension permissions checks; the
65 // returned object will contain privacy-sensitive data.
66 static base::DictionaryValue* CreateTabValue(
67 const content::WebContents* web_contents) {
68 return CreateTabValue(web_contents, NULL, -1);
70 static base::DictionaryValue* CreateTabValue(
71 const content::WebContents* web_contents,
72 TabStripModel* tab_strip,
75 // Removes any privacy-sensitive fields from a Tab object if appropriate,
76 // given the permissions of the extension and the tab in question. The
77 // tab_info object is modified in place.
78 static void ScrubTabValueForExtension(const content::WebContents* contents,
79 const extensions::Extension* extension,
80 base::DictionaryValue* tab_info);
82 // Removes any privacy-sensitive fields from a Tab object if appropriate,
83 // given the permissions of the extension in question.
84 static void ScrubTabForExtension(const extensions::Extension* extension,
85 extensions::api::tabs::Tab* tab);
87 // Gets the |tab_strip_model| and |tab_index| for the given |web_contents|.
88 static bool GetTabStripModel(const content::WebContents* web_contents,
89 TabStripModel** tab_strip_model,
91 static bool GetDefaultTab(Browser* browser,
92 content::WebContents** contents,
94 // Any out parameter (|browser|, |tab_strip|, |contents|, & |tab_index|) may
95 // be NULL and will not be set within the function.
96 static bool GetTabById(int tab_id, Profile* profile, bool incognito_enabled,
98 TabStripModel** tab_strip,
99 content::WebContents** contents,
102 // Takes |url_string| and returns a GURL which is either valid and absolute
103 // or invalid. If |url_string| is not directly interpretable as a valid (it is
104 // likely a relative URL) an attempt is made to resolve it. |extension| is
105 // provided so it can be resolved relative to its extension base
106 // (chrome-extension://<id>/). Using the source frame url would be more
107 // correct, but because the api shipped with urls resolved relative to their
108 // extension base, we decided it wasn't worth breaking existing extensions to
110 static GURL ResolvePossiblyRelativeURL(const std::string& url_string,
111 const extensions::Extension* extension);
113 // Returns true if |url| is used for testing crashes.
114 static bool IsCrashURL(const GURL& url);
116 // Opens a tab for the specified |web_contents|.
117 static void CreateTab(content::WebContents* web_contents,
118 const std::string& extension_id,
119 WindowOpenDisposition disposition,
120 const gfx::Rect& initial_pos,
123 // Executes the specified callback for all tabs in all browser windows.
124 static void ForEachTab(
125 const base::Callback<void(content::WebContents*)>& callback);
127 static extensions::WindowController* GetWindowControllerOfTab(
128 const content::WebContents* web_contents);
130 // Open the extension's options page.
131 static void OpenOptionsPage(const extensions::Extension* extension,
135 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_UTIL_H__