1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/plugins/plugin_finder.h"
#include "base/bind.h"
#include "base/json/json_reader.h"
#include "base/message_loop/message_loop.h"
#include "base/prefs/pref_registry_simple.h"
#include "base/prefs/pref_service.h"
#include "base/stl_util.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/plugins/plugin_metadata.h"
#include "chrome/common/pref_names.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/plugin_service.h"
#include "grit/browser_resources.h"
#include "ui/base/resource/resource_bundle.h"
#include "url/gurl.h"
#if defined(ENABLE_PLUGIN_INSTALLATION)
#include "chrome/browser/plugins/plugin_installer.h"
#endif
using base::DictionaryValue;
using content::PluginService;
namespace {
typedef std::map<std::string, PluginMetadata*> PluginMap;
// Gets the full path of the plug-in file as the identifier.
std::string GetLongIdentifier(const content::WebPluginInfo& plugin) {
return plugin.path.AsUTF8Unsafe();
}
// Gets the base name of the file path as the identifier.
std::string GetIdentifier(const content::WebPluginInfo& plugin) {
return plugin.path.BaseName().AsUTF8Unsafe();
}
// Gets the plug-in group name as the plug-in name if it is not empty or
// the filename without extension if the name is empty.
static base::string16 GetGroupName(const content::WebPluginInfo& plugin) {
if (!plugin.name.empty())
return plugin.name;
return plugin.path.BaseName().RemoveExtension().AsUTF16Unsafe();
}
void LoadMimeTypes(bool matching_mime_types,
const base::DictionaryValue* plugin_dict,
PluginMetadata* plugin) {
const base::ListValue* mime_types = NULL;
std::string list_key =
matching_mime_types ? "matching_mime_types" : "mime_types";
if (!plugin_dict->GetList(list_key, &mime_types))
return;
bool success = false;
for (base::ListValue::const_iterator mime_type_it = mime_types->begin();
mime_type_it != mime_types->end(); ++mime_type_it) {
std::string mime_type_str;
success = (*mime_type_it)->GetAsString(&mime_type_str);
DCHECK(success);
if (matching_mime_types) {
plugin->AddMatchingMimeType(mime_type_str);
} else {
plugin->AddMimeType(mime_type_str);
}
}
}
PluginMetadata* CreatePluginMetadata(
const std::string& identifier,
const base::DictionaryValue* plugin_dict) {
std::string url;
bool success = plugin_dict->GetString("url", &url);
std::string help_url;
plugin_dict->GetString("help_url", &help_url);
base::string16 name;
success = plugin_dict->GetString("name", &name);
DCHECK(success);
bool display_url = false;
plugin_dict->GetBoolean("displayurl", &display_url);
base::string16 group_name_matcher;
success = plugin_dict->GetString("group_name_matcher", &group_name_matcher);
DCHECK(success);
std::string language_str;
plugin_dict->GetString("lang", &language_str);
PluginMetadata* plugin = new PluginMetadata(identifier,
name,
display_url,
GURL(url),
GURL(help_url),
group_name_matcher,
language_str);
const base::ListValue* versions = NULL;
if (plugin_dict->GetList("versions", &versions)) {
for (base::ListValue::const_iterator it = versions->begin();
it != versions->end(); ++it) {
base::DictionaryValue* version_dict = NULL;
if (!(*it)->GetAsDictionary(&version_dict)) {
NOTREACHED();
continue;
}
std::string version;
success = version_dict->GetString("version", &version);
DCHECK(success);
std::string status_str;
success = version_dict->GetString("status", &status_str);
DCHECK(success);
PluginMetadata::SecurityStatus status =
PluginMetadata::SECURITY_STATUS_UP_TO_DATE;
success = PluginMetadata::ParseSecurityStatus(status_str, &status);
DCHECK(success);
plugin->AddVersion(Version(version), status);
}
}
LoadMimeTypes(false, plugin_dict, plugin);
LoadMimeTypes(true, plugin_dict, plugin);
return plugin;
}
} // namespace
// static
void PluginFinder::RegisterPrefs(PrefRegistrySimple* registry) {
registry->RegisterBooleanPref(prefs::kDisablePluginFinder, false);
}
// static
PluginFinder* PluginFinder::GetInstance() {
// PluginFinder::GetInstance() is the only method that's allowed to call
// Singleton<PluginFinder>::get().
return Singleton<PluginFinder>::get();
}
PluginFinder::PluginFinder() : version_(-1) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
}
void PluginFinder::Init() {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
// Load the built-in plug-in list first. If we have a newer version stored
// locally or download one, we will replace this one with it.
scoped_ptr<base::DictionaryValue> plugin_list(LoadBuiltInPluginList());
DCHECK(plugin_list);
ReinitializePlugins(plugin_list.get());
}
// static
base::DictionaryValue* PluginFinder::LoadBuiltInPluginList() {
base::StringPiece json_resource(
ResourceBundle::GetSharedInstance().GetRawDataResource(
IDR_PLUGIN_DB_JSON));
std::string error_str;
scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError(
json_resource,
base::JSON_PARSE_RFC,
NULL,
&error_str));
if (!value.get()) {
DLOG(ERROR) << error_str;
return NULL;
}
if (value->GetType() != base::Value::TYPE_DICTIONARY)
return NULL;
return static_cast<base::DictionaryValue*>(value.release());
}
PluginFinder::~PluginFinder() {
#if defined(ENABLE_PLUGIN_INSTALLATION)
STLDeleteValues(&installers_);
#endif
STLDeleteValues(&identifier_plugin_);
}
#if defined(ENABLE_PLUGIN_INSTALLATION)
bool PluginFinder::FindPlugin(
const std::string& mime_type,
const std::string& language,
PluginInstaller** installer,
scoped_ptr<PluginMetadata>* plugin_metadata) {
if (g_browser_process->local_state()->GetBoolean(prefs::kDisablePluginFinder))
return false;
base::AutoLock lock(mutex_);
PluginMap::const_iterator metadata_it = identifier_plugin_.begin();
for (; metadata_it != identifier_plugin_.end(); ++metadata_it) {
if (language == metadata_it->second->language() &&
metadata_it->second->HasMimeType(mime_type)) {
*plugin_metadata = metadata_it->second->Clone();
std::map<std::string, PluginInstaller*>::const_iterator installer_it =
installers_.find(metadata_it->second->identifier());
DCHECK(installer_it != installers_.end());
*installer = installer_it->second;
return true;
}
}
return false;
}
bool PluginFinder::FindPluginWithIdentifier(
const std::string& identifier,
PluginInstaller** installer,
scoped_ptr<PluginMetadata>* plugin_metadata) {
base::AutoLock lock(mutex_);
PluginMap::const_iterator metadata_it = identifier_plugin_.find(identifier);
if (metadata_it == identifier_plugin_.end())
return false;
*plugin_metadata = metadata_it->second->Clone();
if (installer) {
std::map<std::string, PluginInstaller*>::const_iterator installer_it =
installers_.find(identifier);
if (installer_it == installers_.end())
return false;
*installer = installer_it->second;
}
return true;
}
#endif
void PluginFinder::ReinitializePlugins(
const base::DictionaryValue* plugin_list) {
base::AutoLock lock(mutex_);
int version = 0; // If no version is defined, we default to 0.
const char kVersionKey[] = "x-version";
plugin_list->GetInteger(kVersionKey, &version);
if (version <= version_)
return;
version_ = version;
STLDeleteValues(&identifier_plugin_);
for (base::DictionaryValue::Iterator plugin_it(*plugin_list);
!plugin_it.IsAtEnd(); plugin_it.Advance()) {
const base::DictionaryValue* plugin = NULL;
const std::string& identifier = plugin_it.key();
if (plugin_list->GetDictionaryWithoutPathExpansion(identifier, &plugin)) {
DCHECK(!identifier_plugin_[identifier]);
identifier_plugin_[identifier] = CreatePluginMetadata(identifier, plugin);
#if defined(ENABLE_PLUGIN_INSTALLATION)
if (installers_.find(identifier) == installers_.end())
installers_[identifier] = new PluginInstaller();
#endif
}
}
}
base::string16 PluginFinder::FindPluginNameWithIdentifier(
const std::string& identifier) {
base::AutoLock lock(mutex_);
PluginMap::const_iterator it = identifier_plugin_.find(identifier);
base::string16 name;
if (it != identifier_plugin_.end())
name = it->second->name();
return name.empty() ? base::UTF8ToUTF16(identifier) : name;
}
scoped_ptr<PluginMetadata> PluginFinder::GetPluginMetadata(
const content::WebPluginInfo& plugin) {
base::AutoLock lock(mutex_);
for (PluginMap::const_iterator it = identifier_plugin_.begin();
it != identifier_plugin_.end(); ++it) {
if (!it->second->MatchesPlugin(plugin))
continue;
return it->second->Clone();
}
// The plug-in metadata was not found, create a dummy one holding
// the name, identifier and group name only.
std::string identifier = GetIdentifier(plugin);
PluginMetadata* metadata = new PluginMetadata(identifier,
GetGroupName(plugin),
false,
GURL(),
GURL(),
plugin.name,
std::string());
for (size_t i = 0; i < plugin.mime_types.size(); ++i)
metadata->AddMatchingMimeType(plugin.mime_types[i].mime_type);
DCHECK(metadata->MatchesPlugin(plugin));
if (identifier_plugin_.find(identifier) != identifier_plugin_.end())
identifier = GetLongIdentifier(plugin);
DCHECK(identifier_plugin_.find(identifier) == identifier_plugin_.end());
identifier_plugin_[identifier] = metadata;
return metadata->Clone();
}
|