| 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_extension_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/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/json/json_file_value_serializer.h" | 10 #include "base/json/json_file_value_serializer.h" |
| 11 #include "base/json/json_string_value_serializer.h" | 11 #include "base/json/json_string_value_serializer.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 14 #include "base/path_service.h" | 14 #include "base/path_service.h" |
| 15 #include "base/string_util.h" | 15 #include "base/string_util.h" |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 if (extensions->GetAsDictionary(&ext_dictionary)) | 80 if (extensions->GetAsDictionary(&ext_dictionary)) |
| 81 return ext_dictionary; | 81 return ext_dictionary; |
| 82 | 82 |
| 83 LOG(WARNING) << "Expected a JSON dictionary in file " | 83 LOG(WARNING) << "Expected a JSON dictionary in file " |
| 84 << path.value() << "."; | 84 << path.value() << "."; |
| 85 return new DictionaryValue; | 85 return new DictionaryValue; |
| 86 } | 86 } |
| 87 | 87 |
| 88 } // namespace | 88 } // namespace |
| 89 | 89 |
| 90 ExternalPrefExtensionLoader::ExternalPrefExtensionLoader(int base_path_id, | 90 namespace extensions { |
| 91 Options options) | 91 |
| 92 : base_path_id_(base_path_id), | 92 ExternalPrefLoader::ExternalPrefLoader(int base_path_id, Options options) |
| 93 options_(options) { | 93 : base_path_id_(base_path_id), options_(options) { |
| 94 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 94 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 95 } | 95 } |
| 96 | 96 |
| 97 const FilePath ExternalPrefExtensionLoader::GetBaseCrxFilePath() { | 97 const FilePath ExternalPrefLoader::GetBaseCrxFilePath() { |
| 98 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 98 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 99 | 99 |
| 100 // |base_path_| was set in LoadOnFileThread(). | 100 // |base_path_| was set in LoadOnFileThread(). |
| 101 return base_path_; | 101 return base_path_; |
| 102 } | 102 } |
| 103 | 103 |
| 104 void ExternalPrefExtensionLoader::StartLoading() { | 104 void ExternalPrefLoader::StartLoading() { |
| 105 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 105 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 106 BrowserThread::PostTask( | 106 BrowserThread::PostTask( |
| 107 BrowserThread::FILE, FROM_HERE, | 107 BrowserThread::FILE, FROM_HERE, |
| 108 base::Bind(&ExternalPrefExtensionLoader::LoadOnFileThread, this)); | 108 base::Bind(&ExternalPrefLoader::LoadOnFileThread, this)); |
| 109 } | 109 } |
| 110 | 110 |
| 111 void ExternalPrefExtensionLoader::LoadOnFileThread() { | 111 void ExternalPrefLoader::LoadOnFileThread() { |
| 112 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 112 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 113 | 113 |
| 114 // TODO(skerner): Some values of base_path_id_ will cause | 114 // TODO(skerner): Some values of base_path_id_ will cause |
| 115 // PathService::Get() to return false, because the path does | 115 // PathService::Get() to return false, because the path does |
| 116 // not exist. Find and fix the build/install scripts so that | 116 // not exist. Find and fix the build/install scripts so that |
| 117 // this can become a CHECK(). Known examples include chrome | 117 // this can become a CHECK(). Known examples include chrome |
| 118 // OS developer builds and linux install packages. | 118 // OS developer builds and linux install packages. |
| 119 // Tracked as crbug.com/70402 . | 119 // Tracked as crbug.com/70402 . |
| 120 if (!PathService::Get(base_path_id_, &base_path_)) | 120 if (!PathService::Get(base_path_id_, &base_path_)) |
| 121 return; | 121 return; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 141 } | 141 } |
| 142 | 142 |
| 143 // If we have any records to process, then we must have | 143 // If we have any records to process, then we must have |
| 144 // read at least one .json file. If so, then we should have | 144 // read at least one .json file. If so, then we should have |
| 145 // set |base_path_|. | 145 // set |base_path_|. |
| 146 if (!prefs_->empty()) | 146 if (!prefs_->empty()) |
| 147 CHECK(!base_path_.empty()); | 147 CHECK(!base_path_.empty()); |
| 148 | 148 |
| 149 BrowserThread::PostTask( | 149 BrowserThread::PostTask( |
| 150 BrowserThread::UI, FROM_HERE, | 150 BrowserThread::UI, FROM_HERE, |
| 151 base::Bind(&ExternalPrefExtensionLoader::LoadFinished, this)); | 151 base::Bind(&ExternalPrefLoader::LoadFinished, this)); |
| 152 } | 152 } |
| 153 | 153 |
| 154 void ExternalPrefExtensionLoader::ReadExternalExtensionPrefFile( | 154 void ExternalPrefLoader::ReadExternalExtensionPrefFile(DictionaryValue* prefs) { |
| 155 DictionaryValue* prefs) { | |
| 156 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 155 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 157 CHECK(NULL != prefs); | 156 CHECK(NULL != prefs); |
| 158 | 157 |
| 159 FilePath json_file = base_path_.Append(kExternalExtensionJson); | 158 FilePath json_file = base_path_.Append(kExternalExtensionJson); |
| 160 | 159 |
| 161 if (!file_util::PathExists(json_file)) { | 160 if (!file_util::PathExists(json_file)) { |
| 162 // This is not an error. The file does not exist by default. | 161 // This is not an error. The file does not exist by default. |
| 163 return; | 162 return; |
| 164 } | 163 } |
| 165 | 164 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 182 #endif // defined(OS_MACOSX) | 181 #endif // defined(OS_MACOSX) |
| 183 } | 182 } |
| 184 | 183 |
| 185 JSONFileValueSerializer serializer(json_file); | 184 JSONFileValueSerializer serializer(json_file); |
| 186 scoped_ptr<DictionaryValue> ext_prefs( | 185 scoped_ptr<DictionaryValue> ext_prefs( |
| 187 ExtractExtensionPrefs(&serializer, json_file)); | 186 ExtractExtensionPrefs(&serializer, json_file)); |
| 188 if (ext_prefs.get()) | 187 if (ext_prefs.get()) |
| 189 prefs->MergeDictionary(ext_prefs.get()); | 188 prefs->MergeDictionary(ext_prefs.get()); |
| 190 } | 189 } |
| 191 | 190 |
| 192 void ExternalPrefExtensionLoader::ReadStandaloneExtensionPrefFiles( | 191 void ExternalPrefLoader::ReadStandaloneExtensionPrefFiles( |
| 193 DictionaryValue* prefs) { | 192 DictionaryValue* prefs) { |
| 194 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 193 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 195 CHECK(NULL != prefs); | 194 CHECK(NULL != prefs); |
| 196 | 195 |
| 197 // First list the potential .json candidates. | 196 // First list the potential .json candidates. |
| 198 std::set<FilePath> | 197 std::set<FilePath> |
| 199 candidates = GetPrefsCandidateFilesFromFolder(base_path_); | 198 candidates = GetPrefsCandidateFilesFromFolder(base_path_); |
| 200 if (candidates.empty()) { | 199 if (candidates.empty()) { |
| 201 DVLOG(1) << "Extension candidates list empty"; | 200 DVLOG(1) << "Extension candidates list empty"; |
| 202 return; | 201 return; |
| 203 } | 202 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 223 JSONFileValueSerializer serializer(extension_candidate_path); | 222 JSONFileValueSerializer serializer(extension_candidate_path); |
| 224 scoped_ptr<DictionaryValue> ext_prefs( | 223 scoped_ptr<DictionaryValue> ext_prefs( |
| 225 ExtractExtensionPrefs(&serializer, extension_candidate_path)); | 224 ExtractExtensionPrefs(&serializer, extension_candidate_path)); |
| 226 if (ext_prefs.get()) { | 225 if (ext_prefs.get()) { |
| 227 DVLOG(1) << "Adding extension with id: " << id; | 226 DVLOG(1) << "Adding extension with id: " << id; |
| 228 prefs->Set(id, ext_prefs.release()); | 227 prefs->Set(id, ext_prefs.release()); |
| 229 } | 228 } |
| 230 } | 229 } |
| 231 } | 230 } |
| 232 | 231 |
| 233 ExternalTestingExtensionLoader::ExternalTestingExtensionLoader( | 232 ExternalTestingLoader::ExternalTestingLoader(const std::string& json_data, |
| 234 const std::string& json_data, | 233 const FilePath& fake_base_path) |
| 235 const FilePath& fake_base_path) | |
| 236 : fake_base_path_(fake_base_path) { | 234 : fake_base_path_(fake_base_path) { |
| 237 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 235 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 238 JSONStringValueSerializer serializer(json_data); | 236 JSONStringValueSerializer serializer(json_data); |
| 239 FilePath fake_json_path = fake_base_path.AppendASCII("fake.json"); | 237 FilePath fake_json_path = fake_base_path.AppendASCII("fake.json"); |
| 240 testing_prefs_.reset(ExtractExtensionPrefs(&serializer, fake_json_path)); | 238 testing_prefs_.reset(ExtractExtensionPrefs(&serializer, fake_json_path)); |
| 241 } | 239 } |
| 242 | 240 |
| 243 void ExternalTestingExtensionLoader::StartLoading() { | 241 void ExternalTestingLoader::StartLoading() { |
| 244 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 242 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 245 prefs_.reset(testing_prefs_->DeepCopy()); | 243 prefs_.reset(testing_prefs_->DeepCopy()); |
| 246 LoadFinished(); | 244 LoadFinished(); |
| 247 } | 245 } |
| 248 | 246 |
| 249 ExternalTestingExtensionLoader::~ExternalTestingExtensionLoader() {} | 247 ExternalTestingLoader::~ExternalTestingLoader() {} |
| 250 | 248 |
| 251 const FilePath ExternalTestingExtensionLoader::GetBaseCrxFilePath() { | 249 const FilePath ExternalTestingLoader::GetBaseCrxFilePath() { |
| 252 return fake_base_path_; | 250 return fake_base_path_; |
| 253 } | 251 } |
| 252 |
| 253 } // extensions |
| OLD | NEW |