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