NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / extensions / extension_tab_util.cc
blobbe7f4c9640555885503eecd615f2ef1a821322f8
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 "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/manifest_url_handler.h"
23 #include "chrome/common/net/url_fixer_upper.h"
24 #include "chrome/common/url_constants.h"
25 #include "content/public/browser/favicon_status.h"
26 #include "content/public/browser/navigation_entry.h"
27 #include "content/public/browser/web_contents.h"
28 #include "content/public/browser/web_contents_view.h"
29 #include "extensions/common/extension.h"
30 #include "extensions/common/manifest_constants.h"
31 #include "extensions/common/permissions/api_permission.h"
32 #include "extensions/common/permissions/permissions_data.h"
33 #include "url/gurl.h"
35 using apps::AppWindow;
36 using content::NavigationEntry;
37 using content::WebContents;
39 namespace extensions {
41 namespace {
43 namespace keys = tabs_constants;
45 WindowController* GetAppWindowController(const WebContents* contents) {
46 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext());
47 apps::AppWindowRegistry* registry = apps::AppWindowRegistry::Get(profile);
48 if (!registry)
49 return NULL;
50 AppWindow* app_window =
51 registry->GetAppWindowForRenderViewHost(contents->GetRenderViewHost());
52 if (!app_window)
53 return NULL;
54 return WindowControllerList::GetInstance()->FindWindowById(
55 app_window->session_id().id());
58 } // namespace
60 int ExtensionTabUtil::GetWindowId(const Browser* browser) {
61 return browser->session_id().id();
64 int ExtensionTabUtil::GetWindowIdOfTabStripModel(
65 const TabStripModel* tab_strip_model) {
66 for (chrome::BrowserIterator it; !it.done(); it.Next()) {
67 if (it->tab_strip_model() == tab_strip_model)
68 return GetWindowId(*it);
70 return -1;
73 int ExtensionTabUtil::GetTabId(const WebContents* web_contents) {
74 return SessionID::IdForTab(web_contents);
77 std::string ExtensionTabUtil::GetTabStatusText(bool is_loading) {
78 return is_loading ? keys::kStatusValueLoading : keys::kStatusValueComplete;
81 int ExtensionTabUtil::GetWindowIdOfTab(const WebContents* web_contents) {
82 return SessionID::IdForWindowContainingTab(web_contents);
85 base::DictionaryValue* ExtensionTabUtil::CreateTabValue(
86 const WebContents* contents,
87 TabStripModel* tab_strip,
88 int tab_index,
89 const Extension* extension) {
90 // If we have a matching AppWindow with a controller, get the tab value
91 // from its controller instead.
92 WindowController* controller = GetAppWindowController(contents);
93 if (controller &&
94 (!extension || controller->IsVisibleToExtension(extension))) {
95 return controller->CreateTabValue(extension, tab_index);
97 base::DictionaryValue* result =
98 CreateTabValue(contents, tab_strip, tab_index);
99 ScrubTabValueForExtension(contents, extension, result);
100 return result;
103 base::ListValue* ExtensionTabUtil::CreateTabList(
104 const Browser* browser,
105 const Extension* extension) {
106 base::ListValue* tab_list = new base::ListValue();
107 TabStripModel* tab_strip = browser->tab_strip_model();
108 for (int i = 0; i < tab_strip->count(); ++i) {
109 tab_list->Append(CreateTabValue(tab_strip->GetWebContentsAt(i),
110 tab_strip,
112 extension));
115 return tab_list;
118 base::DictionaryValue* ExtensionTabUtil::CreateTabValue(
119 const WebContents* contents,
120 TabStripModel* tab_strip,
121 int tab_index) {
122 // If we have a matching AppWindow with a controller, get the tab value
123 // from its controller instead.
124 WindowController* controller = GetAppWindowController(contents);
125 if (controller)
126 return controller->CreateTabValue(NULL, tab_index);
128 if (!tab_strip)
129 ExtensionTabUtil::GetTabStripModel(contents, &tab_strip, &tab_index);
131 base::DictionaryValue* result = new base::DictionaryValue();
132 bool is_loading = contents->IsLoading();
133 result->SetInteger(keys::kIdKey, GetTabId(contents));
134 result->SetInteger(keys::kIndexKey, tab_index);
135 result->SetInteger(keys::kWindowIdKey, GetWindowIdOfTab(contents));
136 result->SetString(keys::kStatusKey, GetTabStatusText(is_loading));
137 result->SetBoolean(keys::kActiveKey,
138 tab_strip && tab_index == tab_strip->active_index());
139 result->SetBoolean(keys::kSelectedKey,
140 tab_strip && tab_index == tab_strip->active_index());
141 result->SetBoolean(keys::kHighlightedKey,
142 tab_strip && tab_strip->IsTabSelected(tab_index));
143 result->SetBoolean(keys::kPinnedKey,
144 tab_strip && tab_strip->IsTabPinned(tab_index));
145 result->SetBoolean(keys::kIncognitoKey,
146 contents->GetBrowserContext()->IsOffTheRecord());
147 result->SetInteger(keys::kWidthKey,
148 contents->GetView()->GetContainerSize().width());
149 result->SetInteger(keys::kHeightKey,
150 contents->GetView()->GetContainerSize().height());
152 // Privacy-sensitive fields: these should be stripped off by
153 // ScrubTabValueForExtension if the extension should not see them.
154 result->SetString(keys::kUrlKey, contents->GetURL().spec());
155 result->SetString(keys::kTitleKey, contents->GetTitle());
156 if (!is_loading) {
157 NavigationEntry* entry = contents->GetController().GetVisibleEntry();
158 if (entry && entry->GetFavicon().valid)
159 result->SetString(keys::kFaviconUrlKey, entry->GetFavicon().url.spec());
162 if (tab_strip) {
163 WebContents* opener = tab_strip->GetOpenerOfWebContentsAt(tab_index);
164 if (opener)
165 result->SetInteger(keys::kOpenerTabIdKey, GetTabId(opener));
168 return result;
171 void ExtensionTabUtil::ScrubTabValueForExtension(
172 const WebContents* contents,
173 const Extension* extension,
174 base::DictionaryValue* tab_info) {
175 bool has_permission =
176 extension &&
177 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,
188 api::tabs::Tab* tab) {
189 bool has_permission = extension && extension->HasAPIPermission(
190 APIPermission::kTab);
192 if (!has_permission) {
193 tab->url.reset();
194 tab->title.reset();
195 tab->fav_icon_url.reset();
199 bool ExtensionTabUtil::GetTabStripModel(const WebContents* web_contents,
200 TabStripModel** tab_strip_model,
201 int* tab_index) {
202 DCHECK(web_contents);
203 DCHECK(tab_strip_model);
204 DCHECK(tab_index);
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);
209 if (index != -1) {
210 *tab_strip_model = tab_strip;
211 *tab_index = index;
212 return true;
216 return false;
219 bool ExtensionTabUtil::GetDefaultTab(Browser* browser,
220 WebContents** contents,
221 int* tab_id) {
222 DCHECK(browser);
223 DCHECK(contents);
225 *contents = browser->tab_strip_model()->GetActiveWebContents();
226 if (*contents) {
227 if (tab_id)
228 *tab_id = GetTabId(*contents);
229 return true;
232 return false;
235 bool ExtensionTabUtil::GetTabById(int tab_id,
236 Profile* profile,
237 bool include_incognito,
238 Browser** browser,
239 TabStripModel** tab_strip,
240 WebContents** contents,
241 int* tab_index) {
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) {
253 if (browser)
254 *browser = target_browser;
255 if (tab_strip)
256 *tab_strip = target_tab_strip;
257 if (contents)
258 *contents = target_contents;
259 if (tab_index)
260 *tab_index = i;
261 return true;
266 return false;
269 GURL ExtensionTabUtil::ResolvePossiblyRelativeURL(const std::string& url_string,
270 const Extension* extension) {
271 GURL url = GURL(url_string);
272 if (!url.is_valid())
273 url = extension->GetResourceURL(url_string);
275 return url;
278 bool ExtensionTabUtil::IsCrashURL(const GURL& url) {
279 // Check a fixed-up URL, to normalize the scheme and parse hosts correctly.
280 GURL fixed_url =
281 URLFixerUpper::FixupURL(url.possibly_invalid_spec(), std::string());
282 return (fixed_url.SchemeIs(content::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,
291 bool user_gesture) {
292 Profile* profile =
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;
297 if (!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
305 // in a tab?
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(&params);
315 // Close the browser if chrome::Navigate created a new one.
316 if (browser_created && (browser != params.browser))
317 browser->window()->Close();
320 // static
321 void ExtensionTabUtil::ForEachTab(
322 const base::Callback<void(WebContents*)>& callback) {
323 for (TabContentsIterator iterator; !iterator.done(); iterator.Next())
324 callback.Run(*iterator);
327 // static
328 WindowController* ExtensionTabUtil::GetWindowControllerOfTab(
329 const WebContents* web_contents) {
330 Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
331 if (browser != NULL)
332 return browser->extension_window_controller();
334 return NULL;
337 void ExtensionTabUtil::OpenOptionsPage(const Extension* extension,
338 Browser* browser) {
339 DCHECK(!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 scoped_ptr<chrome::ScopedTabbedBrowserDisplayer> displayer;
344 if (browser->profile()->IsOffTheRecord()) {
345 displayer.reset(new chrome::ScopedTabbedBrowserDisplayer(
346 browser->profile()->GetOriginalProfile(),
347 browser->host_desktop_type()));
348 browser = displayer->browser();
351 content::OpenURLParams params(ManifestURL::GetOptionsPage(extension),
352 content::Referrer(),
353 SINGLETON_TAB,
354 content::PAGE_TRANSITION_LINK,
355 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);
363 } // namespace extensions