Devlin Cronin | 9a9206e | 2018-09-28 23:39:51 | [diff] [blame] | 1 | // Copyright 2018 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 | #include "chrome/browser/extensions/installed_loader.h" |
| 6 | |
| 7 | #include "base/macros.h" |
| 8 | #include "base/test/metrics/histogram_tester.h" |
Devlin Cronin | 9a9206e | 2018-09-28 23:39:51 | [diff] [blame] | 9 | #include "chrome/browser/extensions/extension_service.h" |
| 10 | #include "chrome/browser/extensions/extension_service_test_base.h" |
| 11 | #include "chrome/browser/extensions/permissions_updater.h" |
| 12 | #include "chrome/browser/extensions/scripting_permissions_modifier.h" |
| 13 | #include "chrome/browser/profiles/profile.h" |
| 14 | #include "extensions/common/extension_builder.h" |
| 15 | #include "extensions/common/extension_features.h" |
| 16 | |
| 17 | namespace extensions { |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | constexpr const char kHasWithheldHostsHistogram[] = |
| 22 | "Extensions.RuntimeHostPermissions.ExtensionHasWithheldHosts"; |
| 23 | constexpr const char kGrantedHostCountHistogram[] = |
| 24 | "Extensions.RuntimeHostPermissions.GrantedHostCount"; |
| 25 | |
| 26 | } // namespace |
| 27 | |
| 28 | class InstalledLoaderUnitTest : public ExtensionServiceTestBase { |
| 29 | public: |
| 30 | InstalledLoaderUnitTest() {} |
| 31 | ~InstalledLoaderUnitTest() override = default; |
| 32 | |
| 33 | void SetUp() override { |
| 34 | ExtensionServiceTestBase::SetUp(); |
| 35 | InitializeEmptyExtensionService(); |
| 36 | } |
| 37 | |
| 38 | const Extension* AddExtension(); |
| 39 | |
| 40 | private: |
| 41 | DISALLOW_COPY_AND_ASSIGN(InstalledLoaderUnitTest); |
| 42 | }; |
| 43 | |
| 44 | const Extension* InstalledLoaderUnitTest::AddExtension() { |
| 45 | // Metrics aren't recorded for unpacked extensions, so we need to make sure |
| 46 | // the extension has an INTERNAL location. |
| 47 | constexpr Manifest::Location kManifestLocation = Manifest::INTERNAL; |
| 48 | scoped_refptr<const Extension> extension = ExtensionBuilder("test") |
| 49 | .AddPermissions({"<all_urls>"}) |
| 50 | .SetLocation(kManifestLocation) |
| 51 | .Build(); |
| 52 | PermissionsUpdater updater(profile()); |
| 53 | updater.InitializePermissions(extension.get()); |
| 54 | updater.GrantActivePermissions(extension.get()); |
| 55 | service()->AddExtension(extension.get()); |
| 56 | |
| 57 | return extension.get(); |
| 58 | } |
| 59 | |
| 60 | TEST_F(InstalledLoaderUnitTest, |
Devlin Cronin | 9a9206e | 2018-09-28 23:39:51 | [diff] [blame] | 61 | RuntimeHostPermissions_Metrics_HasWithheldHosts_False) { |
Devlin Cronin | 9a9206e | 2018-09-28 23:39:51 | [diff] [blame] | 62 | AddExtension(); |
| 63 | |
| 64 | base::HistogramTester histograms; |
| 65 | InstalledLoader loader(service()); |
| 66 | loader.RecordExtensionsMetricsForTesting(); |
| 67 | |
| 68 | // The extension didn't have withheld hosts, so a single `false` record |
| 69 | // should be present. |
| 70 | histograms.ExpectUniqueSample(kHasWithheldHostsHistogram, false, 1); |
| 71 | // Granted host counts should only be recorded if the extension had withheld |
| 72 | // hosts. |
| 73 | histograms.ExpectTotalCount(kGrantedHostCountHistogram, 0); |
| 74 | } |
| 75 | |
| 76 | TEST_F(InstalledLoaderUnitTest, |
| 77 | RuntimeHostPermissions_Metrics_HasWithheldHosts_True) { |
Devlin Cronin | 9a9206e | 2018-09-28 23:39:51 | [diff] [blame] | 78 | const Extension* extension = AddExtension(); |
| 79 | ScriptingPermissionsModifier(profile(), extension) |
| 80 | .SetWithholdHostPermissions(true); |
| 81 | |
| 82 | base::HistogramTester histograms; |
| 83 | InstalledLoader loader(service()); |
| 84 | loader.RecordExtensionsMetricsForTesting(); |
| 85 | |
| 86 | // The extension had withheld hosts, so a single `true` record should be |
| 87 | // present. |
| 88 | histograms.ExpectUniqueSample(kHasWithheldHostsHistogram, true, 1); |
| 89 | // There were no granted hosts, so a single `0` record should be present. |
| 90 | constexpr int kGrantedHostCount = 0; |
| 91 | constexpr int kEmitCount = 1; |
| 92 | histograms.ExpectUniqueSample(kGrantedHostCountHistogram, kGrantedHostCount, |
| 93 | kEmitCount); |
| 94 | } |
| 95 | |
| 96 | TEST_F(InstalledLoaderUnitTest, |
| 97 | RuntimeHostPermissions_Metrics_GrantedHostCount) { |
Devlin Cronin | 9a9206e | 2018-09-28 23:39:51 | [diff] [blame] | 98 | const Extension* extension = AddExtension(); |
| 99 | ScriptingPermissionsModifier modifier(profile(), extension); |
| 100 | modifier.SetWithholdHostPermissions(true); |
| 101 | modifier.GrantHostPermission(GURL("https://example.com/")); |
| 102 | modifier.GrantHostPermission(GURL("https://chromium.org/")); |
| 103 | |
| 104 | base::HistogramTester histograms; |
| 105 | InstalledLoader loader(service()); |
| 106 | loader.RecordExtensionsMetricsForTesting(); |
| 107 | |
| 108 | histograms.ExpectUniqueSample(kHasWithheldHostsHistogram, true, 1); |
| 109 | // The extension had granted hosts, so a single `2` record should be present. |
| 110 | constexpr int kGrantedHostCount = 2; |
| 111 | constexpr int kEmitCount = 1; |
| 112 | histograms.ExpectUniqueSample(kGrantedHostCountHistogram, kGrantedHostCount, |
| 113 | kEmitCount); |
| 114 | } |
| 115 | |
| 116 | } // namespace extensions |