OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/plugin_updater.h" |
| 6 |
| 7 #include <vector> |
| 8 #include <string> |
| 9 |
| 10 #include "base/path_service.h" |
| 11 #include "base/scoped_ptr.h" |
| 12 #include "base/string_util.h" |
| 13 #include "base/values.h" |
| 14 #include "base/version.h" |
| 15 #include "base/utf_string_conversions.h" |
| 16 #include "chrome/browser/pref_service.h" |
| 17 #include "chrome/common/chrome_paths.h" |
| 18 #include "chrome/browser/profile.h" |
| 19 #include "chrome/common/pref_names.h" |
| 20 #include "webkit/glue/plugins/plugin_list.h" |
| 21 #include "webkit/glue/plugins/webplugininfo.h" |
| 22 |
| 23 /*private*/ |
| 24 PluginGroup::PluginGroup(const string16& group_name, |
| 25 const string16& name_matcher, |
| 26 const std::string& version_range_low, |
| 27 const std::string& version_range_high, |
| 28 const std::string& min_version, |
| 29 const std::string& update_url) { |
| 30 group_name_ = group_name; |
| 31 name_matcher_ = name_matcher; |
| 32 version_range_low_str_ = version_range_low; |
| 33 if (!version_range_low.empty()) { |
| 34 version_range_low_.reset( |
| 35 Version::GetVersionFromString(version_range_low)); |
| 36 } |
| 37 version_range_high_str_ = version_range_high; |
| 38 if (!version_range_high.empty()) { |
| 39 version_range_high_.reset( |
| 40 Version::GetVersionFromString(version_range_high)); |
| 41 } |
| 42 min_version_str_ = min_version; |
| 43 if (!min_version.empty()) { |
| 44 min_version_.reset(Version::GetVersionFromString(min_version)); |
| 45 } |
| 46 update_url_ = update_url; |
| 47 enabled_ = false; |
| 48 max_version_.reset(Version::GetVersionFromString("0")); |
| 49 } |
| 50 |
| 51 PluginGroup* PluginGroup::FromPluginGroupDefinition( |
| 52 const PluginGroupDefinition& definition) { |
| 53 return new PluginGroup(ASCIIToUTF16(definition.name), |
| 54 ASCIIToUTF16(definition.name_matcher), |
| 55 definition.version_matcher_low, |
| 56 definition.version_matcher_high, |
| 57 definition.min_version, |
| 58 definition.update_url); |
| 59 } |
| 60 |
| 61 PluginGroup* PluginGroup::FromWebPluginInfo(const WebPluginInfo& wpi) { |
| 62 // Create a matcher from the name of this plugin. |
| 63 return new PluginGroup(WideToUTF16Hack(wpi.name), WideToUTF16Hack(wpi.name), |
| 64 "", "", "", ""); |
| 65 } |
| 66 |
| 67 PluginGroup* PluginGroup::Copy() { |
| 68 return new PluginGroup(group_name_, name_matcher_, version_range_low_str_, |
| 69 version_range_high_str_, min_version_str_, |
| 70 update_url_); |
| 71 } |
| 72 |
| 73 const string16 PluginGroup::GetGroupName() const { |
| 74 return group_name_; |
| 75 } |
| 76 |
| 77 bool PluginGroup::Match(const WebPluginInfo& plugin) const { |
| 78 if (name_matcher_.empty()) { |
| 79 return false; |
| 80 } |
| 81 |
| 82 // Look for the name matcher anywhere in the plugin name. |
| 83 if (WideToUTF16Hack(plugin.name).find(name_matcher_) == string16::npos) { |
| 84 return false; |
| 85 } |
| 86 |
| 87 if (version_range_low_.get() == NULL || |
| 88 version_range_high_.get() == NULL) { |
| 89 return true; |
| 90 } |
| 91 |
| 92 // There's a version range, we must be in it. |
| 93 scoped_ptr<Version> plugin_version( |
| 94 Version::GetVersionFromString(plugin.version)); |
| 95 if (plugin_version.get() == NULL) { |
| 96 // No version could be extracted, assume we don't match the range. |
| 97 return false; |
| 98 } |
| 99 |
| 100 // We match if we are in the range: [low, high) |
| 101 return (version_range_low_->CompareTo(*plugin_version) <= 0 && |
| 102 version_range_high_->CompareTo(*plugin_version) > 0); |
| 103 } |
| 104 |
| 105 void PluginGroup::AddPlugin(const WebPluginInfo& plugin, int position) { |
| 106 web_plugin_infos_.push_back(plugin); |
| 107 // The position of this plugin relative to the global list of plugins. |
| 108 web_plugin_positions_.push_back(position); |
| 109 description_ = WideToUTF16Hack(plugin.desc); |
| 110 |
| 111 // A group is enabled if any of the files are enabled. |
| 112 if (plugin.enabled) { |
| 113 enabled_ = true; |
| 114 } |
| 115 |
| 116 // update max_version_. Remove spaces and ')' from the version string, |
| 117 // Replace any instances of 'r', ',' or '(' with a dot. |
| 118 std::wstring version = plugin.version; |
| 119 RemoveChars(version, L") ", &version); |
| 120 std::replace(version.begin(), version.end(), 'r', '.'); |
| 121 std::replace(version.begin(), version.end(), ',', '.'); |
| 122 std::replace(version.begin(), version.end(), '(', '.'); |
| 123 |
| 124 scoped_ptr<Version> plugin_version( |
| 125 Version::GetVersionFromString(version)); |
| 126 if (plugin_version.get() != NULL) { |
| 127 if (plugin_version->CompareTo(*(max_version_)) > 0) { |
| 128 max_version_.reset(plugin_version.release()); |
| 129 } |
| 130 } |
| 131 } |
| 132 |
| 133 DictionaryValue* PluginGroup::GetSummary() const { |
| 134 DictionaryValue* result = new DictionaryValue(); |
| 135 result->SetStringFromUTF16(L"name", group_name_); |
| 136 result->SetBoolean(L"enabled", enabled_); |
| 137 return result; |
| 138 } |
| 139 |
| 140 DictionaryValue* PluginGroup::GetData() const { |
| 141 DictionaryValue* result = new DictionaryValue(); |
| 142 result->SetStringFromUTF16(L"name", group_name_); |
| 143 result->SetStringFromUTF16(L"description", description_); |
| 144 result->SetString(L"version", max_version_->GetString()); |
| 145 result->SetString(L"update_url", update_url_); |
| 146 result->SetBoolean(L"critical", IsVulnerable()); |
| 147 result->SetBoolean(L"enabled", enabled_); |
| 148 |
| 149 ListValue* plugin_files = new ListValue(); |
| 150 for (size_t i = 0; i < web_plugin_infos_.size(); ++i) { |
| 151 const WebPluginInfo& web_plugin = web_plugin_infos_[i]; |
| 152 int priority = web_plugin_positions_[i]; |
| 153 DictionaryValue* plugin_file = new DictionaryValue(); |
| 154 plugin_file->SetString(L"name", web_plugin.name); |
| 155 plugin_file->SetString(L"description", web_plugin.desc); |
| 156 plugin_file->SetString(L"path", web_plugin.path.value()); |
| 157 plugin_file->SetString(L"version", web_plugin.version); |
| 158 plugin_file->SetBoolean(L"enabled", web_plugin.enabled); |
| 159 plugin_file->SetInteger(L"priority", priority); |
| 160 |
| 161 ListValue* mime_types = new ListValue(); |
| 162 for (std::vector<WebPluginMimeType>::const_iterator type_it = |
| 163 web_plugin.mime_types.begin(); |
| 164 type_it != web_plugin.mime_types.end(); |
| 165 ++type_it) { |
| 166 DictionaryValue* mime_type = new DictionaryValue(); |
| 167 mime_type->SetString(L"mimeType", type_it->mime_type); |
| 168 mime_type->SetString(L"description", type_it->description); |
| 169 |
| 170 ListValue* file_extensions = new ListValue(); |
| 171 for (std::vector<std::string>::const_iterator ext_it = |
| 172 type_it->file_extensions.begin(); |
| 173 ext_it != type_it->file_extensions.end(); |
| 174 ++ext_it) { |
| 175 file_extensions->Append(new StringValue(*ext_it)); |
| 176 } |
| 177 mime_type->Set(L"fileExtensions", file_extensions); |
| 178 |
| 179 mime_types->Append(mime_type); |
| 180 } |
| 181 plugin_file->Set(L"mimeTypes", mime_types); |
| 182 |
| 183 plugin_files->Append(plugin_file); |
| 184 } |
| 185 result->Set(L"plugin_files", plugin_files); |
| 186 |
| 187 return result; |
| 188 } |
| 189 |
| 190 // Returns true if the latest version of this plugin group is vulnerable. |
| 191 bool PluginGroup::IsVulnerable() const { |
| 192 if (min_version_.get() == NULL || max_version_->GetString() == "0") { |
| 193 return false; |
| 194 } |
| 195 return max_version_->CompareTo(*min_version_) < 0; |
| 196 } |
| 197 |
| 198 void PluginGroup::Enable(bool enable) { |
| 199 for (std::vector<WebPluginInfo>::const_iterator it = |
| 200 web_plugin_infos_.begin(); |
| 201 it != web_plugin_infos_.end(); ++it) { |
| 202 if (enable) { |
| 203 NPAPI::PluginList::Singleton()->EnablePlugin( |
| 204 FilePath(it->path)); |
| 205 } else { |
| 206 NPAPI::PluginList::Singleton()->DisablePlugin( |
| 207 FilePath(it->path)); |
| 208 } |
| 209 } |
| 210 } |
| 211 |
| 212 #if defined(OS_MACOSX) |
| 213 // Plugin Groups for Mac. |
| 214 // Plugins are listed here as soon as vulnerabilities and solutions |
| 215 // (new versions) are published. |
| 216 // TODO(panayiotis): Track Java as soon as it's supported on Chrome Mac. |
| 217 // TODO(panayiotis): Get the Real Player version on Mac, somehow. |
| 218 static const PluginGroupDefinition kGroupDefinitions[] = { |
| 219 { "Quicktime", "QuickTime Plug-in", "", "", "7.6.6", |
| 220 "http://www.apple.com/quicktime/download/" }, |
| 221 { "Flash", "Shockwave Flash", "", "", "10.0.45", |
| 222 "http://get.adobe.com/flashplayer/" }, |
| 223 { "Silverlight 3", "Silverlight", "0", "4", "3.0.50106.0", |
| 224 "http://go.microsoft.com/fwlink/?LinkID=185927" }, |
| 225 { "Silverlight 4", "Silverlight", "4", "5", "", |
| 226 "http://go.microsoft.com/fwlink/?LinkID=185927" }, |
| 227 { "Flip4Mac", "Flip4Mac", "", "", "2.2.1", |
| 228 "http://www.telestream.net/flip4mac-wmv/overview.htm" }, |
| 229 { "Shockwave", "Shockwave for Director", "", "", "11.5.7.609", |
| 230 "http://www.adobe.com/shockwave/download/" } |
| 231 }; |
| 232 |
| 233 #elif defined(OS_WIN) |
| 234 // TODO(panayiotis): We should group "RealJukebox NS Plugin" with the rest of |
| 235 // the RealPlayer files. |
| 236 static const PluginGroupDefinition kGroupDefinitions[] = { |
| 237 { "Quicktime", "QuickTime Plug-in", "", "", "7.6.6", |
| 238 "http://www.apple.com/quicktime/download/" }, |
| 239 { "Java 6", "Java", "", "6", "6.0.200", |
| 240 "http://www.java.com/" }, |
| 241 { "Adobe Reader 9", "Adobe Acrobat", "9", "10", "9.3.2", |
| 242 "http://get.adobe.com/reader/" }, |
| 243 { "Adobe Reader 8", "Adobe Acrobat", "0", "9", "8.2.2", |
| 244 "http://get.adobe.com/reader/" }, |
| 245 { "Flash", "Shockwave Flash", "", "", "10.0.45", |
| 246 "http://get.adobe.com/flashplayer/" }, |
| 247 { "Silverlight 3", "Silverlight", "0", "4", "3.0.50106.0", |
| 248 "http://go.microsoft.com/fwlink/?LinkID=185927" }, |
| 249 { "Silverlight 4", "Silverlight", "4", "5", "", |
| 250 "http://go.microsoft.com/fwlink/?LinkID=185927" }, |
| 251 { "Shockwave", "Shockwave for Director", "", "", "11.5.7.609", |
| 252 "http://www.adobe.com/shockwave/download/" }, |
| 253 { "DivX Player", "DivX Web Player", "", "", "1.4.3.4", |
| 254 "http://download.divx.com/divx/autoupdate/player/DivXWebPlayerInstaller.exe"
}, |
| 255 // These are here for grouping, no vulnerabilies known. |
| 256 { "Windows Media Player", "Windows Media Player", "", "", "", "" }, |
| 257 { "Microsoft Office", "Microsoft Office", "", "", "", "" }, |
| 258 // TODO(panayiotis): The vulnerable versions are |
| 259 // (v >= 6.0.12.1040 && v <= 6.0.12.1663) |
| 260 // || v == 6.0.12.1698 || v == 6.0.12.1741 |
| 261 { "RealPlayer", "RealPlayer", "", "", "", |
| 262 "http://www.adobe.com/shockwave/download/" }, |
| 263 }; |
| 264 |
| 265 #else |
| 266 static const PluginGroupDefinition kGroupDefinitions[] = {}; |
| 267 #endif |
| 268 |
| 269 /*static*/ |
| 270 const PluginGroupDefinition* PluginUpdater::GetPluginGroupDefinitions() { |
| 271 return kGroupDefinitions; |
| 272 } |
| 273 |
| 274 /*static*/ |
| 275 const size_t PluginUpdater::GetPluginGroupDefinitionsSize() { |
| 276 return arraysize(kGroupDefinitions); |
| 277 } |
| 278 |
| 279 // static |
| 280 PluginUpdater* PluginUpdater::GetInstance() { |
| 281 return Singleton<PluginUpdater>::get(); |
| 282 } |
| 283 |
| 284 PluginUpdater::PluginUpdater() { |
| 285 const PluginGroupDefinition* definitions = GetPluginGroupDefinitions(); |
| 286 for (size_t i = 0; i < GetPluginGroupDefinitionsSize(); ++i) { |
| 287 PluginGroup* definition_group = PluginGroup::FromPluginGroupDefinition( |
| 288 definitions[i]); |
| 289 plugin_group_definitions_.push_back(linked_ptr<PluginGroup>( |
| 290 definition_group)); |
| 291 } |
| 292 } |
| 293 |
| 294 PluginUpdater::~PluginUpdater() { |
| 295 } |
| 296 |
| 297 // Convert to a List of Groups |
| 298 void PluginUpdater::GetPluginGroups( |
| 299 std::vector<linked_ptr<PluginGroup> >* plugin_groups) { |
| 300 // Read all plugins and convert them to plugin groups |
| 301 std::vector<WebPluginInfo> web_plugins; |
| 302 NPAPI::PluginList::Singleton()->GetPlugins(false, &web_plugins); |
| 303 |
| 304 // We first search for an existing group that matches our name, |
| 305 // and only create a new group if we can't find any. |
| 306 for (size_t i = 0; i < web_plugins.size(); ++i) { |
| 307 const WebPluginInfo& web_plugin = web_plugins[i]; |
| 308 bool found = false; |
| 309 for (std::vector<linked_ptr<PluginGroup> >::iterator existing_it = |
| 310 plugin_groups->begin(); |
| 311 existing_it != plugin_groups->end(); |
| 312 ++existing_it) { |
| 313 if ((*existing_it)->Match(web_plugin)) { |
| 314 (*existing_it)->AddPlugin(web_plugin, i); |
| 315 found = true; |
| 316 break; |
| 317 } |
| 318 } |
| 319 |
| 320 if (!found) { |
| 321 // See if this plugin matches any of the hardcoded groups. |
| 322 for (std::vector<linked_ptr<PluginGroup> >::iterator defs_it = |
| 323 plugin_group_definitions_.begin(); |
| 324 defs_it != plugin_group_definitions_.end(); |
| 325 ++defs_it) { |
| 326 if ((*defs_it)->Match(web_plugin)) { |
| 327 // Make a copy, otherwise we'd be modifying plugin_group_defs_ every |
| 328 // time this method is called. |
| 329 PluginGroup* copy = (*defs_it)->Copy(); |
| 330 copy->AddPlugin(web_plugin, i); |
| 331 plugin_groups->push_back(linked_ptr<PluginGroup>(copy)); |
| 332 found = true; |
| 333 break; |
| 334 } |
| 335 } |
| 336 } |
| 337 |
| 338 // Not found in our hardcoded list, create a new one. |
| 339 if (!found) { |
| 340 PluginGroup* plugin_group = PluginGroup::FromWebPluginInfo(web_plugin); |
| 341 plugin_group->AddPlugin(web_plugin, i); |
| 342 plugin_groups->push_back(linked_ptr<PluginGroup>(plugin_group)); |
| 343 } |
| 344 } |
| 345 } |
| 346 |
| 347 ListValue* PluginUpdater::GetPluginGroupsData() { |
| 348 std::vector<linked_ptr<PluginGroup> > plugin_groups; |
| 349 GetPluginGroups(&plugin_groups); |
| 350 |
| 351 // Construct DictionaryValues to return to the UI |
| 352 ListValue* plugin_groups_data = new ListValue(); |
| 353 for (std::vector<linked_ptr<PluginGroup> >::iterator it = |
| 354 plugin_groups.begin(); |
| 355 it != plugin_groups.end(); |
| 356 ++it) { |
| 357 plugin_groups_data->Append((*it)->GetData()); |
| 358 } |
| 359 return plugin_groups_data; |
| 360 } |
| 361 |
| 362 ListValue* PluginUpdater::GetPluginGroupsSummary() { |
| 363 std::vector<linked_ptr<PluginGroup> > plugin_groups; |
| 364 GetPluginGroups(&plugin_groups); |
| 365 |
| 366 // Construct DictionaryValues to return to the UI |
| 367 ListValue* plugin_groups_data = new ListValue(); |
| 368 for (std::vector<linked_ptr<PluginGroup> >::iterator it = |
| 369 plugin_groups.begin(); |
| 370 it != plugin_groups.end(); |
| 371 ++it) { |
| 372 plugin_groups_data->Append((*it)->GetSummary()); |
| 373 } |
| 374 return plugin_groups_data; |
| 375 } |
| 376 |
| 377 void PluginUpdater::EnablePluginGroup(bool enable, |
| 378 const string16& group_name) { |
| 379 std::vector<linked_ptr<PluginGroup> > plugin_groups; |
| 380 GetPluginGroups(&plugin_groups); |
| 381 |
| 382 for (std::vector<linked_ptr<PluginGroup> >::iterator it = |
| 383 plugin_groups.begin(); |
| 384 it != plugin_groups.end(); |
| 385 ++it) { |
| 386 if ((*it)->GetGroupName() == group_name) { |
| 387 (*it)->Enable(enable); |
| 388 } |
| 389 } |
| 390 } |
| 391 |
| 392 void PluginUpdater::EnablePluginFile(bool enable, |
| 393 const FilePath::StringType& file_path) { |
| 394 if (enable) |
| 395 NPAPI::PluginList::Singleton()->EnablePlugin(FilePath(file_path)); |
| 396 else |
| 397 NPAPI::PluginList::Singleton()->DisablePlugin(FilePath(file_path)); |
| 398 } |
| 399 |
| 400 void PluginUpdater::DisablePluginGroupsFromPrefs(Profile* profile) { |
| 401 bool update_internal_dir = false; |
| 402 FilePath last_internal_dir = |
| 403 profile->GetPrefs()->GetFilePath(prefs::kPluginsLastInternalDirectory); |
| 404 FilePath cur_internal_dir; |
| 405 if (PathService::Get(chrome::DIR_INTERNAL_PLUGINS, &cur_internal_dir)) |
| 406 update_internal_dir = (cur_internal_dir != last_internal_dir); |
| 407 |
| 408 if (const ListValue* saved_plugins_list = |
| 409 profile->GetPrefs()->GetList(prefs::kPluginsPluginsList)) { |
| 410 for (ListValue::const_iterator it = saved_plugins_list->begin(); |
| 411 it != saved_plugins_list->end(); |
| 412 ++it) { |
| 413 if (!(*it)->IsType(Value::TYPE_DICTIONARY)) { |
| 414 LOG(WARNING) << "Invalid entry in " << prefs::kPluginsPluginsList; |
| 415 continue; // Oops, don't know what to do with this item. |
| 416 } |
| 417 |
| 418 DictionaryValue* plugin = static_cast<DictionaryValue*>(*it); |
| 419 string16 group_name; |
| 420 bool enabled = true; |
| 421 plugin->GetBoolean(L"enabled", &enabled); |
| 422 |
| 423 FilePath::StringType path; |
| 424 if (!enabled && plugin->GetString(L"path", &path)) { |
| 425 // The plugin list constains all the plugin files in addition to the |
| 426 // plugin groups. Files have a path attribute, groups don't. |
| 427 NPAPI::PluginList::Singleton()->DisablePlugin(FilePath(path)); |
| 428 |
| 429 // If the internal plugin directory has changed and if the plugin looks |
| 430 // internal, also disable it in the current internal plugins directory. |
| 431 if (update_internal_dir && |
| 432 FilePath(path).DirName() == last_internal_dir) { |
| 433 NPAPI::PluginList::Singleton()->DisablePlugin( |
| 434 cur_internal_dir.Append(FilePath(path).BaseName())); |
| 435 } |
| 436 } else if (!enabled && plugin->GetStringAsUTF16(L"name", &group_name)) { |
| 437 // Otherwise this is a list of groups. |
| 438 EnablePluginGroup(false, group_name); |
| 439 } |
| 440 } |
| 441 } |
| 442 } |
| 443 |
| 444 DictionaryValue* PluginUpdater::CreatePluginFileSummary( |
| 445 const WebPluginInfo& plugin) { |
| 446 DictionaryValue* data = new DictionaryValue(); |
| 447 data->SetString(L"path", plugin.path.value()); |
| 448 data->SetString(L"name", plugin.name); |
| 449 data->SetString(L"version", plugin.version); |
| 450 data->SetBoolean(L"enabled", plugin.enabled); |
| 451 return data; |
| 452 } |
| 453 |
| 454 void PluginUpdater::UpdatePreferences(Profile* profile) { |
| 455 ListValue* plugins_list = profile->GetPrefs()->GetMutableList( |
| 456 prefs::kPluginsPluginsList); |
| 457 plugins_list->Clear(); |
| 458 |
| 459 FilePath internal_dir; |
| 460 if (PathService::Get(chrome::DIR_INTERNAL_PLUGINS, &internal_dir)) |
| 461 profile->GetPrefs()->SetFilePath(prefs::kPluginsLastInternalDirectory, |
| 462 internal_dir); |
| 463 |
| 464 // Add the plugin files. |
| 465 std::vector<WebPluginInfo> plugins; |
| 466 NPAPI::PluginList::Singleton()->GetPlugins(false, &plugins); |
| 467 for (std::vector<WebPluginInfo>::const_iterator it = plugins.begin(); |
| 468 it != plugins.end(); |
| 469 ++it) { |
| 470 plugins_list->Append(CreatePluginFileSummary(*it)); |
| 471 } |
| 472 |
| 473 // Add the groups as well. |
| 474 ListValue* plugin_groups = GetPluginGroupsSummary(); |
| 475 for (ListValue::const_iterator it = plugin_groups->begin(); |
| 476 it != plugin_groups->end(); |
| 477 ++it) { |
| 478 plugins_list->Append(*it); |
| 479 } |
| 480 } |
OLD | NEW |