Make default apps cache multiprofile friendly
[chromium-blink-merge.git] / chrome / browser / extensions / external_policy_loader.cc
blob61c83638c5fb1112b23265a87f29a82eadc92ff4
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.
5 #include "chrome/browser/extensions/external_policy_loader.h"
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"
19 namespace extensions {
21 ExternalPolicyLoader::ExternalPolicyLoader(Profile* profile)
22 : profile_(profile) {
23 pref_change_registrar_.Init(profile_->GetPrefs());
24 pref_change_registrar_.Add(prefs::kExtensionInstallForceList,
25 base::Bind(&ExternalPolicyLoader::StartLoading,
26 base::Unretained(this)));
27 pref_change_registrar_.Add(prefs::kExtensionAllowedTypes,
28 base::Bind(&ExternalPolicyLoader::StartLoading,
29 base::Unretained(this)));
30 notification_registrar_.Add(this,
31 chrome::NOTIFICATION_PROFILE_DESTROYED,
32 content::Source<Profile>(profile_));
35 // static
36 void ExternalPolicyLoader::AddExtension(base::DictionaryValue* dict,
37 const std::string& extension_id,
38 const std::string& update_url) {
39 dict->SetString(base::StringPrintf("%s.%s", extension_id.c_str(),
40 ExternalProviderImpl::kExternalUpdateUrl),
41 update_url);
44 void ExternalPolicyLoader::Observe(
45 int type,
46 const content::NotificationSource& source,
47 const content::NotificationDetails& details) {
48 if (profile_ == NULL) return;
49 DCHECK(type == chrome::NOTIFICATION_PROFILE_DESTROYED) <<
50 "Unexpected notification type.";
51 if (content::Source<Profile>(source).ptr() == profile_) {
52 notification_registrar_.RemoveAll();
53 pref_change_registrar_.RemoveAll();
54 profile_ = NULL;
58 void ExternalPolicyLoader::StartLoading() {
59 const DictionaryValue* forcelist =
60 profile_->GetPrefs()->GetDictionary(prefs::kExtensionInstallForceList);
61 prefs_.reset(forcelist ? forcelist->DeepCopy() : NULL);
62 LoadFinished();
65 } // namespace extensions