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

Side by Side 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 creis@. 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 unified diff | Download patch
OLDNEW
(Empty)
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 #include "chrome/browser/page_load_metrics/ads_detection.h"
6
7 #include <memory>
8 #include <string>
9 #include <utility>
10
11 #include "base/logging.h"
12 #include "base/macros.h"
13 #include "base/memory/ptr_util.h"
14 #include "base/strings/string_util.h"
15 #include "base/supports_user_data.h"
16 #include "content/public/browser/navigation_handle.h"
17 #include "content/public/browser/render_frame_host.h"
18 #include "content/public/browser/web_contents.h"
19 #include "url/gurl.h"
20
21 namespace page_load_metrics {
22
23 namespace {
24
25 bool IsGoogleAd(content::NavigationHandle* navigation_handle) {
26 // Because sub-resource filtering isn't always enabled, and doesn't work
27 // well in monitoring mode (no CSS enforcement), it's difficult to identify
28 // ads. Google ads are prevalent and easy to track, so we'll start by
29 // tracking those. Note that the frame name can be very large, so be careful
30 // to avoid full string searches if possible.
31 // TODO(jkarlin): Track other ad networks that are easy to identify.
32
33 // In case the navigation aborted, look up the RFH by the Frame Tree Node
34 // ID. It returns the committed frame host or the initial frame host for the
35 // frame if no committed host exists. Using a previous host is fine because
36 // once a frame has an ad we always consider it to have an ad.
37 // We use the unsafe method of FindFrameByFrameTreeNodeId because we're not
38 // concerned with which process the frame lives on (we're just measuring
39 // bytes and not granting security privileges).
40 content::RenderFrameHost* current_frame_host =
41 navigation_handle->GetWebContents()->UnsafeFindFrameByFrameTreeNodeId(
42 navigation_handle->GetFrameTreeNodeId());
43 if (current_frame_host) {
44 const std::string& frame_name = current_frame_host->GetFrameName();
45 if (base::StartsWith(frame_name, "google_ads_iframe",
46 base::CompareCase::SENSITIVE) ||
47 base::StartsWith(frame_name, "google_ads_frame",
48 base::CompareCase::SENSITIVE)) {
49 return true;
50 }
51 }
52
53 const GURL& frame_url = navigation_handle->GetURL();
54 if (frame_url.host_piece() == "tpc.googlesyndication.com" &&
55 base::StartsWith(frame_url.path_piece(), "/safeframe",
56 base::CompareCase::SENSITIVE)) {
57 return true;
58 }
59
60 return false;
61 }
62
63 // Associates the detected AdTypes for a navigation with its NavigationHandle.
64 class NavigationHandleAdsData : public base::SupportsUserData::Data {
65 public:
66 static NavigationHandleAdsData* GetOrCreate(
67 content::NavigationHandle* navigation_handle) {
68 DCHECK(navigation_handle);
69 NavigationHandleAdsData* ads_data = static_cast<NavigationHandleAdsData*>(
70 navigation_handle->GetUserData(kUserDataKey));
71 if (!ads_data) {
72 std::unique_ptr<NavigationHandleAdsData> new_ads_data =
73 base::MakeUnique<NavigationHandleAdsData>();
74
75 // It is safe to retain |ads_data| raw pointer, despite passing an
76 // ownership of |new_ads_data| to SetUserData, because |navigation_handle|
77 // will keep the NavigationHandleAdsData instance alive until the
78 // |navigation_handle| is destroyed.
79 ads_data = new_ads_data.get();
80
81 navigation_handle->SetUserData(kUserDataKey, std::move(new_ads_data));
82 }
83
84 return ads_data;
85 }
86
87 NavigationHandleAdsData() = default;
88 ~NavigationHandleAdsData() override {}
89
90 AdTypes& ad_types() { return ad_types_; }
91 const AdTypes& ad_types() const { return ad_types_; }
92
93 void CalculateGoogleAdsIfNecessary(
94 content::NavigationHandle* navigation_handle) {
95 if (has_calculated_google_ads_)
96 return;
97 has_calculated_google_ads_ = true;
98
99 if (IsGoogleAd(navigation_handle))
100 ad_types_.set(AD_TYPE_GOOGLE);
101 }
102
103 private:
104 AdTypes ad_types_;
105 bool has_calculated_google_ads_ = false;
106
107 static const char kUserDataKey[];
108
109 DISALLOW_COPY_AND_ASSIGN(NavigationHandleAdsData);
110 };
111
112 const char NavigationHandleAdsData::kUserDataKey[] = "AdsData";
113
114 } // namespace
115
116 const AdTypes& GetDetectedAdTypes(
117 content::NavigationHandle* navigation_handle) {
118 DCHECK(navigation_handle);
119
120 // TODO(lukasza): https://crbug.com/746638: Add a DCHECK that verifies that
121 // GetDetectedAdTypes is called not earlier than when the |navigation_handle|
122 // is ready to commit.
123
124 NavigationHandleAdsData* ads_data =
125 NavigationHandleAdsData::GetOrCreate(navigation_handle);
126 ads_data->CalculateGoogleAdsIfNecessary(navigation_handle);
127 return ads_data->ad_types();
128 }
129
130 void SetDetectedAdType(content::NavigationHandle* navigation_handle,
131 AdType type) {
132 DCHECK(navigation_handle);
133 NavigationHandleAdsData::GetOrCreate(navigation_handle)->ad_types().set(type);
134 }
135
136 } // namespace page_load_metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698