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