| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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_extension_loader.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/values.h" | |
| 9 #include "chrome/browser/prefs/pref_service.h" | |
| 10 #include "chrome/browser/profiles/profile.h" | |
| 11 #include "chrome/common/chrome_notification_types.h" | |
| 12 #include "chrome/common/extensions/extension.h" | |
| 13 #include "chrome/common/pref_names.h" | |
| 14 #include "content/public/browser/browser_thread.h" | |
| 15 #include "content/public/browser/notification_details.h" | |
| 16 #include "content/public/browser/notification_source.h" | |
| 17 #include "googleurl/src/gurl.h" | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 // Check an extension ID and an URL to be syntactically correct. | |
| 22 bool CheckExtension(const std::string& id, const std::string& update_url) { | |
| 23 GURL url(update_url); | |
| 24 if (!url.is_valid()) { | |
| 25 LOG(WARNING) << "Policy specifies invalid update URL for external " | |
| 26 << "extension: " << update_url; | |
| 27 return false; | |
| 28 } | |
| 29 if (!extensions::Extension::IdIsValid(id)) { | |
| 30 LOG(WARNING) << "Policy specifies invalid ID for external " | |
| 31 << "extension: " << id; | |
| 32 return false; | |
| 33 } | |
| 34 return true; | |
| 35 } | |
| 36 | |
| 37 } // namespace | |
| 38 | |
| 39 ExternalPolicyExtensionLoader::ExternalPolicyExtensionLoader( | |
| 40 Profile* profile) | |
| 41 : profile_(profile) { | |
| 42 pref_change_registrar_.Init(profile_->GetPrefs()); | |
| 43 pref_change_registrar_.Add(prefs::kExtensionInstallForceList, this); | |
| 44 notification_registrar_.Add(this, | |
| 45 chrome::NOTIFICATION_PROFILE_DESTROYED, | |
| 46 content::Source<Profile>(profile_)); | |
| 47 } | |
| 48 | |
| 49 void ExternalPolicyExtensionLoader::StartLoading() { | |
| 50 const ListValue* forcelist = | |
| 51 profile_->GetPrefs()->GetList(prefs::kExtensionInstallForceList); | |
| 52 DictionaryValue* result = new DictionaryValue(); | |
| 53 if (forcelist != NULL) { | |
| 54 std::string extension_desc; | |
| 55 for (ListValue::const_iterator it = forcelist->begin(); | |
| 56 it != forcelist->end(); ++it) { | |
| 57 if (!(*it)->GetAsString(&extension_desc)) { | |
| 58 LOG(WARNING) << "Failed to read forcelist string."; | |
| 59 } else { | |
| 60 // Each string item of the list has the following form: | |
| 61 // extension_id_code;extension_update_url | |
| 62 // The update URL might also contain semicolons. | |
| 63 size_t pos = extension_desc.find(';'); | |
| 64 std::string id = extension_desc.substr(0, pos); | |
| 65 std::string update_url = extension_desc.substr(pos+1); | |
| 66 if (CheckExtension(id, update_url)) { | |
| 67 result->SetString(id + ".external_update_url", update_url); | |
| 68 } | |
| 69 } | |
| 70 } | |
| 71 } | |
| 72 prefs_.reset(result); | |
| 73 LoadFinished(); | |
| 74 } | |
| 75 | |
| 76 void ExternalPolicyExtensionLoader::Observe( | |
| 77 int type, | |
| 78 const content::NotificationSource& source, | |
| 79 const content::NotificationDetails& details) { | |
| 80 if (profile_ == NULL) return; | |
| 81 switch (type) { | |
| 82 case chrome::NOTIFICATION_PREF_CHANGED: { | |
| 83 if (content::Source<PrefService>(source).ptr() == profile_->GetPrefs()) { | |
| 84 std::string* pref_name = content::Details<std::string>(details).ptr(); | |
| 85 if (*pref_name == prefs::kExtensionInstallForceList) { | |
| 86 StartLoading(); | |
| 87 } else { | |
| 88 NOTREACHED() << "Unexpected preference name."; | |
| 89 } | |
| 90 } | |
| 91 break; | |
| 92 } | |
| 93 case chrome::NOTIFICATION_PROFILE_DESTROYED: { | |
| 94 if (content::Source<Profile>(source).ptr() == profile_) { | |
| 95 notification_registrar_.RemoveAll(); | |
| 96 pref_change_registrar_.RemoveAll(); | |
| 97 profile_ = NULL; | |
| 98 } | |
| 99 break; | |
| 100 } | |
| 101 default: | |
| 102 NOTREACHED() << "Unexpected notification type."; | |
| 103 } | |
| 104 } | |
| OLD | NEW |