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

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: Rebasing... 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.
Charlie Reis 2017/07/20 17:06:16 Are we ok with this for the process model decision
jkarlin 2017/07/20 17:19:43 Actually, that comment (the once an ad always an a
Łukasz Anforowicz 2017/07/20 17:33:51 I think this is okay for now.
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 priveleges).
Charlie Reis 2017/07/20 17:06:16 nit: privileges
Łukasz Anforowicz 2017/07/20 17:33:51 Done.
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 class NavigationHandleAdsData : public base::SupportsUserData::Data {
Charlie Reis 2017/07/20 17:06:16 nit: Please add comment, along the lines of "Assoc
Łukasz Anforowicz 2017/07/20 17:33:51 Done.
64 public:
65 static NavigationHandleAdsData* GetOrCreate(
66 content::NavigationHandle* navigation_handle) {
67 DCHECK(navigation_handle);
68 NavigationHandleAdsData* ads_data = static_cast<NavigationHandleAdsData*>(
69 navigation_handle->GetUserData(kUserDataKey));
70 if (!ads_data) {
71 std::unique_ptr<NavigationHandleAdsData> new_ads_data =
72 base::MakeUnique<NavigationHandleAdsData>();
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";
Charlie Reis 2017/07/20 17:06:16 nit: Move above the NavigationHandleAdsData class.
Łukasz Anforowicz 2017/07/20 17:33:51 This wouldn't compile (error: use of undeclared id
Charlie Reis 2017/07/20 20:10:53 Ah, sorry, I didn't see it was a member, just that
101
102 } // namespace
103
104 const AdTypes& GetDetectedAdTypes(
105 content::NavigationHandle* navigation_handle) {
106 DCHECK(navigation_handle);
107
108 // Verify that we are called after the navigation is ready to commit.
109 // Being called late enough in the navigation process ensures that
110 // 1) subresource filter got a chance to run its heuristics
111 // 2) the URL looked at by IsGoogleAd is the final, redirected URL.
112 DCHECK((navigation_handle->GetNetErrorCode() != net::Error::OK) ||
113 navigation_handle->GetRenderFrameHost());
114
115 NavigationHandleAdsData* ads_data =
116 NavigationHandleAdsData::GetOrCreate(navigation_handle);
117 if (IsGoogleAd(navigation_handle))
118 ads_data->ad_types().set(AD_TYPE_GOOGLE);
119 return ads_data->ad_types();
120 }
121
122 void SetDetectedAdType(content::NavigationHandle* navigation_handle,
123 AdType type) {
124 DCHECK(navigation_handle);
125 NavigationHandleAdsData::GetOrCreate(navigation_handle)->ad_types().set(type);
126 }
127
128 } // namespace page_load_metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698