 Chromium Code Reviews
 Chromium Code Reviews Issue 2302913003:
  Add SafeBrowsingNavigationObserver to listen to navigation events  (Closed)
    
  
    Issue 2302913003:
  Add SafeBrowsingNavigationObserver to listen to navigation events  (Closed) 
  | Index: chrome/browser/safe_browsing/safe_browsing_navigation_observer.h | 
| diff --git a/chrome/browser/safe_browsing/safe_browsing_navigation_observer.h b/chrome/browser/safe_browsing/safe_browsing_navigation_observer.h | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..fb064e027c4b927bdac33d6f9b47b5a6a72db16c | 
| --- /dev/null | 
| +++ b/chrome/browser/safe_browsing/safe_browsing_navigation_observer.h | 
| @@ -0,0 +1,109 @@ | 
| +// Copyright 2016 The Chromium Authors. All rights reserved. | 
| +// Use of this source code is governed by a BSD-style license that can be | 
| +// found in the LICENSE file. | 
| + | 
| +#ifndef CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_NAVIGATION_OBSERVER_H_ | 
| +#define CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_NAVIGATION_OBSERVER_H_ | 
| + | 
| +#include "base/supports_user_data.h" | 
| +#include "content/public/browser/web_contents_observer.h" | 
| +#include "url/gurl.h" | 
| + | 
| +namespace content { | 
| +class NavigationHandle; | 
| +struct ResourceRedirectDetails; | 
| +} | 
| + | 
| +namespace safe_browsing { | 
| +class SafeBrowsingNavigationObserverManager; | 
| + | 
| +// Struct to record the details of a navigation event for any frames. | 
| 
Charlie Reis
2016/10/17 23:54:03
nit: s/frames/frame/
 
Jialiu Lin
2016/10/18 19:06:05
Done.
 | 
| +// This information will be used to fill |url_chain| field in safe browsing | 
| +// download pings. | 
| +struct NavigationEvent { | 
| + NavigationEvent(); | 
| + NavigationEvent(NavigationEvent&& nav_event); | 
| + NavigationEvent& operator=(NavigationEvent&& nav_event); | 
| + ~NavigationEvent(); | 
| + | 
| + GURL source_url; // URL that caused this navigation to occur. | 
| + int source_tab_id; // Which tab the above source_url is in. Tab ID is | 
| 
Charlie Reis
2016/10/17 23:54:04
Assuming that source_url might be a subframe, I th
 
Jialiu Lin
2016/10/18 19:06:05
Ah, that's much clearer. Thanks!
 | 
| + // returned by SessionTabHelper::IdForTab. This ID is | 
| + // immutable for a given tab and unique across Chrome | 
| + // within the current session. | 
| + | 
| + GURL target_url; // The original request URL of this navigation. | 
| + int target_tab_id; // Which tab this target url is in. | 
| + | 
| + int frame_id; // Frame tree node ID of the frame where this navigation takes | 
| + // place. | 
| + GURL main_frame_url; // Main frame url of the source_url. Can be the same | 
| 
Charlie Reis
2016/10/17 23:54:04
This should probably be called source_main_frame_u
 
Jialiu Lin
2016/10/18 19:06:05
make sense.
 | 
| + // as source url, if source_url was loaded in main | 
| + // frame. | 
| + | 
| + base::Time timestamp; // Timestamp of last time this object is updated. | 
| 
Charlie Reis
2016/10/17 23:54:04
last_updated, perhaps?
 
Jialiu Lin
2016/10/18 19:06:05
Done.
 | 
| + | 
| + bool is_user_initiated; // browser_initiated || has_user_gesture. | 
| + bool has_committed; | 
| + bool has_server_redirect; | 
| + GURL server_redirect_url; // Last server redirect url in server redirect | 
| 
Charlie Reis
2016/10/17 23:54:04
This sounds a bit awkward to use.  In most places
 
Jialiu Lin
2016/10/18 19:06:05
I agree. though for some cases (e.g. download urls
 
Charlie Reis
2016/10/19 17:10:45
Good point about downloads not committing.  Maybe
 
Jialiu Lin
2016/10/21 02:38:38
SG. renamed to destination_url.
 | 
| + // chain. In the absence of server redirect, this | 
| + // field will be empty. | 
| +}; | 
| + | 
| +// Structure to keep tracks of resolved IP address of a host. | 
| 
Charlie Reis
2016/10/17 23:54:04
nit: track
 
Jialiu Lin
2016/10/18 19:06:05
Done.
 | 
| +struct ResolvedIPAddress { | 
| + ResolvedIPAddress() : timestamp(base::Time::Now()), ip() {} | 
| + ResolvedIPAddress(base::Time timestamp, const std::string& ip) | 
| + : timestamp(timestamp), ip(ip) {} | 
| + base::Time timestamp; // Timestamp of when we get the resolved IP. | 
| + std::string ip; // Resolved IP address | 
| +}; | 
| + | 
| +// Observes the navigation events for a single WebContents (both main-frame | 
| +// and sub-frame navigations) | 
| +class SafeBrowsingNavigationObserver : public base::SupportsUserData::Data, | 
| + public content::WebContentsObserver { | 
| + public: | 
| + static void MaybeCreateForWebContents(content::WebContents* web_contents); | 
| + static SafeBrowsingNavigationObserver* FromWebContents( | 
| + content::WebContents* web_contents); | 
| + | 
| + SafeBrowsingNavigationObserver( | 
| + content::WebContents* contents, | 
| + const scoped_refptr<SafeBrowsingNavigationObserverManager>& manager); | 
| + | 
| + ~SafeBrowsingNavigationObserver() override; | 
| + | 
| + private: | 
| + typedef std::unordered_map<content::NavigationHandle*, NavigationEvent> | 
| + NavigationHandleMap; | 
| + | 
| + // content::WebContentsObserver: | 
| + void DidStartNavigation( | 
| + content::NavigationHandle* navigation_handle) override; | 
| + void DidRedirectNavigation( | 
| + content::NavigationHandle* navigation_handle) override; | 
| + void DidFinishNavigation( | 
| + content::NavigationHandle* navigation_handle) override; | 
| + void DidGetResourceResponseStart( | 
| + const content::ResourceRequestDetails& details) override; | 
| + void DidGetUserInteraction(const blink::WebInputEvent::Type type) override; | 
| + void WebContentsDestroyed() override; | 
| + | 
| + // Map keyed on NavigationHandle* to keep track of all the ongoing navigation | 
| + // events. NavigationHandle pointers are owned by RenderFrameHost. Since a | 
| + // NavigationHandle object will be destructed after navigation is done, | 
| + // corresponding entry in this map will be removed from navigation_handle_map_ | 
| + // and added to navigation_map_ at the end of DidFinishNavigation(...). | 
| 
Charlie Reis
2016/10/17 23:54:04
Please clarify where navigation_map_ lives.
 
Jialiu Lin
2016/10/18 19:06:05
Done.
 | 
| + NavigationHandleMap navigation_handle_map_; | 
| + scoped_refptr<SafeBrowsingNavigationObserverManager> manager_; | 
| 
Charlie Reis
2016/10/17 23:54:04
nit: I'd find this section more readable with blan
 
Jialiu Lin
2016/10/18 19:06:05
Blank lines added.
 | 
| + // If the observed WebContents just got an user gesture. | 
| + bool has_user_gesture_; | 
| + base::Time last_user_gesture_timestamp_; | 
| + DISALLOW_COPY_AND_ASSIGN(SafeBrowsingNavigationObserver); | 
| +}; | 
| + | 
| +} // namespace safe_browsing | 
| + | 
| +#endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_NAVIGATION_OBSERVER_H_ |