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 ChromeAsyncExtensionFunction
;
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(ChromeAsyncExtensionFunction
* function
,
57 const OpenTabParams
& params
,
60 static int GetWindowId(const Browser
* browser
);
61 static int GetWindowIdOfTabStripModel(const TabStripModel
* tab_strip_model
);
62 static int GetTabId(const content::WebContents
* web_contents
);
63 static std::string
GetTabStatusText(bool is_loading
);
64 static int GetWindowIdOfTab(const content::WebContents
* web_contents
);
65 static base::ListValue
* CreateTabList(const Browser
* browser
,
66 const Extension
* extension
);
67 static Browser
* GetBrowserFromWindowID(ChromeAsyncExtensionFunction
* function
,
69 std::string
* error_message
);
71 // Creates a Tab object (see chrome/common/extensions/api/tabs.json) with
72 // information about the state of a browser tab. Depending on the
73 // permissions of the extension, the object may or may not include sensitive
74 // data such as the tab's URL.
75 static base::DictionaryValue
* CreateTabValue(
76 const content::WebContents
* web_contents
,
77 const Extension
* extension
) {
78 return CreateTabValue(web_contents
, NULL
, -1, extension
);
80 static base::DictionaryValue
* CreateTabValue(
81 const content::WebContents
* web_contents
,
82 TabStripModel
* tab_strip
,
84 const Extension
* extension
);
86 // Creates a Tab object but performs no extension permissions checks; the
87 // returned object will contain privacy-sensitive data.
88 static base::DictionaryValue
* CreateTabValue(
89 const content::WebContents
* web_contents
) {
90 return CreateTabValue(web_contents
, NULL
, -1);
92 static base::DictionaryValue
* CreateTabValue(
93 const content::WebContents
* web_contents
,
94 TabStripModel
* tab_strip
,
97 // Removes any privacy-sensitive fields from a Tab object if appropriate,
98 // given the permissions of the extension and the tab in question. The
99 // tab_info object is modified in place.
100 static void ScrubTabValueForExtension(const content::WebContents
* contents
,
101 const Extension
* extension
,
102 base::DictionaryValue
* tab_info
);
104 // Removes any privacy-sensitive fields from a Tab object if appropriate,
105 // given the permissions of the extension in question.
106 static void ScrubTabForExtension(const Extension
* extension
,
107 api::tabs::Tab
* tab
);
109 // Gets the |tab_strip_model| and |tab_index| for the given |web_contents|.
110 static bool GetTabStripModel(const content::WebContents
* web_contents
,
111 TabStripModel
** tab_strip_model
,
113 static bool GetDefaultTab(Browser
* browser
,
114 content::WebContents
** contents
,
116 // Any out parameter (|browser|, |tab_strip|, |contents|, & |tab_index|) may
117 // be NULL and will not be set within the function.
118 static bool GetTabById(int tab_id
, Profile
* profile
, bool incognito_enabled
,
120 TabStripModel
** tab_strip
,
121 content::WebContents
** contents
,
124 // Takes |url_string| and returns a GURL which is either valid and absolute
125 // or invalid. If |url_string| is not directly interpretable as a valid (it is
126 // likely a relative URL) an attempt is made to resolve it. |extension| is
127 // provided so it can be resolved relative to its extension base
128 // (chrome-extension://<id>/). Using the source frame url would be more
129 // correct, but because the api shipped with urls resolved relative to their
130 // extension base, we decided it wasn't worth breaking existing extensions to
132 static GURL
ResolvePossiblyRelativeURL(const std::string
& url_string
,
133 const Extension
* extension
);
135 // Returns true if |url| is used for testing crashes.
136 static bool IsCrashURL(const GURL
& url
);
138 // Opens a tab for the specified |web_contents|.
139 static void CreateTab(content::WebContents
* web_contents
,
140 const std::string
& extension_id
,
141 WindowOpenDisposition disposition
,
142 const gfx::Rect
& initial_pos
,
145 // Executes the specified callback for all tabs in all browser windows.
146 static void ForEachTab(
147 const base::Callback
<void(content::WebContents
*)>& callback
);
149 static WindowController
* GetWindowControllerOfTab(
150 const content::WebContents
* web_contents
);
152 // Open the extension's options page.
153 static void OpenOptionsPage(const Extension
* extension
, Browser
* browser
);
156 } // namespace extensions
158 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_UTIL_H_