diff options
author | rafaelw@chromium.org <rafaelw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-12-23 03:28:51 +0000 |
---|---|---|
committer | rafaelw@chromium.org <rafaelw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-12-23 03:28:51 +0000 |
commit | c931db6d8adbf334c86e31714e2897bc2e9db08a (patch) | |
tree | cf83d11e8e00e45a4733ad62c1a38f0925d8dd8c | |
parent | d146b8311c38186872d2be3764e2c2d66827107f (diff) | |
download | chromium_src-c931db6d8adbf334c86e31714e2897bc2e9db08a.zip chromium_src-c931db6d8adbf334c86e31714e2897bc2e9db08a.tar.gz chromium_src-c931db6d8adbf334c86e31714e2897bc2e9db08a.tar.bz2 |
Don't inject content scripts into incognito browsers.
This patch prevents content scripts from being injected into incognito profile tabs by only injecting into the same profile in which the extension is running.
BUG=21392
TEST=Load chrome/test/data/extensions/api/incognito_no_script and open any url in incognito browser. No page titles should be modified to "modified".
Review URL: http://codereview.chromium.org/502079
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@35203 0039d316-1c4b-4281-b951-d872f2087c98
10 files changed, 96 insertions, 17 deletions
diff --git a/chrome/browser/extensions/incognito_noscript_apitest.cc b/chrome/browser/extensions/incognito_noscript_apitest.cc new file mode 100755 index 0000000..69f3876 --- /dev/null +++ b/chrome/browser/extensions/incognito_noscript_apitest.cc @@ -0,0 +1,44 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/browser.h" +#include "chrome/browser/browser_list.h" +#include "chrome/browser/browser_window.h" +#include "chrome/browser/extensions/extension_browsertest.h" +#include "chrome/browser/profile.h" +#include "chrome/browser/tab_contents/tab_contents.h" +#include "chrome/common/url_constants.h" +#include "chrome/test/ui_test_utils.h" + +IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, IncognitoNoScript) { + host_resolver()->AddRule("*", "127.0.0.1"); + StartHTTPServer(); + + // Loads a simple extension which attempts to change the title of every page + // that loads to "modified". + FilePath extension_path = test_data_dir_.AppendASCII("api_test") + .AppendASCII("incognito_no_script"); + ASSERT_TRUE(LoadExtension(extension_path)); + + // Open incognito window and navigate to test page. + Browser::OpenURLOffTheRecord(browser()->profile(), + GURL("http://www.foo.com:1337/files/extensions/test_file.html")); + Profile* off_the_record_profile = + browser()->profile()->GetOffTheRecordProfile(); + Browser* otr_browser = Browser::Create(off_the_record_profile); + otr_browser->AddTabWithURL( + GURL("http://www.foo.com:1337/files/extensions/test_file.html"), + GURL(), + PageTransition::LINK, + true, + -1, + false, + NULL); + otr_browser->window()->Show(); + ui_test_utils::WaitForNavigationInCurrentTab(otr_browser); + + string16 title; + ui_test_utils::GetCurrentTabTitle(otr_browser, &title); + ASSERT_EQ("Unmodified", UTF16ToASCII(title)); +} diff --git a/chrome/browser/extensions/user_script_listener_unittest.cc b/chrome/browser/extensions/user_script_listener_unittest.cc index aea50cf..82a8880 100644 --- a/chrome/browser/extensions/user_script_listener_unittest.cc +++ b/chrome/browser/extensions/user_script_listener_unittest.cc @@ -33,8 +33,8 @@ class SimpleTestJob : public URLRequestTestJob { class MockUserScriptMaster : public UserScriptMaster { public: - explicit MockUserScriptMaster(const FilePath& script_dir) - : UserScriptMaster(script_dir) {} + explicit MockUserScriptMaster(const FilePath& script_dir, Profile* profile) + : UserScriptMaster(script_dir, profile) {} virtual void StartScan() { // Do nothing. We want to manually control when scans occur. @@ -237,7 +237,8 @@ class UserScriptListenerTest : public testing::Test { resource_tester_ = new ResourceDispatcherHostTester(); - master_ = new MockUserScriptMaster(profile_.GetExtensionsInstallDir()); + master_ = new MockUserScriptMaster(profile_.GetExtensionsInstallDir(), + &profile_); profile_.InitializeExtensionsService(); } diff --git a/chrome/browser/extensions/user_script_master.cc b/chrome/browser/extensions/user_script_master.cc index 06ec0c5..528f1b9 100644 --- a/chrome/browser/extensions/user_script_master.cc +++ b/chrome/browser/extensions/user_script_master.cc @@ -279,19 +279,20 @@ void UserScriptMaster::ScriptReloader::RunScan( } -UserScriptMaster::UserScriptMaster(const FilePath& script_dir) +UserScriptMaster::UserScriptMaster(const FilePath& script_dir, Profile* profile) : user_script_dir_(script_dir), extensions_service_ready_(false), - pending_scan_(false) { + pending_scan_(false), + profile_(profile) { if (!user_script_dir_.value().empty()) AddWatchedPath(script_dir); registrar_.Add(this, NotificationType::EXTENSIONS_READY, - NotificationService::AllSources()); + Source<Profile>(profile_)); registrar_.Add(this, NotificationType::EXTENSION_LOADED, - NotificationService::AllSources()); + Source<Profile>(profile_)); registrar_.Add(this, NotificationType::EXTENSION_UNLOADED, - NotificationService::AllSources()); + Source<Profile>(profile_)); } UserScriptMaster::~UserScriptMaster() { @@ -332,7 +333,7 @@ void UserScriptMaster::NewScriptsAvailable(base::SharedMemory* handle) { NotificationService::current()->Notify( NotificationType::USER_SCRIPTS_UPDATED, - NotificationService::AllSources(), + Source<Profile>(profile_), Details<base::SharedMemory>(handle)); } } diff --git a/chrome/browser/extensions/user_script_master.h b/chrome/browser/extensions/user_script_master.h index ff1e589..3a7afd5 100644 --- a/chrome/browser/extensions/user_script_master.h +++ b/chrome/browser/extensions/user_script_master.h @@ -12,6 +12,7 @@ #include "base/scoped_ptr.h" #include "base/shared_memory.h" #include "chrome/browser/chrome_thread.h" +#include "chrome/browser/profile.h" #include "chrome/common/extensions/user_script.h" #include "chrome/common/notification_registrar.h" #include "testing/gtest/include/gtest/gtest_prod.h" @@ -28,7 +29,7 @@ class UserScriptMaster : public base::RefCountedThreadSafe<UserScriptMaster>, public: // For testability, the constructor takes the path the scripts live in. // This is normally a directory inside the profile. - explicit UserScriptMaster(const FilePath& script_dir); + explicit UserScriptMaster(const FilePath& script_dir, Profile* profile); // Add a watched directory. All scripts will be reloaded when any file in // this directory changes. @@ -164,6 +165,9 @@ class UserScriptMaster : public base::RefCountedThreadSafe<UserScriptMaster>, // finishes. This boolean tracks whether another scan is pending. bool pending_scan_; + // The profile for which the scripts managed here are installed. + Profile* profile_; + DISALLOW_COPY_AND_ASSIGN(UserScriptMaster); }; diff --git a/chrome/browser/extensions/user_script_master_unittest.cc b/chrome/browser/extensions/user_script_master_unittest.cc index 6c01939..562f4cf 100644 --- a/chrome/browser/extensions/user_script_master_unittest.cc +++ b/chrome/browser/extensions/user_script_master_unittest.cc @@ -14,6 +14,7 @@ #include "chrome/browser/chrome_thread.h" #include "chrome/common/notification_registrar.h" #include "chrome/common/notification_service.h" +#include "chrome/test/testing_profile.h" #include "testing/gtest/include/gtest/gtest.h" // Test bringing up a master on a specific directory, putting a script @@ -80,7 +81,9 @@ class UserScriptMasterTest : public testing::Test, // Test that we get notified even when there are no scripts. TEST_F(UserScriptMasterTest, NoScripts) { - scoped_refptr<UserScriptMaster> master(new UserScriptMaster(script_dir_)); + TestingProfile profile; + scoped_refptr<UserScriptMaster> master(new UserScriptMaster(script_dir_, + &profile)); master->StartScan(); message_loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask); message_loop_.Run(); @@ -92,7 +95,9 @@ TEST_F(UserScriptMasterTest, NoScripts) { #if defined(OS_WIN) || defined(OS_MACOSX) // Test that we get notified about new scripts after they're added. TEST_F(UserScriptMasterTest, NewScripts) { - scoped_refptr<UserScriptMaster> master(new UserScriptMaster(script_dir_)); + TestingProfile profile; + scoped_refptr<UserScriptMaster> master(new UserScriptMaster(script_dir_, + &profile)); FilePath path = script_dir_.AppendASCII("script.user.js"); @@ -112,13 +117,15 @@ TEST_F(UserScriptMasterTest, NewScripts) { // Test that we get notified about scripts if they're already in the test dir. TEST_F(UserScriptMasterTest, ExistingScripts) { + TestingProfile profile; FilePath path = script_dir_.AppendASCII("script.user.js"); const char content[] = "some content"; size_t written = file_util::WriteFile(path, content, sizeof(content)); ASSERT_EQ(written, sizeof(content)); - scoped_refptr<UserScriptMaster> master(new UserScriptMaster(script_dir_)); + scoped_refptr<UserScriptMaster> master(new UserScriptMaster(script_dir_, + &profile)); master->StartScan(); message_loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask); diff --git a/chrome/browser/profile.cc b/chrome/browser/profile.cc index 51990a0..3231ede2 100644 --- a/chrome/browser/profile.cc +++ b/chrome/browser/profile.cc @@ -250,7 +250,7 @@ class OffTheRecordProfileImpl : public Profile, } virtual UserScriptMaster* GetUserScriptMaster() { - return profile_->GetUserScriptMaster(); + return NULL; } virtual ExtensionDevToolsManager* GetExtensionDevToolsManager() { @@ -667,7 +667,7 @@ void ProfileImpl::InitExtensions() { FilePath script_dir; // Don't look for user scripts in any directory. // TODO(aa): We should just remove this functionality, // since it isn't used anymore. - user_script_master_ = new UserScriptMaster(script_dir); + user_script_master_ = new UserScriptMaster(script_dir, this); extensions_service_ = new ExtensionsService( this, diff --git a/chrome/browser/renderer_host/browser_render_process_host.cc b/chrome/browser/renderer_host/browser_render_process_host.cc index 2d7d2f8..5a69ccd 100644 --- a/chrome/browser/renderer_host/browser_render_process_host.cc +++ b/chrome/browser/renderer_host/browser_render_process_host.cc @@ -196,7 +196,7 @@ BrowserRenderProcessHost::BrowserRenderProcessHost(Profile* profile) widget_helper_ = new RenderWidgetHelper(); registrar_.Add(this, NotificationType::USER_SCRIPTS_UPDATED, - NotificationService::AllSources()); + Source<Profile>(profile)); registrar_.Add(this, NotificationType::SPELLCHECK_HOST_REINITIALIZED, NotificationService::AllSources()); registrar_.Add(this, NotificationType::SPELLCHECK_WORD_ADDED, @@ -575,7 +575,10 @@ void BrowserRenderProcessHost::InitVisitedLinks() { void BrowserRenderProcessHost::InitUserScripts() { UserScriptMaster* user_script_master = profile()->GetUserScriptMaster(); - DCHECK(user_script_master); + + // Incognito profiles won't have user scripts. + if (!user_script_master) + return; if (!user_script_master->ScriptsReady()) { // No scripts ready. :( diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index e9e445a..0bfe048 100755 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -1103,6 +1103,7 @@ 'browser/extensions/extension_messages_apitest.cc', 'browser/extensions/extension_override_apitest.cc', 'browser/extensions/extension_toolstrip_apitest.cc', + 'browser/extensions/incognito_noscript_apitest.cc', 'browser/extensions/isolated_world_apitest.cc', 'browser/extensions/page_action_apitest.cc', 'browser/extensions/stubs_apitest.cc', diff --git a/chrome/test/data/extensions/api_test/incognito_no_script/change_page_title.js b/chrome/test/data/extensions/api_test/incognito_no_script/change_page_title.js new file mode 100755 index 0000000..ed4cfd2 --- /dev/null +++ b/chrome/test/data/extensions/api_test/incognito_no_script/change_page_title.js @@ -0,0 +1,5 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +document.title = "modified"; diff --git a/chrome/test/data/extensions/api_test/incognito_no_script/manifest.json b/chrome/test/data/extensions/api_test/incognito_no_script/manifest.json new file mode 100755 index 0000000..7c6156e --- /dev/null +++ b/chrome/test/data/extensions/api_test/incognito_no_script/manifest.json @@ -0,0 +1,13 @@ +{ + "name": "incognito no script", + "version": "0.1", + "description": "Checks that content scripts do not inject js into incognito browsers.", + "permissions": ["http://*/*", "https://*/*"], + "content_scripts": [ + { + "matches": ["http://*/*", "https://*/*"], + "js": ["change_page_title.js"], + "run_at": "document_start" + } + ] +} |