OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <map> | 5 #include <map> |
6 | 6 |
7 #include "base/values.h" | 7 #include "base/values.h" |
8 #include "chrome/browser/extensions/active_script_controller.h" | 8 #include "chrome/browser/extensions/active_script_controller.h" |
9 #include "chrome/browser/extensions/active_tab_permission_granter.h" | 9 #include "chrome/browser/extensions/active_tab_permission_granter.h" |
10 #include "chrome/browser/extensions/extension_util.h" | 10 #include "chrome/browser/extensions/extension_util.h" |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 void ActiveScriptControllerUnitTest::IncrementExecutionCount( | 157 void ActiveScriptControllerUnitTest::IncrementExecutionCount( |
158 const std::string& extension_id) { | 158 const std::string& extension_id) { |
159 ++extension_executions_[extension_id]; | 159 ++extension_executions_[extension_id]; |
160 } | 160 } |
161 | 161 |
162 void ActiveScriptControllerUnitTest::SetUp() { | 162 void ActiveScriptControllerUnitTest::SetUp() { |
163 ChromeRenderViewHostTestHarness::SetUp(); | 163 ChromeRenderViewHostTestHarness::SetUp(); |
164 | 164 |
165 TabHelper::CreateForWebContents(web_contents()); | 165 TabHelper::CreateForWebContents(web_contents()); |
166 TabHelper* tab_helper = TabHelper::FromWebContents(web_contents()); | 166 TabHelper* tab_helper = TabHelper::FromWebContents(web_contents()); |
167 // None of these should ever be NULL. | 167 // These should never be NULL. |
168 DCHECK(tab_helper); | 168 DCHECK(tab_helper); |
169 DCHECK(tab_helper->location_bar_controller()); | 169 active_script_controller_ = tab_helper->active_script_controller(); |
170 active_script_controller_ = | |
171 tab_helper->location_bar_controller()->active_script_controller(); | |
172 DCHECK(active_script_controller_); | 170 DCHECK(active_script_controller_); |
173 } | 171 } |
174 | 172 |
175 // Test that extensions with all_hosts require permission to execute, and, once | 173 // Test that extensions with all_hosts require permission to execute, and, once |
176 // that permission is granted, do execute. | 174 // that permission is granted, do execute. |
177 TEST_F(ActiveScriptControllerUnitTest, RequestPermissionAndExecute) { | 175 TEST_F(ActiveScriptControllerUnitTest, RequestPermissionAndExecute) { |
178 const Extension* extension = AddExtension(); | 176 const Extension* extension = AddExtension(); |
179 ASSERT_TRUE(extension); | 177 ASSERT_TRUE(extension); |
180 | 178 |
181 NavigateAndCommit(GURL("https://www.google.com")); | 179 NavigateAndCommit(GURL("https://www.google.com")); |
182 | 180 |
183 // Ensure that there aren't any executions pending. | 181 // Ensure that there aren't any executions pending. |
184 ASSERT_EQ(0u, GetExecutionCountForExtension(extension->id())); | 182 ASSERT_EQ(0u, GetExecutionCountForExtension(extension->id())); |
185 ASSERT_FALSE(controller()->GetActionForExtension(extension)); | 183 ASSERT_FALSE(controller()->WantsToRun(extension)); |
186 | 184 |
187 // Since the extension requests all_hosts, we should require user consent. | 185 // Since the extension requests all_hosts, we should require user consent. |
188 EXPECT_TRUE(RequiresUserConsent(extension)); | 186 EXPECT_TRUE(RequiresUserConsent(extension)); |
189 | 187 |
190 // Request an injection. There should be an action visible, but no executions. | 188 // Request an injection. The extension should want to run, but should not have |
| 189 // executed. |
191 RequestInjection(extension); | 190 RequestInjection(extension); |
192 EXPECT_TRUE(controller()->GetActionForExtension(extension)); | 191 EXPECT_TRUE(controller()->WantsToRun(extension)); |
193 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); | 192 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); |
194 | 193 |
195 // Click to accept the extension executing. | 194 // Click to accept the extension executing. |
196 controller()->OnClicked(extension); | 195 controller()->OnClicked(extension); |
197 | 196 |
198 // The extension should execute, and the action should go away. | 197 // The extension should execute, and the extension shouldn't want to run. |
199 EXPECT_EQ(1u, GetExecutionCountForExtension(extension->id())); | 198 EXPECT_EQ(1u, GetExecutionCountForExtension(extension->id())); |
200 EXPECT_FALSE(controller()->GetActionForExtension(extension)); | 199 EXPECT_FALSE(controller()->WantsToRun(extension)); |
201 | 200 |
202 // Since we already executed on the given page, we shouldn't need permission | 201 // Since we already executed on the given page, we shouldn't need permission |
203 // for a second time. | 202 // for a second time. |
204 EXPECT_FALSE(RequiresUserConsent(extension)); | 203 EXPECT_FALSE(RequiresUserConsent(extension)); |
205 | 204 |
206 // Reloading and same-origin navigations shouldn't clear those permissions, | 205 // Reloading and same-origin navigations shouldn't clear those permissions, |
207 // and we shouldn't require user constent again. | 206 // and we shouldn't require user constent again. |
208 Reload(); | 207 Reload(); |
209 EXPECT_FALSE(RequiresUserConsent(extension)); | 208 EXPECT_FALSE(RequiresUserConsent(extension)); |
210 NavigateAndCommit(GURL("https://www.google.com/foo")); | 209 NavigateAndCommit(GURL("https://www.google.com/foo")); |
211 EXPECT_FALSE(RequiresUserConsent(extension)); | 210 EXPECT_FALSE(RequiresUserConsent(extension)); |
212 NavigateAndCommit(GURL("https://www.google.com/bar")); | 211 NavigateAndCommit(GURL("https://www.google.com/bar")); |
213 EXPECT_FALSE(RequiresUserConsent(extension)); | 212 EXPECT_FALSE(RequiresUserConsent(extension)); |
214 | 213 |
215 // Cross-origin navigations should clear permissions. | 214 // Cross-origin navigations should clear permissions. |
216 NavigateAndCommit(GURL("https://otherdomain.google.com")); | 215 NavigateAndCommit(GURL("https://otherdomain.google.com")); |
217 EXPECT_TRUE(RequiresUserConsent(extension)); | 216 EXPECT_TRUE(RequiresUserConsent(extension)); |
218 | 217 |
219 // Grant access. | 218 // Grant access. |
220 RequestInjection(extension); | 219 RequestInjection(extension); |
221 controller()->OnClicked(extension); | 220 controller()->OnClicked(extension); |
222 EXPECT_EQ(2u, GetExecutionCountForExtension(extension->id())); | 221 EXPECT_EQ(2u, GetExecutionCountForExtension(extension->id())); |
223 EXPECT_FALSE(controller()->GetActionForExtension(extension)); | 222 EXPECT_FALSE(controller()->WantsToRun(extension)); |
224 | 223 |
225 // Navigating to another site should also clear the permissions. | 224 // Navigating to another site should also clear the permissions. |
226 NavigateAndCommit(GURL("https://www.foo.com")); | 225 NavigateAndCommit(GURL("https://www.foo.com")); |
227 EXPECT_TRUE(RequiresUserConsent(extension)); | 226 EXPECT_TRUE(RequiresUserConsent(extension)); |
228 } | 227 } |
229 | 228 |
230 // Test that injections that are not executed by the time the user navigates are | 229 // Test that injections that are not executed by the time the user navigates are |
231 // ignored and never execute. | 230 // ignored and never execute. |
232 TEST_F(ActiveScriptControllerUnitTest, PendingInjectionsRemovedAtNavigation) { | 231 TEST_F(ActiveScriptControllerUnitTest, PendingInjectionsRemovedAtNavigation) { |
233 const Extension* extension = AddExtension(); | 232 const Extension* extension = AddExtension(); |
234 ASSERT_TRUE(extension); | 233 ASSERT_TRUE(extension); |
235 | 234 |
236 NavigateAndCommit(GURL("https://www.google.com")); | 235 NavigateAndCommit(GURL("https://www.google.com")); |
237 | 236 |
238 ASSERT_EQ(0u, GetExecutionCountForExtension(extension->id())); | 237 ASSERT_EQ(0u, GetExecutionCountForExtension(extension->id())); |
239 | 238 |
240 // Request an injection. There should be an action visible, but no executions. | 239 // Request an injection. The extension should want to run, but not execute. |
241 RequestInjection(extension); | 240 RequestInjection(extension); |
242 EXPECT_TRUE(controller()->GetActionForExtension(extension)); | 241 EXPECT_TRUE(controller()->WantsToRun(extension)); |
243 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); | 242 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); |
244 | 243 |
245 // Reload. This should remove the pending injection, and we should not | 244 // Reload. This should remove the pending injection, and we should not |
246 // execute anything. | 245 // execute anything. |
247 Reload(); | 246 Reload(); |
248 EXPECT_FALSE(controller()->GetActionForExtension(extension)); | 247 EXPECT_FALSE(controller()->WantsToRun(extension)); |
249 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); | 248 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); |
250 | 249 |
251 // Request and accept a new injection. | 250 // Request and accept a new injection. |
252 RequestInjection(extension); | 251 RequestInjection(extension); |
253 controller()->OnClicked(extension); | 252 controller()->OnClicked(extension); |
254 | 253 |
255 // The extension should only have executed once, even though a grand total | 254 // The extension should only have executed once, even though a grand total |
256 // of two executions were requested. | 255 // of two executions were requested. |
257 EXPECT_EQ(1u, GetExecutionCountForExtension(extension->id())); | 256 EXPECT_EQ(1u, GetExecutionCountForExtension(extension->id())); |
258 EXPECT_FALSE(controller()->GetActionForExtension(extension)); | 257 EXPECT_FALSE(controller()->WantsToRun(extension)); |
259 } | 258 } |
260 | 259 |
261 // Test that queueing multiple pending injections, and then accepting, triggers | 260 // Test that queueing multiple pending injections, and then accepting, triggers |
262 // them all. | 261 // them all. |
263 TEST_F(ActiveScriptControllerUnitTest, MultiplePendingInjection) { | 262 TEST_F(ActiveScriptControllerUnitTest, MultiplePendingInjection) { |
264 const Extension* extension = AddExtension(); | 263 const Extension* extension = AddExtension(); |
265 ASSERT_TRUE(extension); | 264 ASSERT_TRUE(extension); |
266 NavigateAndCommit(GURL("https://www.google.com")); | 265 NavigateAndCommit(GURL("https://www.google.com")); |
267 | 266 |
268 ASSERT_EQ(0u, GetExecutionCountForExtension(extension->id())); | 267 ASSERT_EQ(0u, GetExecutionCountForExtension(extension->id())); |
269 | 268 |
270 const size_t kNumInjections = 3u; | 269 const size_t kNumInjections = 3u; |
271 // Queue multiple pending injections. | 270 // Queue multiple pending injections. |
272 for (size_t i = 0u; i < kNumInjections; ++i) | 271 for (size_t i = 0u; i < kNumInjections; ++i) |
273 RequestInjection(extension); | 272 RequestInjection(extension); |
274 | 273 |
275 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); | 274 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); |
276 | 275 |
277 controller()->OnClicked(extension); | 276 controller()->OnClicked(extension); |
278 | 277 |
279 // All pending injections should have executed. | 278 // All pending injections should have executed. |
280 EXPECT_EQ(kNumInjections, GetExecutionCountForExtension(extension->id())); | 279 EXPECT_EQ(kNumInjections, GetExecutionCountForExtension(extension->id())); |
281 EXPECT_FALSE(controller()->GetActionForExtension(extension)); | 280 EXPECT_FALSE(controller()->WantsToRun(extension)); |
282 } | 281 } |
283 | 282 |
284 TEST_F(ActiveScriptControllerUnitTest, ActiveScriptsUseActiveTabPermissions) { | 283 TEST_F(ActiveScriptControllerUnitTest, ActiveScriptsUseActiveTabPermissions) { |
285 const Extension* extension = AddExtension(); | 284 const Extension* extension = AddExtension(); |
286 NavigateAndCommit(GURL("https://www.google.com")); | 285 NavigateAndCommit(GURL("https://www.google.com")); |
287 | 286 |
288 ActiveTabPermissionGranter* active_tab_permission_granter = | 287 ActiveTabPermissionGranter* active_tab_permission_granter = |
289 TabHelper::FromWebContents(web_contents()) | 288 TabHelper::FromWebContents(web_contents()) |
290 ->active_tab_permission_granter(); | 289 ->active_tab_permission_granter(); |
291 ASSERT_TRUE(active_tab_permission_granter); | 290 ASSERT_TRUE(active_tab_permission_granter); |
(...skipping 16 matching lines...) Expand all Loading... |
308 | 307 |
309 // Navigating to a different origin will require user consent again. | 308 // Navigating to a different origin will require user consent again. |
310 NavigateAndCommit(GURL("https://yahoo.com")); | 309 NavigateAndCommit(GURL("https://yahoo.com")); |
311 EXPECT_TRUE(RequiresUserConsent(extension)); | 310 EXPECT_TRUE(RequiresUserConsent(extension)); |
312 | 311 |
313 // Back to the original origin should also re-require constent. | 312 // Back to the original origin should also re-require constent. |
314 NavigateAndCommit(GURL("https://www.google.com")); | 313 NavigateAndCommit(GURL("https://www.google.com")); |
315 EXPECT_TRUE(RequiresUserConsent(extension)); | 314 EXPECT_TRUE(RequiresUserConsent(extension)); |
316 | 315 |
317 RequestInjection(extension); | 316 RequestInjection(extension); |
318 EXPECT_TRUE(controller()->GetActionForExtension(extension)); | 317 EXPECT_TRUE(controller()->WantsToRun(extension)); |
319 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); | 318 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); |
320 | 319 |
321 // Grant active tab. | 320 // Grant active tab. |
322 active_tab_permission_granter->GrantIfRequested(extension); | 321 active_tab_permission_granter->GrantIfRequested(extension); |
323 | 322 |
324 // The pending injections should have run since active tab permission was | 323 // The pending injections should have run since active tab permission was |
325 // granted. | 324 // granted. |
326 EXPECT_EQ(1u, GetExecutionCountForExtension(extension->id())); | 325 EXPECT_EQ(1u, GetExecutionCountForExtension(extension->id())); |
327 EXPECT_FALSE(controller()->GetActionForExtension(extension)); | 326 EXPECT_FALSE(controller()->WantsToRun(extension)); |
328 } | 327 } |
329 | 328 |
330 TEST_F(ActiveScriptControllerUnitTest, ActiveScriptsCanHaveAllUrlsPref) { | 329 TEST_F(ActiveScriptControllerUnitTest, ActiveScriptsCanHaveAllUrlsPref) { |
331 const Extension* extension = AddExtension(); | 330 const Extension* extension = AddExtension(); |
332 ASSERT_TRUE(extension); | 331 ASSERT_TRUE(extension); |
333 | 332 |
334 NavigateAndCommit(GURL("https://www.google.com")); | 333 NavigateAndCommit(GURL("https://www.google.com")); |
335 EXPECT_TRUE(RequiresUserConsent(extension)); | 334 EXPECT_TRUE(RequiresUserConsent(extension)); |
336 | 335 |
337 // Enable the extension on all urls. | 336 // Enable the extension on all urls. |
(...skipping 14 matching lines...) Expand all Loading... |
352 } | 351 } |
353 | 352 |
354 TEST_F(ActiveScriptControllerUnitTest, TestAlwaysRun) { | 353 TEST_F(ActiveScriptControllerUnitTest, TestAlwaysRun) { |
355 const Extension* extension = AddExtension(); | 354 const Extension* extension = AddExtension(); |
356 ASSERT_TRUE(extension); | 355 ASSERT_TRUE(extension); |
357 | 356 |
358 NavigateAndCommit(GURL("https://www.google.com/?gws_rd=ssl")); | 357 NavigateAndCommit(GURL("https://www.google.com/?gws_rd=ssl")); |
359 | 358 |
360 // Ensure that there aren't any executions pending. | 359 // Ensure that there aren't any executions pending. |
361 ASSERT_EQ(0u, GetExecutionCountForExtension(extension->id())); | 360 ASSERT_EQ(0u, GetExecutionCountForExtension(extension->id())); |
362 ASSERT_FALSE(controller()->GetActionForExtension(extension)); | 361 ASSERT_FALSE(controller()->WantsToRun(extension)); |
363 | 362 |
364 // Since the extension requests all_hosts, we should require user consent. | 363 // Since the extension requests all_hosts, we should require user consent. |
365 EXPECT_TRUE(RequiresUserConsent(extension)); | 364 EXPECT_TRUE(RequiresUserConsent(extension)); |
366 | 365 |
367 // Request an injection. There should be an action visible, but no executions. | 366 // Request an injection. The extension should want to run, but not execute. |
368 RequestInjection(extension); | 367 RequestInjection(extension); |
369 EXPECT_TRUE(controller()->GetActionForExtension(extension)); | 368 EXPECT_TRUE(controller()->WantsToRun(extension)); |
370 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); | 369 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); |
371 | 370 |
372 // Allow the extension to always run on this origin. | 371 // Allow the extension to always run on this origin. |
373 controller()->AlwaysRunOnVisibleOrigin(extension); | 372 controller()->AlwaysRunOnVisibleOrigin(extension); |
374 | 373 |
375 // The extension should execute, and the action should go away. | 374 // The extension should execute, and the extension shouldn't want to run. |
376 EXPECT_EQ(1u, GetExecutionCountForExtension(extension->id())); | 375 EXPECT_EQ(1u, GetExecutionCountForExtension(extension->id())); |
377 EXPECT_FALSE(controller()->GetActionForExtension(extension)); | 376 EXPECT_FALSE(controller()->WantsToRun(extension)); |
378 | 377 |
379 // Since we already executed on the given page, we shouldn't need permission | 378 // Since we already executed on the given page, we shouldn't need permission |
380 // for a second time. | 379 // for a second time. |
381 EXPECT_FALSE(RequiresUserConsent(extension)); | 380 EXPECT_FALSE(RequiresUserConsent(extension)); |
382 | 381 |
383 // Navigating to another site that hasn't been granted a persisted permission | 382 // Navigating to another site that hasn't been granted a persisted permission |
384 // should necessitate user consent. | 383 // should necessitate user consent. |
385 NavigateAndCommit(GURL("https://www.foo.com/bar")); | 384 NavigateAndCommit(GURL("https://www.foo.com/bar")); |
386 EXPECT_TRUE(RequiresUserConsent(extension)); | 385 EXPECT_TRUE(RequiresUserConsent(extension)); |
387 | 386 |
(...skipping 14 matching lines...) Expand all Loading... |
402 EXPECT_TRUE(RequiresUserConsent(extension)); | 401 EXPECT_TRUE(RequiresUserConsent(extension)); |
403 // Different subdomain... | 402 // Different subdomain... |
404 NavigateAndCommit(GURL("https://en.google.com/foo/bar")); | 403 NavigateAndCommit(GURL("https://en.google.com/foo/bar")); |
405 EXPECT_TRUE(RequiresUserConsent(extension)); | 404 EXPECT_TRUE(RequiresUserConsent(extension)); |
406 // Only the "always run" origin should be allowed to run without user consent. | 405 // Only the "always run" origin should be allowed to run without user consent. |
407 NavigateAndCommit(GURL("https://www.google.com/foo/bar")); | 406 NavigateAndCommit(GURL("https://www.google.com/foo/bar")); |
408 EXPECT_FALSE(RequiresUserConsent(extension)); | 407 EXPECT_FALSE(RequiresUserConsent(extension)); |
409 } | 408 } |
410 | 409 |
411 } // namespace extensions | 410 } // namespace extensions |
OLD | NEW |