| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/ui/webui/plugins_ui.h" | 5 #include "chrome/browser/ui/webui/plugins_ui.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/memory/singleton.h" | 11 #include "base/memory/singleton.h" |
| 12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 13 #include "base/path_service.h" | 13 #include "base/path_service.h" |
| 14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
| 15 #include "base/values.h" | 15 #include "base/values.h" |
| 16 #include "chrome/browser/plugin_prefs.h" | 16 #include "chrome/browser/plugin_updater.h" |
| 17 #include "chrome/browser/prefs/pref_member.h" | 17 #include "chrome/browser/prefs/pref_member.h" |
| 18 #include "chrome/browser/prefs/pref_service.h" | 18 #include "chrome/browser/prefs/pref_service.h" |
| 19 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
| 20 #include "chrome/browser/ui/browser.h" | 20 #include "chrome/browser/ui/browser.h" |
| 21 #include "chrome/browser/ui/browser_window.h" | 21 #include "chrome/browser/ui/browser_window.h" |
| 22 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | 22 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" |
| 23 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" | 23 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" |
| 24 #include "chrome/common/chrome_content_client.h" | 24 #include "chrome/common/chrome_content_client.h" |
| 25 #include "chrome/common/chrome_paths.h" | 25 #include "chrome/common/chrome_paths.h" |
| 26 #include "chrome/common/pref_names.h" | 26 #include "chrome/common/pref_names.h" |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 | 115 |
| 116 // Calback for the "getShowDetails" message. | 116 // Calback for the "getShowDetails" message. |
| 117 void HandleGetShowDetails(const ListValue* args); | 117 void HandleGetShowDetails(const ListValue* args); |
| 118 | 118 |
| 119 // NotificationObserver method overrides | 119 // NotificationObserver method overrides |
| 120 virtual void Observe(int type, | 120 virtual void Observe(int type, |
| 121 const NotificationSource& source, | 121 const NotificationSource& source, |
| 122 const NotificationDetails& details) OVERRIDE; | 122 const NotificationDetails& details) OVERRIDE; |
| 123 | 123 |
| 124 private: | 124 private: |
| 125 // This extra wrapper is used to ensure we don't leak the ListValue* pointer |
| 126 // if the PluginsDOMHandler object goes away before the task on the UI thread |
| 127 // to give it the plugin list runs. |
| 128 struct ListWrapper { |
| 129 ListValue* list; |
| 130 }; |
| 125 // Loads the plugins on the FILE thread. | 131 // Loads the plugins on the FILE thread. |
| 126 static void LoadPluginsOnFileThread( | 132 static void LoadPluginsOnFileThread(ListWrapper* wrapper, Task* task); |
| 127 std::vector<webkit::npapi::PluginGroup>* groups, Task* task); | |
| 128 | 133 |
| 129 // Used in conjunction with ListWrapper to avoid any memory leaks. | 134 // Used in conjunction with ListWrapper to avoid any memory leaks. |
| 130 static void EnsurePluginGroupsDeleted( | 135 static void EnsureListDeleted(ListWrapper* wrapper); |
| 131 std::vector<webkit::npapi::PluginGroup>* groups); | |
| 132 | 136 |
| 133 // Call this to start getting the plugins on the UI thread. | 137 // Call this to start getting the plugins on the UI thread. |
| 134 void LoadPlugins(); | 138 void LoadPlugins(); |
| 135 | 139 |
| 136 // Called on the UI thread when the plugin information is ready. | 140 // Called on the UI thread when the plugin information is ready. |
| 137 void PluginsLoaded(const std::vector<webkit::npapi::PluginGroup>* groups); | 141 void PluginsLoaded(ListWrapper* wrapper); |
| 138 | 142 |
| 139 NotificationRegistrar registrar_; | 143 NotificationRegistrar registrar_; |
| 140 | 144 |
| 141 ScopedRunnableMethodFactory<PluginsDOMHandler> get_plugins_factory_; | 145 ScopedRunnableMethodFactory<PluginsDOMHandler> get_plugins_factory_; |
| 142 | 146 |
| 143 // This pref guards the value whether about:plugins is in the details mode or | 147 // This pref guards the value whether about:plugins is in the details mode or |
| 144 // not. | 148 // not. |
| 145 BooleanPrefMember show_details_; | 149 BooleanPrefMember show_details_; |
| 146 | 150 |
| 147 DISALLOW_COPY_AND_ASSIGN(PluginsDOMHandler); | 151 DISALLOW_COPY_AND_ASSIGN(PluginsDOMHandler); |
| 148 }; | 152 }; |
| 149 | 153 |
| 150 PluginsDOMHandler::PluginsDOMHandler() | 154 PluginsDOMHandler::PluginsDOMHandler() |
| 151 : ALLOW_THIS_IN_INITIALIZER_LIST(get_plugins_factory_(this)) { | 155 : ALLOW_THIS_IN_INITIALIZER_LIST(get_plugins_factory_(this)) { |
| 152 registrar_.Add(this, | 156 registrar_.Add(this, |
| 153 content::NOTIFICATION_PLUGIN_ENABLE_STATUS_CHANGED, | 157 content::NOTIFICATION_PLUGIN_ENABLE_STATUS_CHANGED, |
| 154 NotificationService::AllSources()); | 158 NotificationService::AllSources()); |
| 155 } | 159 } |
| 156 | 160 |
| 157 WebUIMessageHandler* PluginsDOMHandler::Attach(WebUI* web_ui) { | 161 WebUIMessageHandler* PluginsDOMHandler::Attach(WebUI* web_ui) { |
| 158 PrefService* prefs = Profile::FromWebUI(web_ui)->GetPrefs(); | 162 PrefService* prefs = Profile::FromWebUI(web_ui)->GetPrefs(); |
| 159 | 163 |
| 160 show_details_.Init(prefs::kPluginsShowDetails, prefs, NULL); | 164 show_details_.Init(prefs::kPluginsShowDetails, prefs, this); |
| 161 | 165 |
| 162 return WebUIMessageHandler::Attach(web_ui); | 166 return WebUIMessageHandler::Attach(web_ui); |
| 163 } | 167 } |
| 164 | 168 |
| 165 void PluginsDOMHandler::RegisterMessages() { | 169 void PluginsDOMHandler::RegisterMessages() { |
| 166 web_ui_->RegisterMessageCallback("requestPluginsData", | 170 web_ui_->RegisterMessageCallback("requestPluginsData", |
| 167 NewCallback(this, &PluginsDOMHandler::HandleRequestPluginsData)); | 171 NewCallback(this, &PluginsDOMHandler::HandleRequestPluginsData)); |
| 168 web_ui_->RegisterMessageCallback("enablePlugin", | 172 web_ui_->RegisterMessageCallback("enablePlugin", |
| 169 NewCallback(this, &PluginsDOMHandler::HandleEnablePluginMessage)); | 173 NewCallback(this, &PluginsDOMHandler::HandleEnablePluginMessage)); |
| 170 web_ui_->RegisterMessageCallback("saveShowDetailsToPrefs", | 174 web_ui_->RegisterMessageCallback("saveShowDetailsToPrefs", |
| (...skipping 20 matching lines...) Expand all Loading... |
| 191 // JavaScript). | 195 // JavaScript). |
| 192 if (args->GetSize() != 3) | 196 if (args->GetSize() != 3) |
| 193 return; | 197 return; |
| 194 | 198 |
| 195 std::string enable_str; | 199 std::string enable_str; |
| 196 std::string is_group_str; | 200 std::string is_group_str; |
| 197 if (!args->GetString(1, &enable_str) || !args->GetString(2, &is_group_str)) | 201 if (!args->GetString(1, &enable_str) || !args->GetString(2, &is_group_str)) |
| 198 return; | 202 return; |
| 199 bool enable = enable_str == "true"; | 203 bool enable = enable_str == "true"; |
| 200 | 204 |
| 201 PluginPrefs* plugin_prefs = PluginPrefs::GetForProfile(profile); | 205 PluginUpdater* plugin_updater = PluginUpdater::GetInstance(); |
| 202 if (is_group_str == "true") { | 206 if (is_group_str == "true") { |
| 203 string16 group_name; | 207 string16 group_name; |
| 204 if (!args->GetString(0, &group_name)) | 208 if (!args->GetString(0, &group_name)) |
| 205 return; | 209 return; |
| 206 | 210 |
| 207 plugin_prefs->EnablePluginGroup(enable, group_name); | 211 plugin_updater->EnablePluginGroup(enable, group_name); |
| 208 if (enable) { | 212 if (enable) { |
| 209 // See http://crbug.com/50105 for background. | 213 // See http://crbug.com/50105 for background. |
| 210 string16 adobereader = ASCIIToUTF16( | 214 string16 adobereader = ASCIIToUTF16( |
| 211 webkit::npapi::PluginGroup::kAdobeReaderGroupName); | 215 webkit::npapi::PluginGroup::kAdobeReaderGroupName); |
| 212 string16 internalpdf = | 216 string16 internalpdf = |
| 213 ASCIIToUTF16(chrome::ChromeContentClient::kPDFPluginName); | 217 ASCIIToUTF16(chrome::ChromeContentClient::kPDFPluginName); |
| 214 if (group_name == adobereader) { | 218 if (group_name == adobereader) { |
| 215 plugin_prefs->EnablePluginGroup(false, internalpdf); | 219 plugin_updater->EnablePluginGroup(false, internalpdf); |
| 216 } else if (group_name == internalpdf) { | 220 } else if (group_name == internalpdf) { |
| 217 plugin_prefs->EnablePluginGroup(false, adobereader); | 221 plugin_updater->EnablePluginGroup(false, adobereader); |
| 218 } | 222 } |
| 219 } | 223 } |
| 220 } else { | 224 } else { |
| 221 FilePath::StringType file_path; | 225 FilePath::StringType file_path; |
| 222 if (!args->GetString(0, &file_path)) | 226 if (!args->GetString(0, &file_path)) |
| 223 return; | 227 return; |
| 224 | 228 |
| 225 plugin_prefs->EnablePlugin(enable, FilePath(file_path)); | 229 plugin_updater->EnablePlugin(enable, file_path); |
| 226 } | 230 } |
| 227 | 231 |
| 228 // TODO(viettrungluu): We might also want to ensure that the plugins | 232 // TODO(viettrungluu): We might also want to ensure that the plugins |
| 229 // list is always written to prefs even when the user hasn't disabled a | 233 // list is always written to prefs even when the user hasn't disabled a |
| 230 // plugin. <http://crbug.com/39101> | 234 // plugin. <http://crbug.com/39101> |
| 231 plugin_prefs->UpdatePreferences(0); | 235 plugin_updater->UpdatePreferences(profile, 0); |
| 232 } | 236 } |
| 233 | 237 |
| 234 void PluginsDOMHandler::HandleSaveShowDetailsToPrefs(const ListValue* args) { | 238 void PluginsDOMHandler::HandleSaveShowDetailsToPrefs(const ListValue* args) { |
| 235 std::string details_mode; | 239 std::string details_mode; |
| 236 if (!args->GetString(0, &details_mode)) { | 240 if (!args->GetString(0, &details_mode)) { |
| 237 NOTREACHED(); | 241 NOTREACHED(); |
| 238 return; | 242 return; |
| 239 } | 243 } |
| 240 show_details_.SetValue(details_mode == "true"); | 244 show_details_.SetValue(details_mode == "true"); |
| 241 } | 245 } |
| 242 | 246 |
| 243 void PluginsDOMHandler::HandleGetShowDetails(const ListValue* args) { | 247 void PluginsDOMHandler::HandleGetShowDetails(const ListValue* args) { |
| 244 base::FundamentalValue show_details(show_details_.GetValue()); | 248 base::FundamentalValue show_details(show_details_.GetValue()); |
| 245 web_ui_->CallJavascriptFunction("loadShowDetailsFromPrefs", show_details); | 249 web_ui_->CallJavascriptFunction("loadShowDetailsFromPrefs", show_details); |
| 246 } | 250 } |
| 247 | 251 |
| 248 void PluginsDOMHandler::Observe(int type, | 252 void PluginsDOMHandler::Observe(int type, |
| 249 const NotificationSource& source, | 253 const NotificationSource& source, |
| 250 const NotificationDetails& details) { | 254 const NotificationDetails& details) { |
| 251 DCHECK_EQ(content::NOTIFICATION_PLUGIN_ENABLE_STATUS_CHANGED, type); | 255 DCHECK_EQ(content::NOTIFICATION_PLUGIN_ENABLE_STATUS_CHANGED, type); |
| 252 LoadPlugins(); | 256 LoadPlugins(); |
| 253 } | 257 } |
| 254 | 258 |
| 255 void PluginsDOMHandler::LoadPluginsOnFileThread( | 259 void PluginsDOMHandler::LoadPluginsOnFileThread(ListWrapper* wrapper, |
| 256 std::vector<webkit::npapi::PluginGroup>* groups, | 260 Task* task) { |
| 257 Task* task) { | 261 wrapper->list = PluginUpdater::GetInstance()->GetPluginGroupsData(); |
| 258 webkit::npapi::PluginList::Singleton()->GetPluginGroups(true, groups); | |
| 259 | |
| 260 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, task); | 262 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, task); |
| 261 BrowserThread::PostTask( | 263 BrowserThread::PostTask( |
| 262 BrowserThread::UI, | 264 BrowserThread::UI, |
| 263 FROM_HERE, | 265 FROM_HERE, |
| 264 NewRunnableFunction(&PluginsDOMHandler::EnsurePluginGroupsDeleted, | 266 NewRunnableFunction(&PluginsDOMHandler::EnsureListDeleted, wrapper)); |
| 265 groups)); | |
| 266 } | 267 } |
| 267 | 268 |
| 268 void PluginsDOMHandler::EnsurePluginGroupsDeleted( | 269 void PluginsDOMHandler::EnsureListDeleted(ListWrapper* wrapper) { |
| 269 std::vector<webkit::npapi::PluginGroup>* groups) { | 270 delete wrapper->list; |
| 270 delete groups; | 271 delete wrapper; |
| 271 } | 272 } |
| 272 | 273 |
| 273 void PluginsDOMHandler::LoadPlugins() { | 274 void PluginsDOMHandler::LoadPlugins() { |
| 274 if (!get_plugins_factory_.empty()) | 275 if (!get_plugins_factory_.empty()) |
| 275 return; | 276 return; |
| 276 | 277 |
| 277 std::vector<webkit::npapi::PluginGroup>* groups = | 278 ListWrapper* wrapper = new ListWrapper; |
| 278 new std::vector<webkit::npapi::PluginGroup>; | 279 wrapper->list = NULL; |
| 279 Task* task = get_plugins_factory_.NewRunnableMethod( | 280 Task* task = get_plugins_factory_.NewRunnableMethod( |
| 280 &PluginsDOMHandler::PluginsLoaded, groups); | 281 &PluginsDOMHandler::PluginsLoaded, wrapper); |
| 281 | 282 |
| 282 BrowserThread::PostTask( | 283 BrowserThread::PostTask( |
| 283 BrowserThread::FILE, | 284 BrowserThread::FILE, |
| 284 FROM_HERE, | 285 FROM_HERE, |
| 285 NewRunnableFunction( | 286 NewRunnableFunction( |
| 286 &PluginsDOMHandler::LoadPluginsOnFileThread, groups, task)); | 287 &PluginsDOMHandler::LoadPluginsOnFileThread, wrapper, task)); |
| 287 } | 288 } |
| 288 | 289 |
| 289 void PluginsDOMHandler::PluginsLoaded( | 290 void PluginsDOMHandler::PluginsLoaded(ListWrapper* wrapper) { |
| 290 const std::vector<webkit::npapi::PluginGroup>* groups) { | |
| 291 // Construct DictionaryValues to return to the UI | |
| 292 ListValue* plugin_groups_data = new ListValue(); | |
| 293 for (size_t i = 0; i < groups->size(); ++i) { | |
| 294 plugin_groups_data->Append((*groups)[i].GetDataForUI()); | |
| 295 // TODO(bauerb): Fetch plugin enabled state from PluginPrefs. | |
| 296 } | |
| 297 DictionaryValue results; | 291 DictionaryValue results; |
| 298 results.Set("plugins", plugin_groups_data); | 292 results.Set("plugins", wrapper->list); |
| 293 wrapper->list = NULL; // So it doesn't get deleted. |
| 299 web_ui_->CallJavascriptFunction("returnPluginsData", results); | 294 web_ui_->CallJavascriptFunction("returnPluginsData", results); |
| 300 } | 295 } |
| 301 | 296 |
| 302 } // namespace | 297 } // namespace |
| 303 | 298 |
| 304 /////////////////////////////////////////////////////////////////////////////// | 299 /////////////////////////////////////////////////////////////////////////////// |
| 305 // | 300 // |
| 306 // PluginsUI | 301 // PluginsUI |
| 307 // | 302 // |
| 308 /////////////////////////////////////////////////////////////////////////////// | 303 /////////////////////////////////////////////////////////////////////////////// |
| (...skipping 25 matching lines...) Expand all Loading... |
| 334 prefs->RegisterBooleanPref(prefs::kPluginsEnabledInternalPDF, | 329 prefs->RegisterBooleanPref(prefs::kPluginsEnabledInternalPDF, |
| 335 false, | 330 false, |
| 336 PrefService::UNSYNCABLE_PREF); | 331 PrefService::UNSYNCABLE_PREF); |
| 337 prefs->RegisterBooleanPref(prefs::kPluginsShowDetails, | 332 prefs->RegisterBooleanPref(prefs::kPluginsShowDetails, |
| 338 false, | 333 false, |
| 339 PrefService::UNSYNCABLE_PREF); | 334 PrefService::UNSYNCABLE_PREF); |
| 340 prefs->RegisterBooleanPref(prefs::kPluginsShowSetReaderDefaultInfobar, | 335 prefs->RegisterBooleanPref(prefs::kPluginsShowSetReaderDefaultInfobar, |
| 341 true, | 336 true, |
| 342 PrefService::UNSYNCABLE_PREF); | 337 PrefService::UNSYNCABLE_PREF); |
| 343 } | 338 } |
| OLD | NEW |