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 #ifndef CHROME_BROWSER_PAGE_LOAD_METRICS_OBSERVERS_SECURITY_STATE_PAGE_LOAD_METRICS_OBSERVER_H_
6 #define CHROME_BROWSER_PAGE_LOAD_METRICS_OBSERVERS_SECURITY_STATE_PAGE_LOAD_METRICS_OBSERVER_H_
7 
8 #include <memory>
9 #include <string>
10 
11 #include "base/macros.h"
12 #include "components/page_load_metrics/browser/page_load_metrics_observer.h"
13 #include "components/security_state/core/security_state.h"
14 #include "content/public/browser/web_contents_observer.h"
15 #include "services/metrics/public/cpp/ukm_source_id.h"
16 
17 namespace content {
18 class BrowserContext;
19 }  // namespace content
20 
21 class SecurityStateTabHelper;
22 class SiteEngagementService;
23 
24 // Tracks the SecurityLevel of the page from the time it commits to the time it
25 // completes. This is uses to track metrics keyed on the SecurityLevel of the
26 // page. This has the same lifetime as a traditional PageLoadMetricsObserver,
27 // not a WebContentsObserver.
28 class SecurityStatePageLoadMetricsObserver
29     : public page_load_metrics::PageLoadMetricsObserver,
30       public content::WebContentsObserver {
31  public:
32   // Create a SecurityStatePageLoadMetricsObserver using the profile's
33   // SiteEngagementService, if it exists. Otherwise, creates a
34   // SecurityStatePageLoadMetricsObserver that will not track site engagement
35   // metrics.
36   static std::unique_ptr<page_load_metrics::PageLoadMetricsObserver>
37   MaybeCreateForProfile(content::BrowserContext* profile);
38 
39   static std::string GetEngagementDeltaHistogramNameForTesting(
40       security_state::SecurityLevel level);
41   static std::string GetEngagementFinalHistogramNameForTesting(
42       security_state::SecurityLevel level);
43   static std::string GetSecurityLevelPageEndReasonHistogramNameForTesting(
44       security_state::SecurityLevel level);
45   static std::string GetSafetyTipPageEndReasonHistogramNameForTesting(
46       security_state::SafetyTipStatus safety_tip_status);
47 
48   explicit SecurityStatePageLoadMetricsObserver(
49       SiteEngagementService* engagement_service);
50   ~SecurityStatePageLoadMetricsObserver() override;
51 
52   // page_load_metrics::PageLoadMetricsObserver:
53   ObservePolicy OnStart(content::NavigationHandle* navigation_handle,
54                         const GURL& currently_committed_url,
55                         bool started_in_foreground) override;
56   ObservePolicy OnCommit(content::NavigationHandle* navigation_handle,
57                          ukm::SourceId source_id) override;
58   void OnComplete(
59       const page_load_metrics::mojom::PageLoadTiming& timing) override;
60 
61   // content::WebContentsObserver:
62   void DidChangeVisibleSecurityState() override;
63 
64  private:
65   // If the SiteEngagementService does not exist, this will be null.
66   SiteEngagementService* engagement_service_ = nullptr;
67 
68   SecurityStateTabHelper* security_state_tab_helper_ = nullptr;
69   double initial_engagement_score_ = 0.0;
70   security_state::SecurityLevel initial_security_level_ = security_state::NONE;
71   security_state::SecurityLevel current_security_level_ = security_state::NONE;
72   ukm::SourceId source_id_ = ukm::kInvalidSourceId;
73 
74   DISALLOW_COPY_AND_ASSIGN(SecurityStatePageLoadMetricsObserver);
75 };
76 
77 #endif  // CHROME_BROWSER_PAGE_LOAD_METRICS_OBSERVERS_SECURITY_STATE_PAGE_LOAD_METRICS_OBSERVER_H_
78