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 | |
| 5 #include "chrome/browser/safe_browsing/safe_browsing_navigation_observer.h" | |
| 6 | |
| 7 #include "base/memory/ptr_util.h" | |
| 8 #include "base/time/time.h" | |
| 9 #include "chrome/browser/safe_browsing/safe_browsing_navigation_observer_manager .h" | |
| 10 #include "chrome/browser/sessions/session_tab_helper.h" | |
| 11 #include "content/public/browser/navigation_handle.h" | |
| 12 #include "content/public/browser/render_frame_host.h" | |
| 13 #include "content/public/browser/resource_request_details.h" | |
| 14 #include "content/public/browser/web_contents.h" | |
| 15 #include "content/public/common/resource_type.h" | |
| 16 | |
| 17 using content::WebContents; | |
| 18 | |
| 19 namespace { | |
| 20 const char kWebContentsUserDataKey[] = | |
| 21 "web_contents_safe_browsing_navigation_observer"; | |
| 22 } // namespace | |
| 23 | |
| 24 namespace safe_browsing { | |
| 25 | |
| 26 // SafeBrowsingNavigationObserver::NavigationEvent----------------------------- | |
| 27 NavigationEvent::NavigationEvent() | |
| 28 : source_url(), | |
|
Charlie Reis
2016/10/17 23:54:03
nit: These empty initializations aren't needed, ri
Jialiu Lin
2016/10/18 19:06:04
Acknowledged. Compiler complains if I omit this de
Charlie Reis
2016/10/19 17:10:45
Acknowledged.
| |
| 29 source_tab_id(-1), | |
| 30 target_url(), | |
| 31 target_tab_id(-1), | |
| 32 frame_id(-1), | |
| 33 main_frame_url(), | |
| 34 timestamp(base::Time::Now()), | |
| 35 is_user_initiated(false), | |
| 36 has_committed(false), | |
| 37 has_server_redirect(false), | |
| 38 server_redirect_url() {} | |
| 39 | |
| 40 NavigationEvent::NavigationEvent(NavigationEvent&& nav_event) | |
| 41 : source_url(std::move(nav_event.source_url)), | |
| 42 source_tab_id(std::move(nav_event.source_tab_id)), | |
| 43 target_url(std::move(nav_event.target_url)), | |
| 44 target_tab_id(std::move(nav_event.target_tab_id)), | |
| 45 frame_id(nav_event.frame_id), | |
| 46 main_frame_url(std::move(nav_event.main_frame_url)), | |
| 47 timestamp(nav_event.timestamp), | |
| 48 is_user_initiated(nav_event.is_user_initiated), | |
| 49 has_committed(nav_event.has_committed), | |
| 50 has_server_redirect(nav_event.has_server_redirect), | |
| 51 server_redirect_url(std::move(nav_event.server_redirect_url)) {} | |
| 52 | |
| 53 NavigationEvent& NavigationEvent::operator=(NavigationEvent&& nav_event) { | |
| 54 source_url = std::move(nav_event.source_url); | |
| 55 source_tab_id = std::move(nav_event.source_tab_id); | |
| 56 target_url = std::move(nav_event.target_url); | |
| 57 target_tab_id = std::move(nav_event.target_tab_id); | |
| 58 frame_id = nav_event.frame_id; | |
| 59 main_frame_url = std::move(nav_event.main_frame_url); | |
| 60 timestamp = nav_event.timestamp; | |
| 61 is_user_initiated = nav_event.is_user_initiated; | |
| 62 has_committed = nav_event.has_committed; | |
| 63 has_server_redirect = nav_event.has_server_redirect; | |
| 64 server_redirect_url = std::move(nav_event.server_redirect_url); | |
| 65 return *this; | |
| 66 } | |
| 67 | |
| 68 NavigationEvent::~NavigationEvent() {} | |
| 69 | |
| 70 // SafeBrowsingNavigationObserver -------------------------------------------- | |
| 71 | |
| 72 // static | |
| 73 void SafeBrowsingNavigationObserver::MaybeCreateForWebContents( | |
| 74 content::WebContents* web_contents) { | |
| 75 if (FromWebContents(web_contents)) | |
| 76 return; | |
| 77 NOTIMPLEMENTED(); | |
| 78 // TODO(jialiul): This method will be called by TabHelpers::AttachTabHelpers. | |
| 79 // Complete this method when the entire class is ready. | |
| 80 } | |
| 81 | |
| 82 // static | |
| 83 SafeBrowsingNavigationObserver* SafeBrowsingNavigationObserver::FromWebContents( | |
| 84 content::WebContents* web_contents) { | |
| 85 return static_cast<SafeBrowsingNavigationObserver*>( | |
| 86 web_contents->GetUserData(kWebContentsUserDataKey)); | |
| 87 } | |
| 88 | |
| 89 SafeBrowsingNavigationObserver::SafeBrowsingNavigationObserver( | |
| 90 content::WebContents* contents, | |
| 91 const scoped_refptr<SafeBrowsingNavigationObserverManager>& manager) | |
| 92 : content::WebContentsObserver(contents), | |
| 93 manager_(manager), | |
| 94 has_user_gesture_(false), | |
| 95 last_user_gesture_timestamp_(base::Time()) {} | |
| 96 | |
| 97 SafeBrowsingNavigationObserver::~SafeBrowsingNavigationObserver() {} | |
| 98 | |
| 99 // Called when a navigation started in the WebContents. |navigation_handle| in | |
| 100 // parameter is unique to this navigation, which will appear in the following | |
| 101 // DidRedirectNavigation, and DidFinishNavigation too. | |
| 102 void SafeBrowsingNavigationObserver::DidStartNavigation( | |
| 103 content::NavigationHandle* navigation_handle) { | |
| 104 // If we already seen this navigation_handle before, no need to do anything. | |
|
Charlie Reis
2016/10/17 23:54:03
I'm not sure whether it's a good idea to ignore it
Jialiu Lin
2016/10/18 19:06:04
Acknowledged. I added a TODO here, since this 2 Di
Charlie Reis
2016/10/19 17:10:45
This happens on every transfer, not just occasiona
Jialiu Lin
2016/10/21 02:38:38
Agree. refactored this function to address this c
| |
| 105 if (navigation_handle_map_.find(navigation_handle) != | |
| 106 navigation_handle_map_.end()) | |
| 107 return; | |
| 108 | |
| 109 // Construct a NavigationEvent based on available information in | |
| 110 // navigation_handle. | |
| 111 NavigationEvent nav_event; | |
| 112 content::RenderFrameHost* host = | |
| 113 navigation_handle->GetWebContents()->FindFrameByFrameTreeNodeId( | |
| 114 navigation_handle->GetFrameTreeNodeId()); | |
|
Charlie Reis
2016/10/17 23:54:03
This doesn't look safe, at least for general uses
Jialiu Lin
2016/10/18 19:06:04
Let's keep it for now. Since |host| here is only u
Charlie Reis
2016/10/19 17:10:45
I see. We should name it current_frame_host to be
Jialiu Lin
2016/10/21 02:38:38
Done.
| |
| 115 | |
| 116 // If there was URL previously committed in this render frame host, | |
| 117 // set it as the source url of this navigation. Otherwise, this is the | |
| 118 // first url going to commit in this frame. We set navigation_handle's URL as | |
| 119 // the source url. | |
| 120 if (host && host->GetLastCommittedURL().is_valid()) { | |
|
Charlie Reis
2016/10/17 23:54:03
The last committed URL in the frame is not the sou
Jialiu Lin
2016/10/18 19:06:04
Oh, yes, forgot to add this TODO.
| |
| 121 nav_event.source_url = | |
| 122 SafeBrowsingNavigationObserverManager::ClearEmptyURLRef( | |
| 123 host->GetLastCommittedURL()); | |
| 124 } else { | |
| 125 nav_event.source_url = | |
| 126 SafeBrowsingNavigationObserverManager::ClearEmptyURLRef( | |
| 127 navigation_handle->GetURL()); | |
| 128 } | |
| 129 nav_event.target_url = | |
| 130 SafeBrowsingNavigationObserverManager::ClearEmptyURLRef( | |
| 131 navigation_handle->GetURL()); | |
| 132 | |
| 133 nav_event.source_tab_id = | |
| 134 SessionTabHelper::IdForTab(navigation_handle->GetWebContents()); | |
| 135 nav_event.timestamp = base::Time::Now(); | |
| 136 nav_event.frame_id = navigation_handle->GetFrameTreeNodeId(); | |
| 137 | |
| 138 if (navigation_handle->IsInMainFrame()) { | |
| 139 nav_event.main_frame_url = nav_event.source_url; | |
| 140 } else { | |
| 141 nav_event.main_frame_url = | |
| 142 SafeBrowsingNavigationObserverManager::ClearEmptyURLRef( | |
| 143 navigation_handle->GetWebContents()->GetLastCommittedURL()); | |
| 144 } | |
| 145 if ((has_user_gesture_ && | |
| 146 !SafeBrowsingNavigationObserverManager::IsUserGestureExpired( | |
| 147 last_user_gesture_timestamp_)) || | |
| 148 !navigation_handle->IsRendererInitiated()) { | |
| 149 nav_event.is_user_initiated = has_user_gesture_; | |
| 150 manager_->OnUserGestureConsumed(web_contents(), | |
| 151 last_user_gesture_timestamp_); | |
| 152 } | |
| 153 has_user_gesture_ = false; | |
| 154 navigation_handle_map_.insert( | |
| 155 std::make_pair(navigation_handle, std::move(nav_event))); | |
| 156 } | |
| 157 | |
| 158 void SafeBrowsingNavigationObserver::DidRedirectNavigation( | |
| 159 content::NavigationHandle* navigation_handle) { | |
| 160 // We should have already seen this navigation_handle in DidStartNavigation. | |
| 161 if (navigation_handle_map_.find(navigation_handle) == | |
| 162 navigation_handle_map_.end()) | |
| 163 return; | |
|
Charlie Reis
2016/10/17 23:54:03
Maybe add a NOTREACHED to aid with debugging and t
Jialiu Lin
2016/10/18 19:06:04
Good idea. Done.
| |
| 164 | |
| 165 NavigationEvent* nav_event = &navigation_handle_map_[navigation_handle]; | |
| 166 nav_event->has_server_redirect = true; | |
| 167 nav_event->server_redirect_url = | |
| 168 SafeBrowsingNavigationObserverManager::ClearEmptyURLRef( | |
| 169 navigation_handle->GetURL()); | |
| 170 nav_event->timestamp = base::Time::Now(); | |
| 171 } | |
| 172 | |
| 173 void SafeBrowsingNavigationObserver::DidFinishNavigation( | |
| 174 content::NavigationHandle* navigation_handle) { | |
| 175 if (navigation_handle_map_.find(navigation_handle) == | |
| 176 navigation_handle_map_.end()) | |
| 177 return; | |
|
Charlie Reis
2016/10/17 23:54:03
NOTREACHED?
Jialiu Lin
2016/10/18 19:06:04
Done.
| |
| 178 | |
| 179 // If it is an error page, we ignore this navigation. | |
| 180 if (navigation_handle->IsErrorPage()) { | |
| 181 navigation_handle_map_.erase(navigation_handle); | |
| 182 return; | |
| 183 } | |
| 184 NavigationEvent* nav_event = &navigation_handle_map_[navigation_handle]; | |
| 185 | |
| 186 nav_event->has_committed = navigation_handle->HasCommitted(); | |
| 187 nav_event->target_tab_id = | |
| 188 SessionTabHelper::IdForTab(navigation_handle->GetWebContents()); | |
| 189 nav_event->timestamp = base::Time::Now(); | |
| 190 | |
| 191 GURL nav_event_key = | |
|
Charlie Reis
2016/10/17 23:54:03
I'm not sure I understand this complex clause. Wo
Jialiu Lin
2016/10/18 19:06:04
Ah, don't need this complex thing anymore.
| |
| 192 navigation_handle->HasCommitted() | |
| 193 ? SafeBrowsingNavigationObserverManager::ClearEmptyURLRef( | |
| 194 navigation_handle->GetRenderFrameHost()->GetLastCommittedURL()) | |
| 195 : nav_event->has_server_redirect ? nav_event->server_redirect_url | |
| 196 : nav_event->target_url; | |
| 197 manager_->RecordNavigationEvent(nav_event_key, nav_event); | |
| 198 navigation_handle_map_.erase(navigation_handle); | |
| 199 } | |
| 200 | |
| 201 void SafeBrowsingNavigationObserver::DidGetResourceResponseStart( | |
| 202 const content::ResourceRequestDetails& details) { | |
| 203 // We only care about main frame and sub frame. | |
| 204 if (details.resource_type != content::RESOURCE_TYPE_MAIN_FRAME && | |
| 205 details.resource_type != content::RESOURCE_TYPE_SUB_FRAME) { | |
| 206 return; | |
| 207 } | |
| 208 if (!details.url.is_valid() || details.socket_address.IsEmpty()) | |
| 209 return; | |
| 210 | |
| 211 manager_->RecordHostToIpMapping(details.url.host(), | |
| 212 details.socket_address.host()); | |
| 213 } | |
| 214 | |
| 215 void SafeBrowsingNavigationObserver::DidGetUserInteraction( | |
| 216 const blink::WebInputEvent::Type type) { | |
| 217 last_user_gesture_timestamp_ = base::Time::Now(); | |
| 218 has_user_gesture_ = true; | |
| 219 // TODO (jialiul): Refine user gesture logic when DidOpenRequestedURL | |
| 220 // covers all retargetting cases. | |
| 221 manager_->RecordUserGestureForWebContents(web_contents(), | |
| 222 last_user_gesture_timestamp_); | |
| 223 } | |
| 224 | |
| 225 void SafeBrowsingNavigationObserver::WebContentsDestroyed() { | |
| 226 manager_->OnWebContentDestroyed(web_contents()); | |
| 227 web_contents()->RemoveUserData(kWebContentsUserDataKey); | |
| 228 // web_contents is null after this function. | |
| 229 } | |
| 230 | |
| 231 } // namespace safe_browsing | |
| OLD | NEW |