Chromium Code Reviews| 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/plugins/plugin_finder.h" | 5 #include "chrome/browser/plugins/plugin_finder.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 140 registry->RegisterBooleanPref(prefs::kDisablePluginFinder, false); | 140 registry->RegisterBooleanPref(prefs::kDisablePluginFinder, false); |
| 141 } | 141 } |
| 142 | 142 |
| 143 // static | 143 // static |
| 144 PluginFinder* PluginFinder::GetInstance() { | 144 PluginFinder* PluginFinder::GetInstance() { |
| 145 // PluginFinder::GetInstance() is the only method that's allowed to call | 145 // PluginFinder::GetInstance() is the only method that's allowed to call |
| 146 // Singleton<PluginFinder>::get(). | 146 // Singleton<PluginFinder>::get(). |
| 147 return Singleton<PluginFinder>::get(); | 147 return Singleton<PluginFinder>::get(); |
| 148 } | 148 } |
| 149 | 149 |
| 150 PluginFinder::PluginFinder() { | 150 PluginFinder::PluginFinder() : version_(-1) { |
| 151 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 151 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 152 } | 152 } |
| 153 | 153 |
| 154 void PluginFinder::Init() { | 154 void PluginFinder::Init() { |
| 155 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 155 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 156 plugin_list_.reset(ComputePluginList()); | 156 // Load the built-in plug-in list first. If we have a newer version stored |
| 157 DCHECK(plugin_list_.get()); | 157 // locally or download one, we will replace this one with it. |
| 158 | 158 scoped_ptr<DictionaryValue> plugin_list(LoadBuiltInPluginList()); |
|
Chris Evans
2013/02/08 19:25:23
Would it be possible to DCHECK(plugin_list.get())
Bernhard Bauer
2013/02/11 09:58:19
Done (although we have unit tests for that).
| |
| 159 InitInternal(); | 159 if (plugin_list) |
| 160 ReinitializePlugins(plugin_list.get()); | |
| 160 } | 161 } |
| 161 | 162 |
| 162 // static | 163 // static |
| 163 DictionaryValue* PluginFinder::ComputePluginList() { | 164 DictionaryValue* PluginFinder::LoadBuiltInPluginList() { |
| 164 #if defined(ENABLE_PLUGIN_INSTALLATION) | |
| 165 const base::DictionaryValue* metadata = | |
| 166 g_browser_process->local_state()->GetDictionary(prefs::kPluginsMetadata); | |
| 167 if (!metadata->empty()) | |
| 168 return metadata->DeepCopy(); | |
| 169 #endif | |
| 170 base::DictionaryValue* result = LoadPluginList(); | |
| 171 if (result) | |
| 172 return result; | |
| 173 return new base::DictionaryValue(); | |
| 174 } | |
| 175 | |
| 176 // static | |
| 177 DictionaryValue* PluginFinder::LoadPluginList() { | |
| 178 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) | |
| 179 base::StringPiece json_resource( | 165 base::StringPiece json_resource( |
| 180 ResourceBundle::GetSharedInstance().GetRawDataResource( | 166 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 181 IDR_PLUGIN_DB_JSON)); | 167 IDR_PLUGIN_DB_JSON)); |
| 182 std::string error_str; | 168 std::string error_str; |
| 183 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError( | 169 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError( |
| 184 json_resource, | 170 json_resource, |
| 185 base::JSON_PARSE_RFC, | 171 base::JSON_PARSE_RFC, |
| 186 NULL, | 172 NULL, |
| 187 &error_str)); | 173 &error_str)); |
| 188 if (!value.get()) { | 174 if (!value.get()) { |
| 189 DLOG(ERROR) << error_str; | 175 DLOG(ERROR) << error_str; |
| 190 return NULL; | 176 return NULL; |
| 191 } | 177 } |
| 192 if (value->GetType() != base::Value::TYPE_DICTIONARY) | 178 if (value->GetType() != base::Value::TYPE_DICTIONARY) |
| 193 return NULL; | 179 return NULL; |
| 194 return static_cast<base::DictionaryValue*>(value.release()); | 180 return static_cast<base::DictionaryValue*>(value.release()); |
| 195 #else | |
| 196 return new DictionaryValue(); | |
| 197 #endif | |
| 198 } | 181 } |
| 199 | 182 |
| 200 PluginFinder::~PluginFinder() { | 183 PluginFinder::~PluginFinder() { |
| 201 #if defined(ENABLE_PLUGIN_INSTALLATION) | 184 #if defined(ENABLE_PLUGIN_INSTALLATION) |
| 202 STLDeleteValues(&installers_); | 185 STLDeleteValues(&installers_); |
| 203 #endif | 186 #endif |
| 204 STLDeleteValues(&identifier_plugin_); | 187 STLDeleteValues(&identifier_plugin_); |
| 205 } | 188 } |
| 206 | 189 |
| 207 #if defined(ENABLE_PLUGIN_INSTALLATION) | 190 #if defined(ENABLE_PLUGIN_INSTALLATION) |
| 208 bool PluginFinder::FindPlugin( | 191 bool PluginFinder::FindPlugin( |
| 209 const std::string& mime_type, | 192 const std::string& mime_type, |
| 210 const std::string& language, | 193 const std::string& language, |
| 211 PluginInstaller** installer, | 194 PluginInstaller** installer, |
| 212 scoped_ptr<PluginMetadata>* plugin_metadata) { | 195 scoped_ptr<PluginMetadata>* plugin_metadata) { |
| 213 base::AutoLock lock(mutex_); | |
| 214 if (g_browser_process->local_state()->GetBoolean(prefs::kDisablePluginFinder)) | 196 if (g_browser_process->local_state()->GetBoolean(prefs::kDisablePluginFinder)) |
| 215 return false; | 197 return false; |
| 216 | 198 |
| 199 base::AutoLock lock(mutex_); | |
| 217 PluginMap::const_iterator metadata_it = identifier_plugin_.begin(); | 200 PluginMap::const_iterator metadata_it = identifier_plugin_.begin(); |
| 218 for (; metadata_it != identifier_plugin_.end(); ++metadata_it) { | 201 for (; metadata_it != identifier_plugin_.end(); ++metadata_it) { |
| 219 if (language == metadata_it->second->language() && | 202 if (language == metadata_it->second->language() && |
| 220 metadata_it->second->HasMimeType(mime_type)) { | 203 metadata_it->second->HasMimeType(mime_type)) { |
| 221 *plugin_metadata = metadata_it->second->Clone(); | 204 *plugin_metadata = metadata_it->second->Clone(); |
| 222 | 205 |
| 223 std::map<std::string, PluginInstaller*>::const_iterator installer_it = | 206 std::map<std::string, PluginInstaller*>::const_iterator installer_it = |
| 224 installers_.find(metadata_it->second->identifier()); | 207 installers_.find(metadata_it->second->identifier()); |
| 225 DCHECK(installer_it != installers_.end()); | 208 DCHECK(installer_it != installers_.end()); |
| 226 *installer = installer_it->second; | 209 *installer = installer_it->second; |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 242 | 225 |
| 243 if (installer) { | 226 if (installer) { |
| 244 std::map<std::string, PluginInstaller*>::const_iterator installer_it = | 227 std::map<std::string, PluginInstaller*>::const_iterator installer_it = |
| 245 installers_.find(identifier); | 228 installers_.find(identifier); |
| 246 if (installer_it == installers_.end()) | 229 if (installer_it == installers_.end()) |
| 247 return false; | 230 return false; |
| 248 *installer = installer_it->second; | 231 *installer = installer_it->second; |
| 249 } | 232 } |
| 250 return true; | 233 return true; |
| 251 } | 234 } |
| 235 #endif | |
| 252 | 236 |
| 253 void PluginFinder::ReinitializePlugins( | 237 void PluginFinder::ReinitializePlugins( |
| 254 const base::DictionaryValue& json_metadata) { | 238 const base::DictionaryValue* plugin_list) { |
| 255 base::AutoLock lock(mutex_); | 239 base::AutoLock lock(mutex_); |
| 240 int version = 0; // If no version is defined, we default to 0. | |
| 241 const char kVersionKey[] = "x-version"; | |
| 242 plugin_list->GetInteger(kVersionKey, &version); | |
| 243 if (version <= version_) | |
| 244 return; | |
| 245 | |
| 246 version_ = version; | |
| 247 | |
| 256 STLDeleteValues(&identifier_plugin_); | 248 STLDeleteValues(&identifier_plugin_); |
| 257 identifier_plugin_.clear(); | 249 identifier_plugin_.clear(); |
| 258 | 250 |
| 259 plugin_list_.reset(json_metadata.DeepCopy()); | 251 for (DictionaryValue::Iterator plugin_it(*plugin_list); |
| 260 InitInternal(); | 252 plugin_it.HasNext(); plugin_it.Advance()) { |
| 253 const DictionaryValue* plugin = NULL; | |
| 254 const std::string& identifier = plugin_it.key(); | |
| 255 if (plugin_list->GetDictionaryWithoutPathExpansion(identifier, &plugin)) { | |
| 256 DCHECK(!identifier_plugin_[identifier]); | |
| 257 identifier_plugin_[identifier] = CreatePluginMetadata(identifier, plugin); | |
| 258 | |
| 259 #if defined(ENABLE_PLUGIN_INSTALLATION) | |
| 260 if (installers_.find(identifier) == installers_.end()) | |
| 261 installers_[identifier] = new PluginInstaller(); | |
| 262 #endif | |
| 263 } | |
| 264 } | |
| 261 } | 265 } |
| 262 #endif | |
| 263 | 266 |
| 264 string16 PluginFinder::FindPluginNameWithIdentifier( | 267 string16 PluginFinder::FindPluginNameWithIdentifier( |
| 265 const std::string& identifier) { | 268 const std::string& identifier) { |
| 266 base::AutoLock lock(mutex_); | 269 base::AutoLock lock(mutex_); |
| 267 PluginMap::const_iterator it = identifier_plugin_.find(identifier); | 270 PluginMap::const_iterator it = identifier_plugin_.find(identifier); |
| 268 string16 name; | 271 string16 name; |
| 269 if (it != identifier_plugin_.end()) | 272 if (it != identifier_plugin_.end()) |
| 270 name = it->second->name(); | 273 name = it->second->name(); |
| 271 | 274 |
| 272 return name.empty() ? UTF8ToUTF16(identifier) : name; | 275 return name.empty() ? UTF8ToUTF16(identifier) : name; |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 295 metadata->AddMatchingMimeType(plugin.mime_types[i].mime_type); | 298 metadata->AddMatchingMimeType(plugin.mime_types[i].mime_type); |
| 296 | 299 |
| 297 DCHECK(metadata->MatchesPlugin(plugin)); | 300 DCHECK(metadata->MatchesPlugin(plugin)); |
| 298 if (identifier_plugin_.find(identifier) != identifier_plugin_.end()) | 301 if (identifier_plugin_.find(identifier) != identifier_plugin_.end()) |
| 299 identifier = GetLongIdentifier(plugin); | 302 identifier = GetLongIdentifier(plugin); |
| 300 | 303 |
| 301 DCHECK(identifier_plugin_.find(identifier) == identifier_plugin_.end()); | 304 DCHECK(identifier_plugin_.find(identifier) == identifier_plugin_.end()); |
| 302 identifier_plugin_[identifier] = metadata; | 305 identifier_plugin_[identifier] = metadata; |
| 303 return metadata->Clone(); | 306 return metadata->Clone(); |
| 304 } | 307 } |
| 305 | |
| 306 void PluginFinder::InitInternal() { | |
| 307 for (DictionaryValue::Iterator plugin_it(*plugin_list_); | |
| 308 plugin_it.HasNext(); plugin_it.Advance()) { | |
| 309 DictionaryValue* plugin = NULL; | |
| 310 const std::string& identifier = plugin_it.key(); | |
| 311 if (plugin_list_->GetDictionaryWithoutPathExpansion(identifier, &plugin)) { | |
| 312 DCHECK(!identifier_plugin_[identifier]); | |
| 313 identifier_plugin_[identifier] = CreatePluginMetadata(identifier, plugin); | |
| 314 | |
| 315 #if defined(ENABLE_PLUGIN_INSTALLATION) | |
| 316 if (installers_.find(identifier) == installers_.end()) | |
| 317 installers_[identifier] = new PluginInstaller(); | |
| 318 #endif | |
| 319 } | |
| 320 } | |
| 321 } | |
| OLD | NEW |