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/bind.h" |
11 #include "base/memory/singleton.h" | 12 #include "base/memory/singleton.h" |
| 13 #include "base/memory/weak_ptr.h" |
12 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
13 #include "base/path_service.h" | 15 #include "base/path_service.h" |
14 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
15 #include "base/values.h" | 17 #include "base/values.h" |
16 #include "chrome/browser/plugin_prefs.h" | 18 #include "chrome/browser/plugin_prefs.h" |
17 #include "chrome/browser/prefs/pref_member.h" | 19 #include "chrome/browser/prefs/pref_member.h" |
18 #include "chrome/browser/prefs/pref_service.h" | 20 #include "chrome/browser/prefs/pref_service.h" |
19 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
20 #include "chrome/browser/ui/browser.h" | 22 #include "chrome/browser/ui/browser.h" |
21 #include "chrome/browser/ui/browser_window.h" | 23 #include "chrome/browser/ui/browser_window.h" |
22 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | 24 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" |
23 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" | 25 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" |
24 #include "chrome/common/chrome_content_client.h" | 26 #include "chrome/common/chrome_content_client.h" |
25 #include "chrome/common/chrome_notification_types.h" | 27 #include "chrome/common/chrome_notification_types.h" |
26 #include "chrome/common/chrome_paths.h" | 28 #include "chrome/common/chrome_paths.h" |
27 #include "chrome/common/pref_names.h" | 29 #include "chrome/common/pref_names.h" |
28 #include "chrome/common/url_constants.h" | 30 #include "chrome/common/url_constants.h" |
29 #include "content/browser/browser_thread.h" | 31 #include "content/browser/browser_thread.h" |
| 32 #include "content/browser/plugin_service.h" |
30 #include "content/browser/tab_contents/tab_contents.h" | 33 #include "content/browser/tab_contents/tab_contents.h" |
31 #include "content/common/notification_service.h" | 34 #include "content/common/notification_service.h" |
32 #include "grit/browser_resources.h" | 35 #include "grit/browser_resources.h" |
33 #include "grit/generated_resources.h" | 36 #include "grit/generated_resources.h" |
34 #include "grit/theme_resources.h" | 37 #include "grit/theme_resources.h" |
35 #include "grit/theme_resources_standard.h" | 38 #include "grit/theme_resources_standard.h" |
36 #include "ui/base/resource/resource_bundle.h" | 39 #include "ui/base/resource/resource_bundle.h" |
37 #include "webkit/plugins/npapi/plugin_list.h" | 40 #include "webkit/plugins/npapi/plugin_group.h" |
38 | 41 |
39 using webkit::npapi::PluginGroup; | 42 using webkit::npapi::PluginGroup; |
40 using webkit::WebPluginInfo; | 43 using webkit::WebPluginInfo; |
41 | 44 |
42 namespace { | 45 namespace { |
43 | 46 |
44 ChromeWebUIDataSource* CreatePluginsUIHTMLSource() { | 47 ChromeWebUIDataSource* CreatePluginsUIHTMLSource() { |
45 ChromeWebUIDataSource* source = | 48 ChromeWebUIDataSource* source = |
46 new ChromeWebUIDataSource(chrome::kChromeUIPluginsHost); | 49 new ChromeWebUIDataSource(chrome::kChromeUIPluginsHost); |
47 | 50 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 | 115 |
113 // Calback for the "getShowDetails" message. | 116 // Calback for the "getShowDetails" message. |
114 void HandleGetShowDetails(const ListValue* args); | 117 void HandleGetShowDetails(const ListValue* args); |
115 | 118 |
116 // NotificationObserver method overrides | 119 // NotificationObserver method overrides |
117 virtual void Observe(int type, | 120 virtual void Observe(int type, |
118 const NotificationSource& source, | 121 const NotificationSource& source, |
119 const NotificationDetails& details) OVERRIDE; | 122 const NotificationDetails& details) OVERRIDE; |
120 | 123 |
121 private: | 124 private: |
122 // Loads the plugins on the FILE thread. | |
123 static void LoadPluginsOnFileThread( | |
124 std::vector<PluginGroup>* groups, Task* task); | |
125 | |
126 // Used in conjunction with ListWrapper to avoid any memory leaks. | |
127 static void EnsurePluginGroupsDeleted( | |
128 std::vector<PluginGroup>* groups); | |
129 | |
130 // Call this to start getting the plugins on the UI thread. | 125 // Call this to start getting the plugins on the UI thread. |
131 void LoadPlugins(); | 126 void LoadPlugins(); |
132 | 127 |
133 // Called on the UI thread when the plugin information is ready. | 128 // Called on the UI thread when the plugin information is ready. |
134 void PluginsLoaded(const std::vector<PluginGroup>* groups); | 129 void PluginsLoaded(const std::vector<PluginGroup>& groups); |
135 | 130 |
136 NotificationRegistrar registrar_; | 131 NotificationRegistrar registrar_; |
137 | 132 |
138 ScopedRunnableMethodFactory<PluginsDOMHandler> get_plugins_factory_; | 133 base::WeakPtrFactory<PluginsDOMHandler> weak_ptr_factory_; |
139 | 134 |
140 // This pref guards the value whether about:plugins is in the details mode or | 135 // This pref guards the value whether about:plugins is in the details mode or |
141 // not. | 136 // not. |
142 BooleanPrefMember show_details_; | 137 BooleanPrefMember show_details_; |
143 | 138 |
144 DISALLOW_COPY_AND_ASSIGN(PluginsDOMHandler); | 139 DISALLOW_COPY_AND_ASSIGN(PluginsDOMHandler); |
145 }; | 140 }; |
146 | 141 |
147 PluginsDOMHandler::PluginsDOMHandler() | 142 PluginsDOMHandler::PluginsDOMHandler() |
148 : ALLOW_THIS_IN_INITIALIZER_LIST(get_plugins_factory_(this)) { | 143 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
149 registrar_.Add(this, | 144 registrar_.Add(this, |
150 chrome::NOTIFICATION_PLUGIN_ENABLE_STATUS_CHANGED, | 145 chrome::NOTIFICATION_PLUGIN_ENABLE_STATUS_CHANGED, |
151 NotificationService::AllSources()); | 146 NotificationService::AllSources()); |
152 } | 147 } |
153 | 148 |
154 WebUIMessageHandler* PluginsDOMHandler::Attach(WebUI* web_ui) { | 149 WebUIMessageHandler* PluginsDOMHandler::Attach(WebUI* web_ui) { |
155 PrefService* prefs = Profile::FromWebUI(web_ui)->GetPrefs(); | 150 PrefService* prefs = Profile::FromWebUI(web_ui)->GetPrefs(); |
156 | 151 |
157 show_details_.Init(prefs::kPluginsShowDetails, prefs, NULL); | 152 show_details_.Init(prefs::kPluginsShowDetails, prefs, NULL); |
158 | 153 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 web_ui_->CallJavascriptFunction("loadShowDetailsFromPrefs", show_details); | 223 web_ui_->CallJavascriptFunction("loadShowDetailsFromPrefs", show_details); |
229 } | 224 } |
230 | 225 |
231 void PluginsDOMHandler::Observe(int type, | 226 void PluginsDOMHandler::Observe(int type, |
232 const NotificationSource& source, | 227 const NotificationSource& source, |
233 const NotificationDetails& details) { | 228 const NotificationDetails& details) { |
234 DCHECK_EQ(chrome::NOTIFICATION_PLUGIN_ENABLE_STATUS_CHANGED, type); | 229 DCHECK_EQ(chrome::NOTIFICATION_PLUGIN_ENABLE_STATUS_CHANGED, type); |
235 LoadPlugins(); | 230 LoadPlugins(); |
236 } | 231 } |
237 | 232 |
238 void PluginsDOMHandler::LoadPluginsOnFileThread( | 233 void PluginsDOMHandler::LoadPlugins() { |
239 std::vector<PluginGroup>* groups, | 234 if (weak_ptr_factory_.HasWeakPtrs()) |
240 Task* task) { | 235 return; |
241 webkit::npapi::PluginList::Singleton()->GetPluginGroups(true, groups); | |
242 | 236 |
243 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, task); | 237 PluginService::GetInstance()->GetPluginGroups( |
244 BrowserThread::PostTask( | 238 base::Bind(&PluginsDOMHandler::PluginsLoaded, |
245 BrowserThread::UI, | 239 weak_ptr_factory_.GetWeakPtr())); |
246 FROM_HERE, | |
247 NewRunnableFunction(&PluginsDOMHandler::EnsurePluginGroupsDeleted, | |
248 groups)); | |
249 } | 240 } |
250 | 241 |
251 void PluginsDOMHandler::EnsurePluginGroupsDeleted( | 242 void PluginsDOMHandler::PluginsLoaded(const std::vector<PluginGroup>& groups) { |
252 std::vector<PluginGroup>* groups) { | |
253 delete groups; | |
254 } | |
255 | |
256 void PluginsDOMHandler::LoadPlugins() { | |
257 if (!get_plugins_factory_.empty()) | |
258 return; | |
259 | |
260 std::vector<PluginGroup>* groups = new std::vector<PluginGroup>; | |
261 Task* task = get_plugins_factory_.NewRunnableMethod( | |
262 &PluginsDOMHandler::PluginsLoaded, groups); | |
263 | |
264 BrowserThread::PostTask( | |
265 BrowserThread::FILE, | |
266 FROM_HERE, | |
267 NewRunnableFunction( | |
268 &PluginsDOMHandler::LoadPluginsOnFileThread, groups, task)); | |
269 } | |
270 | |
271 void PluginsDOMHandler::PluginsLoaded(const std::vector<PluginGroup>* groups) { | |
272 PluginPrefs* plugin_prefs = | 243 PluginPrefs* plugin_prefs = |
273 PluginPrefs::GetForProfile(Profile::FromWebUI(web_ui_)); | 244 PluginPrefs::GetForProfile(Profile::FromWebUI(web_ui_)); |
274 | 245 |
275 bool all_plugins_enabled_by_policy = true; | 246 bool all_plugins_enabled_by_policy = true; |
276 bool all_plugins_disabled_by_policy = true; | 247 bool all_plugins_disabled_by_policy = true; |
277 | 248 |
278 // Construct DictionaryValues to return to the UI | 249 // Construct DictionaryValues to return to the UI |
279 ListValue* plugin_groups_data = new ListValue(); | 250 ListValue* plugin_groups_data = new ListValue(); |
280 for (size_t i = 0; i < groups->size(); ++i) { | 251 for (size_t i = 0; i < groups.size(); ++i) { |
281 ListValue* plugin_files = new ListValue(); | 252 ListValue* plugin_files = new ListValue(); |
282 const PluginGroup& group = (*groups)[i]; | 253 const PluginGroup& group = groups[i]; |
283 string16 group_name = group.GetGroupName(); | 254 string16 group_name = group.GetGroupName(); |
284 bool group_enabled = false; | 255 bool group_enabled = false; |
285 const WebPluginInfo* active_plugin = NULL; | 256 const WebPluginInfo* active_plugin = NULL; |
286 for (size_t j = 0; j < group.web_plugin_infos().size(); ++j) { | 257 for (size_t j = 0; j < group.web_plugin_infos().size(); ++j) { |
287 const WebPluginInfo& group_plugin = group.web_plugin_infos()[j]; | 258 const WebPluginInfo& group_plugin = group.web_plugin_infos()[j]; |
288 | 259 |
289 DictionaryValue* plugin_file = new DictionaryValue(); | 260 DictionaryValue* plugin_file = new DictionaryValue(); |
290 plugin_file->SetString("name", group_plugin.name); | 261 plugin_file->SetString("name", group_plugin.name); |
291 plugin_file->SetString("description", group_plugin.desc); | 262 plugin_file->SetString("description", group_plugin.desc); |
292 plugin_file->SetString("path", group_plugin.path.value()); | 263 plugin_file->SetString("path", group_plugin.path.value()); |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 | 368 |
398 // static | 369 // static |
399 void PluginsUI::RegisterUserPrefs(PrefService* prefs) { | 370 void PluginsUI::RegisterUserPrefs(PrefService* prefs) { |
400 prefs->RegisterBooleanPref(prefs::kPluginsShowDetails, | 371 prefs->RegisterBooleanPref(prefs::kPluginsShowDetails, |
401 false, | 372 false, |
402 PrefService::UNSYNCABLE_PREF); | 373 PrefService::UNSYNCABLE_PREF); |
403 prefs->RegisterBooleanPref(prefs::kPluginsShowSetReaderDefaultInfobar, | 374 prefs->RegisterBooleanPref(prefs::kPluginsShowSetReaderDefaultInfobar, |
404 true, | 375 true, |
405 PrefService::UNSYNCABLE_PREF); | 376 PrefService::UNSYNCABLE_PREF); |
406 } | 377 } |
OLD | NEW |