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. 4 5 #include "extensions/browser/management_policy.h" 6 7 namespace extensions { 8 9 namespace { 10 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"; 17 } 18 19 } // namespace 20 21 ManagementPolicy::ManagementPolicy() { 22 } 23 24 ManagementPolicy::~ManagementPolicy() { 25 } 26 27 bool ManagementPolicy::Provider::UserMayLoad(const Extension* extension, 28 base::string16* error) const { 29 return true; 30 } 31 32 bool ManagementPolicy::Provider::UserMayModifySettings( 33 const Extension* extension, base::string16* error) const { 34 return true; 35 } 36 37 bool ManagementPolicy::Provider::MustRemainEnabled(const Extension* extension, 38 base::string16* error) 39 const { 40 return false; 41 } 42 43 bool ManagementPolicy::Provider::MustRemainDisabled( 44 const Extension* extension, 45 Extension::DisableReason* reason, 46 base::string16* error) const { 47 return false; 48 } 49 50 void ManagementPolicy::RegisterProvider(Provider* provider) { 51 providers_.insert(provider); 52 } 53 54 void ManagementPolicy::UnregisterProvider(Provider* provider) { 55 providers_.erase(provider); 56 } 57 58 bool ManagementPolicy::UserMayLoad(const Extension* extension, 59 base::string16* error) const { 60 return ApplyToProviderList(&Provider::UserMayLoad, "Installation", 61 true, extension, error); 62 } 63 64 bool ManagementPolicy::UserMayModifySettings(const Extension* extension, 65 base::string16* error) const { 66 return ApplyToProviderList(&Provider::UserMayModifySettings, "Modification", 67 true, extension, error); 68 } 69 70 bool ManagementPolicy::MustRemainEnabled(const Extension* extension, 71 base::string16* error) const { 72 return ApplyToProviderList(&Provider::MustRemainEnabled, "Disabling", 73 false, extension, error); 74 } 75 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; 83 84 return false; 85 } 86 87 void ManagementPolicy::UnregisterAllProviders() { 88 providers_.clear(); 89 } 90 91 int ManagementPolicy::GetNumProviders() const { 92 return providers_.size(); 93 } 94 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; 112 } 113 } 114 return normal_result; 115 } 116 117 } // namespace extensions 118