1 // Copyright (c) 2013 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/strings/stringprintf.h" 6 #include "chrome/browser/extensions/activity_log/activity_log.h" 7 #include "chrome/browser/extensions/extension_apitest.h" 8 #include "chrome/browser/extensions/extension_browsertest.h" 9 #include "chrome/browser/prerender/prerender_handle.h" 10 #include "chrome/browser/prerender/prerender_manager.h" 11 #include "chrome/browser/prerender/prerender_manager_factory.h" 12 #include "chrome/browser/ui/tabs/tab_strip_model.h" 13 #include "chrome/common/chrome_switches.h" 14 #include "chrome/test/base/ui_test_utils.h" 15 #include "content/public/browser/browser_context.h" 16 #include "content/public/browser/browser_thread.h" 17 #include "content/public/browser/notification_service.h" 18 #include "content/public/browser/storage_partition.h" 19 #include "content/public/test/browser_test_utils.h" 20 #include "net/dns/mock_host_resolver.h" 21 #include "net/test/embedded_test_server/embedded_test_server.h" 22 23 namespace extensions { 24 25 // Only the prerender tests are in this file. To add tests for activity 26 // logging please see: 27 // chrome/test/data/extensions/api_test/activity_log_private/README 28 29 class ActivityLogPrerenderTest : public ExtensionApiTest { 30 protected: 31 // Make sure the activity log is turned on. 32 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { 33 ExtensionBrowserTest::SetUpCommandLine(command_line); 34 command_line->AppendSwitch(switches::kEnableExtensionActivityLogging); 35 command_line->AppendSwitchASCII(switches::kPrerenderMode, 36 switches::kPrerenderModeSwitchValueEnabled); 37 } 38 39 static void Prerender_Arguments( 40 const std::string& extension_id, 41 int port, 42 scoped_ptr<std::vector<scoped_refptr<Action> > > i) { 43 // This is to exit RunLoop (base::MessageLoop::current()->Run()) below 44 base::MessageLoop::current()->PostTask( 45 FROM_HERE, base::MessageLoop::QuitClosure()); 46 47 ASSERT_TRUE(i->size()); 48 scoped_refptr<Action> last = i->front(); 49 50 ASSERT_EQ(extension_id, last->extension_id()); 51 ASSERT_EQ(Action::ACTION_CONTENT_SCRIPT, last->action_type()); 52 ASSERT_EQ("[\"/google_cs.js\"]", 53 ActivityLogPolicy::Util::Serialize(last->args())); 54 ASSERT_EQ( 55 base::StringPrintf("http://www.google.com.bo:%d/test.html", port), 56 last->SerializePageUrl()); 57 ASSERT_EQ( 58 base::StringPrintf("www.google.com.bo:%d/test.html", port), 59 last->page_title()); 60 ASSERT_EQ("{\"prerender\":true}", 61 ActivityLogPolicy::Util::Serialize(last->other())); 62 ASSERT_EQ("", last->api_name()); 63 ASSERT_EQ("", last->SerializeArgUrl()); 64 } 65 }; 66 67 IN_PROC_BROWSER_TEST_F(ActivityLogPrerenderTest, TestScriptInjected) { 68 host_resolver()->AddRule("*", "127.0.0.1"); 69 ASSERT_TRUE(StartEmbeddedTestServer()); 70 int port = embedded_test_server()->port(); 71 72 // Get the extension (chrome/test/data/extensions/activity_log) 73 const Extension* ext = 74 LoadExtension(test_data_dir_.AppendASCII("activity_log")); 75 ASSERT_TRUE(ext); 76 77 ActivityLog* activity_log = ActivityLog::GetInstance(profile()); 78 ASSERT_TRUE(activity_log); 79 80 //Disable rate limiting in PrerenderManager 81 prerender::PrerenderManager* prerender_manager = 82 prerender::PrerenderManagerFactory::GetForProfile(profile()); 83 ASSERT_TRUE(prerender_manager); 84 prerender_manager->mutable_config().rate_limit_enabled = false; 85 // Increase prerenderer limits, otherwise this test fails 86 // on Windows XP. 87 prerender_manager->mutable_config().max_bytes = 1000 * 1024 * 1024; 88 prerender_manager->mutable_config().time_to_live = 89 base::TimeDelta::FromMinutes(10); 90 prerender_manager->mutable_config().abandon_time_to_live = 91 base::TimeDelta::FromMinutes(10); 92 93 content::WebContents* web_contents = 94 browser()->tab_strip_model()->GetActiveWebContents(); 95 ASSERT_TRUE(web_contents); 96 97 content::WindowedNotificationObserver page_observer( 98 content::NOTIFICATION_LOAD_STOP, 99 content::NotificationService::AllSources()); 100 101 GURL url(base::StringPrintf( 102 "http://www.google.com.bo:%d/test.html", 103 port)); 104 105 if (!prerender_manager->cookie_store_loaded()) { 106 base::RunLoop loop; 107 prerender_manager->set_on_cookie_store_loaded_cb_for_testing( 108 loop.QuitClosure()); 109 loop.Run(); 110 } 111 112 const gfx::Size kSize(640, 480); 113 scoped_ptr<prerender::PrerenderHandle> prerender_handle( 114 prerender_manager->AddPrerenderFromLocalPredictor( 115 url, 116 web_contents->GetController().GetDefaultSessionStorageNamespace(), 117 kSize)); 118 119 page_observer.Wait(); 120 121 activity_log->GetFilteredActions( 122 ext->id(), 123 Action::ACTION_ANY, 124 "", 125 "", 126 "", 127 -1, 128 base::Bind( 129 ActivityLogPrerenderTest::Prerender_Arguments, ext->id(), port)); 130 131 // Allow invocation of Prerender_Arguments 132 base::MessageLoop::current()->Run(); 133 } 134 135 } // namespace extensions 136