- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / plugins / plugin_metadata.cc
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.
4
5 #include "chrome/browser/plugins/plugin_metadata.h"
6
7 #include <algorithm>
8
9 #include "base/logging.h"
10 #include "base/strings/string_util.h"
11 #include "content/public/common/webplugininfo.h"
12
13 // static
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";
22
23 PluginMetadata::PluginMetadata(const std::string& identifier,
24                                const string16& name,
25                                bool url_for_display,
26                                const GURL& plugin_url,
27                                const GURL& help_url,
28                                const string16& group_name_matcher,
29                                const std::string& language)
30     : identifier_(identifier),
31       name_(name),
32       group_name_matcher_(group_name_matcher),
33       url_for_display_(url_for_display),
34       plugin_url_(plugin_url),
35       help_url_(help_url),
36       language_(language) {
37 }
38
39 PluginMetadata::~PluginMetadata() {
40 }
41
42 void PluginMetadata::AddVersion(const Version& version,
43                                 SecurityStatus status) {
44   DCHECK(versions_.find(version) == versions_.end());
45   versions_[version] = status;
46 }
47
48 void PluginMetadata::AddMimeType(const std::string& mime_type) {
49   all_mime_types_.push_back(mime_type);
50 }
51
52 void PluginMetadata::AddMatchingMimeType(const std::string& mime_type) {
53   matching_mime_types_.push_back(mime_type);
54 }
55
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();
59 }
60
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.
65     size_t j = 0;
66     for (; j < plugin.mime_types.size(); ++j) {
67       if (plugin.mime_types[j].mime_type == matching_mime_types_[i])
68         break;
69     }
70     if (j == plugin.mime_types.size())
71       return false;
72   }
73
74   return MatchPattern(plugin.name, group_name_matcher_);
75 }
76
77 // static
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;
87   else
88     return false;
89
90   return true;
91 }
92
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;
98   }
99
100   Version version;
101   content::WebPluginInfo::CreateVersionFromString(plugin.version, &version);
102   if (!version.IsValid())
103     version = Version("0");
104
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;
112
113   return it->second;
114 }
115
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;
120 }
121
122 scoped_ptr<PluginMetadata> PluginMetadata::Clone() const {
123   PluginMetadata* copy = new PluginMetadata(identifier_,
124                                             name_,
125                                             url_for_display_,
126                                             plugin_url_,
127                                             help_url_,
128                                             group_name_matcher_,
129                                             language_);
130   copy->versions_ = versions_;
131   return make_scoped_ptr(copy);
132 }