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 #ifndef CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_NAVIGATION_OBSERVER_MANAGER_H _ | |
| 6 #define CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_NAVIGATION_OBSERVER_MANAGER_H _ | |
| 7 | |
| 8 #include "content/public/browser/notification_observer.h" | |
| 9 #include "content/public/browser/notification_registrar.h" | |
| 10 #include "content/public/browser/web_contents_observer.h" | |
| 11 #include "url/gurl.h" | |
| 12 | |
| 13 namespace content { | |
| 14 class NavigationHandle; | |
| 15 struct ResourceRedirectDetails; | |
| 16 } | |
| 17 | |
| 18 namespace safe_browsing { | |
| 19 | |
| 20 class SafeBrowsingNavigationObserver; | |
| 21 struct NavigationEvent; | |
| 22 struct ResolvedIPAddress; | |
| 23 | |
| 24 // Manager class for SafeBrowsingNavigationObserver, which is in charge of | |
| 25 // cleaning up stale navigation events, and identifing landing page/landing | |
| 26 // referrer for a specific download. | |
| 27 // TODO(jialiul): For now, SafeBrowsingNavigationObserverManager also listens to | |
| 28 // NOTIFICATION_RETARGETING as a way to detect cross frame/tab navigation. | |
| 29 // Remove base class content::NotificationObserver when | |
| 30 // WebContentsObserver::DidOpenRequestedURL() covers all retargeting cases. | |
| 31 class SafeBrowsingNavigationObserverManager | |
| 32 : public content::NotificationObserver, | |
| 33 public base::RefCountedThreadSafe<SafeBrowsingNavigationObserverManager> { | |
| 34 public: | |
| 35 // Helper function to check if user gesture is older than | |
| 36 // kUserGestureTTLInSecond; | |
| 37 static bool IsUserGestureExpired(const base::Time& timestamp); | |
| 38 // Helper function to strip empty ref fragment from a URL. | |
| 39 static GURL ClearEmptyURLRef(const GURL& url); | |
|
Charlie Reis
2016/10/17 23:54:04
nit: ClearEmptyRef
Jialiu Lin
2016/10/18 19:06:05
Done.
| |
| 40 | |
| 41 SafeBrowsingNavigationObserverManager(); | |
| 42 | |
| 43 // Add |nav_event| to |navigation_map_| based on |nav_event_key|. Object | |
| 44 // pointed by |nav_event| will be no longer accessible after this function. | |
| 45 void RecordNavigationEvent(const GURL& nav_event_key, | |
| 46 NavigationEvent* nav_event); | |
| 47 void RecordUserGestureForWebContents(content::WebContents* web_contents, | |
| 48 const base::Time& timestamp); | |
| 49 void OnUserGestureConsumed(content::WebContents* web_contents, | |
| 50 const base::Time& timestamp); | |
| 51 void RecordHostToIpMapping(const std::string& host, const std::string& ip); | |
| 52 // Clean-ups need to be done when a WebContents gets destroyed. | |
| 53 void OnWebContentDestroyed(content::WebContents* web_contents); | |
| 54 | |
| 55 // TODO(jialiul): more functions are coming for managing navigation_map_. | |
| 56 | |
| 57 private: | |
| 58 friend class base::RefCountedThreadSafe< | |
| 59 SafeBrowsingNavigationObserverManager>; | |
| 60 friend class TestNavigationObserverManager; | |
| 61 friend class SBNavigationObserverBrowserTest; | |
| 62 friend class SBNavigationObserverTest; | |
| 63 | |
| 64 struct GurlHash { | |
| 65 std::size_t operator()(const GURL& url) const { | |
| 66 return std::hash<std::string>()(url.spec()); | |
| 67 } | |
| 68 }; | |
| 69 | |
| 70 typedef std::unordered_map<GURL, std::vector<NavigationEvent>, GurlHash> | |
| 71 NavigationMap; | |
| 72 typedef std::unordered_map<content::WebContents*, base::Time> UserGestureMap; | |
| 73 typedef std::unordered_map<std::string, std::vector<ResolvedIPAddress>> | |
| 74 HostToIpMap; | |
| 75 | |
| 76 ~SafeBrowsingNavigationObserverManager() override; | |
| 77 | |
| 78 // content::NotificationObserver: | |
| 79 void Observe(int type, | |
| 80 const content::NotificationSource& source, | |
| 81 const content::NotificationDetails& details) override; | |
| 82 | |
| 83 void RecordRetargeting(const content::NotificationDetails& details); | |
| 84 | |
| 85 NavigationMap* navigation_map() { return &navigation_map_; } | |
| 86 | |
| 87 HostToIpMap* host_to_ip_map() { return &host_to_ip_map_; } | |
| 88 | |
| 89 // navigation_map_ keeps track of all the observed navigations. This map is | |
| 90 // keyed on the resolved request url. In other words, in case of server | |
| 91 // redirects, its key is the last server redirect url, otherwise, it is the | |
| 92 // original target url. Since the same url can be requested multiple times | |
| 93 // across different tabs and frames, the value of this map is a vector of | |
| 94 // NavigationEvent ordered by navigation finish time. | |
| 95 // TODO(jialiul): Entries in navigation_map_ will be removed if they are older | |
| 96 // than 2 minutes since their corresponding navigations finish. | |
| 97 NavigationMap navigation_map_; | |
| 98 | |
| 99 // user_gesture_map_ keeps track of the timestamp of last user gesture in | |
| 100 // in each WebContents. We assume for majority of cases, a navigation | |
| 101 // shortly after a user gesture indicate this navigation is user initiated. | |
| 102 UserGestureMap user_gesture_map_; | |
| 103 | |
| 104 // Host to timestamped IP addresses map that covers all the main frame and | |
| 105 // sub frame URLs' hosts. Since it is possible for a host to resolve to more | |
| 106 // than one IP in even a short period of time, we map a single host to a | |
| 107 // vector of ResolvedIPAddresss. This map is used to fill in ip_address field | |
| 108 // in URLChainEntry in ClientDownlodRequest. | |
|
Charlie Reis
2016/10/17 23:54:04
nit: ClientDownloadRequest
Jialiu Lin
2016/10/18 19:06:05
Done.
| |
| 109 HostToIpMap host_to_ip_map_; | |
|
Charlie Reis
2016/10/17 23:54:04
It seems a bit complex/unfortunate to have to trac
Jialiu Lin
2016/10/18 19:06:05
SB backend wants this info to see :(1) if website
Charlie Reis
2016/10/19 17:10:45
Acknowledged.
| |
| 110 | |
| 111 content::NotificationRegistrar registrar_; | |
|
Charlie Reis
2016/10/17 23:54:04
nti: Add blank line after.
Jialiu Lin
2016/10/18 19:06:05
Done.
| |
| 112 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingNavigationObserverManager); | |
| 113 }; | |
| 114 } // namespace safe_browsing | |
| 115 | |
| 116 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_NAVIGATION_OBSERVER_MANAGE R_H_ | |
| OLD | NEW |