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#ifndef CHROME_BROWSER_METRICS_PLUGIN_METRICS_PROVIDER_H_
6#define CHROME_BROWSER_METRICS_PLUGIN_METRICS_PROVIDER_H_
7
8#include <map>
9#include <vector>
10
11#include "base/basictypes.h"
12#include "base/gtest_prod_util.h"
13#include "base/memory/weak_ptr.h"
14#include "base/strings/string16.h"
15#include "components/metrics/metrics_provider.h"
16#include "content/public/browser/browser_child_process_observer.h"
17
18namespace base {
19class FilePath;
20}
21
22namespace content {
23struct WebPluginInfo;
24}
25
26class PrefRegistrySimple;
27class PrefService;
28
29// PluginMetricsProvider is responsible for adding out plugin information to
30// the UMA proto.
31class PluginMetricsProvider : public metrics::MetricsProvider,
32                              public content::BrowserChildProcessObserver {
33 public:
34  explicit PluginMetricsProvider(PrefService* local_state);
35  virtual ~PluginMetricsProvider();
36
37  // Fetches plugin information data asynchronously and calls |done_callback|
38  // when done.
39  void GetPluginInformation(const base::Closure& done_callback);
40
41  // metrics::MetricsDataProvider:
42  virtual void ProvideSystemProfileMetrics(
43      metrics::SystemProfileProto* system_profile_proto) OVERRIDE;
44  virtual void ProvideStabilityMetrics(
45      metrics::SystemProfileProto* system_profile_proto) OVERRIDE;
46  virtual void ClearSavedStabilityMetrics() OVERRIDE;
47
48  // Notifies the provider about an error loading the plugin at |plugin_path|.
49  void LogPluginLoadingError(const base::FilePath& plugin_path);
50
51  // Sets this provider's list of plugins, exposed for testing.
52  void SetPluginsForTesting(const std::vector<content::WebPluginInfo>& plugins);
53
54  // Returns true if process of type |type| should be counted as a plugin
55  // process, and false otherwise.
56  static bool IsPluginProcess(int process_type);
57
58  // Registers local state prefs used by this class.
59  static void RegisterPrefs(PrefRegistrySimple* registry);
60
61 private:
62  FRIEND_TEST_ALL_PREFIXES(PluginMetricsProviderTest,
63                           RecordCurrentStateWithDelay);
64  FRIEND_TEST_ALL_PREFIXES(PluginMetricsProviderTest,
65                           RecordCurrentStateIfPending);
66  FRIEND_TEST_ALL_PREFIXES(PluginMetricsProviderTest,
67                           ProvideStabilityMetricsWhenPendingTask);
68  struct ChildProcessStats;
69
70  // Receives the plugin list from the PluginService and calls |done_callback|.
71  void OnGotPlugins(const base::Closure& done_callback,
72                    const std::vector<content::WebPluginInfo>& plugins);
73
74  // Returns reference to ChildProcessStats corresponding to |data|.
75  ChildProcessStats& GetChildProcessStats(
76      const content::ChildProcessData& data);
77
78  // Saves plugin information to local state.
79  void RecordCurrentState();
80
81  // Posts a delayed task for RecordCurrentState. Returns true if new task is
82  // posted and false if there was one already waiting for execution.
83  // The param delay_sec is for unit tests.
84  bool RecordCurrentStateWithDelay(int delay_ms);
85
86  // If a delayed RecordCurrnetState task exists then cancels it, calls
87  // RecordCurrentState immediately and returns true. Otherwise returns false.
88  bool RecordCurrentStateIfPending();
89
90  // content::BrowserChildProcessObserver:
91  virtual void BrowserChildProcessHostConnected(
92      const content::ChildProcessData& data) OVERRIDE;
93  virtual void BrowserChildProcessCrashed(
94      const content::ChildProcessData& data) OVERRIDE;
95  virtual void BrowserChildProcessInstanceCreated(
96      const content::ChildProcessData& data) OVERRIDE;
97
98  PrefService* local_state_;
99
100  // The list of plugins which was retrieved on the file thread.
101  std::vector<content::WebPluginInfo> plugins_;
102
103  // Buffer of child process notifications for quick access.
104  std::map<base::string16, ChildProcessStats> child_process_stats_buffer_;
105
106  base::WeakPtrFactory<PluginMetricsProvider> weak_ptr_factory_;
107
108  DISALLOW_COPY_AND_ASSIGN(PluginMetricsProvider);
109};
110
111#endif  // CHROME_BROWSER_METRICS_PLUGIN_METRICS_PROVIDER_H_
112