Chromium Code Reviews| 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/observers/ads_detection.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 | |
| 9 #include "base/strings/string_util.h" | |
| 10 #include "base/supports_user_data.h" | |
| 11 #include "content/public/browser/navigation_handle.h" | |
| 12 #include "content/public/browser/render_frame_host.h" | |
| 13 #include "url/gurl.h" | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 class NavigationHandleAdsData : public base::SupportsUserData::Data { | |
| 18 public: | |
| 19 static NavigationHandleAdsData* From( | |
| 20 content::NavigationHandle* navigation_handle) { | |
| 21 DCHECK(navigation_handle); | |
| 22 return static_cast<NavigationHandleAdsData*>( | |
| 23 navigation_handle->GetUserData(kUserDataKey)); | |
| 24 } | |
| 25 | |
| 26 static NavigationHandleAdsData* Create( | |
| 27 content::NavigationHandle* navigation_handle) { | |
| 28 DCHECK(navigation_handle); | |
| 29 DCHECK(!From(navigation_handle)); | |
| 30 | |
| 31 NavigationHandleAdsData* ads_data = new NavigationHandleAdsData; | |
| 32 navigation_handle->SetUserData( | |
| 33 kUserDataKey, std::unique_ptr<NavigationHandleAdsData>(ads_data)); | |
| 34 return ads_data; // Safe, because navigation_handle will keep ads_data | |
| 35 // alive until the navigation_handle is destroyed. | |
| 36 } | |
| 37 | |
| 38 ~NavigationHandleAdsData() override {} | |
| 39 | |
| 40 AdTypes& ad_types() { return ad_types_; } | |
| 41 const AdTypes& ad_types() const { return ad_types_; } | |
| 42 | |
| 43 private: | |
| 44 NavigationHandleAdsData() = default; | |
| 45 | |
| 46 AdTypes ad_types_; | |
| 47 | |
| 48 static const char kUserDataKey[]; | |
| 49 }; | |
| 50 | |
| 51 const char NavigationHandleAdsData::kUserDataKey[] = "AdsData"; | |
| 52 | |
| 53 bool IsGoogleAd(content::NavigationHandle* navigation_handle) { | |
| 54 // Because sub-resource filtering isn't always enabled, and doesn't work | |
| 55 // well in monitoring mode (no CSS enforcement), it's difficult to identify | |
| 56 // ads. Google ads are prevalent and easy to track, so we'll start by | |
| 57 // tracking those. Note that the frame name can be very large, so be careful | |
| 58 // to avoid full string searches if possible. | |
| 59 // TODO(jkarlin): Track other ad networks that are easy to identify. | |
| 60 | |
| 61 content::RenderFrameHost* current_frame_host = | |
| 62 navigation_handle->GetRenderFrameHost(); | |
| 63 if (current_frame_host) { | |
| 64 const std::string& frame_name = current_frame_host->GetFrameName(); | |
| 65 if (base::StartsWith(frame_name, "google_ads_iframe", | |
| 66 base::CompareCase::SENSITIVE) || | |
| 67 base::StartsWith(frame_name, "google_ads_frame", | |
| 68 base::CompareCase::SENSITIVE)) { | |
| 69 return true; | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 const GURL& frame_url = navigation_handle->GetURL(); | |
| 74 if (frame_url.host_piece() == "tpc.googlesyndication.com" && | |
| 75 base::StartsWith(frame_url.path_piece(), "/safeframe", | |
| 76 base::CompareCase::SENSITIVE)) { | |
| 77 return true; | |
| 78 } | |
| 79 | |
| 80 return false; | |
| 81 } | |
| 82 | |
| 83 } // namespace | |
| 84 | |
| 85 AdTypes GetAdDetectionHeuristics(content::NavigationHandle* navigation_handle) { | |
| 86 AdTypes ad_types; | |
| 87 | |
| 88 if (IsGoogleAd(navigation_handle)) | |
|
jkarlin
2017/07/18 17:34:44
Perhaps:
AdTypes GetAdDetectionHeuristics(content
Łukasz Anforowicz
2017/07/18 19:06:11
Good point. This probably means that NavigationHa
| |
| 89 ad_types.set(AD_TYPE_GOOGLE); | |
| 90 | |
| 91 const NavigationHandleAdsData* ads_data = | |
| 92 NavigationHandleAdsData::From(navigation_handle); | |
| 93 if (ads_data) | |
| 94 ad_types |= ads_data->ad_types(); | |
| 95 | |
| 96 return ad_types; | |
| 97 } | |
| 98 | |
| 99 void SetAdDetectionHeuristics(content::NavigationHandle* navigation_handle, | |
| 100 AdType type) { | |
| 101 DCHECK(navigation_handle); | |
| 102 NavigationHandleAdsData* ads_data = | |
| 103 NavigationHandleAdsData::Create(navigation_handle); | |
| 104 ads_data->ad_types() |= type; | |
| 105 } | |
| OLD | NEW |