OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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/dom_ui/plugins_ui.h" |
| 6 |
| 7 #include "app/l10n_util.h" |
| 8 #include "app/resource_bundle.h" |
| 9 #include "base/singleton.h" |
| 10 #include "base/values.h" |
| 11 #include "chrome/browser/chrome_thread.h" |
| 12 #include "chrome/browser/dom_ui/chrome_url_data_manager.h" |
| 13 #include "chrome/browser/pref_service.h" |
| 14 #include "chrome/browser/profile.h" |
| 15 #include "chrome/common/jstemplate_builder.h" |
| 16 #include "chrome/common/pref_names.h" |
| 17 #include "chrome/common/url_constants.h" |
| 18 #include "grit/browser_resources.h" |
| 19 #include "grit/generated_resources.h" |
| 20 #include "grit/theme_resources.h" |
| 21 #include "webkit/glue/plugins/plugin_list.h" |
| 22 |
| 23 namespace { |
| 24 |
| 25 /////////////////////////////////////////////////////////////////////////////// |
| 26 // |
| 27 // PluginsHTMLSource |
| 28 // |
| 29 /////////////////////////////////////////////////////////////////////////////// |
| 30 |
| 31 class PluginsUIHTMLSource : public ChromeURLDataManager::DataSource { |
| 32 public: |
| 33 PluginsUIHTMLSource() |
| 34 : DataSource(chrome::kChromeUIPluginsHost, MessageLoop::current()) {} |
| 35 |
| 36 // Called when the network layer has requested a resource underneath |
| 37 // the path we registered. |
| 38 virtual void StartDataRequest(const std::string& path, |
| 39 bool is_off_the_record, |
| 40 int request_id); |
| 41 virtual std::string GetMimeType(const std::string&) const { |
| 42 return "text/html"; |
| 43 } |
| 44 |
| 45 private: |
| 46 ~PluginsUIHTMLSource() {} |
| 47 |
| 48 DISALLOW_COPY_AND_ASSIGN(PluginsUIHTMLSource); |
| 49 }; |
| 50 |
| 51 void PluginsUIHTMLSource::StartDataRequest(const std::string& path, |
| 52 bool is_off_the_record, int request_id) { |
| 53 // Strings used in the JsTemplate file. |
| 54 DictionaryValue localized_strings; |
| 55 localized_strings.SetString(L"pluginsTitle", |
| 56 l10n_util::GetString(IDS_PLUGINS_TITLE)); |
| 57 localized_strings.SetString(L"pluginsDetailsModeLink", |
| 58 l10n_util::GetString(IDS_PLUGINS_DETAILS_MODE_LINK)); |
| 59 localized_strings.SetString(L"pluginsNoneInstalled", |
| 60 l10n_util::GetString(IDS_PLUGINS_NONE_INSTALLED)); |
| 61 localized_strings.SetString(L"pluginDisabled", |
| 62 l10n_util::GetString(IDS_PLUGINS_DISABLED_PLUGIN)); |
| 63 localized_strings.SetString(L"pluginVersion", |
| 64 l10n_util::GetString(IDS_PLUGINS_VERSION)); |
| 65 localized_strings.SetString(L"pluginDescription", |
| 66 l10n_util::GetString(IDS_PLUGINS_DESCRIPTION)); |
| 67 localized_strings.SetString(L"pluginPath", |
| 68 l10n_util::GetString(IDS_PLUGINS_PATH)); |
| 69 localized_strings.SetString(L"pluginMimeTypes", |
| 70 l10n_util::GetString(IDS_PLUGINS_MIME_TYPES)); |
| 71 localized_strings.SetString(L"pluginMimeTypesMimeType", |
| 72 l10n_util::GetString(IDS_PLUGINS_MIME_TYPES_MIME_TYPE)); |
| 73 localized_strings.SetString(L"pluginMimeTypesDescription", |
| 74 l10n_util::GetString(IDS_PLUGINS_MIME_TYPES_DESCRIPTION)); |
| 75 localized_strings.SetString(L"pluginMimeTypesFileExtensions", |
| 76 l10n_util::GetString(IDS_PLUGINS_MIME_TYPES_FILE_EXTENSIONS)); |
| 77 localized_strings.SetString(L"disable", |
| 78 l10n_util::GetString(IDS_PLUGINS_DISABLE)); |
| 79 localized_strings.SetString(L"enable", |
| 80 l10n_util::GetString(IDS_PLUGINS_ENABLE)); |
| 81 |
| 82 ChromeURLDataManager::DataSource::SetFontAndTextDirection(&localized_strings); |
| 83 |
| 84 static const base::StringPiece plugins_html( |
| 85 ResourceBundle::GetSharedInstance().GetRawDataResource(IDR_PLUGINS_HTML)); |
| 86 std::string full_html(plugins_html.data(), plugins_html.size()); |
| 87 jstemplate_builder::AppendJsonHtml(&localized_strings, &full_html); |
| 88 jstemplate_builder::AppendI18nTemplateSourceHtml(&full_html); |
| 89 jstemplate_builder::AppendI18nTemplateProcessHtml(&full_html); |
| 90 jstemplate_builder::AppendJsTemplateSourceHtml(&full_html); |
| 91 |
| 92 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); |
| 93 html_bytes->data.resize(full_html.size()); |
| 94 std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin()); |
| 95 |
| 96 SendResponse(request_id, html_bytes); |
| 97 } |
| 98 |
| 99 //////////////////////////////////////////////////////////////////////////////// |
| 100 // |
| 101 // PluginsDOMHandler |
| 102 // |
| 103 //////////////////////////////////////////////////////////////////////////////// |
| 104 |
| 105 // The handler for Javascript messages related to the "history" view. |
| 106 // TODO(viettrungluu): Make plugin list updates notify, and then observe |
| 107 // changes; maybe replumb plugin list through plugin service? |
| 108 // <http://crbug.com/39101> |
| 109 class PluginsDOMHandler : public DOMMessageHandler { |
| 110 public: |
| 111 PluginsDOMHandler() {} |
| 112 virtual ~PluginsDOMHandler() {} |
| 113 |
| 114 // DOMMessageHandler implementation. |
| 115 virtual void RegisterMessages(); |
| 116 |
| 117 // Callback for the "requestPluginsData" message. |
| 118 void HandleRequestPluginsData(const Value* value); |
| 119 |
| 120 // Callback for the "enablePlugin" message. |
| 121 void HandleEnablePluginMessage(const Value* value); |
| 122 |
| 123 private: |
| 124 // Creates a dictionary containing all the information about the given plugin; |
| 125 // this is put into the list to "return" for the "requestPluginsData" message. |
| 126 DictionaryValue* CreatePluginDetailValue(const WebPluginInfo& plugin); |
| 127 |
| 128 // Creates a dictionary containing the important parts of the information |
| 129 // about the given plugin; this is put into a list and saved in prefs. |
| 130 DictionaryValue* CreatePluginSummaryValue(const WebPluginInfo& plugin); |
| 131 |
| 132 // Update the user preferences to reflect the current (user-selected) state of |
| 133 // plugins. |
| 134 void UpdatePreferences(); |
| 135 |
| 136 DISALLOW_COPY_AND_ASSIGN(PluginsDOMHandler); |
| 137 }; |
| 138 |
| 139 void PluginsDOMHandler::RegisterMessages() { |
| 140 dom_ui_->RegisterMessageCallback("requestPluginsData", |
| 141 NewCallback(this, &PluginsDOMHandler::HandleRequestPluginsData)); |
| 142 dom_ui_->RegisterMessageCallback("enablePlugin", |
| 143 NewCallback(this, &PluginsDOMHandler::HandleEnablePluginMessage)); |
| 144 } |
| 145 |
| 146 void PluginsDOMHandler::HandleRequestPluginsData(const Value* value) { |
| 147 DictionaryValue* results = new DictionaryValue(); |
| 148 |
| 149 // Add plugins to the results structure. |
| 150 ListValue* plugins_list = new ListValue(); |
| 151 |
| 152 std::vector<WebPluginInfo> plugins; |
| 153 NPAPI::PluginList::Singleton()->GetPlugins(false, &plugins); |
| 154 |
| 155 for (std::vector<WebPluginInfo>::const_iterator it = plugins.begin(); |
| 156 it != plugins.end(); |
| 157 ++it) { |
| 158 plugins_list->Append(CreatePluginDetailValue(*it)); |
| 159 } |
| 160 results->Set(L"plugins", plugins_list); |
| 161 |
| 162 dom_ui_->CallJavascriptFunction(L"returnPluginsData", *results); |
| 163 } |
| 164 |
| 165 void PluginsDOMHandler::HandleEnablePluginMessage(const Value* value) { |
| 166 CHECK(value->IsType(Value::TYPE_LIST)); |
| 167 const ListValue* list = static_cast<const ListValue*>(value); |
| 168 CHECK(list->GetSize() == 2); |
| 169 FilePath::StringType plugin_path; |
| 170 std::string enable_str; |
| 171 CHECK(list->GetString(0, &plugin_path)); |
| 172 CHECK(list->GetString(1, &enable_str)); |
| 173 if (enable_str == "true") |
| 174 NPAPI::PluginList::Singleton()->EnablePlugin(FilePath(plugin_path)); |
| 175 else |
| 176 NPAPI::PluginList::Singleton()->DisablePlugin(FilePath(plugin_path)); |
| 177 |
| 178 // TODO(viettrungluu): It's morally wrong to do this here (it should be done |
| 179 // by the plugins service), and we might also want to ensure that the plugins |
| 180 // list is always written to prefs even when the user hasn't disabled a |
| 181 // plugin. This will require refactoring the plugin list and service. |
| 182 // <http://crbug.com/39101> |
| 183 UpdatePreferences(); |
| 184 } |
| 185 |
| 186 DictionaryValue* PluginsDOMHandler::CreatePluginDetailValue( |
| 187 const WebPluginInfo& plugin) { |
| 188 DictionaryValue* plugin_data = new DictionaryValue(); |
| 189 plugin_data->SetString(L"path", plugin.path.value()); |
| 190 plugin_data->SetString(L"name", plugin.name); |
| 191 plugin_data->SetString(L"version", plugin.version); |
| 192 plugin_data->SetString(L"description", plugin.desc); |
| 193 plugin_data->SetBoolean(L"enabled", plugin.enabled); |
| 194 |
| 195 ListValue* mime_types = new ListValue(); |
| 196 for (std::vector<WebPluginMimeType>::const_iterator type_it = |
| 197 plugin.mime_types.begin(); |
| 198 type_it != plugin.mime_types.end(); |
| 199 ++type_it) { |
| 200 DictionaryValue* mime_type = new DictionaryValue(); |
| 201 mime_type->SetString(L"mimeType", type_it->mime_type); |
| 202 mime_type->SetString(L"description", type_it->description); |
| 203 |
| 204 ListValue* file_extensions = new ListValue(); |
| 205 for (std::vector<std::string>::const_iterator ext_it = |
| 206 type_it->file_extensions.begin(); |
| 207 ext_it != type_it->file_extensions.end(); |
| 208 ++ext_it) { |
| 209 file_extensions->Append(new StringValue(*ext_it)); |
| 210 } |
| 211 mime_type->Set(L"fileExtensions", file_extensions); |
| 212 |
| 213 mime_types->Append(mime_type); |
| 214 } |
| 215 plugin_data->Set(L"mimeTypes", mime_types); |
| 216 |
| 217 return plugin_data; |
| 218 } |
| 219 |
| 220 DictionaryValue* PluginsDOMHandler::CreatePluginSummaryValue( |
| 221 const WebPluginInfo& plugin) { |
| 222 DictionaryValue* plugin_data = new DictionaryValue(); |
| 223 plugin_data->SetString(L"path", plugin.path.value()); |
| 224 plugin_data->SetString(L"name", plugin.name); |
| 225 plugin_data->SetString(L"version", plugin.version); |
| 226 plugin_data->SetBoolean(L"enabled", plugin.enabled); |
| 227 return plugin_data; |
| 228 } |
| 229 |
| 230 void PluginsDOMHandler::UpdatePreferences() { |
| 231 ListValue* plugins_list = dom_ui_->GetProfile()->GetPrefs()->GetMutableList( |
| 232 prefs::kPluginsPluginsList); |
| 233 plugins_list->Clear(); |
| 234 |
| 235 std::vector<WebPluginInfo> plugins; |
| 236 NPAPI::PluginList::Singleton()->GetPlugins(false, &plugins); |
| 237 |
| 238 for (std::vector<WebPluginInfo>::const_iterator it = plugins.begin(); |
| 239 it != plugins.end(); |
| 240 ++it) { |
| 241 plugins_list->Append(CreatePluginSummaryValue(*it)); |
| 242 } |
| 243 } |
| 244 |
| 245 } // namespace |
| 246 |
| 247 /////////////////////////////////////////////////////////////////////////////// |
| 248 // |
| 249 // PluginsUI |
| 250 // |
| 251 /////////////////////////////////////////////////////////////////////////////// |
| 252 |
| 253 PluginsUI::PluginsUI(TabContents* contents) : DOMUI(contents) { |
| 254 AddMessageHandler((new PluginsDOMHandler())->Attach(this)); |
| 255 |
| 256 PluginsUIHTMLSource* html_source = new PluginsUIHTMLSource(); |
| 257 |
| 258 // Set up the chrome://plugins/ source. |
| 259 ChromeThread::PostTask( |
| 260 ChromeThread::IO, FROM_HERE, |
| 261 NewRunnableMethod(Singleton<ChromeURLDataManager>::get(), |
| 262 &ChromeURLDataManager::AddDataSource, |
| 263 make_scoped_refptr(html_source))); |
| 264 } |
| 265 |
| 266 |
| 267 // static |
| 268 RefCountedMemory* PluginsUI::GetFaviconResourceBytes() { |
| 269 return ResourceBundle::GetSharedInstance(). |
| 270 LoadDataResourceBytes(IDR_PLUGIN); |
| 271 } |
| 272 |
| 273 // static |
| 274 void PluginsUI::RegisterUserPrefs(PrefService* prefs) { |
| 275 prefs->RegisterListPref(prefs::kPluginsPluginsList); |
| 276 } |
OLD | NEW |