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

Side by Side Diff: chrome/browser/safe_browsing/safe_browsing_navigation_observer_manager.cc

Issue 2302913003: Add SafeBrowsingNavigationObserver to listen to navigation events (Closed)
Patch Set: nit Created 4 years, 2 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 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 static const double kUserGestureTTLInSecond = 0.5;
Nathan Parker 2016/10/14 23:52:49 Add a comment to explain what this does, or how it
Jialiu Lin 2016/10/17 19:17:15 Done.
24
25 // static
26 bool SafeBrowsingNavigationObserverManager::IsUserGestureExpired(
27 const base::Time& timestamp) {
28 double now = base::Time::Now().ToDoubleT();
29 double timestamp_in_double = timestamp.ToDoubleT();
30 return now > timestamp_in_double
31 ? now - timestamp_in_double > kUserGestureTTLInSecond
Nathan Parker 2016/10/14 23:52:49 Should be safe to just return now - timestamp_in_
Jialiu Lin 2016/10/17 19:17:15 ACK. I would like to return expired (true) if tim
32 : true;
33 }
34
35 // static
36 GURL SafeBrowsingNavigationObserverManager::ClearEmptyURLRef(const GURL& url) {
37 if (url.has_ref() && url.ref().empty()) {
38 url::Replacements<char> replacements;
39 replacements.ClearRef();
40 return url.ReplaceComponents(replacements);
41 }
42 return url;
43 }
44
45 SafeBrowsingNavigationObserverManager::SafeBrowsingNavigationObserverManager() {
46 registrar_.Add(this, chrome::NOTIFICATION_RETARGETING,
47 content::NotificationService::AllSources());
48 }
49
50 void SafeBrowsingNavigationObserverManager::RecordNavigationEvent(
51 const GURL& nav_event_key,
52 NavigationEvent* nav_event) {
53 auto insertion_result = navigation_map_.insert(
54 std::make_pair(nav_event_key, std::vector<NavigationEvent>()));
55
56 insertion_result.first->second.push_back(std::move(*nav_event));
Nathan Parker 2016/10/14 23:52:49 Does the std::move destroy the nav_event? If so, m
Jialiu Lin 2016/10/17 19:17:15 Done.
57 }
58
59 void SafeBrowsingNavigationObserverManager::RecordUserGestureForWebContents(
60 content::WebContents* web_contents,
61 const base::Time& timestamp) {
62 auto insertion_result =
63 user_gesture_map_.insert(std::make_pair(web_contents, timestamp));
64 if (!insertion_result.second)
Nathan Parker 2016/10/14 23:52:49 nit: add comment, like // Update the timestamp if
Jialiu Lin 2016/10/17 19:17:15 Agree. comment added.
65 insertion_result.first->second = timestamp;
66 }
67
68 void SafeBrowsingNavigationObserverManager::OnUserGestureConsumed(
69 content::WebContents* web_contents,
70 const base::Time& timestamp) {
71 auto it = user_gesture_map_.find(web_contents);
72 if (it == user_gesture_map_.end())
73 return;
74 if (timestamp >= it->second)
75 user_gesture_map_.erase(web_contents);
Nathan Parker 2016/10/14 23:52:49 Some comments here would help, since I'm not clear
Jialiu Lin 2016/10/17 19:17:15 Done.
76 }
77
78 void SafeBrowsingNavigationObserverManager::RecordHostToIpMapping(
79 const std::string& host,
80 const std::string& ip) {
81 auto insert_result = host_to_ip_map_.insert(
82 std::make_pair(host, std::vector<ResolvedIPAddress>()));
83 if (!insert_result.second) {
84 // host_to_ip_map already contains this key.
85 // If this IP is already in the vector, we update its timestamp.
86 for (auto& vector_entry : insert_result.first->second) {
87 if (vector_entry.ip == host) {
88 vector_entry.timestamp = base::Time::Now();
89 return;
90 }
91 }
92 }
93 // If this is a new IP of this host, and we added to the end of the vector.
94 insert_result.first->second.push_back(
95 ResolvedIPAddress(base::Time::Now(), ip));
96 }
97
98 void SafeBrowsingNavigationObserverManager::OnWebContentDestroyed(
99 content::WebContents* web_contents) {
100 user_gesture_map_.erase(web_contents);
101 // TODO (jialiul): Will add other clean up tasks shortly.
102 }
103
104 SafeBrowsingNavigationObserverManager::
105 ~SafeBrowsingNavigationObserverManager() {}
106
107 void SafeBrowsingNavigationObserverManager::Observe(
108 int type,
109 const content::NotificationSource& source,
110 const content::NotificationDetails& details) {
111 if (type == chrome::NOTIFICATION_RETARGETING)
112 RecordRetargeting(details);
113 }
114
115 void SafeBrowsingNavigationObserverManager::RecordRetargeting(
116 const content::NotificationDetails& details) {
117 const RetargetingDetails* retargeting_detail =
118 content::Details<const RetargetingDetails>(details).ptr();
119 DCHECK(retargeting_detail);
120 content::WebContents* source_contents =
121 retargeting_detail->source_web_contents;
122 content::WebContents* target_contents =
123 retargeting_detail->target_web_contents;
124 DCHECK(source_contents);
125 DCHECK(target_contents);
126
127 content::RenderFrameHost* rfh = content::RenderFrameHost::FromID(
128 retargeting_detail->source_render_process_id,
129 retargeting_detail->source_render_frame_id);
130 const GURL& target_url =
131 SafeBrowsingNavigationObserverManager::ClearEmptyURLRef(
Nathan Parker 2016/10/14 23:52:49 Add a comment as to why ClearEmptyURLRef() is this
Jialiu Lin 2016/10/17 19:17:15 Done.
132 retargeting_detail->target_url);
133
134 navigation_map_.insert(
135 std::make_pair(target_url, std::vector<NavigationEvent>()));
Nathan Parker 2016/10/14 23:52:49 If you save the iterator this returns, you can do
Jialiu Lin 2016/10/17 19:17:15 Thanks for catching these.
136
137 NavigationEvent nav_event;
138 nav_event.source_url =
Nathan Parker 2016/10/14 23:52:49 since source_url is already empty, you could do if
Jialiu Lin 2016/10/17 19:17:15 Done.
139 rfh ? SafeBrowsingNavigationObserverManager::ClearEmptyURLRef(
140 rfh->GetLastCommittedURL())
141 : GURL();
142 nav_event.source_tab_id = SessionTabHelper::IdForTab(source_contents);
143 nav_event.target_url = target_url;
144 nav_event.target_tab_id = SessionTabHelper::IdForTab(target_contents);
145 nav_event.frame_id = rfh ? rfh->GetFrameTreeNodeId() : -1;
146 nav_event.main_frame_url =
147 SafeBrowsingNavigationObserverManager::ClearEmptyURLRef(
148 source_contents->GetLastCommittedURL());
149 nav_event.timestamp = base::Time::Now();
Nathan Parker 2016/10/14 23:52:49 This is already set in the nav_event ctor. You co
Jialiu Lin 2016/10/17 19:17:15 Yes, this is redundant. Fixed.
150 auto it = user_gesture_map_.find(source_contents);
151 if (it != user_gesture_map_.end() &&
152 !SafeBrowsingNavigationObserverManager::IsUserGestureExpired(
153 it->second)) {
154 nav_event.is_user_initiated = true;
155 OnUserGestureConsumed(it->first, it->second);
156 } else {
157 nav_event.is_user_initiated = false;
158 }
159
160 navigation_map_[target_url].push_back(std::move(nav_event));
161 }
162
163 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698