blob: 86b15cfe0523121a04bfdb44549178f9a7fee6a9 [file] [log] [blame]
binjinb2454382014-09-22 15:17:431// 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
9namespace extensions {
10
11namespace schema = schema_constants;
12
13namespace {
14
15std::string make_path(std::string a, std::string b) {
16 return a + "." + b;
17}
18
19const char kInstallSourcesPath[] = "*.install_sources";
20const char kAllowedTypesPath[] = "*.allowed_types";
21
22} // namespace
23
24ExtensionManagementPrefUpdaterBase::ExtensionManagementPrefUpdaterBase() {
25}
26
27ExtensionManagementPrefUpdaterBase::~ExtensionManagementPrefUpdaterBase() {
28}
29
30void ExtensionManagementPrefUpdaterBase::SetBlacklistedByDefault(bool value) {
31 pref_->SetString(make_path(schema::kWildcard, schema::kInstallationMode),
32 value ? schema::kBlocked : schema::kAllowed);
33}
34
35void 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
48void
49ExtensionManagementPrefUpdaterBase::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
58void 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
68void ExtensionManagementPrefUpdaterBase::UnsetInstallSources() {
69 pref_->Remove(kInstallSourcesPath, NULL);
70}
71
72void ExtensionManagementPrefUpdaterBase::ClearInstallSources() {
73 ClearList(kInstallSourcesPath);
74}
75
76void ExtensionManagementPrefUpdaterBase::AddInstallSource(
77 const std::string& install_source) {
78 AddStringToList(kInstallSourcesPath, install_source);
79}
80
81void ExtensionManagementPrefUpdaterBase::RemoveInstallSource(
82 const std::string& install_source) {
83 RemoveStringFromList(kInstallSourcesPath, install_source);
84}
85
86void ExtensionManagementPrefUpdaterBase::UnsetAllowedTypes() {
87 pref_->Remove(kAllowedTypesPath, NULL);
88}
89
90void ExtensionManagementPrefUpdaterBase::ClearAllowedTypes() {
91 ClearList(kAllowedTypesPath);
92}
93
94void ExtensionManagementPrefUpdaterBase::AddAllowedType(
95 const std::string& allowed_type) {
96 AddStringToList(kAllowedTypesPath, allowed_type);
97}
98
99const base::DictionaryValue* ExtensionManagementPrefUpdaterBase::GetPref() {
100 return pref_.get();
101}
102
103void ExtensionManagementPrefUpdaterBase::SetPref(base::DictionaryValue* pref) {
104 pref_.reset(pref);
105}
106
107scoped_ptr<base::DictionaryValue>
108ExtensionManagementPrefUpdaterBase::TakePref() {
109 return pref_.Pass();
110}
111
112void ExtensionManagementPrefUpdaterBase::RemoveAllowedType(
113 const std::string& allowd_type) {
114 RemoveStringFromList(kAllowedTypesPath, allowd_type);
115}
116
117void ExtensionManagementPrefUpdaterBase::ClearList(const std::string& path) {
118 pref_->Set(path, new base::ListValue());
119}
120
121void 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
132void 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