| 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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/files/file_enumerator.h" | 10 #include "base/files/file_enumerator.h" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 13 #include "base/json/json_file_value_serializer.h" | 13 #include "base/json/json_file_value_serializer.h" |
| 14 #include "base/json/json_string_value_serializer.h" | 14 #include "base/json/json_string_value_serializer.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/memory/ptr_util.h" |
| 16 #include "base/metrics/histogram.h" | 17 #include "base/metrics/histogram.h" |
| 17 #include "base/path_service.h" | 18 #include "base/path_service.h" |
| 18 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
| 19 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
| 20 #include "build/build_config.h" | 21 #include "build/build_config.h" |
| 21 #include "chrome/browser/defaults.h" | 22 #include "chrome/browser/defaults.h" |
| 22 #include "chrome/browser/prefs/pref_service_syncable_util.h" | 23 #include "chrome/browser/prefs/pref_service_syncable_util.h" |
| 23 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
| 24 #include "chrome/browser/sync/profile_sync_service_factory.h" | 25 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 25 #include "chrome/common/chrome_paths.h" | 26 #include "chrome/common/chrome_paths.h" |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 } while (true); | 69 } while (true); |
| 69 | 70 |
| 70 return external_extension_paths; | 71 return external_extension_paths; |
| 71 } | 72 } |
| 72 | 73 |
| 73 // Extracts extension information from a json file serialized by |serializer|. | 74 // Extracts extension information from a json file serialized by |serializer|. |
| 74 // |path| is only used for informational purposes (outputted when an error | 75 // |path| is only used for informational purposes (outputted when an error |
| 75 // occurs). An empty dictionary is returned in case of failure (e.g. invalid | 76 // occurs). An empty dictionary is returned in case of failure (e.g. invalid |
| 76 // path or json content). | 77 // path or json content). |
| 77 // Caller takes ownership of the returned dictionary. | 78 // Caller takes ownership of the returned dictionary. |
| 78 scoped_ptr<base::DictionaryValue> ExtractExtensionPrefs( | 79 std::unique_ptr<base::DictionaryValue> ExtractExtensionPrefs( |
| 79 base::ValueDeserializer* deserializer, | 80 base::ValueDeserializer* deserializer, |
| 80 const base::FilePath& path) { | 81 const base::FilePath& path) { |
| 81 std::string error_msg; | 82 std::string error_msg; |
| 82 scoped_ptr<base::Value> extensions = | 83 std::unique_ptr<base::Value> extensions = |
| 83 deserializer->Deserialize(NULL, &error_msg); | 84 deserializer->Deserialize(NULL, &error_msg); |
| 84 if (!extensions) { | 85 if (!extensions) { |
| 85 LOG(WARNING) << "Unable to deserialize json data: " << error_msg | 86 LOG(WARNING) << "Unable to deserialize json data: " << error_msg |
| 86 << " in file " << path.value() << "."; | 87 << " in file " << path.value() << "."; |
| 87 return make_scoped_ptr(new base::DictionaryValue); | 88 return base::WrapUnique(new base::DictionaryValue); |
| 88 } | 89 } |
| 89 | 90 |
| 90 scoped_ptr<base::DictionaryValue> ext_dictionary = | 91 std::unique_ptr<base::DictionaryValue> ext_dictionary = |
| 91 base::DictionaryValue::From(std::move(extensions)); | 92 base::DictionaryValue::From(std::move(extensions)); |
| 92 if (ext_dictionary) { | 93 if (ext_dictionary) { |
| 93 return ext_dictionary; | 94 return ext_dictionary; |
| 94 } | 95 } |
| 95 | 96 |
| 96 LOG(WARNING) << "Expected a JSON dictionary in file " << path.value() | 97 LOG(WARNING) << "Expected a JSON dictionary in file " << path.value() |
| 97 << "."; | 98 << "."; |
| 98 return make_scoped_ptr(new base::DictionaryValue); | 99 return base::WrapUnique(new base::DictionaryValue); |
| 99 | |
| 100 } | 100 } |
| 101 | 101 |
| 102 } // namespace | 102 } // namespace |
| 103 | 103 |
| 104 namespace extensions { | 104 namespace extensions { |
| 105 | 105 |
| 106 ExternalPrefLoader::ExternalPrefLoader(int base_path_id, | 106 ExternalPrefLoader::ExternalPrefLoader(int base_path_id, |
| 107 Options options, | 107 Options options, |
| 108 Profile* profile) | 108 Profile* profile) |
| 109 : base_path_id_(base_path_id), | 109 : base_path_id_(base_path_id), |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 service->RemoveObserver(this); | 190 service->RemoveObserver(this); |
| 191 | 191 |
| 192 BrowserThread::PostTask( | 192 BrowserThread::PostTask( |
| 193 BrowserThread::FILE, FROM_HERE, | 193 BrowserThread::FILE, FROM_HERE, |
| 194 base::Bind(&ExternalPrefLoader::LoadOnFileThread, this)); | 194 base::Bind(&ExternalPrefLoader::LoadOnFileThread, this)); |
| 195 } | 195 } |
| 196 | 196 |
| 197 void ExternalPrefLoader::LoadOnFileThread() { | 197 void ExternalPrefLoader::LoadOnFileThread() { |
| 198 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 198 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 199 | 199 |
| 200 scoped_ptr<base::DictionaryValue> prefs(new base::DictionaryValue); | 200 std::unique_ptr<base::DictionaryValue> prefs(new base::DictionaryValue); |
| 201 | 201 |
| 202 // TODO(skerner): Some values of base_path_id_ will cause | 202 // TODO(skerner): Some values of base_path_id_ will cause |
| 203 // PathService::Get() to return false, because the path does | 203 // PathService::Get() to return false, because the path does |
| 204 // not exist. Find and fix the build/install scripts so that | 204 // not exist. Find and fix the build/install scripts so that |
| 205 // this can become a CHECK(). Known examples include chrome | 205 // this can become a CHECK(). Known examples include chrome |
| 206 // OS developer builds and linux install packages. | 206 // OS developer builds and linux install packages. |
| 207 // Tracked as crbug.com/70402 . | 207 // Tracked as crbug.com/70402 . |
| 208 if (PathService::Get(base_path_id_, &base_path_)) { | 208 if (PathService::Get(base_path_id_, &base_path_)) { |
| 209 ReadExternalExtensionPrefFile(prefs.get()); | 209 ReadExternalExtensionPrefFile(prefs.get()); |
| 210 | 210 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 } | 260 } |
| 261 #else | 261 #else |
| 262 // The only platform that uses this check is Mac OS. If you add one, | 262 // The only platform that uses this check is Mac OS. If you add one, |
| 263 // you need to implement base::VerifyPathControlledByAdmin() for | 263 // you need to implement base::VerifyPathControlledByAdmin() for |
| 264 // that platform. | 264 // that platform. |
| 265 NOTREACHED(); | 265 NOTREACHED(); |
| 266 #endif // defined(OS_MACOSX) | 266 #endif // defined(OS_MACOSX) |
| 267 } | 267 } |
| 268 | 268 |
| 269 JSONFileValueDeserializer deserializer(json_file); | 269 JSONFileValueDeserializer deserializer(json_file); |
| 270 scoped_ptr<base::DictionaryValue> ext_prefs = | 270 std::unique_ptr<base::DictionaryValue> ext_prefs = |
| 271 ExtractExtensionPrefs(&deserializer, json_file); | 271 ExtractExtensionPrefs(&deserializer, json_file); |
| 272 if (ext_prefs) | 272 if (ext_prefs) |
| 273 prefs->MergeDictionary(ext_prefs.get()); | 273 prefs->MergeDictionary(ext_prefs.get()); |
| 274 } | 274 } |
| 275 | 275 |
| 276 void ExternalPrefLoader::ReadStandaloneExtensionPrefFiles( | 276 void ExternalPrefLoader::ReadStandaloneExtensionPrefFiles( |
| 277 base::DictionaryValue* prefs) { | 277 base::DictionaryValue* prefs) { |
| 278 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 278 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 279 CHECK(NULL != prefs); | 279 CHECK(NULL != prefs); |
| 280 | 280 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 298 base::UTF16ToASCII( | 298 base::UTF16ToASCII( |
| 299 extension_candidate_path.RemoveExtension().BaseName().value()); | 299 extension_candidate_path.RemoveExtension().BaseName().value()); |
| 300 #elif defined(OS_POSIX) | 300 #elif defined(OS_POSIX) |
| 301 extension_candidate_path.RemoveExtension().BaseName().value(); | 301 extension_candidate_path.RemoveExtension().BaseName().value(); |
| 302 #endif | 302 #endif |
| 303 | 303 |
| 304 DVLOG(1) << "Reading json file: " | 304 DVLOG(1) << "Reading json file: " |
| 305 << extension_candidate_path.LossyDisplayName(); | 305 << extension_candidate_path.LossyDisplayName(); |
| 306 | 306 |
| 307 JSONFileValueDeserializer deserializer(extension_candidate_path); | 307 JSONFileValueDeserializer deserializer(extension_candidate_path); |
| 308 scoped_ptr<base::DictionaryValue> ext_prefs = | 308 std::unique_ptr<base::DictionaryValue> ext_prefs = |
| 309 ExtractExtensionPrefs(&deserializer, extension_candidate_path); | 309 ExtractExtensionPrefs(&deserializer, extension_candidate_path); |
| 310 if (ext_prefs) { | 310 if (ext_prefs) { |
| 311 DVLOG(1) << "Adding extension with id: " << id; | 311 DVLOG(1) << "Adding extension with id: " << id; |
| 312 prefs->Set(id, ext_prefs.release()); | 312 prefs->Set(id, ext_prefs.release()); |
| 313 } | 313 } |
| 314 } | 314 } |
| 315 } | 315 } |
| 316 | 316 |
| 317 ExternalTestingLoader::ExternalTestingLoader( | 317 ExternalTestingLoader::ExternalTestingLoader( |
| 318 const std::string& json_data, | 318 const std::string& json_data, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 330 LoadFinished(); | 330 LoadFinished(); |
| 331 } | 331 } |
| 332 | 332 |
| 333 ExternalTestingLoader::~ExternalTestingLoader() {} | 333 ExternalTestingLoader::~ExternalTestingLoader() {} |
| 334 | 334 |
| 335 const base::FilePath ExternalTestingLoader::GetBaseCrxFilePath() { | 335 const base::FilePath ExternalTestingLoader::GetBaseCrxFilePath() { |
| 336 return fake_base_path_; | 336 return fake_base_path_; |
| 337 } | 337 } |
| 338 | 338 |
| 339 } // namespace extensions | 339 } // namespace extensions |
| OLD | NEW |