app_list: Fix possible out of bounds index.
[chromium-blink-merge.git] / extensions / browser / management_policy.cc
blob50183d04884cd5596df851ad43c7f58a5aaaf90b
1 // Copyright 2013 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 "extensions/browser/management_policy.h"
7 namespace extensions {
9 namespace {
11 void GetExtensionNameAndId(const Extension* extension,
12 std::string* name,
13 std::string* id) {
14 // The extension may be NULL in testing.
15 *id = extension ? extension->id() : "[test]";
16 *name = extension ? extension->name() : "test";
19 } // namespace
21 ManagementPolicy::ManagementPolicy() {
24 ManagementPolicy::~ManagementPolicy() {
27 bool ManagementPolicy::Provider::UserMayLoad(const Extension* extension,
28 base::string16* error) const {
29 return true;
32 bool ManagementPolicy::Provider::UserMayModifySettings(
33 const Extension* extension, base::string16* error) const {
34 return true;
37 bool ManagementPolicy::Provider::MustRemainEnabled(const Extension* extension,
38 base::string16* error)
39 const {
40 return false;
43 bool ManagementPolicy::Provider::MustRemainDisabled(
44 const Extension* extension,
45 Extension::DisableReason* reason,
46 base::string16* error) const {
47 return false;
50 void ManagementPolicy::RegisterProvider(Provider* provider) {
51 providers_.insert(provider);
54 void ManagementPolicy::UnregisterProvider(Provider* provider) {
55 providers_.erase(provider);
58 bool ManagementPolicy::UserMayLoad(const Extension* extension,
59 base::string16* error) const {
60 return ApplyToProviderList(&Provider::UserMayLoad, "Installation",
61 true, extension, error);
64 bool ManagementPolicy::UserMayModifySettings(const Extension* extension,
65 base::string16* error) const {
66 return ApplyToProviderList(&Provider::UserMayModifySettings, "Modification",
67 true, extension, error);
70 bool ManagementPolicy::MustRemainEnabled(const Extension* extension,
71 base::string16* error) const {
72 return ApplyToProviderList(&Provider::MustRemainEnabled, "Disabling",
73 false, extension, error);
76 bool ManagementPolicy::MustRemainDisabled(const Extension* extension,
77 Extension::DisableReason* reason,
78 base::string16* error) const {
79 for (ProviderList::const_iterator it = providers_.begin();
80 it != providers_.end(); ++it)
81 if ((*it)->MustRemainDisabled(extension, reason, error))
82 return true;
84 return false;
87 void ManagementPolicy::UnregisterAllProviders() {
88 providers_.clear();
91 int ManagementPolicy::GetNumProviders() const {
92 return providers_.size();
95 bool ManagementPolicy::ApplyToProviderList(ProviderFunction function,
96 const char* debug_operation_name,
97 bool normal_result,
98 const Extension* extension,
99 base::string16* error) const {
100 for (ProviderList::const_iterator it = providers_.begin();
101 it != providers_.end(); ++it) {
102 const Provider* provider = *it;
103 bool result = (provider->*function)(extension, error);
104 if (result != normal_result) {
105 std::string id;
106 std::string name;
107 GetExtensionNameAndId(extension, &name, &id);
108 DVLOG(1) << debug_operation_name << " of extension " << name
109 << " (" << id << ")"
110 << " prohibited by " << provider->GetDebugPolicyProviderName();
111 return !normal_result;
114 return normal_result;
117 } // namespace extensions