Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6206)

Unified Diff: chrome/browser/page_load_metrics/ads_detection.cc

Issue 2946113002: Use FrameIsAd to decide whether to isolate a frame in TopDocumentIsolation mode. (Closed)
Patch Set: Addressing CR feedback from jkarlin@ and csharrison@ (and also small self-review tweaks.) Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/page_load_metrics/ads_detection.cc
diff --git a/chrome/browser/page_load_metrics/ads_detection.cc b/chrome/browser/page_load_metrics/ads_detection.cc
new file mode 100644
index 0000000000000000000000000000000000000000..a770deb9abda91f1bdda924d5e6a788b0ce423a0
--- /dev/null
+++ b/chrome/browser/page_load_metrics/ads_detection.cc
@@ -0,0 +1,108 @@
+// Copyright 2017 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.
+
+#include "chrome/browser/page_load_metrics/ads_detection.h"
+
+#include <memory>
+#include <string>
+
+#include "base/logging.h"
+#include "base/strings/string_util.h"
+#include "base/supports_user_data.h"
+#include "content/public/browser/navigation_handle.h"
+#include "content/public/browser/render_frame_host.h"
+#include "content/public/browser/web_contents.h"
+#include "url/gurl.h"
+
+namespace {
+
+bool IsGoogleAd(content::NavigationHandle* navigation_handle) {
+ // Because sub-resource filtering isn't always enabled, and doesn't work
+ // well in monitoring mode (no CSS enforcement), it's difficult to identify
+ // ads. Google ads are prevalent and easy to track, so we'll start by
+ // tracking those. Note that the frame name can be very large, so be careful
+ // to avoid full string searches if possible.
+ // TODO(jkarlin): Track other ad networks that are easy to identify.
+
+ // 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're just measuring
+ // bytes and not granting security priveleges).
+ content::RenderFrameHost* current_frame_host =
+ navigation_handle->GetWebContents()->UnsafeFindFrameByFrameTreeNodeId(
+ navigation_handle->GetFrameTreeNodeId());
+ if (current_frame_host) {
+ const std::string& frame_name = current_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;
+ }
+ }
+
+ const GURL& frame_url = navigation_handle->GetURL();
+ if (frame_url.host_piece() == "tpc.googlesyndication.com" &&
+ base::StartsWith(frame_url.path_piece(), "/safeframe",
+ base::CompareCase::SENSITIVE)) {
+ return true;
+ }
+
+ return false;
+}
+
+class NavigationHandleAdsData : public base::SupportsUserData::Data {
+ public:
+ static NavigationHandleAdsData* GetOrCreate(
+ content::NavigationHandle* navigation_handle) {
+ DCHECK(navigation_handle);
+ NavigationHandleAdsData* ads_data = static_cast<NavigationHandleAdsData*>(
Charlie Harrison 2017/07/18 19:24:41 Optional: I would much prefer the following, to av
Łukasz Anforowicz 2017/07/18 20:04:29 Done (= I switched to base::MakeUnique<...>; OTOH
Charlie Harrison 2017/07/18 20:07:28 Right, my typo, you can just return raw_ads_data d
+ navigation_handle->GetUserData(kUserDataKey));
+ if (!ads_data) {
+ ads_data = new NavigationHandleAdsData;
+
+ // It is safe to retain |ads_data| raw pointer, despite passing an
+ // ownership to SetUserData, because |navigation_handle| will keep
+ // |ads_data| alive until the |navigation_handle| is destroyed.
+ navigation_handle->SetUserData(
+ kUserDataKey, std::unique_ptr<NavigationHandleAdsData>(ads_data));
+
+ if (IsGoogleAd(navigation_handle))
+ ads_data->ad_types().set(AD_TYPE_GOOGLE);
+ }
+
+ return ads_data;
+ }
+
+ ~NavigationHandleAdsData() override {}
+
+ AdTypes& ad_types() { return ad_types_; }
+ const AdTypes& ad_types() const { return ad_types_; }
+
+ private:
+ NavigationHandleAdsData() = default;
+
+ AdTypes ad_types_;
+
+ static const char kUserDataKey[];
+};
Charlie Harrison 2017/07/18 19:24:41 DISALLOW_COPY_AND_ASSIGN?
Łukasz Anforowicz 2017/07/18 20:04:29 Ooops. Yes - DISALLOW_COPY_AND_ASSIGN!
+
+const char NavigationHandleAdsData::kUserDataKey[] = "AdsData";
+
+} // namespace
+
+const AdTypes& GetDetectedAdTypes(
+ content::NavigationHandle* navigation_handle) {
+ DCHECK(navigation_handle);
+ return NavigationHandleAdsData::GetOrCreate(navigation_handle)->ad_types();
+}
+
+void SetDetectedAdTypes(content::NavigationHandle* navigation_handle,
+ AdType type) {
+ DCHECK(navigation_handle);
+ NavigationHandleAdsData::GetOrCreate(navigation_handle)->ad_types().set(type);
+}

Powered by Google App Engine
This is Rietveld 408576698