Commit ec5143b6 authored by bauerb@chromium.org's avatar bauerb@chromium.org

Reland r223124: Don't clear existing extension-defined preferences and content...

Reland r223124: Don't clear existing extension-defined preferences and content settings when reloading or updating an extension.

TBR=falken@chromium.org,sky@chromium.org
BUG=284385

Original review: https://chromiumcodereview.appspot.com/23694020

Review URL: https://chromiumcodereview.appspot.com/23581015

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@223587 0039d316-1c4b-4281-b951-d872f2087c98
parent fc20fdd3
......@@ -4,6 +4,8 @@
#include "base/prefs/pref_service.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/content_settings/cookie_settings.h"
#include "chrome/browser/content_settings/host_content_settings_map.h"
#include "chrome/browser/extensions/api/content_settings/content_settings_api.h"
......@@ -12,18 +14,59 @@
#include "chrome/browser/ui/browser.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/plugin_service.h"
#include "content/public/common/webplugininfo.h"
#include "content/public/test/test_utils.h"
namespace {
void ReleaseBrowserProcessModule() {
g_browser_process->ReleaseModule();
}
} // namespace
namespace extensions {
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ContentSettings) {
EXPECT_TRUE(RunExtensionTest("content_settings/standard")) << message_;
class ExtensionContentSettingsApiTest : public ExtensionApiTest {
public:
ExtensionContentSettingsApiTest() : profile_(NULL) {}
virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
ExtensionApiTest::SetUpCommandLine(command_line);
command_line->AppendSwitch(switches::kDisablePluginsDiscovery);
}
virtual void SetUpOnMainThread() OVERRIDE {
ExtensionApiTest::SetUpOnMainThread();
// The browser might get closed later (and therefore be destroyed), so we
// save the profile.
profile_ = browser()->profile();
// Closing the last browser window also releases a module reference. Make
// sure it's not the last one, so the message loop doesn't quit
// unexpectedly.
g_browser_process->AddRefModule();
}
virtual void CleanUpOnMainThread() OVERRIDE {
// ReleaseBrowserProcessModule() needs to be called in a message loop, so we
// post a task to do it, then run the message loop.
base::MessageLoop::current()->PostTask(
FROM_HERE, base::Bind(&ReleaseBrowserProcessModule));
content::RunAllPendingInMessageLoop();
ExtensionApiTest::CleanUpOnMainThread();
}
protected:
void CheckContentSettingsSet() {
HostContentSettingsMap* map =
browser()->profile()->GetHostContentSettingsMap();
profile_->GetHostContentSettingsMap();
CookieSettings* cookie_settings =
CookieSettings::Factory::GetForProfile(browser()->profile()).get();
CookieSettings::Factory::GetForProfile(profile_).get();
// Check default content settings by using an unknown URL.
GURL example_url("http://www.example.com");
......@@ -85,24 +128,88 @@ IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ContentSettings) {
#if 0
EXPECT_EQ(CONTENT_SETTING_BLOCK,
map->GetContentSetting(
url, url, CONTENT_SETTINGS_TYPE_GEOLOCATION, ""));
url, url, CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
#endif
EXPECT_EQ(CONTENT_SETTING_BLOCK,
EXPECT_EQ(
CONTENT_SETTING_BLOCK,
map->GetContentSetting(
url, url, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string()));
}
}
class ContentSettingsGetResourceIdentifiersTest : public ExtensionApiTest {
public:
virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
ExtensionApiTest::SetUpCommandLine(command_line);
command_line->AppendSwitch(switches::kDisablePluginsDiscovery);
void CheckContentSettingsDefault() {
HostContentSettingsMap* map =
profile_->GetHostContentSettingsMap();
CookieSettings* cookie_settings =
CookieSettings::Factory::GetForProfile(profile_).get();
// Check content settings for www.google.com
GURL url("http://www.google.com");
EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed(url, url));
EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed(url, url));
EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(url));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
map->GetContentSetting(
url, url, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
map->GetContentSetting(
url, url, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string()));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
map->GetContentSetting(
url, url, CONTENT_SETTINGS_TYPE_PLUGINS, std::string()));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
map->GetContentSetting(
url, url, CONTENT_SETTINGS_TYPE_POPUPS, std::string()));
#if 0
// TODO(bauerb): Enable once geolocation settings are integrated into the
// HostContentSettingsMap.
EXPECT_EQ(CONTENT_SETTING_ALLOW,
map->GetContentSetting(
url, url, CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
#endif
EXPECT_EQ(
CONTENT_SETTING_ASK,
map->GetContentSetting(
url, url, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string()));
}
private:
Profile* profile_;
};
// http://crbug.com/177163
#if defined(OS_WIN) && !defined(NDEBUG)
#define MAYBE_Standard DISABLED_Standard
#else
#define MAYBE_Standard Standard
#endif
IN_PROC_BROWSER_TEST_F(ExtensionContentSettingsApiTest, MAYBE_Standard) {
CheckContentSettingsDefault();
const char kExtensionPath[] = "content_settings/standard";
EXPECT_TRUE(RunExtensionSubtest(kExtensionPath, "test.html")) << message_;
CheckContentSettingsSet();
// The settings should not be reset when the extension is reloaded.
ReloadExtension(last_loaded_extension_id_);
CheckContentSettingsSet();
// Uninstalling and installing the extension (without running the test that
// calls the extension API) should clear the settings.
content::WindowedNotificationObserver observer(
chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
content::NotificationService::AllSources());
UninstallExtension(last_loaded_extension_id_);
observer.Wait();
CheckContentSettingsDefault();
LoadExtension(test_data_dir_.AppendASCII(kExtensionPath));
CheckContentSettingsDefault();
}
// Flaky on the trybots. See http://crbug.com/96725.
IN_PROC_BROWSER_TEST_F(ContentSettingsGetResourceIdentifiersTest,
DISABLED_Test) {
IN_PROC_BROWSER_TEST_F(ExtensionContentSettingsApiTest,
DISABLED_GetResourceIdentifiers) {
base::FilePath::CharType kFooPath[] =
FILE_PATH_LITERAL("/plugins/foo.plugin");
base::FilePath::CharType kBarPath[] =
......
......@@ -123,15 +123,16 @@ void ContentSettingsStore::RegisterExtension(
bool is_enabled) {
base::AutoLock lock(lock_);
ExtensionEntryMap::iterator i = FindEntry(ext_id);
ExtensionEntry* entry;
if (i != entries_.end()) {
delete i->second;
entries_.erase(i);
entry = i->second;
} else {
entry = new ExtensionEntry;
entries_.insert(std::make_pair(install_time, entry));
}
ExtensionEntry* entry = new ExtensionEntry;
entry->id = ext_id;
entry->enabled = is_enabled;
entries_.insert(std::make_pair(install_time, entry));
}
void ContentSettingsStore::UnregisterExtension(
......
......@@ -4,6 +4,8 @@
#include "base/prefs/pref_service.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/extension_test_message_listener.h"
#include "chrome/browser/profiles/profile.h"
......@@ -11,38 +13,122 @@
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/notification_service.h"
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PreferenceApi) {
PrefService* pref_service = browser()->profile()->GetPrefs();
pref_service->SetBoolean(prefs::kAlternateErrorPagesEnabled, false);
pref_service->SetBoolean(autofill::prefs::kAutofillEnabled, false);
pref_service->SetBoolean(prefs::kBlockThirdPartyCookies, true);
pref_service->SetBoolean(prefs::kEnableHyperlinkAuditing, false);
pref_service->SetBoolean(prefs::kEnableReferrers, false);
pref_service->SetBoolean(prefs::kEnableTranslate, false);
pref_service->SetBoolean(prefs::kNetworkPredictionEnabled, false);
pref_service->SetBoolean(prefs::kSafeBrowsingEnabled, false);
pref_service->SetBoolean(prefs::kSearchSuggestEnabled, false);
EXPECT_TRUE(RunExtensionTest("preference/standard")) << message_;
const PrefService::Preference* pref = pref_service->FindPreference(
namespace {
void ReleaseBrowserProcessModule() {
g_browser_process->ReleaseModule();
}
} // namespace
class ExtensionPreferenceApiTest : public ExtensionApiTest {
protected:
ExtensionPreferenceApiTest() : profile_(NULL) {}
void CheckPreferencesSet() {
PrefService* prefs = profile_->GetPrefs();
const PrefService::Preference* pref = prefs->FindPreference(
prefs::kBlockThirdPartyCookies);
ASSERT_TRUE(pref);
EXPECT_TRUE(pref->IsExtensionControlled());
EXPECT_TRUE(pref_service->GetBoolean(prefs::kAlternateErrorPagesEnabled));
EXPECT_TRUE(pref_service->GetBoolean(autofill::prefs::kAutofillEnabled));
EXPECT_FALSE(pref_service->GetBoolean(prefs::kBlockThirdPartyCookies));
EXPECT_TRUE(pref_service->GetBoolean(prefs::kEnableHyperlinkAuditing));
EXPECT_TRUE(pref_service->GetBoolean(prefs::kEnableReferrers));
EXPECT_TRUE(pref_service->GetBoolean(prefs::kEnableTranslate));
EXPECT_TRUE(pref_service->GetBoolean(prefs::kNetworkPredictionEnabled));
EXPECT_TRUE(pref_service->GetBoolean(prefs::kSafeBrowsingEnabled));
EXPECT_TRUE(pref_service->GetBoolean(prefs::kSearchSuggestEnabled));
EXPECT_TRUE(prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled));
EXPECT_TRUE(prefs->GetBoolean(autofill::prefs::kAutofillEnabled));
EXPECT_FALSE(prefs->GetBoolean(prefs::kBlockThirdPartyCookies));
EXPECT_TRUE(prefs->GetBoolean(prefs::kEnableHyperlinkAuditing));
EXPECT_TRUE(prefs->GetBoolean(prefs::kEnableReferrers));
EXPECT_TRUE(prefs->GetBoolean(prefs::kEnableTranslate));
EXPECT_TRUE(prefs->GetBoolean(prefs::kNetworkPredictionEnabled));
EXPECT_TRUE(prefs->GetBoolean(prefs::kSafeBrowsingEnabled));
EXPECT_TRUE(prefs->GetBoolean(prefs::kSearchSuggestEnabled));
}
void CheckPreferencesCleared() {
PrefService* prefs = profile_->GetPrefs();
const PrefService::Preference* pref = prefs->FindPreference(
prefs::kBlockThirdPartyCookies);
ASSERT_TRUE(pref);
EXPECT_FALSE(pref->IsExtensionControlled());
EXPECT_FALSE(prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled));
EXPECT_FALSE(prefs->GetBoolean(autofill::prefs::kAutofillEnabled));
EXPECT_TRUE(prefs->GetBoolean(prefs::kBlockThirdPartyCookies));
EXPECT_FALSE(prefs->GetBoolean(prefs::kEnableHyperlinkAuditing));
EXPECT_FALSE(prefs->GetBoolean(prefs::kEnableReferrers));
EXPECT_FALSE(prefs->GetBoolean(prefs::kEnableTranslate));
EXPECT_FALSE(prefs->GetBoolean(prefs::kNetworkPredictionEnabled));
EXPECT_FALSE(prefs->GetBoolean(prefs::kSafeBrowsingEnabled));
EXPECT_FALSE(prefs->GetBoolean(prefs::kSearchSuggestEnabled));
}
virtual void SetUpOnMainThread() OVERRIDE {
ExtensionApiTest::SetUpOnMainThread();
// The browser might get closed later (and therefore be destroyed), so we
// save the profile.
profile_ = browser()->profile();
// Closing the last browser window also releases a module reference. Make
// sure it's not the last one, so the message loop doesn't quit
// unexpectedly.
g_browser_process->AddRefModule();
}
virtual void CleanUpOnMainThread() OVERRIDE {
// ReleaseBrowserProcessModule() needs to be called in a message loop, so we
// post a task to do it, then run the message loop.
base::MessageLoop::current()->PostTask(
FROM_HERE, base::Bind(&ReleaseBrowserProcessModule));
content::RunAllPendingInMessageLoop();
ExtensionApiTest::CleanUpOnMainThread();
}
Profile* profile_;
};
// http://crbug.com/177163
#if defined(OS_WIN) && !defined(NDEBUG)
#define MAYBE_Standard DISABLED_Standard
#else
#define MAYBE_Standard Standard
#endif
IN_PROC_BROWSER_TEST_F(ExtensionPreferenceApiTest, MAYBE_Standard) {
PrefService* prefs = profile_->GetPrefs();
prefs->SetBoolean(prefs::kAlternateErrorPagesEnabled, false);
prefs->SetBoolean(autofill::prefs::kAutofillEnabled, false);
prefs->SetBoolean(prefs::kBlockThirdPartyCookies, true);
prefs->SetBoolean(prefs::kEnableHyperlinkAuditing, false);
prefs->SetBoolean(prefs::kEnableReferrers, false);
prefs->SetBoolean(prefs::kEnableTranslate, false);
prefs->SetBoolean(prefs::kNetworkPredictionEnabled, false);
prefs->SetBoolean(prefs::kSafeBrowsingEnabled, false);
prefs->SetBoolean(prefs::kSearchSuggestEnabled, false);
const char kExtensionPath[] = "preference/standard";
EXPECT_TRUE(RunExtensionSubtest(kExtensionPath, "test.html")) << message_;
CheckPreferencesSet();
// The settings should not be reset when the extension is reloaded.
ReloadExtension(last_loaded_extension_id_);
CheckPreferencesSet();
// Uninstalling and installing the extension (without running the test that
// calls the extension API) should clear the settings.
content::WindowedNotificationObserver observer(
chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
content::NotificationService::AllSources());
UninstallExtension(last_loaded_extension_id_);
observer.Wait();
CheckPreferencesCleared();
LoadExtension(test_data_dir_.AppendASCII(kExtensionPath));
CheckPreferencesCleared();
}
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PreferencePersistentIncognito) {
PrefService* prefs = browser()->profile()->GetPrefs();
IN_PROC_BROWSER_TEST_F(ExtensionPreferenceApiTest, PersistentIncognito) {
PrefService* prefs = profile_->GetPrefs();
prefs->SetBoolean(prefs::kBlockThirdPartyCookies, false);
EXPECT_TRUE(
......@@ -50,10 +136,9 @@ IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PreferencePersistentIncognito) {
message_;
// Setting an incognito preference should not create an incognito profile.
EXPECT_FALSE(browser()->profile()->HasOffTheRecordProfile());
EXPECT_FALSE(profile_->HasOffTheRecordProfile());
PrefService* otr_prefs =
browser()->profile()->GetOffTheRecordProfile()->GetPrefs();
PrefService* otr_prefs = profile_->GetOffTheRecordProfile()->GetPrefs();
const PrefService::Preference* pref =
otr_prefs->FindPreference(prefs::kBlockThirdPartyCookies);
ASSERT_TRUE(pref);
......@@ -67,22 +152,21 @@ IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PreferencePersistentIncognito) {
}
// Flakily times out: http://crbug.com/106144
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DISABLED_PreferenceIncognitoDisabled) {
IN_PROC_BROWSER_TEST_F(ExtensionPreferenceApiTest, DISABLED_IncognitoDisabled) {
EXPECT_FALSE(RunExtensionTest("preference/persistent_incognito"));
}
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PreferenceSessionOnlyIncognito) {
PrefService* prefs = browser()->profile()->GetPrefs();
IN_PROC_BROWSER_TEST_F(ExtensionPreferenceApiTest, SessionOnlyIncognito) {
PrefService* prefs = profile_->GetPrefs();
prefs->SetBoolean(prefs::kBlockThirdPartyCookies, false);
EXPECT_TRUE(
RunExtensionTestIncognito("preference/session_only_incognito")) <<
message_;
EXPECT_TRUE(browser()->profile()->HasOffTheRecordProfile());
EXPECT_TRUE(profile_->HasOffTheRecordProfile());
PrefService* otr_prefs =
browser()->profile()->GetOffTheRecordProfile()->GetPrefs();
PrefService* otr_prefs = profile_->GetOffTheRecordProfile()->GetPrefs();
const PrefService::Preference* pref =
otr_prefs->FindPreference(prefs::kBlockThirdPartyCookies);
ASSERT_TRUE(pref);
......@@ -95,34 +179,32 @@ IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PreferenceSessionOnlyIncognito) {
EXPECT_FALSE(prefs->GetBoolean(prefs::kBlockThirdPartyCookies));
}
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PreferenceClear) {
PrefService* pref_service = browser()->profile()->GetPrefs();
pref_service->SetBoolean(prefs::kBlockThirdPartyCookies, true);
IN_PROC_BROWSER_TEST_F(ExtensionPreferenceApiTest, Clear) {
PrefService* prefs = profile_->GetPrefs();
prefs->SetBoolean(prefs::kBlockThirdPartyCookies, true);
EXPECT_TRUE(RunExtensionTest("preference/clear")) << message_;
const PrefService::Preference* pref = pref_service->FindPreference(
const PrefService::Preference* pref = prefs->FindPreference(
prefs::kBlockThirdPartyCookies);
ASSERT_TRUE(pref);
EXPECT_FALSE(pref->IsExtensionControlled());
EXPECT_EQ(true, pref_service->GetBoolean(prefs::kBlockThirdPartyCookies));
EXPECT_EQ(true, prefs->GetBoolean(prefs::kBlockThirdPartyCookies));
}
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PreferenceOnChange) {
IN_PROC_BROWSER_TEST_F(ExtensionPreferenceApiTest, OnChange) {
EXPECT_TRUE(RunExtensionTestIncognito("preference/onchange")) <<
message_;
}
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PreferenceOnChangeSplit) {
IN_PROC_BROWSER_TEST_F(ExtensionPreferenceApiTest, OnChangeSplit) {
ResultCatcher catcher;
catcher.RestrictToProfile(browser()->profile());
catcher.RestrictToProfile(profile_);
ResultCatcher catcher_incognito;
catcher_incognito.RestrictToProfile(
browser()->profile()->GetOffTheRecordProfile());
catcher_incognito.RestrictToProfile(profile_->GetOffTheRecordProfile());
// Open an incognito window.
ui_test_utils::OpenURLOffTheRecord(browser()->profile(),
GURL("chrome://newtab/"));
ui_test_utils::OpenURLOffTheRecord(profile_, GURL("chrome://newtab/"));
// changeDefault listeners.
ExtensionTestMessageListener listener1("changeDefault regular ready", true);
......
......@@ -116,10 +116,13 @@ bool ExtensionPrefValueMap::DoesExtensionControlPref(
void ExtensionPrefValueMap::RegisterExtension(const std::string& ext_id,
const base::Time& install_time,
bool is_enabled) {
if (entries_.find(ext_id) != entries_.end())
UnregisterExtension(ext_id);
if (entries_.find(ext_id) == entries_.end()) {
entries_[ext_id] = new ExtensionEntry;
// Only update the install time if the extension is newly installed.
entries_[ext_id]->install_time = install_time;
}
entries_[ext_id]->enabled = is_enabled;
}
......
......@@ -1835,13 +1835,26 @@ void ExtensionPrefs::FinishExtensionInfoPrefs(
const syncer::StringOrdinal& suggested_page_ordinal,
DictionaryValue* extension_dict) {
// Reinitializes various preferences with empty dictionaries.
if (!extension_dict->HasKey(pref_names::kPrefPreferences))
extension_dict->Set(pref_names::kPrefPreferences, new DictionaryValue);
if (!extension_dict->HasKey(pref_names::kPrefIncognitoPreferences)) {
extension_dict->Set(pref_names::kPrefIncognitoPreferences,
new DictionaryValue);
}
if (!extension_dict->HasKey(pref_names::kPrefRegularOnlyPreferences)) {
extension_dict->Set(pref_names::kPrefRegularOnlyPreferences,
new DictionaryValue);
}
if (!extension_dict->HasKey(pref_names::kPrefContentSettings))
extension_dict->Set(pref_names::kPrefContentSettings, new ListValue);
extension_dict->Set(pref_names::kPrefIncognitoContentSettings, new ListValue);
if (!extension_dict->HasKey(pref_names::kPrefIncognitoContentSettings)) {
extension_dict->Set(pref_names::kPrefIncognitoContentSettings,
new ListValue);
}
// If this point has been reached, any pending installs should be considered
// out of date.
......
......@@ -224,7 +224,7 @@ IN_PROC_BROWSER_TEST_F(ManagedModeBlockModeTest,
QueryHistory(history_service, "", options, &results);
// Check that the entries have the correct blocked_visit value.
EXPECT_EQ(2u, results.size());
ASSERT_EQ(2u, results.size());
EXPECT_EQ(blocked_url.spec(), results[0].url().spec());
EXPECT_TRUE(results[0].blocked_visit());
EXPECT_EQ(allowed_url.spec(), results[1].url().spec());
......
......@@ -3,8 +3,5 @@
"version" : "0.1",
"manifest_version": 2,
"description" : "Content Settings API Test Extension",
"permissions": [ "contentSettings" ],
"background": {
"page": "test.html"
}
"permissions": [ "contentSettings" ]
}
......@@ -3,8 +3,5 @@
"version" : "0.1",
"manifest_version": 2,
"description" : "Preferences API Test Extension",
"permissions": [ "privacy" ],
"background": {
"page": "test.html"
}
"permissions": [ "privacy" ]
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment