1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/api/messaging/native_messaging_policy_handler.h"
7 #include "base/logging.h"
8 #include "base/prefs/pref_value_map.h"
9 #include "chrome/browser/extensions/api/messaging/native_messaging_host_manifest.h"
10 #include "chrome/browser/extensions/external_policy_loader.h"
11 #include "components/policy/core/browser/policy_error_map.h"
12 #include "components/policy/core/common/policy_map.h"
13 #include "grit/components_strings.h"
14 #include "policy/policy_constants.h"
16 namespace extensions
{
18 NativeMessagingHostListPolicyHandler::NativeMessagingHostListPolicyHandler(
19 const char* policy_name
,
20 const char* pref_path
,
22 : policy::TypeCheckingPolicyHandler(policy_name
, base::Value::TYPE_LIST
),
23 pref_path_(pref_path
),
24 allow_wildcards_(allow_wildcards
) {}
26 NativeMessagingHostListPolicyHandler::~NativeMessagingHostListPolicyHandler() {}
28 bool NativeMessagingHostListPolicyHandler::CheckPolicySettings(
29 const policy::PolicyMap
& policies
,
30 policy::PolicyErrorMap
* errors
) {
31 return CheckAndGetList(policies
, errors
, NULL
);
34 void NativeMessagingHostListPolicyHandler::ApplyPolicySettings(
35 const policy::PolicyMap
& policies
,
36 PrefValueMap
* prefs
) {
37 scoped_ptr
<base::ListValue
> list
;
38 policy::PolicyErrorMap errors
;
39 if (CheckAndGetList(policies
, &errors
, &list
) && list
)
40 prefs
->SetValue(pref_path(), list
.Pass());
43 const char* NativeMessagingHostListPolicyHandler::pref_path() const {
47 bool NativeMessagingHostListPolicyHandler::CheckAndGetList(
48 const policy::PolicyMap
& policies
,
49 policy::PolicyErrorMap
* errors
,
50 scoped_ptr
<base::ListValue
>* names
) {
51 const base::Value
* value
= NULL
;
52 if (!CheckAndGetValue(policies
, errors
, &value
))
58 const base::ListValue
* list_value
= NULL
;
59 if (!value
->GetAsList(&list_value
)) {
64 // Filter the list, rejecting any invalid native messaging host names.
65 scoped_ptr
<base::ListValue
> filtered_list(new base::ListValue());
66 for (base::ListValue::const_iterator
entry(list_value
->begin());
67 entry
!= list_value
->end(); ++entry
) {
69 if (!(*entry
)->GetAsString(&name
)) {
70 errors
->AddError(policy_name(),
71 entry
- list_value
->begin(),
72 IDS_POLICY_TYPE_ERROR
,
73 ValueTypeToString(base::Value::TYPE_STRING
));
76 if (!(allow_wildcards_
&& name
== "*") &&
77 !NativeMessagingHostManifest::IsValidName(name
)) {
78 errors
->AddError(policy_name(),
79 entry
- list_value
->begin(),
80 IDS_POLICY_VALUE_FORMAT_ERROR
);
83 filtered_list
->Append(new base::StringValue(name
));
87 *names
= filtered_list
.Pass();
92 } // namespace extensions