Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / messaging / native_messaging_policy_handler.cc
blob170ca99ec2a03ac39f4338e5bfc003a0271a3c3c
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,
21 bool allow_wildcards)
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 {
44 return pref_path_;
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))
53 return false;
55 if (!value)
56 return true;
58 const base::ListValue* list_value = NULL;
59 if (!value->GetAsList(&list_value)) {
60 NOTREACHED();
61 return false;
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) {
68 std::string name;
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));
74 continue;
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);
81 continue;
83 filtered_list->Append(new base::StringValue(name));
86 if (names)
87 *names = filtered_list.Pass();
89 return true;
92 } // namespace extensions