1 // Copyright 2017 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 #ifndef COMPONENTS_SAFE_BROWSING_CONTENT_TRIGGERS_AD_SAMPLER_TRIGGER_H_ 6 #define COMPONENTS_SAFE_BROWSING_CONTENT_TRIGGERS_AD_SAMPLER_TRIGGER_H_ 7 8 #include "base/macros.h" 9 #include "base/memory/weak_ptr.h" 10 #include "content/public/browser/web_contents_observer.h" 11 #include "content/public/browser/web_contents_user_data.h" 12 13 class PrefService; 14 15 namespace history { 16 class HistoryService; 17 } 18 19 namespace network { 20 class SharedURLLoaderFactory; 21 } // namespace network 22 23 namespace safe_browsing { 24 class TriggerManager; 25 26 // Param name of the denominator for controlling sampling frequency. 27 extern const char kAdSamplerFrequencyDenominatorParam[]; 28 29 // Default frequency for the ad sampler, if not configured in Finch. 30 extern const size_t kAdSamplerDefaultFrequency; 31 32 // A frequency denominator with this value indicates sampling is disabled. 33 extern const size_t kAdSamplerFrequencyDisabled; 34 35 // Metric for tracking what the Ad Sampler trigger does on each navigation. 36 extern const char kAdSamplerTriggerActionMetricName[]; 37 38 // Actions performed by this trigger. These values are written to logs. New enum 39 // values can be added, but existing enums must never be renumbered or deleted 40 // and reused. 41 enum AdSamplerTriggerAction { 42 // An event occurred that caused the trigger to perform its checks. 43 TRIGGER_CHECK = 0, 44 // An ad was detected and a sample was collected. 45 AD_SAMPLED = 1, 46 // An ad was detected but no sample was taken to honour sampling frequency. 47 NO_SAMPLE_AD_SKIPPED_FOR_FREQUENCY = 2, 48 // No ad was detected. 49 NO_SAMPLE_NO_AD = 3, 50 // An ad was detected and could have been sampled, but the trigger manager 51 // rejected the report (eg: because a report was already in progress). 52 NO_SAMPLE_COULD_NOT_START_REPORT = 4, 53 // New actions must be added before MAX_ACTIONS. 54 MAX_ACTIONS 55 }; 56 57 // This class periodically checks for Google ads on the page and may decide to 58 // send a report to Google with the ad's structure for further analysis. 59 class AdSamplerTrigger : public content::WebContentsObserver, 60 public content::WebContentsUserData<AdSamplerTrigger> { 61 public: 62 ~AdSamplerTrigger() override; 63 64 static void CreateForWebContents( 65 content::WebContents* web_contents, 66 TriggerManager* trigger_manager, 67 PrefService* prefs, 68 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, 69 history::HistoryService* history_service); 70 71 // content::WebContentsObserver implementation. 72 void DidFinishLoad(content::RenderFrameHost* render_frame_host, 73 const GURL& validated_url) override; 74 75 private: 76 friend class AdSamplerTriggerTest; 77 friend class content::WebContentsUserData<AdSamplerTrigger>; 78 FRIEND_TEST_ALL_PREFIXES(AdSamplerTriggerTestFinch, 79 FrequencyDenominatorFeature); 80 81 AdSamplerTrigger( 82 content::WebContents* web_contents, 83 TriggerManager* trigger_manager, 84 PrefService* prefs, 85 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, 86 history::HistoryService* history_service); 87 88 // Called to create an ad sample report. 89 void CreateAdSampleReport(); 90 91 // Sets |sampler_frequency_denominator_| for tests. 92 void SetSamplerFrequencyForTest(size_t denominator); 93 94 // Sets a task runner to use for tests. 95 void SetTaskRunnerForTest( 96 scoped_refptr<base::SingleThreadTaskRunner> task_runner); 97 98 // Ad samples will be collected with frequency 99 // 1/|sampler_frequency_denominator_| 100 size_t sampler_frequency_denominator_; 101 102 // The delay (in milliseconds) to wait before starting a report. Can be 103 // ovewritten for tests. 104 int64_t start_report_delay_ms_; 105 106 // The delay (in milliseconds) to wait before finishing a report. Can be 107 // overwritten for tests. 108 int64_t finish_report_delay_ms_; 109 110 // TriggerManager gets called if this trigger detects an ad and wants to 111 // collect some data about it. Not owned. 112 TriggerManager* trigger_manager_; 113 114 PrefService* prefs_; 115 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_; 116 history::HistoryService* history_service_; 117 118 // Task runner for posting delayed tasks. Normally set to the runner for the 119 // UI thread, but can be overwritten for tests. 120 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; 121 122 base::WeakPtrFactory<AdSamplerTrigger> weak_ptr_factory_{this}; 123 124 WEB_CONTENTS_USER_DATA_KEY_DECL(); 125 126 DISALLOW_COPY_AND_ASSIGN(AdSamplerTrigger); 127 }; 128 129 } // namespace safe_browsing 130 131 #endif // COMPONENTS_SAFE_BROWSING_CONTENT_TRIGGERS_AD_SAMPLER_TRIGGER_H_ 132