OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/plugins/plugin_finder.h" | 5 #include "chrome/browser/plugins/plugin_finder.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 | 24 |
25 #if defined(ENABLE_PLUGIN_INSTALLATION) | 25 #if defined(ENABLE_PLUGIN_INSTALLATION) |
26 #include "chrome/browser/plugins/plugin_installer.h" | 26 #include "chrome/browser/plugins/plugin_installer.h" |
27 #endif | 27 #endif |
28 | 28 |
29 using base::DictionaryValue; | 29 using base::DictionaryValue; |
30 using content::PluginService; | 30 using content::PluginService; |
31 | 31 |
32 namespace { | 32 namespace { |
33 | 33 |
| 34 typedef std::map<std::string, PluginMetadata*> PluginMap; |
| 35 |
34 // Gets the base name of the file path as the identifier. | 36 // Gets the base name of the file path as the identifier. |
35 static std::string GetIdentifier(const webkit::WebPluginInfo& plugin) { | 37 static std::string GetIdentifier(const webkit::WebPluginInfo& plugin) { |
36 #if defined(OS_POSIX) | 38 #if defined(OS_POSIX) |
37 return plugin.path.BaseName().value(); | 39 return plugin.path.BaseName().value(); |
38 #elif defined(OS_WIN) | 40 #elif defined(OS_WIN) |
39 return base::SysWideToUTF8(plugin.path.BaseName().value()); | 41 return base::SysWideToUTF8(plugin.path.BaseName().value()); |
40 #endif | 42 #endif |
41 } | 43 } |
42 | 44 |
43 // Gets the plug-in group name as the plug-in name if it is not empty or | 45 // Gets the plug-in group name as the plug-in name if it is not empty or |
44 // the filename without extension if the name is empty. | 46 // the filename without extension if the name is empty. |
45 static string16 GetGroupName(const webkit::WebPluginInfo& plugin) { | 47 static string16 GetGroupName(const webkit::WebPluginInfo& plugin) { |
46 if (!plugin.name.empty()) | 48 if (!plugin.name.empty()) |
47 return plugin.name; | 49 return plugin.name; |
48 | 50 |
49 FilePath::StringType path = plugin.path.BaseName().RemoveExtension().value(); | 51 FilePath::StringType path = plugin.path.BaseName().RemoveExtension().value(); |
50 #if defined(OS_POSIX) | 52 #if defined(OS_POSIX) |
51 return UTF8ToUTF16(path); | 53 return UTF8ToUTF16(path); |
52 #elif defined(OS_WIN) | 54 #elif defined(OS_WIN) |
53 return WideToUTF16(path); | 55 return WideToUTF16(path); |
54 #endif | 56 #endif |
55 } | 57 } |
56 | 58 |
| 59 void LoadMimeTypes(bool matching_mime_types, |
| 60 const DictionaryValue* plugin_dict, |
| 61 PluginMetadata* plugin) { |
| 62 const ListValue* mime_types = NULL; |
| 63 std::string list_key = |
| 64 matching_mime_types ? "matching_mime_types" : "mime_types"; |
| 65 if (!plugin_dict->GetList(list_key, &mime_types)) |
| 66 return; |
| 67 |
| 68 bool success = false; |
| 69 for (ListValue::const_iterator mime_type_it = mime_types->begin(); |
| 70 mime_type_it != mime_types->end(); ++mime_type_it) { |
| 71 std::string mime_type_str; |
| 72 success = (*mime_type_it)->GetAsString(&mime_type_str); |
| 73 DCHECK(success); |
| 74 if (matching_mime_types) { |
| 75 plugin->AddMatchingMimeType(mime_type_str); |
| 76 } else { |
| 77 plugin->AddMimeType(mime_type_str); |
| 78 } |
| 79 } |
| 80 } |
| 81 |
57 PluginMetadata* CreatePluginMetadata( | 82 PluginMetadata* CreatePluginMetadata( |
58 const std::string& identifier, | 83 const std::string& identifier, |
59 const DictionaryValue* plugin_dict) { | 84 const DictionaryValue* plugin_dict) { |
60 std::string url; | 85 std::string url; |
61 bool success = plugin_dict->GetString("url", &url); | 86 bool success = plugin_dict->GetString("url", &url); |
62 std::string help_url; | 87 std::string help_url; |
63 plugin_dict->GetString("help_url", &help_url); | 88 plugin_dict->GetString("help_url", &help_url); |
64 string16 name; | 89 string16 name; |
65 success = plugin_dict->GetString("name", &name); | 90 success = plugin_dict->GetString("name", &name); |
66 DCHECK(success); | 91 DCHECK(success); |
67 bool display_url = false; | 92 bool display_url = false; |
68 plugin_dict->GetBoolean("displayurl", &display_url); | 93 plugin_dict->GetBoolean("displayurl", &display_url); |
69 string16 group_name_matcher; | 94 string16 group_name_matcher; |
70 success = plugin_dict->GetString("group_name_matcher", &group_name_matcher); | 95 success = plugin_dict->GetString("group_name_matcher", &group_name_matcher); |
71 DCHECK(success); | 96 DCHECK(success); |
| 97 std::string language_str; |
| 98 plugin_dict->GetString("lang", &language_str); |
72 | 99 |
73 PluginMetadata* plugin = new PluginMetadata(identifier, | 100 PluginMetadata* plugin = new PluginMetadata(identifier, |
74 name, | 101 name, |
75 display_url, | 102 display_url, |
76 GURL(url), | 103 GURL(url), |
77 GURL(help_url), | 104 GURL(help_url), |
78 group_name_matcher); | 105 group_name_matcher, |
| 106 language_str); |
79 const ListValue* versions = NULL; | 107 const ListValue* versions = NULL; |
80 if (plugin_dict->GetList("versions", &versions)) { | 108 if (plugin_dict->GetList("versions", &versions)) { |
81 for (ListValue::const_iterator it = versions->begin(); | 109 for (ListValue::const_iterator it = versions->begin(); |
82 it != versions->end(); ++it) { | 110 it != versions->end(); ++it) { |
83 DictionaryValue* version_dict = NULL; | 111 DictionaryValue* version_dict = NULL; |
84 if (!(*it)->GetAsDictionary(&version_dict)) { | 112 if (!(*it)->GetAsDictionary(&version_dict)) { |
85 NOTREACHED(); | 113 NOTREACHED(); |
86 continue; | 114 continue; |
87 } | 115 } |
88 std::string version; | 116 std::string version; |
89 success = version_dict->GetString("version", &version); | 117 success = version_dict->GetString("version", &version); |
90 DCHECK(success); | 118 DCHECK(success); |
91 std::string status_str; | 119 std::string status_str; |
92 success = version_dict->GetString("status", &status_str); | 120 success = version_dict->GetString("status", &status_str); |
93 DCHECK(success); | 121 DCHECK(success); |
94 PluginMetadata::SecurityStatus status = | 122 PluginMetadata::SecurityStatus status = |
95 PluginMetadata::SECURITY_STATUS_UP_TO_DATE; | 123 PluginMetadata::SECURITY_STATUS_UP_TO_DATE; |
96 success = PluginMetadata::ParseSecurityStatus(status_str, &status); | 124 success = PluginMetadata::ParseSecurityStatus(status_str, &status); |
97 DCHECK(success); | 125 DCHECK(success); |
98 plugin->AddVersion(Version(version), status); | 126 plugin->AddVersion(Version(version), status); |
99 } | 127 } |
100 } | 128 } |
101 | 129 |
| 130 LoadMimeTypes(false, plugin_dict, plugin); |
| 131 LoadMimeTypes(true, plugin_dict, plugin); |
102 return plugin; | 132 return plugin; |
103 } | 133 } |
104 | 134 |
105 } // namespace | 135 } // namespace |
106 | 136 |
107 // static | 137 // static |
108 PluginFinder* PluginFinder::GetInstance() { | 138 PluginFinder* PluginFinder::GetInstance() { |
109 // PluginFinder::GetInstance() is the only method that's allowed to call | 139 // PluginFinder::GetInstance() is the only method that's allowed to call |
110 // Singleton<PluginFinder>::get(). | 140 // Singleton<PluginFinder>::get(). |
111 return Singleton<PluginFinder>::get(); | 141 return Singleton<PluginFinder>::get(); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 | 192 |
163 #if defined(ENABLE_PLUGIN_INSTALLATION) | 193 #if defined(ENABLE_PLUGIN_INSTALLATION) |
164 bool PluginFinder::FindPlugin( | 194 bool PluginFinder::FindPlugin( |
165 const std::string& mime_type, | 195 const std::string& mime_type, |
166 const std::string& language, | 196 const std::string& language, |
167 PluginInstaller** installer, | 197 PluginInstaller** installer, |
168 scoped_ptr<PluginMetadata>* plugin_metadata) { | 198 scoped_ptr<PluginMetadata>* plugin_metadata) { |
169 base::AutoLock lock(mutex_); | 199 base::AutoLock lock(mutex_); |
170 if (g_browser_process->local_state()->GetBoolean(prefs::kDisablePluginFinder)) | 200 if (g_browser_process->local_state()->GetBoolean(prefs::kDisablePluginFinder)) |
171 return false; | 201 return false; |
172 for (DictionaryValue::Iterator plugin_it(*plugin_list_); | |
173 plugin_it.HasNext(); plugin_it.Advance()) { | |
174 const DictionaryValue* plugin = NULL; | |
175 if (!plugin_it.value().GetAsDictionary(&plugin)) { | |
176 NOTREACHED(); | |
177 continue; | |
178 } | |
179 std::string language_str; | |
180 bool success = plugin->GetString("lang", &language_str); | |
181 if (language_str != language) | |
182 continue; | |
183 const ListValue* mime_types = NULL; | |
184 if (plugin->GetList("mime_types", &mime_types)) { | |
185 for (ListValue::const_iterator mime_type_it = mime_types->begin(); | |
186 mime_type_it != mime_types->end(); ++mime_type_it) { | |
187 std::string mime_type_str; | |
188 success = (*mime_type_it)->GetAsString(&mime_type_str); | |
189 DCHECK(success); | |
190 if (mime_type_str == mime_type) { | |
191 std::string identifier = plugin_it.key(); | |
192 std::map<std::string, PluginMetadata*>::const_iterator metadata_it = | |
193 identifier_plugin_.find(identifier); | |
194 DCHECK(metadata_it != identifier_plugin_.end()); | |
195 *plugin_metadata = metadata_it->second->Clone(); | |
196 | 202 |
197 std::map<std::string, PluginInstaller*>::const_iterator installer_it = | 203 PluginMap::const_iterator metadata_it = identifier_plugin_.begin(); |
198 installers_.find(identifier); | 204 for (; metadata_it != identifier_plugin_.end(); ++metadata_it) { |
199 DCHECK(installer_it != installers_.end()); | 205 if (language == metadata_it->second->language() && |
200 *installer = installer_it->second; | 206 metadata_it->second->HasMimeType(mime_type)) { |
201 return true; | 207 *plugin_metadata = metadata_it->second->Clone(); |
202 } | 208 |
203 } | 209 std::map<std::string, PluginInstaller*>::const_iterator installer_it = |
| 210 installers_.find(metadata_it->second->identifier()); |
| 211 DCHECK(installer_it != installers_.end()); |
| 212 *installer = installer_it->second; |
| 213 return true; |
204 } | 214 } |
205 } | 215 } |
206 return false; | 216 return false; |
207 } | 217 } |
208 | 218 |
209 bool PluginFinder::FindPluginWithIdentifier( | 219 bool PluginFinder::FindPluginWithIdentifier( |
210 const std::string& identifier, | 220 const std::string& identifier, |
211 PluginInstaller** installer, | 221 PluginInstaller** installer, |
212 scoped_ptr<PluginMetadata>* plugin_metadata) { | 222 scoped_ptr<PluginMetadata>* plugin_metadata) { |
213 base::AutoLock lock(mutex_); | 223 base::AutoLock lock(mutex_); |
214 std::map<std::string, PluginInstaller*>::const_iterator it = | 224 std::map<std::string, PluginInstaller*>::const_iterator it = |
215 installers_.find(identifier); | 225 installers_.find(identifier); |
216 if (it != installers_.end()) { | 226 if (it != installers_.end()) { |
217 *installer = it->second; | 227 *installer = it->second; |
218 std::map<std::string, PluginMetadata*>::const_iterator metadata_it = | 228 PluginMap::const_iterator metadata_it = identifier_plugin_.find(identifier); |
219 identifier_plugin_.find(identifier); | |
220 DCHECK(metadata_it != identifier_plugin_.end()); | 229 DCHECK(metadata_it != identifier_plugin_.end()); |
221 *plugin_metadata = metadata_it->second->Clone(); | 230 *plugin_metadata = metadata_it->second->Clone(); |
222 return true; | 231 return true; |
223 } | 232 } |
224 | 233 |
225 return false; | 234 return false; |
226 } | 235 } |
227 | 236 |
228 void PluginFinder::ReinitializePlugins( | 237 void PluginFinder::ReinitializePlugins( |
229 const base::DictionaryValue& json_metadata) { | 238 const base::DictionaryValue& json_metadata) { |
230 base::AutoLock lock(mutex_); | 239 base::AutoLock lock(mutex_); |
231 STLDeleteValues(&identifier_plugin_); | 240 STLDeleteValues(&identifier_plugin_); |
232 identifier_plugin_.clear(); | 241 identifier_plugin_.clear(); |
233 name_plugin_.clear(); | |
234 | 242 |
235 plugin_list_.reset(json_metadata.DeepCopy()); | 243 plugin_list_.reset(json_metadata.DeepCopy()); |
236 InitInternal(); | 244 InitInternal(); |
237 } | 245 } |
238 #endif | 246 #endif |
239 | 247 |
240 string16 PluginFinder::FindPluginNameWithIdentifier( | 248 string16 PluginFinder::FindPluginNameWithIdentifier( |
241 const std::string& identifier) { | 249 const std::string& identifier) { |
242 base::AutoLock lock(mutex_); | 250 base::AutoLock lock(mutex_); |
243 std::map<std::string, PluginMetadata*>::const_iterator it = | 251 PluginMap::const_iterator it = identifier_plugin_.find(identifier); |
244 identifier_plugin_.find(identifier); | |
245 string16 name; | 252 string16 name; |
246 if (it != identifier_plugin_.end()) | 253 if (it != identifier_plugin_.end()) |
247 name = it->second->name(); | 254 name = it->second->name(); |
248 | 255 |
249 return name.empty() ? UTF8ToUTF16(identifier) : name; | 256 return name.empty() ? UTF8ToUTF16(identifier) : name; |
250 } | 257 } |
251 | 258 |
252 scoped_ptr<PluginMetadata> PluginFinder::GetPluginMetadata( | 259 scoped_ptr<PluginMetadata> PluginFinder::GetPluginMetadata( |
253 const webkit::WebPluginInfo& plugin) { | 260 const webkit::WebPluginInfo& plugin) { |
254 base::AutoLock lock(mutex_); | 261 base::AutoLock lock(mutex_); |
255 if (name_plugin_.find(plugin.name) != name_plugin_.end()) | 262 for (PluginMap::const_iterator it = identifier_plugin_.begin(); |
256 return name_plugin_[plugin.name]->Clone(); | 263 it != identifier_plugin_.end(); ++it) { |
257 | |
258 // Use the group name matcher to find the plug-in metadata we want. | |
259 for (std::map<std::string, PluginMetadata*>::const_iterator it = | |
260 identifier_plugin_.begin(); it != identifier_plugin_.end(); ++it) { | |
261 if (!it->second->MatchesPlugin(plugin)) | 264 if (!it->second->MatchesPlugin(plugin)) |
262 continue; | 265 continue; |
263 | 266 |
264 name_plugin_[plugin.name] = it->second; | |
265 return it->second->Clone(); | 267 return it->second->Clone(); |
266 } | 268 } |
267 | 269 |
268 // The plug-in metadata was not found, create a dummy one holding | 270 // The plug-in metadata was not found, create a dummy one holding |
269 // the name, identifier and group name only. | 271 // the name, identifier and group name only. |
270 std::string identifier = GetIdentifier(plugin); | 272 std::string identifier = GetIdentifier(plugin); |
271 PluginMetadata* metadata = new PluginMetadata(identifier, | 273 PluginMetadata* metadata = new PluginMetadata(identifier, |
272 GetGroupName(plugin), | 274 GetGroupName(plugin), |
273 false, GURL(), GURL(), | 275 false, GURL(), GURL(), |
274 GetGroupName(plugin)); | 276 GetGroupName(plugin), |
275 | 277 ""); |
276 name_plugin_[plugin.name] = metadata; | |
277 identifier_plugin_[identifier] = metadata; | 278 identifier_plugin_[identifier] = metadata; |
278 return metadata->Clone(); | 279 return metadata->Clone(); |
279 } | 280 } |
280 | 281 |
281 void PluginFinder::InitInternal() { | 282 void PluginFinder::InitInternal() { |
282 for (DictionaryValue::Iterator plugin_it(*plugin_list_); | 283 for (DictionaryValue::Iterator plugin_it(*plugin_list_); |
283 plugin_it.HasNext(); plugin_it.Advance()) { | 284 plugin_it.HasNext(); plugin_it.Advance()) { |
284 DictionaryValue* plugin = NULL; | 285 DictionaryValue* plugin = NULL; |
285 const std::string& identifier = plugin_it.key(); | 286 const std::string& identifier = plugin_it.key(); |
286 if (plugin_list_->GetDictionaryWithoutPathExpansion(identifier, &plugin)) { | 287 if (plugin_list_->GetDictionaryWithoutPathExpansion(identifier, &plugin)) { |
287 DCHECK(!identifier_plugin_[identifier]); | 288 DCHECK(!identifier_plugin_[identifier]); |
288 identifier_plugin_[identifier] = CreatePluginMetadata(identifier, plugin); | 289 identifier_plugin_[identifier] = CreatePluginMetadata(identifier, plugin); |
289 | 290 |
290 #if defined(ENABLE_PLUGIN_INSTALLATION) | 291 #if defined(ENABLE_PLUGIN_INSTALLATION) |
291 if (installers_.find(identifier) == installers_.end()) | 292 if (installers_.find(identifier) == installers_.end()) |
292 installers_[identifier] = new PluginInstaller(); | 293 installers_[identifier] = new PluginInstaller(); |
293 #endif | 294 #endif |
294 } | 295 } |
295 } | 296 } |
296 } | 297 } |
OLD | NEW |