| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/metrics/plugin_metrics_provider.h" | 5 #include "chrome/browser/metrics/plugin_metrics_provider.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
| 11 #include "base/prefs/scoped_user_pref_update.h" | 11 #include "base/prefs/scoped_user_pref_update.h" |
| 12 #include "base/prefs/testing_pref_service.h" | 12 #include "base/prefs/testing_pref_service.h" |
| 13 #include "base/run_loop.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 14 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
| 15 #include "components/metrics/proto/system_profile.pb.h" | 16 #include "components/metrics/proto/system_profile.pb.h" |
| 17 #include "content/public/browser/child_process_data.h" |
| 16 #include "content/public/common/process_type.h" | 18 #include "content/public/common/process_type.h" |
| 17 #include "content/public/common/webplugininfo.h" | 19 #include "content/public/common/webplugininfo.h" |
| 18 #include "content/public/test/test_browser_thread_bundle.h" | 20 #include "content/public/test/test_browser_thread_bundle.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 22 |
| 21 namespace { | 23 namespace { |
| 22 | 24 |
| 23 content::WebPluginInfo CreateFakePluginInfo( | 25 content::WebPluginInfo CreateFakePluginInfo( |
| 24 const std::string& name, | 26 const std::string& name, |
| 25 const base::FilePath::CharType* path, | 27 const base::FilePath::CharType* path, |
| 26 const std::string& version, | 28 const std::string& version, |
| 27 bool is_pepper) { | 29 bool is_pepper) { |
| 28 content::WebPluginInfo plugin(base::UTF8ToUTF16(name), | 30 content::WebPluginInfo plugin(base::UTF8ToUTF16(name), |
| 29 base::FilePath(path), | 31 base::FilePath(path), |
| 30 base::UTF8ToUTF16(version), | 32 base::UTF8ToUTF16(version), |
| 31 base::string16()); | 33 base::string16()); |
| 32 if (is_pepper) | 34 if (is_pepper) |
| 33 plugin.type = content::WebPluginInfo::PLUGIN_TYPE_PEPPER_IN_PROCESS; | 35 plugin.type = content::WebPluginInfo::PLUGIN_TYPE_PEPPER_IN_PROCESS; |
| 34 else | 36 else |
| 35 plugin.type = content::WebPluginInfo::PLUGIN_TYPE_NPAPI; | 37 plugin.type = content::WebPluginInfo::PLUGIN_TYPE_NPAPI; |
| 36 return plugin; | 38 return plugin; |
| 37 } | 39 } |
| 38 | 40 |
| 41 class PluginMetricsProviderTest : public ::testing::Test { |
| 42 protected: |
| 43 PluginMetricsProviderTest() |
| 44 : prefs_(new TestingPrefServiceSimple) { |
| 45 PluginMetricsProvider::RegisterPrefs(prefs()->registry()); |
| 46 } |
| 47 |
| 48 TestingPrefServiceSimple* prefs() { |
| 49 return prefs_.get(); |
| 50 } |
| 51 |
| 52 private: |
| 53 scoped_ptr<TestingPrefServiceSimple> prefs_; |
| 54 |
| 55 DISALLOW_COPY_AND_ASSIGN(PluginMetricsProviderTest); |
| 56 |
| 57 }; |
| 58 |
| 39 } // namespace | 59 } // namespace |
| 40 | 60 |
| 41 TEST(PluginMetricsProviderTest, IsPluginProcess) { | 61 TEST_F(PluginMetricsProviderTest, IsPluginProcess) { |
| 42 EXPECT_TRUE(PluginMetricsProvider::IsPluginProcess( | 62 EXPECT_TRUE(PluginMetricsProvider::IsPluginProcess( |
| 43 content::PROCESS_TYPE_PLUGIN)); | 63 content::PROCESS_TYPE_PLUGIN)); |
| 44 EXPECT_TRUE(PluginMetricsProvider::IsPluginProcess( | 64 EXPECT_TRUE(PluginMetricsProvider::IsPluginProcess( |
| 45 content::PROCESS_TYPE_PPAPI_PLUGIN)); | 65 content::PROCESS_TYPE_PPAPI_PLUGIN)); |
| 46 EXPECT_FALSE(PluginMetricsProvider::IsPluginProcess( | 66 EXPECT_FALSE(PluginMetricsProvider::IsPluginProcess( |
| 47 content::PROCESS_TYPE_GPU)); | 67 content::PROCESS_TYPE_GPU)); |
| 48 } | 68 } |
| 49 | 69 |
| 50 TEST(PluginMetricsProviderTest, Plugins) { | 70 TEST_F(PluginMetricsProviderTest, Plugins) { |
| 51 content::TestBrowserThreadBundle thread_bundle; | 71 content::TestBrowserThreadBundle thread_bundle; |
| 52 | 72 |
| 53 TestingPrefServiceSimple prefs; | 73 PluginMetricsProvider provider(prefs()); |
| 54 PluginMetricsProvider::RegisterPrefs(prefs.registry()); | |
| 55 PluginMetricsProvider provider(&prefs); | |
| 56 | 74 |
| 57 std::vector<content::WebPluginInfo> plugins; | 75 std::vector<content::WebPluginInfo> plugins; |
| 58 plugins.push_back(CreateFakePluginInfo("p1", FILE_PATH_LITERAL("p1.plugin"), | 76 plugins.push_back(CreateFakePluginInfo("p1", FILE_PATH_LITERAL("p1.plugin"), |
| 59 "1.5", true)); | 77 "1.5", true)); |
| 60 plugins.push_back(CreateFakePluginInfo("p2", FILE_PATH_LITERAL("p2.plugin"), | 78 plugins.push_back(CreateFakePluginInfo("p2", FILE_PATH_LITERAL("p2.plugin"), |
| 61 "2.0", false)); | 79 "2.0", false)); |
| 62 provider.SetPluginsForTesting(plugins); | 80 provider.SetPluginsForTesting(plugins); |
| 63 | 81 |
| 64 metrics::SystemProfileProto system_profile; | 82 metrics::SystemProfileProto system_profile; |
| 65 provider.ProvideSystemProfileMetrics(&system_profile); | 83 provider.ProvideSystemProfileMetrics(&system_profile); |
| 66 | 84 |
| 67 ASSERT_EQ(2, system_profile.plugin_size()); | 85 ASSERT_EQ(2, system_profile.plugin_size()); |
| 68 EXPECT_EQ("p1", system_profile.plugin(0).name()); | 86 EXPECT_EQ("p1", system_profile.plugin(0).name()); |
| 69 EXPECT_EQ("p1.plugin", system_profile.plugin(0).filename()); | 87 EXPECT_EQ("p1.plugin", system_profile.plugin(0).filename()); |
| 70 EXPECT_EQ("1.5", system_profile.plugin(0).version()); | 88 EXPECT_EQ("1.5", system_profile.plugin(0).version()); |
| 71 EXPECT_TRUE(system_profile.plugin(0).is_pepper()); | 89 EXPECT_TRUE(system_profile.plugin(0).is_pepper()); |
| 72 EXPECT_EQ("p2", system_profile.plugin(1).name()); | 90 EXPECT_EQ("p2", system_profile.plugin(1).name()); |
| 73 EXPECT_EQ("p2.plugin", system_profile.plugin(1).filename()); | 91 EXPECT_EQ("p2.plugin", system_profile.plugin(1).filename()); |
| 74 EXPECT_EQ("2.0", system_profile.plugin(1).version()); | 92 EXPECT_EQ("2.0", system_profile.plugin(1).version()); |
| 75 EXPECT_FALSE(system_profile.plugin(1).is_pepper()); | 93 EXPECT_FALSE(system_profile.plugin(1).is_pepper()); |
| 76 | 94 |
| 77 // Now set some plugin stability stats for p2 and verify they're recorded. | 95 // Now set some plugin stability stats for p2 and verify they're recorded. |
| 78 scoped_ptr<base::DictionaryValue> plugin_dict(new base::DictionaryValue); | 96 scoped_ptr<base::DictionaryValue> plugin_dict(new base::DictionaryValue); |
| 79 plugin_dict->SetString(prefs::kStabilityPluginName, "p2"); | 97 plugin_dict->SetString(prefs::kStabilityPluginName, "p2"); |
| 80 plugin_dict->SetInteger(prefs::kStabilityPluginLaunches, 1); | 98 plugin_dict->SetInteger(prefs::kStabilityPluginLaunches, 1); |
| 81 plugin_dict->SetInteger(prefs::kStabilityPluginCrashes, 2); | 99 plugin_dict->SetInteger(prefs::kStabilityPluginCrashes, 2); |
| 82 plugin_dict->SetInteger(prefs::kStabilityPluginInstances, 3); | 100 plugin_dict->SetInteger(prefs::kStabilityPluginInstances, 3); |
| 83 plugin_dict->SetInteger(prefs::kStabilityPluginLoadingErrors, 4); | 101 plugin_dict->SetInteger(prefs::kStabilityPluginLoadingErrors, 4); |
| 84 { | 102 { |
| 85 ListPrefUpdate update(&prefs, prefs::kStabilityPluginStats); | 103 ListPrefUpdate update(prefs(), prefs::kStabilityPluginStats); |
| 86 update.Get()->Append(plugin_dict.release()); | 104 update.Get()->Append(plugin_dict.release()); |
| 87 } | 105 } |
| 88 | 106 |
| 89 provider.ProvideStabilityMetrics(&system_profile); | 107 provider.ProvideStabilityMetrics(&system_profile); |
| 90 | 108 |
| 91 const metrics::SystemProfileProto_Stability& stability = | 109 const metrics::SystemProfileProto_Stability& stability = |
| 92 system_profile.stability(); | 110 system_profile.stability(); |
| 93 ASSERT_EQ(1, stability.plugin_stability_size()); | 111 ASSERT_EQ(1, stability.plugin_stability_size()); |
| 94 EXPECT_EQ("p2", stability.plugin_stability(0).plugin().name()); | 112 EXPECT_EQ("p2", stability.plugin_stability(0).plugin().name()); |
| 95 EXPECT_EQ("p2.plugin", stability.plugin_stability(0).plugin().filename()); | 113 EXPECT_EQ("p2.plugin", stability.plugin_stability(0).plugin().filename()); |
| 96 EXPECT_EQ("2.0", stability.plugin_stability(0).plugin().version()); | 114 EXPECT_EQ("2.0", stability.plugin_stability(0).plugin().version()); |
| 97 EXPECT_FALSE(stability.plugin_stability(0).plugin().is_pepper()); | 115 EXPECT_FALSE(stability.plugin_stability(0).plugin().is_pepper()); |
| 98 EXPECT_EQ(1, stability.plugin_stability(0).launch_count()); | 116 EXPECT_EQ(1, stability.plugin_stability(0).launch_count()); |
| 99 EXPECT_EQ(2, stability.plugin_stability(0).crash_count()); | 117 EXPECT_EQ(2, stability.plugin_stability(0).crash_count()); |
| 100 EXPECT_EQ(3, stability.plugin_stability(0).instance_count()); | 118 EXPECT_EQ(3, stability.plugin_stability(0).instance_count()); |
| 101 EXPECT_EQ(4, stability.plugin_stability(0).loading_error_count()); | 119 EXPECT_EQ(4, stability.plugin_stability(0).loading_error_count()); |
| 102 } | 120 } |
| 121 |
| 122 TEST_F(PluginMetricsProviderTest, RecordCurrentStateWithDelay) { |
| 123 content::TestBrowserThreadBundle thread_bundle; |
| 124 |
| 125 PluginMetricsProvider provider(prefs()); |
| 126 |
| 127 int delay_ms = 10; |
| 128 EXPECT_TRUE(provider.RecordCurrentStateWithDelay(delay_ms)); |
| 129 EXPECT_FALSE(provider.RecordCurrentStateWithDelay(delay_ms)); |
| 130 |
| 131 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(delay_ms)); |
| 132 base::RunLoop().RunUntilIdle(); |
| 133 |
| 134 EXPECT_TRUE(provider.RecordCurrentStateWithDelay(delay_ms)); |
| 135 } |
| 136 |
| 137 TEST_F(PluginMetricsProviderTest, RecordCurrentStateIfPending) { |
| 138 content::TestBrowserThreadBundle thread_bundle; |
| 139 |
| 140 PluginMetricsProvider provider(prefs()); |
| 141 |
| 142 // First there should be no need to force RecordCurrentState. |
| 143 EXPECT_FALSE(provider.RecordCurrentStateIfPending()); |
| 144 |
| 145 // After delayed task is posted RecordCurrentStateIfPending should return |
| 146 // true. |
| 147 int delay_ms = 100000; |
| 148 EXPECT_TRUE(provider.RecordCurrentStateWithDelay(delay_ms)); |
| 149 EXPECT_TRUE(provider.RecordCurrentStateIfPending()); |
| 150 |
| 151 // If RecordCurrentStateIfPending was successful then we should be able to |
| 152 // post a new delayed task. |
| 153 EXPECT_TRUE(provider.RecordCurrentStateWithDelay(delay_ms)); |
| 154 } |
| 155 |
| 156 TEST_F(PluginMetricsProviderTest, ProvideStabilityMetricsWhenPendingTask) { |
| 157 content::TestBrowserThreadBundle thread_bundle; |
| 158 |
| 159 PluginMetricsProvider provider(prefs()); |
| 160 |
| 161 // Create plugin information for testing. |
| 162 std::vector<content::WebPluginInfo> plugins; |
| 163 plugins.push_back(CreateFakePluginInfo("p1", FILE_PATH_LITERAL("p1.plugin"), |
| 164 "1.5", true)); |
| 165 provider.SetPluginsForTesting(plugins); |
| 166 metrics::SystemProfileProto system_profile; |
| 167 provider.ProvideSystemProfileMetrics(&system_profile); |
| 168 |
| 169 // Increase number of created instances which should also start a delayed |
| 170 // task. |
| 171 content::ChildProcessData child_process_data(content::PROCESS_TYPE_PLUGIN); |
| 172 child_process_data.name = base::UTF8ToUTF16("p1"); |
| 173 provider.BrowserChildProcessInstanceCreated(child_process_data); |
| 174 |
| 175 // Call ProvideStabilityMetrics to check that it will force pending tasks to |
| 176 // be executed immediately. |
| 177 provider.ProvideStabilityMetrics(&system_profile); |
| 178 |
| 179 // Check current number of instances created. |
| 180 const metrics::SystemProfileProto_Stability& stability = |
| 181 system_profile.stability(); |
| 182 EXPECT_EQ(1, stability.plugin_stability(0).instance_count()); |
| 183 } |
| OLD | NEW |