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 "base/logging.h"
8 #include "base/values.h"
9 #include "chrome/browser/extensions/api/tabs/tabs_constants.h"
10 #include "chrome/browser/extensions/window_controller.h"
11 #include "chrome/browser/extensions/window_controller_list.h"
12 #include "chrome/browser/sessions/session_tab_helper.h"
13 #include "content/public/browser/browser_context.h"
14 #include "content/public/browser/favicon_status.h"
15 #include "content/public/browser/navigation_entry.h"
16 #include "extensions/browser/app_window/app_window.h"
17 #include "extensions/browser/app_window/app_window_registry.h"
20 using content::NavigationEntry
;
21 using content::WebContents
;
23 namespace extensions
{
25 namespace keys
= tabs_constants
;
29 WindowController
* GetAppWindowController(const WebContents
* contents
) {
30 AppWindowRegistry
* registry
=
31 AppWindowRegistry::Get(contents
->GetBrowserContext());
34 AppWindow
* app_window
=
35 registry
->GetAppWindowForRenderViewHost(contents
->GetRenderViewHost());
38 return WindowControllerList::GetInstance()->FindWindowById(
39 app_window
->session_id().id());
44 ExtensionTabUtil::OpenTabParams::OpenTabParams()
45 : create_browser_if_needed(false) {
48 ExtensionTabUtil::OpenTabParams::~OpenTabParams() {
51 // Opens a new tab for a given extension. Returns NULL and sets |error| if an
53 base::DictionaryValue
* ExtensionTabUtil::OpenTab(
54 ChromeUIThreadExtensionFunction
* function
,
55 const OpenTabParams
& params
,
61 Browser
* ExtensionTabUtil::GetBrowserFromWindowID(
62 ChromeUIThreadExtensionFunction
* function
,
69 Browser
* ExtensionTabUtil::GetBrowserFromWindowID(
70 const ChromeExtensionFunctionDetails
& details
,
77 int ExtensionTabUtil::GetWindowId(const Browser
* browser
) {
82 int ExtensionTabUtil::GetWindowIdOfTabStripModel(
83 const TabStripModel
* tab_strip_model
) {
88 int ExtensionTabUtil::GetTabId(const WebContents
* web_contents
) {
89 return SessionTabHelper::IdForTab(web_contents
);
92 std::string
ExtensionTabUtil::GetTabStatusText(bool is_loading
) {
93 return is_loading
? keys::kStatusValueLoading
: keys::kStatusValueComplete
;
96 int ExtensionTabUtil::GetWindowIdOfTab(const WebContents
* web_contents
) {
97 return SessionTabHelper::IdForWindowContainingTab(web_contents
);
100 base::DictionaryValue
* ExtensionTabUtil::CreateTabValue(
101 WebContents
* contents
,
102 TabStripModel
* tab_strip
,
104 const Extension
* extension
) {
109 base::ListValue
* ExtensionTabUtil::CreateTabList(
110 const Browser
* browser
,
111 const Extension
* extension
) {
112 return new base::ListValue();
115 base::DictionaryValue
* ExtensionTabUtil::CreateTabValue(
116 WebContents
* contents
,
117 TabStripModel
* tab_strip
,
119 // There's no TabStrip in Athena.
122 // If we have a matching AppWindow with a controller, get the tab value
123 // from its controller instead.
124 WindowController
* controller
= GetAppWindowController(contents
);
126 return controller
->CreateTabValue(NULL
, tab_index
);
128 base::DictionaryValue
* result
= new base::DictionaryValue();
129 bool is_loading
= contents
->IsLoading();
130 result
->SetInteger(keys::kIdKey
, GetTabId(contents
));
131 result
->SetInteger(keys::kIndexKey
, tab_index
);
132 result
->SetInteger(keys::kWindowIdKey
, GetWindowIdOfTab(contents
));
133 result
->SetString(keys::kStatusKey
, GetTabStatusText(is_loading
));
134 result
->SetBoolean(keys::kActiveKey
, false);
135 result
->SetBoolean(keys::kSelectedKey
, false);
136 result
->SetBoolean(keys::kHighlightedKey
, false);
137 result
->SetBoolean(keys::kPinnedKey
, false);
138 result
->SetBoolean(keys::kIncognitoKey
,
139 contents
->GetBrowserContext()->IsOffTheRecord());
140 result
->SetInteger(keys::kWidthKey
,
141 contents
->GetContainerBounds().size().width());
142 result
->SetInteger(keys::kHeightKey
,
143 contents
->GetContainerBounds().size().height());
145 // Privacy-sensitive fields: these should be stripped off by
146 // ScrubTabValueForExtension if the extension should not see them.
147 result
->SetString(keys::kUrlKey
, contents
->GetURL().spec());
148 result
->SetString(keys::kTitleKey
, contents
->GetTitle());
150 NavigationEntry
* entry
= contents
->GetController().GetVisibleEntry();
151 if (entry
&& entry
->GetFavicon().valid
)
152 result
->SetString(keys::kFaviconUrlKey
, entry
->GetFavicon().url
.spec());
158 void ExtensionTabUtil::ScrubTabValueForExtension(
159 WebContents
* contents
,
160 const Extension
* extension
,
161 base::DictionaryValue
* tab_info
) {
162 // TODO(oshima): Move this to common impl.
165 void ExtensionTabUtil::ScrubTabForExtension(const Extension
* extension
,
166 api::tabs::Tab
* tab
) {
168 // TODO(oshima): Move this to common impl.
171 bool ExtensionTabUtil::GetTabStripModel(const WebContents
* web_contents
,
172 TabStripModel
** tab_strip_model
,
179 bool ExtensionTabUtil::GetDefaultTab(Browser
* browser
,
180 WebContents
** contents
,
186 bool ExtensionTabUtil::GetTabById(int tab_id
,
187 content::BrowserContext
* browser_context
,
188 bool include_incognito
,
190 TabStripModel
** tab_strip
,
191 WebContents
** contents
,
197 GURL
ExtensionTabUtil::ResolvePossiblyRelativeURL(const std::string
& url_string
,
198 const Extension
* extension
) {
199 // TODO(oshima): Move this to common impl.
200 return GURL(url_string
);
203 bool ExtensionTabUtil::IsCrashURL(const GURL
& url
) {
204 // TODO(oshima): Move this to common impl.
208 void ExtensionTabUtil::CreateTab(WebContents
* web_contents
,
209 const std::string
& extension_id
,
210 WindowOpenDisposition disposition
,
211 const gfx::Rect
& initial_pos
,
217 void ExtensionTabUtil::ForEachTab(
218 const base::Callback
<void(WebContents
*)>& callback
) {
219 // TODO(oshima): Move this to common impl.
223 WindowController
* ExtensionTabUtil::GetWindowControllerOfTab(
224 const WebContents
* web_contents
) {
229 void ExtensionTabUtil::OpenOptionsPage(const Extension
* extension
,
234 } // namespace extensions