Home | History | Annotate | Download | only in extensions
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "base/command_line.h"
      6 #include "chrome/browser/extensions/extension_apitest.h"
      7 #include "chrome/browser/extensions/extension_service.h"
      8 #include "chrome/browser/extensions/extension_system.h"
      9 #include "chrome/browser/extensions/extension_test_message_listener.h"
     10 #include "chrome/browser/profiles/profile.h"
     11 #include "chrome/browser/ui/browser.h"
     12 #include "chrome/test/base/test_switches.h"
     13 #include "chrome/test/base/ui_test_utils.h"
     14 #include "extensions/common/extension.h"
     15 #include "extensions/common/extensions_client.h"
     16 #include "extensions/common/id_util.h"
     17 
     18 const std::string kAllUrlsTarget =
     19     "files/extensions/api_test/all_urls/index.html";
     20 
     21 typedef ExtensionApiTest AllUrlsApiTest;
     22 
     23 #if defined(OS_WIN) && !defined(NDEBUG)
     24 // http://crbug.com/174341
     25 #define MAYBE_WhitelistedExtension DISABLED_WhitelistedExtension
     26 #else
     27 #define MAYBE_WhitelistedExtension WhitelistedExtension
     28 #endif
     29 IN_PROC_BROWSER_TEST_F(AllUrlsApiTest, MAYBE_WhitelistedExtension) {
     30 #if defined(OS_WIN) && defined(USE_ASH)
     31   // Disable this test in Metro+Ash for now (http://crbug.com/262796).
     32   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
     33     return;
     34 #endif
     35 
     36   // First setup the two extensions.
     37   base::FilePath extension_dir1 = test_data_dir_.AppendASCII("all_urls")
     38                                           .AppendASCII("content_script");
     39   base::FilePath extension_dir2 = test_data_dir_.AppendASCII("all_urls")
     40                                           .AppendASCII("execute_script");
     41 
     42   // Then add the two extensions to the whitelist.
     43   extensions::ExtensionsClient::ScriptingWhitelist whitelist;
     44   whitelist.push_back(extensions::id_util::GenerateIdForPath(extension_dir1));
     45   whitelist.push_back(extensions::id_util::GenerateIdForPath(extension_dir2));
     46   extensions::ExtensionsClient::Get()->SetScriptingWhitelist(whitelist);
     47 
     48   // Then load extensions.
     49   ExtensionService* service = extensions::ExtensionSystem::Get(
     50       browser()->profile())->extension_service();
     51   const size_t size_before = service->extensions()->size();
     52   ASSERT_TRUE(LoadExtension(extension_dir1));
     53   ASSERT_TRUE(LoadExtension(extension_dir2));
     54   EXPECT_EQ(size_before + 2, service->extensions()->size());
     55 
     56   std::string url;
     57 
     58   // Now verify we run content scripts on chrome://newtab/.
     59   url = "chrome://newtab/";
     60   ExtensionTestMessageListener listener1a("content script: " + url, false);
     61   ExtensionTestMessageListener listener1b("execute: " + url, false);
     62   ui_test_utils::NavigateToURL(browser(), GURL(url));
     63   ASSERT_TRUE(listener1a.WaitUntilSatisfied());
     64   ASSERT_TRUE(listener1b.WaitUntilSatisfied());
     65 
     66   // Now verify data: urls.
     67   url = "data:text/html;charset=utf-8,<html>asdf</html>";
     68   ExtensionTestMessageListener listener2a("content script: " + url, false);
     69   ExtensionTestMessageListener listener2b("execute: " + url, false);
     70   ui_test_utils::NavigateToURL(browser(), GURL(url));
     71   ASSERT_TRUE(listener2a.WaitUntilSatisfied());
     72   ASSERT_TRUE(listener2b.WaitUntilSatisfied());
     73 
     74   // Now verify chrome://version/.
     75   url = "chrome://version/";
     76   ExtensionTestMessageListener listener3a("content script: " + url, false);
     77   ExtensionTestMessageListener listener3b("execute: " + url, false);
     78   ui_test_utils::NavigateToURL(browser(), GURL(url));
     79   ASSERT_TRUE(listener3a.WaitUntilSatisfied());
     80   ASSERT_TRUE(listener3b.WaitUntilSatisfied());
     81 
     82   // Now verify about:blank.
     83   url = "about:blank";
     84   ExtensionTestMessageListener listener4a("content script: " + url, false);
     85   ExtensionTestMessageListener listener4b("execute: " + url, false);
     86   ui_test_utils::NavigateToURL(browser(), GURL(url));
     87   ASSERT_TRUE(listener4a.WaitUntilSatisfied());
     88   ASSERT_TRUE(listener4b.WaitUntilSatisfied());
     89 
     90   // Now verify we can script a regular http page.
     91   ASSERT_TRUE(test_server()->Start());
     92   GURL page_url = test_server()->GetURL(kAllUrlsTarget);
     93   ExtensionTestMessageListener listener5a("content script: " + page_url.spec(),
     94                                           false);
     95   ExtensionTestMessageListener listener5b("execute: " + page_url.spec(), false);
     96   ui_test_utils::NavigateToURL(browser(), page_url);
     97   ASSERT_TRUE(listener5a.WaitUntilSatisfied());
     98   ASSERT_TRUE(listener5b.WaitUntilSatisfied());
     99 }
    100 
    101 // Test that an extension NOT whitelisted for scripting can ask for <all_urls>
    102 // and run scripts on non-restricted all pages.
    103 IN_PROC_BROWSER_TEST_F(AllUrlsApiTest, RegularExtensions) {
    104   // First load the two extensions.
    105   base::FilePath extension_dir1 = test_data_dir_.AppendASCII("all_urls")
    106                                           .AppendASCII("content_script");
    107   base::FilePath extension_dir2 = test_data_dir_.AppendASCII("all_urls")
    108                                           .AppendASCII("execute_script");
    109 
    110   ExtensionService* service = extensions::ExtensionSystem::Get(
    111       browser()->profile())->extension_service();
    112   const size_t size_before = service->extensions()->size();
    113   ASSERT_TRUE(LoadExtension(extension_dir1));
    114   ASSERT_TRUE(LoadExtension(extension_dir2));
    115   EXPECT_EQ(size_before + 2, service->extensions()->size());
    116 
    117   // Now verify we can script a regular http page.
    118   ASSERT_TRUE(test_server()->Start());
    119   GURL page_url = test_server()->GetURL(kAllUrlsTarget);
    120   ExtensionTestMessageListener listener1a("content script: " + page_url.spec(),
    121                                           false);
    122   ExtensionTestMessageListener listener1b("execute: " + page_url.spec(), false);
    123   ui_test_utils::NavigateToURL(browser(), page_url);
    124   ASSERT_TRUE(listener1a.WaitUntilSatisfied());
    125   ASSERT_TRUE(listener1b.WaitUntilSatisfied());
    126 }
    127