Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / plugins / plugin_metadata.cc
blob87b4418a9a0132321e1ae87acbd5a34365bc829a
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
3 // found in the LICENSE file.
5 #include "chrome/browser/plugins/plugin_metadata.h"
7 #include <algorithm>
9 #include "base/logging.h"
10 #include "base/strings/pattern.h"
11 #include "base/strings/string_util.h"
12 #include "content/public/common/webplugininfo.h"
14 // static
15 const char PluginMetadata::kAdobeReaderGroupName[] = "Adobe Reader";
16 const char PluginMetadata::kJavaGroupName[] = "Java(TM)";
17 const char PluginMetadata::kQuickTimeGroupName[] = "QuickTime Player";
18 const char PluginMetadata::kShockwaveGroupName[] = "Adobe Shockwave Player";
19 const char PluginMetadata::kRealPlayerGroupName[] = "RealPlayer";
20 const char PluginMetadata::kSilverlightGroupName[] = "Silverlight";
21 const char PluginMetadata::kWindowsMediaPlayerGroupName[] =
22 "Windows Media Player";
23 const char PluginMetadata::kGoogleTalkGroupName[] = "Google Talk";
24 const char PluginMetadata::kGoogleEarthGroupName[] = "Google Earth";
26 PluginMetadata::PluginMetadata(const std::string& identifier,
27 const base::string16& name,
28 bool url_for_display,
29 const GURL& plugin_url,
30 const GURL& help_url,
31 const base::string16& group_name_matcher,
32 const std::string& language)
33 : identifier_(identifier),
34 name_(name),
35 group_name_matcher_(group_name_matcher),
36 url_for_display_(url_for_display),
37 plugin_url_(plugin_url),
38 help_url_(help_url),
39 language_(language) {
42 PluginMetadata::~PluginMetadata() {
45 void PluginMetadata::AddVersion(const Version& version,
46 SecurityStatus status) {
47 DCHECK(versions_.find(version) == versions_.end());
48 versions_[version] = status;
51 void PluginMetadata::AddMimeType(const std::string& mime_type) {
52 all_mime_types_.push_back(mime_type);
55 void PluginMetadata::AddMatchingMimeType(const std::string& mime_type) {
56 matching_mime_types_.push_back(mime_type);
59 bool PluginMetadata::HasMimeType(const std::string& mime_type) const {
60 return std::find(all_mime_types_.begin(), all_mime_types_.end(), mime_type) !=
61 all_mime_types_.end();
64 bool PluginMetadata::MatchesPlugin(const content::WebPluginInfo& plugin) {
65 for (size_t i = 0; i < matching_mime_types_.size(); ++i) {
66 // To have a match, every one of the |matching_mime_types_|
67 // must be handled by the plugin.
68 size_t j = 0;
69 for (; j < plugin.mime_types.size(); ++j) {
70 if (plugin.mime_types[j].mime_type == matching_mime_types_[i])
71 break;
73 if (j == plugin.mime_types.size())
74 return false;
77 return base::MatchPattern(plugin.name, group_name_matcher_);
80 // static
81 bool PluginMetadata::ParseSecurityStatus(
82 const std::string& status_str,
83 PluginMetadata::SecurityStatus* status) {
84 if (status_str == "up_to_date")
85 *status = SECURITY_STATUS_UP_TO_DATE;
86 else if (status_str == "out_of_date")
87 *status = SECURITY_STATUS_OUT_OF_DATE;
88 else if (status_str == "requires_authorization")
89 *status = SECURITY_STATUS_REQUIRES_AUTHORIZATION;
90 else
91 return false;
93 return true;
96 PluginMetadata::SecurityStatus PluginMetadata::GetSecurityStatus(
97 const content::WebPluginInfo& plugin) const {
98 if (versions_.empty()) {
99 // Unknown plugins require authorization.
100 return SECURITY_STATUS_REQUIRES_AUTHORIZATION;
103 Version version;
104 content::WebPluginInfo::CreateVersionFromString(plugin.version, &version);
105 if (!version.IsValid())
106 version = Version("0");
108 // |lower_bound| returns the latest version that is not newer than |version|.
109 std::map<Version, SecurityStatus, VersionComparator>::const_iterator it =
110 versions_.lower_bound(version);
111 // If there is at least one version defined, everything older than the oldest
112 // defined version is considered out-of-date.
113 if (it == versions_.end())
114 return SECURITY_STATUS_OUT_OF_DATE;
116 return it->second;
119 bool PluginMetadata::VersionComparator::operator() (const Version& lhs,
120 const Version& rhs) const {
121 // Keep versions ordered by newest (biggest) first.
122 return lhs.CompareTo(rhs) > 0;
125 scoped_ptr<PluginMetadata> PluginMetadata::Clone() const {
126 PluginMetadata* copy = new PluginMetadata(identifier_,
127 name_,
128 url_for_display_,
129 plugin_url_,
130 help_url_,
131 group_name_matcher_,
132 language_);
133 copy->versions_ = versions_;
134 return make_scoped_ptr(copy);