plugin_metrics_provider.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
1// Copyright 2014 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/metrics/plugin_metrics_provider.h" 6 7#include <string> 8 9#include "base/prefs/pref_registry_simple.h" 10#include "base/prefs/pref_service.h" 11#include "base/prefs/scoped_user_pref_update.h" 12#include "base/stl_util.h" 13#include "base/strings/utf_string_conversions.h" 14#include "base/time/time.h" 15#include "chrome/browser/browser_process.h" 16#include "chrome/browser/plugins/plugin_prefs.h" 17#include "chrome/browser/profiles/profile_manager.h" 18#include "chrome/common/pref_names.h" 19#include "components/metrics/proto/system_profile.pb.h" 20#include "content/public/browser/child_process_data.h" 21#include "content/public/browser/plugin_service.h" 22#include "content/public/common/process_type.h" 23#include "content/public/common/webplugininfo.h" 24 25namespace { 26 27// Delay for RecordCurrentState execution. 28const int kRecordStateDelayMs = 15 * base::Time::kMillisecondsPerSecond; 29 30// Returns the plugin preferences corresponding for this user, if available. 31// If multiple user profiles are loaded, returns the preferences corresponding 32// to an arbitrary one of the profiles. 33PluginPrefs* GetPluginPrefs() { 34 ProfileManager* profile_manager = g_browser_process->profile_manager(); 35 36 if (!profile_manager) { 37 // The profile manager can be NULL when testing. 38 return NULL; 39 } 40 41 std::vector<Profile*> profiles = profile_manager->GetLoadedProfiles(); 42 if (profiles.empty()) 43 return NULL; 44 45 return PluginPrefs::GetForProfile(profiles.front()).get(); 46} 47 48// Fills |plugin| with the info contained in |plugin_info| and |plugin_prefs|. 49void SetPluginInfo(const content::WebPluginInfo& plugin_info, 50 const PluginPrefs* plugin_prefs, 51 metrics::SystemProfileProto::Plugin* plugin) { 52 plugin->set_name(base::UTF16ToUTF8(plugin_info.name)); 53 plugin->set_filename(plugin_info.path.BaseName().AsUTF8Unsafe()); 54 plugin->set_version(base::UTF16ToUTF8(plugin_info.version)); 55 plugin->set_is_pepper(plugin_info.is_pepper_plugin()); 56 if (plugin_prefs) 57 plugin->set_is_disabled(!plugin_prefs->IsPluginEnabled(plugin_info)); 58} 59 60} // namespace 61 62// This is used to quickly log stats from child process related notifications in 63// PluginMetricsProvider::child_stats_buffer_. The buffer's contents are 64// transferred out when Local State is periodically saved. The information is 65// then reported to the UMA server on next launch. 66struct PluginMetricsProvider::ChildProcessStats { 67 public: 68 explicit ChildProcessStats(int process_type) 69 : process_launches(0), 70 process_crashes(0), 71 instances(0), 72 loading_errors(0), 73 process_type(process_type) {} 74 75 // This constructor is only used by the map to return some default value for 76 // an index for which no value has been assigned. 77 ChildProcessStats() 78 : process_launches(0), 79 process_crashes(0), 80 instances(0), 81 loading_errors(0), 82 process_type(content::PROCESS_TYPE_UNKNOWN) {} 83 84 // The number of times that the given child process has been launched 85 int process_launches; 86 87 // The number of times that the given child process has crashed 88 int process_crashes; 89 90 // The number of instances of this child process that have been created. 91 // An instance is a DOM object rendered by this child process during a page 92 // load. 93 int instances; 94 95 // The number of times there was an error loading an instance of this child 96 // process. 97 int loading_errors; 98 99 int process_type; 100}; 101 102PluginMetricsProvider::PluginMetricsProvider(PrefService* local_state) 103 : local_state_(local_state), 104 weak_ptr_factory_(this) { 105 DCHECK(local_state_); 106 107 BrowserChildProcessObserver::Add(this); 108} 109 110PluginMetricsProvider::~PluginMetricsProvider() { 111 BrowserChildProcessObserver::Remove(this); 112} 113 114void PluginMetricsProvider::GetPluginInformation( 115 const base::Closure& done_callback) { 116 content::PluginService::GetInstance()->GetPlugins( 117 base::Bind(&PluginMetricsProvider::OnGotPlugins, 118 weak_ptr_factory_.GetWeakPtr(), 119 done_callback)); 120} 121 122void PluginMetricsProvider::ProvideSystemProfileMetrics( 123 metrics::SystemProfileProto* system_profile_proto) { 124 PluginPrefs* plugin_prefs = GetPluginPrefs(); 125 for (size_t i = 0; i < plugins_.size(); ++i) { 126 SetPluginInfo(plugins_[i], plugin_prefs, 127 system_profile_proto->add_plugin()); 128 } 129} 130 131void PluginMetricsProvider::ProvideStabilityMetrics( 132 metrics::SystemProfileProto* system_profile_proto) { 133 RecordCurrentStateIfPending(); 134 const base::ListValue* plugin_stats_list = local_state_->GetList( 135 prefs::kStabilityPluginStats); 136 if (!plugin_stats_list) 137 return; 138 139 metrics::SystemProfileProto::Stability* stability = 140 system_profile_proto->mutable_stability(); 141 for (base::ListValue::const_iterator iter = plugin_stats_list->begin(); 142 iter != plugin_stats_list->end(); ++iter) { 143 if (!(*iter)->IsType(base::Value::TYPE_DICTIONARY)) { 144 NOTREACHED(); 145 continue; 146 } 147 base::DictionaryValue* plugin_dict = 148 static_cast<base::DictionaryValue*>(*iter); 149 150 // Note that this search is potentially a quadratic operation, but given the 151 // low number of plugins installed on a "reasonable" setup, this should be 152 // fine. 153 // TODO(isherman): Verify that this does not show up as a hotspot in 154 // profiler runs. 155 const metrics::SystemProfileProto::Plugin* system_profile_plugin = NULL; 156 std::string plugin_name; 157 plugin_dict->GetString(prefs::kStabilityPluginName, &plugin_name); 158 for (int i = 0; i < system_profile_proto->plugin_size(); ++i) { 159 if (system_profile_proto->plugin(i).name() == plugin_name) { 160 system_profile_plugin = &system_profile_proto->plugin(i); 161 break; 162 } 163 } 164 165 if (!system_profile_plugin) { 166 NOTREACHED(); 167 continue; 168 } 169 170 metrics::SystemProfileProto::Stability::PluginStability* plugin_stability = 171 stability->add_plugin_stability(); 172 *plugin_stability->mutable_plugin() = *system_profile_plugin; 173 174 int launches = 0; 175 plugin_dict->GetInteger(prefs::kStabilityPluginLaunches, &launches); 176 if (launches > 0) 177 plugin_stability->set_launch_count(launches); 178 179 int instances = 0; 180 plugin_dict->GetInteger(prefs::kStabilityPluginInstances, &instances); 181 if (instances > 0) 182 plugin_stability->set_instance_count(instances); 183 184 int crashes = 0; 185 plugin_dict->GetInteger(prefs::kStabilityPluginCrashes, &crashes); 186 if (crashes > 0) 187 plugin_stability->set_crash_count(crashes); 188 189 int loading_errors = 0; 190 plugin_dict->GetInteger(prefs::kStabilityPluginLoadingErrors, 191 &loading_errors); 192 if (loading_errors > 0) 193 plugin_stability->set_loading_error_count(loading_errors); 194 } 195 196 local_state_->ClearPref(prefs::kStabilityPluginStats); 197} 198 199// Saves plugin-related updates from the in-object buffer to Local State 200// for retrieval next time we send a Profile log (generally next launch). 201void PluginMetricsProvider::RecordCurrentState() { 202 ListPrefUpdate update(local_state_, prefs::kStabilityPluginStats); 203 base::ListValue* plugins = update.Get(); 204 DCHECK(plugins); 205 206 for (base::ListValue::iterator value_iter = plugins->begin(); 207 value_iter != plugins->end(); ++value_iter) { 208 if (!(*value_iter)->IsType(base::Value::TYPE_DICTIONARY)) { 209 NOTREACHED(); 210 continue; 211 } 212 213 base::DictionaryValue* plugin_dict = 214 static_cast<base::DictionaryValue*>(*value_iter); 215 std::string plugin_name; 216 plugin_dict->GetString(prefs::kStabilityPluginName, &plugin_name); 217 if (plugin_name.empty()) { 218 NOTREACHED(); 219 continue; 220 } 221 222 // TODO(viettrungluu): remove conversions 223 base::string16 name16 = base::UTF8ToUTF16(plugin_name); 224 if (child_process_stats_buffer_.find(name16) == 225 child_process_stats_buffer_.end()) { 226 continue; 227 } 228 229 ChildProcessStats stats = child_process_stats_buffer_[name16]; 230 if (stats.process_launches) { 231 int launches = 0; 232 plugin_dict->GetInteger(prefs::kStabilityPluginLaunches, &launches); 233 launches += stats.process_launches; 234 plugin_dict->SetInteger(prefs::kStabilityPluginLaunches, launches); 235 } 236 if (stats.process_crashes) { 237 int crashes = 0; 238 plugin_dict->GetInteger(prefs::kStabilityPluginCrashes, &crashes); 239 crashes += stats.process_crashes; 240 plugin_dict->SetInteger(prefs::kStabilityPluginCrashes, crashes); 241 } 242 if (stats.instances) { 243 int instances = 0; 244 plugin_dict->GetInteger(prefs::kStabilityPluginInstances, &instances); 245 instances += stats.instances; 246 plugin_dict->SetInteger(prefs::kStabilityPluginInstances, instances); 247 } 248 if (stats.loading_errors) { 249 int loading_errors = 0; 250 plugin_dict->GetInteger(prefs::kStabilityPluginLoadingErrors, 251 &loading_errors); 252 loading_errors += stats.loading_errors; 253 plugin_dict->SetInteger(prefs::kStabilityPluginLoadingErrors, 254 loading_errors); 255 } 256 257 child_process_stats_buffer_.erase(name16); 258 } 259 260 // Now go through and add dictionaries for plugins that didn't already have 261 // reports in Local State. 262 for (std::map<base::string16, ChildProcessStats>::iterator cache_iter = 263 child_process_stats_buffer_.begin(); 264 cache_iter != child_process_stats_buffer_.end(); ++cache_iter) { 265 ChildProcessStats stats = cache_iter->second; 266 267 // Insert only plugins information into the plugins list. 268 if (!IsPluginProcess(stats.process_type)) 269 continue; 270 271 // TODO(viettrungluu): remove conversion 272 std::string plugin_name = base::UTF16ToUTF8(cache_iter->first); 273 274 base::DictionaryValue* plugin_dict = new base::DictionaryValue; 275 276 plugin_dict->SetString(prefs::kStabilityPluginName, plugin_name); 277 plugin_dict->SetInteger(prefs::kStabilityPluginLaunches, 278 stats.process_launches); 279 plugin_dict->SetInteger(prefs::kStabilityPluginCrashes, 280 stats.process_crashes); 281 plugin_dict->SetInteger(prefs::kStabilityPluginInstances, 282 stats.instances); 283 plugin_dict->SetInteger(prefs::kStabilityPluginLoadingErrors, 284 stats.loading_errors); 285 plugins->Append(plugin_dict); 286 } 287 child_process_stats_buffer_.clear(); 288} 289 290void PluginMetricsProvider::LogPluginLoadingError( 291 const base::FilePath& plugin_path) { 292 content::WebPluginInfo plugin; 293 bool success = 294 content::PluginService::GetInstance()->GetPluginInfoByPath(plugin_path, 295 &plugin); 296 DCHECK(success); 297 ChildProcessStats& stats = child_process_stats_buffer_[plugin.name]; 298 // Initialize the type if this entry is new. 299 if (stats.process_type == content::PROCESS_TYPE_UNKNOWN) { 300 // The plug-in process might not actually be of type PLUGIN (which means 301 // NPAPI), but we only care that it is *a* plug-in process. 302 stats.process_type = content::PROCESS_TYPE_PLUGIN; 303 } else { 304 DCHECK(IsPluginProcess(stats.process_type)); 305 } 306 stats.loading_errors++; 307 RecordCurrentStateWithDelay(kRecordStateDelayMs); 308} 309 310void PluginMetricsProvider::SetPluginsForTesting( 311 const std::vector<content::WebPluginInfo>& plugins) { 312 plugins_ = plugins; 313} 314 315// static 316bool PluginMetricsProvider::IsPluginProcess(int process_type) { 317 return (process_type == content::PROCESS_TYPE_PLUGIN || 318 process_type == content::PROCESS_TYPE_PPAPI_PLUGIN || 319 process_type == content::PROCESS_TYPE_PPAPI_BROKER); 320} 321 322// static 323void PluginMetricsProvider::RegisterPrefs(PrefRegistrySimple* registry) { 324 registry->RegisterListPref(prefs::kStabilityPluginStats); 325} 326 327void PluginMetricsProvider::OnGotPlugins( 328 const base::Closure& done_callback, 329 const std::vector<content::WebPluginInfo>& plugins) { 330 plugins_ = plugins; 331 done_callback.Run(); 332} 333 334PluginMetricsProvider::ChildProcessStats& 335PluginMetricsProvider::GetChildProcessStats( 336 const content::ChildProcessData& data) { 337 const base::string16& child_name = data.name; 338 if (!ContainsKey(child_process_stats_buffer_, child_name)) { 339 child_process_stats_buffer_[child_name] = 340 ChildProcessStats(data.process_type); 341 } 342 return child_process_stats_buffer_[child_name]; 343} 344 345void PluginMetricsProvider::BrowserChildProcessHostConnected( 346 const content::ChildProcessData& data) { 347 GetChildProcessStats(data).process_launches++; 348 RecordCurrentStateWithDelay(kRecordStateDelayMs); 349} 350 351void PluginMetricsProvider::BrowserChildProcessCrashed( 352 const content::ChildProcessData& data) { 353 GetChildProcessStats(data).process_crashes++; 354 RecordCurrentStateWithDelay(kRecordStateDelayMs); 355} 356 357void PluginMetricsProvider::BrowserChildProcessInstanceCreated( 358 const content::ChildProcessData& data) { 359 GetChildProcessStats(data).instances++; 360 RecordCurrentStateWithDelay(kRecordStateDelayMs); 361} 362 363bool PluginMetricsProvider::RecordCurrentStateWithDelay(int delay_sec) { 364 if (weak_ptr_factory_.HasWeakPtrs()) 365 return false; 366 367 base::MessageLoopProxy::current()->PostDelayedTask( 368 FROM_HERE, 369 base::Bind(&PluginMetricsProvider::RecordCurrentState, 370 weak_ptr_factory_.GetWeakPtr()), 371 base::TimeDelta::FromMilliseconds(delay_sec)); 372 return true; 373} 374 375bool PluginMetricsProvider::RecordCurrentStateIfPending() { 376 if (!weak_ptr_factory_.HasWeakPtrs()) 377 return false; 378 379 weak_ptr_factory_.InvalidateWeakPtrs(); 380 RecordCurrentState(); 381 return true; 382} 383