blob: bbce801089b181726f4f263c00ec207a23a78122 [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
5#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]ab3f61412013-01-29 21:55:0711#include "chrome/common/extensions/api/tabs.h"
[email protected]f47621b2013-01-22 20:50:3312#include "ui/base/window_open_disposition.h"
[email protected]73c1a6842012-07-13 17:39:0413
[email protected]ac84431b2011-09-27 17:26:1114class Browser;
[email protected]45c75e62012-03-21 19:56:3515class GURL;
[email protected]ac84431b2011-09-27 17:26:1116class Profile;
[email protected]ac84431b2011-09-27 17:26:1117class TabStripModel;
18
19namespace base {
20class DictionaryValue;
21class ListValue;
22}
23
[email protected]26b5e322011-12-23 01:36:4724namespace content {
25class WebContents;
26}
27
[email protected]1c321ee2012-05-21 03:02:3428namespace extensions {
29class Extension;
[email protected]e9570fdf2012-07-18 20:01:2130class WindowController;
[email protected]1c321ee2012-05-21 03:02:3431}
32
[email protected]73c1a6842012-07-13 17:39:0433namespace gfx {
34class Rect;
35}
36
[email protected]ac84431b2011-09-27 17:26:1137// Provides various utility functions that help manipulate tabs.
38class ExtensionTabUtil {
39 public:
40 static int GetWindowId(const Browser* browser);
[email protected]8c3495c2011-09-28 03:32:3041 static int GetWindowIdOfTabStripModel(const TabStripModel* tab_strip_model);
[email protected]26b5e322011-12-23 01:36:4742 static int GetTabId(const content::WebContents* web_contents);
[email protected]ac84431b2011-09-27 17:26:1143 static std::string GetTabStatusText(bool is_loading);
[email protected]ea049a02011-12-25 21:37:0944 static int GetWindowIdOfTab(const content::WebContents* web_contents);
[email protected]f34706be2012-09-04 07:32:0945 static base::ListValue* CreateTabList(
46 const Browser* browser,
47 const extensions::Extension* extension);
[email protected]0c9f3262012-09-17 05:59:0648
[email protected]304fd152013-01-12 16:54:4449 // 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.
[email protected]ac84431b2011-09-27 17:26:1153 static base::DictionaryValue* CreateTabValue(
[email protected]f34706be2012-09-04 07:32:0954 const content::WebContents* web_contents,
[email protected]0c9f3262012-09-17 05:59:0655 const extensions::Extension* extension) {
56 return CreateTabValue(web_contents, NULL, -1, extension);
57 }
[email protected]ea049a02011-12-25 21:37:0958 static base::DictionaryValue* CreateTabValue(
59 const content::WebContents* web_contents,
60 TabStripModel* tab_strip,
[email protected]f34706be2012-09-04 07:32:0961 int tab_index,
62 const extensions::Extension* extension);
[email protected]0c9f3262012-09-17 05:59:0663
[email protected]304fd152013-01-12 16:54:4464 // Creates a Tab object but performs no extension permissions checks; the
65 // returned object will contain privacy-sensitive data.
[email protected]0c9f3262012-09-17 05:59:0666 static base::DictionaryValue* CreateTabValue(
[email protected]304fd152013-01-12 16:54:4467 const content::WebContents* web_contents) {
68 return CreateTabValue(web_contents, NULL, -1);
[email protected]0c9f3262012-09-17 05:59:0669 }
70 static base::DictionaryValue* CreateTabValue(
71 const content::WebContents* web_contents,
72 TabStripModel* tab_strip,
[email protected]304fd152013-01-12 16:54:4473 int tab_index);
74
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);
[email protected]41d9faf2012-02-28 23:46:0281
[email protected]ab3f61412013-01-29 21:55:0782 // 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);
86
[email protected]ea049a02011-12-25 21:37:0987 // Gets the |tab_strip_model| and |tab_index| for the given |web_contents|.
88 static bool GetTabStripModel(const content::WebContents* web_contents,
[email protected]ac84431b2011-09-27 17:26:1189 TabStripModel** tab_strip_model,
90 int* tab_index);
91 static bool GetDefaultTab(Browser* browser,
[email protected]72f67972012-10-30 18:53:2892 content::WebContents** contents,
[email protected]ac84431b2011-09-27 17:26:1193 int* tab_id);
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,
97 Browser** browser,
98 TabStripModel** tab_strip,
[email protected]72f67972012-10-30 18:53:2899 content::WebContents** contents,
[email protected]ac84431b2011-09-27 17:26:11100 int* tab_index);
[email protected]45c75e62012-03-21 19:56:35101
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
109 // fix.
110 static GURL ResolvePossiblyRelativeURL(const std::string& url_string,
[email protected]1c321ee2012-05-21 03:02:34111 const extensions::Extension* extension);
[email protected]45c75e62012-03-21 19:56:35112
113 // Returns true if |url| is used for testing crashes.
114 static bool IsCrashURL(const GURL& url);
[email protected]73c1a6842012-07-13 17:39:04115
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,
121 bool user_gesture);
[email protected]44e329a2012-07-14 01:13:06122
123 // Executes the specified callback for all tabs in all browser windows.
124 static void ForEachTab(
125 const base::Callback<void(content::WebContents*)>& callback);
[email protected]e9570fdf2012-07-18 20:01:21126
127 static extensions::WindowController* GetWindowControllerOfTab(
128 const content::WebContents* web_contents);
[email protected]ac84431b2011-09-27 17:26:11129};
130
131#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_UTIL_H__