Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(137)

Side by Side Diff: chrome/browser/extensions/extension_tab_util.cc

Issue 10829186: Tabs API is usable without tabs permission. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: . Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/event_names.h"
8 #include "chrome/browser/extensions/tab_helper.h" 9 #include "chrome/browser/extensions/tab_helper.h"
9 #include "chrome/browser/extensions/window_controller.h" 10 #include "chrome/browser/extensions/window_controller.h"
10 #include "chrome/browser/net/url_fixer_upper.h" 11 #include "chrome/browser/net/url_fixer_upper.h"
11 #include "chrome/browser/profiles/profile.h" 12 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/sessions/session_id.h" 13 #include "chrome/browser/sessions/session_id.h"
13 #include "chrome/browser/ui/browser.h" 14 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_finder.h" 15 #include "chrome/browser/ui/browser_finder.h"
15 #include "chrome/browser/ui/browser_list.h" 16 #include "chrome/browser/ui/browser_list.h"
16 #include "chrome/browser/ui/browser_tabstrip.h" 17 #include "chrome/browser/ui/browser_tabstrip.h"
17 #include "chrome/browser/ui/browser_window.h" 18 #include "chrome/browser/ui/browser_window.h"
18 #include "chrome/browser/ui/tab_contents/tab_contents.h" 19 #include "chrome/browser/ui/tab_contents/tab_contents.h"
19 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" 20 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h"
20 #include "chrome/browser/ui/tabs/tab_strip_model.h" 21 #include "chrome/browser/ui/tabs/tab_strip_model.h"
21 #include "chrome/common/extensions/extension.h" 22 #include "chrome/common/extensions/extension.h"
22 #include "chrome/common/extensions/extension_manifest_constants.h" 23 #include "chrome/common/extensions/extension_manifest_constants.h"
24 #include "chrome/common/extensions/permissions/api_permission.h"
23 #include "chrome/common/url_constants.h" 25 #include "chrome/common/url_constants.h"
24 #include "content/public/browser/favicon_status.h" 26 #include "content/public/browser/favicon_status.h"
25 #include "content/public/browser/navigation_entry.h" 27 #include "content/public/browser/navigation_entry.h"
26 #include "content/public/browser/web_contents.h" 28 #include "content/public/browser/web_contents.h"
27 #include "googleurl/src/gurl.h" 29 #include "googleurl/src/gurl.h"
28 30
29 namespace keys = extensions::tabs_constants; 31 namespace keys = extensions::tabs_constants;
30 32
31 using content::NavigationEntry; 33 using content::NavigationEntry;
32 using content::WebContents; 34 using content::WebContents;
33 35
36 namespace {
37
38 void StripTabOfSensitiveData(DictionaryValue* tab) {
39 tab->SetString(keys::kUrlKey, std::string());
40 tab->SetString(keys::kTitleKey, std::string());
41 tab->SetString(keys::kFaviconUrlKey, std::string());
42 }
43
44 }
45
34 int ExtensionTabUtil::GetWindowId(const Browser* browser) { 46 int ExtensionTabUtil::GetWindowId(const Browser* browser) {
35 return browser->session_id().id(); 47 return browser->session_id().id();
36 } 48 }
37 49
38 int ExtensionTabUtil::GetWindowIdOfTabStripModel( 50 int ExtensionTabUtil::GetWindowIdOfTabStripModel(
39 const TabStripModel* tab_strip_model) { 51 const TabStripModel* tab_strip_model) {
40 for (BrowserList::const_iterator it = BrowserList::begin(); 52 for (BrowserList::const_iterator it = BrowserList::begin();
41 it != BrowserList::end(); ++it) { 53 it != BrowserList::end(); ++it) {
42 if ((*it)->tab_strip_model() == tab_strip_model) 54 if ((*it)->tab_strip_model() == tab_strip_model)
43 return GetWindowId(*it); 55 return GetWindowId(*it);
44 } 56 }
45 return -1; 57 return -1;
46 } 58 }
47 59
48 int ExtensionTabUtil::GetTabId(const WebContents* web_contents) { 60 int ExtensionTabUtil::GetTabId(const WebContents* web_contents) {
49 return SessionID::IdForTab(TabContents::FromWebContents(web_contents)); 61 return SessionID::IdForTab(TabContents::FromWebContents(web_contents));
50 } 62 }
51 63
52 std::string ExtensionTabUtil::GetTabStatusText(bool is_loading) { 64 std::string ExtensionTabUtil::GetTabStatusText(bool is_loading) {
53 return is_loading ? keys::kStatusValueLoading : keys::kStatusValueComplete; 65 return is_loading ? keys::kStatusValueLoading : keys::kStatusValueComplete;
54 } 66 }
55 67
56 int ExtensionTabUtil::GetWindowIdOfTab(const WebContents* web_contents) { 68 int ExtensionTabUtil::GetWindowIdOfTab(const WebContents* web_contents) {
57 return SessionID::IdForWindowContainingTab( 69 return SessionID::IdForWindowContainingTab(
58 TabContents::FromWebContents(web_contents)); 70 TabContents::FromWebContents(web_contents));
59 } 71 }
60 72
61 DictionaryValue* ExtensionTabUtil::CreateTabValue(const WebContents* contents) { 73 DictionaryValue* ExtensionTabUtil::CreateTabValue(
74 const WebContents* contents,
75 const extensions::Extension* extension) {
62 // Find the tab strip and index of this guy. 76 // Find the tab strip and index of this guy.
63 TabStripModel* tab_strip = NULL; 77 TabStripModel* tab_strip = NULL;
64 int tab_index; 78 int tab_index;
65 if (ExtensionTabUtil::GetTabStripModel(contents, &tab_strip, &tab_index)) 79 if (ExtensionTabUtil::GetTabStripModel(contents, &tab_strip, &tab_index)) {
66 return ExtensionTabUtil::CreateTabValue(contents, tab_strip, tab_index); 80 return ExtensionTabUtil::CreateTabValue(contents,
81 tab_strip,
82 tab_index,
83 extension);
84 }
67 85
68 // Couldn't find it. This can happen if the tab is being dragged. 86 // Couldn't find it. This can happen if the tab is being dragged.
69 return ExtensionTabUtil::CreateTabValue(contents, NULL, -1); 87 return ExtensionTabUtil::CreateTabValue(contents, NULL, -1, extension);
70 } 88 }
71 89
72 ListValue* ExtensionTabUtil::CreateTabList(const Browser* browser) { 90 ListValue* ExtensionTabUtil::CreateTabList(
91 const Browser* browser,
92 const extensions::Extension* extension) {
73 ListValue* tab_list = new ListValue(); 93 ListValue* tab_list = new ListValue();
74 TabStripModel* tab_strip = browser->tab_strip_model(); 94 TabStripModel* tab_strip = browser->tab_strip_model();
75 for (int i = 0; i < tab_strip->count(); ++i) { 95 for (int i = 0; i < tab_strip->count(); ++i) {
76 tab_list->Append(ExtensionTabUtil::CreateTabValue( 96 tab_list->Append(CreateTabValue(
77 tab_strip->GetTabContentsAt(i)->web_contents(), tab_strip, i)); 97 tab_strip->GetTabContentsAt(i)->web_contents(),
98 tab_strip,
99 i,
100 extension));
78 } 101 }
79 102
80 return tab_list; 103 return tab_list;
81 } 104 }
82 105
83 DictionaryValue* ExtensionTabUtil::CreateTabValue(const WebContents* contents, 106 DictionaryValue* ExtensionTabUtil::CreateTabValue(
84 TabStripModel* tab_strip, 107 const WebContents* contents,
85 int tab_index) { 108 TabStripModel* tab_strip,
109 int tab_index,
110 const extensions::Extension* extension) {
86 DictionaryValue* result = new DictionaryValue(); 111 DictionaryValue* result = new DictionaryValue();
87 bool is_loading = contents->IsLoading(); 112 bool is_loading = contents->IsLoading();
88 result->SetInteger(keys::kIdKey, ExtensionTabUtil::GetTabId(contents)); 113 result->SetInteger(keys::kIdKey, GetTabId(contents));
89 result->SetInteger(keys::kIndexKey, tab_index); 114 result->SetInteger(keys::kIndexKey, tab_index);
90 result->SetInteger(keys::kWindowIdKey, 115 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)); 116 result->SetString(keys::kStatusKey, GetTabStatusText(is_loading));
94 result->SetBoolean(keys::kActiveKey, 117 result->SetBoolean(keys::kActiveKey,
95 tab_strip && tab_index == tab_strip->active_index()); 118 tab_strip && tab_index == tab_strip->active_index());
96 result->SetBoolean(keys::kSelectedKey, 119 result->SetBoolean(keys::kSelectedKey,
97 tab_strip && tab_index == tab_strip->active_index()); 120 tab_strip && tab_index == tab_strip->active_index());
98 result->SetBoolean(keys::kHighlightedKey, 121 result->SetBoolean(keys::kHighlightedKey,
99 tab_strip && tab_strip->IsTabSelected(tab_index)); 122 tab_strip && tab_strip->IsTabSelected(tab_index));
100 result->SetBoolean(keys::kPinnedKey, 123 result->SetBoolean(keys::kPinnedKey,
101 tab_strip && tab_strip->IsTabPinned(tab_index)); 124 tab_strip && tab_strip->IsTabPinned(tab_index));
102 result->SetString(keys::kTitleKey, contents->GetTitle());
103 result->SetBoolean(keys::kIncognitoKey, 125 result->SetBoolean(keys::kIncognitoKey,
104 contents->GetBrowserContext()->IsOffTheRecord()); 126 contents->GetBrowserContext()->IsOffTheRecord());
105 127
128 // If we have an extension without permissions, we don't add sensitive data.
129 if (extension && !extension->HasAPIPermissionForTab(
130 tab_index, extensions::APIPermission::kTab)) {
131 result->SetString(keys::kUrlKey, std::string());
Aaron Boodman 2012/08/23 17:47:29 There's no reason to set these keys at all is ther
chebert 2012/08/29 21:51:58 Done.
132 result->SetString(keys::kTitleKey, std::string());
133 } else {
134 result->SetString(keys::kUrlKey, contents->GetURL().spec());
135 result->SetString(keys::kTitleKey, contents->GetTitle());
136 }
137
106 if (tab_strip) { 138 if (tab_strip) {
107 content::NavigationController* opener = 139 content::NavigationController* opener =
108 tab_strip->GetOpenerOfTabContentsAt(tab_index); 140 tab_strip->GetOpenerOfTabContentsAt(tab_index);
109 if (opener) { 141 if (opener) {
110 result->SetInteger(keys::kOpenerTabIdKey, 142 result->SetInteger(keys::kOpenerTabIdKey,
111 ExtensionTabUtil::GetTabId(opener->GetWebContents())); 143 GetTabId(opener->GetWebContents()));
112 } 144 }
113 } 145 }
114 146
115 if (!is_loading) { 147 if (!is_loading) {
116 NavigationEntry* entry = contents->GetController().GetActiveEntry(); 148 NavigationEntry* entry = contents->GetController().GetActiveEntry();
117 if (entry) { 149 if (entry) {
118 if (entry->GetFavicon().valid) 150 if (entry->GetFavicon().valid) {
Aaron Boodman 2012/08/23 17:47:29 Can you rejigger things so that here is just one c
chebert 2012/08/29 21:51:58 Rejiggered.
119 result->SetString(keys::kFaviconUrlKey, entry->GetFavicon().url.spec()); 151 if (extension && !extension->HasAPIPermissionForTab(
152 tab_index, extensions::APIPermission::kTab)) {
153 result->SetString(keys::kFaviconUrlKey, std::string());
154 } else {
155 result->SetString(keys::kFaviconUrlKey,
156 entry->GetFavicon().url.spec());
157 }
158 }
120 } 159 }
121 } 160 }
122 161
123 return result; 162 return result;
124 } 163 }
125 164
126 DictionaryValue* ExtensionTabUtil::CreateTabValueActive( 165 DictionaryValue* ExtensionTabUtil::CreateTabValueActive(
127 const WebContents* contents, 166 const WebContents* contents,
128 bool active) { 167 bool active,
129 DictionaryValue* result = ExtensionTabUtil::CreateTabValue(contents); 168 const extensions::Extension* extension) {
169 DictionaryValue* result = CreateTabValue(contents, extension);
130 result->SetBoolean(keys::kSelectedKey, active); 170 result->SetBoolean(keys::kSelectedKey, active);
131 return result; 171 return result;
132 } 172 }
133 173
174 void ExtensionTabUtil::MaybeStripEventArgsOfSensitiveData(
175 const std::string& event_name,
176 const extensions::Extension* extension,
177 base::ListValue* event_args) {
178 int arg = 0;
179 if (event_name == extensions::event_names::kOnTabUpdated)
180 arg = 2;
181 else if (event_name == extensions::event_names::kOnTabUpdated)
182 arg = 0;
183 else
184 return;
185
186 if (!extension->HasAPIPermission(extensions::APIPermission::kTab)) {
187 DictionaryValue* tab;
188 event_args->GetDictionary(arg, &tab);
189 StripTabOfSensitiveData(tab);
190 }
191 }
192
134 bool ExtensionTabUtil::GetTabStripModel(const WebContents* web_contents, 193 bool ExtensionTabUtil::GetTabStripModel(const WebContents* web_contents,
135 TabStripModel** tab_strip_model, 194 TabStripModel** tab_strip_model,
136 int* tab_index) { 195 int* tab_index) {
137 DCHECK(web_contents); 196 DCHECK(web_contents);
138 DCHECK(tab_strip_model); 197 DCHECK(tab_strip_model);
139 DCHECK(tab_index); 198 DCHECK(tab_index);
140 199
141 for (BrowserList::const_iterator it = BrowserList::begin(); 200 for (BrowserList::const_iterator it = BrowserList::begin();
142 it != BrowserList::end(); ++it) { 201 it != BrowserList::end(); ++it) {
143 TabStripModel* tab_strip = (*it)->tab_strip_model(); 202 TabStripModel* tab_strip = (*it)->tab_strip_model();
(...skipping 10 matching lines...) Expand all
154 213
155 bool ExtensionTabUtil::GetDefaultTab(Browser* browser, 214 bool ExtensionTabUtil::GetDefaultTab(Browser* browser,
156 TabContents** contents, 215 TabContents** contents,
157 int* tab_id) { 216 int* tab_id) {
158 DCHECK(browser); 217 DCHECK(browser);
159 DCHECK(contents); 218 DCHECK(contents);
160 219
161 *contents = chrome::GetActiveTabContents(browser); 220 *contents = chrome::GetActiveTabContents(browser);
162 if (*contents) { 221 if (*contents) {
163 if (tab_id) 222 if (tab_id)
164 *tab_id = ExtensionTabUtil::GetTabId((*contents)->web_contents()); 223 *tab_id = GetTabId((*contents)->web_contents());
165 return true; 224 return true;
166 } 225 }
167 226
168 return false; 227 return false;
169 } 228 }
170 229
171 bool ExtensionTabUtil::GetTabById(int tab_id, 230 bool ExtensionTabUtil::GetTabById(int tab_id,
172 Profile* profile, 231 Profile* profile,
173 bool include_incognito, 232 bool include_incognito,
174 Browser** browser, 233 Browser** browser,
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 322
264 // static 323 // static
265 extensions::WindowController* ExtensionTabUtil::GetWindowControllerOfTab( 324 extensions::WindowController* ExtensionTabUtil::GetWindowControllerOfTab(
266 const WebContents* web_contents) { 325 const WebContents* web_contents) {
267 Browser* browser = browser::FindBrowserWithWebContents(web_contents); 326 Browser* browser = browser::FindBrowserWithWebContents(web_contents);
268 if (browser != NULL) 327 if (browser != NULL)
269 return browser->extension_window_controller(); 328 return browser->extension_window_controller();
270 329
271 return NULL; 330 return NULL;
272 } 331 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_tab_util.h ('k') | chrome/browser/extensions/extension_tab_util_android.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698