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"
9 #include "base/logging.h"
10 #include "base/strings/string_util.h"
11 #include "content/public/common/webplugininfo.h"
14 const char PluginMetadata::kAdobeReaderGroupName
[] = "Adobe Reader";
15 const char PluginMetadata::kJavaGroupName
[] = "Java(TM)";
16 const char PluginMetadata::kQuickTimeGroupName
[] = "QuickTime Player";
17 const char PluginMetadata::kShockwaveGroupName
[] = "Adobe Shockwave Player";
18 const char PluginMetadata::kRealPlayerGroupName
[] = "RealPlayer";
19 const char PluginMetadata::kSilverlightGroupName
[] = "Silverlight";
20 const char PluginMetadata::kWindowsMediaPlayerGroupName
[] =
21 "Windows Media Player";
23 PluginMetadata::PluginMetadata(const std::string
& identifier
,
24 const base::string16
& name
,
26 const GURL
& plugin_url
,
28 const base::string16
& group_name_matcher
,
29 const std::string
& language
)
30 : identifier_(identifier
),
32 group_name_matcher_(group_name_matcher
),
33 url_for_display_(url_for_display
),
34 plugin_url_(plugin_url
),
39 PluginMetadata::~PluginMetadata() {
42 void PluginMetadata::AddVersion(const Version
& version
,
43 SecurityStatus status
) {
44 DCHECK(versions_
.find(version
) == versions_
.end());
45 versions_
[version
] = status
;
48 void PluginMetadata::AddMimeType(const std::string
& mime_type
) {
49 all_mime_types_
.push_back(mime_type
);
52 void PluginMetadata::AddMatchingMimeType(const std::string
& mime_type
) {
53 matching_mime_types_
.push_back(mime_type
);
56 bool PluginMetadata::HasMimeType(const std::string
& mime_type
) const {
57 return std::find(all_mime_types_
.begin(), all_mime_types_
.end(), mime_type
) !=
58 all_mime_types_
.end();
61 bool PluginMetadata::MatchesPlugin(const content::WebPluginInfo
& plugin
) {
62 for (size_t i
= 0; i
< matching_mime_types_
.size(); ++i
) {
63 // To have a match, every one of the |matching_mime_types_|
64 // must be handled by the plug-in.
66 for (; j
< plugin
.mime_types
.size(); ++j
) {
67 if (plugin
.mime_types
[j
].mime_type
== matching_mime_types_
[i
])
70 if (j
== plugin
.mime_types
.size())
74 return MatchPattern(plugin
.name
, group_name_matcher_
);
78 bool PluginMetadata::ParseSecurityStatus(
79 const std::string
& status_str
,
80 PluginMetadata::SecurityStatus
* status
) {
81 if (status_str
== "up_to_date")
82 *status
= SECURITY_STATUS_UP_TO_DATE
;
83 else if (status_str
== "out_of_date")
84 *status
= SECURITY_STATUS_OUT_OF_DATE
;
85 else if (status_str
== "requires_authorization")
86 *status
= SECURITY_STATUS_REQUIRES_AUTHORIZATION
;
93 PluginMetadata::SecurityStatus
PluginMetadata::GetSecurityStatus(
94 const content::WebPluginInfo
& plugin
) const {
95 if (versions_
.empty()) {
96 // Unknown plugins require authorization.
97 return SECURITY_STATUS_REQUIRES_AUTHORIZATION
;
101 content::WebPluginInfo::CreateVersionFromString(plugin
.version
, &version
);
102 if (!version
.IsValid())
103 version
= Version("0");
105 // |lower_bound| returns the latest version that is not newer than |version|.
106 std::map
<Version
, SecurityStatus
, VersionComparator
>::const_iterator it
=
107 versions_
.lower_bound(version
);
108 // If there is at least one version defined, everything older than the oldest
109 // defined version is considered out-of-date.
110 if (it
== versions_
.end())
111 return SECURITY_STATUS_OUT_OF_DATE
;
116 bool PluginMetadata::VersionComparator::operator() (const Version
& lhs
,
117 const Version
& rhs
) const {
118 // Keep versions ordered by newest (biggest) first.
119 return lhs
.CompareTo(rhs
) > 0;
122 scoped_ptr
<PluginMetadata
> PluginMetadata::Clone() const {
123 PluginMetadata
* copy
= new PluginMetadata(identifier_
,
130 copy
->versions_
= versions_
;
131 return make_scoped_ptr(copy
);