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.
5 #ifndef CHROME_BROWSER_METRICS_PLUGIN_METRICS_PROVIDER_H_
6 #define CHROME_BROWSER_METRICS_PLUGIN_METRICS_PROVIDER_H_
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"
26 class PrefRegistrySimple
;
29 // PluginMetricsProvider is responsible for adding out plugin information to
31 class PluginMetricsProvider
: public metrics::MetricsProvider
,
32 public content::BrowserChildProcessObserver
{
34 explicit PluginMetricsProvider(PrefService
* local_state
);
35 ~PluginMetricsProvider() override
;
37 // Fetches plugin information data asynchronously and calls |done_callback|
39 void GetPluginInformation(const base::Closure
& done_callback
);
41 // metrics::MetricsDataProvider:
42 void ProvideSystemProfileMetrics(
43 metrics::SystemProfileProto
* system_profile_proto
) override
;
44 void ProvideStabilityMetrics(
45 metrics::SystemProfileProto
* system_profile_proto
) override
;
46 void ClearSavedStabilityMetrics() override
;
48 // Notifies the provider about an error loading the plugin at |plugin_path|.
49 void LogPluginLoadingError(const base::FilePath
& plugin_path
);
51 // Sets this provider's list of plugins, exposed for testing.
52 void SetPluginsForTesting(const std::vector
<content::WebPluginInfo
>& plugins
);
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
);
58 // Registers local state prefs used by this class.
59 static void RegisterPrefs(PrefRegistrySimple
* registry
);
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
;
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
);
74 // Returns reference to ChildProcessStats corresponding to |data|.
75 ChildProcessStats
& GetChildProcessStats(
76 const content::ChildProcessData
& data
);
78 // Saves plugin information to local state.
79 void RecordCurrentState();
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
);
86 // If a delayed RecordCurrnetState task exists then cancels it, calls
87 // RecordCurrentState immediately and returns true. Otherwise returns false.
88 bool RecordCurrentStateIfPending();
90 // content::BrowserChildProcessObserver:
91 void BrowserChildProcessHostConnected(
92 const content::ChildProcessData
& data
) override
;
93 void BrowserChildProcessCrashed(const content::ChildProcessData
& data
,
94 int exit_code
) override
;
95 void BrowserChildProcessInstanceCreated(
96 const content::ChildProcessData
& data
) override
;
97 void BrowserChildProcessKilled(const content::ChildProcessData
& data
,
98 int exit_code
) override
;
100 PrefService
* local_state_
;
102 // The list of plugins which was retrieved on the file thread.
103 std::vector
<content::WebPluginInfo
> plugins_
;
105 // Buffer of child process notifications for quick access.
106 std::map
<base::string16
, ChildProcessStats
> child_process_stats_buffer_
;
108 base::WeakPtrFactory
<PluginMetricsProvider
> weak_ptr_factory_
;
110 DISALLOW_COPY_AND_ASSIGN(PluginMetricsProvider
);
113 #endif // CHROME_BROWSER_METRICS_PLUGIN_METRICS_PROVIDER_H_