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/tab_contents/tab_contents_iterator.h"
20 #include "chrome/browser/ui/tabs/tab_strip_model.h"
21 #include "chrome/common/extensions/extension.h"
22 #include "chrome/common/extensions/manifest_url_handler.h"
23 #include "chrome/common/extensions/permissions/api_permission.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"
34 namespace keys
= extensions::tabs_constants
;
35 namespace tabs
= extensions::api::tabs
;
37 using apps::ShellWindow
;
38 using content::NavigationEntry
;
39 using content::WebContents
;
40 using extensions::APIPermission
;
41 using extensions::Extension
;
45 extensions::WindowController
* GetShellWindowController(
46 const WebContents
* contents
) {
47 Profile
* profile
= Profile::FromBrowserContext(contents
->GetBrowserContext());
48 apps::ShellWindowRegistry
* registry
=
49 apps::ShellWindowRegistry::Get(profile
);
52 ShellWindow
* shell_window
=
53 registry
->GetShellWindowForRenderViewHost(contents
->GetRenderViewHost());
56 return extensions::WindowControllerList::GetInstance()->
57 FindWindowById(shell_window
->session_id().id());
62 int ExtensionTabUtil::GetWindowId(const Browser
* browser
) {
63 return browser
->session_id().id();
66 int ExtensionTabUtil::GetWindowIdOfTabStripModel(
67 const TabStripModel
* tab_strip_model
) {
68 for (chrome::BrowserIterator it
; !it
.done(); it
.Next()) {
69 if (it
->tab_strip_model() == tab_strip_model
)
70 return GetWindowId(*it
);
75 int ExtensionTabUtil::GetTabId(const WebContents
* web_contents
) {
76 return SessionID::IdForTab(web_contents
);
79 std::string
ExtensionTabUtil::GetTabStatusText(bool is_loading
) {
80 return is_loading
? keys::kStatusValueLoading
: keys::kStatusValueComplete
;
83 int ExtensionTabUtil::GetWindowIdOfTab(const WebContents
* web_contents
) {
84 return SessionID::IdForWindowContainingTab(web_contents
);
87 DictionaryValue
* ExtensionTabUtil::CreateTabValue(
88 const WebContents
* contents
,
89 TabStripModel
* tab_strip
,
91 const Extension
* extension
) {
92 // If we have a matching ShellWindow with a controller, get the tab value
93 // from its controller instead.
94 extensions::WindowController
* controller
= GetShellWindowController(contents
);
96 (!extension
|| controller
->IsVisibleToExtension(extension
))) {
97 return controller
->CreateTabValue(extension
, tab_index
);
99 DictionaryValue
*result
= CreateTabValue(contents
, tab_strip
, tab_index
);
100 ScrubTabValueForExtension(contents
, extension
, result
);
104 base::ListValue
* ExtensionTabUtil::CreateTabList(
105 const Browser
* browser
,
106 const Extension
* extension
) {
107 base::ListValue
* tab_list
= new base::ListValue();
108 TabStripModel
* tab_strip
= browser
->tab_strip_model();
109 for (int i
= 0; i
< tab_strip
->count(); ++i
) {
110 tab_list
->Append(CreateTabValue(tab_strip
->GetWebContentsAt(i
),
119 DictionaryValue
* ExtensionTabUtil::CreateTabValue(
120 const WebContents
* contents
,
121 TabStripModel
* tab_strip
,
123 // If we have a matching ShellWindow with a controller, get the tab value
124 // from its controller instead.
125 extensions::WindowController
* controller
= GetShellWindowController(contents
);
127 return controller
->CreateTabValue(NULL
, tab_index
);
130 ExtensionTabUtil::GetTabStripModel(contents
, &tab_strip
, &tab_index
);
132 DictionaryValue
* result
= new DictionaryValue();
133 bool is_loading
= contents
->IsLoading();
134 result
->SetInteger(keys::kIdKey
, GetTabId(contents
));
135 result
->SetInteger(keys::kIndexKey
, tab_index
);
136 result
->SetInteger(keys::kWindowIdKey
, GetWindowIdOfTab(contents
));
137 result
->SetString(keys::kStatusKey
, GetTabStatusText(is_loading
));
138 result
->SetBoolean(keys::kActiveKey
,
139 tab_strip
&& tab_index
== tab_strip
->active_index());
140 result
->SetBoolean(keys::kSelectedKey
,
141 tab_strip
&& tab_index
== tab_strip
->active_index());
142 result
->SetBoolean(keys::kHighlightedKey
,
143 tab_strip
&& tab_strip
->IsTabSelected(tab_index
));
144 result
->SetBoolean(keys::kPinnedKey
,
145 tab_strip
&& tab_strip
->IsTabPinned(tab_index
));
146 result
->SetBoolean(keys::kIncognitoKey
,
147 contents
->GetBrowserContext()->IsOffTheRecord());
148 result
->SetInteger(keys::kWidthKey
,
149 contents
->GetView()->GetContainerSize().width());
150 result
->SetInteger(keys::kHeightKey
,
151 contents
->GetView()->GetContainerSize().height());
153 // Privacy-sensitive fields: these should be stripped off by
154 // ScrubTabValueForExtension if the extension should not see them.
155 result
->SetString(keys::kUrlKey
, contents
->GetURL().spec());
156 result
->SetString(keys::kTitleKey
, contents
->GetTitle());
158 NavigationEntry
* entry
= contents
->GetController().GetVisibleEntry();
159 if (entry
&& entry
->GetFavicon().valid
)
160 result
->SetString(keys::kFaviconUrlKey
, entry
->GetFavicon().url
.spec());
164 WebContents
* opener
= tab_strip
->GetOpenerOfWebContentsAt(tab_index
);
166 result
->SetInteger(keys::kOpenerTabIdKey
, GetTabId(opener
));
172 void ExtensionTabUtil::ScrubTabValueForExtension(const WebContents
* contents
,
173 const Extension
* extension
,
174 DictionaryValue
* tab_info
) {
175 bool has_permission
=
177 extensions::PermissionsData::HasAPIPermissionForTab(
178 extension
, GetTabId(contents
), APIPermission::kTab
);
180 if (!has_permission
) {
181 tab_info
->Remove(keys::kUrlKey
, NULL
);
182 tab_info
->Remove(keys::kTitleKey
, NULL
);
183 tab_info
->Remove(keys::kFaviconUrlKey
, NULL
);
187 void ExtensionTabUtil::ScrubTabForExtension(const Extension
* extension
,
189 bool has_permission
= extension
&& extension
->HasAPIPermission(
190 APIPermission::kTab
);
192 if (!has_permission
) {
195 tab
->fav_icon_url
.reset();
199 bool ExtensionTabUtil::GetTabStripModel(const WebContents
* web_contents
,
200 TabStripModel
** tab_strip_model
,
202 DCHECK(web_contents
);
203 DCHECK(tab_strip_model
);
206 for (chrome::BrowserIterator it
; !it
.done(); it
.Next()) {
207 TabStripModel
* tab_strip
= it
->tab_strip_model();
208 int index
= tab_strip
->GetIndexOfWebContents(web_contents
);
210 *tab_strip_model
= tab_strip
;
219 bool ExtensionTabUtil::GetDefaultTab(Browser
* browser
,
220 WebContents
** contents
,
225 *contents
= browser
->tab_strip_model()->GetActiveWebContents();
228 *tab_id
= GetTabId(*contents
);
235 bool ExtensionTabUtil::GetTabById(int tab_id
,
237 bool include_incognito
,
239 TabStripModel
** tab_strip
,
240 WebContents
** contents
,
242 Profile
* incognito_profile
=
243 include_incognito
&& profile
->HasOffTheRecordProfile() ?
244 profile
->GetOffTheRecordProfile() : NULL
;
245 for (chrome::BrowserIterator it
; !it
.done(); it
.Next()) {
246 Browser
* target_browser
= *it
;
247 if (target_browser
->profile() == profile
||
248 target_browser
->profile() == incognito_profile
) {
249 TabStripModel
* target_tab_strip
= target_browser
->tab_strip_model();
250 for (int i
= 0; i
< target_tab_strip
->count(); ++i
) {
251 WebContents
* target_contents
= target_tab_strip
->GetWebContentsAt(i
);
252 if (SessionID::IdForTab(target_contents
) == tab_id
) {
254 *browser
= target_browser
;
256 *tab_strip
= target_tab_strip
;
258 *contents
= target_contents
;
269 GURL
ExtensionTabUtil::ResolvePossiblyRelativeURL(const std::string
& url_string
,
270 const extensions::Extension
* extension
) {
271 GURL url
= GURL(url_string
);
273 url
= extension
->GetResourceURL(url_string
);
278 bool ExtensionTabUtil::IsCrashURL(const GURL
& url
) {
279 // Check a fixed-up URL, to normalize the scheme and parse hosts correctly.
281 URLFixerUpper::FixupURL(url
.possibly_invalid_spec(), std::string());
282 return (fixed_url
.SchemeIs(chrome::kChromeUIScheme
) &&
283 (fixed_url
.host() == content::kChromeUIBrowserCrashHost
||
284 fixed_url
.host() == chrome::kChromeUICrashHost
));
287 void ExtensionTabUtil::CreateTab(WebContents
* web_contents
,
288 const std::string
& extension_id
,
289 WindowOpenDisposition disposition
,
290 const gfx::Rect
& initial_pos
,
293 Profile::FromBrowserContext(web_contents
->GetBrowserContext());
294 chrome::HostDesktopType active_desktop
= chrome::GetActiveDesktop();
295 Browser
* browser
= chrome::FindTabbedBrowser(profile
, false, active_desktop
);
296 const bool browser_created
= !browser
;
298 browser
= new Browser(Browser::CreateParams(profile
, active_desktop
));
299 chrome::NavigateParams
params(browser
, web_contents
);
301 // The extension_app_id parameter ends up as app_name in the Browser
302 // which causes the Browser to return true for is_app(). This affects
303 // among other things, whether the location bar gets displayed.
304 // TODO(mpcomplete): This seems wrong. What if the extension content is hosted
306 if (disposition
== NEW_POPUP
)
307 params
.extension_app_id
= extension_id
;
309 params
.disposition
= disposition
;
310 params
.window_bounds
= initial_pos
;
311 params
.window_action
= chrome::NavigateParams::SHOW_WINDOW
;
312 params
.user_gesture
= user_gesture
;
313 chrome::Navigate(¶ms
);
315 // Close the browser if chrome::Navigate created a new one.
316 if (browser_created
&& (browser
!= params
.browser
))
317 browser
->window()->Close();
321 void ExtensionTabUtil::ForEachTab(
322 const base::Callback
<void(WebContents
*)>& callback
) {
323 for (TabContentsIterator iterator
; !iterator
.done(); iterator
.Next())
324 callback
.Run(*iterator
);
328 extensions::WindowController
* ExtensionTabUtil::GetWindowControllerOfTab(
329 const WebContents
* web_contents
) {
330 Browser
* browser
= chrome::FindBrowserWithWebContents(web_contents
);
332 return browser
->extension_window_controller();
337 void ExtensionTabUtil::OpenOptionsPage(const Extension
* extension
,
339 DCHECK(!extensions::ManifestURL::GetOptionsPage(extension
).is_empty());
341 // Force the options page to open in non-OTR window, because it won't be
342 // able to save settings from OTR.
343 if (browser
->profile()->IsOffTheRecord()) {
344 browser
= chrome::FindOrCreateTabbedBrowser(
345 browser
->profile()->GetOriginalProfile(), browser
->host_desktop_type());
348 content::OpenURLParams
params(
349 extensions::ManifestURL::GetOptionsPage(extension
),
350 content::Referrer(), SINGLETON_TAB
,
351 content::PAGE_TRANSITION_LINK
, false);
352 browser
->OpenURL(params
);
353 browser
->window()->Show();
354 WebContents
* web_contents
=
355 browser
->tab_strip_model()->GetActiveWebContents();
356 web_contents
->GetDelegate()->ActivateContents(web_contents
);