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/plugin_finder.h" | 5 #include "chrome/browser/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" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "chrome/browser/browser_process.h" | 12 #include "chrome/browser/browser_process.h" |
13 #include "chrome/browser/plugin_installer.h" | 13 #include "chrome/browser/plugin_installer.h" |
14 #include "chrome/browser/prefs/pref_service.h" | 14 #include "chrome/browser/prefs/pref_service.h" |
15 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
16 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
17 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
18 #include "grit/browser_resources.h" | 18 #include "grit/browser_resources.h" |
19 #include "ui/base/resource/resource_bundle.h" | 19 #include "ui/base/resource/resource_bundle.h" |
20 | 20 |
| 21 using base::DictionaryValue; |
| 22 |
| 23 // static |
| 24 void PluginFinder::Get(const base::Callback<void(PluginFinder*)>& cb) { |
| 25 // At a later point we might want to do intialization here that needs to be |
| 26 // done asynchronously, like loading the plug-in list from disk or from a URL. |
| 27 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(cb, GetInstance())); |
| 28 } |
| 29 |
21 // static | 30 // static |
22 PluginFinder* PluginFinder::GetInstance() { | 31 PluginFinder* PluginFinder::GetInstance() { |
| 32 // PluginFinder::GetInstance() is the only method that's allowed to call |
| 33 // Singleton<PluginFinder>::get(). |
23 return Singleton<PluginFinder>::get(); | 34 return Singleton<PluginFinder>::get(); |
24 } | 35 } |
25 | 36 |
26 PluginFinder::PluginFinder() : plugin_list_(LoadPluginList()) { | 37 PluginFinder::PluginFinder() : plugin_list_(LoadPluginList()) { |
27 if (!plugin_list_.get()) { | 38 if (!plugin_list_.get()) { |
28 NOTREACHED(); | 39 NOTREACHED(); |
29 plugin_list_.reset(new base::ListValue()); | 40 plugin_list_.reset(new DictionaryValue()); |
30 } | 41 } |
31 } | 42 } |
32 | 43 |
33 // static | 44 // static |
34 scoped_ptr<base::ListValue> PluginFinder::LoadPluginList() { | 45 scoped_ptr<DictionaryValue> PluginFinder::LoadPluginList() { |
35 return scoped_ptr<base::ListValue>(LoadPluginListInternal()).Pass(); | 46 return scoped_ptr<DictionaryValue>(LoadPluginListInternal()); |
36 } | 47 } |
37 | 48 |
38 base::ListValue* PluginFinder::LoadPluginListInternal() { | 49 DictionaryValue* PluginFinder::LoadPluginListInternal() { |
39 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) | 50 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) |
40 base::StringPiece json_resource( | 51 base::StringPiece json_resource( |
41 ResourceBundle::GetSharedInstance().GetRawDataResource( | 52 ResourceBundle::GetSharedInstance().GetRawDataResource( |
42 IDR_PLUGIN_DB_JSON)); | 53 IDR_PLUGIN_DB_JSON)); |
43 bool allow_trailing_comma = false; | 54 bool allow_trailing_comma = false; |
44 std::string error_str; | 55 std::string error_str; |
45 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError( | 56 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError( |
46 json_resource.as_string(), | 57 json_resource.as_string(), |
47 allow_trailing_comma, | 58 allow_trailing_comma, |
48 NULL, | 59 NULL, |
49 &error_str)); | 60 &error_str)); |
50 if (!value.get()) { | 61 if (!value.get()) { |
51 DLOG(ERROR) << error_str; | 62 DLOG(ERROR) << error_str; |
52 return NULL; | 63 return NULL; |
53 } | 64 } |
54 base::DictionaryValue* dict = NULL; | 65 if (value->GetType() != base::Value::TYPE_DICTIONARY) |
55 if (!value->GetAsDictionary(&dict)) | |
56 return NULL; | 66 return NULL; |
57 base::ListValue* list = NULL; | 67 return static_cast<base::DictionaryValue*>(value.release()); |
58 if (!dict->GetList("plugins", &list)) | |
59 return NULL; | |
60 return list->DeepCopy(); | |
61 #else | 68 #else |
62 return new base::ListValue(); | 69 return new DictionaryValue(); |
63 #endif | 70 #endif |
64 } | 71 } |
65 | 72 |
66 PluginFinder::~PluginFinder() { | 73 PluginFinder::~PluginFinder() { |
67 STLDeleteValues(&installers_); | 74 STLDeleteValues(&installers_); |
68 } | 75 } |
69 | 76 |
70 void PluginFinder::FindPlugin( | 77 PluginInstaller* PluginFinder::FindPlugin(const std::string& mime_type, |
71 const std::string& mime_type, | 78 const std::string& language) { |
72 const std::string& language, | 79 if (g_browser_process->local_state()->GetBoolean(prefs::kDisablePluginFinder)) |
73 const FindPluginCallback& callback) { | 80 return NULL; |
74 PluginInstaller* installer = FindPluginInternal(mime_type, language); | 81 for (DictionaryValue::Iterator plugin_it(*plugin_list_); |
75 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, installer)); | 82 plugin_it.HasNext(); plugin_it.Advance()) { |
76 } | 83 const DictionaryValue* plugin = NULL; |
77 | 84 if (!plugin_it.value().GetAsDictionary(&plugin)) { |
78 void PluginFinder::FindPluginWithIdentifier( | 85 NOTREACHED(); |
79 const std::string& identifier, | 86 continue; |
80 const FindPluginCallback& found_callback) { | 87 } |
81 PluginInstaller* installer = NULL; | 88 std::string language_str; |
82 std::map<std::string, PluginInstaller*>::const_iterator it = | 89 bool success = plugin->GetString("lang", &language_str); |
83 installers_.find(identifier); | 90 DCHECK(success); |
84 if (it != installers_.end()) { | 91 if (language_str != language) |
85 installer = it->second; | 92 continue; |
86 } else { | 93 ListValue* mime_types = NULL; |
87 for (ListValue::const_iterator plugin_it = plugin_list_->begin(); | 94 plugin->GetList("mime_types", &mime_types); |
88 plugin_it != plugin_list_->end(); ++plugin_it) { | 95 DCHECK(success); |
89 const base::DictionaryValue* plugin = NULL; | 96 for (ListValue::const_iterator mime_type_it = mime_types->begin(); |
90 if (!(*plugin_it)->GetAsDictionary(&plugin)) { | 97 mime_type_it != mime_types->end(); ++mime_type_it) { |
91 NOTREACHED(); | 98 std::string mime_type_str; |
92 continue; | 99 success = (*mime_type_it)->GetAsString(&mime_type_str); |
93 } | |
94 std::string id; | |
95 bool success = plugin->GetString("identifier", &id); | |
96 DCHECK(success); | 100 DCHECK(success); |
97 if (id == identifier) { | 101 if (mime_type_str == mime_type) { |
98 installer = CreateInstaller(identifier, plugin); | 102 std::string identifier = plugin_it.key(); |
99 break; | 103 std::map<std::string, PluginInstaller*>::const_iterator installer = |
| 104 installers_.find(identifier); |
| 105 if (installer != installers_.end()) |
| 106 return installer->second; |
| 107 return CreateInstaller(identifier, plugin); |
100 } | 108 } |
101 } | 109 } |
102 } | 110 } |
103 MessageLoop::current()->PostTask(FROM_HERE, | 111 return NULL; |
104 base::Bind(found_callback, installer)); | 112 } |
| 113 |
| 114 PluginInstaller* PluginFinder::FindPluginWithIdentifier( |
| 115 const std::string& identifier) { |
| 116 std::map<std::string, PluginInstaller*>::const_iterator it = |
| 117 installers_.find(identifier); |
| 118 if (it != installers_.end()) |
| 119 return it->second; |
| 120 DictionaryValue* plugin = NULL; |
| 121 if (plugin_list_->GetDictionaryWithoutPathExpansion(identifier, &plugin)) |
| 122 return CreateInstaller(identifier, plugin); |
| 123 return NULL; |
105 } | 124 } |
106 | 125 |
107 PluginInstaller* PluginFinder::CreateInstaller( | 126 PluginInstaller* PluginFinder::CreateInstaller( |
108 const std::string& identifier, | 127 const std::string& identifier, |
109 const base::DictionaryValue* plugin_dict) { | 128 const DictionaryValue* plugin_dict) { |
110 DCHECK(!installers_[identifier]); | 129 DCHECK(!installers_[identifier]); |
111 std::string url; | 130 std::string url; |
112 bool success = plugin_dict->GetString("url", &url); | 131 bool success = plugin_dict->GetString("url", &url); |
113 DCHECK(success); | 132 DCHECK(success); |
114 std::string help_url; | 133 std::string help_url; |
115 plugin_dict->GetString("help_url", &help_url); | 134 plugin_dict->GetString("help_url", &help_url); |
116 string16 name; | 135 string16 name; |
117 success = plugin_dict->GetString("name", &name); | 136 success = plugin_dict->GetString("name", &name); |
118 DCHECK(success); | 137 DCHECK(success); |
119 bool display_url = false; | 138 bool display_url = false; |
120 plugin_dict->GetBoolean("displayurl", &display_url); | 139 plugin_dict->GetBoolean("displayurl", &display_url); |
121 PluginInstaller*installer = new PluginInstaller(identifier, | 140 bool requires_authorization = true; |
122 GURL(url), | 141 plugin_dict->GetBoolean("requires_authorization", &display_url); |
123 GURL(help_url), | 142 PluginInstaller* installer = new PluginInstaller(identifier, |
124 name, | 143 GURL(url), |
125 display_url); | 144 GURL(help_url), |
| 145 name, |
| 146 display_url, |
| 147 requires_authorization); |
126 installers_[identifier] = installer; | 148 installers_[identifier] = installer; |
127 return installer; | 149 return installer; |
128 } | 150 } |
129 | |
130 PluginInstaller* PluginFinder::FindPluginInternal( | |
131 const std::string& mime_type, | |
132 const std::string& language) { | |
133 if (!g_browser_process->local_state()->GetBoolean( | |
134 prefs::kDisablePluginFinder)) { | |
135 for (ListValue::const_iterator plugin_it = plugin_list_->begin(); | |
136 plugin_it != plugin_list_->end(); ++plugin_it) { | |
137 const base::DictionaryValue* plugin = NULL; | |
138 if (!(*plugin_it)->GetAsDictionary(&plugin)) { | |
139 NOTREACHED(); | |
140 continue; | |
141 } | |
142 std::string language_str; | |
143 bool success = plugin->GetString("lang", &language_str); | |
144 DCHECK(success); | |
145 if (language_str != language) | |
146 continue; | |
147 ListValue* mime_types = NULL; | |
148 success = plugin->GetList("mime_types", &mime_types); | |
149 DCHECK(success); | |
150 for (ListValue::const_iterator mime_type_it = mime_types->begin(); | |
151 mime_type_it != mime_types->end(); ++mime_type_it) { | |
152 std::string mime_type_str; | |
153 success = (*mime_type_it)->GetAsString(&mime_type_str); | |
154 DCHECK(success); | |
155 if (mime_type_str == mime_type) { | |
156 std::string identifier; | |
157 bool success = plugin->GetString("identifier", &identifier); | |
158 DCHECK(success); | |
159 std::map<std::string, PluginInstaller*>::const_iterator it = | |
160 installers_.find(identifier); | |
161 if (it != installers_.end()) | |
162 return it->second; | |
163 return CreateInstaller(identifier, plugin); | |
164 } | |
165 } | |
166 } | |
167 } | |
168 return NULL; | |
169 } | |
OLD | NEW |