1 // Copyright 2019 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_POPUP_TRIGGER_H_
6 #define COMPONENTS_SAFE_BROWSING_CONTENT_TRIGGERS_AD_POPUP_TRIGGER_H_
7 
8 #include "base/macros.h"
9 #include "base/memory/weak_ptr.h"
10 #include "content/public/browser/web_contents_user_data.h"
11 
12 class PrefService;
13 
14 namespace history {
15 class HistoryService;
16 }
17 
18 namespace network {
19 class SharedURLLoaderFactory;
20 }  // namespace network
21 
22 namespace safe_browsing {
23 class TriggerManager;
24 
25 // Metric for tracking what the Ad Popup trigger does on each navigation.
26 extern const char kAdPopupTriggerActionMetricName[];
27 
28 enum class AdPopupTriggerAction {
29   // An event occurred that caused the trigger to perform its checks.
30   POPUP_CHECK = 0,
31   // A popup cause by an ad was detected and a report was collected.
32   POPUP_REPORTED = 1,
33   // No ad was detected.
34   POPUP_NO_GOOGLE_AD = 2,
35   // An ad was detected on the page causing a popup and could have been
36   // reported, but the trigger manager rejected the report (eg: because user is
37   // incognito or has not opted into extended reporting).
38   POPUP_COULD_NOT_START_REPORT = 3,
39   // Daily quota for ads that caused blocked popups was met.
40   POPUP_DAILY_QUOTA_EXCEEDED = 4,
41   // New events must be added before kMaxValue, and the value of kMaxValue
42   // updated.
43   kMaxValue = POPUP_DAILY_QUOTA_EXCEEDED
44 };
45 
46 // This class is notified when a popup caused by an ad in the browser is
47 // blocked. If the Ad is a Google Ad, this class sends a report to Google.
48 // Design doc: go/extending-chrind-q2-2019-1
49 class AdPopupTrigger : public content::WebContentsUserData<AdPopupTrigger> {
50  public:
51   ~AdPopupTrigger() override;
52 
53   static void CreateForWebContents(
54       content::WebContents* web_contents,
55       TriggerManager* trigger_manager,
56       PrefService* prefs,
57       scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
58       history::HistoryService* history_service);
59 
60   void PopupWasBlocked(content::RenderFrameHost* render_frame);
61 
62  private:
63   friend class AdPopupTriggerTest;
64   friend class content::WebContentsUserData<AdPopupTrigger>;
65 
66   AdPopupTrigger(
67       content::WebContents* web_contents,
68       TriggerManager* trigger_manager,
69       PrefService* prefs,
70       scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
71       history::HistoryService* history_service);
72 
73   // Called to create an ad popup report.
74   void CreateAdPopupReport();
75 
76   // Sets a task runner to use for tests.
77   void SetTaskRunnerForTest(
78       scoped_refptr<base::SingleThreadTaskRunner> task_runner);
79 
80   // WebContents of the current tab.
81   content::WebContents* web_contents_;
82 
83   // The delay (in milliseconds) to wait before starting a report. Can be
84   // ovewritten for tests.
85   int64_t start_report_delay_ms_;
86 
87   // The delay (in milliseconds) to wait before finishing a report. Can be
88   // overwritten for tests.
89   int64_t finish_report_delay_ms_;
90 
91   // TriggerManager gets called if this trigger detects apopup caused by ad and
92   // wants to collect some data about it. Not owned.
93   TriggerManager* trigger_manager_;
94 
95   PrefService* prefs_;
96   scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
97   history::HistoryService* history_service_;
98 
99   // Task runner for posting delayed tasks. Normally set to the runner for the
100   // UI thread, but can be overwritten for tests.
101   scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
102 
103   base::WeakPtrFactory<AdPopupTrigger> weak_ptr_factory_{this};
104   WEB_CONTENTS_USER_DATA_KEY_DECL();
105 
106   DISALLOW_COPY_AND_ASSIGN(AdPopupTrigger);
107 };
108 
109 }  // namespace safe_browsing
110 
111 #endif  // COMPONENTS_SAFE_BROWSING_CONTENT_TRIGGERS_AD_POPUP_TRIGGER_H_
112