| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
| 15 #include "components/metrics/proto/system_profile.pb.h" | 15 #include "components/metrics/proto/system_profile.pb.h" |
| 16 #include "components/prefs/pref_service.h" | 16 #include "components/prefs/pref_service.h" |
| 17 #include "components/prefs/scoped_user_pref_update.h" | 17 #include "components/prefs/scoped_user_pref_update.h" |
| 18 #include "components/prefs/testing_pref_service.h" | 18 #include "components/prefs/testing_pref_service.h" |
| 19 #include "content/public/browser/child_process_data.h" | 19 #include "content/public/browser/child_process_data.h" |
| 20 #include "content/public/common/process_type.h" | 20 #include "content/public/common/process_type.h" |
| 21 #include "content/public/common/webplugininfo.h" | 21 #include "content/public/common/webplugininfo.h" |
| 22 #include "content/public/test/test_browser_thread_bundle.h" | 22 #include "content/public/test/test_browser_thread_bundle.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 content::WebPluginInfo CreateFakePluginInfo( | 27 content::WebPluginInfo CreateFakePluginInfo( |
| 28 const std::string& name, | 28 const std::string& name, |
| 29 const base::FilePath::CharType* path, | 29 const base::FilePath::CharType* path, |
| 30 const std::string& version, | 30 const std::string& version) { |
| 31 bool is_pepper) { | |
| 32 content::WebPluginInfo plugin(base::UTF8ToUTF16(name), | 31 content::WebPluginInfo plugin(base::UTF8ToUTF16(name), |
| 33 base::FilePath(path), | 32 base::FilePath(path), |
| 34 base::UTF8ToUTF16(version), | 33 base::UTF8ToUTF16(version), |
| 35 base::string16()); | 34 base::string16()); |
| 36 if (is_pepper) | 35 plugin.type = content::WebPluginInfo::PLUGIN_TYPE_PEPPER_IN_PROCESS; |
| 37 plugin.type = content::WebPluginInfo::PLUGIN_TYPE_PEPPER_IN_PROCESS; | |
| 38 else | |
| 39 plugin.type = content::WebPluginInfo::PLUGIN_TYPE_NPAPI; | |
| 40 return plugin; | 36 return plugin; |
| 41 } | 37 } |
| 42 | 38 |
| 43 class PluginMetricsProviderTest : public ::testing::Test { | 39 class PluginMetricsProviderTest : public ::testing::Test { |
| 44 protected: | 40 protected: |
| 45 PluginMetricsProviderTest() | 41 PluginMetricsProviderTest() |
| 46 : prefs_(new TestingPrefServiceSimple) { | 42 : prefs_(new TestingPrefServiceSimple) { |
| 47 PluginMetricsProvider::RegisterPrefs(prefs()->registry()); | 43 PluginMetricsProvider::RegisterPrefs(prefs()->registry()); |
| 48 } | 44 } |
| 49 | 45 |
| 50 TestingPrefServiceSimple* prefs() { | 46 TestingPrefServiceSimple* prefs() { |
| 51 return prefs_.get(); | 47 return prefs_.get(); |
| 52 } | 48 } |
| 53 | 49 |
| 54 private: | 50 private: |
| 55 scoped_ptr<TestingPrefServiceSimple> prefs_; | 51 scoped_ptr<TestingPrefServiceSimple> prefs_; |
| 56 | 52 |
| 57 DISALLOW_COPY_AND_ASSIGN(PluginMetricsProviderTest); | 53 DISALLOW_COPY_AND_ASSIGN(PluginMetricsProviderTest); |
| 58 }; | 54 }; |
| 59 | 55 |
| 60 } // namespace | 56 } // namespace |
| 61 | 57 |
| 62 TEST_F(PluginMetricsProviderTest, IsPluginProcess) { | 58 TEST_F(PluginMetricsProviderTest, IsPluginProcess) { |
| 63 EXPECT_TRUE(PluginMetricsProvider::IsPluginProcess( | 59 EXPECT_TRUE(PluginMetricsProvider::IsPluginProcess( |
| 64 content::PROCESS_TYPE_PLUGIN)); | |
| 65 EXPECT_TRUE(PluginMetricsProvider::IsPluginProcess( | |
| 66 content::PROCESS_TYPE_PPAPI_PLUGIN)); | 60 content::PROCESS_TYPE_PPAPI_PLUGIN)); |
| 67 EXPECT_FALSE(PluginMetricsProvider::IsPluginProcess( | 61 EXPECT_FALSE(PluginMetricsProvider::IsPluginProcess( |
| 68 content::PROCESS_TYPE_GPU)); | 62 content::PROCESS_TYPE_GPU)); |
| 69 } | 63 } |
| 70 | 64 |
| 71 TEST_F(PluginMetricsProviderTest, Plugins) { | 65 TEST_F(PluginMetricsProviderTest, Plugins) { |
| 72 content::TestBrowserThreadBundle thread_bundle; | 66 content::TestBrowserThreadBundle thread_bundle; |
| 73 | 67 |
| 74 PluginMetricsProvider provider(prefs()); | 68 PluginMetricsProvider provider(prefs()); |
| 75 | 69 |
| 76 std::vector<content::WebPluginInfo> plugins; | 70 std::vector<content::WebPluginInfo> plugins; |
| 77 plugins.push_back(CreateFakePluginInfo("p1", FILE_PATH_LITERAL("p1.plugin"), | 71 plugins.push_back(CreateFakePluginInfo("p1", FILE_PATH_LITERAL("p1.plugin"), |
| 78 "1.5", true)); | 72 "1.5")); |
| 79 plugins.push_back(CreateFakePluginInfo("p2", FILE_PATH_LITERAL("p2.plugin"), | 73 plugins.push_back(CreateFakePluginInfo("p2", FILE_PATH_LITERAL("p2.plugin"), |
| 80 "2.0", false)); | 74 "2.0")); |
| 81 provider.SetPluginsForTesting(plugins); | 75 provider.SetPluginsForTesting(plugins); |
| 82 | 76 |
| 83 metrics::SystemProfileProto system_profile; | 77 metrics::SystemProfileProto system_profile; |
| 84 provider.ProvideSystemProfileMetrics(&system_profile); | 78 provider.ProvideSystemProfileMetrics(&system_profile); |
| 85 | 79 |
| 86 ASSERT_EQ(2, system_profile.plugin_size()); | 80 ASSERT_EQ(2, system_profile.plugin_size()); |
| 87 EXPECT_EQ("p1", system_profile.plugin(0).name()); | 81 EXPECT_EQ("p1", system_profile.plugin(0).name()); |
| 88 EXPECT_EQ("p1.plugin", system_profile.plugin(0).filename()); | 82 EXPECT_EQ("p1.plugin", system_profile.plugin(0).filename()); |
| 89 EXPECT_EQ("1.5", system_profile.plugin(0).version()); | 83 EXPECT_EQ("1.5", system_profile.plugin(0).version()); |
| 90 EXPECT_TRUE(system_profile.plugin(0).is_pepper()); | 84 EXPECT_TRUE(system_profile.plugin(0).is_pepper()); |
| 91 EXPECT_EQ("p2", system_profile.plugin(1).name()); | 85 EXPECT_EQ("p2", system_profile.plugin(1).name()); |
| 92 EXPECT_EQ("p2.plugin", system_profile.plugin(1).filename()); | 86 EXPECT_EQ("p2.plugin", system_profile.plugin(1).filename()); |
| 93 EXPECT_EQ("2.0", system_profile.plugin(1).version()); | 87 EXPECT_EQ("2.0", system_profile.plugin(1).version()); |
| 94 EXPECT_FALSE(system_profile.plugin(1).is_pepper()); | 88 EXPECT_TRUE(system_profile.plugin(1).is_pepper()); |
| 95 | 89 |
| 96 // Now set some plugin stability stats for p2 and verify they're recorded. | 90 // Now set some plugin stability stats for p2 and verify they're recorded. |
| 97 scoped_ptr<base::DictionaryValue> plugin_dict(new base::DictionaryValue); | 91 scoped_ptr<base::DictionaryValue> plugin_dict(new base::DictionaryValue); |
| 98 plugin_dict->SetString(prefs::kStabilityPluginName, "p2"); | 92 plugin_dict->SetString(prefs::kStabilityPluginName, "p2"); |
| 99 plugin_dict->SetInteger(prefs::kStabilityPluginLaunches, 1); | 93 plugin_dict->SetInteger(prefs::kStabilityPluginLaunches, 1); |
| 100 plugin_dict->SetInteger(prefs::kStabilityPluginCrashes, 2); | 94 plugin_dict->SetInteger(prefs::kStabilityPluginCrashes, 2); |
| 101 plugin_dict->SetInteger(prefs::kStabilityPluginInstances, 3); | 95 plugin_dict->SetInteger(prefs::kStabilityPluginInstances, 3); |
| 102 plugin_dict->SetInteger(prefs::kStabilityPluginLoadingErrors, 4); | 96 plugin_dict->SetInteger(prefs::kStabilityPluginLoadingErrors, 4); |
| 103 { | 97 { |
| 104 ListPrefUpdate update(prefs(), prefs::kStabilityPluginStats); | 98 ListPrefUpdate update(prefs(), prefs::kStabilityPluginStats); |
| 105 update.Get()->Append(plugin_dict.release()); | 99 update.Get()->Append(plugin_dict.release()); |
| 106 } | 100 } |
| 107 | 101 |
| 108 provider.ProvideStabilityMetrics(&system_profile); | 102 provider.ProvideStabilityMetrics(&system_profile); |
| 109 | 103 |
| 110 const metrics::SystemProfileProto_Stability& stability = | 104 const metrics::SystemProfileProto_Stability& stability = |
| 111 system_profile.stability(); | 105 system_profile.stability(); |
| 112 ASSERT_EQ(1, stability.plugin_stability_size()); | 106 ASSERT_EQ(1, stability.plugin_stability_size()); |
| 113 EXPECT_EQ("p2", stability.plugin_stability(0).plugin().name()); | 107 EXPECT_EQ("p2", stability.plugin_stability(0).plugin().name()); |
| 114 EXPECT_EQ("p2.plugin", stability.plugin_stability(0).plugin().filename()); | 108 EXPECT_EQ("p2.plugin", stability.plugin_stability(0).plugin().filename()); |
| 115 EXPECT_EQ("2.0", stability.plugin_stability(0).plugin().version()); | 109 EXPECT_EQ("2.0", stability.plugin_stability(0).plugin().version()); |
| 116 EXPECT_FALSE(stability.plugin_stability(0).plugin().is_pepper()); | 110 EXPECT_TRUE(stability.plugin_stability(0).plugin().is_pepper()); |
| 117 EXPECT_EQ(1, stability.plugin_stability(0).launch_count()); | 111 EXPECT_EQ(1, stability.plugin_stability(0).launch_count()); |
| 118 EXPECT_EQ(2, stability.plugin_stability(0).crash_count()); | 112 EXPECT_EQ(2, stability.plugin_stability(0).crash_count()); |
| 119 EXPECT_EQ(3, stability.plugin_stability(0).instance_count()); | 113 EXPECT_EQ(3, stability.plugin_stability(0).instance_count()); |
| 120 EXPECT_EQ(4, stability.plugin_stability(0).loading_error_count()); | 114 EXPECT_EQ(4, stability.plugin_stability(0).loading_error_count()); |
| 121 } | 115 } |
| 122 | 116 |
| 123 TEST_F(PluginMetricsProviderTest, RecordCurrentStateWithDelay) { | 117 TEST_F(PluginMetricsProviderTest, RecordCurrentStateWithDelay) { |
| 124 content::TestBrowserThreadBundle thread_bundle; | 118 content::TestBrowserThreadBundle thread_bundle; |
| 125 | 119 |
| 126 PluginMetricsProvider provider(prefs()); | 120 PluginMetricsProvider provider(prefs()); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 154 EXPECT_TRUE(provider.RecordCurrentStateWithDelay(delay_ms)); | 148 EXPECT_TRUE(provider.RecordCurrentStateWithDelay(delay_ms)); |
| 155 } | 149 } |
| 156 | 150 |
| 157 TEST_F(PluginMetricsProviderTest, ProvideStabilityMetricsWhenPendingTask) { | 151 TEST_F(PluginMetricsProviderTest, ProvideStabilityMetricsWhenPendingTask) { |
| 158 content::TestBrowserThreadBundle thread_bundle; | 152 content::TestBrowserThreadBundle thread_bundle; |
| 159 | 153 |
| 160 PluginMetricsProvider provider(prefs()); | 154 PluginMetricsProvider provider(prefs()); |
| 161 | 155 |
| 162 // Create plugin information for testing. | 156 // Create plugin information for testing. |
| 163 std::vector<content::WebPluginInfo> plugins; | 157 std::vector<content::WebPluginInfo> plugins; |
| 164 plugins.push_back(CreateFakePluginInfo("p1", FILE_PATH_LITERAL("p1.plugin"), | 158 plugins.push_back( |
| 165 "1.5", true)); | 159 CreateFakePluginInfo("p1", FILE_PATH_LITERAL("p1.plugin"), "1.5")); |
| 166 plugins.push_back(CreateFakePluginInfo("p2", FILE_PATH_LITERAL("p2.plugin"), | 160 plugins.push_back( |
| 167 "1.5", true)); | 161 CreateFakePluginInfo("p2", FILE_PATH_LITERAL("p2.plugin"), "1.5")); |
| 168 provider.SetPluginsForTesting(plugins); | 162 provider.SetPluginsForTesting(plugins); |
| 169 metrics::SystemProfileProto system_profile; | 163 metrics::SystemProfileProto system_profile; |
| 170 provider.ProvideSystemProfileMetrics(&system_profile); | 164 provider.ProvideSystemProfileMetrics(&system_profile); |
| 171 | 165 |
| 172 // Increase number of created instances which should also start a delayed | 166 // Increase number of process launches which should also start a delayed |
| 173 // task. | 167 // task. |
| 174 content::ChildProcessData child_process_data1(content::PROCESS_TYPE_PLUGIN); | 168 content::ChildProcessData child_process_data1( |
| 169 content::PROCESS_TYPE_PPAPI_PLUGIN); |
| 175 child_process_data1.name = base::UTF8ToUTF16("p1"); | 170 child_process_data1.name = base::UTF8ToUTF16("p1"); |
| 176 provider.BrowserChildProcessInstanceCreated(child_process_data1); | 171 provider.BrowserChildProcessHostConnected(child_process_data1); |
| 177 provider.BrowserChildProcessCrashed(child_process_data1, 1); | 172 provider.BrowserChildProcessCrashed(child_process_data1, 1); |
| 178 | 173 |
| 179 // A disconnect should not generate a crash event. | 174 // A disconnect should not generate a crash event. |
| 180 provider.BrowserChildProcessInstanceCreated(child_process_data1); | 175 provider.BrowserChildProcessHostConnected(child_process_data1); |
| 181 provider.BrowserChildProcessHostDisconnected(child_process_data1); | 176 provider.BrowserChildProcessHostDisconnected(child_process_data1); |
| 182 | 177 |
| 183 content::ChildProcessData child_process_data2( | 178 content::ChildProcessData child_process_data2( |
| 184 content::PROCESS_TYPE_PPAPI_PLUGIN); | 179 content::PROCESS_TYPE_PPAPI_PLUGIN); |
| 185 child_process_data2.name = base::UTF8ToUTF16("p2"); | 180 child_process_data2.name = base::UTF8ToUTF16("p2"); |
| 186 provider.BrowserChildProcessInstanceCreated(child_process_data2); | 181 provider.BrowserChildProcessHostConnected(child_process_data2); |
| 187 provider.BrowserChildProcessCrashed(child_process_data2, 1); | 182 provider.BrowserChildProcessCrashed(child_process_data2, 1); |
| 188 | 183 |
| 189 // A kill should generate a crash event | 184 // A kill should generate a crash event |
| 190 provider.BrowserChildProcessInstanceCreated(child_process_data2); | 185 provider.BrowserChildProcessHostConnected(child_process_data2); |
| 191 provider.BrowserChildProcessKilled(child_process_data2, 1); | 186 provider.BrowserChildProcessKilled(child_process_data2, 1); |
| 192 | 187 |
| 193 // Call ProvideStabilityMetrics to check that it will force pending tasks to | 188 // Call ProvideStabilityMetrics to check that it will force pending tasks to |
| 194 // be executed immediately. | 189 // be executed immediately. |
| 195 provider.ProvideStabilityMetrics(&system_profile); | 190 provider.ProvideStabilityMetrics(&system_profile); |
| 196 | 191 |
| 197 // Check current number of instances created. | 192 // Check current number of instances created. |
| 198 const metrics::SystemProfileProto_Stability& stability = | 193 const metrics::SystemProfileProto_Stability& stability = |
| 199 system_profile.stability(); | 194 system_profile.stability(); |
| 200 size_t found = 0; | 195 size_t found = 0; |
| 201 EXPECT_EQ(stability.plugin_stability_size(), 2); | 196 EXPECT_EQ(stability.plugin_stability_size(), 2); |
| 202 for (int i = 0; i < 2; i++) { | 197 for (int i = 0; i < 2; i++) { |
| 203 std::string name = stability.plugin_stability(i).plugin().name(); | 198 std::string name = stability.plugin_stability(i).plugin().name(); |
| 204 if (name == "p1") { | 199 if (name == "p1") { |
| 205 EXPECT_EQ(2, stability.plugin_stability(i).instance_count()); | 200 EXPECT_EQ(2, stability.plugin_stability(i).launch_count()); |
| 206 EXPECT_EQ(1, stability.plugin_stability(i).crash_count()); | 201 EXPECT_EQ(1, stability.plugin_stability(i).crash_count()); |
| 207 found++; | 202 found++; |
| 208 } else if (name == "p2") { | 203 } else if (name == "p2") { |
| 209 EXPECT_EQ(2, stability.plugin_stability(i).instance_count()); | 204 EXPECT_EQ(2, stability.plugin_stability(i).launch_count()); |
| 210 EXPECT_EQ(2, stability.plugin_stability(i).crash_count()); | 205 EXPECT_EQ(2, stability.plugin_stability(i).crash_count()); |
| 211 found++; | 206 found++; |
| 212 } else { | 207 } else { |
| 213 GTEST_FAIL() << "Unexpected plugin name : " << name; | 208 GTEST_FAIL() << "Unexpected plugin name : " << name; |
| 214 } | 209 } |
| 215 } | 210 } |
| 216 EXPECT_EQ(found, 2U); | 211 EXPECT_EQ(found, 2U); |
| 217 } | 212 } |
| OLD | NEW |