Home | History | Annotate | Download | only in browser
      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                                              string16* error) const {
     29   return true;
     30 }
     31 
     32 bool ManagementPolicy::Provider::UserMayModifySettings(
     33     const Extension* extension, string16* error) const {
     34   return true;
     35 }
     36 
     37 bool ManagementPolicy::Provider::MustRemainEnabled(const Extension* extension,
     38                                                    string16* error) const {
     39   return false;
     40 }
     41 
     42 bool ManagementPolicy::Provider::MustRemainDisabled(
     43     const Extension* extension,
     44     Extension::DisableReason* reason,
     45     string16* error) const {
     46   return false;
     47 }
     48 
     49 void ManagementPolicy::RegisterProvider(Provider* provider) {
     50   providers_.insert(provider);
     51 }
     52 
     53 void ManagementPolicy::UnregisterProvider(Provider* provider) {
     54   providers_.erase(provider);
     55 }
     56 
     57 bool ManagementPolicy::UserMayLoad(const Extension* extension,
     58                                    string16* error) const {
     59   return ApplyToProviderList(&Provider::UserMayLoad, "Installation",
     60                              true, extension, error);
     61 }
     62 
     63 bool ManagementPolicy::UserMayModifySettings(const Extension* extension,
     64                                              string16* error) const {
     65   return ApplyToProviderList(&Provider::UserMayModifySettings, "Modification",
     66                              true, extension, error);
     67 }
     68 
     69 bool ManagementPolicy::MustRemainEnabled(const Extension* extension,
     70                                          string16* error) const {
     71   return ApplyToProviderList(&Provider::MustRemainEnabled, "Disabling",
     72                              false, extension, error);
     73 }
     74 
     75 bool ManagementPolicy::MustRemainDisabled(const Extension* extension,
     76                                           Extension::DisableReason* reason,
     77                                           string16* error) const {
     78   for (ProviderList::const_iterator it = providers_.begin();
     79        it != providers_.end(); ++it)
     80     if ((*it)->MustRemainDisabled(extension, reason, error))
     81       return true;
     82 
     83   return false;
     84 }
     85 
     86 void ManagementPolicy::UnregisterAllProviders() {
     87   providers_.clear();
     88 }
     89 
     90 int ManagementPolicy::GetNumProviders() const {
     91   return providers_.size();
     92 }
     93 
     94 bool ManagementPolicy::ApplyToProviderList(ProviderFunction function,
     95                                            const char* debug_operation_name,
     96                                            bool normal_result,
     97                                            const Extension* extension,
     98                                            string16* error) const {
     99   for (ProviderList::const_iterator it = providers_.begin();
    100        it != providers_.end(); ++it) {
    101     const Provider* provider = *it;
    102     bool result = (provider->*function)(extension, error);
    103     if (result != normal_result) {
    104       std::string id;
    105       std::string name;
    106       GetExtensionNameAndId(extension, &name, &id);
    107       DVLOG(1) << debug_operation_name << " of extension " << name
    108                << " (" << id << ")"
    109                << " prohibited by " << provider->GetDebugPolicyProviderName();
    110       return !normal_result;
    111     }
    112   }
    113   return normal_result;
    114 }
    115 
    116 }  // namespace extensions
    117