| 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 | |
| 30 // static | 21 // static |
| 31 PluginFinder* PluginFinder::GetInstance() { | 22 PluginFinder* PluginFinder::GetInstance() { |
| 32 // PluginFinder::GetInstance() is the only method that's allowed to call | |
| 33 // Singleton<PluginFinder>::get(). | |
| 34 return Singleton<PluginFinder>::get(); | 23 return Singleton<PluginFinder>::get(); |
| 35 } | 24 } |
| 36 | 25 |
| 37 PluginFinder::PluginFinder() : plugin_list_(LoadPluginList()) { | 26 PluginFinder::PluginFinder() : plugin_list_(LoadPluginList()) { |
| 38 if (!plugin_list_.get()) { | 27 if (!plugin_list_.get()) { |
| 39 NOTREACHED(); | 28 NOTREACHED(); |
| 40 plugin_list_.reset(new DictionaryValue()); | 29 plugin_list_.reset(new base::ListValue()); |
| 41 } | 30 } |
| 42 } | 31 } |
| 43 | 32 |
| 44 // static | 33 // static |
| 45 scoped_ptr<DictionaryValue> PluginFinder::LoadPluginList() { | 34 scoped_ptr<base::ListValue> PluginFinder::LoadPluginList() { |
| 46 return scoped_ptr<DictionaryValue>(LoadPluginListInternal()); | 35 return scoped_ptr<base::ListValue>(LoadPluginListInternal()).Pass(); |
| 47 } | 36 } |
| 48 | 37 |
| 49 DictionaryValue* PluginFinder::LoadPluginListInternal() { | 38 base::ListValue* PluginFinder::LoadPluginListInternal() { |
| 50 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) | 39 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) |
| 51 base::StringPiece json_resource( | 40 base::StringPiece json_resource( |
| 52 ResourceBundle::GetSharedInstance().GetRawDataResource( | 41 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 53 IDR_PLUGIN_DB_JSON)); | 42 IDR_PLUGIN_DB_JSON)); |
| 54 bool allow_trailing_comma = false; | 43 bool allow_trailing_comma = false; |
| 55 std::string error_str; | 44 std::string error_str; |
| 56 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError( | 45 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError( |
| 57 json_resource.as_string(), | 46 json_resource.as_string(), |
| 58 allow_trailing_comma, | 47 allow_trailing_comma, |
| 59 NULL, | 48 NULL, |
| 60 &error_str)); | 49 &error_str)); |
| 61 if (!value.get()) { | 50 if (!value.get()) { |
| 62 DLOG(ERROR) << error_str; | 51 DLOG(ERROR) << error_str; |
| 63 return NULL; | 52 return NULL; |
| 64 } | 53 } |
| 65 if (value->GetType() != base::Value::TYPE_DICTIONARY) | 54 base::DictionaryValue* dict = NULL; |
| 55 if (!value->GetAsDictionary(&dict)) |
| 66 return NULL; | 56 return NULL; |
| 67 return static_cast<base::DictionaryValue*>(value.release()); | 57 base::ListValue* list = NULL; |
| 58 if (!dict->GetList("plugins", &list)) |
| 59 return NULL; |
| 60 return list->DeepCopy(); |
| 68 #else | 61 #else |
| 69 return new DictionaryValue(); | 62 return new base::ListValue(); |
| 70 #endif | 63 #endif |
| 71 } | 64 } |
| 72 | 65 |
| 73 PluginFinder::~PluginFinder() { | 66 PluginFinder::~PluginFinder() { |
| 74 STLDeleteValues(&installers_); | 67 STLDeleteValues(&installers_); |
| 75 } | 68 } |
| 76 | 69 |
| 77 PluginInstaller* PluginFinder::FindPlugin(const std::string& mime_type, | 70 void PluginFinder::FindPlugin( |
| 78 const std::string& language) { | 71 const std::string& mime_type, |
| 79 if (g_browser_process->local_state()->GetBoolean(prefs::kDisablePluginFinder)) | 72 const std::string& language, |
| 80 return NULL; | 73 const FindPluginCallback& callback) { |
| 81 for (DictionaryValue::Iterator plugin_it(*plugin_list_); | 74 PluginInstaller* installer = FindPluginInternal(mime_type, language); |
| 82 plugin_it.HasNext(); plugin_it.Advance()) { | 75 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, installer)); |
| 83 const DictionaryValue* plugin = NULL; | 76 } |
| 84 if (!plugin_it.value().GetAsDictionary(&plugin)) { | 77 |
| 85 NOTREACHED(); | 78 void PluginFinder::FindPluginWithIdentifier( |
| 86 continue; | 79 const std::string& identifier, |
| 87 } | 80 const FindPluginCallback& found_callback) { |
| 88 std::string language_str; | 81 PluginInstaller* installer = NULL; |
| 89 bool success = plugin->GetString("lang", &language_str); | 82 std::map<std::string, PluginInstaller*>::const_iterator it = |
| 90 DCHECK(success); | 83 installers_.find(identifier); |
| 91 if (language_str != language) | 84 if (it != installers_.end()) { |
| 92 continue; | 85 installer = it->second; |
| 93 ListValue* mime_types = NULL; | 86 } else { |
| 94 plugin->GetList("mime_types", &mime_types); | 87 for (ListValue::const_iterator plugin_it = plugin_list_->begin(); |
| 95 DCHECK(success); | 88 plugin_it != plugin_list_->end(); ++plugin_it) { |
| 96 for (ListValue::const_iterator mime_type_it = mime_types->begin(); | 89 const base::DictionaryValue* plugin = NULL; |
| 97 mime_type_it != mime_types->end(); ++mime_type_it) { | 90 if (!(*plugin_it)->GetAsDictionary(&plugin)) { |
| 98 std::string mime_type_str; | 91 NOTREACHED(); |
| 99 success = (*mime_type_it)->GetAsString(&mime_type_str); | 92 continue; |
| 93 } |
| 94 std::string id; |
| 95 bool success = plugin->GetString("identifier", &id); |
| 100 DCHECK(success); | 96 DCHECK(success); |
| 101 if (mime_type_str == mime_type) { | 97 if (id == identifier) { |
| 102 std::string identifier = plugin_it.key(); | 98 installer = CreateInstaller(identifier, plugin); |
| 103 std::map<std::string, PluginInstaller*>::const_iterator installer = | 99 break; |
| 104 installers_.find(identifier); | |
| 105 if (installer != installers_.end()) | |
| 106 return installer->second; | |
| 107 return CreateInstaller(identifier, plugin); | |
| 108 } | 100 } |
| 109 } | 101 } |
| 110 } | 102 } |
| 111 return NULL; | 103 MessageLoop::current()->PostTask(FROM_HERE, |
| 112 } | 104 base::Bind(found_callback, installer)); |
| 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; | |
| 124 } | 105 } |
| 125 | 106 |
| 126 PluginInstaller* PluginFinder::CreateInstaller( | 107 PluginInstaller* PluginFinder::CreateInstaller( |
| 127 const std::string& identifier, | 108 const std::string& identifier, |
| 128 const DictionaryValue* plugin_dict) { | 109 const base::DictionaryValue* plugin_dict) { |
| 129 DCHECK(!installers_[identifier]); | 110 DCHECK(!installers_[identifier]); |
| 130 std::string url; | 111 std::string url; |
| 131 bool success = plugin_dict->GetString("url", &url); | 112 bool success = plugin_dict->GetString("url", &url); |
| 132 DCHECK(success); | 113 DCHECK(success); |
| 133 std::string help_url; | 114 std::string help_url; |
| 134 plugin_dict->GetString("help_url", &help_url); | 115 plugin_dict->GetString("help_url", &help_url); |
| 135 string16 name; | 116 string16 name; |
| 136 success = plugin_dict->GetString("name", &name); | 117 success = plugin_dict->GetString("name", &name); |
| 137 DCHECK(success); | 118 DCHECK(success); |
| 138 bool display_url = false; | 119 bool display_url = false; |
| 139 plugin_dict->GetBoolean("displayurl", &display_url); | 120 plugin_dict->GetBoolean("displayurl", &display_url); |
| 140 bool requires_authorization = true; | 121 PluginInstaller*installer = new PluginInstaller(identifier, |
| 141 plugin_dict->GetBoolean("requires_authorization", &requires_authorization); | 122 GURL(url), |
| 142 PluginInstaller* installer = new PluginInstaller(identifier, | 123 GURL(help_url), |
| 143 GURL(url), | 124 name, |
| 144 GURL(help_url), | 125 display_url); |
| 145 name, | |
| 146 display_url, | |
| 147 requires_authorization); | |
| 148 installers_[identifier] = installer; | 126 installers_[identifier] = installer; |
| 149 return installer; | 127 return installer; |
| 150 } | 128 } |
| 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 |