1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_UTIL_H_
#define CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_UTIL_H_
#include <memory>
#include <string>
#include "base/callback.h"
#include "chrome/common/extensions/api/tabs.h"
#include "ui/base/window_open_disposition.h"
class Browser;
class ChromeExtensionFunctionDetails;
class GURL;
class TabStripModel;
class UIThreadExtensionFunction;
namespace base {
class DictionaryValue;
class ListValue;
}
namespace content {
class BrowserContext;
class WebContents;
}
namespace gfx {
class Rect;
}
namespace extensions {
class Extension;
class WindowController;
// Provides various utility functions that help manipulate tabs.
class ExtensionTabUtil {
public:
enum PopulateTabBehavior {
kPopulateTabs,
kDontPopulateTabs,
};
enum ScrubTabBehavior {
kScrubTab,
kDontScrubTab,
};
struct OpenTabParams {
OpenTabParams();
~OpenTabParams();
bool create_browser_if_needed;
std::unique_ptr<int> window_id;
std::unique_ptr<int> opener_tab_id;
std::unique_ptr<std::string> url;
std::unique_ptr<bool> active;
std::unique_ptr<bool> pinned;
std::unique_ptr<int> index;
};
// Platform specific delegate.
class Delegate {
public:
virtual ~Delegate() {}
// Platform specific scrubbing of tab info for |extension|.
virtual void ScrubTabForExtension(const Extension* extension,
content::WebContents* contents,
api::tabs::Tab* tab) = 0;
};
// Opens a new tab given an extension function |function| and creation
// parameters |params|. Returns a Tab object if successful, or NULL and
// optionally sets |error| if an error occurs.
static base::DictionaryValue* OpenTab(UIThreadExtensionFunction* function,
const OpenTabParams& params,
bool user_gesture,
std::string* error);
static int GetWindowId(const Browser* browser);
static int GetWindowIdOfTabStripModel(const TabStripModel* tab_strip_model);
static int GetTabId(const content::WebContents* web_contents);
static std::string GetTabStatusText(bool is_loading);
static int GetWindowIdOfTab(const content::WebContents* web_contents);
static std::unique_ptr<base::ListValue> CreateTabList(
const Browser* browser,
const Extension* extension);
static Browser* GetBrowserFromWindowID(
const ChromeExtensionFunctionDetails& details,
int window_id,
std::string* error_message);
// Returns the tabs:: API constant for the window type of the |browser|.
static std::string GetBrowserWindowTypeText(const Browser& browser);
// Creates a Tab object (see chrome/common/extensions/api/tabs.json) with
// information about the state of a browser tab for the given |web_contents|.
// This will scrub the tab of sensitive data (URL, favicon, title) according
// to |scrub_tab_behavior| and |extension|'s permissions. A null extension is
// treated as having no permissions.
// By default, tab information should always be scrubbed (kScrubTab) for any
// data passed to any extension.
static std::unique_ptr<api::tabs::Tab> CreateTabObject(
content::WebContents* web_contents,
ScrubTabBehavior scrub_tab_behavior,
const Extension* extension) {
return CreateTabObject(web_contents, scrub_tab_behavior, extension, nullptr,
-1);
}
static std::unique_ptr<api::tabs::Tab> CreateTabObject(
content::WebContents* web_contents,
ScrubTabBehavior scrub_tab_behavior,
const Extension* extension,
TabStripModel* tab_strip,
int tab_index);
// Creates a DictionaryValue representing the window for the given |browser|,
// and scrubs any privacy-sensitive data that |extension| does not have
// access to. |populate_tab_behavior| determines whether tabs will be
// populated in the result.
// TODO(devlin): Convert this to a api::Windows::Window object.
static std::unique_ptr<base::DictionaryValue> CreateWindowValueForExtension(
const Browser& browser,
const Extension* extension,
PopulateTabBehavior populate_tab_behavior);
// Creates a tab MutedInfo object (see chrome/common/extensions/api/tabs.json)
// with information about the mute state of a browser tab.
static std::unique_ptr<api::tabs::MutedInfo> CreateMutedInfo(
content::WebContents* contents);
// Platform specific logic moved to delegate. This should be set during
// startup.
static void SetPlatformDelegate(std::unique_ptr<Delegate> delegate);
// Removes any privacy-sensitive fields from a Tab object if appropriate,
// given the permissions of the extension and the tab in question. The
// tab object is modified in place.
static void ScrubTabForExtension(const Extension* extension,
content::WebContents* contents,
api::tabs::Tab* tab);
// Gets the |tab_strip_model| and |tab_index| for the given |web_contents|.
static bool GetTabStripModel(const content::WebContents* web_contents,
TabStripModel** tab_strip_model,
int* tab_index);
static bool GetDefaultTab(Browser* browser,
content::WebContents** contents,
int* tab_id);
// Any out parameter (|browser|, |tab_strip|, |contents|, & |tab_index|) may
// be NULL and will not be set within the function.
static bool GetTabById(int tab_id,
content::BrowserContext* browser_context,
bool incognito_enabled,
Browser** browser,
TabStripModel** tab_strip,
content::WebContents** contents,
int* tab_index);
// Takes |url_string| and returns a GURL which is either valid and absolute
// or invalid. If |url_string| is not directly interpretable as a valid (it is
// likely a relative URL) an attempt is made to resolve it. |extension| is
// provided so it can be resolved relative to its extension base
// (chrome-extension://<id>/). Using the source frame url would be more
// correct, but because the api shipped with urls resolved relative to their
// extension base, we decided it wasn't worth breaking existing extensions to
// fix.
static GURL ResolvePossiblyRelativeURL(const std::string& url_string,
const Extension* extension);
// Returns true if navigating to |url| would kill a page or the browser
// itself, whether by simulating a crash, browser quit, thread hang, or
// equivalent. Extensions should be prevented from navigating to such URLs.
static bool IsKillURL(const GURL& url);
// Opens a tab for the specified |web_contents|.
static void CreateTab(std::unique_ptr<content::WebContents> web_contents,
const std::string& extension_id,
WindowOpenDisposition disposition,
const gfx::Rect& initial_rect,
bool user_gesture);
// Executes the specified callback for all tabs in all browser windows.
static void ForEachTab(
const base::Callback<void(content::WebContents*)>& callback);
static WindowController* GetWindowControllerOfTab(
const content::WebContents* web_contents);
// Open the extension's options page. Returns true if an options page was
// successfully opened (though it may not necessarily *load*, e.g. if the
// URL does not exist). This call to open the options page is iniatiated by
// the extension via chrome.runtime.openOptionsPage.
static bool OpenOptionsPageFromAPI(const Extension* extension,
content::BrowserContext* browser_context);
// Open the extension's options page. Returns true if an options page was
// successfully opened (though it may not necessarily *load*, e.g. if the
// URL does not exist).
static bool OpenOptionsPage(const Extension* extension, Browser* browser);
// Returns true if the given Browser can report tabs to extensions.
// Example of Browsers which don't support tabs include apps and devtools.
static bool BrowserSupportsTabs(Browser* browser);
};
} // namespace extensions
#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_UTIL_H_
|