| 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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 } while (true); | 63 } while (true); |
| 64 | 64 |
| 65 return external_extension_paths; | 65 return external_extension_paths; |
| 66 } | 66 } |
| 67 | 67 |
| 68 // Extracts extension information from a json file serialized by |serializer|. | 68 // Extracts extension information from a json file serialized by |serializer|. |
| 69 // |path| is only used for informational purposes (outputted when an error | 69 // |path| is only used for informational purposes (outputted when an error |
| 70 // occurs). An empty dictionary is returned in case of failure (e.g. invalid | 70 // occurs). An empty dictionary is returned in case of failure (e.g. invalid |
| 71 // path or json content). | 71 // path or json content). |
| 72 // Caller takes ownership of the returned dictionary. | 72 // Caller takes ownership of the returned dictionary. |
| 73 base::DictionaryValue* ExtractExtensionPrefs(base::ValueSerializer* serializer, | 73 base::DictionaryValue* ExtractExtensionPrefs( |
| 74 const base::FilePath& path) { | 74 base::ValueDeserializer* deserializer, |
| 75 const base::FilePath& path) { |
| 75 std::string error_msg; | 76 std::string error_msg; |
| 76 base::Value* extensions = serializer->Deserialize(NULL, &error_msg); | 77 base::Value* extensions = deserializer->Deserialize(NULL, &error_msg); |
| 77 if (!extensions) { | 78 if (!extensions) { |
| 78 LOG(WARNING) << "Unable to deserialize json data: " << error_msg | 79 LOG(WARNING) << "Unable to deserialize json data: " << error_msg |
| 79 << " in file " << path.value() << "."; | 80 << " in file " << path.value() << "."; |
| 80 return new base::DictionaryValue; | 81 return new base::DictionaryValue; |
| 81 } | 82 } |
| 82 | 83 |
| 83 base::DictionaryValue* ext_dictionary = NULL; | 84 base::DictionaryValue* ext_dictionary = NULL; |
| 84 if (extensions->GetAsDictionary(&ext_dictionary)) | 85 if (extensions->GetAsDictionary(&ext_dictionary)) |
| 85 return ext_dictionary; | 86 return ext_dictionary; |
| 86 | 87 |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 return; | 248 return; |
| 248 } | 249 } |
| 249 #else | 250 #else |
| 250 // The only platform that uses this check is Mac OS. If you add one, | 251 // The only platform that uses this check is Mac OS. If you add one, |
| 251 // you need to implement base::VerifyPathControlledByAdmin() for | 252 // you need to implement base::VerifyPathControlledByAdmin() for |
| 252 // that platform. | 253 // that platform. |
| 253 NOTREACHED(); | 254 NOTREACHED(); |
| 254 #endif // defined(OS_MACOSX) | 255 #endif // defined(OS_MACOSX) |
| 255 } | 256 } |
| 256 | 257 |
| 257 JSONFileValueSerializer serializer(json_file); | 258 JSONFileValueDeserializer deserializer(json_file); |
| 258 scoped_ptr<base::DictionaryValue> ext_prefs( | 259 scoped_ptr<base::DictionaryValue> ext_prefs( |
| 259 ExtractExtensionPrefs(&serializer, json_file)); | 260 ExtractExtensionPrefs(&deserializer, json_file)); |
| 260 if (ext_prefs) | 261 if (ext_prefs) |
| 261 prefs->MergeDictionary(ext_prefs.get()); | 262 prefs->MergeDictionary(ext_prefs.get()); |
| 262 } | 263 } |
| 263 | 264 |
| 264 void ExternalPrefLoader::ReadStandaloneExtensionPrefFiles( | 265 void ExternalPrefLoader::ReadStandaloneExtensionPrefFiles( |
| 265 base::DictionaryValue* prefs) { | 266 base::DictionaryValue* prefs) { |
| 266 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 267 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 267 CHECK(NULL != prefs); | 268 CHECK(NULL != prefs); |
| 268 | 269 |
| 269 // First list the potential .json candidates. | 270 // First list the potential .json candidates. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 285 #if defined(OS_WIN) | 286 #if defined(OS_WIN) |
| 286 base::UTF16ToASCII( | 287 base::UTF16ToASCII( |
| 287 extension_candidate_path.RemoveExtension().BaseName().value()); | 288 extension_candidate_path.RemoveExtension().BaseName().value()); |
| 288 #elif defined(OS_POSIX) | 289 #elif defined(OS_POSIX) |
| 289 extension_candidate_path.RemoveExtension().BaseName().value(); | 290 extension_candidate_path.RemoveExtension().BaseName().value(); |
| 290 #endif | 291 #endif |
| 291 | 292 |
| 292 DVLOG(1) << "Reading json file: " | 293 DVLOG(1) << "Reading json file: " |
| 293 << extension_candidate_path.LossyDisplayName(); | 294 << extension_candidate_path.LossyDisplayName(); |
| 294 | 295 |
| 295 JSONFileValueSerializer serializer(extension_candidate_path); | 296 JSONFileValueDeserializer deserializer(extension_candidate_path); |
| 296 scoped_ptr<base::DictionaryValue> ext_prefs( | 297 scoped_ptr<base::DictionaryValue> ext_prefs( |
| 297 ExtractExtensionPrefs(&serializer, extension_candidate_path)); | 298 ExtractExtensionPrefs(&deserializer, extension_candidate_path)); |
| 298 if (ext_prefs) { | 299 if (ext_prefs) { |
| 299 DVLOG(1) << "Adding extension with id: " << id; | 300 DVLOG(1) << "Adding extension with id: " << id; |
| 300 prefs->Set(id, ext_prefs.release()); | 301 prefs->Set(id, ext_prefs.release()); |
| 301 } | 302 } |
| 302 } | 303 } |
| 303 } | 304 } |
| 304 | 305 |
| 305 ExternalTestingLoader::ExternalTestingLoader( | 306 ExternalTestingLoader::ExternalTestingLoader( |
| 306 const std::string& json_data, | 307 const std::string& json_data, |
| 307 const base::FilePath& fake_base_path) | 308 const base::FilePath& fake_base_path) |
| 308 : fake_base_path_(fake_base_path) { | 309 : fake_base_path_(fake_base_path) { |
| 309 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 310 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 310 JSONStringValueSerializer serializer(json_data); | 311 JSONStringValueDeserializer deserializer(json_data); |
| 311 base::FilePath fake_json_path = fake_base_path.AppendASCII("fake.json"); | 312 base::FilePath fake_json_path = fake_base_path.AppendASCII("fake.json"); |
| 312 testing_prefs_.reset(ExtractExtensionPrefs(&serializer, fake_json_path)); | 313 testing_prefs_.reset(ExtractExtensionPrefs(&deserializer, fake_json_path)); |
| 313 } | 314 } |
| 314 | 315 |
| 315 void ExternalTestingLoader::StartLoading() { | 316 void ExternalTestingLoader::StartLoading() { |
| 316 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 317 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 317 prefs_.reset(testing_prefs_->DeepCopy()); | 318 prefs_.reset(testing_prefs_->DeepCopy()); |
| 318 LoadFinished(); | 319 LoadFinished(); |
| 319 } | 320 } |
| 320 | 321 |
| 321 ExternalTestingLoader::~ExternalTestingLoader() {} | 322 ExternalTestingLoader::~ExternalTestingLoader() {} |
| 322 | 323 |
| 323 const base::FilePath ExternalTestingLoader::GetBaseCrxFilePath() { | 324 const base::FilePath ExternalTestingLoader::GetBaseCrxFilePath() { |
| 324 return fake_base_path_; | 325 return fake_base_path_; |
| 325 } | 326 } |
| 326 | 327 |
| 327 } // namespace extensions | 328 } // namespace extensions |
| OLD | NEW |