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

Side by Side Diff: chrome/browser/extensions/extension_prefs.cc

Issue 140018: Make ExtensionPrefs paths relative, re-enable 2 ExtensionsService unit_tests (Closed)
Patch Set: final changes Created 11 years, 6 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
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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/extension_prefs.h" 5 #include "chrome/browser/extensions/extension_prefs.h"
6 6
7 #include "base/string_util.h" 7 #include "base/string_util.h"
8 #include "chrome/common/extensions/extension.h" 8 #include "chrome/common/extensions/extension.h"
9 9
10 namespace { 10 namespace {
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 continue; 61 continue;
62 } 62 }
63 Extension::Location location = 63 Extension::Location location =
64 static_cast<Extension::Location>(location_value); 64 static_cast<Extension::Location>(location_value);
65 callback->Run(WideToASCII(*extension_id), FilePath(path), location); 65 callback->Run(WideToASCII(*extension_id), FilePath(path), location);
66 } 66 }
67 } 67 }
68 68
69 //////////////////////////////////////////////////////////////////////////////// 69 ////////////////////////////////////////////////////////////////////////////////
70 70
71 ExtensionPrefs::ExtensionPrefs(PrefService* prefs) : prefs_(prefs) { 71 ExtensionPrefs::ExtensionPrefs(PrefService* prefs, const FilePath& root_dir)
72 : prefs_(prefs),
73 install_directory_(root_dir) {
72 if (!prefs_->FindPreference(kExtensionsPref)) 74 if (!prefs_->FindPreference(kExtensionsPref))
73 prefs_->RegisterDictionaryPref(kExtensionsPref); 75 prefs_->RegisterDictionaryPref(kExtensionsPref);
74 if (!prefs->FindPreference(kExtensionShelf)) 76 if (!prefs->FindPreference(kExtensionShelf))
75 prefs->RegisterListPref(kExtensionShelf); 77 prefs->RegisterListPref(kExtensionShelf);
78 MakePathsRelative();
79 }
80
81 static FilePath::StringType MakePathRelative(const FilePath& parent,
82 const FilePath& child,
83 bool *dirty) {
84 if (!parent.IsParent(child))
85 return child.value();
86
87 if (dirty)
88 *dirty = true;
89 FilePath::StringType retval = child.value().substr(
90 parent.value().length());
91 if (FilePath::IsSeparator(retval[0]))
92 return retval.substr(1);
93 else
94 return retval;
95 }
96
97 void ExtensionPrefs::MakePathsRelative() {
98 bool dirty = false;
99 const DictionaryValue* dict = prefs_->GetMutableDictionary(kExtensionsPref);
100 if (!dict || dict->GetSize() == 0)
101 return;
102
103 for (DictionaryValue::key_iterator i = dict->begin_keys();
104 i != dict->end_keys(); ++i) {
105 DictionaryValue* extension_dict;
106 if (!dict->GetDictionary(*i, &extension_dict))
107 continue;
108 FilePath::StringType path_string;
109 if (!extension_dict->GetString(kPrefPath, &path_string))
110 continue;
111 FilePath path(path_string);
112 if (path.IsAbsolute()) {
113 extension_dict->SetString(kPrefPath,
114 MakePathRelative(install_directory_, path, &dirty));
115 }
116 }
117 if (dirty)
118 prefs_->ScheduleSavePersistentPrefs();
119 }
120
121 void ExtensionPrefs::MakePathsAbsolute(DictionaryValue* dict) {
122 if (!dict || dict->GetSize() == 0)
123 return;
124
125 for (DictionaryValue::key_iterator i = dict->begin_keys();
126 i != dict->end_keys(); ++i) {
127 DictionaryValue* extension_dict;
128 if (!dict->GetDictionary(*i, &extension_dict)) {
129 NOTREACHED();
130 continue;
131 }
132 FilePath::StringType path_string;
133 if (!extension_dict->GetString(kPrefPath, &path_string)) {
134 NOTREACHED();
135 continue;
136 }
137 DCHECK(!FilePath(path_string).IsAbsolute());
138 extension_dict->SetString(
139 kPrefPath, install_directory_.Append(path_string).value());
140 }
76 } 141 }
77 142
78 DictionaryValue* ExtensionPrefs::CopyCurrentExtensions() { 143 DictionaryValue* ExtensionPrefs::CopyCurrentExtensions() {
79 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); 144 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref);
80 if (extensions) { 145 if (extensions) {
81 DictionaryValue* copy = 146 DictionaryValue* copy =
82 static_cast<DictionaryValue*>(extensions->DeepCopy()); 147 static_cast<DictionaryValue*>(extensions->DeepCopy());
148 MakePathsAbsolute(copy);
83 return copy; 149 return copy;
84 } 150 }
85 return new DictionaryValue; 151 return new DictionaryValue;
86 } 152 }
87 153
88 void ExtensionPrefs::GetKilledExtensionIds(std::set<std::string>* killed_ids) { 154 void ExtensionPrefs::GetKilledExtensionIds(std::set<std::string>* killed_ids) {
89 const DictionaryValue* dict = prefs_->GetDictionary(kExtensionsPref); 155 const DictionaryValue* dict = prefs_->GetDictionary(kExtensionsPref);
90 if (!dict || dict->GetSize() == 0) 156 if (!dict || dict->GetSize() == 0)
91 return; 157 return;
92 158
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 } 203 }
138 prefs_->ScheduleSavePersistentPrefs(); 204 prefs_->ScheduleSavePersistentPrefs();
139 } 205 }
140 206
141 void ExtensionPrefs::OnExtensionInstalled(Extension* extension) { 207 void ExtensionPrefs::OnExtensionInstalled(Extension* extension) {
142 std::string id = extension->id(); 208 std::string id = extension->id();
143 UpdateExtensionPref(id, kPrefState, 209 UpdateExtensionPref(id, kPrefState,
144 Value::CreateIntegerValue(Extension::ENABLED)); 210 Value::CreateIntegerValue(Extension::ENABLED));
145 UpdateExtensionPref(id, kPrefLocation, 211 UpdateExtensionPref(id, kPrefLocation,
146 Value::CreateIntegerValue(extension->location())); 212 Value::CreateIntegerValue(extension->location()));
147 UpdateExtensionPref(id, kPrefPath, 213 FilePath::StringType path = MakePathRelative(install_directory_,
148 Value::CreateStringValue(extension->path().value())); 214 extension->path(), NULL);
215 UpdateExtensionPref(id, kPrefPath, Value::CreateStringValue(path));
149 prefs_->ScheduleSavePersistentPrefs(); 216 prefs_->ScheduleSavePersistentPrefs();
150 } 217 }
151 218
152 void ExtensionPrefs::OnExtensionUninstalled(const Extension* extension, 219 void ExtensionPrefs::OnExtensionUninstalled(const Extension* extension,
153 bool external_uninstall) { 220 bool external_uninstall) {
154 // For external extensions, we save a preference reminding ourself not to try 221 // For external extensions, we save a preference reminding ourself not to try
155 // and install the extension anymore (except when |external_uninstall| is 222 // and install the extension anymore (except when |external_uninstall| is
156 // true, which signifies that the registry key was deleted or the pref file 223 // true, which signifies that the registry key was deleted or the pref file
157 // no longer lists the extension). 224 // no longer lists the extension).
158 if (!external_uninstall && 225 if (!external_uninstall &&
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
192 DictionaryValue* extension = NULL; 259 DictionaryValue* extension = NULL;
193 std::wstring id = ASCIIToWide(extension_id); 260 std::wstring id = ASCIIToWide(extension_id);
194 if (!dict->GetDictionary(id, &extension)) { 261 if (!dict->GetDictionary(id, &extension)) {
195 // Extension pref does not exist, create it. 262 // Extension pref does not exist, create it.
196 extension = new DictionaryValue(); 263 extension = new DictionaryValue();
197 dict->Set(id, extension); 264 dict->Set(id, extension);
198 } 265 }
199 return extension; 266 return extension;
200 } 267 }
201 268
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_prefs.h ('k') | chrome/browser/extensions/extensions_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698