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

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 2 more CR comments from csharrison@. 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
jkarlin 2017/07/19 13:32:36 page_load_metrics namespace
Łukasz Anforowicz 2017/07/19 18:11:29 Done.
21 namespace {
22
23 bool IsGoogleAd(content::NavigationHandle* navigation_handle) {
24 // Because sub-resource filtering isn't always enabled, and doesn't work
25 // well in monitoring mode (no CSS enforcement), it's difficult to identify
26 // ads. Google ads are prevalent and easy to track, so we'll start by
27 // tracking those. Note that the frame name can be very large, so be careful
28 // to avoid full string searches if possible.
29 // TODO(jkarlin): Track other ad networks that are easy to identify.
30
31 // In case the navigation aborted, look up the RFH by the Frame Tree Node
32 // ID. It returns the committed frame host or the initial frame host for the
33 // frame if no committed host exists. Using a previous host is fine because
34 // once a frame has an ad we always consider it to have an ad.
35 // We use the unsafe method of FindFrameByFrameTreeNodeId because we're not
36 // concerned with which process the frame lives on (we're just measuring
37 // bytes and not granting security priveleges).
38 content::RenderFrameHost* current_frame_host =
39 navigation_handle->GetWebContents()->UnsafeFindFrameByFrameTreeNodeId(
40 navigation_handle->GetFrameTreeNodeId());
41 if (current_frame_host) {
42 const std::string& frame_name = current_frame_host->GetFrameName();
43 if (base::StartsWith(frame_name, "google_ads_iframe",
44 base::CompareCase::SENSITIVE) ||
45 base::StartsWith(frame_name, "google_ads_frame",
46 base::CompareCase::SENSITIVE)) {
47 return true;
48 }
49 }
50
51 const GURL& frame_url = navigation_handle->GetURL();
52 if (frame_url.host_piece() == "tpc.googlesyndication.com" &&
53 base::StartsWith(frame_url.path_piece(), "/safeframe",
54 base::CompareCase::SENSITIVE)) {
55 return true;
56 }
57
58 return false;
59 }
60
61 class NavigationHandleAdsData : public base::SupportsUserData::Data {
62 public:
63 static NavigationHandleAdsData* GetOrCreate(
64 content::NavigationHandle* navigation_handle) {
65 DCHECK(navigation_handle);
66 NavigationHandleAdsData* ads_data = static_cast<NavigationHandleAdsData*>(
67 navigation_handle->GetUserData(kUserDataKey));
68 if (!ads_data) {
69 std::unique_ptr<NavigationHandleAdsData> new_ads_data =
70 base::MakeUnique<NavigationHandleAdsData>();
71 if (IsGoogleAd(navigation_handle))
jkarlin 2017/07/19 13:32:36 The NavigationHandleAdsData might be created earli
Łukasz Anforowicz 2017/07/19 18:11:29 Good point (and sort of "done"), but: 1. Repeated
jkarlin 2017/07/20 16:51:51 The calculation cost isn't terrible, but I'd prefe
Łukasz Anforowicz 2017/07/20 17:33:51 Done.
72 new_ads_data->ad_types().set(AD_TYPE_GOOGLE);
73
74 // It is safe to retain |ads_data| raw pointer, despite passing an
75 // ownership of |new_ads_data| to SetUserData, because |navigation_handle|
76 // will keep the NavigationHandleAdsData instance alive until the
77 // |navigation_handle| is destroyed.
78 ads_data = new_ads_data.get();
79
80 navigation_handle->SetUserData(kUserDataKey, std::move(new_ads_data));
81 }
82
83 return ads_data;
84 }
85
86 NavigationHandleAdsData() = default;
87 ~NavigationHandleAdsData() override {}
88
89 AdTypes& ad_types() { return ad_types_; }
90 const AdTypes& ad_types() const { return ad_types_; }
91
92 private:
93 AdTypes ad_types_;
94
95 static const char kUserDataKey[];
96
97 DISALLOW_COPY_AND_ASSIGN(NavigationHandleAdsData);
98 };
99
100 const char NavigationHandleAdsData::kUserDataKey[] = "AdsData";
101
102 } // namespace
103
104 const AdTypes& GetDetectedAdTypes(
105 content::NavigationHandle* navigation_handle) {
106 DCHECK(navigation_handle);
jkarlin 2017/07/19 13:32:35 An indirect (but better than nothing) way to verif
Łukasz Anforowicz 2017/07/19 18:11:29 Done, although this is somewhat tricky in case the
Łukasz Anforowicz 2017/07/19 22:44:26 Actually this didn't quite work with PlzNavigate:
jkarlin 2017/07/20 16:51:51 Sorry, I should have realized that it wouldn't wor
Łukasz Anforowicz 2017/07/20 17:33:51 I've added a TODO here.
107 return NavigationHandleAdsData::GetOrCreate(navigation_handle)->ad_types();
108 }
109
110 void SetDetectedAdTypes(content::NavigationHandle* navigation_handle,
111 AdType type) {
112 DCHECK(navigation_handle);
113 NavigationHandleAdsData::GetOrCreate(navigation_handle)->ad_types().set(type);
114 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698