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 #include "chrome/browser/extensions/extension_tab_util.h"
7 #include "apps/shell_window.h"
8 #include "apps/shell_window_registry.h"
9 #include "chrome/browser/extensions/api/tabs/tabs_constants.h"
10 #include "chrome/browser/extensions/tab_helper.h"
11 #include "chrome/browser/extensions/window_controller.h"
12 #include "chrome/browser/extensions/window_controller_list.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/sessions/session_id.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/browser_finder.h"
17 #include "chrome/browser/ui/browser_iterator.h"
18 #include "chrome/browser/ui/browser_window.h"
19 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
20 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h"
21 #include "chrome/browser/ui/tabs/tab_strip_model.h"
22 #include "chrome/common/extensions/extension.h"
23 #include "chrome/common/extensions/manifest_url_handler.h"
24 #include "chrome/common/extensions/permissions/permissions_data.h"
25 #include "chrome/common/net/url_fixer_upper.h"
26 #include "chrome/common/url_constants.h"
27 #include "content/public/browser/favicon_status.h"
28 #include "content/public/browser/navigation_entry.h"
29 #include "content/public/browser/web_contents.h"
30 #include "content/public/browser/web_contents_view.h"
31 #include "extensions/common/manifest_constants.h"
32 #include "extensions/common/permissions/api_permission.h"
35 namespace keys = extensions::tabs_constants;
36 namespace tabs = extensions::api::tabs;
38 using apps::ShellWindow;
39 using content::NavigationEntry;
40 using content::WebContents;
41 using extensions::APIPermission;
42 using extensions::Extension;
46 extensions::WindowController* GetShellWindowController(
47 const WebContents* contents) {
48 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext());
49 apps::ShellWindowRegistry* registry =
50 apps::ShellWindowRegistry::Get(profile);
53 ShellWindow* shell_window =
54 registry->GetShellWindowForRenderViewHost(contents->GetRenderViewHost());
57 return extensions::WindowControllerList::GetInstance()->
58 FindWindowById(shell_window->session_id().id());
63 int ExtensionTabUtil::GetWindowId(const Browser* browser) {
64 return browser->session_id().id();
67 int ExtensionTabUtil::GetWindowIdOfTabStripModel(
68 const TabStripModel* tab_strip_model) {
69 for (chrome::BrowserIterator it; !it.done(); it.Next()) {
70 if (it->tab_strip_model() == tab_strip_model)
71 return GetWindowId(*it);
76 int ExtensionTabUtil::GetTabId(const WebContents* web_contents) {
77 return SessionID::IdForTab(web_contents);
80 std::string ExtensionTabUtil::GetTabStatusText(bool is_loading) {
81 return is_loading ? keys::kStatusValueLoading : keys::kStatusValueComplete;
84 int ExtensionTabUtil::GetWindowIdOfTab(const WebContents* web_contents) {
85 return SessionID::IdForWindowContainingTab(web_contents);
88 DictionaryValue* ExtensionTabUtil::CreateTabValue(
89 const WebContents* contents,
90 TabStripModel* tab_strip,
92 const Extension* extension) {
93 // If we have a matching ShellWindow with a controller, get the tab value
94 // from its controller instead.
95 extensions::WindowController* controller = GetShellWindowController(contents);
97 (!extension || controller->IsVisibleToExtension(extension))) {
98 return controller->CreateTabValue(extension, tab_index);
100 DictionaryValue *result = CreateTabValue(contents, tab_strip, tab_index);
101 ScrubTabValueForExtension(contents, extension, result);
105 base::ListValue* ExtensionTabUtil::CreateTabList(
106 const Browser* browser,
107 const Extension* extension) {
108 base::ListValue* tab_list = new base::ListValue();
109 TabStripModel* tab_strip = browser->tab_strip_model();
110 for (int i = 0; i < tab_strip->count(); ++i) {
111 tab_list->Append(CreateTabValue(tab_strip->GetWebContentsAt(i),
120 DictionaryValue* ExtensionTabUtil::CreateTabValue(
121 const WebContents* contents,
122 TabStripModel* tab_strip,
124 // If we have a matching ShellWindow with a controller, get the tab value
125 // from its controller instead.
126 extensions::WindowController* controller = GetShellWindowController(contents);
128 return controller->CreateTabValue(NULL, tab_index);
131 ExtensionTabUtil::GetTabStripModel(contents, &tab_strip, &tab_index);
133 DictionaryValue* result = new DictionaryValue();
134 bool is_loading = contents->IsLoading();
135 result->SetInteger(keys::kIdKey, GetTabId(contents));
136 result->SetInteger(keys::kIndexKey, tab_index);
137 result->SetInteger(keys::kWindowIdKey, GetWindowIdOfTab(contents));
138 result->SetString(keys::kStatusKey, GetTabStatusText(is_loading));
139 result->SetBoolean(keys::kActiveKey,
140 tab_strip && tab_index == tab_strip->active_index());
141 result->SetBoolean(keys::kSelectedKey,
142 tab_strip && tab_index == tab_strip->active_index());
143 result->SetBoolean(keys::kHighlightedKey,
144 tab_strip && tab_strip->IsTabSelected(tab_index));
145 result->SetBoolean(keys::kPinnedKey,
146 tab_strip && tab_strip->IsTabPinned(tab_index));
147 result->SetBoolean(keys::kIncognitoKey,
148 contents->GetBrowserContext()->IsOffTheRecord());
149 result->SetInteger(keys::kWidthKey,
150 contents->GetView()->GetContainerSize().width());
151 result->SetInteger(keys::kHeightKey,
152 contents->GetView()->GetContainerSize().height());
154 // Privacy-sensitive fields: these should be stripped off by
155 // ScrubTabValueForExtension if the extension should not see them.
156 result->SetString(keys::kUrlKey, contents->GetURL().spec());
157 result->SetString(keys::kTitleKey, contents->GetTitle());
159 NavigationEntry* entry = contents->GetController().GetVisibleEntry();
160 if (entry && entry->GetFavicon().valid)
161 result->SetString(keys::kFaviconUrlKey, entry->GetFavicon().url.spec());
165 WebContents* opener = tab_strip->GetOpenerOfWebContentsAt(tab_index);
167 result->SetInteger(keys::kOpenerTabIdKey, GetTabId(opener));
173 void ExtensionTabUtil::ScrubTabValueForExtension(const WebContents* contents,
174 const Extension* extension,
175 DictionaryValue* tab_info) {
176 bool has_permission =
178 extensions::PermissionsData::HasAPIPermissionForTab(
179 extension, GetTabId(contents), APIPermission::kTab);
181 if (!has_permission) {
182 tab_info->Remove(keys::kUrlKey, NULL);
183 tab_info->Remove(keys::kTitleKey, NULL);
184 tab_info->Remove(keys::kFaviconUrlKey, NULL);
188 void ExtensionTabUtil::ScrubTabForExtension(const Extension* extension,
190 bool has_permission = extension && extension->HasAPIPermission(
191 APIPermission::kTab);
193 if (!has_permission) {
196 tab->fav_icon_url.reset();
200 bool ExtensionTabUtil::GetTabStripModel(const WebContents* web_contents,
201 TabStripModel** tab_strip_model,
203 DCHECK(web_contents);
204 DCHECK(tab_strip_model);
207 for (chrome::BrowserIterator it; !it.done(); it.Next()) {
208 TabStripModel* tab_strip = it->tab_strip_model();
209 int index = tab_strip->GetIndexOfWebContents(web_contents);
211 *tab_strip_model = tab_strip;
220 bool ExtensionTabUtil::GetDefaultTab(Browser* browser,
221 WebContents** contents,
226 *contents = browser->tab_strip_model()->GetActiveWebContents();
229 *tab_id = GetTabId(*contents);
236 bool ExtensionTabUtil::GetTabById(int tab_id,
238 bool include_incognito,
240 TabStripModel** tab_strip,
241 WebContents** contents,
243 Profile* incognito_profile =
244 include_incognito && profile->HasOffTheRecordProfile() ?
245 profile->GetOffTheRecordProfile() : NULL;
246 for (chrome::BrowserIterator it; !it.done(); it.Next()) {
247 Browser* target_browser = *it;
248 if (target_browser->profile() == profile ||
249 target_browser->profile() == incognito_profile) {
250 TabStripModel* target_tab_strip = target_browser->tab_strip_model();
251 for (int i = 0; i < target_tab_strip->count(); ++i) {
252 WebContents* target_contents = target_tab_strip->GetWebContentsAt(i);
253 if (SessionID::IdForTab(target_contents) == tab_id) {
255 *browser = target_browser;
257 *tab_strip = target_tab_strip;
259 *contents = target_contents;
270 GURL ExtensionTabUtil::ResolvePossiblyRelativeURL(const std::string& url_string,
271 const extensions::Extension* extension) {
272 GURL url = GURL(url_string);
274 url = extension->GetResourceURL(url_string);
279 bool ExtensionTabUtil::IsCrashURL(const GURL& url) {
280 // Check a fixed-up URL, to normalize the scheme and parse hosts correctly.
282 URLFixerUpper::FixupURL(url.possibly_invalid_spec(), std::string());
283 return (fixed_url.SchemeIs(chrome::kChromeUIScheme) &&
284 (fixed_url.host() == content::kChromeUIBrowserCrashHost ||
285 fixed_url.host() == chrome::kChromeUICrashHost));
288 void ExtensionTabUtil::CreateTab(WebContents* web_contents,
289 const std::string& extension_id,
290 WindowOpenDisposition disposition,
291 const gfx::Rect& initial_pos,
294 Profile::FromBrowserContext(web_contents->GetBrowserContext());
295 chrome::HostDesktopType active_desktop = chrome::GetActiveDesktop();
296 Browser* browser = chrome::FindTabbedBrowser(profile, false, active_desktop);
297 const bool browser_created = !browser;
299 browser = new Browser(Browser::CreateParams(profile, active_desktop));
300 chrome::NavigateParams params(browser, web_contents);
302 // The extension_app_id parameter ends up as app_name in the Browser
303 // which causes the Browser to return true for is_app(). This affects
304 // among other things, whether the location bar gets displayed.
305 // TODO(mpcomplete): This seems wrong. What if the extension content is hosted
307 if (disposition == NEW_POPUP)
308 params.extension_app_id = extension_id;
310 params.disposition = disposition;
311 params.window_bounds = initial_pos;
312 params.window_action = chrome::NavigateParams::SHOW_WINDOW;
313 params.user_gesture = user_gesture;
314 chrome::Navigate(¶ms);
316 // Close the browser if chrome::Navigate created a new one.
317 if (browser_created && (browser != params.browser))
318 browser->window()->Close();
322 void ExtensionTabUtil::ForEachTab(
323 const base::Callback<void(WebContents*)>& callback) {
324 for (TabContentsIterator iterator; !iterator.done(); iterator.Next())
325 callback.Run(*iterator);
329 extensions::WindowController* ExtensionTabUtil::GetWindowControllerOfTab(
330 const WebContents* web_contents) {
331 Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
333 return browser->extension_window_controller();
338 void ExtensionTabUtil::OpenOptionsPage(const Extension* extension,
340 DCHECK(!extensions::ManifestURL::GetOptionsPage(extension).is_empty());
342 // Force the options page to open in non-OTR window, because it won't be
343 // able to save settings from OTR.
344 scoped_ptr<chrome::ScopedTabbedBrowserDisplayer> displayer;
345 if (browser->profile()->IsOffTheRecord()) {
346 displayer.reset(new chrome::ScopedTabbedBrowserDisplayer(
347 browser->profile()->GetOriginalProfile(),
348 browser->host_desktop_type()));
349 browser = displayer->browser();
352 content::OpenURLParams params(
353 extensions::ManifestURL::GetOptionsPage(extension),
354 content::Referrer(), SINGLETON_TAB,
355 content::PAGE_TRANSITION_LINK, false);
356 browser->OpenURL(params);
357 browser->window()->Show();
358 WebContents* web_contents =
359 browser->tab_strip_model()->GetActiveWebContents();
360 web_contents->GetDelegate()->ActivateContents(web_contents);