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

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

Issue 10880064: Make extension commands grant the activeTab permission. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: comments Created 8 years, 2 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 | Annotate | Revision Log
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 <string> 5 #include <string>
6 6
7 #include "base/compiler_specific.h" 7 #include "base/compiler_specific.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop.h" 9 #include "base/message_loop.h"
10 #include "base/values.h" 10 #include "base/values.h"
11 #include "chrome/browser/extensions/active_tab_permission_manager.h" 11 #include "chrome/browser/extensions/active_tab_permission_granter.h"
12 #include "chrome/browser/extensions/tab_helper.h" 12 #include "chrome/browser/extensions/tab_helper.h"
13 #include "chrome/browser/sessions/session_id.h" 13 #include "chrome/browser/sessions/session_id.h"
14 #include "chrome/browser/ui/tab_contents/tab_contents.h" 14 #include "chrome/browser/ui/tab_contents/tab_contents.h"
15 #include "chrome/browser/ui/tab_contents/test_tab_contents.h" 15 #include "chrome/browser/ui/tab_contents/test_tab_contents.h"
16 #include "chrome/common/chrome_notification_types.h" 16 #include "chrome/common/chrome_notification_types.h"
17 #include "chrome/common/extensions/extension.h" 17 #include "chrome/common/extensions/extension.h"
18 #include "chrome/common/extensions/extension_builder.h" 18 #include "chrome/common/extensions/extension_builder.h"
19 #include "chrome/common/extensions/features/feature.h" 19 #include "chrome/common/extensions/features/feature.h"
20 #include "chrome/common/extensions/value_builder.h" 20 #include "chrome/common/extensions/value_builder.h"
21 #include "content/public/browser/browser_thread.h" 21 #include "content/public/browser/browser_thread.h"
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 extension(CreateTestExtension("deadbeef", true)), 59 extension(CreateTestExtension("deadbeef", true)),
60 another_extension(CreateTestExtension("feedbeef", true)), 60 another_extension(CreateTestExtension("feedbeef", true)),
61 extension_without_active_tab(CreateTestExtension("badbeef", false)), 61 extension_without_active_tab(CreateTestExtension("badbeef", false)),
62 ui_thread_(BrowserThread::UI, MessageLoop::current()) {} 62 ui_thread_(BrowserThread::UI, MessageLoop::current()) {}
63 63
64 protected: 64 protected:
65 int tab_id() { 65 int tab_id() {
66 return SessionID::IdForTab(tab_contents()->web_contents()); 66 return SessionID::IdForTab(tab_contents()->web_contents());
67 } 67 }
68 68
69 ActiveTabPermissionManager* active_tab_permission_manager() { 69 ActiveTabPermissionGranter* active_tab_permission_granter() {
70 return extensions::TabHelper::FromWebContents(web_contents())-> 70 return extensions::TabHelper::FromWebContents(web_contents())->
71 active_tab_permission_manager(); 71 active_tab_permission_granter();
72 } 72 }
73 73
74 bool IsAllowed(const scoped_refptr<const Extension>& extension, 74 bool IsAllowed(const scoped_refptr<const Extension>& extension,
75 const GURL& url) { 75 const GURL& url) {
76 return IsAllowed(extension, url, tab_id()); 76 return IsAllowed(extension, url, tab_id());
77 } 77 }
78 78
79 bool IsAllowed(const scoped_refptr<const Extension>& extension, 79 bool IsAllowed(const scoped_refptr<const Extension>& extension,
80 const GURL& url, 80 const GURL& url,
81 int tab_id) { 81 int tab_id) {
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 130
131 // No access unless it's been granted. 131 // No access unless it's been granted.
132 EXPECT_TRUE(IsBlocked(extension, google)); 132 EXPECT_TRUE(IsBlocked(extension, google));
133 EXPECT_TRUE(IsBlocked(another_extension, google)); 133 EXPECT_TRUE(IsBlocked(another_extension, google));
134 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google)); 134 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google));
135 135
136 EXPECT_FALSE(HasTabsPermission(extension)); 136 EXPECT_FALSE(HasTabsPermission(extension));
137 EXPECT_FALSE(HasTabsPermission(another_extension)); 137 EXPECT_FALSE(HasTabsPermission(another_extension));
138 EXPECT_FALSE(HasTabsPermission(extension_without_active_tab)); 138 EXPECT_FALSE(HasTabsPermission(extension_without_active_tab));
139 139
140 active_tab_permission_manager()->GrantIfRequested(extension); 140 active_tab_permission_granter()->GrantIfRequested(extension);
141 active_tab_permission_manager()->GrantIfRequested( 141 active_tab_permission_granter()->GrantIfRequested(
142 extension_without_active_tab); 142 extension_without_active_tab);
143 143
144 // Granted to extension and extension_without_active_tab, but the latter 144 // Granted to extension and extension_without_active_tab, but the latter
145 // doesn't have the activeTab permission so not granted. 145 // doesn't have the activeTab permission so not granted.
146 EXPECT_TRUE(IsAllowed(extension, google)); 146 EXPECT_TRUE(IsAllowed(extension, google));
147 EXPECT_TRUE(IsBlocked(another_extension, google)); 147 EXPECT_TRUE(IsBlocked(another_extension, google));
148 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google)); 148 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google));
149 149
150 // Other subdomains shouldn't be given access. 150 // Other subdomains shouldn't be given access.
151 GURL mail_google("http://mail.google.com"); 151 GURL mail_google("http://mail.google.com");
152 EXPECT_TRUE(IsBlocked(extension, mail_google)); 152 EXPECT_TRUE(IsBlocked(extension, mail_google));
153 EXPECT_TRUE(IsBlocked(another_extension, mail_google)); 153 EXPECT_TRUE(IsBlocked(another_extension, mail_google));
154 EXPECT_TRUE(IsBlocked(extension_without_active_tab, mail_google)); 154 EXPECT_TRUE(IsBlocked(extension_without_active_tab, mail_google));
155 155
156 // Reloading the page should clear the active permissions. 156 // Reloading the page should clear the active permissions.
157 Reload(); 157 Reload();
158 158
159 EXPECT_TRUE(IsBlocked(extension, google)); 159 EXPECT_TRUE(IsBlocked(extension, google));
160 EXPECT_TRUE(IsBlocked(another_extension, google)); 160 EXPECT_TRUE(IsBlocked(another_extension, google));
161 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google)); 161 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google));
162 162
163 EXPECT_FALSE(HasTabsPermission(extension)); 163 EXPECT_FALSE(HasTabsPermission(extension));
164 EXPECT_FALSE(HasTabsPermission(another_extension)); 164 EXPECT_FALSE(HasTabsPermission(another_extension));
165 EXPECT_FALSE(HasTabsPermission(extension_without_active_tab)); 165 EXPECT_FALSE(HasTabsPermission(extension_without_active_tab));
166 166
167 // But they should still be able to be granted again. 167 // But they should still be able to be granted again.
168 active_tab_permission_manager()->GrantIfRequested(extension); 168 active_tab_permission_granter()->GrantIfRequested(extension);
169 169
170 EXPECT_TRUE(IsAllowed(extension, google)); 170 EXPECT_TRUE(IsAllowed(extension, google));
171 EXPECT_TRUE(IsBlocked(another_extension, google)); 171 EXPECT_TRUE(IsBlocked(another_extension, google));
172 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google)); 172 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google));
173 173
174 // And grant a few more times redundantly for good measure. 174 // And grant a few more times redundantly for good measure.
175 active_tab_permission_manager()->GrantIfRequested(extension); 175 active_tab_permission_granter()->GrantIfRequested(extension);
176 active_tab_permission_manager()->GrantIfRequested(extension); 176 active_tab_permission_granter()->GrantIfRequested(extension);
177 active_tab_permission_manager()->GrantIfRequested(another_extension); 177 active_tab_permission_granter()->GrantIfRequested(another_extension);
178 active_tab_permission_manager()->GrantIfRequested(another_extension); 178 active_tab_permission_granter()->GrantIfRequested(another_extension);
179 active_tab_permission_manager()->GrantIfRequested(another_extension); 179 active_tab_permission_granter()->GrantIfRequested(another_extension);
180 active_tab_permission_manager()->GrantIfRequested(extension); 180 active_tab_permission_granter()->GrantIfRequested(extension);
181 active_tab_permission_manager()->GrantIfRequested(extension); 181 active_tab_permission_granter()->GrantIfRequested(extension);
182 active_tab_permission_manager()->GrantIfRequested(another_extension); 182 active_tab_permission_granter()->GrantIfRequested(another_extension);
183 active_tab_permission_manager()->GrantIfRequested(another_extension); 183 active_tab_permission_granter()->GrantIfRequested(another_extension);
184 184
185 EXPECT_TRUE(IsAllowed(extension, google)); 185 EXPECT_TRUE(IsAllowed(extension, google));
186 EXPECT_TRUE(IsAllowed(another_extension, google)); 186 EXPECT_TRUE(IsAllowed(another_extension, google));
187 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google)); 187 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google));
188 188
189 // Navigating to a new URL should clear the active permissions. 189 // Navigating to a new URL should clear the active permissions.
190 GURL chromium("http://www.chromium.org"); 190 GURL chromium("http://www.chromium.org");
191 NavigateAndCommit(chromium); 191 NavigateAndCommit(chromium);
192 192
193 EXPECT_TRUE(IsBlocked(extension, google)); 193 EXPECT_TRUE(IsBlocked(extension, google));
194 EXPECT_TRUE(IsBlocked(another_extension, google)); 194 EXPECT_TRUE(IsBlocked(another_extension, google));
195 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google)); 195 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google));
196 196
197 EXPECT_TRUE(IsBlocked(extension, chromium)); 197 EXPECT_TRUE(IsBlocked(extension, chromium));
198 EXPECT_TRUE(IsBlocked(another_extension, chromium)); 198 EXPECT_TRUE(IsBlocked(another_extension, chromium));
199 EXPECT_TRUE(IsBlocked(extension_without_active_tab, chromium)); 199 EXPECT_TRUE(IsBlocked(extension_without_active_tab, chromium));
200 200
201 EXPECT_FALSE(HasTabsPermission(extension)); 201 EXPECT_FALSE(HasTabsPermission(extension));
202 EXPECT_FALSE(HasTabsPermission(another_extension)); 202 EXPECT_FALSE(HasTabsPermission(another_extension));
203 EXPECT_FALSE(HasTabsPermission(extension_without_active_tab)); 203 EXPECT_FALSE(HasTabsPermission(extension_without_active_tab));
204 204
205 // Should be able to grant to multiple extensions at the same time (if they 205 // Should be able to grant to multiple extensions at the same time (if they
206 // have the activeTab permission, of course). 206 // have the activeTab permission, of course).
207 active_tab_permission_manager()->GrantIfRequested(extension); 207 active_tab_permission_granter()->GrantIfRequested(extension);
208 active_tab_permission_manager()->GrantIfRequested(another_extension); 208 active_tab_permission_granter()->GrantIfRequested(another_extension);
209 active_tab_permission_manager()->GrantIfRequested( 209 active_tab_permission_granter()->GrantIfRequested(
210 extension_without_active_tab); 210 extension_without_active_tab);
211 211
212 EXPECT_TRUE(IsBlocked(extension, google)); 212 EXPECT_TRUE(IsBlocked(extension, google));
213 EXPECT_TRUE(IsBlocked(another_extension, google)); 213 EXPECT_TRUE(IsBlocked(another_extension, google));
214 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google)); 214 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google));
215 215
216 EXPECT_TRUE(IsAllowed(extension, chromium)); 216 EXPECT_TRUE(IsAllowed(extension, chromium));
217 EXPECT_TRUE(IsAllowed(another_extension, chromium)); 217 EXPECT_TRUE(IsAllowed(another_extension, chromium));
218 EXPECT_TRUE(IsBlocked(extension_without_active_tab, chromium)); 218 EXPECT_TRUE(IsBlocked(extension_without_active_tab, chromium));
219 219
220 // Should be able to go back to URLs that were previously cleared. 220 // Should be able to go back to URLs that were previously cleared.
221 NavigateAndCommit(google); 221 NavigateAndCommit(google);
222 222
223 active_tab_permission_manager()->GrantIfRequested(extension); 223 active_tab_permission_granter()->GrantIfRequested(extension);
224 active_tab_permission_manager()->GrantIfRequested(another_extension); 224 active_tab_permission_granter()->GrantIfRequested(another_extension);
225 active_tab_permission_manager()->GrantIfRequested( 225 active_tab_permission_granter()->GrantIfRequested(
226 extension_without_active_tab); 226 extension_without_active_tab);
227 227
228 EXPECT_TRUE(IsAllowed(extension, google)); 228 EXPECT_TRUE(IsAllowed(extension, google));
229 EXPECT_TRUE(IsAllowed(another_extension, google)); 229 EXPECT_TRUE(IsAllowed(another_extension, google));
230 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google)); 230 EXPECT_TRUE(IsBlocked(extension_without_active_tab, google));
231 231
232 EXPECT_TRUE(IsBlocked(extension, chromium)); 232 EXPECT_TRUE(IsBlocked(extension, chromium));
233 EXPECT_TRUE(IsBlocked(another_extension, chromium)); 233 EXPECT_TRUE(IsBlocked(another_extension, chromium));
234 EXPECT_TRUE(IsBlocked(extension_without_active_tab, chromium)); 234 EXPECT_TRUE(IsBlocked(extension_without_active_tab, chromium));
235 }; 235 };
236 236
237 TEST_F(ActiveTabTest, Uninstalling) { 237 TEST_F(ActiveTabTest, Uninstalling) {
238 // Some semi-arbitrary setup. 238 // Some semi-arbitrary setup.
239 GURL google("http://www.google.com"); 239 GURL google("http://www.google.com");
240 NavigateAndCommit(google); 240 NavigateAndCommit(google);
241 241
242 active_tab_permission_manager()->GrantIfRequested(extension); 242 active_tab_permission_granter()->GrantIfRequested(extension);
243 243
244 EXPECT_TRUE(active_tab_permission_manager()->IsGranted(extension)); 244 EXPECT_TRUE(active_tab_permission_granter()->IsGranted(extension));
245 EXPECT_TRUE(IsAllowed(extension, google)); 245 EXPECT_TRUE(IsAllowed(extension, google));
246 246
247 // Uninstalling the extension should clear its tab permissions. 247 // Uninstalling the extension should clear its tab permissions.
248 UnloadedExtensionInfo details( 248 UnloadedExtensionInfo details(
249 extension, 249 extension,
250 extension_misc::UNLOAD_REASON_DISABLE); 250 extension_misc::UNLOAD_REASON_DISABLE);
251 content::NotificationService::current()->Notify( 251 content::NotificationService::current()->Notify(
252 chrome::NOTIFICATION_EXTENSION_UNLOADED, 252 chrome::NOTIFICATION_EXTENSION_UNLOADED,
253 content::Source<Profile>(tab_contents()->profile()), 253 content::Source<Profile>(tab_contents()->profile()),
254 content::Details<UnloadedExtensionInfo>(&details)); 254 content::Details<UnloadedExtensionInfo>(&details));
255 255
256 EXPECT_FALSE(active_tab_permission_manager()->IsGranted(extension)); 256 EXPECT_FALSE(active_tab_permission_granter()->IsGranted(extension));
257 // Note: can't EXPECT_FALSE(IsAllowed) here because uninstalled extensions 257 // Note: can't EXPECT_FALSE(IsAllowed) here because uninstalled extensions
258 // are just that... considered to be uninstalled, and the manager might 258 // are just that... considered to be uninstalled, and the manager might
259 // just ignore them from here on. 259 // just ignore them from here on.
260 260
261 // Granting the extension again should give them back. 261 // Granting the extension again should give them back.
262 active_tab_permission_manager()->GrantIfRequested(extension); 262 active_tab_permission_granter()->GrantIfRequested(extension);
263 263
264 EXPECT_TRUE(active_tab_permission_manager()->IsGranted(extension)); 264 EXPECT_TRUE(active_tab_permission_granter()->IsGranted(extension));
265 EXPECT_TRUE(IsAllowed(extension, google)); 265 EXPECT_TRUE(IsAllowed(extension, google));
266 } 266 }
267 267
268 TEST_F(ActiveTabTest, OnlyActiveTab) { 268 TEST_F(ActiveTabTest, OnlyActiveTab) {
269 GURL google("http://www.google.com"); 269 GURL google("http://www.google.com");
270 NavigateAndCommit(google); 270 NavigateAndCommit(google);
271 271
272 active_tab_permission_manager()->GrantIfRequested(extension); 272 active_tab_permission_granter()->GrantIfRequested(extension);
273 273
274 EXPECT_TRUE(IsAllowed(extension, google, tab_id())); 274 EXPECT_TRUE(IsAllowed(extension, google, tab_id()));
275 EXPECT_TRUE(IsBlocked(extension, google, tab_id() + 1)); 275 EXPECT_TRUE(IsBlocked(extension, google, tab_id() + 1));
276 EXPECT_FALSE(HasTabsPermission(extension, tab_id() + 1)); 276 EXPECT_FALSE(HasTabsPermission(extension, tab_id() + 1));
277 } 277 }
278 278
279 TEST_F(ActiveTabTest, NavigateInPage) { 279 TEST_F(ActiveTabTest, NavigateInPage) {
280 GURL google("http://www.google.com"); 280 GURL google("http://www.google.com");
281 NavigateAndCommit(google); 281 NavigateAndCommit(google);
282 282
283 active_tab_permission_manager()->GrantIfRequested(extension); 283 active_tab_permission_granter()->GrantIfRequested(extension);
284 284
285 // Perform an in-page navigation. The extension should not lose the temporary 285 // Perform an in-page navigation. The extension should not lose the temporary
286 // permission. 286 // permission.
287 GURL google_h1("http://www.google.com#h1"); 287 GURL google_h1("http://www.google.com#h1");
288 NavigateAndCommit(google_h1); 288 NavigateAndCommit(google_h1);
289 289
290 EXPECT_TRUE(IsAllowed(extension, google, tab_id())); 290 EXPECT_TRUE(IsAllowed(extension, google, tab_id()));
291 EXPECT_TRUE(IsAllowed(extension, google_h1, tab_id())); 291 EXPECT_TRUE(IsAllowed(extension, google_h1, tab_id()));
292 292
293 GURL chromium("http://www.chromium.org"); 293 GURL chromium("http://www.chromium.org");
294 NavigateAndCommit(chromium); 294 NavigateAndCommit(chromium);
295 295
296 EXPECT_FALSE(IsAllowed(extension, google, tab_id())); 296 EXPECT_FALSE(IsAllowed(extension, google, tab_id()));
297 EXPECT_FALSE(IsAllowed(extension, google_h1, tab_id())); 297 EXPECT_FALSE(IsAllowed(extension, google_h1, tab_id()));
298 EXPECT_FALSE(IsAllowed(extension, chromium, tab_id())); 298 EXPECT_FALSE(IsAllowed(extension, chromium, tab_id()));
299 299
300 active_tab_permission_manager()->GrantIfRequested(extension); 300 active_tab_permission_granter()->GrantIfRequested(extension);
301 301
302 EXPECT_FALSE(IsAllowed(extension, google, tab_id())); 302 EXPECT_FALSE(IsAllowed(extension, google, tab_id()));
303 EXPECT_FALSE(IsAllowed(extension, google_h1, tab_id())); 303 EXPECT_FALSE(IsAllowed(extension, google_h1, tab_id()));
304 EXPECT_TRUE(IsAllowed(extension, chromium, tab_id())); 304 EXPECT_TRUE(IsAllowed(extension, chromium, tab_id()));
305 305
306 GURL chromium_h1("http://www.chromium.org#h1"); 306 GURL chromium_h1("http://www.chromium.org#h1");
307 NavigateAndCommit(chromium_h1); 307 NavigateAndCommit(chromium_h1);
308 308
309 EXPECT_FALSE(IsAllowed(extension, google, tab_id())); 309 EXPECT_FALSE(IsAllowed(extension, google, tab_id()));
310 EXPECT_FALSE(IsAllowed(extension, google_h1, tab_id())); 310 EXPECT_FALSE(IsAllowed(extension, google_h1, tab_id()));
311 EXPECT_TRUE(IsAllowed(extension, chromium, tab_id())); 311 EXPECT_TRUE(IsAllowed(extension, chromium, tab_id()));
312 EXPECT_TRUE(IsAllowed(extension, chromium_h1, tab_id())); 312 EXPECT_TRUE(IsAllowed(extension, chromium_h1, tab_id()));
313 313
314 Reload(); 314 Reload();
315 315
316 EXPECT_FALSE(IsAllowed(extension, google, tab_id())); 316 EXPECT_FALSE(IsAllowed(extension, google, tab_id()));
317 EXPECT_FALSE(IsAllowed(extension, google_h1, tab_id())); 317 EXPECT_FALSE(IsAllowed(extension, google_h1, tab_id()));
318 EXPECT_FALSE(IsAllowed(extension, chromium, tab_id())); 318 EXPECT_FALSE(IsAllowed(extension, chromium, tab_id()));
319 EXPECT_FALSE(IsAllowed(extension, chromium_h1, tab_id())); 319 EXPECT_FALSE(IsAllowed(extension, chromium_h1, tab_id()));
320 } 320 }
321 321
322 } // namespace 322 } // namespace
323 } // namespace extensions 323 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/active_tab_permission_manager.cc ('k') | chrome/browser/extensions/api/omnibox/omnibox_api.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698