summaryrefslogtreecommitdiffstats
path: root/chrome/browser/metrics/plugin_metrics_provider.h
blob: 8d374c98a681ee470ba5801b04bbcaef57935942 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_METRICS_PLUGIN_METRICS_PROVIDER_H_
#define CHROME_BROWSER_METRICS_PLUGIN_METRICS_PROVIDER_H_

#include <map>
#include <vector>

#include "base/basictypes.h"
#include "base/gtest_prod_util.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/string16.h"
#include "components/metrics/metrics_provider.h"
#include "content/public/browser/browser_child_process_observer.h"

namespace base {
class FilePath;
}

namespace content {
struct WebPluginInfo;
}

class PrefRegistrySimple;
class PrefService;

// PluginMetricsProvider is responsible for adding out plugin information to
// the UMA proto.
class PluginMetricsProvider : public metrics::MetricsProvider,
                              public content::BrowserChildProcessObserver {
 public:
  explicit PluginMetricsProvider(PrefService* local_state);
  ~PluginMetricsProvider() override;

  // Fetches plugin information data asynchronously and calls |done_callback|
  // when done.
  void GetPluginInformation(const base::Closure& done_callback);

  // metrics::MetricsDataProvider:
  void ProvideSystemProfileMetrics(
      metrics::SystemProfileProto* system_profile_proto) override;
  void ProvideStabilityMetrics(
      metrics::SystemProfileProto* system_profile_proto) override;
  void ClearSavedStabilityMetrics() override;

  // Notifies the provider about an error loading the plugin at |plugin_path|.
  void LogPluginLoadingError(const base::FilePath& plugin_path);

  // Sets this provider's list of plugins, exposed for testing.
  void SetPluginsForTesting(const std::vector<content::WebPluginInfo>& plugins);

  // Returns true if process of type |type| should be counted as a plugin
  // process, and false otherwise.
  static bool IsPluginProcess(int process_type);

  // Registers local state prefs used by this class.
  static void RegisterPrefs(PrefRegistrySimple* registry);

 private:
  FRIEND_TEST_ALL_PREFIXES(PluginMetricsProviderTest,
                           RecordCurrentStateWithDelay);
  FRIEND_TEST_ALL_PREFIXES(PluginMetricsProviderTest,
                           RecordCurrentStateIfPending);
  FRIEND_TEST_ALL_PREFIXES(PluginMetricsProviderTest,
                           ProvideStabilityMetricsWhenPendingTask);
  struct ChildProcessStats;

  // Receives the plugin list from the PluginService and calls |done_callback|.
  void OnGotPlugins(const base::Closure& done_callback,
                    const std::vector<content::WebPluginInfo>& plugins);

  // Returns reference to ChildProcessStats corresponding to |data|.
  ChildProcessStats& GetChildProcessStats(
      const content::ChildProcessData& data);

  // Saves plugin information to local state.
  void RecordCurrentState();

  // Posts a delayed task for RecordCurrentState. Returns true if new task is
  // posted and false if there was one already waiting for execution.
  // The param delay_sec is for unit tests.
  bool RecordCurrentStateWithDelay(int delay_ms);

  // If a delayed RecordCurrnetState task exists then cancels it, calls
  // RecordCurrentState immediately and returns true. Otherwise returns false.
  bool RecordCurrentStateIfPending();

  // content::BrowserChildProcessObserver:
  void BrowserChildProcessHostConnected(
      const content::ChildProcessData& data) override;
  void BrowserChildProcessCrashed(
      const content::ChildProcessData& data) override;
  void BrowserChildProcessInstanceCreated(
      const content::ChildProcessData& data) override;

  PrefService* local_state_;

  // The list of plugins which was retrieved on the file thread.
  std::vector<content::WebPluginInfo> plugins_;

  // Buffer of child process notifications for quick access.
  std::map<base::string16, ChildProcessStats> child_process_stats_buffer_;

  base::WeakPtrFactory<PluginMetricsProvider> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(PluginMetricsProvider);
};

#endif  // CHROME_BROWSER_METRICS_PLUGIN_METRICS_PROVIDER_H_