[Chromoting] Add a host domain policy to the PolicyWatcher.

Follow-up CLs will make the host respect this policy, and will let Windows
and Mac builds read this string-valued policy.

BUG=132684


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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@148871 0039d316-1c4b-4281-b951-d872f2087c98
parent e5bb0cd6
...@@ -41,6 +41,24 @@ bool GetBooleanOrDefault(const base::DictionaryValue* dict, const char* key, ...@@ -41,6 +41,24 @@ bool GetBooleanOrDefault(const base::DictionaryValue* dict, const char* key,
return default_if_value_not_boolean; return default_if_value_not_boolean;
} }
// Gets a string from a dictionary, or returns a default value if the string
// couldn't be read.
std::string GetStringOrDefault(const base::DictionaryValue* dict,
const char* key,
const std::string& default_if_value_missing,
const std::string& default_if_value_not_string) {
if (!dict->HasKey(key)) {
return default_if_value_missing;
}
const base::Value* value;
if (dict->Get(key, &value) && value->IsType(base::Value::TYPE_STRING)) {
std::string string_value;
CHECK(value->GetAsString(&string_value));
return string_value;
}
return default_if_value_not_string;
}
// Copies a boolean from one dictionary to another, using a default value // Copies a boolean from one dictionary to another, using a default value
// if the boolean couldn't be read from the first dictionary. // if the boolean couldn't be read from the first dictionary.
void CopyBooleanOrDefault(base::DictionaryValue* to, void CopyBooleanOrDefault(base::DictionaryValue* to,
...@@ -52,6 +70,17 @@ void CopyBooleanOrDefault(base::DictionaryValue* to, ...@@ -52,6 +70,17 @@ void CopyBooleanOrDefault(base::DictionaryValue* to,
default_if_value_not_boolean))); default_if_value_not_boolean)));
} }
// Copies a string from one dictionary to another, using a default value
// if the string couldn't be read from the first dictionary.
void CopyStringOrDefault(base::DictionaryValue* to,
const base::DictionaryValue* from, const char* key,
const std::string& default_if_value_missing,
const std::string& default_if_value_not_string) {
to->Set(key, base::Value::CreateStringValue(
GetStringOrDefault(from, key, default_if_value_missing,
default_if_value_not_string)));
}
// Copies all policy values from one dictionary to another, using default values // Copies all policy values from one dictionary to another, using default values
// when necessary. // when necessary.
scoped_ptr<base::DictionaryValue> AddDefaultValuesWhenNecessary( scoped_ptr<base::DictionaryValue> AddDefaultValuesWhenNecessary(
...@@ -59,6 +88,8 @@ scoped_ptr<base::DictionaryValue> AddDefaultValuesWhenNecessary( ...@@ -59,6 +88,8 @@ scoped_ptr<base::DictionaryValue> AddDefaultValuesWhenNecessary(
scoped_ptr<base::DictionaryValue> to(new base::DictionaryValue()); scoped_ptr<base::DictionaryValue> to(new base::DictionaryValue());
CopyBooleanOrDefault(to.get(), from, CopyBooleanOrDefault(to.get(), from,
PolicyWatcher::kNatPolicyName, true, false); PolicyWatcher::kNatPolicyName, true, false);
CopyStringOrDefault(to.get(), from,
PolicyWatcher::kHostDomainPolicyName, "", "");
return to.Pass(); return to.Pass();
} }
...@@ -67,12 +98,21 @@ scoped_ptr<base::DictionaryValue> AddDefaultValuesWhenNecessary( ...@@ -67,12 +98,21 @@ scoped_ptr<base::DictionaryValue> AddDefaultValuesWhenNecessary(
const char PolicyWatcher::kNatPolicyName[] = const char PolicyWatcher::kNatPolicyName[] =
"RemoteAccessHostFirewallTraversal"; "RemoteAccessHostFirewallTraversal";
const char PolicyWatcher::kHostDomainPolicyName[] =
"RemoteAccessHostDomain";
const char* const PolicyWatcher::kBooleanPolicyNames[] = const char* const PolicyWatcher::kBooleanPolicyNames[] =
{ PolicyWatcher::kNatPolicyName }; { PolicyWatcher::kNatPolicyName };
const int PolicyWatcher::kBooleanPolicyNamesNum = const int PolicyWatcher::kBooleanPolicyNamesNum =
arraysize(kBooleanPolicyNames); arraysize(kBooleanPolicyNames);
const char* const PolicyWatcher::kStringPolicyNames[] =
{ PolicyWatcher::kHostDomainPolicyName };
const int PolicyWatcher::kStringPolicyNamesNum =
arraysize(kStringPolicyNames);
PolicyWatcher::PolicyWatcher( PolicyWatcher::PolicyWatcher(
scoped_refptr<base::SingleThreadTaskRunner> task_runner) scoped_refptr<base::SingleThreadTaskRunner> task_runner)
: task_runner_(task_runner), : task_runner_(task_runner),
......
...@@ -49,6 +49,9 @@ class PolicyWatcher { ...@@ -49,6 +49,9 @@ class PolicyWatcher {
// The name of the NAT traversal policy. // The name of the NAT traversal policy.
static const char kNatPolicyName[]; static const char kNatPolicyName[];
// The name of the host domain policy.
static const char kHostDomainPolicyName[];
protected: protected:
virtual void StartWatchingInternal() = 0; virtual void StartWatchingInternal() = 0;
virtual void StopWatchingInternal() = 0; virtual void StopWatchingInternal() = 0;
...@@ -70,6 +73,10 @@ class PolicyWatcher { ...@@ -70,6 +73,10 @@ class PolicyWatcher {
static const char* const kBooleanPolicyNames[]; static const char* const kBooleanPolicyNames[];
static const int kBooleanPolicyNamesNum; static const int kBooleanPolicyNamesNum;
// The names of policies with string values.
static const char* const kStringPolicyNames[];
static const int kStringPolicyNamesNum;
private: private:
scoped_refptr<base::SingleThreadTaskRunner> task_runner_; scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
......
...@@ -56,6 +56,7 @@ class PolicyWatcherMac : public PolicyWatcher { ...@@ -56,6 +56,7 @@ class PolicyWatcherMac : public PolicyWatcher {
policy.SetBoolean(policy_name, allowed); policy.SetBoolean(policy_name, allowed);
} }
} }
// TODO(simonmorris): Read policies whose names are in kStringPolicyNames.
} }
// Set policy. Policy must be set (even if it is empty) so that the // Set policy. Policy must be set (even if it is empty) so that the
......
...@@ -25,9 +25,31 @@ class PolicyWatcherTest : public testing::Test { ...@@ -25,9 +25,31 @@ class PolicyWatcherTest : public testing::Test {
policy_callback_ = base::Bind(&MockPolicyCallback::OnPolicyUpdate, policy_callback_ = base::Bind(&MockPolicyCallback::OnPolicyUpdate,
base::Unretained(&mock_policy_callback_)); base::Unretained(&mock_policy_callback_));
policy_watcher_.reset(new FakePolicyWatcher(message_loop_proxy_)); policy_watcher_.reset(new FakePolicyWatcher(message_loop_proxy_));
nat_true.SetBoolean(PolicyWatcher::kNatPolicyName, true); nat_true_.SetBoolean(PolicyWatcher::kNatPolicyName, true);
nat_false.SetBoolean(PolicyWatcher::kNatPolicyName, false); nat_false_.SetBoolean(PolicyWatcher::kNatPolicyName, false);
nat_one.SetInteger(PolicyWatcher::kNatPolicyName, 1); nat_one_.SetInteger(PolicyWatcher::kNatPolicyName, 1);
domain_empty_.SetString(PolicyWatcher::kHostDomainPolicyName, "");
domain_full_.SetString(PolicyWatcher::kHostDomainPolicyName, kHostDomain);
SetDefaults(nat_true_others_default_);
nat_true_others_default_.SetBoolean(PolicyWatcher::kNatPolicyName, true);
SetDefaults(nat_false_others_default_);
nat_false_others_default_.SetBoolean(PolicyWatcher::kNatPolicyName, false);
SetDefaults(domain_empty_others_default_);
domain_empty_others_default_.SetString(PolicyWatcher::kHostDomainPolicyName,
"");
SetDefaults(domain_full_others_default_);
domain_full_others_default_.SetString(PolicyWatcher::kHostDomainPolicyName,
kHostDomain);
nat_true_domain_empty_.SetBoolean(PolicyWatcher::kNatPolicyName, true);
nat_true_domain_empty_.SetString(PolicyWatcher::kHostDomainPolicyName, "");
nat_true_domain_full_.SetBoolean(PolicyWatcher::kNatPolicyName, true);
nat_true_domain_full_.SetString(PolicyWatcher::kHostDomainPolicyName,
kHostDomain);
nat_false_domain_empty_.SetBoolean(PolicyWatcher::kNatPolicyName, false);
nat_false_domain_empty_.SetString(PolicyWatcher::kHostDomainPolicyName, "");
nat_false_domain_full_.SetBoolean(PolicyWatcher::kNatPolicyName, false);
nat_false_domain_full_.SetString(PolicyWatcher::kHostDomainPolicyName,
kHostDomain);
} }
protected: protected:
...@@ -43,106 +65,178 @@ class PolicyWatcherTest : public testing::Test { ...@@ -43,106 +65,178 @@ class PolicyWatcherTest : public testing::Test {
EXPECT_EQ(true, stop_event.IsSignaled()); EXPECT_EQ(true, stop_event.IsSignaled());
} }
static const char* kHostDomain;
MessageLoop message_loop_; MessageLoop message_loop_;
scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
MockPolicyCallback mock_policy_callback_; MockPolicyCallback mock_policy_callback_;
PolicyWatcher::PolicyCallback policy_callback_; PolicyWatcher::PolicyCallback policy_callback_;
scoped_ptr<FakePolicyWatcher> policy_watcher_; scoped_ptr<FakePolicyWatcher> policy_watcher_;
base::DictionaryValue empty; base::DictionaryValue empty_;
base::DictionaryValue nat_true; base::DictionaryValue nat_true_;
base::DictionaryValue nat_false; base::DictionaryValue nat_false_;
base::DictionaryValue nat_one; base::DictionaryValue nat_one_;
base::DictionaryValue domain_empty_;
base::DictionaryValue domain_full_;
base::DictionaryValue nat_true_others_default_;
base::DictionaryValue nat_false_others_default_;
base::DictionaryValue domain_empty_others_default_;
base::DictionaryValue domain_full_others_default_;
base::DictionaryValue nat_true_domain_empty_;
base::DictionaryValue nat_true_domain_full_;
base::DictionaryValue nat_false_domain_empty_;
base::DictionaryValue nat_false_domain_full_;
private:
void SetDefaults(base::DictionaryValue& dict) {
dict.SetBoolean(PolicyWatcher::kNatPolicyName, true);
dict.SetString(PolicyWatcher::kHostDomainPolicyName, "");
}
}; };
const char* PolicyWatcherTest::kHostDomain = "google.com";
MATCHER_P(IsPolicies, dict, "") { MATCHER_P(IsPolicies, dict, "") {
return arg->Equals(dict); return arg->Equals(dict);
} }
TEST_F(PolicyWatcherTest, None) { TEST_F(PolicyWatcherTest, None) {
EXPECT_CALL(mock_policy_callback_, OnPolicyUpdatePtr(IsPolicies(&nat_true))); EXPECT_CALL(mock_policy_callback_,
OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
StartWatching(); StartWatching();
policy_watcher_->SetPolicies(&empty); policy_watcher_->SetPolicies(&empty_);
StopWatching(); StopWatching();
} }
TEST_F(PolicyWatcherTest, NatTrue) { TEST_F(PolicyWatcherTest, NatTrue) {
EXPECT_CALL(mock_policy_callback_, OnPolicyUpdatePtr(IsPolicies(&nat_true))); EXPECT_CALL(mock_policy_callback_,
OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
StartWatching(); StartWatching();
policy_watcher_->SetPolicies(&nat_true); policy_watcher_->SetPolicies(&nat_true_);
StopWatching(); StopWatching();
} }
TEST_F(PolicyWatcherTest, NatFalse) { TEST_F(PolicyWatcherTest, NatFalse) {
EXPECT_CALL(mock_policy_callback_, OnPolicyUpdatePtr(IsPolicies(&nat_false))); EXPECT_CALL(mock_policy_callback_,
OnPolicyUpdatePtr(IsPolicies(&nat_false_others_default_)));
StartWatching(); StartWatching();
policy_watcher_->SetPolicies(&nat_false); policy_watcher_->SetPolicies(&nat_false_);
StopWatching(); StopWatching();
} }
TEST_F(PolicyWatcherTest, NatOne) { TEST_F(PolicyWatcherTest, NatOne) {
EXPECT_CALL(mock_policy_callback_, OnPolicyUpdatePtr(IsPolicies(&nat_false))); EXPECT_CALL(mock_policy_callback_,
OnPolicyUpdatePtr(IsPolicies(&nat_false_others_default_)));
StartWatching();
policy_watcher_->SetPolicies(&nat_one_);
StopWatching();
}
TEST_F(PolicyWatcherTest, DomainEmpty) {
EXPECT_CALL(mock_policy_callback_,
OnPolicyUpdatePtr(IsPolicies(&domain_empty_others_default_)));
StartWatching();
policy_watcher_->SetPolicies(&domain_empty_);
StopWatching();
}
TEST_F(PolicyWatcherTest, DomainFull) {
EXPECT_CALL(mock_policy_callback_,
OnPolicyUpdatePtr(IsPolicies(&domain_full_others_default_)));
StartWatching(); StartWatching();
policy_watcher_->SetPolicies(&nat_one); policy_watcher_->SetPolicies(&domain_full_);
StopWatching(); StopWatching();
} }
TEST_F(PolicyWatcherTest, NatNoneThenTrue) { TEST_F(PolicyWatcherTest, NatNoneThenTrue) {
EXPECT_CALL(mock_policy_callback_, OnPolicyUpdatePtr(IsPolicies(&nat_true))); EXPECT_CALL(mock_policy_callback_,
OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
StartWatching(); StartWatching();
policy_watcher_->SetPolicies(&empty); policy_watcher_->SetPolicies(&empty_);
policy_watcher_->SetPolicies(&nat_true); policy_watcher_->SetPolicies(&nat_true_);
StopWatching(); StopWatching();
} }
TEST_F(PolicyWatcherTest, NatNoneThenTrueThenTrue) { TEST_F(PolicyWatcherTest, NatNoneThenTrueThenTrue) {
EXPECT_CALL(mock_policy_callback_, OnPolicyUpdatePtr(IsPolicies(&nat_true))); EXPECT_CALL(mock_policy_callback_,
OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
StartWatching(); StartWatching();
policy_watcher_->SetPolicies(&empty); policy_watcher_->SetPolicies(&empty_);
policy_watcher_->SetPolicies(&nat_true); policy_watcher_->SetPolicies(&nat_true_);
policy_watcher_->SetPolicies(&nat_true); policy_watcher_->SetPolicies(&nat_true_);
StopWatching(); StopWatching();
} }
TEST_F(PolicyWatcherTest, NatNoneThenTrueThenTrueThenFalse) { TEST_F(PolicyWatcherTest, NatNoneThenTrueThenTrueThenFalse) {
testing::InSequence sequence; testing::InSequence sequence;
EXPECT_CALL(mock_policy_callback_, OnPolicyUpdatePtr(IsPolicies(&nat_true))); EXPECT_CALL(mock_policy_callback_,
EXPECT_CALL(mock_policy_callback_, OnPolicyUpdatePtr(IsPolicies(&nat_false))); OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
EXPECT_CALL(mock_policy_callback_,
OnPolicyUpdatePtr(IsPolicies(&nat_false_)));
StartWatching(); StartWatching();
policy_watcher_->SetPolicies(&empty); policy_watcher_->SetPolicies(&empty_);
policy_watcher_->SetPolicies(&nat_true); policy_watcher_->SetPolicies(&nat_true_);
policy_watcher_->SetPolicies(&nat_true); policy_watcher_->SetPolicies(&nat_true_);
policy_watcher_->SetPolicies(&nat_false); policy_watcher_->SetPolicies(&nat_false_);
StopWatching(); StopWatching();
} }
TEST_F(PolicyWatcherTest, NatNoneThenFalse) { TEST_F(PolicyWatcherTest, NatNoneThenFalse) {
testing::InSequence sequence; testing::InSequence sequence;
EXPECT_CALL(mock_policy_callback_, OnPolicyUpdatePtr(IsPolicies(&nat_true))); EXPECT_CALL(mock_policy_callback_,
EXPECT_CALL(mock_policy_callback_, OnPolicyUpdatePtr(IsPolicies(&nat_false))); OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
EXPECT_CALL(mock_policy_callback_,
OnPolicyUpdatePtr(IsPolicies(&nat_false_)));
StartWatching(); StartWatching();
policy_watcher_->SetPolicies(&empty); policy_watcher_->SetPolicies(&empty_);
policy_watcher_->SetPolicies(&nat_false); policy_watcher_->SetPolicies(&nat_false_);
StopWatching(); StopWatching();
} }
TEST_F(PolicyWatcherTest, NatNoneThenFalseThenTrue) { TEST_F(PolicyWatcherTest, NatNoneThenFalseThenTrue) {
testing::InSequence sequence; testing::InSequence sequence;
EXPECT_CALL(mock_policy_callback_, OnPolicyUpdatePtr(IsPolicies(&nat_true))); EXPECT_CALL(mock_policy_callback_,
EXPECT_CALL(mock_policy_callback_, OnPolicyUpdatePtr(IsPolicies(&nat_false))); OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
EXPECT_CALL(mock_policy_callback_, OnPolicyUpdatePtr(IsPolicies(&nat_true))); EXPECT_CALL(mock_policy_callback_,
OnPolicyUpdatePtr(IsPolicies(&nat_false_)));
EXPECT_CALL(mock_policy_callback_,
OnPolicyUpdatePtr(IsPolicies(&nat_true_)));
StartWatching();
policy_watcher_->SetPolicies(&empty_);
policy_watcher_->SetPolicies(&nat_false_);
policy_watcher_->SetPolicies(&nat_true_);
StopWatching();
}
TEST_F(PolicyWatcherTest, ChangeOneRepeatedlyThenTwo) {
testing::InSequence sequence;
EXPECT_CALL(mock_policy_callback_,
OnPolicyUpdatePtr(IsPolicies(&nat_true_domain_empty_)));
EXPECT_CALL(mock_policy_callback_,
OnPolicyUpdatePtr(IsPolicies(&domain_full_)));
EXPECT_CALL(mock_policy_callback_,
OnPolicyUpdatePtr(IsPolicies(&nat_false_)));
EXPECT_CALL(mock_policy_callback_,
OnPolicyUpdatePtr(IsPolicies(&domain_empty_)));
EXPECT_CALL(mock_policy_callback_,
OnPolicyUpdatePtr(IsPolicies(&nat_true_domain_full_)));
StartWatching(); StartWatching();
policy_watcher_->SetPolicies(&empty); policy_watcher_->SetPolicies(&nat_true_domain_empty_);
policy_watcher_->SetPolicies(&nat_false); policy_watcher_->SetPolicies(&nat_true_domain_full_);
policy_watcher_->SetPolicies(&nat_true); policy_watcher_->SetPolicies(&nat_false_domain_full_);
policy_watcher_->SetPolicies(&nat_false_domain_empty_);
policy_watcher_->SetPolicies(&nat_true_domain_full_);
StopWatching(); StopWatching();
} }
......
...@@ -151,6 +151,7 @@ class PolicyWatcherWin : ...@@ -151,6 +151,7 @@ class PolicyWatcherWin :
policy->SetBoolean(policy_name, bool_value); policy->SetBoolean(policy_name, bool_value);
} }
} }
// TODO(simonmorris): Read policies whose names are in kStringPolicyNames.
return policy; return policy;
} }
......
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