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 <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| 11 #include "base/memory/ptr_util.h" |
11 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
12 #include "base/metrics/histogram_macros.h" | 13 #include "base/metrics/histogram_macros.h" |
13 #include "base/stl_util.h" | |
14 #include "base/strings/sys_string_conversions.h" | 14 #include "base/strings/sys_string_conversions.h" |
15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
16 #include "base/values.h" | 16 #include "base/values.h" |
17 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
18 #include "chrome/browser/plugins/plugin_metadata.h" | 18 #include "chrome/browser/plugins/plugin_metadata.h" |
19 #include "chrome/common/features.h" | 19 #include "chrome/common/features.h" |
20 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
21 #include "chrome/grit/browser_resources.h" | 21 #include "chrome/grit/browser_resources.h" |
22 #include "components/prefs/pref_registry_simple.h" | 22 #include "components/prefs/pref_registry_simple.h" |
23 #include "components/prefs/pref_service.h" | 23 #include "components/prefs/pref_service.h" |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 success = (*mime_type_it)->GetAsString(&mime_type_str); | 88 success = (*mime_type_it)->GetAsString(&mime_type_str); |
89 DCHECK(success); | 89 DCHECK(success); |
90 if (matching_mime_types) { | 90 if (matching_mime_types) { |
91 plugin->AddMatchingMimeType(mime_type_str); | 91 plugin->AddMatchingMimeType(mime_type_str); |
92 } else { | 92 } else { |
93 plugin->AddMimeType(mime_type_str); | 93 plugin->AddMimeType(mime_type_str); |
94 } | 94 } |
95 } | 95 } |
96 } | 96 } |
97 | 97 |
98 PluginMetadata* CreatePluginMetadata( | 98 std::unique_ptr<PluginMetadata> CreatePluginMetadata( |
99 const std::string& identifier, | 99 const std::string& identifier, |
100 const base::DictionaryValue* plugin_dict) { | 100 const base::DictionaryValue* plugin_dict) { |
101 std::string url; | 101 std::string url; |
102 bool success = plugin_dict->GetString("url", &url); | 102 bool success = plugin_dict->GetString("url", &url); |
103 std::string help_url; | 103 std::string help_url; |
104 plugin_dict->GetString("help_url", &help_url); | 104 plugin_dict->GetString("help_url", &help_url); |
105 base::string16 name; | 105 base::string16 name; |
106 success = plugin_dict->GetString("name", &name); | 106 success = plugin_dict->GetString("name", &name); |
107 DCHECK(success); | 107 DCHECK(success); |
108 bool display_url = false; | 108 bool display_url = false; |
109 plugin_dict->GetBoolean("displayurl", &display_url); | 109 plugin_dict->GetBoolean("displayurl", &display_url); |
110 base::string16 group_name_matcher; | 110 base::string16 group_name_matcher; |
111 success = plugin_dict->GetString("group_name_matcher", &group_name_matcher); | 111 success = plugin_dict->GetString("group_name_matcher", &group_name_matcher); |
112 DCHECK(success); | 112 DCHECK(success); |
113 std::string language_str; | 113 std::string language_str; |
114 plugin_dict->GetString("lang", &language_str); | 114 plugin_dict->GetString("lang", &language_str); |
115 | 115 |
116 PluginMetadata* plugin = new PluginMetadata(identifier, | 116 std::unique_ptr<PluginMetadata> plugin = base::MakeUnique<PluginMetadata>( |
117 name, | 117 identifier, name, display_url, GURL(url), GURL(help_url), |
118 display_url, | 118 group_name_matcher, language_str); |
119 GURL(url), | |
120 GURL(help_url), | |
121 group_name_matcher, | |
122 language_str); | |
123 const base::ListValue* versions = NULL; | 119 const base::ListValue* versions = NULL; |
124 if (plugin_dict->GetList("versions", &versions)) { | 120 if (plugin_dict->GetList("versions", &versions)) { |
125 for (base::ListValue::const_iterator it = versions->begin(); | 121 for (base::ListValue::const_iterator it = versions->begin(); |
126 it != versions->end(); ++it) { | 122 it != versions->end(); ++it) { |
127 base::DictionaryValue* version_dict = NULL; | 123 base::DictionaryValue* version_dict = NULL; |
128 if (!(*it)->GetAsDictionary(&version_dict)) { | 124 if (!(*it)->GetAsDictionary(&version_dict)) { |
129 NOTREACHED(); | 125 NOTREACHED(); |
130 continue; | 126 continue; |
131 } | 127 } |
132 std::string version; | 128 std::string version; |
133 success = version_dict->GetString("version", &version); | 129 success = version_dict->GetString("version", &version); |
134 DCHECK(success); | 130 DCHECK(success); |
135 std::string status_str; | 131 std::string status_str; |
136 success = version_dict->GetString("status", &status_str); | 132 success = version_dict->GetString("status", &status_str); |
137 DCHECK(success); | 133 DCHECK(success); |
138 PluginMetadata::SecurityStatus status = | 134 PluginMetadata::SecurityStatus status = |
139 PluginMetadata::SECURITY_STATUS_UP_TO_DATE; | 135 PluginMetadata::SECURITY_STATUS_UP_TO_DATE; |
140 success = PluginMetadata::ParseSecurityStatus(status_str, &status); | 136 success = PluginMetadata::ParseSecurityStatus(status_str, &status); |
141 DCHECK(success); | 137 DCHECK(success); |
142 plugin->AddVersion(base::Version(version), status); | 138 plugin->AddVersion(base::Version(version), status); |
143 } | 139 } |
144 } | 140 } |
145 | 141 |
146 LoadMimeTypes(false, plugin_dict, plugin); | 142 LoadMimeTypes(false, plugin_dict, plugin.get()); |
147 LoadMimeTypes(true, plugin_dict, plugin); | 143 LoadMimeTypes(true, plugin_dict, plugin.get()); |
148 return plugin; | 144 return plugin; |
149 } | 145 } |
150 | 146 |
151 void RecordBuiltInPluginListError(PluginListError error_code) { | 147 void RecordBuiltInPluginListError(PluginListError error_code) { |
152 UMA_HISTOGRAM_ENUMERATION("PluginFinder.BuiltInPluginList.ErrorCode", | 148 UMA_HISTOGRAM_ENUMERATION("PluginFinder.BuiltInPluginList.ErrorCode", |
153 static_cast<int>(error_code), | 149 static_cast<int>(error_code), |
154 static_cast<int>(PluginListError::NUM_VALUES)); | 150 static_cast<int>(PluginListError::NUM_VALUES)); |
155 } | 151 } |
156 | 152 |
157 } // namespace | 153 } // namespace |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 RecordBuiltInPluginListError(PluginListError::SCHEMA_ERROR); | 236 RecordBuiltInPluginListError(PluginListError::SCHEMA_ERROR); |
241 return nullptr; | 237 return nullptr; |
242 } | 238 } |
243 | 239 |
244 DCHECK_EQ(base::JSONReader::JSON_NO_ERROR, error_code); | 240 DCHECK_EQ(base::JSONReader::JSON_NO_ERROR, error_code); |
245 RecordBuiltInPluginListError(PluginListError::PLUGIN_LIST_NO_ERROR); | 241 RecordBuiltInPluginListError(PluginListError::PLUGIN_LIST_NO_ERROR); |
246 return static_cast<base::DictionaryValue*>(value.release()); | 242 return static_cast<base::DictionaryValue*>(value.release()); |
247 } | 243 } |
248 | 244 |
249 PluginFinder::~PluginFinder() { | 245 PluginFinder::~PluginFinder() { |
250 #if BUILDFLAG(ENABLE_PLUGIN_INSTALLATION) | |
251 base::STLDeleteValues(&installers_); | |
252 #endif | |
253 base::STLDeleteValues(&identifier_plugin_); | |
254 } | 246 } |
255 | 247 |
256 #if BUILDFLAG(ENABLE_PLUGIN_INSTALLATION) | 248 #if BUILDFLAG(ENABLE_PLUGIN_INSTALLATION) |
257 bool PluginFinder::FindPlugin( | 249 bool PluginFinder::FindPlugin( |
258 const std::string& mime_type, | 250 const std::string& mime_type, |
259 const std::string& language, | 251 const std::string& language, |
260 PluginInstaller** installer, | 252 PluginInstaller** installer, |
261 std::unique_ptr<PluginMetadata>* plugin_metadata) { | 253 std::unique_ptr<PluginMetadata>* plugin_metadata) { |
262 if (g_browser_process->local_state()->GetBoolean(prefs::kDisablePluginFinder)) | 254 if (g_browser_process->local_state()->GetBoolean(prefs::kDisablePluginFinder)) |
263 return false; | 255 return false; |
264 | 256 |
265 base::AutoLock lock(mutex_); | 257 base::AutoLock lock(mutex_); |
266 PluginMap::const_iterator metadata_it = identifier_plugin_.begin(); | 258 auto metadata_it = identifier_plugin_.begin(); |
267 for (; metadata_it != identifier_plugin_.end(); ++metadata_it) { | 259 for (; metadata_it != identifier_plugin_.end(); ++metadata_it) { |
268 if (language == metadata_it->second->language() && | 260 if (language == metadata_it->second->language() && |
269 metadata_it->second->HasMimeType(mime_type)) { | 261 metadata_it->second->HasMimeType(mime_type)) { |
270 *plugin_metadata = metadata_it->second->Clone(); | 262 *plugin_metadata = metadata_it->second->Clone(); |
271 | 263 |
272 std::map<std::string, PluginInstaller*>::const_iterator installer_it = | 264 auto installer_it = installers_.find(metadata_it->second->identifier()); |
273 installers_.find(metadata_it->second->identifier()); | |
274 DCHECK(installer_it != installers_.end()); | 265 DCHECK(installer_it != installers_.end()); |
275 *installer = installer_it->second; | 266 *installer = installer_it->second.get(); |
276 return true; | 267 return true; |
277 } | 268 } |
278 } | 269 } |
279 return false; | 270 return false; |
280 } | 271 } |
281 | 272 |
282 bool PluginFinder::FindPluginWithIdentifier( | 273 bool PluginFinder::FindPluginWithIdentifier( |
283 const std::string& identifier, | 274 const std::string& identifier, |
284 PluginInstaller** installer, | 275 PluginInstaller** installer, |
285 std::unique_ptr<PluginMetadata>* plugin_metadata) { | 276 std::unique_ptr<PluginMetadata>* plugin_metadata) { |
286 base::AutoLock lock(mutex_); | 277 base::AutoLock lock(mutex_); |
287 PluginMap::const_iterator metadata_it = identifier_plugin_.find(identifier); | 278 auto metadata_it = identifier_plugin_.find(identifier); |
288 if (metadata_it == identifier_plugin_.end()) | 279 if (metadata_it == identifier_plugin_.end()) |
289 return false; | 280 return false; |
290 *plugin_metadata = metadata_it->second->Clone(); | 281 *plugin_metadata = metadata_it->second->Clone(); |
291 | 282 |
292 if (installer) { | 283 if (installer) { |
293 std::map<std::string, PluginInstaller*>::const_iterator installer_it = | 284 auto installer_it = installers_.find(identifier); |
294 installers_.find(identifier); | |
295 if (installer_it == installers_.end()) | 285 if (installer_it == installers_.end()) |
296 return false; | 286 return false; |
297 *installer = installer_it->second; | 287 *installer = installer_it->second.get(); |
298 } | 288 } |
299 return true; | 289 return true; |
300 } | 290 } |
301 #endif | 291 #endif |
302 | 292 |
303 void PluginFinder::ReinitializePlugins( | 293 void PluginFinder::ReinitializePlugins( |
304 const base::DictionaryValue* plugin_list) { | 294 const base::DictionaryValue* plugin_list) { |
305 base::AutoLock lock(mutex_); | 295 base::AutoLock lock(mutex_); |
306 int version = 0; // If no version is defined, we default to 0. | 296 int version = 0; // If no version is defined, we default to 0. |
307 const char kVersionKey[] = "x-version"; | 297 const char kVersionKey[] = "x-version"; |
308 plugin_list->GetInteger(kVersionKey, &version); | 298 plugin_list->GetInteger(kVersionKey, &version); |
309 if (version <= version_) | 299 if (version <= version_) |
310 return; | 300 return; |
311 | 301 |
312 version_ = version; | 302 version_ = version; |
313 | 303 |
314 base::STLDeleteValues(&identifier_plugin_); | 304 identifier_plugin_.clear(); |
315 | 305 |
316 for (base::DictionaryValue::Iterator plugin_it(*plugin_list); | 306 for (base::DictionaryValue::Iterator plugin_it(*plugin_list); |
317 !plugin_it.IsAtEnd(); plugin_it.Advance()) { | 307 !plugin_it.IsAtEnd(); plugin_it.Advance()) { |
318 const base::DictionaryValue* plugin = NULL; | 308 const base::DictionaryValue* plugin = NULL; |
319 const std::string& identifier = plugin_it.key(); | 309 const std::string& identifier = plugin_it.key(); |
320 if (plugin_list->GetDictionaryWithoutPathExpansion(identifier, &plugin)) { | 310 if (plugin_list->GetDictionaryWithoutPathExpansion(identifier, &plugin)) { |
321 DCHECK(!identifier_plugin_[identifier]); | 311 DCHECK(!identifier_plugin_[identifier]); |
322 identifier_plugin_[identifier] = CreatePluginMetadata(identifier, plugin); | 312 identifier_plugin_[identifier] = CreatePluginMetadata(identifier, plugin); |
323 | 313 |
324 #if BUILDFLAG(ENABLE_PLUGIN_INSTALLATION) | 314 #if BUILDFLAG(ENABLE_PLUGIN_INSTALLATION) |
325 if (installers_.find(identifier) == installers_.end()) | 315 if (installers_.find(identifier) == installers_.end()) |
326 installers_[identifier] = new PluginInstaller(); | 316 installers_[identifier] = base::MakeUnique<PluginInstaller>(); |
327 #endif | 317 #endif |
328 } | 318 } |
329 } | 319 } |
330 } | 320 } |
331 | 321 |
332 std::unique_ptr<PluginMetadata> PluginFinder::GetPluginMetadata( | 322 std::unique_ptr<PluginMetadata> PluginFinder::GetPluginMetadata( |
333 const content::WebPluginInfo& plugin) { | 323 const content::WebPluginInfo& plugin) { |
334 base::AutoLock lock(mutex_); | 324 base::AutoLock lock(mutex_); |
335 for (PluginMap::const_iterator it = identifier_plugin_.begin(); | 325 for (const auto& plugin_pair : identifier_plugin_) { |
336 it != identifier_plugin_.end(); ++it) { | 326 if (!plugin_pair.second->MatchesPlugin(plugin)) |
337 if (!it->second->MatchesPlugin(plugin)) | |
338 continue; | 327 continue; |
339 | 328 |
340 return it->second->Clone(); | 329 return plugin_pair.second->Clone(); |
341 } | 330 } |
342 | 331 |
343 // The plugin metadata was not found, create a dummy one holding | 332 // The plugin metadata was not found, create a dummy one holding |
344 // the name, identifier and group name only. | 333 // the name, identifier and group name only. |
345 std::string identifier = GetIdentifier(plugin); | 334 std::string identifier = GetIdentifier(plugin); |
346 PluginMetadata* metadata = new PluginMetadata(identifier, | 335 std::unique_ptr<PluginMetadata> metadata = base::MakeUnique<PluginMetadata>( |
347 GetGroupName(plugin), | 336 identifier, GetGroupName(plugin), false, GURL(), GURL(), plugin.name, |
348 false, | 337 std::string()); |
349 GURL(), | |
350 GURL(), | |
351 plugin.name, | |
352 std::string()); | |
353 for (size_t i = 0; i < plugin.mime_types.size(); ++i) | 338 for (size_t i = 0; i < plugin.mime_types.size(); ++i) |
354 metadata->AddMatchingMimeType(plugin.mime_types[i].mime_type); | 339 metadata->AddMatchingMimeType(plugin.mime_types[i].mime_type); |
355 | 340 |
356 DCHECK(metadata->MatchesPlugin(plugin)); | 341 DCHECK(metadata->MatchesPlugin(plugin)); |
357 if (identifier_plugin_.find(identifier) != identifier_plugin_.end()) | 342 if (identifier_plugin_.find(identifier) != identifier_plugin_.end()) |
358 identifier = GetLongIdentifier(plugin); | 343 identifier = GetLongIdentifier(plugin); |
359 | 344 |
360 DCHECK(identifier_plugin_.find(identifier) == identifier_plugin_.end()); | 345 DCHECK(identifier_plugin_.find(identifier) == identifier_plugin_.end()); |
361 identifier_plugin_[identifier] = metadata; | 346 identifier_plugin_[identifier] = std::move(metadata); |
362 return metadata->Clone(); | 347 return identifier_plugin_[identifier]->Clone(); |
363 } | 348 } |
OLD | NEW |