Roll src/third_party/skia 054eef2:51985e3
[chromium-blink-merge.git] / extensions / browser / management_policy.cc
blobb09734a908b31b3b1c5c82a4555ed320544aa9d1
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 bool ManagementPolicy::Provider::MustRemainInstalled(
51 const Extension* extension,
52 base::string16* error) const {
53 return false;
56 void ManagementPolicy::RegisterProvider(Provider* provider) {
57 providers_.insert(provider);
60 void ManagementPolicy::UnregisterProvider(Provider* provider) {
61 providers_.erase(provider);
64 void ManagementPolicy::RegisterProviders(std::vector<Provider*> providers) {
65 providers_.insert(providers.begin(), providers.end());
68 bool ManagementPolicy::UserMayLoad(const Extension* extension,
69 base::string16* error) const {
70 return ApplyToProviderList(
71 &Provider::UserMayLoad, "Installation", true, extension, error);
74 bool ManagementPolicy::UserMayModifySettings(const Extension* extension,
75 base::string16* error) const {
76 return ApplyToProviderList(
77 &Provider::UserMayModifySettings, "Modification", true, extension, error);
80 bool ManagementPolicy::MustRemainEnabled(const Extension* extension,
81 base::string16* error) const {
82 return ApplyToProviderList(
83 &Provider::MustRemainEnabled, "Disabling", false, extension, error);
86 bool ManagementPolicy::MustRemainDisabled(const Extension* extension,
87 Extension::DisableReason* reason,
88 base::string16* error) const {
89 for (ProviderList::const_iterator it = providers_.begin();
90 it != providers_.end(); ++it)
91 if ((*it)->MustRemainDisabled(extension, reason, error))
92 return true;
94 return false;
97 bool ManagementPolicy::MustRemainInstalled(const Extension* extension,
98 base::string16* error) const {
99 return ApplyToProviderList(
100 &Provider::MustRemainInstalled, "Removing", false, extension, error);
103 void ManagementPolicy::UnregisterAllProviders() {
104 providers_.clear();
107 int ManagementPolicy::GetNumProviders() const {
108 return providers_.size();
111 bool ManagementPolicy::ApplyToProviderList(ProviderFunction function,
112 const char* debug_operation_name,
113 bool normal_result,
114 const Extension* extension,
115 base::string16* error) const {
116 for (ProviderList::const_iterator it = providers_.begin();
117 it != providers_.end(); ++it) {
118 const Provider* provider = *it;
119 bool result = (provider->*function)(extension, error);
120 if (result != normal_result) {
121 std::string id;
122 std::string name;
123 GetExtensionNameAndId(extension, &name, &id);
124 DVLOG(1) << debug_operation_name << " of extension " << name
125 << " (" << id << ")"
126 << " prohibited by " << provider->GetDebugPolicyProviderName();
127 return !normal_result;
130 return normal_result;
133 } // namespace extensions