Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(298)

Side by Side Diff: chrome/browser/extensions/extension_management_test_util.h

Issue 559603002: Add new ExtensionManagement preference (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase; fixes Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_TEST_UTIL_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_TEST_UTIL_H_
7
8 #include "base/memory/scoped_ptr.h"
9 #include "base/values.h"
10 #include "chrome/browser/extensions/extension_management.h"
11 #include "components/crx_file/id_util.h"
12 #include "extensions/browser/pref_names.h"
13 #include "extensions/common/extension.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace extensions {
17
18 namespace schema = schema_constants;
19
20 // A helper class to manipulate the extension management preference in unit
21 // tests. It's a template class and |TestingPrefService| is suppored to be a
22 // subclass of TestingPrefServiceBase, which is another template class.
23 template <class TestingPrefService>
24 class ExtensionManagementPrefUpdater {
25 public:
26 explicit ExtensionManagementPrefUpdater(TestingPrefService* service)
27 : service_(service) {
28 const base::Value* pref_value =
29 service_->GetManagedPref(pref_names::kExtensionManagement);
30 if (pref_value) {
31 const base::DictionaryValue* dict_value = NULL;
32 pref_value->GetAsDictionary(&dict_value);
33 pref_.reset(dict_value->DeepCopy());
34 } else {
35 pref_.reset(new base::DictionaryValue);
36 }
37 }
38
39 virtual ~ExtensionManagementPrefUpdater() {
40 DCHECK(pref_.get());
41 service_->SetManagedPref(pref_names::kExtensionManagement, pref_.release());
Joao da Silva 2014/09/18 12:08:22 Break this class into 2: class ExtensionManagemen
binjin 2014/09/18 14:37:27 Done.
42 }
43
44 void SetBlacklistedByDefault(bool value) {
45 pref_->SetString(
46 make_path(schema::kAllOtherExtension, schema::kInstallationMode),
47 value ? schema::kBlocked : schema::kAllowed);
48 }
49
50 void ClearInstallationModesForIndividualExtensions() {
51 for (base::DictionaryValue::Iterator it(*pref_.get()); !it.IsAtEnd();
52 it.Advance()) {
53 DCHECK(it.value().IsType(base::Value::TYPE_DICTIONARY));
54 if (it.key() != schema::kAllOtherExtension) {
55 DCHECK(crx_file::id_util::IdIsValid(it.key()));
56 pref_->Remove(make_path(it.key(), schema::kInstallationMode), NULL);
57 pref_->Remove(make_path(it.key(), schema::kUpdateUrl), NULL);
58 }
59 }
60 }
61
62 void SetIndividualExtensionInstallationAllowed(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);
68 }
69
70 void SetIndividualExtensionAutoInstalled(const ExtensionId& id,
71 const std::string& update_url,
72 bool forced) {
73 DCHECK(crx_file::id_util::IdIsValid(id));
74 pref_->SetString(
75 make_path(id, schema::kInstallationMode),
76 forced ? schema::kForceInstalled : schema::kNormalInstalled);
77 pref_->SetString(make_path(id, schema::kUpdateUrl), update_url);
78 }
79
80 void UnsetInstallSources() { pref_->Remove(kInstallSourcesPath, NULL); }
81
82 void ClearInstallSources() { ClearList(kInstallSourcesPath); }
83
84 void AddInstallSource(const std::string& install_source) {
85 AddStringToList(kInstallSourcesPath, install_source);
86 }
87
88 void RemoveInstallSource(const std::string& install_source) {
89 RemoveStringFromList(kInstallSourcesPath, install_source);
90 }
91
92 void UnsetAllowedTypes() { pref_->Remove(kAllowedTypesPath, NULL); }
93
94 void ClearAllowedTypes() { ClearList(kAllowedTypesPath); }
95
96 void AddAllowedType(const std::string &allowed_type) {
97 AddStringToList(kAllowedTypesPath, allowed_type);
98 }
99
100 void RemoveAllowedType(const std::string& allowd_type) {
101 RemoveStringFromList(kAllowedTypesPath, allowd_type);
102 }
103
104 private:
105 const std::string kInstallSourcesPath =
106 make_path(schema::kAllOtherExtension, schema::kInstallSources);
107 const std::string kAllowedTypesPath =
108 make_path(schema::kAllOtherExtension, schema::kAllowedTypes);
109
110 static std::string make_path(std::string a, std::string b) {
111 return a + "." + b;
112 }
113
114 static std::string make_path3(std::string a, std::string b, std::string c) {
115 return a + "." + b + "." + c;
116 }
117
118 void ClearList(const std::string& path) {
119 base::ListValue* list_value = NULL;
120 if (pref_->GetList(path, &list_value))
121 list_value->Clear();
122 else
123 pref_->Set(path, new base::ListValue());
124 }
125
126 void AddStringToList(const std::string& path, const std::string& str) {
127 base::ListValue* list_value = NULL;
128 if (!pref_->GetList(path, &list_value)) {
129 list_value = new base::ListValue();
130 pref_->Set(path, list_value);
131 }
132 CHECK(list_value->AppendIfNotPresent(new base::StringValue(str)));
133 }
134
135 void RemoveStringFromList(const std::string& path, const std::string& str) {
136 base::ListValue* list_value = NULL;
137 if (pref_->GetList(path, &list_value))
138 CHECK(list_value->Remove(base::StringValue(str), NULL));
139 }
140
141 TestingPrefService* service_;
142 scoped_ptr<base::DictionaryValue> pref_;
143
144 DISALLOW_COPY_AND_ASSIGN(ExtensionManagementPrefUpdater);
145 };
146
147 } // namespace extensions
148
149 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_TEST_UTIL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698