diff options
author | rafaelw@chromium.org <rafaelw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-12-23 21:23:17 +0000 |
---|---|---|
committer | rafaelw@chromium.org <rafaelw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-12-23 21:23:17 +0000 |
commit | d30c30de79653ffc137018f09dbe34dd92ad6bc9 (patch) | |
tree | 7b716558e33ffae592d82e223a002b9aad698500 | |
parent | dcf57bce3ea0b5349474d9d8bdf4d8d86faba154 (diff) | |
download | chromium_src-d30c30de79653ffc137018f09dbe34dd92ad6bc9.zip chromium_src-d30c30de79653ffc137018f09dbe34dd92ad6bc9.tar.gz chromium_src-d30c30de79653ffc137018f09dbe34dd92ad6bc9.tar.bz2 |
Merge revision 35203 to branch 249.
Don't inject content scripts into incognito browsers
BUG=21392
original review: http://codereview.chromium.org/502079
Review URL: http://codereview.chromium.org/517007
git-svn-id: svn://svn.chromium.org/chrome/branches/249/src@35233 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 100644 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 c9228ff..70c4754 100644 --- a/chrome/browser/extensions/user_script_master.cc +++ b/chrome/browser/extensions/user_script_master.cc @@ -274,19 +274,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() { @@ -327,7 +328,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 29faaea..75da067ee 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 ddff913..1fde7a0 100644 --- a/chrome/browser/profile.cc +++ b/chrome/browser/profile.cc @@ -254,7 +254,7 @@ class OffTheRecordProfileImpl : public Profile, } virtual UserScriptMaster* GetUserScriptMaster() { - return profile_->GetUserScriptMaster(); + return NULL; } virtual ExtensionDevToolsManager* GetExtensionDevToolsManager() { @@ -669,7 +669,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 c231d69..e77809d 100644 --- a/chrome/browser/renderer_host/browser_render_process_host.cc +++ b/chrome/browser/renderer_host/browser_render_process_host.cc @@ -192,7 +192,7 @@ BrowserRenderProcessHost::BrowserRenderProcessHost(Profile* profile) widget_helper_ = new RenderWidgetHelper(); registrar_.Add(this, NotificationType::USER_SCRIPTS_UPDATED, - NotificationService::AllSources()); + Source<Profile>(profile)); visited_link_updater_.reset(new VisitedLinkUpdater()); WebCacheManager::GetInstance()->Add(id()); @@ -563,7 +563,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.gyp b/chrome/chrome.gyp index c63e459..49d395d 100755 --- a/chrome/chrome.gyp +++ b/chrome/chrome.gyp @@ -75,6 +75,7 @@ 'browser/extensions/extension_browsertests_misc.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 100644 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 100644 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" + } + ] +} |