Chromium Code Reviews| 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 #include "chrome/browser/safe_browsing/safe_browsing_navigation_observer.h" | |
|
Charlie Reis
2016/09/16 23:35:04
nit: Add blank line before.
Jialiu Lin
2016/09/22 21:30:28
Done.
| |
| 5 | |
| 6 #include "base/memory/ptr_util.h" | |
| 7 #include "base/time/time.h" | |
| 8 #include "chrome/browser/chrome_notification_types.h" | |
| 9 #include "chrome/browser/sessions/session_tab_helper.h" | |
| 10 #include "chrome/browser/tab_contents/retargeting_details.h" | |
| 11 #include "content/public/browser/navigation_controller.h" | |
| 12 #include "content/public/browser/navigation_details.h" | |
| 13 #include "content/public/browser/navigation_entry.h" | |
| 14 #include "content/public/browser/navigation_handle.h" | |
| 15 #include "content/public/browser/notification_service.h" | |
| 16 #include "content/public/browser/notification_types.h" | |
| 17 #include "content/public/browser/render_frame_host.h" | |
| 18 #include "content/public/browser/render_process_host.h" | |
| 19 #include "content/public/browser/resource_request_details.h" | |
| 20 #include "content/public/browser/web_contents.h" | |
| 21 #include "content/public/common/resource_type.h" | |
| 22 #include "ui/base/page_transition_types.h" | |
| 23 | |
| 24 namespace safe_browsing { | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 // Returns |time| as seconds since the epoch. | |
| 29 double SecondsFromTime(const base::Time& time) { | |
|
Charlie Reis
2016/09/16 23:35:04
Why do we need this?
Jialiu Lin
2016/09/22 21:30:28
removed.
| |
| 30 return 1000000 * time.ToDoubleT(); | |
| 31 } | |
| 32 | |
| 33 } // namespace | |
| 34 | |
| 35 // SafeBrowsingNavigationObserver::NavigationEvent----------------------------- | |
| 36 | |
| 37 SafeBrowsingNavigationObserver::NavigationEvent::NavigationEvent() | |
| 38 : source_url(), | |
| 39 source_tab_id(-1), | |
| 40 target_url(), | |
| 41 target_tab_id(-1), | |
| 42 frame_id(-1), | |
| 43 main_frame_url(), | |
| 44 timestamp(0.0), | |
| 45 is_user_initiated(false), | |
| 46 has_committed(false), | |
| 47 is_server_redirect(false), | |
| 48 is_finished(false), | |
| 49 server_redirect_url() {} | |
| 50 | |
| 51 SafeBrowsingNavigationObserver::NavigationEvent::NavigationEvent( | |
| 52 NavigationEvent&& nav_event) | |
| 53 : source_url(std::move(nav_event.source_url)), | |
| 54 source_tab_id(std::move(nav_event.source_tab_id)), | |
| 55 target_url(std::move(nav_event.target_url)), | |
| 56 target_tab_id(std::move(nav_event.target_tab_id)), | |
| 57 frame_id(nav_event.frame_id), | |
| 58 main_frame_url(std::move(nav_event.main_frame_url)), | |
| 59 timestamp(nav_event.timestamp), | |
| 60 is_user_initiated(nav_event.is_user_initiated), | |
| 61 has_committed(nav_event.has_committed), | |
| 62 is_server_redirect(nav_event.is_server_redirect), | |
| 63 is_finished(nav_event.is_finished), | |
| 64 server_redirect_url(std::move(nav_event.server_redirect_url)) {} | |
| 65 | |
| 66 SafeBrowsingNavigationObserver::NavigationEvent& | |
| 67 SafeBrowsingNavigationObserver::NavigationEvent::operator=( | |
| 68 NavigationEvent&& nav_event) { | |
| 69 source_url = std::move(nav_event.source_url); | |
| 70 source_tab_id = std::move(nav_event.source_tab_id); | |
| 71 target_url = std::move(nav_event.target_url); | |
| 72 target_tab_id = std::move(nav_event.target_tab_id); | |
| 73 frame_id = nav_event.frame_id; | |
| 74 main_frame_url = std::move(nav_event.main_frame_url); | |
| 75 timestamp = nav_event.timestamp; | |
| 76 is_user_initiated = nav_event.is_user_initiated; | |
| 77 has_committed = nav_event.has_committed; | |
| 78 is_server_redirect = nav_event.is_server_redirect; | |
| 79 is_finished = nav_event.is_finished; | |
| 80 server_redirect_url = std::move(nav_event.server_redirect_url); | |
| 81 return *this; | |
| 82 } | |
| 83 | |
| 84 SafeBrowsingNavigationObserver::NavigationEvent::~NavigationEvent() {} | |
| 85 | |
| 86 SafeBrowsingNavigationObserver::SafeBrowsingNavigationObserver() { | |
| 87 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_PENDING, | |
|
Charlie Reis
2016/09/16 23:35:05
Why do you need this one? The fewer notifications
Jialiu Lin
2016/09/22 21:30:28
Removed, only keep NOTIFICATION_RETARGETING
| |
| 88 content::NotificationService::AllSources()); | |
| 89 registrar_.Add(this, chrome::NOTIFICATION_TAB_ADDED, | |
| 90 content::NotificationService::AllSources()); | |
| 91 registrar_.Add(this, chrome::NOTIFICATION_TAB_CLOSING, | |
| 92 content::NotificationService::AllSources()); | |
| 93 registrar_.Add(this, chrome::NOTIFICATION_RETARGETING, | |
| 94 content::NotificationService::AllSources()); | |
| 95 } | |
| 96 | |
| 97 SafeBrowsingNavigationObserver::~SafeBrowsingNavigationObserver() { | |
| 98 ClearNavigationMap(); | |
| 99 } | |
| 100 | |
| 101 void SafeBrowsingNavigationObserver::ClearNavigationMap() { | |
| 102 navigation_map_.clear(); | |
| 103 } | |
| 104 | |
| 105 void SafeBrowsingNavigationObserver::RecordNavigationPendingEntry( | |
| 106 const content::NotificationSource& source, | |
| 107 const content::NotificationDetails& details) { | |
| 108 content::NavigationController* source_controller = | |
| 109 content::Source<content::NavigationController>(source).ptr(); | |
| 110 DCHECK(source_controller); | |
| 111 content::WebContents* source_contents = source_controller->GetWebContents(); | |
| 112 DCHECK(source_contents); | |
| 113 WebContentsObserver::Observe(source_contents); | |
| 114 } | |
| 115 | |
| 116 void SafeBrowsingNavigationObserver::RecordRetargeting( | |
| 117 const content::NotificationDetails& details) { | |
| 118 const RetargetingDetails* retargeting_detail = | |
| 119 content::Details<const RetargetingDetails>(details).ptr(); | |
| 120 DCHECK(retargeting_detail); | |
| 121 content::WebContents* source_contents = | |
| 122 retargeting_detail->source_web_contents; | |
| 123 content::WebContents* target_contents = | |
| 124 retargeting_detail->target_web_contents; | |
| 125 DCHECK(source_contents); | |
| 126 DCHECK(target_contents); | |
| 127 | |
| 128 content::RenderFrameHost* rfh = content::RenderFrameHost::FromID( | |
| 129 source_contents->GetRenderProcessHost()->GetID(), | |
| 130 retargeting_detail->source_render_frame_id); | |
| 131 const GURL& target_url = retargeting_detail->target_url; | |
| 132 | |
| 133 navigation_map_.insert( | |
| 134 std::make_pair(target_url, std::vector<NavigationEvent>())); | |
| 135 | |
| 136 NavigationEvent nav_event; | |
| 137 nav_event.source_url = rfh ? rfh->GetLastCommittedURL() : GURL(); | |
| 138 nav_event.source_tab_id = SessionTabHelper::IdForTab(source_contents); | |
| 139 nav_event.target_url = target_url; | |
| 140 nav_event.target_tab_id = SessionTabHelper::IdForTab(target_contents); | |
| 141 nav_event.frame_id = rfh ? rfh->GetFrameTreeNodeId() : -1; | |
| 142 nav_event.main_frame_url = source_contents->GetVisibleURL(); | |
| 143 nav_event.timestamp = SecondsFromTime(base::Time::Now()); | |
| 144 nav_event.is_user_initiated = true; | |
| 145 | |
| 146 navigation_map_[target_url].push_back(std::move(nav_event)); | |
| 147 } | |
| 148 | |
| 149 void SafeBrowsingNavigationObserver::Observe( | |
| 150 int type, | |
| 151 const content::NotificationSource& source, | |
| 152 const content::NotificationDetails& details) { | |
| 153 switch (type) { | |
| 154 case content::NOTIFICATION_NAV_ENTRY_PENDING: { | |
| 155 RecordNavigationPendingEntry(source, details); | |
| 156 break; | |
| 157 } | |
| 158 case chrome::NOTIFICATION_RETARGETING: { | |
| 159 RecordRetargeting(details); | |
| 160 break; | |
| 161 } | |
| 162 case chrome::NOTIFICATION_TAB_ADDED: { | |
| 163 content::WebContents* dest_content = | |
| 164 content::Details<content::WebContents>(details).ptr(); | |
| 165 DCHECK(dest_content); | |
| 166 WebContentsObserver::Observe(dest_content); | |
|
Charlie Reis
2016/09/16 23:35:05
This will stop observing the previous WebContents,
Jialiu Lin
2016/09/22 21:30:28
Done.
| |
| 167 break; | |
| 168 } | |
| 169 case chrome::NOTIFICATION_TAB_CLOSING: { | |
| 170 // TODO(jialiul): For now, we don't want to delete NavigationEvent if | |
| 171 // its corresponding tab is closed, since it could be used to hide landing | |
| 172 // page. But we may consider using this event as a trigger to clean up | |
| 173 // some outdated entries. | |
| 174 break; | |
| 175 } | |
| 176 default: | |
| 177 NOTREACHED(); | |
| 178 break; | |
| 179 } | |
| 180 } | |
| 181 | |
| 182 // -----------------content::WebContentsObserver overrides-------------- | |
| 183 // Called when a navigation started in the WebContents. |navigation_handle| in | |
| 184 // paramter is unique to this navigation, which will appear in the following | |
| 185 // DidRedirectNavigation, and DidFinishNavigation call too. | |
| 186 void SafeBrowsingNavigationObserver::DidStartNavigation( | |
| 187 content::NavigationHandle* navigation_handle) { | |
| 188 // If we already seen this navigation_handle before, no need to do anything. | |
| 189 if (navigation_handle_map_.find(navigation_handle) != | |
| 190 navigation_handle_map_.end()) | |
| 191 return; | |
| 192 | |
| 193 // Construct a NavigationEvent based on available information in | |
| 194 // navigation_handle. | |
| 195 NavigationEvent nav_event; | |
| 196 content::RenderFrameHost* host = | |
| 197 navigation_handle->GetWebContents()->FindFrameByFrameTreeNodeId( | |
| 198 navigation_handle->GetFrameTreeNodeId()); | |
| 199 | |
| 200 // If there was URL previously committed in this render frame host, | |
| 201 // set it as the source url of this navigation. Otherwise, this is the | |
| 202 // first url going to commit in this frame. We set navigation_handle's URL as | |
| 203 // the source url. | |
|
Charlie Reis
2016/09/16 23:35:05
This doesn't sound right to me. If a navigation i
Jialiu Lin
2016/09/22 21:30:28
retargeting is handled by listening to chrome::NOT
| |
| 204 if (host && host->GetLastCommittedURL().is_valid()) | |
| 205 nav_event.source_url = host->GetLastCommittedURL(); | |
| 206 else | |
| 207 nav_event.source_url = navigation_handle->GetURL(); | |
| 208 | |
| 209 nav_event.target_url = navigation_handle->GetURL(); | |
| 210 | |
| 211 nav_event.source_tab_id = | |
| 212 SessionTabHelper::IdForTab(navigation_handle->GetWebContents()); | |
| 213 nav_event.timestamp = SecondsFromTime(base::Time::Now()); | |
| 214 nav_event.frame_id = navigation_handle->GetFrameTreeNodeId(); | |
| 215 | |
| 216 if (navigation_handle->IsInMainFrame()) | |
| 217 nav_event.main_frame_url = nav_event.source_url; | |
| 218 else | |
| 219 nav_event.main_frame_url = navigation_handle->GetWebContents()->GetURL(); | |
|
Charlie Reis
2016/09/16 23:35:04
GetURL is deprecated because it's ambiguous whethe
Jialiu Lin
2016/09/22 21:30:28
Done.
| |
| 220 | |
| 221 navigation_handle_map_.insert( | |
| 222 std::make_pair(navigation_handle, std::move(nav_event))); | |
| 223 } | |
| 224 | |
| 225 void SafeBrowsingNavigationObserver::DidRedirectNavigation( | |
| 226 content::NavigationHandle* navigation_handle) { | |
| 227 // We should have already seen this navigation_handle in DidStartNavigation. | |
| 228 if (navigation_handle_map_.find(navigation_handle) == | |
| 229 navigation_handle_map_.end()) | |
| 230 return; | |
| 231 NavigationEvent* nav_event = &navigation_handle_map_[navigation_handle]; | |
| 232 nav_event->is_server_redirect = true; | |
| 233 nav_event->server_redirect_url = navigation_handle->GetURL(); | |
|
Charlie Reis
2016/09/16 23:35:04
If you only want to track the last redirect and no
Jialiu Lin
2016/09/22 21:30:28
Done.
| |
| 234 nav_event->timestamp = SecondsFromTime(base::Time::Now()); | |
| 235 } | |
| 236 | |
| 237 void SafeBrowsingNavigationObserver::DidFinishNavigation( | |
| 238 content::NavigationHandle* navigation_handle) { | |
| 239 if (navigation_handle_map_.find(navigation_handle) == | |
| 240 navigation_handle_map_.end()) | |
| 241 return; | |
| 242 NavigationEvent* nav_event = &navigation_handle_map_[navigation_handle]; | |
| 243 | |
| 244 // In NavigationHandle's definition, render initiated navigation includes: | |
|
Charlie Reis
2016/09/16 23:35:05
nit: s/render/renderer/
Jialiu Lin
2016/09/22 21:30:28
Done.
| |
| 245 // clicking <a> link, changing window.location.href, client redirect via | |
| 246 // meta refresh tag and using window.history.pushState. | |
|
Charlie Reis
2016/09/16 23:35:04
Be careful not to imply that this list exhaustive.
Jialiu Lin
2016/09/22 21:30:28
Fixed comments
| |
| 247 // Though in our case, we consider user explicitly clicking on a link as user | |
| 248 // initiated. We first use the negate of IsRenderInitiated() to include | |
| 249 // user initiated navigations such as typing in the omni box or clicking on | |
| 250 // bookmark then we refine this estimate by using HasUserGesture() | |
| 251 // if this info is available. | |
|
Charlie Reis
2016/09/16 23:35:04
Yes, we generally consider "user initiated" to mea
Jialiu Lin
2016/09/22 21:30:28
Done.
| |
| 252 // For non-committed navigations (e.g. downloads), | |
| 253 // NavigationHandle::HasUserGesture() is not availble. But we can later get it | |
| 254 // from DownloadItem::HasUserGesture(). | |
| 255 nav_event->is_user_initiated = !navigation_handle->IsRendererInitiated(); | |
| 256 if (navigation_handle->HasCommitted()) { | |
| 257 nav_event->has_committed = true; | |
| 258 nav_event->is_user_initiated = | |
| 259 nav_event->is_user_initiated || navigation_handle->HasUserGesture(); | |
| 260 } | |
| 261 nav_event->target_tab_id = | |
| 262 SessionTabHelper::IdForTab(navigation_handle->GetWebContents()); | |
| 263 nav_event->timestamp = SecondsFromTime(base::Time::Now()); | |
| 264 nav_event->is_finished = true; | |
| 265 | |
| 266 GURL key_url = nav_event->is_server_redirect ? nav_event->server_redirect_url | |
| 267 : nav_event->target_url; | |
|
Charlie Reis
2016/09/16 23:35:04
I think we might need to be more careful about the
Jialiu Lin
2016/09/22 21:30:28
You're right. I should use the committed URL inste
| |
| 268 navigation_map_.insert( | |
| 269 std::make_pair(key_url, std::vector<NavigationEvent>())); | |
| 270 | |
| 271 navigation_map_.at(key_url).push_back(std::move(*nav_event)); | |
|
Charlie Reis
2016/09/16 23:35:05
Why do this separately (requiring a second lookup)
Jialiu Lin
2016/09/22 21:30:28
Done.
| |
| 272 | |
| 273 navigation_handle_map_.erase(navigation_handle); | |
| 274 } | |
| 275 | |
| 276 } // namespace safe_browsing | |
| OLD | NEW |