Update RestoreOnStartupPolicyHandler to translate to the correct preferences

BUG=124027
TEST=pyauto:policy.PolicyTest.testStartupOptionsHomepage


Review URL: http://codereview.chromium.org/10035043

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@133504 0039d316-1c4b-4281-b951-d872f2087c98
parent 5ce53d29
......@@ -1154,9 +1154,9 @@
'type': 'int-enum',
'items': [
{
'name': 'RestoreOnStartupIsNone',
'value': 0,
'caption': '''Open home page'''
'name': 'RestoreOnStartupIsNewTabPage',
'value': 5,
'caption': '''Open New Tab Page'''
},
{
'name': 'RestoreOnStartupIsLastSession',
......@@ -1179,7 +1179,7 @@
'caption': '''Action on startup''',
'desc': '''Allows you to specify the behavior on startup.
If you choose 'Open home page' the home page will always be opened when you start <ph name="PRODUCT_NAME">$1<ex>Google Chrome</ex></ph>.
If you choose 'Open New Tab Page' the New Tab Page will always be opened when you start <ph name="PRODUCT_NAME">$1<ex>Google Chrome</ex></ph>.
If you choose 'Restore the last session', the URLs that were open last time <ph name="PRODUCT_NAME">$1<ex>Google Chrome</ex></ph> was closed will be reopened and the browsing session will be restored as it was left.
Choosing this option disables some settings that rely on sessions or that perform actions on exit (such as Clear browsing data on exit or session-only cookies).
......
......@@ -965,48 +965,111 @@ void JavascriptPolicyHandler::ApplyPolicySettings(const PolicyMap& policies,
// RestoreOnStartupPolicyHandler implementation --------------------------------
RestoreOnStartupPolicyHandler::RestoreOnStartupPolicyHandler()
: SimplePolicyHandler(key::kRestoreOnStartup,
prefs::kRestoreOnStartup,
Value::TYPE_INTEGER) {
: TypeCheckingPolicyHandler(key::kRestoreOnStartup,
Value::TYPE_INTEGER) {
}
RestoreOnStartupPolicyHandler::~RestoreOnStartupPolicyHandler() {
}
void RestoreOnStartupPolicyHandler::ApplyPolicySettings(
const PolicyMap& policies,
PrefValueMap* prefs) {
const Value* restore_on_startup_value = policies.GetValue(policy_name());
if (restore_on_startup_value) {
int restore_on_startup;
if (!restore_on_startup_value->GetAsInteger(&restore_on_startup))
return;
if (restore_on_startup == SessionStartupPref::kPrefValueHomePage)
ApplyPolicySettingsFromHomePage(policies, prefs);
else
prefs->SetInteger(prefs::kRestoreOnStartup, restore_on_startup);
}
}
void RestoreOnStartupPolicyHandler::ApplyPolicySettingsFromHomePage(
const PolicyMap& policies,
PrefValueMap* prefs) {
const base::Value* homepage_is_new_tab_page_value =
policies.GetValue(key::kHomepageIsNewTabPage);
if (!homepage_is_new_tab_page_value) {
// The policy is enforcing 'open the homepage on startup' but not
// enforcing what the homepage should be. Don't set any prefs.
return;
}
bool homepage_is_new_tab_page;
if (!homepage_is_new_tab_page_value->GetAsBoolean(&homepage_is_new_tab_page))
return;
if (homepage_is_new_tab_page) {
prefs->SetInteger(prefs::kRestoreOnStartup,
SessionStartupPref::kPrefValueNewTab);
} else {
const base::Value* homepage_value =
policies.GetValue(key::kHomepageLocation);
if (!homepage_value || !homepage_value->IsType(base::Value::TYPE_STRING)) {
// The policy is enforcing 'open the homepage on startup' but not
// enforcing what the homepage should be. Don't set any prefs.
return;
}
ListValue* url_list = new ListValue();
url_list->Append(homepage_value->DeepCopy());
prefs->SetInteger(prefs::kRestoreOnStartup,
SessionStartupPref::kPrefValueURLs);
prefs->SetValue(prefs::kURLsToRestoreOnStartup, url_list);
}
}
bool RestoreOnStartupPolicyHandler::CheckPolicySettings(
const PolicyMap& policies,
PolicyErrorMap* errors) {
if (!SimplePolicyHandler::CheckPolicySettings(policies, errors))
if (!TypeCheckingPolicyHandler::CheckPolicySettings(policies, errors))
return false;
// If the restore urls at start up policy is set, session cookies are treated
// as permanent cookies and site data needed to restore the session is not
// cleared so we have to warn the user in that case.
const base::Value* restore_policy = policies.GetValue(key::kRestoreOnStartup);
if (restore_policy) {
int restore_value;
if (restore_policy->GetAsInteger(&restore_value) &&
SessionStartupPref::PrefValueToType(restore_value) ==
SessionStartupPref::LAST) {
const base::Value* cookies_policy =
policies.GetValue(key::kCookiesSessionOnlyForUrls);
const base::ListValue *cookies_value;
if (cookies_policy && cookies_policy->GetAsList(&cookies_value) &&
!cookies_value->empty()) {
errors->AddError(key::kCookiesSessionOnlyForUrls,
IDS_POLICY_OVERRIDDEN,
key::kRestoreOnStartup);
}
const base::Value* exit_policy =
policies.GetValue(key::kClearSiteDataOnExit);
bool exit_value;
if (exit_policy && exit_policy->GetAsBoolean(&exit_value) && exit_value) {
errors->AddError(key::kClearSiteDataOnExit,
IDS_POLICY_OVERRIDDEN,
key::kRestoreOnStartup);
if (restore_policy->GetAsInteger(&restore_value)) {
switch (restore_value) {
case SessionStartupPref::kPrefValueHomePage:
// TODO(tbreisacher): AddError indicating this value is deprecated.
break;
case SessionStartupPref::kPrefValueLast: {
// If the "restore last session" policy is set, session cookies are
// treated as permanent cookies and site data needed to restore the
// session is not cleared so we have to warn the user in that case.
const base::Value* cookies_policy =
policies.GetValue(key::kCookiesSessionOnlyForUrls);
const base::ListValue *cookies_value;
if (cookies_policy && cookies_policy->GetAsList(&cookies_value) &&
!cookies_value->empty()) {
errors->AddError(key::kCookiesSessionOnlyForUrls,
IDS_POLICY_OVERRIDDEN,
key::kRestoreOnStartup);
}
const base::Value* exit_policy =
policies.GetValue(key::kClearSiteDataOnExit);
bool exit_value;
if (exit_policy &&
exit_policy->GetAsBoolean(&exit_value) && exit_value) {
errors->AddError(key::kClearSiteDataOnExit,
IDS_POLICY_OVERRIDDEN,
key::kRestoreOnStartup);
}
break;
}
case SessionStartupPref::kPrefValueURLs:
case SessionStartupPref::kPrefValueNewTab:
// No error
break;
default:
errors->AddError(policy_name(),
IDS_POLICY_OUT_OF_RANGE_ERROR,
base::IntToString(restore_value));
}
}
}
......
......@@ -307,7 +307,7 @@ class JavascriptPolicyHandler : public ConfigurationPolicyHandler {
};
// Handles RestoreOnStartup policy.
class RestoreOnStartupPolicyHandler : public SimplePolicyHandler {
class RestoreOnStartupPolicyHandler : public TypeCheckingPolicyHandler {
public:
RestoreOnStartupPolicyHandler();
virtual ~RestoreOnStartupPolicyHandler();
......@@ -315,8 +315,13 @@ class RestoreOnStartupPolicyHandler : public SimplePolicyHandler {
// ConfigurationPolicyHandler methods:
virtual bool CheckPolicySettings(const PolicyMap& policies,
PolicyErrorMap* errors) OVERRIDE;
virtual void ApplyPolicySettings(const PolicyMap& policies,
PrefValueMap* prefs) OVERRIDE;
private:
void ApplyPolicySettingsFromHomePage(const PolicyMap& policies,
PrefValueMap* prefs);
DISALLOW_COPY_AND_ASSIGN(RestoreOnStartupPolicyHandler);
};
......
......@@ -133,12 +133,12 @@ TEST_F(ConfigurationPolicyReaderTest, SetBooleanValue) {
// Test for integer-valued policy settings.
TEST_F(ConfigurationPolicyReaderTest, SetIntegerValue) {
provider_.AddMandatoryPolicy(key::kRestoreOnStartup,
Value::CreateIntegerValue(3));
Value::CreateIntegerValue(4));
provider_.AddRecommendedPolicy(key::kIncognitoModeAvailability,
Value::CreateIntegerValue(2));
reader_->OnUpdatePolicy(&provider_);
scoped_ptr<DictionaryValue> dict(
CreateDictionary(key::kRestoreOnStartup, Value::CreateIntegerValue(3)));
CreateDictionary(key::kRestoreOnStartup, Value::CreateIntegerValue(4)));
scoped_ptr<DictionaryValue> result(
reader_->GetPolicyStatus(key::kRestoreOnStartup));
EXPECT_TRUE(dict->Equals(result.get()));
......
......@@ -25,19 +25,12 @@ using protector::ProtectorServiceFactory;
namespace {
// For historical reasons the enum and value registered in the prefs don't line
// up. These are the values registered in prefs.
const int kPrefValueHomePage = 0; // Deprecated
const int kPrefValueLast = 1;
const int kPrefValueURLs = 4;
const int kPrefValueNewTab = 5;
// Converts a SessionStartupPref::Type to an integer written to prefs.
int TypeToPrefValue(SessionStartupPref::Type type) {
switch (type) {
case SessionStartupPref::LAST: return kPrefValueLast;
case SessionStartupPref::URLS: return kPrefValueURLs;
default: return kPrefValueNewTab;
case SessionStartupPref::LAST: return SessionStartupPref::kPrefValueLast;
case SessionStartupPref::URLS: return SessionStartupPref::kPrefValueURLs;
default: return SessionStartupPref::kPrefValueNewTab;
}
}
......
......@@ -34,6 +34,13 @@ struct SessionStartupPref {
TYPE_COUNT
};
// For historical reasons the enum and value registered in the prefs don't
// line up. These are the values registered in prefs.
static const int kPrefValueHomePage = 0; // Deprecated
static const int kPrefValueLast = 1;
static const int kPrefValueURLs = 4;
static const int kPrefValueNewTab = 5;
static void RegisterUserPrefs(PrefService* prefs);
// Returns the default value for |type|.
......
......@@ -266,8 +266,10 @@ class PolicyTest(policy_base.PolicyTestBase):
self._RestartRenderer()
self.assertFalse(self._IsWebGLEnabled())
def testStartupOptions(self):
"""Verify that user cannot modify the startup page options."""
def testStartupOptionsURLs(self):
"""Verify that user cannot modify the startup page options if "Open the
following URLs" is set by a policy.
"""
policy = {
'RestoreOnStartup': 4,
'RestoreOnStartupURLs': ['http://chromium.org']
......@@ -278,9 +280,18 @@ class PolicyTest(policy_base.PolicyTestBase):
self.assertRaises(
pyauto.JSONInterfaceError,
lambda: self.SetPrefs(pyauto.kRestoreOnStartup, 1))
policy['RestoreOnStartup'] = 0
def testStartupOptionsHomepage(self):
"""Verify that user cannot modify the startup page options if the
deprecated "Open the homepage" option is set by a policy.
"""
policy = {
'RestoreOnStartup': 0,
'HomepageLocation': 'http://chromium.org',
'HomepageIsNewTabPage': False,
}
self.SetUserPolicy(policy)
self.assertEquals(0, self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup))
self.assertEquals(4, self.GetPrefsInfo().Prefs(pyauto.kRestoreOnStartup))
self.assertRaises(
pyauto.JSONInterfaceError,
lambda: self.SetPrefs(pyauto.kRestoreOnStartup, 1))
......
......@@ -139,7 +139,7 @@ class PolicyPrefsTestCases(object):
'https://clients2.google.com/service/update2/crx'], [], OS_ALL),
'ShowHomeButton': ('kShowHomeButton', True, [BROWSER], OS_ALL),
'DeveloperToolsDisabled': ('kDevToolsDisabled', True, [], OS_ALL),
'RestoreOnStartup': (None, 0, [BROWSER], OS_ALL),
'RestoreOnStartup': (None, 5, [BROWSER], OS_ALL),
# TODO(joaodasilva): Should be BROWSER. http://crbug.com/97749
'RestoreOnStartupURLs':
('kURLsToRestoreOnStartup', ['chromium.org'], [], OS_ALL),
......@@ -271,6 +271,8 @@ class PolicyPrefsTestCases(object):
'DeviceStartUpUrls': (None, ['http://google.com'], [], ['chromeos']),
'DeviceAppPack': (None, [], [], ['chromeos']),
'DeviceAutoUpdateDisabled': (None, True, [], ['chromeos']),
'DeviceTargetVersionPrefix': (None, '1412.', [], ['chromeos']),
'ReportDeviceLocation': (None, False, [], ['chromeos']),
# Chrome Frame policies:
'ChromeFrameRendererSettings': (None, 0, [], []),
......
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