| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/safe_browsing/safe_browsing_navigation_observer.h" | 5 #include "chrome/browser/safe_browsing/safe_browsing_navigation_observer.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/time/time.h" | 8 #include "base/time/time.h" |
| 9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 "web_contents_safe_browsing_navigation_observer"; | 24 "web_contents_safe_browsing_navigation_observer"; |
| 25 } // namespace | 25 } // namespace |
| 26 | 26 |
| 27 namespace safe_browsing { | 27 namespace safe_browsing { |
| 28 | 28 |
| 29 // SafeBrowsingNavigationObserver::NavigationEvent----------------------------- | 29 // SafeBrowsingNavigationObserver::NavigationEvent----------------------------- |
| 30 NavigationEvent::NavigationEvent() | 30 NavigationEvent::NavigationEvent() |
| 31 : source_url(), | 31 : source_url(), |
| 32 source_main_frame_url(), | 32 source_main_frame_url(), |
| 33 original_request_url(), | 33 original_request_url(), |
| 34 destination_url(), | |
| 35 source_tab_id(-1), | 34 source_tab_id(-1), |
| 36 target_tab_id(-1), | 35 target_tab_id(-1), |
| 37 frame_id(-1), | 36 frame_id(-1), |
| 38 last_updated(base::Time::Now()), | 37 last_updated(base::Time::Now()), |
| 39 is_user_initiated(false), | 38 is_user_initiated(false), |
| 40 has_committed(false), | 39 has_committed(false) {} |
| 41 has_server_redirect(false) {} | |
| 42 | 40 |
| 43 NavigationEvent::NavigationEvent(NavigationEvent&& nav_event) | 41 NavigationEvent::NavigationEvent(NavigationEvent&& nav_event) |
| 44 : source_url(std::move(nav_event.source_url)), | 42 : source_url(std::move(nav_event.source_url)), |
| 45 source_main_frame_url(std::move(nav_event.source_main_frame_url)), | 43 source_main_frame_url(std::move(nav_event.source_main_frame_url)), |
| 46 original_request_url(std::move(nav_event.original_request_url)), | 44 original_request_url(std::move(nav_event.original_request_url)), |
| 47 destination_url(std::move(nav_event.destination_url)), | 45 server_redirect_urls(std::move(nav_event.server_redirect_urls)), |
| 48 source_tab_id(std::move(nav_event.source_tab_id)), | 46 source_tab_id(std::move(nav_event.source_tab_id)), |
| 49 target_tab_id(std::move(nav_event.target_tab_id)), | 47 target_tab_id(std::move(nav_event.target_tab_id)), |
| 50 frame_id(nav_event.frame_id), | 48 frame_id(nav_event.frame_id), |
| 51 last_updated(nav_event.last_updated), | 49 last_updated(nav_event.last_updated), |
| 52 is_user_initiated(nav_event.is_user_initiated), | 50 is_user_initiated(nav_event.is_user_initiated), |
| 53 has_committed(nav_event.has_committed), | 51 has_committed(nav_event.has_committed) {} |
| 54 has_server_redirect(nav_event.has_server_redirect) {} | |
| 55 | 52 |
| 56 NavigationEvent& NavigationEvent::operator=(NavigationEvent&& nav_event) { | 53 NavigationEvent& NavigationEvent::operator=(NavigationEvent&& nav_event) { |
| 57 source_url = std::move(nav_event.source_url); | 54 source_url = std::move(nav_event.source_url); |
| 58 source_main_frame_url = std::move(nav_event.source_main_frame_url); | 55 source_main_frame_url = std::move(nav_event.source_main_frame_url); |
| 59 original_request_url = std::move(nav_event.original_request_url); | 56 original_request_url = std::move(nav_event.original_request_url); |
| 60 destination_url = std::move(nav_event.destination_url); | |
| 61 source_tab_id = nav_event.source_tab_id; | 57 source_tab_id = nav_event.source_tab_id; |
| 62 target_tab_id = nav_event.target_tab_id; | 58 target_tab_id = nav_event.target_tab_id; |
| 63 frame_id = nav_event.frame_id; | 59 frame_id = nav_event.frame_id; |
| 64 last_updated = nav_event.last_updated; | 60 last_updated = nav_event.last_updated; |
| 65 is_user_initiated = nav_event.is_user_initiated; | 61 is_user_initiated = nav_event.is_user_initiated; |
| 66 has_committed = nav_event.has_committed; | 62 has_committed = nav_event.has_committed; |
| 67 has_server_redirect = nav_event.has_server_redirect; | 63 server_redirect_urls = std::move(nav_event.server_redirect_urls); |
| 68 return *this; | 64 return *this; |
| 69 } | 65 } |
| 70 | 66 |
| 71 NavigationEvent::~NavigationEvent() {} | 67 NavigationEvent::~NavigationEvent() {} |
| 72 | 68 |
| 73 // SafeBrowsingNavigationObserver -------------------------------------------- | 69 // SafeBrowsingNavigationObserver -------------------------------------------- |
| 74 | 70 |
| 75 // static | 71 // static |
| 76 void SafeBrowsingNavigationObserver::MaybeCreateForWebContents( | 72 void SafeBrowsingNavigationObserver::MaybeCreateForWebContents( |
| 77 content::WebContents* web_contents) { | 73 content::WebContents* web_contents) { |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 // don't fill the source_url to prevent attributing navigation to the last | 149 // don't fill the source_url to prevent attributing navigation to the last |
| 154 // committed navigation. | 150 // committed navigation. |
| 155 if (navigation_handle->IsRendererInitiated() && current_frame_host && | 151 if (navigation_handle->IsRendererInitiated() && current_frame_host && |
| 156 current_frame_host->GetLastCommittedURL().is_valid()) { | 152 current_frame_host->GetLastCommittedURL().is_valid()) { |
| 157 nav_event.source_url = SafeBrowsingNavigationObserverManager::ClearEmptyRef( | 153 nav_event.source_url = SafeBrowsingNavigationObserverManager::ClearEmptyRef( |
| 158 current_frame_host->GetLastCommittedURL()); | 154 current_frame_host->GetLastCommittedURL()); |
| 159 } | 155 } |
| 160 nav_event.original_request_url = | 156 nav_event.original_request_url = |
| 161 SafeBrowsingNavigationObserverManager::ClearEmptyRef( | 157 SafeBrowsingNavigationObserverManager::ClearEmptyRef( |
| 162 navigation_handle->GetURL()); | 158 navigation_handle->GetURL()); |
| 163 nav_event.destination_url = nav_event.original_request_url; | |
| 164 | 159 |
| 165 nav_event.source_tab_id = | 160 nav_event.source_tab_id = |
| 166 SessionTabHelper::IdForTab(navigation_handle->GetWebContents()); | 161 SessionTabHelper::IdForTab(navigation_handle->GetWebContents()); |
| 167 | 162 |
| 168 if (navigation_handle->IsInMainFrame()) { | 163 if (navigation_handle->IsInMainFrame()) { |
| 169 nav_event.source_main_frame_url = nav_event.source_url; | 164 nav_event.source_main_frame_url = nav_event.source_url; |
| 170 } else { | 165 } else { |
| 171 nav_event.source_main_frame_url = | 166 nav_event.source_main_frame_url = |
| 172 SafeBrowsingNavigationObserverManager::ClearEmptyRef( | 167 SafeBrowsingNavigationObserverManager::ClearEmptyRef( |
| 173 navigation_handle->GetWebContents()->GetLastCommittedURL()); | 168 navigation_handle->GetWebContents()->GetLastCommittedURL()); |
| 174 } | 169 } |
| 175 navigation_handle_map_[navigation_handle] = std::move(nav_event); | 170 navigation_handle_map_[navigation_handle] = std::move(nav_event); |
| 176 } | 171 } |
| 177 | 172 |
| 178 void SafeBrowsingNavigationObserver::DidRedirectNavigation( | 173 void SafeBrowsingNavigationObserver::DidRedirectNavigation( |
| 179 content::NavigationHandle* navigation_handle) { | 174 content::NavigationHandle* navigation_handle) { |
| 180 // We should have already seen this navigation_handle in DidStartNavigation. | 175 // We should have already seen this navigation_handle in DidStartNavigation. |
| 181 if (navigation_handle_map_.find(navigation_handle) == | 176 if (navigation_handle_map_.find(navigation_handle) == |
| 182 navigation_handle_map_.end()) { | 177 navigation_handle_map_.end()) { |
| 183 NOTREACHED(); | 178 NOTREACHED(); |
| 184 return; | 179 return; |
| 185 } | 180 } |
| 186 | 181 |
| 187 NavigationEvent* nav_event = &navigation_handle_map_[navigation_handle]; | 182 NavigationEvent* nav_event = &navigation_handle_map_[navigation_handle]; |
| 188 nav_event->has_server_redirect = true; | 183 nav_event->server_redirect_urls.push_back( |
| 189 nav_event->destination_url = | |
| 190 SafeBrowsingNavigationObserverManager::ClearEmptyRef( | 184 SafeBrowsingNavigationObserverManager::ClearEmptyRef( |
| 191 navigation_handle->GetURL()); | 185 navigation_handle->GetURL())); |
| 192 nav_event->last_updated = base::Time::Now(); | 186 nav_event->last_updated = base::Time::Now(); |
| 193 } | 187 } |
| 194 | 188 |
| 195 void SafeBrowsingNavigationObserver::DidFinishNavigation( | 189 void SafeBrowsingNavigationObserver::DidFinishNavigation( |
| 196 content::NavigationHandle* navigation_handle) { | 190 content::NavigationHandle* navigation_handle) { |
| 197 if (navigation_handle_map_.find(navigation_handle) == | 191 if (navigation_handle_map_.find(navigation_handle) == |
| 198 navigation_handle_map_.end()) { | 192 navigation_handle_map_.end()) { |
| 199 NOTREACHED(); | 193 NOTREACHED(); |
| 200 return; | 194 return; |
| 201 } | 195 } |
| 202 | 196 |
| 203 // If it is an error page, we ignore this navigation. | 197 // If it is an error page, we ignore this navigation. |
| 204 if (navigation_handle->IsErrorPage()) { | 198 if (navigation_handle->IsErrorPage()) { |
| 205 navigation_handle_map_.erase(navigation_handle); | 199 navigation_handle_map_.erase(navigation_handle); |
| 206 return; | 200 return; |
| 207 } | 201 } |
| 208 NavigationEvent* nav_event = &navigation_handle_map_[navigation_handle]; | 202 NavigationEvent* nav_event = &navigation_handle_map_[navigation_handle]; |
| 209 | 203 |
| 210 nav_event->has_committed = navigation_handle->HasCommitted(); | 204 nav_event->has_committed = navigation_handle->HasCommitted(); |
| 211 nav_event->target_tab_id = | 205 nav_event->target_tab_id = |
| 212 SessionTabHelper::IdForTab(navigation_handle->GetWebContents()); | 206 SessionTabHelper::IdForTab(navigation_handle->GetWebContents()); |
| 213 nav_event->last_updated = base::Time::Now(); | 207 nav_event->last_updated = base::Time::Now(); |
| 214 | 208 |
| 215 manager_->RecordNavigationEvent(nav_event->destination_url, nav_event); | 209 manager_->RecordNavigationEvent(nav_event->GetDestinationUrl(), nav_event); |
| 216 navigation_handle_map_.erase(navigation_handle); | 210 navigation_handle_map_.erase(navigation_handle); |
| 217 } | 211 } |
| 218 | 212 |
| 219 void SafeBrowsingNavigationObserver::DidGetResourceResponseStart( | 213 void SafeBrowsingNavigationObserver::DidGetResourceResponseStart( |
| 220 const content::ResourceRequestDetails& details) { | 214 const content::ResourceRequestDetails& details) { |
| 221 // We only care about main frame and sub frame. | 215 // We only care about main frame and sub frame. |
| 222 if (details.resource_type != content::RESOURCE_TYPE_MAIN_FRAME && | 216 if (details.resource_type != content::RESOURCE_TYPE_MAIN_FRAME && |
| 223 details.resource_type != content::RESOURCE_TYPE_SUB_FRAME) { | 217 details.resource_type != content::RESOURCE_TYPE_SUB_FRAME) { |
| 224 return; | 218 return; |
| 225 } | 219 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 239 last_user_gesture_timestamp_); | 233 last_user_gesture_timestamp_); |
| 240 } | 234 } |
| 241 | 235 |
| 242 void SafeBrowsingNavigationObserver::WebContentsDestroyed() { | 236 void SafeBrowsingNavigationObserver::WebContentsDestroyed() { |
| 243 manager_->OnWebContentDestroyed(web_contents()); | 237 manager_->OnWebContentDestroyed(web_contents()); |
| 244 web_contents()->RemoveUserData(kWebContentsUserDataKey); | 238 web_contents()->RemoveUserData(kWebContentsUserDataKey); |
| 245 // web_contents is null after this function. | 239 // web_contents is null after this function. |
| 246 } | 240 } |
| 247 | 241 |
| 248 } // namespace safe_browsing | 242 } // namespace safe_browsing |
| OLD | NEW |