Created a trigger utils file for methods common to safebrowsing triggers.

Bug: 965587
Change-Id: I38bebf7e51d02990c990398795c487318d2cef01
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1622925
Reviewed-by: Varun Khaneja <vakh@chromium.org>
Reviewed-by: Luke Z <lpz@chromium.org>
Reviewed-by: Daniel Rubery <drubery@chromium.org>
Commit-Queue: Archana Simha <archanasimha@google.com>
Cr-Commit-Position: refs/heads/master@{#663277}
diff --git a/components/safe_browsing/triggers/BUILD.gn b/components/safe_browsing/triggers/BUILD.gn
index 5cac8f5..94e713b5 100644
--- a/components/safe_browsing/triggers/BUILD.gn
+++ b/components/safe_browsing/triggers/BUILD.gn
@@ -39,6 +39,17 @@
   ]
 }
 
+source_set("trigger_util") {
+  sources = [
+    "trigger_util.cc",
+    "trigger_util.h",
+  ]
+  deps = [
+    ":triggers",
+    "//content/public/browser",
+  ]
+}
+
 source_set("ad_sampler_trigger") {
   sources = [
     "ad_sampler_trigger.cc",
@@ -46,6 +57,7 @@
   ]
   deps = [
     ":trigger_throttler",
+    ":trigger_util",
     ":triggers",
     "//base:base",
     "//components/safe_browsing:features",
diff --git a/components/safe_browsing/triggers/ad_sampler_trigger.cc b/components/safe_browsing/triggers/ad_sampler_trigger.cc
index 2656fc4..43a40ef 100644
--- a/components/safe_browsing/triggers/ad_sampler_trigger.cc
+++ b/components/safe_browsing/triggers/ad_sampler_trigger.cc
@@ -17,6 +17,7 @@
 #include "components/safe_browsing/features.h"
 #include "components/safe_browsing/triggers/trigger_manager.h"
 #include "components/safe_browsing/triggers/trigger_throttler.h"
+#include "components/safe_browsing/triggers/trigger_util.h"
 #include "components/security_interstitials/content/unsafe_resource.h"
 #include "content/public/browser/browser_task_traits.h"
 #include "content/public/browser/browser_thread.h"
@@ -68,34 +69,6 @@
   return result;
 }
 
-bool DetectGoogleAd(content::RenderFrameHost* render_frame_host,
-                    const GURL& frame_url) {
-  // TODO(crbug.com/742397): This function is temporarily copied from
-  // c/b/page_load_metrics/observers/ads_page_load_metrics_observer.cc
-  // This code should be updated to use shared infrastructure when available.
-
-  // In case the navigation aborted, look up the RFH by the Frame Tree Node
-  // ID. It returns the committed frame host or the initial frame host for the
-  // frame if no committed host exists. Using a previous host is fine because
-  // once a frame has an ad we always consider it to have an ad.
-  // We use the unsafe method of FindFrameByFrameTreeNodeId because we're not
-  // concerned with which process the frame lives on (we only want to know if an
-  // ad could be present on the page right now).
-  if (render_frame_host) {
-    const std::string& frame_name = render_frame_host->GetFrameName();
-    if (base::StartsWith(frame_name, "google_ads_iframe",
-                         base::CompareCase::SENSITIVE) ||
-        base::StartsWith(frame_name, "google_ads_frame",
-                         base::CompareCase::SENSITIVE)) {
-      return true;
-    }
-  }
-
-  return frame_url.host_piece() == "tpc.googlesyndication.com" &&
-         base::StartsWith(frame_url.path_piece(), "/safeframe",
-                          base::CompareCase::SENSITIVE);
-}
-
 bool ShouldSampleAd(const size_t frequency_denominator) {
   return frequency_denominator != kAdSamplerFrequencyDisabled &&
          (base::RandUint64() % frequency_denominator) == 0;
diff --git a/components/safe_browsing/triggers/trigger_util.cc b/components/safe_browsing/triggers/trigger_util.cc
new file mode 100644
index 0000000..85a0863
--- /dev/null
+++ b/components/safe_browsing/triggers/trigger_util.cc
@@ -0,0 +1,40 @@
+// Copyright 2019 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// This file contains some useful utilities for the safe_browsing/triggers
+// classes
+
+#include "components/safe_browsing/triggers/trigger_util.h"
+
+#include <string>
+
+#include "content/public/browser/render_frame_host.h"
+
+namespace safe_browsing {
+
+bool DetectGoogleAd(content::RenderFrameHost* render_frame_host,
+                    const GURL& frame_url) {
+  // In case the navigation aborted, look up the RFH by the Frame Tree Node
+  // ID. It returns the committed frame host or the initial frame host for the
+  // frame if no committed host exists. Using a previous host is fine because
+  // once a frame has an ad we always consider it to have an ad.
+  // We use the unsafe method of FindFrameByFrameTreeNodeId because we're not
+  // concerned with which process the frame lives on (we only want to know if an
+  // ad could be present on the page right now).
+  if (render_frame_host) {
+    const std::string& frame_name = render_frame_host->GetFrameName();
+    if (base::StartsWith(frame_name, "google_ads_iframe",
+                         base::CompareCase::SENSITIVE) ||
+        base::StartsWith(frame_name, "google_ads_frame",
+                         base::CompareCase::SENSITIVE)) {
+      return true;
+    }
+  }
+
+  return frame_url.host_piece() == "tpc.googlesyndication.com" &&
+         base::StartsWith(frame_url.path_piece(), "/safeframe",
+                          base::CompareCase::SENSITIVE);
+}
+
+}  // namespace safe_browsing
diff --git a/components/safe_browsing/triggers/trigger_util.h b/components/safe_browsing/triggers/trigger_util.h
new file mode 100644
index 0000000..61d44ea
--- /dev/null
+++ b/components/safe_browsing/triggers/trigger_util.h
@@ -0,0 +1,24 @@
+// Copyright 2019 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef COMPONENTS_SAFE_BROWSING_TRIGGERS_TRIGGER_UTIL_H_
+#define COMPONENTS_SAFE_BROWSING_TRIGGERS_TRIGGER_UTIL_H_
+
+#include "url/gurl.h"
+
+// This file contains some useful utilities for the safe_browsing/triggers
+// classes
+namespace content {
+class RenderFrameHost;
+}
+
+namespace safe_browsing {
+
+// Based on heuristics, guesses whether |render_frame_host| is showing a Google
+// Ad, or the |frame_url| is a Google Ad URL.
+bool DetectGoogleAd(content::RenderFrameHost* render_frame_host,
+                    const GURL& frame_url);
+}  // namespace safe_browsing
+
+#endif  // COMPONENTS_SAFE_BROWSING_TRIGGERS_TRIGGER_UTIL_H_