Home | History | Annotate | Download | only in extensions
      1 // Copyright (c) 2012 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 "chrome/browser/extensions/external_policy_loader.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/logging.h"
      9 #include "base/prefs/pref_service.h"
     10 #include "base/strings/stringprintf.h"
     11 #include "base/values.h"
     12 #include "chrome/browser/chrome_notification_types.h"
     13 #include "chrome/browser/extensions/external_provider_impl.h"
     14 #include "chrome/browser/profiles/profile.h"
     15 #include "chrome/common/pref_names.h"
     16 #include "content/public/browser/notification_details.h"
     17 #include "content/public/browser/notification_source.h"
     18 #include "extensions/browser/pref_names.h"
     19 
     20 namespace extensions {
     21 
     22 ExternalPolicyLoader::ExternalPolicyLoader(Profile* profile)
     23     : profile_(profile) {
     24   pref_change_registrar_.Init(profile_->GetPrefs());
     25   pref_change_registrar_.Add(pref_names::kInstallForceList,
     26                              base::Bind(&ExternalPolicyLoader::StartLoading,
     27                                         base::Unretained(this)));
     28   pref_change_registrar_.Add(pref_names::kAllowedTypes,
     29                              base::Bind(&ExternalPolicyLoader::StartLoading,
     30                                         base::Unretained(this)));
     31   notification_registrar_.Add(this,
     32                               chrome::NOTIFICATION_PROFILE_DESTROYED,
     33                               content::Source<Profile>(profile_));
     34 }
     35 
     36 // static
     37 void ExternalPolicyLoader::AddExtension(base::DictionaryValue* dict,
     38                                         const std::string& extension_id,
     39                                         const std::string& update_url) {
     40   dict->SetString(base::StringPrintf("%s.%s", extension_id.c_str(),
     41                                      ExternalProviderImpl::kExternalUpdateUrl),
     42                   update_url);
     43 }
     44 
     45 void ExternalPolicyLoader::Observe(
     46     int type,
     47     const content::NotificationSource& source,
     48     const content::NotificationDetails& details) {
     49   if (profile_ == NULL) return;
     50   DCHECK(type == chrome::NOTIFICATION_PROFILE_DESTROYED) <<
     51       "Unexpected notification type.";
     52   if (content::Source<Profile>(source).ptr() == profile_) {
     53     notification_registrar_.RemoveAll();
     54     pref_change_registrar_.RemoveAll();
     55     profile_ = NULL;
     56   }
     57 }
     58 
     59 void ExternalPolicyLoader::StartLoading() {
     60   const base::DictionaryValue* forcelist =
     61       profile_->GetPrefs()->GetDictionary(pref_names::kInstallForceList);
     62   prefs_.reset(forcelist ? forcelist->DeepCopy() : NULL);
     63   LoadFinished();
     64 }
     65 
     66 }  // namespace extensions
     67