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

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

Issue 1804123003: [Extensions] Refactor extension action execution (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 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 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 <stddef.h> 5 #include <stddef.h>
6 6
7 #include <map> 7 #include <map>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/values.h" 10 #include "base/values.h"
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
192 TEST_F(ExtensionActionRunnerUnitTest, RequestPermissionAndExecute) { 192 TEST_F(ExtensionActionRunnerUnitTest, RequestPermissionAndExecute) {
193 const Extension* extension = AddExtension(); 193 const Extension* extension = AddExtension();
194 ASSERT_TRUE(extension); 194 ASSERT_TRUE(extension);
195 195
196 NavigateAndCommit(GURL("https://www.google.com")); 196 NavigateAndCommit(GURL("https://www.google.com"));
197 197
198 // Ensure that there aren't any executions pending. 198 // Ensure that there aren't any executions pending.
199 ASSERT_EQ(0u, GetExecutionCountForExtension(extension->id())); 199 ASSERT_EQ(0u, GetExecutionCountForExtension(extension->id()));
200 ASSERT_FALSE(runner()->WantsToRun(extension)); 200 ASSERT_FALSE(runner()->WantsToRun(extension));
201 201
202 ExtensionActionAPI* extension_action_api = ExtensionActionAPI::Get(profile());
203 ASSERT_FALSE(extension_action_api->HasBeenBlocked(extension, web_contents()));
204
205 // Since the extension requests all_hosts, we should require user consent. 202 // Since the extension requests all_hosts, we should require user consent.
206 EXPECT_TRUE(RequiresUserConsent(extension)); 203 EXPECT_TRUE(RequiresUserConsent(extension));
207 204
208 // Request an injection. The extension should want to run, but should not have 205 // Request an injection. The extension should want to run, but should not have
209 // executed. 206 // executed.
210 RequestInjection(extension); 207 RequestInjection(extension);
211 EXPECT_TRUE(runner()->WantsToRun(extension)); 208 EXPECT_TRUE(runner()->WantsToRun(extension));
212 EXPECT_TRUE(extension_action_api->HasBeenBlocked(extension, web_contents()));
213 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); 209 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id()));
214 210
215 // Click to accept the extension executing. 211 // Click to accept the extension executing.
216 runner()->OnClicked(extension); 212 runner()->RunAction(extension, true);
217 213
218 // The extension should execute, and the extension shouldn't want to run. 214 // The extension should execute, and the extension shouldn't want to run.
219 EXPECT_EQ(1u, GetExecutionCountForExtension(extension->id())); 215 EXPECT_EQ(1u, GetExecutionCountForExtension(extension->id()));
220 EXPECT_FALSE(runner()->WantsToRun(extension)); 216 EXPECT_FALSE(runner()->WantsToRun(extension));
221 EXPECT_FALSE(extension_action_api->HasBeenBlocked(extension, web_contents()));
222 217
223 // Since we already executed on the given page, we shouldn't need permission 218 // Since we already executed on the given page, we shouldn't need permission
224 // for a second time. 219 // for a second time.
225 EXPECT_FALSE(RequiresUserConsent(extension)); 220 EXPECT_FALSE(RequiresUserConsent(extension));
226 221
227 // Reloading and same-origin navigations shouldn't clear those permissions, 222 // Reloading and same-origin navigations shouldn't clear those permissions,
228 // and we shouldn't require user constent again. 223 // and we shouldn't require user constent again.
229 Reload(); 224 Reload();
230 EXPECT_FALSE(RequiresUserConsent(extension)); 225 EXPECT_FALSE(RequiresUserConsent(extension));
231 NavigateAndCommit(GURL("https://www.google.com/foo")); 226 NavigateAndCommit(GURL("https://www.google.com/foo"));
232 EXPECT_FALSE(RequiresUserConsent(extension)); 227 EXPECT_FALSE(RequiresUserConsent(extension));
233 NavigateAndCommit(GURL("https://www.google.com/bar")); 228 NavigateAndCommit(GURL("https://www.google.com/bar"));
234 EXPECT_FALSE(RequiresUserConsent(extension)); 229 EXPECT_FALSE(RequiresUserConsent(extension));
235 230
236 // Cross-origin navigations should clear permissions. 231 // Cross-origin navigations should clear permissions.
237 NavigateAndCommit(GURL("https://otherdomain.google.com")); 232 NavigateAndCommit(GURL("https://otherdomain.google.com"));
238 EXPECT_TRUE(RequiresUserConsent(extension)); 233 EXPECT_TRUE(RequiresUserConsent(extension));
239 234
240 // Grant access. 235 // Grant access.
241 RequestInjection(extension); 236 RequestInjection(extension);
242 runner()->OnClicked(extension); 237 runner()->RunAction(extension, true);
243 EXPECT_EQ(2u, GetExecutionCountForExtension(extension->id())); 238 EXPECT_EQ(2u, GetExecutionCountForExtension(extension->id()));
244 EXPECT_FALSE(runner()->WantsToRun(extension)); 239 EXPECT_FALSE(runner()->WantsToRun(extension));
245 240
246 // Navigating to another site should also clear the permissions. 241 // Navigating to another site should also clear the permissions.
247 NavigateAndCommit(GURL("https://www.foo.com")); 242 NavigateAndCommit(GURL("https://www.foo.com"));
248 EXPECT_TRUE(RequiresUserConsent(extension)); 243 EXPECT_TRUE(RequiresUserConsent(extension));
249 } 244 }
250 245
251 // Test that injections that are not executed by the time the user navigates are 246 // Test that injections that are not executed by the time the user navigates are
252 // ignored and never execute. 247 // ignored and never execute.
(...skipping 11 matching lines...) Expand all
264 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); 259 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id()));
265 260
266 // Reload. This should remove the pending injection, and we should not 261 // Reload. This should remove the pending injection, and we should not
267 // execute anything. 262 // execute anything.
268 Reload(); 263 Reload();
269 EXPECT_FALSE(runner()->WantsToRun(extension)); 264 EXPECT_FALSE(runner()->WantsToRun(extension));
270 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); 265 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id()));
271 266
272 // Request and accept a new injection. 267 // Request and accept a new injection.
273 RequestInjection(extension); 268 RequestInjection(extension);
274 runner()->OnClicked(extension); 269 runner()->RunAction(extension, true);
275 270
276 // The extension should only have executed once, even though a grand total 271 // The extension should only have executed once, even though a grand total
277 // of two executions were requested. 272 // of two executions were requested.
278 EXPECT_EQ(1u, GetExecutionCountForExtension(extension->id())); 273 EXPECT_EQ(1u, GetExecutionCountForExtension(extension->id()));
279 EXPECT_FALSE(runner()->WantsToRun(extension)); 274 EXPECT_FALSE(runner()->WantsToRun(extension));
280 } 275 }
281 276
282 // Test that queueing multiple pending injections, and then accepting, triggers 277 // Test that queueing multiple pending injections, and then accepting, triggers
283 // them all. 278 // them all.
284 TEST_F(ExtensionActionRunnerUnitTest, MultiplePendingInjection) { 279 TEST_F(ExtensionActionRunnerUnitTest, MultiplePendingInjection) {
285 const Extension* extension = AddExtension(); 280 const Extension* extension = AddExtension();
286 ASSERT_TRUE(extension); 281 ASSERT_TRUE(extension);
287 NavigateAndCommit(GURL("https://www.google.com")); 282 NavigateAndCommit(GURL("https://www.google.com"));
288 283
289 ASSERT_EQ(0u, GetExecutionCountForExtension(extension->id())); 284 ASSERT_EQ(0u, GetExecutionCountForExtension(extension->id()));
290 285
291 const size_t kNumInjections = 3u; 286 const size_t kNumInjections = 3u;
292 // Queue multiple pending injections. 287 // Queue multiple pending injections.
293 for (size_t i = 0u; i < kNumInjections; ++i) 288 for (size_t i = 0u; i < kNumInjections; ++i)
294 RequestInjection(extension); 289 RequestInjection(extension);
295 290
296 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); 291 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id()));
297 292
298 runner()->OnClicked(extension); 293 runner()->RunAction(extension, true);
299 294
300 // All pending injections should have executed. 295 // All pending injections should have executed.
301 EXPECT_EQ(kNumInjections, GetExecutionCountForExtension(extension->id())); 296 EXPECT_EQ(kNumInjections, GetExecutionCountForExtension(extension->id()));
302 EXPECT_FALSE(runner()->WantsToRun(extension)); 297 EXPECT_FALSE(runner()->WantsToRun(extension));
303 } 298 }
304 299
305 TEST_F(ExtensionActionRunnerUnitTest, ActiveScriptsUseActiveTabPermissions) { 300 TEST_F(ExtensionActionRunnerUnitTest, ActiveScriptsUseActiveTabPermissions) {
306 const Extension* extension = AddExtension(); 301 const Extension* extension = AddExtension();
307 NavigateAndCommit(GURL("https://www.google.com")); 302 NavigateAndCommit(GURL("https://www.google.com"));
308 303
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
386 EXPECT_TRUE(RequiresUserConsent(extension)); 381 EXPECT_TRUE(RequiresUserConsent(extension));
387 382
388 // Request an injection. The extension should want to run, but not execute. 383 // Request an injection. The extension should want to run, but not execute.
389 RequestInjection(extension); 384 RequestInjection(extension);
390 EXPECT_TRUE(runner()->WantsToRun(extension)); 385 EXPECT_TRUE(runner()->WantsToRun(extension));
391 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); 386 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id()));
392 387
393 // Allow the extension to always run on this origin. 388 // Allow the extension to always run on this origin.
394 ScriptingPermissionsModifier modifier(profile(), extension); 389 ScriptingPermissionsModifier modifier(profile(), extension);
395 modifier.GrantHostPermission(web_contents()->GetLastCommittedURL()); 390 modifier.GrantHostPermission(web_contents()->GetLastCommittedURL());
396 runner()->OnClicked(extension); 391 runner()->RunAction(extension, true);
397 392
398 // The extension should execute, and the extension shouldn't want to run. 393 // The extension should execute, and the extension shouldn't want to run.
399 EXPECT_EQ(1u, GetExecutionCountForExtension(extension->id())); 394 EXPECT_EQ(1u, GetExecutionCountForExtension(extension->id()));
400 EXPECT_FALSE(runner()->WantsToRun(extension)); 395 EXPECT_FALSE(runner()->WantsToRun(extension));
401 396
402 // Since we already executed on the given page, we shouldn't need permission 397 // Since we already executed on the given page, we shouldn't need permission
403 // for a second time. 398 // for a second time.
404 EXPECT_FALSE(RequiresUserConsent(extension)); 399 EXPECT_FALSE(RequiresUserConsent(extension));
405 400
406 // Navigating to another site that hasn't been granted a persisted permission 401 // Navigating to another site that hasn't been granted a persisted permission
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
442 RequestInjection(extension, UserScript::DOCUMENT_END); 437 RequestInjection(extension, UserScript::DOCUMENT_END);
443 EXPECT_EQ(BLOCKED_ACTION_SCRIPT_OTHER, 438 EXPECT_EQ(BLOCKED_ACTION_SCRIPT_OTHER,
444 runner()->GetBlockedActions(extension)); 439 runner()->GetBlockedActions(extension));
445 RequestInjection(extension, UserScript::DOCUMENT_IDLE); 440 RequestInjection(extension, UserScript::DOCUMENT_IDLE);
446 EXPECT_EQ(BLOCKED_ACTION_SCRIPT_OTHER, 441 EXPECT_EQ(BLOCKED_ACTION_SCRIPT_OTHER,
447 runner()->GetBlockedActions(extension)); 442 runner()->GetBlockedActions(extension));
448 RequestInjection(extension, UserScript::DOCUMENT_START); 443 RequestInjection(extension, UserScript::DOCUMENT_START);
449 EXPECT_EQ(BLOCKED_ACTION_SCRIPT_AT_START | BLOCKED_ACTION_SCRIPT_OTHER, 444 EXPECT_EQ(BLOCKED_ACTION_SCRIPT_AT_START | BLOCKED_ACTION_SCRIPT_OTHER,
450 runner()->GetBlockedActions(extension)); 445 runner()->GetBlockedActions(extension));
451 446
452 runner()->OnClicked(extension); 447 runner()->RunAction(extension, true);
453 EXPECT_EQ(BLOCKED_ACTION_NONE, runner()->GetBlockedActions(extension)); 448 EXPECT_EQ(BLOCKED_ACTION_NONE, runner()->GetBlockedActions(extension));
454 } 449 }
455 450
456 TEST_F(ExtensionActionRunnerUnitTest, TestWebRequestBlocked) { 451 TEST_F(ExtensionActionRunnerUnitTest, TestWebRequestBlocked) {
457 const Extension* extension = AddExtension(); 452 const Extension* extension = AddExtension();
458 ASSERT_TRUE(extension); 453 ASSERT_TRUE(extension);
459 454
460 NavigateAndCommit(GURL("https://www.foo.com")); 455 NavigateAndCommit(GURL("https://www.foo.com"));
461 456
462 EXPECT_EQ(BLOCKED_ACTION_NONE, runner()->GetBlockedActions(extension)); 457 EXPECT_EQ(BLOCKED_ACTION_NONE, runner()->GetBlockedActions(extension));
463 EXPECT_FALSE(runner()->WantsToRun(extension)); 458 EXPECT_FALSE(runner()->WantsToRun(extension));
464 459
465 runner()->OnWebRequestBlocked(extension); 460 runner()->OnWebRequestBlocked(extension);
466 EXPECT_EQ(BLOCKED_ACTION_WEB_REQUEST, runner()->GetBlockedActions(extension)); 461 EXPECT_EQ(BLOCKED_ACTION_WEB_REQUEST, runner()->GetBlockedActions(extension));
467 EXPECT_TRUE(runner()->WantsToRun(extension)); 462 EXPECT_TRUE(runner()->WantsToRun(extension));
468 463
469 RequestInjection(extension); 464 RequestInjection(extension);
470 EXPECT_EQ(BLOCKED_ACTION_WEB_REQUEST | BLOCKED_ACTION_SCRIPT_OTHER, 465 EXPECT_EQ(BLOCKED_ACTION_WEB_REQUEST | BLOCKED_ACTION_SCRIPT_OTHER,
471 runner()->GetBlockedActions(extension)); 466 runner()->GetBlockedActions(extension));
472 EXPECT_TRUE(runner()->WantsToRun(extension)); 467 EXPECT_TRUE(runner()->WantsToRun(extension));
473 468
474 NavigateAndCommit(GURL("https://www.bar.com")); 469 NavigateAndCommit(GURL("https://www.bar.com"));
475 EXPECT_EQ(BLOCKED_ACTION_NONE, runner()->GetBlockedActions(extension)); 470 EXPECT_EQ(BLOCKED_ACTION_NONE, runner()->GetBlockedActions(extension));
476 EXPECT_FALSE(runner()->WantsToRun(extension)); 471 EXPECT_FALSE(runner()->WantsToRun(extension));
477 } 472 }
478 473
479 } // namespace extensions 474 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698