binjin | b245438 | 2014-09-22 15:17:43 | [diff] [blame^] | 1 | // Copyright 2014 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/extension_management_test_util.h" |
| 6 | |
| 7 | #include "components/crx_file/id_util.h" |
| 8 | |
| 9 | namespace extensions { |
| 10 | |
| 11 | namespace schema = schema_constants; |
| 12 | |
| 13 | namespace { |
| 14 | |
| 15 | std::string make_path(std::string a, std::string b) { |
| 16 | return a + "." + b; |
| 17 | } |
| 18 | |
| 19 | const char kInstallSourcesPath[] = "*.install_sources"; |
| 20 | const char kAllowedTypesPath[] = "*.allowed_types"; |
| 21 | |
| 22 | } // namespace |
| 23 | |
| 24 | ExtensionManagementPrefUpdaterBase::ExtensionManagementPrefUpdaterBase() { |
| 25 | } |
| 26 | |
| 27 | ExtensionManagementPrefUpdaterBase::~ExtensionManagementPrefUpdaterBase() { |
| 28 | } |
| 29 | |
| 30 | void ExtensionManagementPrefUpdaterBase::SetBlacklistedByDefault(bool value) { |
| 31 | pref_->SetString(make_path(schema::kWildcard, schema::kInstallationMode), |
| 32 | value ? schema::kBlocked : schema::kAllowed); |
| 33 | } |
| 34 | |
| 35 | void ExtensionManagementPrefUpdaterBase:: |
| 36 | ClearInstallationModesForIndividualExtensions() { |
| 37 | for (base::DictionaryValue::Iterator it(*pref_.get()); !it.IsAtEnd(); |
| 38 | it.Advance()) { |
| 39 | DCHECK(it.value().IsType(base::Value::TYPE_DICTIONARY)); |
| 40 | if (it.key() != schema::kWildcard) { |
| 41 | DCHECK(crx_file::id_util::IdIsValid(it.key())); |
| 42 | pref_->Remove(make_path(it.key(), schema::kInstallationMode), NULL); |
| 43 | pref_->Remove(make_path(it.key(), schema::kUpdateUrl), NULL); |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | void |
| 49 | ExtensionManagementPrefUpdaterBase::SetIndividualExtensionInstallationAllowed( |
| 50 | const ExtensionId& id, |
| 51 | bool allowed) { |
| 52 | DCHECK(crx_file::id_util::IdIsValid(id)); |
| 53 | pref_->SetString(make_path(id, schema::kInstallationMode), |
| 54 | allowed ? schema::kAllowed : schema::kBlocked); |
| 55 | pref_->Remove(make_path(id, schema::kUpdateUrl), NULL); |
| 56 | } |
| 57 | |
| 58 | void ExtensionManagementPrefUpdaterBase::SetIndividualExtensionAutoInstalled( |
| 59 | const ExtensionId& id, |
| 60 | const std::string& update_url, |
| 61 | bool forced) { |
| 62 | DCHECK(crx_file::id_util::IdIsValid(id)); |
| 63 | pref_->SetString(make_path(id, schema::kInstallationMode), |
| 64 | forced ? schema::kForceInstalled : schema::kNormalInstalled); |
| 65 | pref_->SetString(make_path(id, schema::kUpdateUrl), update_url); |
| 66 | } |
| 67 | |
| 68 | void ExtensionManagementPrefUpdaterBase::UnsetInstallSources() { |
| 69 | pref_->Remove(kInstallSourcesPath, NULL); |
| 70 | } |
| 71 | |
| 72 | void ExtensionManagementPrefUpdaterBase::ClearInstallSources() { |
| 73 | ClearList(kInstallSourcesPath); |
| 74 | } |
| 75 | |
| 76 | void ExtensionManagementPrefUpdaterBase::AddInstallSource( |
| 77 | const std::string& install_source) { |
| 78 | AddStringToList(kInstallSourcesPath, install_source); |
| 79 | } |
| 80 | |
| 81 | void ExtensionManagementPrefUpdaterBase::RemoveInstallSource( |
| 82 | const std::string& install_source) { |
| 83 | RemoveStringFromList(kInstallSourcesPath, install_source); |
| 84 | } |
| 85 | |
| 86 | void ExtensionManagementPrefUpdaterBase::UnsetAllowedTypes() { |
| 87 | pref_->Remove(kAllowedTypesPath, NULL); |
| 88 | } |
| 89 | |
| 90 | void ExtensionManagementPrefUpdaterBase::ClearAllowedTypes() { |
| 91 | ClearList(kAllowedTypesPath); |
| 92 | } |
| 93 | |
| 94 | void ExtensionManagementPrefUpdaterBase::AddAllowedType( |
| 95 | const std::string& allowed_type) { |
| 96 | AddStringToList(kAllowedTypesPath, allowed_type); |
| 97 | } |
| 98 | |
| 99 | const base::DictionaryValue* ExtensionManagementPrefUpdaterBase::GetPref() { |
| 100 | return pref_.get(); |
| 101 | } |
| 102 | |
| 103 | void ExtensionManagementPrefUpdaterBase::SetPref(base::DictionaryValue* pref) { |
| 104 | pref_.reset(pref); |
| 105 | } |
| 106 | |
| 107 | scoped_ptr<base::DictionaryValue> |
| 108 | ExtensionManagementPrefUpdaterBase::TakePref() { |
| 109 | return pref_.Pass(); |
| 110 | } |
| 111 | |
| 112 | void ExtensionManagementPrefUpdaterBase::RemoveAllowedType( |
| 113 | const std::string& allowd_type) { |
| 114 | RemoveStringFromList(kAllowedTypesPath, allowd_type); |
| 115 | } |
| 116 | |
| 117 | void ExtensionManagementPrefUpdaterBase::ClearList(const std::string& path) { |
| 118 | pref_->Set(path, new base::ListValue()); |
| 119 | } |
| 120 | |
| 121 | void ExtensionManagementPrefUpdaterBase::AddStringToList( |
| 122 | const std::string& path, |
| 123 | const std::string& str) { |
| 124 | base::ListValue* list_value = NULL; |
| 125 | if (!pref_->GetList(path, &list_value)) { |
| 126 | list_value = new base::ListValue(); |
| 127 | pref_->Set(path, list_value); |
| 128 | } |
| 129 | CHECK(list_value->AppendIfNotPresent(new base::StringValue(str))); |
| 130 | } |
| 131 | |
| 132 | void ExtensionManagementPrefUpdaterBase::RemoveStringFromList( |
| 133 | const std::string& path, |
| 134 | const std::string& str) { |
| 135 | base::ListValue* list_value = NULL; |
| 136 | if (pref_->GetList(path, &list_value)) |
| 137 | CHECK(list_value->Remove(base::StringValue(str), NULL)); |
| 138 | } |
| 139 | |
| 140 | } // namespace extensions |