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.
4
5#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_UTIL_H__
6#define CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_UTIL_H__
7
8#include <string>
9
10#include "base/callback.h"
11#include "chrome/common/extensions/api/tabs.h"
12#include "ui/base/window_open_disposition.h"
13
14class Browser;
15class GURL;
16class Profile;
17class TabStripModel;
18
19namespace base {
20class DictionaryValue;
21class ListValue;
22}
23
24namespace content {
25class WebContents;
26}
27
28namespace extensions {
29class Extension;
30class WindowController;
31}
32
33namespace gfx {
34class Rect;
35}
36
37// Provides various utility functions that help manipulate tabs.
38class ExtensionTabUtil {
39 public:
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);
48
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);
57  }
58  static base::DictionaryValue* CreateTabValue(
59      const content::WebContents* web_contents,
60      TabStripModel* tab_strip,
61      int tab_index,
62      const extensions::Extension* extension);
63
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);
69  }
70  static base::DictionaryValue* CreateTabValue(
71      const content::WebContents* web_contents,
72      TabStripModel* tab_strip,
73      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);
81
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);
86
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,
90                               int* tab_index);
91  static bool GetDefaultTab(Browser* browser,
92                            content::WebContents** contents,
93                            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,
99                         content::WebContents** contents,
100                         int* tab_index);
101
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,
111      const extensions::Extension* extension);
112
113  // Returns true if |url| is used for testing crashes.
114  static bool IsCrashURL(const GURL& url);
115
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);
122
123  // Executes the specified callback for all tabs in all browser windows.
124  static void ForEachTab(
125      const base::Callback<void(content::WebContents*)>& callback);
126
127  static extensions::WindowController* GetWindowControllerOfTab(
128      const content::WebContents* web_contents);
129
130  // Open the extension's options page.
131  static void OpenOptionsPage(const extensions::Extension* extension,
132                              Browser* browser);
133};
134
135#endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_UTIL_H__
136