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/app_window.h"
8 #include "apps/app_window_registry.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "chrome/browser/extensions/api/tabs/tabs_constants.h"
11 #include "chrome/browser/extensions/chrome_extension_function.h"
12 #include "chrome/browser/extensions/tab_helper.h"
13 #include "chrome/browser/extensions/window_controller.h"
14 #include "chrome/browser/extensions/window_controller_list.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/sessions/session_id.h"
17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/browser/ui/browser_finder.h"
19 #include "chrome/browser/ui/browser_iterator.h"
20 #include "chrome/browser/ui/browser_window.h"
21 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
22 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h"
23 #include "chrome/browser/ui/tabs/tab_strip_model.h"
24 #include "chrome/common/extensions/manifest_url_handler.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 "extensions/common/constants.h"
31 #include "extensions/common/error_utils.h"
32 #include "extensions/common/extension.h"
33 #include "extensions/common/manifest_constants.h"
34 #include "extensions/common/manifest_handlers/incognito_info.h"
35 #include "extensions/common/permissions/api_permission.h"
36 #include "extensions/common/permissions/permissions_data.h"
39 using apps::AppWindow
;
40 using content::NavigationEntry
;
41 using content::WebContents
;
43 namespace extensions
{
47 namespace keys
= tabs_constants
;
49 WindowController
* GetAppWindowController(const WebContents
* contents
) {
50 Profile
* profile
= Profile::FromBrowserContext(contents
->GetBrowserContext());
51 apps::AppWindowRegistry
* registry
= apps::AppWindowRegistry::Get(profile
);
54 AppWindow
* app_window
=
55 registry
->GetAppWindowForRenderViewHost(contents
->GetRenderViewHost());
58 return WindowControllerList::GetInstance()->FindWindowById(
59 app_window
->session_id().id());
62 // |error_message| can optionally be passed in and will be set with an
63 // appropriate message if the window cannot be found by id.
64 Browser
* GetBrowserInProfileWithId(Profile
* profile
,
66 bool include_incognito
,
67 std::string
* error_message
) {
68 Profile
* incognito_profile
=
69 include_incognito
&& profile
->HasOffTheRecordProfile()
70 ? profile
->GetOffTheRecordProfile()
72 for (chrome::BrowserIterator it
; !it
.done(); it
.Next()) {
73 Browser
* browser
= *it
;
74 if ((browser
->profile() == profile
||
75 browser
->profile() == incognito_profile
) &&
76 ExtensionTabUtil::GetWindowId(browser
) == window_id
&&
83 *error_message
= ErrorUtils::FormatErrorMessage(
84 keys::kWindowNotFoundError
, base::IntToString(window_id
));
89 Browser
* CreateBrowser(ChromeUIThreadExtensionFunction
* function
,
92 content::WebContents
* web_contents
= function
->GetAssociatedWebContents();
94 DCHECK(web_contents
->GetNativeView());
95 DCHECK(!chrome::FindBrowserWithWebContents(web_contents
));
97 chrome::HostDesktopType desktop_type
=
98 chrome::GetHostDesktopTypeForNativeView(web_contents
->GetNativeView());
99 Browser::CreateParams
params(
100 Browser::TYPE_TABBED
, function
->GetProfile(), desktop_type
);
101 Browser
* browser
= new Browser(params
);
102 browser
->window()->Show();
108 ExtensionTabUtil::OpenTabParams::OpenTabParams()
109 : create_browser_if_needed(false) {
112 ExtensionTabUtil::OpenTabParams::~OpenTabParams() {
115 // Opens a new tab for a given extension. Returns NULL and sets |error| if an
117 base::DictionaryValue
* ExtensionTabUtil::OpenTab(
118 ChromeUIThreadExtensionFunction
* function
,
119 const OpenTabParams
& params
,
120 std::string
* error
) {
121 // windowId defaults to "current" window.
122 int window_id
= extension_misc::kCurrentWindowId
;
123 if (params
.window_id
.get())
124 window_id
= *params
.window_id
;
126 Browser
* browser
= GetBrowserFromWindowID(function
, window_id
, error
);
128 if (!params
.create_browser_if_needed
) {
131 browser
= CreateBrowser(function
, window_id
, error
);
136 // Ensure the selected browser is tabbed.
137 if (!browser
->is_type_tabbed() && browser
->IsAttemptingToCloseBrowser())
138 browser
= chrome::FindTabbedBrowser(function
->GetProfile(),
139 function
->include_incognito(),
140 browser
->host_desktop_type());
142 if (!browser
|| !browser
->window()) {
143 // TODO(rpaquay): Error message?
147 // TODO(jstritar): Add a constant, chrome.tabs.TAB_ID_ACTIVE, that
148 // represents the active tab.
149 WebContents
* opener
= NULL
;
150 if (params
.opener_tab_id
.get()) {
151 int opener_id
= *params
.opener_tab_id
;
153 if (!ExtensionTabUtil::GetTabById(opener_id
,
154 function
->GetProfile(),
155 function
->include_incognito(),
160 // TODO(rpaquay): Error message?
165 // TODO(rafaelw): handle setting remaining tab properties:
170 if (params
.url
.get()) {
171 std::string url_string
= *params
.url
;
172 url
= ExtensionTabUtil::ResolvePossiblyRelativeURL(
173 url_string
, function
->GetExtension());
174 if (!url
.is_valid()) {
176 ErrorUtils::FormatErrorMessage(keys::kInvalidUrlError
, url_string
);
180 url
= GURL(chrome::kChromeUINewTabURL
);
183 // Don't let extensions crash the browser or renderers.
184 if (ExtensionTabUtil::IsCrashURL(url
)) {
185 *error
= keys::kNoCrashBrowserError
;
189 // Default to foreground for the new tab. The presence of 'active' property
190 // will override this default.
192 if (params
.active
.get())
193 active
= *params
.active
;
195 // Default to not pinning the tab. Setting the 'pinned' property to true
196 // will override this default.
198 if (params
.pinned
.get())
199 pinned
= *params
.pinned
;
201 // We can't load extension URLs into incognito windows unless the extension
202 // uses split mode. Special case to fall back to a tabbed window.
203 if (url
.SchemeIs(kExtensionScheme
) &&
204 !IncognitoInfo::IsSplitMode(function
->GetExtension()) &&
205 browser
->profile()->IsOffTheRecord()) {
206 Profile
* profile
= browser
->profile()->GetOriginalProfile();
207 chrome::HostDesktopType desktop_type
= browser
->host_desktop_type();
209 browser
= chrome::FindTabbedBrowser(profile
, false, desktop_type
);
211 browser
= new Browser(
212 Browser::CreateParams(Browser::TYPE_TABBED
, profile
, desktop_type
));
213 browser
->window()->Show();
217 // If index is specified, honor the value, but keep it bound to
218 // -1 <= index <= tab_strip->count() where -1 invokes the default behavior.
220 if (params
.index
.get())
221 index
= *params
.index
;
223 TabStripModel
* tab_strip
= browser
->tab_strip_model();
225 index
= std::min(std::max(index
, -1), tab_strip
->count());
227 int add_types
= active
? TabStripModel::ADD_ACTIVE
: TabStripModel::ADD_NONE
;
228 add_types
|= TabStripModel::ADD_FORCE_INDEX
;
230 add_types
|= TabStripModel::ADD_PINNED
;
231 chrome::NavigateParams
navigate_params(
232 browser
, url
, content::PAGE_TRANSITION_LINK
);
233 navigate_params
.disposition
=
234 active
? NEW_FOREGROUND_TAB
: NEW_BACKGROUND_TAB
;
235 navigate_params
.tabstrip_index
= index
;
236 navigate_params
.tabstrip_add_types
= add_types
;
237 chrome::Navigate(&navigate_params
);
239 // The tab may have been created in a different window, so make sure we look
240 // at the right tab strip.
241 tab_strip
= navigate_params
.browser
->tab_strip_model();
243 tab_strip
->GetIndexOfWebContents(navigate_params
.target_contents
);
245 tab_strip
->SetOpenerOfWebContentsAt(new_index
, opener
);
248 navigate_params
.target_contents
->SetInitialFocus();
250 // Return data about the newly created tab.
251 return ExtensionTabUtil::CreateTabValue(navigate_params
.target_contents
,
254 function
->GetExtension());
257 Browser
* ExtensionTabUtil::GetBrowserFromWindowID(
258 ChromeUIThreadExtensionFunction
* function
,
260 std::string
* error
) {
261 if (window_id
== extension_misc::kCurrentWindowId
) {
262 Browser
* result
= function
->GetCurrentBrowser();
263 if (!result
|| !result
->window()) {
265 *error
= keys::kNoCurrentWindowError
;
270 return GetBrowserInProfileWithId(function
->GetProfile(),
272 function
->include_incognito(),
277 int ExtensionTabUtil::GetWindowId(const Browser
* browser
) {
278 return browser
->session_id().id();
281 int ExtensionTabUtil::GetWindowIdOfTabStripModel(
282 const TabStripModel
* tab_strip_model
) {
283 for (chrome::BrowserIterator it
; !it
.done(); it
.Next()) {
284 if (it
->tab_strip_model() == tab_strip_model
)
285 return GetWindowId(*it
);
290 int ExtensionTabUtil::GetTabId(WebContents
* web_contents
) {
291 return SessionID::IdForTab(web_contents
);
294 std::string
ExtensionTabUtil::GetTabStatusText(bool is_loading
) {
295 return is_loading
? keys::kStatusValueLoading
: keys::kStatusValueComplete
;
298 int ExtensionTabUtil::GetWindowIdOfTab(const WebContents
* web_contents
) {
299 return SessionID::IdForWindowContainingTab(web_contents
);
302 base::DictionaryValue
* ExtensionTabUtil::CreateTabValue(
303 WebContents
* contents
,
304 TabStripModel
* tab_strip
,
306 const Extension
* extension
) {
307 // If we have a matching AppWindow with a controller, get the tab value
308 // from its controller instead.
309 WindowController
* controller
= GetAppWindowController(contents
);
311 (!extension
|| controller
->IsVisibleToExtension(extension
))) {
312 return controller
->CreateTabValue(extension
, tab_index
);
314 base::DictionaryValue
* result
=
315 CreateTabValue(contents
, tab_strip
, tab_index
);
316 ScrubTabValueForExtension(contents
, extension
, result
);
320 base::ListValue
* ExtensionTabUtil::CreateTabList(
321 const Browser
* browser
,
322 const Extension
* extension
) {
323 base::ListValue
* tab_list
= new base::ListValue();
324 TabStripModel
* tab_strip
= browser
->tab_strip_model();
325 for (int i
= 0; i
< tab_strip
->count(); ++i
) {
326 tab_list
->Append(CreateTabValue(tab_strip
->GetWebContentsAt(i
),
335 base::DictionaryValue
* ExtensionTabUtil::CreateTabValue(
336 WebContents
* contents
,
337 TabStripModel
* tab_strip
,
339 // If we have a matching AppWindow with a controller, get the tab value
340 // from its controller instead.
341 WindowController
* controller
= GetAppWindowController(contents
);
343 return controller
->CreateTabValue(NULL
, tab_index
);
346 ExtensionTabUtil::GetTabStripModel(contents
, &tab_strip
, &tab_index
);
348 base::DictionaryValue
* result
= new base::DictionaryValue();
349 bool is_loading
= contents
->IsLoading();
350 result
->SetInteger(keys::kIdKey
, GetTabId(contents
));
351 result
->SetInteger(keys::kIndexKey
, tab_index
);
352 result
->SetInteger(keys::kWindowIdKey
, GetWindowIdOfTab(contents
));
353 result
->SetString(keys::kStatusKey
, GetTabStatusText(is_loading
));
354 result
->SetBoolean(keys::kActiveKey
,
355 tab_strip
&& tab_index
== tab_strip
->active_index());
356 result
->SetBoolean(keys::kSelectedKey
,
357 tab_strip
&& tab_index
== tab_strip
->active_index());
358 result
->SetBoolean(keys::kHighlightedKey
,
359 tab_strip
&& tab_strip
->IsTabSelected(tab_index
));
360 result
->SetBoolean(keys::kPinnedKey
,
361 tab_strip
&& tab_strip
->IsTabPinned(tab_index
));
362 result
->SetBoolean(keys::kIncognitoKey
,
363 contents
->GetBrowserContext()->IsOffTheRecord());
364 result
->SetInteger(keys::kWidthKey
,
365 contents
->GetContainerBounds().size().width());
366 result
->SetInteger(keys::kHeightKey
,
367 contents
->GetContainerBounds().size().height());
369 // Privacy-sensitive fields: these should be stripped off by
370 // ScrubTabValueForExtension if the extension should not see them.
371 result
->SetString(keys::kUrlKey
, contents
->GetURL().spec());
372 result
->SetString(keys::kTitleKey
, contents
->GetTitle());
374 NavigationEntry
* entry
= contents
->GetController().GetVisibleEntry();
375 if (entry
&& entry
->GetFavicon().valid
)
376 result
->SetString(keys::kFaviconUrlKey
, entry
->GetFavicon().url
.spec());
380 WebContents
* opener
= tab_strip
->GetOpenerOfWebContentsAt(tab_index
);
382 result
->SetInteger(keys::kOpenerTabIdKey
, GetTabId(opener
));
388 void ExtensionTabUtil::ScrubTabValueForExtension(
389 WebContents
* contents
,
390 const Extension
* extension
,
391 base::DictionaryValue
* tab_info
) {
392 bool has_permission
=
394 PermissionsData::HasAPIPermissionForTab(
395 extension
, GetTabId(contents
), APIPermission::kTab
);
397 if (!has_permission
) {
398 tab_info
->Remove(keys::kUrlKey
, NULL
);
399 tab_info
->Remove(keys::kTitleKey
, NULL
);
400 tab_info
->Remove(keys::kFaviconUrlKey
, NULL
);
404 void ExtensionTabUtil::ScrubTabForExtension(const Extension
* extension
,
405 api::tabs::Tab
* tab
) {
406 bool has_permission
= extension
&& extension
->HasAPIPermission(
407 APIPermission::kTab
);
409 if (!has_permission
) {
412 tab
->fav_icon_url
.reset();
416 bool ExtensionTabUtil::GetTabStripModel(const WebContents
* web_contents
,
417 TabStripModel
** tab_strip_model
,
419 DCHECK(web_contents
);
420 DCHECK(tab_strip_model
);
423 for (chrome::BrowserIterator it
; !it
.done(); it
.Next()) {
424 TabStripModel
* tab_strip
= it
->tab_strip_model();
425 int index
= tab_strip
->GetIndexOfWebContents(web_contents
);
427 *tab_strip_model
= tab_strip
;
436 bool ExtensionTabUtil::GetDefaultTab(Browser
* browser
,
437 WebContents
** contents
,
442 *contents
= browser
->tab_strip_model()->GetActiveWebContents();
445 *tab_id
= GetTabId(*contents
);
452 bool ExtensionTabUtil::GetTabById(int tab_id
,
454 bool include_incognito
,
456 TabStripModel
** tab_strip
,
457 WebContents
** contents
,
459 Profile
* incognito_profile
=
460 include_incognito
&& profile
->HasOffTheRecordProfile() ?
461 profile
->GetOffTheRecordProfile() : NULL
;
462 for (chrome::BrowserIterator it
; !it
.done(); it
.Next()) {
463 Browser
* target_browser
= *it
;
464 if (target_browser
->profile() == profile
||
465 target_browser
->profile() == incognito_profile
) {
466 TabStripModel
* target_tab_strip
= target_browser
->tab_strip_model();
467 for (int i
= 0; i
< target_tab_strip
->count(); ++i
) {
468 WebContents
* target_contents
= target_tab_strip
->GetWebContentsAt(i
);
469 if (SessionID::IdForTab(target_contents
) == tab_id
) {
471 *browser
= target_browser
;
473 *tab_strip
= target_tab_strip
;
475 *contents
= target_contents
;
486 GURL
ExtensionTabUtil::ResolvePossiblyRelativeURL(const std::string
& url_string
,
487 const Extension
* extension
) {
488 GURL url
= GURL(url_string
);
490 url
= extension
->GetResourceURL(url_string
);
495 bool ExtensionTabUtil::IsCrashURL(const GURL
& url
) {
496 // Check a fixed-up URL, to normalize the scheme and parse hosts correctly.
498 URLFixerUpper::FixupURL(url
.possibly_invalid_spec(), std::string());
499 return (fixed_url
.SchemeIs(content::kChromeUIScheme
) &&
500 (fixed_url
.host() == content::kChromeUIBrowserCrashHost
||
501 fixed_url
.host() == chrome::kChromeUICrashHost
));
504 void ExtensionTabUtil::CreateTab(WebContents
* web_contents
,
505 const std::string
& extension_id
,
506 WindowOpenDisposition disposition
,
507 const gfx::Rect
& initial_pos
,
510 Profile::FromBrowserContext(web_contents
->GetBrowserContext());
511 chrome::HostDesktopType active_desktop
= chrome::GetActiveDesktop();
512 Browser
* browser
= chrome::FindTabbedBrowser(profile
, false, active_desktop
);
513 const bool browser_created
= !browser
;
515 browser
= new Browser(Browser::CreateParams(profile
, active_desktop
));
516 chrome::NavigateParams
params(browser
, web_contents
);
518 // The extension_app_id parameter ends up as app_name in the Browser
519 // which causes the Browser to return true for is_app(). This affects
520 // among other things, whether the location bar gets displayed.
521 // TODO(mpcomplete): This seems wrong. What if the extension content is hosted
523 if (disposition
== NEW_POPUP
)
524 params
.extension_app_id
= extension_id
;
526 params
.disposition
= disposition
;
527 params
.window_bounds
= initial_pos
;
528 params
.window_action
= chrome::NavigateParams::SHOW_WINDOW
;
529 params
.user_gesture
= user_gesture
;
530 chrome::Navigate(¶ms
);
532 // Close the browser if chrome::Navigate created a new one.
533 if (browser_created
&& (browser
!= params
.browser
))
534 browser
->window()->Close();
538 void ExtensionTabUtil::ForEachTab(
539 const base::Callback
<void(WebContents
*)>& callback
) {
540 for (TabContentsIterator iterator
; !iterator
.done(); iterator
.Next())
541 callback
.Run(*iterator
);
545 WindowController
* ExtensionTabUtil::GetWindowControllerOfTab(
546 const WebContents
* web_contents
) {
547 Browser
* browser
= chrome::FindBrowserWithWebContents(web_contents
);
549 return browser
->extension_window_controller();
554 void ExtensionTabUtil::OpenOptionsPage(const Extension
* extension
,
556 DCHECK(!ManifestURL::GetOptionsPage(extension
).is_empty());
558 // Force the options page to open in non-OTR window, because it won't be
559 // able to save settings from OTR.
560 scoped_ptr
<chrome::ScopedTabbedBrowserDisplayer
> displayer
;
561 if (browser
->profile()->IsOffTheRecord()) {
562 displayer
.reset(new chrome::ScopedTabbedBrowserDisplayer(
563 browser
->profile()->GetOriginalProfile(),
564 browser
->host_desktop_type()));
565 browser
= displayer
->browser();
568 content::OpenURLParams
params(ManifestURL::GetOptionsPage(extension
),
571 content::PAGE_TRANSITION_LINK
,
573 browser
->OpenURL(params
);
574 browser
->window()->Show();
575 WebContents
* web_contents
=
576 browser
->tab_strip_model()->GetActiveWebContents();
577 web_contents
->GetDelegate()->ActivateContents(web_contents
);
580 } // namespace extensions