Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / extensions / extension_management_test_util.cc
blobe60f9fc22bdad25603b132f790d4663362b6b5be
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.
5 #include "chrome/browser/extensions/extension_management_test_util.h"
7 #include "components/crx_file/id_util.h"
9 namespace extensions {
11 namespace schema = schema_constants;
13 namespace {
15 std::string make_path(std::string a, std::string b) {
16 return a + "." + b;
19 const char kInstallSourcesPath[] = "*.install_sources";
20 const char kAllowedTypesPath[] = "*.allowed_types";
22 } // namespace
24 ExtensionManagementPrefUpdaterBase::ExtensionManagementPrefUpdaterBase() {
27 ExtensionManagementPrefUpdaterBase::~ExtensionManagementPrefUpdaterBase() {
30 void ExtensionManagementPrefUpdaterBase::UnsetPerExtensionSettings(
31 const ExtensionId& id) {
32 DCHECK(crx_file::id_util::IdIsValid(id));
33 pref_->RemoveWithoutPathExpansion(id, NULL);
36 void ExtensionManagementPrefUpdaterBase::ClearPerExtensionSettings(
37 const ExtensionId& id) {
38 DCHECK(crx_file::id_util::IdIsValid(id));
39 pref_->SetWithoutPathExpansion(id, new base::DictionaryValue());
42 void ExtensionManagementPrefUpdaterBase::SetBlacklistedByDefault(bool value) {
43 pref_->SetString(make_path(schema::kWildcard, schema::kInstallationMode),
44 value ? schema::kBlocked : schema::kAllowed);
47 void ExtensionManagementPrefUpdaterBase::
48 ClearInstallationModesForIndividualExtensions() {
49 for (base::DictionaryValue::Iterator it(*pref_.get()); !it.IsAtEnd();
50 it.Advance()) {
51 DCHECK(it.value().IsType(base::Value::TYPE_DICTIONARY));
52 if (it.key() != schema::kWildcard) {
53 DCHECK(crx_file::id_util::IdIsValid(it.key()));
54 pref_->Remove(make_path(it.key(), schema::kInstallationMode), NULL);
55 pref_->Remove(make_path(it.key(), schema::kUpdateUrl), NULL);
60 void
61 ExtensionManagementPrefUpdaterBase::SetIndividualExtensionInstallationAllowed(
62 const ExtensionId& id,
63 bool allowed) {
64 DCHECK(crx_file::id_util::IdIsValid(id));
65 pref_->SetString(make_path(id, schema::kInstallationMode),
66 allowed ? schema::kAllowed : schema::kBlocked);
67 pref_->Remove(make_path(id, schema::kUpdateUrl), NULL);
70 void ExtensionManagementPrefUpdaterBase::SetIndividualExtensionAutoInstalled(
71 const ExtensionId& id,
72 const std::string& update_url,
73 bool forced) {
74 DCHECK(crx_file::id_util::IdIsValid(id));
75 pref_->SetString(make_path(id, schema::kInstallationMode),
76 forced ? schema::kForceInstalled : schema::kNormalInstalled);
77 pref_->SetString(make_path(id, schema::kUpdateUrl), update_url);
80 void ExtensionManagementPrefUpdaterBase::UnsetInstallSources() {
81 pref_->Remove(kInstallSourcesPath, NULL);
84 void ExtensionManagementPrefUpdaterBase::ClearInstallSources() {
85 ClearList(kInstallSourcesPath);
88 void ExtensionManagementPrefUpdaterBase::AddInstallSource(
89 const std::string& install_source) {
90 AddStringToList(kInstallSourcesPath, install_source);
93 void ExtensionManagementPrefUpdaterBase::RemoveInstallSource(
94 const std::string& install_source) {
95 RemoveStringFromList(kInstallSourcesPath, install_source);
98 void ExtensionManagementPrefUpdaterBase::UnsetAllowedTypes() {
99 pref_->Remove(kAllowedTypesPath, NULL);
102 void ExtensionManagementPrefUpdaterBase::ClearAllowedTypes() {
103 ClearList(kAllowedTypesPath);
106 void ExtensionManagementPrefUpdaterBase::AddAllowedType(
107 const std::string& allowed_type) {
108 AddStringToList(kAllowedTypesPath, allowed_type);
111 const base::DictionaryValue* ExtensionManagementPrefUpdaterBase::GetPref() {
112 return pref_.get();
115 void ExtensionManagementPrefUpdaterBase::SetPref(base::DictionaryValue* pref) {
116 pref_.reset(pref);
119 scoped_ptr<base::DictionaryValue>
120 ExtensionManagementPrefUpdaterBase::TakePref() {
121 return pref_.Pass();
124 void ExtensionManagementPrefUpdaterBase::RemoveAllowedType(
125 const std::string& allowd_type) {
126 RemoveStringFromList(kAllowedTypesPath, allowd_type);
129 void ExtensionManagementPrefUpdaterBase::ClearList(const std::string& path) {
130 pref_->Set(path, new base::ListValue());
133 void ExtensionManagementPrefUpdaterBase::AddStringToList(
134 const std::string& path,
135 const std::string& str) {
136 base::ListValue* list_value = NULL;
137 if (!pref_->GetList(path, &list_value)) {
138 list_value = new base::ListValue();
139 pref_->Set(path, list_value);
141 CHECK(list_value->AppendIfNotPresent(new base::StringValue(str)));
144 void ExtensionManagementPrefUpdaterBase::RemoveStringFromList(
145 const std::string& path,
146 const std::string& str) {
147 base::ListValue* list_value = NULL;
148 if (pref_->GetList(path, &list_value))
149 CHECK(list_value->Remove(base::StringValue(str), NULL));
152 } // namespace extensions