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

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

Issue 2213603002: Prevent duplicate content script injection defined in manifest.json (reland) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed patch 6 code review comments Created 4 years, 3 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 <stddef.h> 5 #include <stddef.h>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/callback.h" 8 #include "base/callback.h"
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after
328 // could lead to spoofing). Unfortunately, host_resolver seems to have 328 // could lead to spoofing). Unfortunately, host_resolver seems to have
329 // problems with redirecting "chrome.google.com" to the test server, so we 329 // problems with redirecting "chrome.google.com" to the test server, so we
330 // can't use the real Webstore's URL. If this changes, we could clean this 330 // can't use the real Webstore's URL. If this changes, we could clean this
331 // up. 331 // up.
332 command_line->AppendSwitchASCII( 332 command_line->AppendSwitchASCII(
333 switches::kAppsGalleryURL, 333 switches::kAppsGalleryURL,
334 base::StringPrintf("http://%s", kWebstoreDomain)); 334 base::StringPrintf("http://%s", kWebstoreDomain));
335 } 335 }
336 }; 336 };
337 337
338 IN_PROC_BROWSER_TEST_F(ExtensionApiTest,
339 ContentScriptDuplicateScriptInjection) {
340 host_resolver()->AddRule("maps.google.com", "127.0.0.1");
341 ASSERT_TRUE(StartEmbeddedTestServer());
342
343 GURL url(
344 base::StringPrintf("http://maps.google.com:%i/extensions/test_file.html",
345 embedded_test_server()->port()));
346
347 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
348 "content_scripts/duplicate_script_injection")));
349
350 ui_test_utils::NavigateToURL(browser(), url);
351
352 // Test that a script that matches two separate, yet overlapping match
353 // patterns is only injected once.
354 bool scripts_injected_once = false;
355 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
356 browser()->tab_strip_model()->GetActiveWebContents(),
357 "window.domAutomationController.send("
358 "document.getElementsByClassName('injected-once')"
359 ".length == 1)",
360 &scripts_injected_once));
361 ASSERT_TRUE(scripts_injected_once);
362
363 // Test that a script injected at two different load process times, document
364 // idle and document end, is injected exactly twice.
365 bool scripts_injected_twice = false;
366 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
367 browser()->tab_strip_model()->GetActiveWebContents(),
368 "window.domAutomationController.send("
369 "document.getElementsByClassName('injected-twice')"
370 ".length == 2)",
371 &scripts_injected_twice));
372 ASSERT_TRUE(scripts_injected_twice);
373 }
374
338 IN_PROC_BROWSER_TEST_F(ContentScriptCssInjectionTest, 375 IN_PROC_BROWSER_TEST_F(ContentScriptCssInjectionTest,
339 ContentScriptInjectsStyles) { 376 ContentScriptInjectsStyles) {
340 ASSERT_TRUE(StartEmbeddedTestServer()); 377 ASSERT_TRUE(StartEmbeddedTestServer());
341 host_resolver()->AddRule(kWebstoreDomain, "127.0.0.1"); 378 host_resolver()->AddRule(kWebstoreDomain, "127.0.0.1");
342 379
343 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("content_scripts") 380 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("content_scripts")
344 .AppendASCII("css_injection"))); 381 .AppendASCII("css_injection")));
345 382
346 // CSS injection should be allowed on an aribitrary web page. 383 // CSS injection should be allowed on an aribitrary web page.
347 GURL url = 384 GURL url =
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after
629 ExtensionTestMessageListener iframe_loaded_listener("iframe loaded", false); 666 ExtensionTestMessageListener iframe_loaded_listener("iframe loaded", false);
630 ExtensionTestMessageListener content_script_listener("script injected", 667 ExtensionTestMessageListener content_script_listener("script injected",
631 false); 668 false);
632 LoadExtension(data_dir.AppendASCII("script_a_com")); 669 LoadExtension(data_dir.AppendASCII("script_a_com"));
633 LoadExtension(data_dir.AppendASCII("background_page_iframe")); 670 LoadExtension(data_dir.AppendASCII("background_page_iframe"));
634 iframe_loaded_listener.WaitUntilSatisfied(); 671 iframe_loaded_listener.WaitUntilSatisfied();
635 EXPECT_FALSE(content_script_listener.was_satisfied()); 672 EXPECT_FALSE(content_script_listener.was_satisfied());
636 } 673 }
637 674
638 } // namespace extensions 675 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698