| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/extension_tab_util.h" | 5 #include "chrome/browser/extensions/extension_tab_util.h" |
| 6 | 6 |
| 7 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" | 7 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" |
| 8 #include "chrome/browser/extensions/tab_helper.h" | 8 #include "chrome/browser/extensions/tab_helper.h" |
| 9 #include "chrome/browser/extensions/window_controller.h" | 9 #include "chrome/browser/extensions/window_controller.h" |
| 10 #include "chrome/browser/net/url_fixer_upper.h" | 10 #include "chrome/browser/net/url_fixer_upper.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/sessions/session_id.h" | 12 #include "chrome/browser/sessions/session_id.h" |
| 13 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/browser_finder.h" | 14 #include "chrome/browser/ui/browser_finder.h" |
| 15 #include "chrome/browser/ui/browser_list.h" | 15 #include "chrome/browser/ui/browser_list.h" |
| 16 #include "chrome/browser/ui/browser_tabstrip.h" | 16 #include "chrome/browser/ui/browser_tabstrip.h" |
| 17 #include "chrome/browser/ui/browser_window.h" | 17 #include "chrome/browser/ui/browser_window.h" |
| 18 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 18 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 19 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" | 19 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" |
| 20 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 20 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 21 #include "chrome/common/extensions/extension.h" | 21 #include "chrome/common/extensions/extension.h" |
| 22 #include "chrome/common/extensions/extension_manifest_constants.h" | 22 #include "chrome/common/extensions/extension_manifest_constants.h" |
| 23 #include "chrome/common/extensions/permissions/api_permission.h" |
| 23 #include "chrome/common/url_constants.h" | 24 #include "chrome/common/url_constants.h" |
| 24 #include "content/public/browser/favicon_status.h" | 25 #include "content/public/browser/favicon_status.h" |
| 25 #include "content/public/browser/navigation_entry.h" | 26 #include "content/public/browser/navigation_entry.h" |
| 26 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" |
| 27 #include "googleurl/src/gurl.h" | 28 #include "googleurl/src/gurl.h" |
| 28 | 29 |
| 29 namespace keys = extensions::tabs_constants; | 30 namespace keys = extensions::tabs_constants; |
| 30 | 31 |
| 31 using content::NavigationEntry; | 32 using content::NavigationEntry; |
| 32 using content::WebContents; | 33 using content::WebContents; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 51 | 52 |
| 52 std::string ExtensionTabUtil::GetTabStatusText(bool is_loading) { | 53 std::string ExtensionTabUtil::GetTabStatusText(bool is_loading) { |
| 53 return is_loading ? keys::kStatusValueLoading : keys::kStatusValueComplete; | 54 return is_loading ? keys::kStatusValueLoading : keys::kStatusValueComplete; |
| 54 } | 55 } |
| 55 | 56 |
| 56 int ExtensionTabUtil::GetWindowIdOfTab(const WebContents* web_contents) { | 57 int ExtensionTabUtil::GetWindowIdOfTab(const WebContents* web_contents) { |
| 57 return SessionID::IdForWindowContainingTab( | 58 return SessionID::IdForWindowContainingTab( |
| 58 TabContents::FromWebContents(web_contents)); | 59 TabContents::FromWebContents(web_contents)); |
| 59 } | 60 } |
| 60 | 61 |
| 61 DictionaryValue* ExtensionTabUtil::CreateTabValue(const WebContents* contents) { | 62 DictionaryValue* ExtensionTabUtil::CreateTabValue( |
| 63 const WebContents* contents, |
| 64 const extensions::Extension* extension) { |
| 62 // Find the tab strip and index of this guy. | 65 // Find the tab strip and index of this guy. |
| 63 TabStripModel* tab_strip = NULL; | 66 TabStripModel* tab_strip = NULL; |
| 64 int tab_index; | 67 int tab_index; |
| 65 if (ExtensionTabUtil::GetTabStripModel(contents, &tab_strip, &tab_index)) | 68 if (ExtensionTabUtil::GetTabStripModel(contents, &tab_strip, &tab_index)) { |
| 66 return ExtensionTabUtil::CreateTabValue(contents, tab_strip, tab_index); | 69 return ExtensionTabUtil::CreateTabValue(contents, |
| 70 tab_strip, |
| 71 tab_index, |
| 72 extension); |
| 73 } |
| 67 | 74 |
| 68 // Couldn't find it. This can happen if the tab is being dragged. | 75 // Couldn't find it. This can happen if the tab is being dragged. |
| 69 return ExtensionTabUtil::CreateTabValue(contents, NULL, -1); | 76 return ExtensionTabUtil::CreateTabValue(contents, NULL, -1, extension); |
| 70 } | 77 } |
| 71 | 78 |
| 72 ListValue* ExtensionTabUtil::CreateTabList(const Browser* browser) { | 79 ListValue* ExtensionTabUtil::CreateTabList( |
| 80 const Browser* browser, |
| 81 const extensions::Extension* extension) { |
| 73 ListValue* tab_list = new ListValue(); | 82 ListValue* tab_list = new ListValue(); |
| 74 TabStripModel* tab_strip = browser->tab_strip_model(); | 83 TabStripModel* tab_strip = browser->tab_strip_model(); |
| 75 for (int i = 0; i < tab_strip->count(); ++i) { | 84 for (int i = 0; i < tab_strip->count(); ++i) { |
| 76 tab_list->Append(ExtensionTabUtil::CreateTabValue( | 85 tab_list->Append(CreateTabValue( |
| 77 tab_strip->GetTabContentsAt(i)->web_contents(), tab_strip, i)); | 86 tab_strip->GetTabContentsAt(i)->web_contents(), |
| 87 tab_strip, |
| 88 i, |
| 89 extension)); |
| 78 } | 90 } |
| 79 | 91 |
| 80 return tab_list; | 92 return tab_list; |
| 81 } | 93 } |
| 82 | 94 |
| 83 DictionaryValue* ExtensionTabUtil::CreateTabValue(const WebContents* contents, | 95 DictionaryValue* ExtensionTabUtil::CreateTabValue( |
| 84 TabStripModel* tab_strip, | 96 const WebContents* contents, |
| 85 int tab_index) { | 97 TabStripModel* tab_strip, |
| 98 int tab_index, |
| 99 const extensions::Extension* extension) { |
| 100 CHECK(extension); |
| 86 DictionaryValue* result = new DictionaryValue(); | 101 DictionaryValue* result = new DictionaryValue(); |
| 87 bool is_loading = contents->IsLoading(); | 102 bool is_loading = contents->IsLoading(); |
| 88 result->SetInteger(keys::kIdKey, ExtensionTabUtil::GetTabId(contents)); | 103 result->SetInteger(keys::kIdKey, GetTabId(contents)); |
| 89 result->SetInteger(keys::kIndexKey, tab_index); | 104 result->SetInteger(keys::kIndexKey, tab_index); |
| 90 result->SetInteger(keys::kWindowIdKey, | 105 result->SetInteger(keys::kWindowIdKey, GetWindowIdOfTab(contents)); |
| 91 ExtensionTabUtil::GetWindowIdOfTab(contents)); | |
| 92 result->SetString(keys::kUrlKey, contents->GetURL().spec()); | |
| 93 result->SetString(keys::kStatusKey, GetTabStatusText(is_loading)); | 106 result->SetString(keys::kStatusKey, GetTabStatusText(is_loading)); |
| 94 result->SetBoolean(keys::kActiveKey, | 107 result->SetBoolean(keys::kActiveKey, |
| 95 tab_strip && tab_index == tab_strip->active_index()); | 108 tab_strip && tab_index == tab_strip->active_index()); |
| 96 result->SetBoolean(keys::kSelectedKey, | 109 result->SetBoolean(keys::kSelectedKey, |
| 97 tab_strip && tab_index == tab_strip->active_index()); | 110 tab_strip && tab_index == tab_strip->active_index()); |
| 98 result->SetBoolean(keys::kHighlightedKey, | 111 result->SetBoolean(keys::kHighlightedKey, |
| 99 tab_strip && tab_strip->IsTabSelected(tab_index)); | 112 tab_strip && tab_strip->IsTabSelected(tab_index)); |
| 100 result->SetBoolean(keys::kPinnedKey, | 113 result->SetBoolean(keys::kPinnedKey, |
| 101 tab_strip && tab_strip->IsTabPinned(tab_index)); | 114 tab_strip && tab_strip->IsTabPinned(tab_index)); |
| 102 result->SetString(keys::kTitleKey, contents->GetTitle()); | |
| 103 result->SetBoolean(keys::kIncognitoKey, | 115 result->SetBoolean(keys::kIncognitoKey, |
| 104 contents->GetBrowserContext()->IsOffTheRecord()); | 116 contents->GetBrowserContext()->IsOffTheRecord()); |
| 105 | 117 |
| 118 // If we have an extension without permissions, we don't add sensitive data. |
| 119 bool has_permission = false; |
| 120 if (tab_index >= 0) { |
| 121 has_permission = extension->HasAPIPermissionForTab( |
| 122 tab_index, extensions::APIPermission::kTab); |
| 123 } else { |
| 124 has_permission = extension->HasAPIPermission( |
| 125 extensions::APIPermission::kTab); |
| 126 } |
| 127 |
| 128 if (has_permission) { |
| 129 result->SetString(keys::kUrlKey, contents->GetURL().spec()); |
| 130 result->SetString(keys::kTitleKey, contents->GetTitle()); |
| 131 if (!is_loading) { |
| 132 NavigationEntry* entry = contents->GetController().GetActiveEntry(); |
| 133 if (entry && entry->GetFavicon().valid) |
| 134 result->SetString(keys::kFaviconUrlKey, entry->GetFavicon().url.spec()); |
| 135 } |
| 136 } else { |
| 137 result->SetString(keys::kUrlKey, std::string()); |
| 138 result->SetString(keys::kTitleKey, std::string()); |
| 139 if (!is_loading) { |
| 140 NavigationEntry* entry = contents->GetController().GetActiveEntry(); |
| 141 if (entry && entry->GetFavicon().valid) |
| 142 result->SetString(keys::kFaviconUrlKey, std::string()); |
| 143 } |
| 144 } |
| 145 |
| 106 if (tab_strip) { | 146 if (tab_strip) { |
| 107 content::NavigationController* opener = | 147 content::NavigationController* opener = |
| 108 tab_strip->GetOpenerOfTabContentsAt(tab_index); | 148 tab_strip->GetOpenerOfTabContentsAt(tab_index); |
| 109 if (opener) { | 149 if (opener) { |
| 110 result->SetInteger(keys::kOpenerTabIdKey, | 150 result->SetInteger(keys::kOpenerTabIdKey, |
| 111 ExtensionTabUtil::GetTabId(opener->GetWebContents())); | 151 GetTabId(opener->GetWebContents())); |
| 112 } | 152 } |
| 113 } | 153 } |
| 114 | 154 |
| 115 if (!is_loading) { | |
| 116 NavigationEntry* entry = contents->GetController().GetActiveEntry(); | |
| 117 if (entry) { | |
| 118 if (entry->GetFavicon().valid) | |
| 119 result->SetString(keys::kFaviconUrlKey, entry->GetFavicon().url.spec()); | |
| 120 } | |
| 121 } | |
| 122 | |
| 123 return result; | 155 return result; |
| 124 } | 156 } |
| 125 | 157 |
| 126 DictionaryValue* ExtensionTabUtil::CreateTabValueActive( | 158 DictionaryValue* ExtensionTabUtil::CreateTabValueActive( |
| 127 const WebContents* contents, | 159 const WebContents* contents, |
| 128 bool active) { | 160 bool active, |
| 129 DictionaryValue* result = ExtensionTabUtil::CreateTabValue(contents); | 161 const extensions::Extension* extension) { |
| 162 DictionaryValue* result = CreateTabValue(contents, extension); |
| 130 result->SetBoolean(keys::kSelectedKey, active); | 163 result->SetBoolean(keys::kSelectedKey, active); |
| 131 return result; | 164 return result; |
| 132 } | 165 } |
| 133 | 166 |
| 134 bool ExtensionTabUtil::GetTabStripModel(const WebContents* web_contents, | 167 bool ExtensionTabUtil::GetTabStripModel(const WebContents* web_contents, |
| 135 TabStripModel** tab_strip_model, | 168 TabStripModel** tab_strip_model, |
| 136 int* tab_index) { | 169 int* tab_index) { |
| 137 DCHECK(web_contents); | 170 DCHECK(web_contents); |
| 138 DCHECK(tab_strip_model); | 171 DCHECK(tab_strip_model); |
| 139 DCHECK(tab_index); | 172 DCHECK(tab_index); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 154 | 187 |
| 155 bool ExtensionTabUtil::GetDefaultTab(Browser* browser, | 188 bool ExtensionTabUtil::GetDefaultTab(Browser* browser, |
| 156 TabContents** contents, | 189 TabContents** contents, |
| 157 int* tab_id) { | 190 int* tab_id) { |
| 158 DCHECK(browser); | 191 DCHECK(browser); |
| 159 DCHECK(contents); | 192 DCHECK(contents); |
| 160 | 193 |
| 161 *contents = chrome::GetActiveTabContents(browser); | 194 *contents = chrome::GetActiveTabContents(browser); |
| 162 if (*contents) { | 195 if (*contents) { |
| 163 if (tab_id) | 196 if (tab_id) |
| 164 *tab_id = ExtensionTabUtil::GetTabId((*contents)->web_contents()); | 197 *tab_id = GetTabId((*contents)->web_contents()); |
| 165 return true; | 198 return true; |
| 166 } | 199 } |
| 167 | 200 |
| 168 return false; | 201 return false; |
| 169 } | 202 } |
| 170 | 203 |
| 171 bool ExtensionTabUtil::GetTabById(int tab_id, | 204 bool ExtensionTabUtil::GetTabById(int tab_id, |
| 172 Profile* profile, | 205 Profile* profile, |
| 173 bool include_incognito, | 206 bool include_incognito, |
| 174 Browser** browser, | 207 Browser** browser, |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 | 297 |
| 265 // static | 298 // static |
| 266 extensions::WindowController* ExtensionTabUtil::GetWindowControllerOfTab( | 299 extensions::WindowController* ExtensionTabUtil::GetWindowControllerOfTab( |
| 267 const WebContents* web_contents) { | 300 const WebContents* web_contents) { |
| 268 Browser* browser = browser::FindBrowserWithWebContents(web_contents); | 301 Browser* browser = browser::FindBrowserWithWebContents(web_contents); |
| 269 if (browser != NULL) | 302 if (browser != NULL) |
| 270 return browser->extension_window_controller(); | 303 return browser->extension_window_controller(); |
| 271 | 304 |
| 272 return NULL; | 305 return NULL; |
| 273 } | 306 } |
| OLD | NEW |