| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/external_pref_loader.h" | 5 #include "chrome/browser/extensions/external_pref_loader.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file_enumerator.h" | 8 #include "base/files/file_enumerator.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 } while (true); | 61 } while (true); |
| 62 | 62 |
| 63 return external_extension_paths; | 63 return external_extension_paths; |
| 64 } | 64 } |
| 65 | 65 |
| 66 // Extracts extension information from a json file serialized by |serializer|. | 66 // Extracts extension information from a json file serialized by |serializer|. |
| 67 // |path| is only used for informational purposes (outputted when an error | 67 // |path| is only used for informational purposes (outputted when an error |
| 68 // occurs). An empty dictionary is returned in case of failure (e.g. invalid | 68 // occurs). An empty dictionary is returned in case of failure (e.g. invalid |
| 69 // path or json content). | 69 // path or json content). |
| 70 // Caller takes ownership of the returned dictionary. | 70 // Caller takes ownership of the returned dictionary. |
| 71 base::DictionaryValue* ExtractExtensionPrefs(base::ValueSerializer* serializer, | 71 base::DictionaryValue* ExtractExtensionPrefs( |
| 72 const base::FilePath& path) { | 72 base::ValueDeserializer* deserializer, |
| 73 const base::FilePath& path) { |
| 73 std::string error_msg; | 74 std::string error_msg; |
| 74 base::Value* extensions = serializer->Deserialize(NULL, &error_msg); | 75 base::Value* extensions = deserializer->Deserialize(NULL, &error_msg); |
| 75 if (!extensions) { | 76 if (!extensions) { |
| 76 LOG(WARNING) << "Unable to deserialize json data: " << error_msg | 77 LOG(WARNING) << "Unable to deserialize json data: " << error_msg |
| 77 << " in file " << path.value() << "."; | 78 << " in file " << path.value() << "."; |
| 78 return new base::DictionaryValue; | 79 return new base::DictionaryValue; |
| 79 } | 80 } |
| 80 | 81 |
| 81 base::DictionaryValue* ext_dictionary = NULL; | 82 base::DictionaryValue* ext_dictionary = NULL; |
| 82 if (extensions->GetAsDictionary(&ext_dictionary)) | 83 if (extensions->GetAsDictionary(&ext_dictionary)) |
| 83 return ext_dictionary; | 84 return ext_dictionary; |
| 84 | 85 |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 return; | 214 return; |
| 214 } | 215 } |
| 215 #else | 216 #else |
| 216 // The only platform that uses this check is Mac OS. If you add one, | 217 // The only platform that uses this check is Mac OS. If you add one, |
| 217 // you need to implement base::VerifyPathControlledByAdmin() for | 218 // you need to implement base::VerifyPathControlledByAdmin() for |
| 218 // that platform. | 219 // that platform. |
| 219 NOTREACHED(); | 220 NOTREACHED(); |
| 220 #endif // defined(OS_MACOSX) | 221 #endif // defined(OS_MACOSX) |
| 221 } | 222 } |
| 222 | 223 |
| 223 JSONFileValueSerializer serializer(json_file); | 224 JSONFileValueDeserializer deserializer(json_file); |
| 224 scoped_ptr<base::DictionaryValue> ext_prefs( | 225 scoped_ptr<base::DictionaryValue> ext_prefs( |
| 225 ExtractExtensionPrefs(&serializer, json_file)); | 226 ExtractExtensionPrefs(&deserializer, json_file)); |
| 226 if (ext_prefs) | 227 if (ext_prefs) |
| 227 prefs->MergeDictionary(ext_prefs.get()); | 228 prefs->MergeDictionary(ext_prefs.get()); |
| 228 } | 229 } |
| 229 | 230 |
| 230 void ExternalPrefLoader::ReadStandaloneExtensionPrefFiles( | 231 void ExternalPrefLoader::ReadStandaloneExtensionPrefFiles( |
| 231 base::DictionaryValue* prefs) { | 232 base::DictionaryValue* prefs) { |
| 232 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 233 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 233 CHECK(NULL != prefs); | 234 CHECK(NULL != prefs); |
| 234 | 235 |
| 235 // First list the potential .json candidates. | 236 // First list the potential .json candidates. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 251 #if defined(OS_WIN) | 252 #if defined(OS_WIN) |
| 252 base::UTF16ToASCII( | 253 base::UTF16ToASCII( |
| 253 extension_candidate_path.RemoveExtension().BaseName().value()); | 254 extension_candidate_path.RemoveExtension().BaseName().value()); |
| 254 #elif defined(OS_POSIX) | 255 #elif defined(OS_POSIX) |
| 255 extension_candidate_path.RemoveExtension().BaseName().value(); | 256 extension_candidate_path.RemoveExtension().BaseName().value(); |
| 256 #endif | 257 #endif |
| 257 | 258 |
| 258 DVLOG(1) << "Reading json file: " | 259 DVLOG(1) << "Reading json file: " |
| 259 << extension_candidate_path.LossyDisplayName(); | 260 << extension_candidate_path.LossyDisplayName(); |
| 260 | 261 |
| 261 JSONFileValueSerializer serializer(extension_candidate_path); | 262 JSONFileValueDeserializer deserializer(extension_candidate_path); |
| 262 scoped_ptr<base::DictionaryValue> ext_prefs( | 263 scoped_ptr<base::DictionaryValue> ext_prefs( |
| 263 ExtractExtensionPrefs(&serializer, extension_candidate_path)); | 264 ExtractExtensionPrefs(&deserializer, extension_candidate_path)); |
| 264 if (ext_prefs) { | 265 if (ext_prefs) { |
| 265 DVLOG(1) << "Adding extension with id: " << id; | 266 DVLOG(1) << "Adding extension with id: " << id; |
| 266 prefs->Set(id, ext_prefs.release()); | 267 prefs->Set(id, ext_prefs.release()); |
| 267 } | 268 } |
| 268 } | 269 } |
| 269 } | 270 } |
| 270 | 271 |
| 271 ExternalTestingLoader::ExternalTestingLoader( | 272 ExternalTestingLoader::ExternalTestingLoader( |
| 272 const std::string& json_data, | 273 const std::string& json_data, |
| 273 const base::FilePath& fake_base_path) | 274 const base::FilePath& fake_base_path) |
| 274 : fake_base_path_(fake_base_path) { | 275 : fake_base_path_(fake_base_path) { |
| 275 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 276 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 276 JSONStringValueSerializer serializer(json_data); | 277 JSONStringValueDeserializer deserializer(json_data); |
| 277 base::FilePath fake_json_path = fake_base_path.AppendASCII("fake.json"); | 278 base::FilePath fake_json_path = fake_base_path.AppendASCII("fake.json"); |
| 278 testing_prefs_.reset(ExtractExtensionPrefs(&serializer, fake_json_path)); | 279 testing_prefs_.reset(ExtractExtensionPrefs(&deserializer, fake_json_path)); |
| 279 } | 280 } |
| 280 | 281 |
| 281 void ExternalTestingLoader::StartLoading() { | 282 void ExternalTestingLoader::StartLoading() { |
| 282 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 283 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 283 prefs_.reset(testing_prefs_->DeepCopy()); | 284 prefs_.reset(testing_prefs_->DeepCopy()); |
| 284 LoadFinished(); | 285 LoadFinished(); |
| 285 } | 286 } |
| 286 | 287 |
| 287 ExternalTestingLoader::~ExternalTestingLoader() {} | 288 ExternalTestingLoader::~ExternalTestingLoader() {} |
| 288 | 289 |
| 289 const base::FilePath ExternalTestingLoader::GetBaseCrxFilePath() { | 290 const base::FilePath ExternalTestingLoader::GetBaseCrxFilePath() { |
| 290 return fake_base_path_; | 291 return fake_base_path_; |
| 291 } | 292 } |
| 292 | 293 |
| 293 } // namespace extensions | 294 } // namespace extensions |
| OLD | NEW |