blob: 0f185d2617dba7cd55928f684a3ba45059a9f8bc [file] [log] [blame]
[email protected]41d9faf2012-02-28 23:46:021// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]ac84431b2011-09-27 17:26:112// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]1c4fbc02013-11-13 02:52:425#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_UTIL_H_
6#define CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_UTIL_H_
[email protected]ac84431b2011-09-27 17:26:117
8#include <string>
9
[email protected]44e329a2012-07-14 01:13:0610#include "base/callback.h"
[email protected]f47621b2013-01-22 20:50:3311#include "ui/base/window_open_disposition.h"
[email protected]73c1a6842012-07-13 17:39:0412
[email protected]ac84431b2011-09-27 17:26:1113class Browser;
lfg185333072014-09-09 20:16:1114class ChromeExtensionFunctionDetails;
[email protected]a0c91a9f2014-05-03 03:41:4315class ChromeUIThreadExtensionFunction;
lfg185333072014-09-09 20:16:1116class ExtensionFunction;
[email protected]45c75e62012-03-21 19:56:3517class GURL;
[email protected]ac84431b2011-09-27 17:26:1118class Profile;
[email protected]ac84431b2011-09-27 17:26:1119class TabStripModel;
20
21namespace base {
22class DictionaryValue;
23class ListValue;
24}
25
[email protected]26b5e322011-12-23 01:36:4726namespace content {
wjmaclean5b11eee2014-09-05 00:55:1427class BrowserContext;
[email protected]26b5e322011-12-23 01:36:4728class WebContents;
29}
30
[email protected]73c1a6842012-07-13 17:39:0431namespace gfx {
32class Rect;
33}
34
[email protected]1c4fbc02013-11-13 02:52:4235namespace extensions {
[email protected]760f6032014-06-30 23:18:1936
[email protected]1c4fbc02013-11-13 02:52:4237class Extension;
38class WindowController;
39
[email protected]760f6032014-06-30 23:18:1940namespace api {
41namespace tabs {
42struct Tab;
43}
44}
45
[email protected]ac84431b2011-09-27 17:26:1146// Provides various utility functions that help manipulate tabs.
47class ExtensionTabUtil {
48 public:
[email protected]98528302014-05-02 00:34:0849 struct OpenTabParams {
50 OpenTabParams();
51 ~OpenTabParams();
52
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;
60 };
61
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.
[email protected]a0c91a9f2014-05-03 03:41:4365 static base::DictionaryValue* OpenTab(
66 ChromeUIThreadExtensionFunction* function,
67 const OpenTabParams& params,
68 std::string* error);
[email protected]98528302014-05-02 00:34:0869
[email protected]ac84431b2011-09-27 17:26:1170 static int GetWindowId(const Browser* browser);
[email protected]8c3495c2011-09-28 03:32:3071 static int GetWindowIdOfTabStripModel(const TabStripModel* tab_strip_model);
[email protected]d2bd5fde2014-05-29 02:24:3172 static int GetTabId(const content::WebContents* web_contents);
[email protected]ac84431b2011-09-27 17:26:1173 static std::string GetTabStatusText(bool is_loading);
[email protected]ea049a02011-12-25 21:37:0974 static int GetWindowIdOfTab(const content::WebContents* web_contents);
[email protected]1c4fbc02013-11-13 02:52:4275 static base::ListValue* CreateTabList(const Browser* browser,
76 const Extension* extension);
lfg185333072014-09-09 20:16:1177
78 // DEPRECATED: Please consider using ChromeExtensionFunctionDetails instead
79 // of the deprecated ChromeUIThreadExtensionFunction and use the overload
80 // below
[email protected]a0c91a9f2014-05-03 03:41:4381 static Browser* GetBrowserFromWindowID(
82 ChromeUIThreadExtensionFunction* function,
83 int window_id,
84 std::string* error_message);
[email protected]0c9f3262012-09-17 05:59:0685
lfg185333072014-09-09 20:16:1186 static Browser* GetBrowserFromWindowID(
87 const ChromeExtensionFunctionDetails& details,
88 int window_id,
89 std::string* error_message);
90
[email protected]304fd152013-01-12 16:54:4491 // 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.
[email protected]ac84431b2011-09-27 17:26:1195 static base::DictionaryValue* CreateTabValue(
[email protected]fc2b46b2014-05-03 16:33:4596 content::WebContents* web_contents,
[email protected]1c4fbc02013-11-13 02:52:4297 const Extension* extension) {
[email protected]0c9f3262012-09-17 05:59:0698 return CreateTabValue(web_contents, NULL, -1, extension);
99 }
[email protected]ea049a02011-12-25 21:37:09100 static base::DictionaryValue* CreateTabValue(
[email protected]fc2b46b2014-05-03 16:33:45101 content::WebContents* web_contents,
[email protected]ea049a02011-12-25 21:37:09102 TabStripModel* tab_strip,
[email protected]f34706be2012-09-04 07:32:09103 int tab_index,
[email protected]1c4fbc02013-11-13 02:52:42104 const Extension* extension);
[email protected]0c9f3262012-09-17 05:59:06105
[email protected]304fd152013-01-12 16:54:44106 // Creates a Tab object but performs no extension permissions checks; the
107 // returned object will contain privacy-sensitive data.
[email protected]0c9f3262012-09-17 05:59:06108 static base::DictionaryValue* CreateTabValue(
[email protected]fc2b46b2014-05-03 16:33:45109 content::WebContents* web_contents) {
[email protected]304fd152013-01-12 16:54:44110 return CreateTabValue(web_contents, NULL, -1);
[email protected]0c9f3262012-09-17 05:59:06111 }
112 static base::DictionaryValue* CreateTabValue(
[email protected]fc2b46b2014-05-03 16:33:45113 content::WebContents* web_contents,
[email protected]0c9f3262012-09-17 05:59:06114 TabStripModel* tab_strip,
[email protected]304fd152013-01-12 16:54:44115 int tab_index);
116
117 // Removes any privacy-sensitive fields from a Tab object if appropriate,
118 // given the permissions of the extension and the tab in question. The
119 // tab_info object is modified in place.
[email protected]fc2b46b2014-05-03 16:33:45120 static void ScrubTabValueForExtension(content::WebContents* contents,
[email protected]1c4fbc02013-11-13 02:52:42121 const Extension* extension,
[email protected]304fd152013-01-12 16:54:44122 base::DictionaryValue* tab_info);
[email protected]41d9faf2012-02-28 23:46:02123
[email protected]ab3f61412013-01-29 21:55:07124 // Removes any privacy-sensitive fields from a Tab object if appropriate,
125 // given the permissions of the extension in question.
[email protected]1c4fbc02013-11-13 02:52:42126 static void ScrubTabForExtension(const Extension* extension,
127 api::tabs::Tab* tab);
[email protected]ab3f61412013-01-29 21:55:07128
[email protected]ea049a02011-12-25 21:37:09129 // Gets the |tab_strip_model| and |tab_index| for the given |web_contents|.
130 static bool GetTabStripModel(const content::WebContents* web_contents,
[email protected]ac84431b2011-09-27 17:26:11131 TabStripModel** tab_strip_model,
132 int* tab_index);
133 static bool GetDefaultTab(Browser* browser,
[email protected]72f67972012-10-30 18:53:28134 content::WebContents** contents,
[email protected]ac84431b2011-09-27 17:26:11135 int* tab_id);
136 // Any out parameter (|browser|, |tab_strip|, |contents|, & |tab_index|) may
137 // be NULL and will not be set within the function.
wjmaclean5b11eee2014-09-05 00:55:14138 static bool GetTabById(int tab_id,
139 content::BrowserContext* browser_context,
140 bool incognito_enabled,
[email protected]ac84431b2011-09-27 17:26:11141 Browser** browser,
142 TabStripModel** tab_strip,
[email protected]72f67972012-10-30 18:53:28143 content::WebContents** contents,
[email protected]ac84431b2011-09-27 17:26:11144 int* tab_index);
[email protected]45c75e62012-03-21 19:56:35145
146 // Takes |url_string| and returns a GURL which is either valid and absolute
147 // or invalid. If |url_string| is not directly interpretable as a valid (it is
148 // likely a relative URL) an attempt is made to resolve it. |extension| is
149 // provided so it can be resolved relative to its extension base
150 // (chrome-extension://<id>/). Using the source frame url would be more
151 // correct, but because the api shipped with urls resolved relative to their
152 // extension base, we decided it wasn't worth breaking existing extensions to
153 // fix.
154 static GURL ResolvePossiblyRelativeURL(const std::string& url_string,
[email protected]1c4fbc02013-11-13 02:52:42155 const Extension* extension);
[email protected]45c75e62012-03-21 19:56:35156
157 // Returns true if |url| is used for testing crashes.
158 static bool IsCrashURL(const GURL& url);
[email protected]73c1a6842012-07-13 17:39:04159
160 // Opens a tab for the specified |web_contents|.
161 static void CreateTab(content::WebContents* web_contents,
162 const std::string& extension_id,
163 WindowOpenDisposition disposition,
164 const gfx::Rect& initial_pos,
165 bool user_gesture);
[email protected]44e329a2012-07-14 01:13:06166
167 // Executes the specified callback for all tabs in all browser windows.
168 static void ForEachTab(
169 const base::Callback<void(content::WebContents*)>& callback);
[email protected]e9570fdf2012-07-18 20:01:21170
[email protected]1c4fbc02013-11-13 02:52:42171 static WindowController* GetWindowControllerOfTab(
[email protected]e9570fdf2012-07-18 20:01:21172 const content::WebContents* web_contents);
[email protected]695089782013-04-09 16:03:17173
174 // Open the extension's options page.
[email protected]1c4fbc02013-11-13 02:52:42175 static void OpenOptionsPage(const Extension* extension, Browser* browser);
[email protected]ac84431b2011-09-27 17:26:11176};
177
[email protected]1c4fbc02013-11-13 02:52:42178} // namespace extensions
179
180#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_UTIL_H_