| OLD | NEW | 
|---|
| (Empty) |  | 
|  | 1 // Copyright 2016 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/safe_browsing/safe_browsing_navigation_observer_manager
     .h" | 
|  | 6 | 
|  | 7 #include "base/memory/ptr_util.h" | 
|  | 8 #include "base/time/time.h" | 
|  | 9 #include "chrome/browser/chrome_notification_types.h" | 
|  | 10 #include "chrome/browser/safe_browsing/safe_browsing_navigation_observer.h" | 
|  | 11 #include "chrome/browser/sessions/session_tab_helper.h" | 
|  | 12 #include "chrome/browser/tab_contents/retargeting_details.h" | 
|  | 13 #include "content/public/browser/navigation_details.h" | 
|  | 14 #include "content/public/browser/notification_service.h" | 
|  | 15 #include "content/public/browser/notification_types.h" | 
|  | 16 #include "content/public/browser/render_frame_host.h" | 
|  | 17 #include "content/public/browser/render_process_host.h" | 
|  | 18 #include "content/public/browser/web_contents.h" | 
|  | 19 | 
|  | 20 using content::WebContents; | 
|  | 21 namespace safe_browsing { | 
|  | 22 | 
|  | 23 // The expiration period of a user gesture. Any user gesture that happened 1.0 | 
|  | 24 // second ago will be considered as expired and not relevant to upcoming | 
|  | 25 // navigation events. | 
|  | 26 static const double kUserGestureTTLInSecond = 1.0; | 
|  | 27 | 
|  | 28 // static | 
|  | 29 bool SafeBrowsingNavigationObserverManager::IsUserGestureExpired( | 
|  | 30     const base::Time& timestamp) { | 
|  | 31   double now = base::Time::Now().ToDoubleT(); | 
|  | 32   double timestamp_in_double = timestamp.ToDoubleT(); | 
|  | 33   return now > timestamp_in_double | 
|  | 34              ? now - timestamp_in_double > kUserGestureTTLInSecond | 
|  | 35              : true; | 
|  | 36 } | 
|  | 37 | 
|  | 38 // static | 
|  | 39 GURL SafeBrowsingNavigationObserverManager::ClearEmptyRef(const GURL& url) { | 
|  | 40   if (url.has_ref() && url.ref().empty()) { | 
|  | 41     url::Replacements<char> replacements; | 
|  | 42     replacements.ClearRef(); | 
|  | 43     return url.ReplaceComponents(replacements); | 
|  | 44   } | 
|  | 45   return url; | 
|  | 46 } | 
|  | 47 | 
|  | 48 SafeBrowsingNavigationObserverManager::SafeBrowsingNavigationObserverManager() { | 
|  | 49   registrar_.Add(this, chrome::NOTIFICATION_RETARGETING, | 
|  | 50                  content::NotificationService::AllSources()); | 
|  | 51 } | 
|  | 52 | 
|  | 53 void SafeBrowsingNavigationObserverManager::RecordNavigationEvent( | 
|  | 54     const GURL& nav_event_key, | 
|  | 55     NavigationEvent* nav_event) { | 
|  | 56   auto insertion_result = navigation_map_.insert( | 
|  | 57       std::make_pair(nav_event_key, std::vector<NavigationEvent>())); | 
|  | 58 | 
|  | 59   insertion_result.first->second.push_back(std::move(*nav_event)); | 
|  | 60 } | 
|  | 61 | 
|  | 62 void SafeBrowsingNavigationObserverManager::RecordUserGestureForWebContents( | 
|  | 63     content::WebContents* web_contents, | 
|  | 64     const base::Time& timestamp) { | 
|  | 65   auto insertion_result = | 
|  | 66       user_gesture_map_.insert(std::make_pair(web_contents, timestamp)); | 
|  | 67   // Update the timestamp if entry already exists. | 
|  | 68   if (!insertion_result.second) | 
|  | 69     insertion_result.first->second = timestamp; | 
|  | 70 } | 
|  | 71 | 
|  | 72 void SafeBrowsingNavigationObserverManager::OnUserGestureConsumed( | 
|  | 73     content::WebContents* web_contents, | 
|  | 74     const base::Time& timestamp) { | 
|  | 75   auto it = user_gesture_map_.find(web_contents); | 
|  | 76   // Remove entry from |user_gesture_map_| as a user_gesture is consumed by | 
|  | 77   // a navigation event. | 
|  | 78   if (it != user_gesture_map_.end() && timestamp >= it->second) | 
|  | 79     user_gesture_map_.erase(it); | 
|  | 80 } | 
|  | 81 | 
|  | 82 void SafeBrowsingNavigationObserverManager::RecordHostToIpMapping( | 
|  | 83     const std::string& host, | 
|  | 84     const std::string& ip) { | 
|  | 85   auto insert_result = host_to_ip_map_.insert( | 
|  | 86       std::make_pair(host, std::vector<ResolvedIPAddress>())); | 
|  | 87   if (!insert_result.second) { | 
|  | 88     // host_to_ip_map already contains this key. | 
|  | 89     // If this IP is already in the vector, we update its timestamp. | 
|  | 90     for (auto& vector_entry : insert_result.first->second) { | 
|  | 91       if (vector_entry.ip == host) { | 
|  | 92         vector_entry.timestamp = base::Time::Now(); | 
|  | 93         return; | 
|  | 94       } | 
|  | 95     } | 
|  | 96   } | 
|  | 97   // If this is a new IP of this host, and we added to the end of the vector. | 
|  | 98   insert_result.first->second.push_back( | 
|  | 99       ResolvedIPAddress(base::Time::Now(), ip)); | 
|  | 100 } | 
|  | 101 | 
|  | 102 void SafeBrowsingNavigationObserverManager::OnWebContentDestroyed( | 
|  | 103     content::WebContents* web_contents) { | 
|  | 104   user_gesture_map_.erase(web_contents); | 
|  | 105   // TODO (jialiul): Will add other clean up tasks shortly. | 
|  | 106 } | 
|  | 107 | 
|  | 108 SafeBrowsingNavigationObserverManager:: | 
|  | 109     ~SafeBrowsingNavigationObserverManager() {} | 
|  | 110 | 
|  | 111 void SafeBrowsingNavigationObserverManager::Observe( | 
|  | 112     int type, | 
|  | 113     const content::NotificationSource& source, | 
|  | 114     const content::NotificationDetails& details) { | 
|  | 115   if (type == chrome::NOTIFICATION_RETARGETING) | 
|  | 116     RecordRetargeting(details); | 
|  | 117 } | 
|  | 118 | 
|  | 119 void SafeBrowsingNavigationObserverManager::RecordRetargeting( | 
|  | 120     const content::NotificationDetails& details) { | 
|  | 121   const RetargetingDetails* retargeting_detail = | 
|  | 122       content::Details<const RetargetingDetails>(details).ptr(); | 
|  | 123   DCHECK(retargeting_detail); | 
|  | 124   content::WebContents* source_contents = | 
|  | 125       retargeting_detail->source_web_contents; | 
|  | 126   content::WebContents* target_contents = | 
|  | 127       retargeting_detail->target_web_contents; | 
|  | 128   DCHECK(source_contents); | 
|  | 129   DCHECK(target_contents); | 
|  | 130 | 
|  | 131   content::RenderFrameHost* rfh = content::RenderFrameHost::FromID( | 
|  | 132       retargeting_detail->source_render_process_id, | 
|  | 133       retargeting_detail->source_render_frame_id); | 
|  | 134   // Remove the "#" at the end of URL, since it does not point to any actual | 
|  | 135   // page fragment ID. | 
|  | 136   const GURL& target_url = SafeBrowsingNavigationObserverManager::ClearEmptyRef( | 
|  | 137       retargeting_detail->target_url); | 
|  | 138 | 
|  | 139   NavigationEvent nav_event; | 
|  | 140   if (rfh) { | 
|  | 141     nav_event.source_url = SafeBrowsingNavigationObserverManager::ClearEmptyRef( | 
|  | 142         rfh->GetLastCommittedURL()); | 
|  | 143   } | 
|  | 144   nav_event.source_tab_id = SessionTabHelper::IdForTab(source_contents); | 
|  | 145   nav_event.source_main_frame_url = | 
|  | 146       SafeBrowsingNavigationObserverManager::ClearEmptyRef( | 
|  | 147           source_contents->GetLastCommittedURL()); | 
|  | 148   nav_event.original_request_url = target_url; | 
|  | 149   nav_event.actual_target_url = target_url; | 
|  | 150   nav_event.target_tab_id = SessionTabHelper::IdForTab(target_contents); | 
|  | 151   nav_event.frame_id = rfh ? rfh->GetFrameTreeNodeId() : -1; | 
|  | 152   auto it = user_gesture_map_.find(source_contents); | 
|  | 153   if (it != user_gesture_map_.end() && | 
|  | 154       !SafeBrowsingNavigationObserverManager::IsUserGestureExpired( | 
|  | 155           it->second)) { | 
|  | 156     nav_event.is_user_initiated = true; | 
|  | 157     OnUserGestureConsumed(it->first, it->second); | 
|  | 158   } else { | 
|  | 159     nav_event.is_user_initiated = false; | 
|  | 160   } | 
|  | 161 | 
|  | 162   auto insertion_result = navigation_map_.insert( | 
|  | 163       std::make_pair(target_url, std::vector<NavigationEvent>())); | 
|  | 164   insertion_result.first->second.push_back(std::move(nav_event)); | 
|  | 165 } | 
|  | 166 | 
|  | 167 }  // namespace safe_browsing | 
| OLD | NEW | 
|---|