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 | |
36 const char kMimeTypesListKey[] = "mime_types"; | |
37 const char kMatchingMimeTypesListKey[] = "matching_mime_types"; | |
38 | |
34 // Gets the base name of the file path as the identifier. | 39 // Gets the base name of the file path as the identifier. |
35 static std::string GetIdentifier(const webkit::WebPluginInfo& plugin) { | 40 static std::string GetIdentifier(const webkit::WebPluginInfo& plugin) { |
36 #if defined(OS_POSIX) | 41 #if defined(OS_POSIX) |
37 return plugin.path.BaseName().value(); | 42 return plugin.path.BaseName().value(); |
38 #elif defined(OS_WIN) | 43 #elif defined(OS_WIN) |
39 return base::SysWideToUTF8(plugin.path.BaseName().value()); | 44 return base::SysWideToUTF8(plugin.path.BaseName().value()); |
40 #endif | 45 #endif |
41 } | 46 } |
42 | 47 |
43 // Gets the plug-in group name as the plug-in name if it is not empty or | 48 // 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. | 49 // the filename without extension if the name is empty. |
45 static string16 GetGroupName(const webkit::WebPluginInfo& plugin) { | 50 static string16 GetGroupName(const webkit::WebPluginInfo& plugin) { |
46 if (!plugin.name.empty()) | 51 if (!plugin.name.empty()) |
47 return plugin.name; | 52 return plugin.name; |
48 | 53 |
49 FilePath::StringType path = plugin.path.BaseName().RemoveExtension().value(); | 54 FilePath::StringType path = plugin.path.BaseName().RemoveExtension().value(); |
50 #if defined(OS_POSIX) | 55 #if defined(OS_POSIX) |
51 return UTF8ToUTF16(path); | 56 return UTF8ToUTF16(path); |
52 #elif defined(OS_WIN) | 57 #elif defined(OS_WIN) |
53 return WideToUTF16(path); | 58 return WideToUTF16(path); |
54 #endif | 59 #endif |
55 } | 60 } |
56 | 61 |
62 void LoadMimeTypes(const std::string& mime_type_list_key, | |
Bernhard Bauer
2012/10/04 12:31:16
You don't really need to explicitly pass in the ke
ibraaaa
2012/10/04 13:03:42
Well, that is what I thought but your last reply t
| |
63 bool matching_mime_types, | |
64 const DictionaryValue* plugin_dict, | |
65 PluginMetadata* plugin) { | |
66 const ListValue* mime_types = NULL; | |
67 if (!plugin_dict->GetList(mime_type_list_key, &mime_types)) | |
68 return; | |
69 | |
70 bool success = false; | |
71 for (ListValue::const_iterator mime_type_it = mime_types->begin(); | |
72 mime_type_it != mime_types->end(); ++mime_type_it) { | |
73 std::string mime_type_str; | |
74 success = (*mime_type_it)->GetAsString(&mime_type_str); | |
75 DCHECK(success); | |
76 if (matching_mime_types) { | |
77 plugin->AddMatchingMimeType(mime_type_str); | |
78 } else { | |
79 plugin->AddMimeType(mime_type_str); | |
80 } | |
81 } | |
82 } | |
83 | |
57 PluginMetadata* CreatePluginMetadata( | 84 PluginMetadata* CreatePluginMetadata( |
58 const std::string& identifier, | 85 const std::string& identifier, |
59 const DictionaryValue* plugin_dict) { | 86 const DictionaryValue* plugin_dict) { |
60 std::string url; | 87 std::string url; |
61 bool success = plugin_dict->GetString("url", &url); | 88 bool success = plugin_dict->GetString("url", &url); |
62 std::string help_url; | 89 std::string help_url; |
63 plugin_dict->GetString("help_url", &help_url); | 90 plugin_dict->GetString("help_url", &help_url); |
64 string16 name; | 91 string16 name; |
65 success = plugin_dict->GetString("name", &name); | 92 success = plugin_dict->GetString("name", &name); |
66 DCHECK(success); | 93 DCHECK(success); |
67 bool display_url = false; | 94 bool display_url = false; |
68 plugin_dict->GetBoolean("displayurl", &display_url); | 95 plugin_dict->GetBoolean("displayurl", &display_url); |
69 string16 group_name_matcher; | 96 string16 group_name_matcher; |
70 success = plugin_dict->GetString("group_name_matcher", &group_name_matcher); | 97 success = plugin_dict->GetString("group_name_matcher", &group_name_matcher); |
71 DCHECK(success); | 98 DCHECK(success); |
99 std::string language_str; | |
100 plugin_dict->GetString("lang", &language_str); | |
72 | 101 |
73 PluginMetadata* plugin = new PluginMetadata(identifier, | 102 PluginMetadata* plugin = new PluginMetadata(identifier, |
74 name, | 103 name, |
75 display_url, | 104 display_url, |
76 GURL(url), | 105 GURL(url), |
77 GURL(help_url), | 106 GURL(help_url), |
78 group_name_matcher); | 107 group_name_matcher, |
108 language_str); | |
79 const ListValue* versions = NULL; | 109 const ListValue* versions = NULL; |
80 if (plugin_dict->GetList("versions", &versions)) { | 110 if (plugin_dict->GetList("versions", &versions)) { |
81 for (ListValue::const_iterator it = versions->begin(); | 111 for (ListValue::const_iterator it = versions->begin(); |
82 it != versions->end(); ++it) { | 112 it != versions->end(); ++it) { |
83 DictionaryValue* version_dict = NULL; | 113 DictionaryValue* version_dict = NULL; |
84 if (!(*it)->GetAsDictionary(&version_dict)) { | 114 if (!(*it)->GetAsDictionary(&version_dict)) { |
85 NOTREACHED(); | 115 NOTREACHED(); |
86 continue; | 116 continue; |
87 } | 117 } |
88 std::string version; | 118 std::string version; |
89 success = version_dict->GetString("version", &version); | 119 success = version_dict->GetString("version", &version); |
90 DCHECK(success); | 120 DCHECK(success); |
91 std::string status_str; | 121 std::string status_str; |
92 success = version_dict->GetString("status", &status_str); | 122 success = version_dict->GetString("status", &status_str); |
93 DCHECK(success); | 123 DCHECK(success); |
94 PluginMetadata::SecurityStatus status = | 124 PluginMetadata::SecurityStatus status = |
95 PluginMetadata::SECURITY_STATUS_UP_TO_DATE; | 125 PluginMetadata::SECURITY_STATUS_UP_TO_DATE; |
96 success = PluginMetadata::ParseSecurityStatus(status_str, &status); | 126 success = PluginMetadata::ParseSecurityStatus(status_str, &status); |
97 DCHECK(success); | 127 DCHECK(success); |
98 plugin->AddVersion(Version(version), status); | 128 plugin->AddVersion(Version(version), status); |
99 } | 129 } |
100 } | 130 } |
101 | 131 |
132 LoadMimeTypes(kMimeTypesListKey, false, plugin_dict, plugin); | |
133 LoadMimeTypes(kMatchingMimeTypesListKey, true, plugin_dict, plugin); | |
102 return plugin; | 134 return plugin; |
103 } | 135 } |
104 | 136 |
105 } // namespace | 137 } // namespace |
106 | 138 |
107 // static | 139 // static |
108 PluginFinder* PluginFinder::GetInstance() { | 140 PluginFinder* PluginFinder::GetInstance() { |
109 // PluginFinder::GetInstance() is the only method that's allowed to call | 141 // PluginFinder::GetInstance() is the only method that's allowed to call |
110 // Singleton<PluginFinder>::get(). | 142 // Singleton<PluginFinder>::get(). |
111 return Singleton<PluginFinder>::get(); | 143 return Singleton<PluginFinder>::get(); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
162 | 194 |
163 #if defined(ENABLE_PLUGIN_INSTALLATION) | 195 #if defined(ENABLE_PLUGIN_INSTALLATION) |
164 bool PluginFinder::FindPlugin( | 196 bool PluginFinder::FindPlugin( |
165 const std::string& mime_type, | 197 const std::string& mime_type, |
166 const std::string& language, | 198 const std::string& language, |
167 PluginInstaller** installer, | 199 PluginInstaller** installer, |
168 scoped_ptr<PluginMetadata>* plugin_metadata) { | 200 scoped_ptr<PluginMetadata>* plugin_metadata) { |
169 base::AutoLock lock(mutex_); | 201 base::AutoLock lock(mutex_); |
170 if (g_browser_process->local_state()->GetBoolean(prefs::kDisablePluginFinder)) | 202 if (g_browser_process->local_state()->GetBoolean(prefs::kDisablePluginFinder)) |
171 return false; | 203 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 | 204 |
197 std::map<std::string, PluginInstaller*>::const_iterator installer_it = | 205 PluginMap::const_iterator metadata_it = identifier_plugin_.begin(); |
198 installers_.find(identifier); | 206 for (; metadata_it != identifier_plugin_.end(); ++metadata_it) { |
199 DCHECK(installer_it != installers_.end()); | 207 if (language == metadata_it->second->language() && |
200 *installer = installer_it->second; | 208 metadata_it->second->HasMimeType(mime_type)) { |
201 return true; | 209 *plugin_metadata = metadata_it->second->Clone(); |
202 } | 210 |
203 } | 211 std::map<std::string, PluginInstaller*>::const_iterator installer_it = |
212 installers_.find(metadata_it->second->identifier()); | |
213 DCHECK(installer_it != installers_.end()); | |
214 *installer = installer_it->second; | |
215 return true; | |
204 } | 216 } |
205 } | 217 } |
206 return false; | 218 return false; |
207 } | 219 } |
208 | 220 |
209 bool PluginFinder::FindPluginWithIdentifier( | 221 bool PluginFinder::FindPluginWithIdentifier( |
210 const std::string& identifier, | 222 const std::string& identifier, |
211 PluginInstaller** installer, | 223 PluginInstaller** installer, |
212 scoped_ptr<PluginMetadata>* plugin_metadata) { | 224 scoped_ptr<PluginMetadata>* plugin_metadata) { |
213 base::AutoLock lock(mutex_); | 225 base::AutoLock lock(mutex_); |
214 std::map<std::string, PluginInstaller*>::const_iterator it = | 226 std::map<std::string, PluginInstaller*>::const_iterator it = |
215 installers_.find(identifier); | 227 installers_.find(identifier); |
216 if (it != installers_.end()) { | 228 if (it != installers_.end()) { |
217 *installer = it->second; | 229 *installer = it->second; |
218 std::map<std::string, PluginMetadata*>::const_iterator metadata_it = | 230 PluginMap::const_iterator metadata_it = identifier_plugin_.find(identifier); |
219 identifier_plugin_.find(identifier); | |
220 DCHECK(metadata_it != identifier_plugin_.end()); | 231 DCHECK(metadata_it != identifier_plugin_.end()); |
221 *plugin_metadata = metadata_it->second->Clone(); | 232 *plugin_metadata = metadata_it->second->Clone(); |
222 return true; | 233 return true; |
223 } | 234 } |
224 | 235 |
225 return false; | 236 return false; |
226 } | 237 } |
227 | 238 |
228 void PluginFinder::ReinitializePlugins( | 239 void PluginFinder::ReinitializePlugins( |
229 const base::DictionaryValue& json_metadata) { | 240 const base::DictionaryValue& json_metadata) { |
230 base::AutoLock lock(mutex_); | 241 base::AutoLock lock(mutex_); |
231 STLDeleteValues(&identifier_plugin_); | 242 STLDeleteValues(&identifier_plugin_); |
232 identifier_plugin_.clear(); | 243 identifier_plugin_.clear(); |
233 name_plugin_.clear(); | |
234 | 244 |
235 plugin_list_.reset(json_metadata.DeepCopy()); | 245 plugin_list_.reset(json_metadata.DeepCopy()); |
236 InitInternal(); | 246 InitInternal(); |
237 } | 247 } |
238 #endif | 248 #endif |
239 | 249 |
240 string16 PluginFinder::FindPluginNameWithIdentifier( | 250 string16 PluginFinder::FindPluginNameWithIdentifier( |
241 const std::string& identifier) { | 251 const std::string& identifier) { |
242 base::AutoLock lock(mutex_); | 252 base::AutoLock lock(mutex_); |
243 std::map<std::string, PluginMetadata*>::const_iterator it = | 253 PluginMap::const_iterator it = identifier_plugin_.find(identifier); |
244 identifier_plugin_.find(identifier); | |
245 string16 name; | 254 string16 name; |
246 if (it != identifier_plugin_.end()) | 255 if (it != identifier_plugin_.end()) |
247 name = it->second->name(); | 256 name = it->second->name(); |
248 | 257 |
249 return name.empty() ? UTF8ToUTF16(identifier) : name; | 258 return name.empty() ? UTF8ToUTF16(identifier) : name; |
250 } | 259 } |
251 | 260 |
252 scoped_ptr<PluginMetadata> PluginFinder::GetPluginMetadata( | 261 scoped_ptr<PluginMetadata> PluginFinder::GetPluginMetadata( |
253 const webkit::WebPluginInfo& plugin) { | 262 const webkit::WebPluginInfo& plugin) { |
254 base::AutoLock lock(mutex_); | 263 base::AutoLock lock(mutex_); |
255 if (name_plugin_.find(plugin.name) != name_plugin_.end()) | 264 for (PluginMap::const_iterator it = identifier_plugin_.begin(); |
256 return name_plugin_[plugin.name]->Clone(); | 265 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)) | 266 if (!it->second->MatchesPlugin(plugin)) |
262 continue; | 267 continue; |
263 | 268 |
264 name_plugin_[plugin.name] = it->second; | |
265 return it->second->Clone(); | 269 return it->second->Clone(); |
266 } | 270 } |
267 | 271 |
268 // The plug-in metadata was not found, create a dummy one holding | 272 // The plug-in metadata was not found, create a dummy one holding |
269 // the name, identifier and group name only. | 273 // the name, identifier and group name only. |
270 std::string identifier = GetIdentifier(plugin); | 274 std::string identifier = GetIdentifier(plugin); |
271 PluginMetadata* metadata = new PluginMetadata(identifier, | 275 PluginMetadata* metadata = new PluginMetadata(identifier, |
272 GetGroupName(plugin), | 276 GetGroupName(plugin), |
273 false, GURL(), GURL(), | 277 false, GURL(), GURL(), |
274 GetGroupName(plugin)); | 278 GetGroupName(plugin), |
275 | 279 ""); |
276 name_plugin_[plugin.name] = metadata; | |
277 identifier_plugin_[identifier] = metadata; | 280 identifier_plugin_[identifier] = metadata; |
278 return metadata->Clone(); | 281 return metadata->Clone(); |
279 } | 282 } |
280 | 283 |
281 void PluginFinder::InitInternal() { | 284 void PluginFinder::InitInternal() { |
282 for (DictionaryValue::Iterator plugin_it(*plugin_list_); | 285 for (DictionaryValue::Iterator plugin_it(*plugin_list_); |
283 plugin_it.HasNext(); plugin_it.Advance()) { | 286 plugin_it.HasNext(); plugin_it.Advance()) { |
284 DictionaryValue* plugin = NULL; | 287 DictionaryValue* plugin = NULL; |
285 const std::string& identifier = plugin_it.key(); | 288 const std::string& identifier = plugin_it.key(); |
286 if (plugin_list_->GetDictionaryWithoutPathExpansion(identifier, &plugin)) { | 289 if (plugin_list_->GetDictionaryWithoutPathExpansion(identifier, &plugin)) { |
287 DCHECK(!identifier_plugin_[identifier]); | 290 DCHECK(!identifier_plugin_[identifier]); |
288 identifier_plugin_[identifier] = CreatePluginMetadata(identifier, plugin); | 291 identifier_plugin_[identifier] = CreatePluginMetadata(identifier, plugin); |
289 | 292 |
290 #if defined(ENABLE_PLUGIN_INSTALLATION) | 293 #if defined(ENABLE_PLUGIN_INSTALLATION) |
291 if (installers_.find(identifier) == installers_.end()) | 294 if (installers_.find(identifier) == installers_.end()) |
292 installers_[identifier] = new PluginInstaller(); | 295 installers_[identifier] = new PluginInstaller(); |
293 #endif | 296 #endif |
294 } | 297 } |
295 } | 298 } |
296 } | 299 } |
OLD | NEW |