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/string_util.h"
11 #include "webkit/plugins/npapi/plugin_list.h"
12 #include "webkit/plugins/npapi/plugin_utils.h"
13 #include "webkit/plugins/webplugininfo.h"
16 const char PluginMetadata::kAdobeReaderGroupName
[] = "Adobe Reader";
17 const char PluginMetadata::kJavaGroupName
[] = "Java(TM)";
18 const char PluginMetadata::kQuickTimeGroupName
[] = "QuickTime Player";
19 const char PluginMetadata::kShockwaveGroupName
[] = "Adobe Shockwave Player";
20 const char PluginMetadata::kRealPlayerGroupName
[] = "RealPlayer";
21 const char PluginMetadata::kSilverlightGroupName
[] = "Silverlight";
22 const char PluginMetadata::kWindowsMediaPlayerGroupName
[] =
23 "Windows Media Player";
25 PluginMetadata::PluginMetadata(const std::string
& identifier
,
28 const GURL
& plugin_url
,
30 const string16
& group_name_matcher
,
31 const std::string
& language
)
32 : identifier_(identifier
),
34 group_name_matcher_(group_name_matcher
),
35 url_for_display_(url_for_display
),
36 plugin_url_(plugin_url
),
41 PluginMetadata::~PluginMetadata() {
44 void PluginMetadata::AddVersion(const Version
& version
,
45 SecurityStatus status
) {
46 DCHECK(versions_
.find(version
) == versions_
.end());
47 versions_
[version
] = status
;
50 void PluginMetadata::AddMimeType(const std::string
& mime_type
) {
51 all_mime_types_
.push_back(mime_type
);
54 void PluginMetadata::AddMatchingMimeType(const std::string
& mime_type
) {
55 matching_mime_types_
.push_back(mime_type
);
58 bool PluginMetadata::HasMimeType(const std::string
& mime_type
) const {
59 return std::find(all_mime_types_
.begin(), all_mime_types_
.end(), mime_type
) !=
60 all_mime_types_
.end();
63 bool PluginMetadata::MatchesPlugin(const webkit::WebPluginInfo
& plugin
) {
64 using webkit::npapi::PluginList
;
66 for (size_t i
= 0; i
< matching_mime_types_
.size(); ++i
) {
67 // To have a match, every one of the |matching_mime_types_|
68 // must be handled by the plug-in.
69 if (!PluginList::SupportsType(plugin
, matching_mime_types_
[i
], false))
73 return MatchPattern(plugin
.name
, group_name_matcher_
);
77 bool PluginMetadata::ParseSecurityStatus(
78 const std::string
& status_str
,
79 PluginMetadata::SecurityStatus
* status
) {
80 if (status_str
== "up_to_date")
81 *status
= SECURITY_STATUS_UP_TO_DATE
;
82 else if (status_str
== "out_of_date")
83 *status
= SECURITY_STATUS_OUT_OF_DATE
;
84 else if (status_str
== "requires_authorization")
85 *status
= SECURITY_STATUS_REQUIRES_AUTHORIZATION
;
92 PluginMetadata::SecurityStatus
PluginMetadata::GetSecurityStatus(
93 const webkit::WebPluginInfo
& plugin
) const {
94 if (versions_
.empty()) {
96 // On Linux, unknown plugins require authorization.
97 return SECURITY_STATUS_REQUIRES_AUTHORIZATION
;
99 return SECURITY_STATUS_UP_TO_DATE
;
104 webkit::npapi::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
;
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_
,
133 copy
->versions_
= versions_
;
134 return make_scoped_ptr(copy
);